Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / boringssl / crypto / fipsmodule / bcm.c
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #if !defined(_GNU_SOURCE)
16 #define _GNU_SOURCE  // needed for syscall() on Linux.
17 #endif
18
19 #include <openssl/crypto.h>
20
21 #include <stdlib.h>
22
23 #include <openssl/digest.h>
24 #include <openssl/hmac.h>
25 #include <openssl/sha.h>
26
27 #include "../internal.h"
28
29 #include "aes/aes.c"
30 #include "aes/key_wrap.c"
31 #include "aes/mode_wrappers.c"
32 #include "bn/add.c"
33 #include "bn/asm/x86_64-gcc.c"
34 #include "bn/bn.c"
35 #include "bn/bytes.c"
36 #include "bn/cmp.c"
37 #include "bn/ctx.c"
38 #include "bn/div.c"
39 #include "bn/exponentiation.c"
40 #include "bn/gcd.c"
41 #include "bn/generic.c"
42 #include "bn/jacobi.c"
43 #include "bn/montgomery.c"
44 #include "bn/montgomery_inv.c"
45 #include "bn/mul.c"
46 #include "bn/prime.c"
47 #include "bn/random.c"
48 #include "bn/rsaz_exp.c"
49 #include "bn/shift.c"
50 #include "bn/sqrt.c"
51 #include "cipher/aead.c"
52 #include "cipher/cipher.c"
53 #include "cipher/e_aes.c"
54 #include "cipher/e_des.c"
55 #include "des/des.c"
56 #include "digest/digest.c"
57 #include "digest/digests.c"
58 #include "ecdsa/ecdsa.c"
59 #include "ec/ec.c"
60 #include "ec/ec_key.c"
61 #include "ec/ec_montgomery.c"
62 #include "ec/oct.c"
63 #include "ec/p224-64.c"
64 #include "../../third_party/fiat/p256.c"
65 #include "ec/p256-x86_64.c"
66 #include "ec/simple.c"
67 #include "ec/util.c"
68 #include "ec/wnaf.c"
69 #include "hmac/hmac.c"
70 #include "md4/md4.c"
71 #include "md5/md5.c"
72 #include "modes/cbc.c"
73 #include "modes/ccm.c"
74 #include "modes/cfb.c"
75 #include "modes/ctr.c"
76 #include "modes/gcm.c"
77 #include "modes/ofb.c"
78 #include "modes/polyval.c"
79 #include "rand/ctrdrbg.c"
80 #include "rand/rand.c"
81 #include "rand/urandom.c"
82 #include "rsa/blinding.c"
83 #include "rsa/padding.c"
84 #include "rsa/rsa.c"
85 #include "rsa/rsa_impl.c"
86 #include "self_check/self_check.c"
87 #include "sha/sha1-altivec.c"
88 #include "sha/sha1.c"
89 #include "sha/sha256.c"
90 #include "sha/sha512.c"
91 #include "tls/kdf.c"
92
93
94 #if defined(BORINGSSL_FIPS)
95
96 #if !defined(OPENSSL_ASAN)
97 // These symbols are filled in by delocate.go. They point to the start and end
98 // of the module, and the location of the integrity hash, respectively.
99 extern const uint8_t BORINGSSL_bcm_text_start[];
100 extern const uint8_t BORINGSSL_bcm_text_end[];
101 extern const uint8_t BORINGSSL_bcm_text_hash[];
102 #endif
103
104 static void __attribute__((constructor))
105 BORINGSSL_bcm_power_on_self_test(void) {
106   CRYPTO_library_init();
107
108 #if !defined(OPENSSL_ASAN)
109   // Integrity tests cannot run under ASAN because it involves reading the full
110   // .text section, which triggers the global-buffer overflow detection.
111   const uint8_t *const start = BORINGSSL_bcm_text_start;
112   const uint8_t *const end = BORINGSSL_bcm_text_end;
113
114   static const uint8_t kHMACKey[64] = {0};
115   uint8_t result[SHA512_DIGEST_LENGTH];
116
117   unsigned result_len;
118   if (!HMAC(EVP_sha512(), kHMACKey, sizeof(kHMACKey), start, end - start,
119             result, &result_len) ||
120       result_len != sizeof(result)) {
121     goto err;
122   }
123
124   const uint8_t *expected = BORINGSSL_bcm_text_hash;
125
126   if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
127     goto err;
128   }
129 #endif
130
131   if (!BORINGSSL_self_test()) {
132     goto err;
133   }
134
135   return;
136
137 err:
138   BORINGSSL_FIPS_abort();
139 }
140
141 void BORINGSSL_FIPS_abort(void) {
142   for (;;) {
143     abort();
144     exit(1);
145   }
146 }
147
148 #endif  // BORINGSSL_FIPS