blob: 028ec4e97b3eb050ff3c456004910512e66db7e6 [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"
39#include "bn/exponentiation.c"
40#include "bn/gcd.c"
41#include "bn/generic.c"
42#include "bn/jacobi.c"
43#include "bn/montgomery.c"
44#include "bn/montgomery_inv.c"
45#include "bn/mul.c"
46#include "bn/prime.c"
47#include "bn/random.c"
48#include "bn/rsaz_exp.c"
49#include "bn/shift.c"
50#include "bn/sqrt.c"
51#include "cipher/aead.c"
52#include "cipher/cipher.c"
53#include "cipher/e_aes.c"
54#include "cipher/e_des.c"
55#include "des/des.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070056#include "digest/digest.c"
57#include "digest/digests.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070058#include "ecdsa/ecdsa.c"
59#include "ec/ec.c"
60#include "ec/ec_key.c"
61#include "ec/ec_montgomery.c"
62#include "ec/oct.c"
63#include "ec/p224-64.c"
Robert Sloan55818102017-12-18 11:26:17 -080064#include "../../third_party/fiat/p256.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070065#include "ec/p256-x86_64.c"
66#include "ec/simple.c"
Robert Sloan55818102017-12-18 11:26:17 -080067#include "ec/util.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070068#include "ec/wnaf.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070069#include "hmac/hmac.c"
70#include "md4/md4.c"
71#include "md5/md5.c"
Robert Sloan9254e682017-04-24 09:42:06 -070072#include "modes/cbc.c"
Robert Sloanab8b8882018-03-26 11:39:51 -070073#include "modes/ccm.c"
Robert Sloan9254e682017-04-24 09:42:06 -070074#include "modes/cfb.c"
75#include "modes/ctr.c"
76#include "modes/gcm.c"
77#include "modes/ofb.c"
78#include "modes/polyval.c"
79#include "rand/ctrdrbg.c"
80#include "rand/rand.c"
81#include "rand/urandom.c"
Robert Sloan8ff03552017-06-14 12:40:58 -070082#include "rsa/blinding.c"
83#include "rsa/padding.c"
84#include "rsa/rsa.c"
85#include "rsa/rsa_impl.c"
Robert Sloan309a31e2018-01-29 10:22:47 -080086#include "self_check/self_check.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070087#include "sha/sha1-altivec.c"
88#include "sha/sha1.c"
89#include "sha/sha256.c"
90#include "sha/sha512.c"
Robert Sloan978112c2018-01-22 12:53:01 -080091#include "tls/kdf.c"
Robert Sloan572a4e22017-04-17 10:52:19 -070092
93
Robert Sloan978112c2018-01-22 12:53:01 -080094#if defined(BORINGSSL_FIPS)
95
96#if !defined(OPENSSL_ASAN)
97// These symbols are filled in by delocate.go. They point to the start and end
98// of the module, and the location of the integrity hash, respectively.
99extern const uint8_t BORINGSSL_bcm_text_start[];
100extern const uint8_t BORINGSSL_bcm_text_end[];
101extern const uint8_t BORINGSSL_bcm_text_hash[];
102#endif
103
104static void __attribute__((constructor))
105BORINGSSL_bcm_power_on_self_test(void) {
106 CRYPTO_library_init();
107
108#if !defined(OPENSSL_ASAN)
109 // Integrity tests cannot run under ASAN because it involves reading the full
110 // .text section, which triggers the global-buffer overflow detection.
111 const uint8_t *const start = BORINGSSL_bcm_text_start;
112 const uint8_t *const end = BORINGSSL_bcm_text_end;
113
114 static const uint8_t kHMACKey[64] = {0};
115 uint8_t result[SHA512_DIGEST_LENGTH];
116
117 unsigned result_len;
118 if (!HMAC(EVP_sha512(), kHMACKey, sizeof(kHMACKey), start, end - start,
119 result, &result_len) ||
120 result_len != sizeof(result)) {
121 goto err;
122 }
123
124 const uint8_t *expected = BORINGSSL_bcm_text_hash;
125
126 if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
127 goto err;
128 }
129#endif
130
131 if (!BORINGSSL_self_test()) {
132 goto err;
133 }
134
Robert Sloan572a4e22017-04-17 10:52:19 -0700135 return;
136
137err:
Robert Sloan8ff03552017-06-14 12:40:58 -0700138 BORINGSSL_FIPS_abort();
139}
140
141void BORINGSSL_FIPS_abort(void) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700142 for (;;) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700143 abort();
Robert Sloan8ff03552017-06-14 12:40:58 -0700144 exit(1);
Robert Sloan572a4e22017-04-17 10:52:19 -0700145 }
146}
Robert Sloan978112c2018-01-22 12:53:01 -0800147
Robert Sloan8f860b12017-08-28 07:37:06 -0700148#endif // BORINGSSL_FIPS