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