xref: /aosp_15_r20/external/boringssl/src/crypto/fipsmodule/bcm.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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 #if !defined(OPENSSL_ASAN)
172   // Integrity tests cannot run under ASAN because it involves reading the full
173   // .text section, which triggers the global-buffer overflow detection.
174   if (!BORINGSSL_integrity_test()) {
175     goto err;
176   }
177 #endif  // OPENSSL_ASAN
178 
179   if (!boringssl_self_test_startup()) {
180     goto err;
181   }
182 
183   return;
184 
185 err:
186   BORINGSSL_FIPS_abort();
187 }
188 
189 #if !defined(OPENSSL_ASAN)
BORINGSSL_integrity_test(void)190 int BORINGSSL_integrity_test(void) {
191   const uint8_t *const start = BORINGSSL_bcm_text_start;
192   const uint8_t *const end = BORINGSSL_bcm_text_end;
193 
194   assert_within(start, AES_encrypt, end);
195   assert_within(start, RSA_sign, end);
196   assert_within(start, RAND_bytes, end);
197   assert_within(start, EC_GROUP_cmp, end);
198   assert_within(start, SHA256_Update, end);
199   assert_within(start, ECDSA_do_verify, end);
200   assert_within(start, EVP_AEAD_CTX_seal, end);
201 
202 #if defined(BORINGSSL_SHARED_LIBRARY)
203   const uint8_t *const rodata_start = BORINGSSL_bcm_rodata_start;
204   const uint8_t *const rodata_end = BORINGSSL_bcm_rodata_end;
205 #else
206   // In the static build, read-only data is placed within the .text segment.
207   const uint8_t *const rodata_start = BORINGSSL_bcm_text_start;
208   const uint8_t *const rodata_end = BORINGSSL_bcm_text_end;
209 #endif
210 
211   assert_within(rodata_start, kPrimes, rodata_end);
212   assert_within(rodata_start, kP256Field, rodata_end);
213   assert_within(rodata_start, kPKCS1SigPrefixes, rodata_end);
214 
215   uint8_t result[SHA256_DIGEST_LENGTH];
216   const EVP_MD *const kHashFunction = EVP_sha256();
217   if (!boringssl_self_test_sha256() ||
218       !boringssl_self_test_hmac_sha256()) {
219     return 0;
220   }
221 
222   static const uint8_t kHMACKey[64] = {0};
223   unsigned result_len;
224   HMAC_CTX hmac_ctx;
225   HMAC_CTX_init(&hmac_ctx);
226   if (!HMAC_Init_ex(&hmac_ctx, kHMACKey, sizeof(kHMACKey), kHashFunction,
227                     NULL /* no ENGINE */)) {
228     fprintf(stderr, "HMAC_Init_ex failed.\n");
229     return 0;
230   }
231 
232   BORINGSSL_maybe_set_module_text_permissions(PROT_READ | PROT_EXEC);
233 #if defined(BORINGSSL_SHARED_LIBRARY)
234   uint64_t length = end - start;
235   HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
236   HMAC_Update(&hmac_ctx, start, length);
237 
238   length = rodata_end - rodata_start;
239   HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
240   HMAC_Update(&hmac_ctx, rodata_start, length);
241 #else
242   HMAC_Update(&hmac_ctx, start, end - start);
243 #endif
244   BORINGSSL_maybe_set_module_text_permissions(PROT_EXEC);
245 
246   if (!HMAC_Final(&hmac_ctx, result, &result_len) ||
247       result_len != sizeof(result)) {
248     fprintf(stderr, "HMAC failed.\n");
249     return 0;
250   }
251   HMAC_CTX_cleanse(&hmac_ctx); // FIPS 140-3, AS05.10.
252 
253   const uint8_t *expected = BORINGSSL_bcm_text_hash;
254 
255   if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
256 #if !defined(BORINGSSL_FIPS_BREAK_TESTS)
257     return 0;
258 #endif
259   }
260 
261   OPENSSL_cleanse(result, sizeof(result)); // FIPS 140-3, AS05.10.
262   return 1;
263 }
264 
FIPS_module_hash(void)265 const uint8_t* FIPS_module_hash(void) {
266   return BORINGSSL_bcm_text_hash;
267 }
268 
269 #endif  // OPENSSL_ASAN
270 
BORINGSSL_FIPS_abort(void)271 void BORINGSSL_FIPS_abort(void) {
272   for (;;) {
273     abort();
274     exit(1);
275   }
276 }
277 
278 #endif  // BORINGSSL_FIPS
279