Home | History | Annotate | Download | only in fipsmodule
      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 
     23 #include <openssl/digest.h>
     24 #include <openssl/hmac.h>
     25 #include <openssl/sha.h>
     26 
     27 #include "../internal.h"
     28 
     29 #include "aes/aes.c"
     30 #include "aes/key_wrap.c"
     31 #include "aes/mode_wrappers.c"
     32 #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"
     56 #include "digest/digest.c"
     57 #include "digest/digests.c"
     58 #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"
     64 #include "../../third_party/fiat/p256.c"
     65 #include "ec/p256-x86_64.c"
     66 #include "ec/simple.c"
     67 #include "ec/util.c"
     68 #include "ec/wnaf.c"
     69 #include "hmac/hmac.c"
     70 #include "md4/md4.c"
     71 #include "md5/md5.c"
     72 #include "modes/cbc.c"
     73 #include "modes/cfb.c"
     74 #include "modes/ctr.c"
     75 #include "modes/gcm.c"
     76 #include "modes/ofb.c"
     77 #include "modes/polyval.c"
     78 #include "rand/ctrdrbg.c"
     79 #include "rand/rand.c"
     80 #include "rand/urandom.c"
     81 #include "rsa/blinding.c"
     82 #include "rsa/padding.c"
     83 #include "rsa/rsa.c"
     84 #include "rsa/rsa_impl.c"
     85 #include "self_check/self_check.c"
     86 #include "sha/sha1-altivec.c"
     87 #include "sha/sha1.c"
     88 #include "sha/sha256.c"
     89 #include "sha/sha512.c"
     90 #include "tls/kdf.c"
     91 
     92 
     93 #if defined(BORINGSSL_FIPS)
     94 
     95 #if !defined(OPENSSL_ASAN)
     96 // These symbols are filled in by delocate.go. They point to the start and end
     97 // of the module, and the location of the integrity hash, respectively.
     98 extern const uint8_t BORINGSSL_bcm_text_start[];
     99 extern const uint8_t BORINGSSL_bcm_text_end[];
    100 extern const uint8_t BORINGSSL_bcm_text_hash[];
    101 #endif
    102 
    103 static void __attribute__((constructor))
    104 BORINGSSL_bcm_power_on_self_test(void) {
    105   CRYPTO_library_init();
    106 
    107 #if !defined(OPENSSL_ASAN)
    108   // Integrity tests cannot run under ASAN because it involves reading the full
    109   // .text section, which triggers the global-buffer overflow detection.
    110   const uint8_t *const start = BORINGSSL_bcm_text_start;
    111   const uint8_t *const end = BORINGSSL_bcm_text_end;
    112 
    113   static const uint8_t kHMACKey[64] = {0};
    114   uint8_t result[SHA512_DIGEST_LENGTH];
    115 
    116   unsigned result_len;
    117   if (!HMAC(EVP_sha512(), kHMACKey, sizeof(kHMACKey), start, end - start,
    118             result, &result_len) ||
    119       result_len != sizeof(result)) {
    120     goto err;
    121   }
    122 
    123   const uint8_t *expected = BORINGSSL_bcm_text_hash;
    124 
    125   if (!check_test(expected, result, sizeof(result), "FIPS integrity test")) {
    126     goto err;
    127   }
    128 #endif
    129 
    130   if (!BORINGSSL_self_test()) {
    131     goto err;
    132   }
    133 
    134   return;
    135 
    136 err:
    137   BORINGSSL_FIPS_abort();
    138 }
    139 
    140 void BORINGSSL_FIPS_abort(void) {
    141   for (;;) {
    142     abort();
    143     exit(1);
    144   }
    145 }
    146 
    147 #endif  // BORINGSSL_FIPS
    148