xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/fipsmodule/bcm.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #if defined(BORINGSSL_FIPS)
23 #include <sys/mman.h>
24 #include <unistd.h>
25 #endif
26 
27 #include <openssl/digest.h>
28 #include <openssl/hmac.h>
29 #include <openssl/sha.h>
30 
31 #include "../internal.h"
32 
33 #include "aes/aes.c"
34 #include "aes/aes_nohw.c"
35 #include "aes/key_wrap.c"
36 #include "aes/mode_wrappers.c"
37 #include "bn/add.c"
38 #include "bn/asm/x86_64-gcc.c"
39 #include "bn/bn.c"
40 #include "bn/bytes.c"
41 #include "bn/cmp.c"
42 #include "bn/ctx.c"
43 #include "bn/div.c"
44 #include "bn/div_extra.c"
45 #include "bn/exponentiation.c"
46 #include "bn/gcd.c"
47 #include "bn/gcd_extra.c"
48 #include "bn/generic.c"
49 #include "bn/jacobi.c"
50 #include "bn/montgomery.c"
51 #include "bn/montgomery_inv.c"
52 #include "bn/mul.c"
53 #include "bn/prime.c"
54 #include "bn/random.c"
55 #include "bn/rsaz_exp.c"
56 #include "bn/shift.c"
57 #include "bn/sqrt.c"
58 #include "cipher/aead.c"
59 #include "cipher/cipher.c"
60 #include "cipher/e_aes.c"
61 #include "cipher/e_aesccm.c"
62 #include "cmac/cmac.c"
63 #include "dh/check.c"
64 #include "dh/dh.c"
65 #include "digest/digest.c"
66 #include "digest/digests.c"
67 #include "digestsign/digestsign.c"
68 #include "ecdh/ecdh.c"
69 #include "ecdsa/ecdsa.c"
70 #include "ec/ec.c"
71 #include "ec/ec_key.c"
72 #include "ec/ec_montgomery.c"
73 #include "ec/felem.c"
74 #include "ec/oct.c"
75 #include "ec/p224-64.c"
76 #include "ec/p256.c"
77 #include "ec/p256-nistz.c"
78 #include "ec/scalar.c"
79 #include "ec/simple.c"
80 #include "ec/simple_mul.c"
81 #include "ec/util.c"
82 #include "ec/wnaf.c"
83 #include "hkdf/hkdf.c"
84 #include "hmac/hmac.c"
85 #include "md4/md4.c"
86 #include "md5/md5.c"
87 #include "modes/cbc.c"
88 #include "modes/cfb.c"
89 #include "modes/ctr.c"
90 #include "modes/gcm.c"
91 #include "modes/gcm_nohw.c"
92 #include "modes/ofb.c"
93 #include "modes/polyval.c"
94 #include "rand/ctrdrbg.c"
95 #include "rand/fork_detect.c"
96 #include "rand/rand.c"
97 #include "rand/urandom.c"
98 #include "rsa/blinding.c"
99 #include "rsa/padding.c"
100 #include "rsa/rsa.c"
101 #include "rsa/rsa_impl.c"
102 #include "self_check/fips.c"
103 #include "self_check/self_check.c"
104 #include "service_indicator/service_indicator.c"
105 #include "sha/sha1.c"
106 #include "sha/sha256.c"
107 #include "sha/sha512.c"
108 #include "tls/kdf.c"
109 
110 
111 #if defined(BORINGSSL_FIPS)
112 
113 #if !defined(OPENSSL_ASAN)
114 
115 // These symbols are filled in by delocate.go (in static builds) or a linker
116 // script (in shared builds). They point to the start and end of the module, and
117 // the location of the integrity hash, respectively.
118 extern const uint8_t BORINGSSL_bcm_text_start[];
119 extern const uint8_t BORINGSSL_bcm_text_end[];
120 extern const uint8_t BORINGSSL_bcm_text_hash[];
121 #if defined(BORINGSSL_SHARED_LIBRARY)
122 extern const uint8_t BORINGSSL_bcm_rodata_start[];
123 extern const uint8_t BORINGSSL_bcm_rodata_end[];
124 #endif
125 
126 // assert_within is used to sanity check that certain symbols are within the
127 // bounds of the integrity check. It checks that start <= symbol < end and
128 // aborts otherwise.
assert_within(const void * start,const void * symbol,const void * end)129 static void assert_within(const void *start, const void *symbol,
130                           const void *end) {
131   const uintptr_t start_val = (uintptr_t) start;
132   const uintptr_t symbol_val = (uintptr_t) symbol;
133   const uintptr_t end_val = (uintptr_t) end;
134 
135   if (start_val <= symbol_val && symbol_val < end_val) {
136     return;
137   }
138 
139   fprintf(
140       stderr,
141       "FIPS module doesn't span expected symbol. Expected %p <= %p < %p\n",
142       start, symbol, end);
143   BORINGSSL_FIPS_abort();
144 }
145 
146 #if defined(OPENSSL_ANDROID) && defined(OPENSSL_AARCH64)
BORINGSSL_maybe_set_module_text_permissions(int permission)147 static void BORINGSSL_maybe_set_module_text_permissions(int permission) {
148   // Android may be compiled in execute-only-memory mode, in which case the
149   // .text segment cannot be read. That conflicts with the need for a FIPS
150   // module to hash its own contents, therefore |mprotect| is used to make
151   // the module's .text readable for the duration of the hashing process. In
152   // other build configurations this is a no-op.
153   const uintptr_t page_size = getpagesize();
154   const uintptr_t page_start =
155       ((uintptr_t)BORINGSSL_bcm_text_start) & ~(page_size - 1);
156 
157   if (mprotect((void *)page_start,
158                ((uintptr_t)BORINGSSL_bcm_text_end) - page_start,
159                permission) != 0) {
160     perror("BoringSSL: mprotect");
161   }
162 }
163 #else
BORINGSSL_maybe_set_module_text_permissions(int permission)164 static void BORINGSSL_maybe_set_module_text_permissions(int permission) {}
165 #endif  // !ANDROID
166 
167 #endif  // !ASAN
168 
169 static void __attribute__((constructor))
BORINGSSL_bcm_power_on_self_test(void)170 BORINGSSL_bcm_power_on_self_test(void) {
171   CRYPTO_library_init();
172 
173 #if !defined(OPENSSL_ASAN)
174   // Integrity tests cannot run under ASAN because it involves reading the full
175   // .text section, which triggers the global-buffer overflow detection.
176   if (!BORINGSSL_integrity_test()) {
177     goto err;
178   }
179 #endif  // OPENSSL_ASAN
180 
181   if (!boringssl_self_test_startup()) {
182     goto err;
183   }
184 
185   return;
186 
187 err:
188   BORINGSSL_FIPS_abort();
189 }
190 
191 #if !defined(OPENSSL_ASAN)
BORINGSSL_integrity_test(void)192 int BORINGSSL_integrity_test(void) {
193   const uint8_t *const start = BORINGSSL_bcm_text_start;
194   const uint8_t *const end = BORINGSSL_bcm_text_end;
195 
196   assert_within(start, AES_encrypt, end);
197   assert_within(start, RSA_sign, end);
198   assert_within(start, RAND_bytes, end);
199   assert_within(start, EC_GROUP_cmp, end);
200   assert_within(start, SHA256_Update, end);
201   assert_within(start, ECDSA_do_verify, end);
202   assert_within(start, EVP_AEAD_CTX_seal, end);
203 
204 #if defined(BORINGSSL_SHARED_LIBRARY)
205   const uint8_t *const rodata_start = BORINGSSL_bcm_rodata_start;
206   const uint8_t *const rodata_end = BORINGSSL_bcm_rodata_end;
207 #else
208   // In the static build, read-only data is placed within the .text segment.
209   const uint8_t *const rodata_start = BORINGSSL_bcm_text_start;
210   const uint8_t *const rodata_end = BORINGSSL_bcm_text_end;
211 #endif
212 
213   assert_within(rodata_start, kPrimes, rodata_end);
214   assert_within(rodata_start, kP256Field, rodata_end);
215   assert_within(rodata_start, kPKCS1SigPrefixes, rodata_end);
216 
217   uint8_t result[SHA256_DIGEST_LENGTH];
218   const EVP_MD *const kHashFunction = EVP_sha256();
219   if (!boringssl_self_test_sha256() ||
220       !boringssl_self_test_hmac_sha256()) {
221     return 0;
222   }
223 
224   static const uint8_t kHMACKey[64] = {0};
225   unsigned result_len;
226   HMAC_CTX hmac_ctx;
227   HMAC_CTX_init(&hmac_ctx);
228   if (!HMAC_Init_ex(&hmac_ctx, kHMACKey, sizeof(kHMACKey), kHashFunction,
229                     NULL /* no ENGINE */)) {
230     fprintf(stderr, "HMAC_Init_ex failed.\n");
231     return 0;
232   }
233 
234   BORINGSSL_maybe_set_module_text_permissions(PROT_READ | PROT_EXEC);
235 #if defined(BORINGSSL_SHARED_LIBRARY)
236   uint64_t length = end - start;
237   HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
238   HMAC_Update(&hmac_ctx, start, length);
239 
240   length = rodata_end - rodata_start;
241   HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
242   HMAC_Update(&hmac_ctx, rodata_start, length);
243 #else
244   HMAC_Update(&hmac_ctx, start, end - start);
245 #endif
246   BORINGSSL_maybe_set_module_text_permissions(PROT_EXEC);
247 
248   if (!HMAC_Final(&hmac_ctx, result, &result_len) ||
249       result_len != sizeof(result)) {
250     fprintf(stderr, "HMAC failed.\n");
251     return 0;
252   }
253   HMAC_CTX_cleanse(&hmac_ctx); // FIPS 140-3, AS05.10.
254 
255   const uint8_t *expected = BORINGSSL_bcm_text_hash;
256 
257   if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
258 #if !defined(BORINGSSL_FIPS_BREAK_TESTS)
259     return 0;
260 #endif
261   }
262 
263   OPENSSL_cleanse(result, sizeof(result)); // FIPS 140-3, AS05.10.
264   return 1;
265 }
266 #endif  // OPENSSL_ASAN
267 
BORINGSSL_FIPS_abort(void)268 void BORINGSSL_FIPS_abort(void) {
269   for (;;) {
270     abort();
271     exit(1);
272   }
273 }
274 
275 #endif  // BORINGSSL_FIPS
276