blob: 7666222d21d6d9f3508a0c9ecf0c418161f2e3fd [file] [log] [blame]
Robert Sloan572a4e22017-04-17 10:52:19 -07001/* 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
Robert Sloan9254e682017-04-24 09:42:06 -070015#if !defined(_GNU_SOURCE)
Robert Sloan8f860b12017-08-28 07:37:06 -070016#define _GNU_SOURCE // needed for syscall() on Linux.
Robert Sloan9254e682017-04-24 09:42:06 -070017#endif
18
Robert Sloan572a4e22017-04-17 10:52:19 -070019#include <openssl/crypto.h>
Robert Sloan309a31e2018-01-29 10:22:47 -080020
21#include <stdlib.h>
22
23#include <openssl/digest.h>
Robert Sloan572a4e22017-04-17 10:52:19 -070024#include <openssl/hmac.h>
Robert Sloan8ff03552017-06-14 12:40:58 -070025#include <openssl/sha.h>
Robert Sloan572a4e22017-04-17 10:52:19 -070026
27#include "../internal.h"
28
29#include "aes/aes.c"
30#include "aes/key_wrap.c"
31#include "aes/mode_wrappers.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070032#include "bn/add.c"
33#include "bn/asm/x86_64-gcc.c"
34#include "bn/bn.c"
35#include "bn/bytes.c"
36#include "bn/cmp.c"
37#include "bn/ctx.c"
38#include "bn/div.c"
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010039#include "bn/div_extra.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070040#include "bn/exponentiation.c"
41#include "bn/gcd.c"
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010042#include "bn/gcd_extra.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070043#include "bn/generic.c"
44#include "bn/jacobi.c"
45#include "bn/montgomery.c"
46#include "bn/montgomery_inv.c"
47#include "bn/mul.c"
48#include "bn/prime.c"
49#include "bn/random.c"
50#include "bn/rsaz_exp.c"
51#include "bn/shift.c"
52#include "bn/sqrt.c"
53#include "cipher/aead.c"
54#include "cipher/cipher.c"
55#include "cipher/e_aes.c"
56#include "cipher/e_des.c"
57#include "des/des.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070058#include "digest/digest.c"
59#include "digest/digests.c"
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010060#include "ecdh/ecdh.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070061#include "ecdsa/ecdsa.c"
62#include "ec/ec.c"
63#include "ec/ec_key.c"
64#include "ec/ec_montgomery.c"
Robert Sloanc6ebb282018-04-30 10:10:26 -070065#include "ec/felem.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070066#include "ec/oct.c"
67#include "ec/p224-64.c"
Robert Sloan55818102017-12-18 11:26:17 -080068#include "../../third_party/fiat/p256.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070069#include "ec/p256-x86_64.c"
Robert Sloan5cbb5c82018-04-24 11:35:46 -070070#include "ec/scalar.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070071#include "ec/simple.c"
Robert Sloanc6ebb282018-04-30 10:10:26 -070072#include "ec/simple_mul.c"
Robert Sloan55818102017-12-18 11:26:17 -080073#include "ec/util.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070074#include "ec/wnaf.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070075#include "hmac/hmac.c"
76#include "md4/md4.c"
77#include "md5/md5.c"
Robert Sloan9254e682017-04-24 09:42:06 -070078#include "modes/cbc.c"
79#include "modes/cfb.c"
80#include "modes/ctr.c"
81#include "modes/gcm.c"
82#include "modes/ofb.c"
83#include "modes/polyval.c"
84#include "rand/ctrdrbg.c"
85#include "rand/rand.c"
86#include "rand/urandom.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070087#include "rsa/blinding.c"
88#include "rsa/padding.c"
89#include "rsa/rsa.c"
90#include "rsa/rsa_impl.c"
Robert Sloan309a31e2018-01-29 10:22:47 -080091#include "self_check/self_check.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070092#include "sha/sha1-altivec.c"
93#include "sha/sha1.c"
94#include "sha/sha256.c"
95#include "sha/sha512.c"
Robert Sloan978112c2018-01-22 12:53:01 -080096#include "tls/kdf.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070097
98
Robert Sloan978112c2018-01-22 12:53:01 -080099#if defined(BORINGSSL_FIPS)
100
101#if !defined(OPENSSL_ASAN)
Pete Bentley0c61efe2019-08-13 09:32:23 +0100102// These symbols are filled in by delocate.go (in static builds) or a linker
103// script (in shared builds). They point to the start and end of the module, and
104// the location of the integrity hash, respectively.
Robert Sloan978112c2018-01-22 12:53:01 -0800105extern const uint8_t BORINGSSL_bcm_text_start[];
106extern const uint8_t BORINGSSL_bcm_text_end[];
107extern const uint8_t BORINGSSL_bcm_text_hash[];
Pete Bentley0c61efe2019-08-13 09:32:23 +0100108#if defined(BORINGSSL_SHARED_LIBRARY)
109extern const uint8_t BORINGSSL_bcm_rodata_start[];
110extern const uint8_t BORINGSSL_bcm_rodata_end[];
111#endif
Robert Sloan978112c2018-01-22 12:53:01 -0800112#endif
113
114static void __attribute__((constructor))
115BORINGSSL_bcm_power_on_self_test(void) {
116 CRYPTO_library_init();
117
118#if !defined(OPENSSL_ASAN)
119 // Integrity tests cannot run under ASAN because it involves reading the full
120 // .text section, which triggers the global-buffer overflow detection.
121 const uint8_t *const start = BORINGSSL_bcm_text_start;
122 const uint8_t *const end = BORINGSSL_bcm_text_end;
Pete Bentley0c61efe2019-08-13 09:32:23 +0100123#if defined(BORINGSSL_SHARED_LIBRARY)
124 const uint8_t *const rodata_start = BORINGSSL_bcm_rodata_start;
125 const uint8_t *const rodata_end = BORINGSSL_bcm_rodata_end;
126#endif
Robert Sloan978112c2018-01-22 12:53:01 -0800127
128 static const uint8_t kHMACKey[64] = {0};
129 uint8_t result[SHA512_DIGEST_LENGTH];
130
131 unsigned result_len;
Pete Bentley0c61efe2019-08-13 09:32:23 +0100132 HMAC_CTX hmac_ctx;
133 HMAC_CTX_init(&hmac_ctx);
134 if (!HMAC_Init_ex(&hmac_ctx, kHMACKey, sizeof(kHMACKey), EVP_sha512(),
135 NULL /* no ENGINE */)) {
136 fprintf(stderr, "HMAC_Init_ex failed.\n");
137 goto err;
138 }
139#if defined(BORINGSSL_SHARED_LIBRARY)
140 uint64_t length = end - start;
141 HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
142 HMAC_Update(&hmac_ctx, start, length);
143
144 length = rodata_end - rodata_start;
145 HMAC_Update(&hmac_ctx, (const uint8_t *) &length, sizeof(length));
146 HMAC_Update(&hmac_ctx, rodata_start, length);
147#else
148 HMAC_Update(&hmac_ctx, start, end - start);
149#endif
150 if (!HMAC_Final(&hmac_ctx, result, &result_len) ||
Robert Sloan978112c2018-01-22 12:53:01 -0800151 result_len != sizeof(result)) {
Robert Sloanf068def2018-10-10 18:45:40 -0700152 fprintf(stderr, "HMAC failed.\n");
Robert Sloan978112c2018-01-22 12:53:01 -0800153 goto err;
154 }
Pete Bentley0c61efe2019-08-13 09:32:23 +0100155 HMAC_CTX_cleanup(&hmac_ctx);
Robert Sloan978112c2018-01-22 12:53:01 -0800156
157 const uint8_t *expected = BORINGSSL_bcm_text_hash;
158
159 if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
160 goto err;
161 }
162#endif
163
164 if (!BORINGSSL_self_test()) {
165 goto err;
166 }
167
Robert Sloan572a4e22017-04-17 10:52:19 -0700168 return;
169
170err:
Robert Sloan8ff03552017-06-14 12:40:58 -0700171 BORINGSSL_FIPS_abort();
172}
173
174void BORINGSSL_FIPS_abort(void) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700175 for (;;) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700176 abort();
Robert Sloan8ff03552017-06-14 12:40:58 -0700177 exit(1);
Robert Sloan572a4e22017-04-17 10:52:19 -0700178 }
179}
Robert Sloan978112c2018-01-22 12:53:01 -0800180
Robert Sloan8f860b12017-08-28 07:37:06 -0700181#endif // BORINGSSL_FIPS