Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bcm.c

raw
1 // Copyright (c) 2017, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #if !defined(_GNU_SOURCE)
5 #define _GNU_SOURCE // needed for syscall() on Linux.
6 #endif
7
8 #include <openssl/crypto.h>
9
10 #include <stdlib.h>
11 #if defined(BORINGSSL_FIPS) && !defined(OPENSSL_WINDOWS)
12 #include <sys/mman.h>
13 #include <unistd.h>
14 #endif
15
16 // On Windows place the bcm code in a specific section that uses Grouped Sections
17 // to control the order. $b section will place bcm in between the start/end markers
18 // which are in $a and $z.
19 #if defined(BORINGSSL_FIPS) && defined(OPENSSL_WINDOWS)
20 #pragma code_seg(".fipstx$b")
21 #pragma data_seg(".fipsda$b")
22 #pragma const_seg(".fipsco$b")
23 #pragma bss_seg(".fipsbs$b")
24 // Explicitly declare the FIPS rodata section with correct attributes. This
25 // ensures the section is known to the compiler/linker even if #pragma const_seg
26 // is not fully supported (e.g. clang-cl on ARM64).
27 #pragma section(".fipsco$b", read)
28 #endif
29
30 #include <openssl/chacha.h>
31 #include <openssl/digest.h>
32 #include <openssl/hmac.h>
33 #include <openssl/sha.h>
34
35 #include "../internal.h"
36
37 #include "aes/aes.c"
38 #include "aes/aes_nohw.c"
39 #include "aes/key_wrap.c"
40 #include "aes/mode_wrappers.c"
41 #include "bn/add.c"
42 #include "bn/asm/x86_64-gcc.c"
43 #include "bn/bn.c"
44 #include "bn/bytes.c"
45 #include "bn/cmp.c"
46 #include "bn/ctx.c"
47 #include "bn/div.c"
48 #include "bn/div_extra.c"
49 #include "bn/exponentiation.c"
50 #include "bn/gcd.c"
51 #include "bn/gcd_extra.c"
52 #include "bn/generic.c"
53 #include "bn/jacobi.c"
54 #include "bn/montgomery.c"
55 #include "bn/montgomery_inv.c"
56 #include "bn/mul.c"
57 #include "bn/prime.c"
58 #include "bn/random.c"
59 #include "bn/rsaz_exp.c"
60 #include "bn/rsaz_exp_x2.c"
61 #include "bn/shift.c"
62 #include "bn/sqrt.c"
63 #include "cipher/aead.c"
64 #include "cipher/cipher.c"
65 #include "cipher/e_aes.c"
66 #include "cipher/e_aesccm.c"
67
68 #include "cpucap/internal.h"
69 #include "cpucap/cpu_aarch64.c"
70 #include "cpucap/cpu_aarch64_sysreg.c"
71 #include "cpucap/cpu_aarch64_apple.c"
72 #include "cpucap/cpu_aarch64_freebsd.c"
73 #include "cpucap/cpu_aarch64_linux.c"
74 #include "cpucap/cpu_aarch64_netbsd.c"
75 #include "cpucap/cpu_aarch64_openbsd.c"
76 #include "cpucap/cpu_aarch64_win.c"
77 #include "cpucap/cpu_arm_freebsd.c"
78 #include "cpucap/cpu_arm_linux.c"
79 #include "cpucap/cpu_intel.c"
80 #include "cpucap/cpu_ppc64le.c"
81
82 #include "cmac/cmac.c"
83 #include "curve25519/curve25519.c"
84 #include "curve25519/curve25519_nohw.c"
85 #include "curve25519/curve25519_s2n_bignum_asm.c"
86 #include "dh/check.c"
87 #include "dh/dh.c"
88 #include "digest/digest.c"
89 #include "digest/digests.c"
90 #include "ecdh/ecdh.c"
91 #include "ecdsa/ecdsa.c"
92 #include "ec/ec.c"
93 #include "ec/ec_key.c"
94 #include "ec/ec_montgomery.c"
95 #include "ec/ec_nistp.c"
96 #include "ec/felem.c"
97 #include "ec/oct.c"
98 #include "ec/p224-64.c"
99 #include "ec/p256.c"
100 #include "ec/p256-nistz.c"
101 #include "ec/p384.c"
102 #include "ec/p521.c"
103 #include "ec/scalar.c"
104 #include "ec/simple.c"
105 #include "ec/simple_mul.c"
106 #include "ec/util.c"
107 #include "ec/wnaf.c"
108 #include "evp/digestsign.c"
109 #include "evp/evp.c"
110 #include "evp/evp_ctx.c"
111 #include "evp/p_ec.c"
112 #include "evp/p_ed25519.c"
113 #include "evp/p_ed25519ph.c"
114 #include "evp/p_hkdf.c"
115 #include "evp/p_hmac.c"
116 #include "evp/p_kem.c"
117 #include "evp/p_pqdsa.c"
118 #include "evp/p_rsa.c"
119 #include "hkdf/hkdf.c"
120 #include "hmac/hmac.c"
121 #include "kdf/kbkdf.c"
122 #include "kdf/sskdf.c"
123 #include "kem/kem.c"
124 #include "md5/md5.c"
125 #include "ml_dsa/ml_dsa.c"
126 #include "ml_kem/ml_kem.c"
127 #include "modes/cbc.c"
128 #include "modes/cfb.c"
129 #include "modes/ctr.c"
130 #include "modes/gcm.c"
131 #include "modes/gcm_nohw.c"
132 #include "modes/ofb.c"
133 #include "modes/xts.c"
134 #include "modes/polyval.c"
135 #include "pbkdf/pbkdf.c"
136 #include "pqdsa/pqdsa.c"
137 #include "rand/ctrdrbg.c"
138 #include "rand/rand.c"
139 #include "rand/entropy/entropy_sources.c"
140 #include "rand/entropy/tree_drbg_jitter_entropy.c"
141 #include "rsa/blinding.c"
142 #include "rsa/padding.c"
143 #include "rsa/rsa.c"
144 #include "rsa/rsa_impl.c"
145 #include "self_check/fips.c"
146 #include "self_check/self_check.c"
147 #include "service_indicator/service_indicator.c"
148 #include "sha/keccak1600.c"
149 #include "sha/sha1-altivec.c"
150 #include "sha/sha1.c"
151 #include "sha/sha256.c"
152 #include "sha/sha3.c"
153 #include "sha/sha512.c"
154 #include "sshkdf/sshkdf.c"
155 #include "tls/kdf.c"
156
157 #if defined(BORINGSSL_FIPS)
158
159 #if !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN)
160
161 static const void* function_entry_ptr(const void* func_sym) {
162 #if defined(OPENSSL_PPC64BE)
163 // Function pointers on ppc64be point to a function descriptor.
164 // https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#FUNC-ADDRESS
165 return (const void*)(((uint64_t *)func_sym)[0]);
166 #else
167 return (const void*)func_sym;
168 #endif
169 }
170
171 // These symbols are filled in by delocate.go (in static builds) or a linker
172 // script (in shared builds). They point to the start and end of the module, and
173 // the location of the integrity hash, respectively.
174 extern const uint8_t BORINGSSL_bcm_text_start[];
175 extern const uint8_t BORINGSSL_bcm_text_end[];
176 extern const uint8_t BORINGSSL_bcm_text_hash[];
177 #if defined(BORINGSSL_SHARED_LIBRARY)
178 extern const uint8_t BORINGSSL_bcm_rodata_start[];
179 extern const uint8_t BORINGSSL_bcm_rodata_end[];
180 #endif
181
182 #define STRING_POINTER_LENGTH 18
183 #define MAX_FUNCTION_NAME 32
184 #define ASSERT_WITHIN_MSG "FIPS module doesn't span expected symbol (%s). Expected %p <= %p < %p\n"
185 #define MAX_WITHIN_MSG_LEN sizeof(ASSERT_WITHIN_MSG) + (3 * STRING_POINTER_LENGTH) + MAX_FUNCTION_NAME
186 #define ASSERT_OUTSIDE_MSG "FIPS module spans unexpected symbol (%s), expected %p < %p || %p > %p\n"
187 #define MAX_OUTSIDE_MSG_LEN sizeof(ASSERT_OUTSIDE_MSG) + (4 * STRING_POINTER_LENGTH) + MAX_FUNCTION_NAME
188 // assert_within is used to sanity check that certain symbols are within the
189 // bounds of the integrity check. It checks that start <= symbol < end and
190 // aborts otherwise.
191 static void assert_within(const void *start, const void *symbol,
192 const char *symbol_name, const void *end) {
193 const uintptr_t start_val = (uintptr_t) start;
194 const uintptr_t symbol_val = (uintptr_t) symbol;
195 const uintptr_t end_val = (uintptr_t) end;
196
197 if (start_val <= symbol_val && symbol_val < end_val) {
198 return;
199 }
200
201 assert(strlen(symbol_name) < MAX_FUNCTION_NAME);
202 char message[MAX_WITHIN_MSG_LEN] = {0};
203 snprintf(message, sizeof(message), ASSERT_WITHIN_MSG, symbol_name, start, symbol, end);
204 AWS_LC_FIPS_failure(message);
205 }
206
207 static void assert_not_within(const void *start, const void *symbol,
208 const char *symbol_name, const void *end) {
209 const uintptr_t start_val = (uintptr_t) start;
210 const uintptr_t symbol_val = (uintptr_t) symbol;
211 const uintptr_t end_val = (uintptr_t) end;
212
213 if (start_val >= symbol_val || symbol_val > end_val) {
214 return;
215 }
216
217 assert(strlen(symbol_name) < MAX_FUNCTION_NAME);
218 char message[MAX_OUTSIDE_MSG_LEN] = {0};
219 snprintf(message, sizeof(message), ASSERT_OUTSIDE_MSG, symbol_name, symbol, start, symbol, end);
220 AWS_LC_FIPS_failure(message);
221 }
222
223 // TODO: Re-enable once all data has been moved out of .text segments CryptoAlg-2360
224 #if 0
225 //#if defined(OPENSSL_ANDROID) && defined(OPENSSL_AARCH64)
226 static void BORINGSSL_maybe_set_module_text_permissions(int permission) {
227 // Android may be compiled in execute-only-memory mode, in which case the
228 // .text segment cannot be read. That conflicts with the need for a FIPS
229 // module to hash its own contents, therefore |mprotect| is used to make
230 // the module's .text readable for the duration of the hashing process. In
231 // other build configurations this is a no-op.
232 const uintptr_t page_size = getpagesize();
233 const uintptr_t page_start =
234 ((uintptr_t)BORINGSSL_bcm_text_start) & ~(page_size - 1);
235
236 if (mprotect((void *)page_start,
237 ((uintptr_t)BORINGSSL_bcm_text_end) - page_start,
238 permission) != 0) {
239 perror("BoringSSL: mprotect");
240 }
241 }
242 #elif !defined(OPENSSL_WINDOWS)
243 static void BORINGSSL_maybe_set_module_text_permissions(int _permission) {}
244 #endif // !ANDROID
245
246 #endif // !ASAN && !MSAN
247
248 #if defined(AWSLC_FIPS_FAILURE_CALLBACK)
249 #if defined(__ELF__) && defined(__GNUC__)
250 WEAK_SYMBOL_FUNC(void, AWS_LC_fips_failure_callback, (const char* message))
251 #else
252 #error AWSLC_FIPS_FAILURE_CALLBACK not supported on this platform
253 #endif
254 #endif
255
256 #if defined(_MSC_VER)
257 #pragma section(".CRT$XCU", read)
258 static void BORINGSSL_bcm_power_on_self_test(void);
259 __declspec(allocate(".CRT$XCU")) void(*fips_library_init_constructor)(void) =
260 BORINGSSL_bcm_power_on_self_test;
261 #else
262 static void BORINGSSL_bcm_power_on_self_test(void) __attribute__ ((constructor));
263 #endif
264
265 static void BORINGSSL_bcm_power_on_self_test(void) {
266 #if defined(HAS_OPENSSL_CPUID_SETUP) && !defined(OPENSSL_NO_ASM)
267 OPENSSL_cpuid_setup();
268 #endif
269
270 if (jent_entropy_init()) {
271 AWS_LC_FIPS_failure("CPU Jitter entropy RNG initialization failed");
272 }
273
274 #if !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN)
275 // Integrity tests cannot run under ASAN or MSAN because it involves reading
276 // the full .text section, which triggers the global-buffer overflow detection
277 // (ASAN) or use of uninstrumented code (MSAN).
278 if (!BORINGSSL_integrity_test()) {
279 AWS_LC_FIPS_failure("Integrity test failed");
280 }
281 #endif // !ASAN && !MSAN
282
283 if (!boringssl_self_test_startup()) {
284 AWS_LC_FIPS_failure("Power on self test failed");
285 }
286 }
287
288 #if !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN)
289 int BORINGSSL_integrity_test(void) {
290 const uint8_t *const start = BORINGSSL_bcm_text_start;
291 const uint8_t *const end = BORINGSSL_bcm_text_end;
292
293 assert_within(start, function_entry_ptr(AES_encrypt), "AES_encrypt", end);
294 assert_within(start, function_entry_ptr(RSA_sign), "RSA_sign", end);
295 assert_within(start, function_entry_ptr(RAND_bytes), "RAND_bytes", end);
296 assert_within(start, function_entry_ptr(EC_GROUP_cmp), "EC_GROUP_cmp", end);
297 assert_within(start, function_entry_ptr(SHA256_Update), "SHA256_Update", end);
298 assert_within(start, function_entry_ptr(ECDSA_do_verify), "ECDSA_do_verify", end);
299 assert_within(start, function_entry_ptr(EVP_AEAD_CTX_seal), "EVP_AEAD_CTX_seal", end);
300 assert_not_within(start, function_entry_ptr(OPENSSL_cleanse), "OPENSSL_cleanse", end);
301 assert_not_within(start, function_entry_ptr(CRYPTO_chacha_20), "CRYPTO_chacha_20", end);
302 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
303 assert_not_within(start, OPENSSL_ia32cap_P, "OPENSSL_ia32cap_P", end);
304 #elif defined(OPENSSL_AARCH64)
305 assert_not_within(start, &OPENSSL_armcap_P, "OPENSSL_armcap_P", end);
306 #endif
307
308 #if defined(BORINGSSL_SHARED_LIBRARY)
309 const uint8_t *const rodata_start = BORINGSSL_bcm_rodata_start;
310 const uint8_t *const rodata_end = BORINGSSL_bcm_rodata_end;
311 #else
312 // In the static build, read-only data is placed within the .text segment.
313 const uint8_t *const rodata_start = BORINGSSL_bcm_text_start;
314 const uint8_t *const rodata_end = BORINGSSL_bcm_text_end;
315 #endif
316
317 assert_within(rodata_start, kPrimes, "kPrimes", rodata_end);
318 assert_within(rodata_start, kP256Field, "kP256Field", rodata_end);
319 assert_within(rodata_start, kPKCS1SigPrefixes, "kPKCS1SigPrefixes", rodata_end);
320 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
321 assert_not_within(rodata_start, OPENSSL_ia32cap_P, "OPENSSL_ia32cap_P", rodata_end);
322 #elif defined(OPENSSL_AARCH64)
323 assert_not_within(rodata_start, &OPENSSL_armcap_P, "OPENSSL_armcap_P", rodata_end);
324 #endif
325
326 // Per FIPS 140-3 we have to perform the CAST of the HMAC used for integrity
327 // check before the integrity check itself. So we first call
328 // SHA-256 and HMAC-SHA256
329 // before we calculate the hash of the module.
330
331 uint8_t result[SHA256_DIGEST_LENGTH];
332 const EVP_MD *const kHashFunction = EVP_sha256();
333 if (!boringssl_self_test_sha256() ||
334 !boringssl_self_test_hmac_sha256()) {
335 return 0;
336 }
337
338 static const uint8_t kHMACKey[64] = {0};
339 unsigned result_len;
340 HMAC_CTX hmac_ctx;
341 HMAC_CTX_init(&hmac_ctx);
342 if (!HMAC_Init_ex(&hmac_ctx, kHMACKey, sizeof(kHMACKey), kHashFunction,
343 NULL /* no ENGINE */)) {
344 fprintf(stderr, "HMAC_Init_ex failed.\n");
345 return 0;
346 }
347 #if !defined(OPENSSL_WINDOWS)
348 BORINGSSL_maybe_set_module_text_permissions(PROT_READ | PROT_EXEC);
349 #endif
350 #if defined(BORINGSSL_SHARED_LIBRARY)
351 uint64_t length = end - start;
352 uint8_t buffer[sizeof(length)];
353 CRYPTO_store_u64_le(buffer, length);
354 HMAC_Update(&hmac_ctx, buffer, sizeof(length));
355 HMAC_Update(&hmac_ctx, start, length);
356
357 length = rodata_end - rodata_start;
358 CRYPTO_store_u64_le(buffer, length);
359 HMAC_Update(&hmac_ctx, buffer, sizeof(length));
360 HMAC_Update(&hmac_ctx, rodata_start, length);
361 #else
362 HMAC_Update(&hmac_ctx, start, end - start);
363 #endif
364 #if !defined(OPENSSL_WINDOWS)
365 BORINGSSL_maybe_set_module_text_permissions(PROT_EXEC);
366 #endif
367
368 if (!HMAC_Final(&hmac_ctx, result, &result_len) ||
369 result_len != sizeof(result)) {
370 fprintf(stderr, "HMAC failed.\n");
371 return 0;
372 }
373 HMAC_CTX_cleanse(&hmac_ctx); // FIPS 140-3, AS05.10.
374
375 const uint8_t *expected = BORINGSSL_bcm_text_hash;
376
377 #if defined(BORINGSSL_FIPS_BREAK_TESTS)
378 // Check the integrity but don't call AWS_LC_FIPS_failure or return 0
379 check_test_optional_abort(expected, result, sizeof(result), "FIPS integrity test", false);
380 #else
381 // Check the integrity, call AWS_LC_FIPS_failure if it doesn't match which will
382 // result in an abort
383 check_test_optional_abort(expected, result, sizeof(result), "FIPS integrity test", true);
384 #endif
385
386 OPENSSL_cleanse(result, sizeof(result)); // FIPS 140-3, AS05.10.
387 return 1;
388 }
389 #endif // !ASAN && !MSAN
390
391 void AWS_LC_FIPS_failure(const char* message) {
392 #if defined(AWSLC_FIPS_FAILURE_CALLBACK)
393 if (AWS_LC_fips_failure_callback != NULL) {
394 AWS_LC_fips_failure_callback(message);
395 return;
396 }
397 // Fallback to the default behavior if the callback is not defined
398 #endif
399 fprintf(stderr, "AWS-LC FIPS failure caused by:\n%s\n", message);
400 fflush(stderr);
401 for (;;) {
402 abort();
403 exit(1);
404 }
405 }
406 #else // BORINGSSL_FIPS
407 void AWS_LC_FIPS_failure(const char* message) {
408 fprintf(stderr, "AWS-LC FIPS failure caused by:\n%s\n", message); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
409 fflush(stderr);
410 }
411 #endif // BORINGSSL_FIPS
412 #if !defined(AWSLC_FIPS) && !defined(BORINGSSL_SHARED_LIBRARY)
413 // When linking with a static library, if no symbols in an object file are
414 // referenced then the object file is discarded, even if it has a constructor
415 // function. For example, if an application is linking with libcrypto.a and
416 // not referencing any symbol from crypto.o file, then crypto.o will be
417 // discarded. This is an issue because we define the library constructor
418 // in crypto.o so if the file is discarded then the library is not initialized.
419 // Note that this is not a problem for the FIPS build because when building the
420 // FIPS mode we have to ensure the initialization is done before the power-on
421 // self-tests so the test function itself calls |OPENSSL_cpuid_setup|.
422 //
423 // So, the issue manifests only in the static non-FIPS build. The work around
424 // is we add a dummy function |dummy_func_for_constructor| in |bcm.c| that
425 // calls |CRYPTO_library_init| function defined in |crypto.c|. This will ensure
426 // that, when linking with libcrypto.a, crypto.o will not be discarded as long
427 // as bcm.o is not discarded.
428 //
429 // This workaround is partial in a sense that if the application that's linking
430 // to libcrypto.a is not using any of the symbols from bcm.o or crypto.o then
431 // both object files will be discarded. But this would be an edge case that we
432 // don't expect to happen with significant probability. In case it happens, the
433 // application would have to call the |CRYPTO_library_init| function itself to
434 // ensure the initialization is done.
435 void dummy_func_for_constructor(void) {
436 CRYPTO_library_init();
437 }
438 #endif