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