diff --git a/Package.swift b/Package.swift index 1743bb9e..49915681 100644 --- a/Package.swift +++ b/Package.swift @@ -20,7 +20,7 @@ // Sources/CCryptoBoringSSL directory. The source repository is at // https://boringssl.googlesource.com/boringssl. // -// BoringSSL Commit: 295b31324f8c557dcd3c1c831857e33a7f23bc52 +// BoringSSL Commit: 8bbefbfeee609b17622deedd100163c12f5c95dc import PackageDescription diff --git a/Sources/CCryptoBoringSSL/CMakeLists.txt b/Sources/CCryptoBoringSSL/CMakeLists.txt index 40f45df4..67fdf01a 100644 --- a/Sources/CCryptoBoringSSL/CMakeLists.txt +++ b/Sources/CCryptoBoringSSL/CMakeLists.txt @@ -17,7 +17,6 @@ add_library(CCryptoBoringSSL STATIC "crypto/asn1/a_bool.c" "crypto/asn1/a_d2i_fp.c" "crypto/asn1/a_dup.c" - "crypto/asn1/a_enum.c" "crypto/asn1/a_gentm.c" "crypto/asn1/a_i2d_fp.c" "crypto/asn1/a_int.c" @@ -70,6 +69,7 @@ add_library(CCryptoBoringSSL STATIC "crypto/cipher_extra/e_aesctrhmac.c" "crypto/cipher_extra/e_aesgcmsiv.c" "crypto/cipher_extra/e_chacha20poly1305.c" + "crypto/cipher_extra/e_des.c" "crypto/cipher_extra/e_null.c" "crypto/cipher_extra/e_rc2.c" "crypto/cipher_extra/e_rc4.c" @@ -88,6 +88,7 @@ add_library(CCryptoBoringSSL STATIC "crypto/crypto.c" "crypto/curve25519/curve25519.c" "crypto/curve25519/spake25519.c" + "crypto/des/des.c" "crypto/dh_extra/dh_asn1.c" "crypto/dh_extra/params.c" "crypto/digest_extra/digest_extra.c" @@ -147,8 +148,6 @@ add_library(CCryptoBoringSSL STATIC "crypto/fipsmodule/cipher/aead.c" "crypto/fipsmodule/cipher/cipher.c" "crypto/fipsmodule/cipher/e_aes.c" - "crypto/fipsmodule/cipher/e_des.c" - "crypto/fipsmodule/des/des.c" "crypto/fipsmodule/dh/check.c" "crypto/fipsmodule/dh/dh.c" "crypto/fipsmodule/digest/digest.c" diff --git a/Sources/CCryptoBoringSSL/crypto/asn1/a_enum.c b/Sources/CCryptoBoringSSL/crypto/asn1/a_enum.c deleted file mode 100644 index 16fc8ba4..00000000 --- a/Sources/CCryptoBoringSSL/crypto/asn1/a_enum.c +++ /dev/null @@ -1,195 +0,0 @@ -/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) - * All rights reserved. - * - * This package is an SSL implementation written - * by Eric Young (eay@cryptsoft.com). - * The implementation was written so as to conform with Netscapes SSL. - * - * This library is free for commercial and non-commercial use as long as - * the following conditions are aheared to. The following conditions - * apply to all code found in this distribution, be it the RC4, RSA, - * lhash, DES, etc., code; not just the SSL code. The SSL documentation - * included with this distribution is covered by the same copyright terms - * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * - * Copyright remains Eric Young's, and as such any Copyright notices in - * the code are not to be removed. - * If this package is used in a product, Eric Young should be given attribution - * as the author of the parts of the library used. - * This can be in the form of a textual message at program startup or - * in documentation (online or textual) provided with the package. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * "This product includes cryptographic software written by - * Eric Young (eay@cryptsoft.com)" - * The word 'cryptographic' can be left out if the rouines from the library - * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from - * the apps directory (application code) you must include an acknowledgement: - * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * - * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * The licence and distribution terms for any publically available version or - * derivative of this code cannot be changed. i.e. this code cannot simply be - * copied and put under another distribution licence - * [including the GNU Public Licence.] */ - -#include - -#include -#include - -#include -#include - -#include "../internal.h" - - -/* - * Code for ENUMERATED type: identical to INTEGER apart from a different tag. - * for comments on encoding see a_int.c - */ - -int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) -{ - int j, k; - unsigned int i; - unsigned char buf[sizeof(long) + 1]; - long d; - - a->type = V_ASN1_ENUMERATED; - if (a->length < (int)(sizeof(long) + 1)) { - if (a->data != NULL) - OPENSSL_free(a->data); - if ((a->data = - (unsigned char *)OPENSSL_malloc(sizeof(long) + 1)) != NULL) - OPENSSL_memset((char *)a->data, 0, sizeof(long) + 1); - } - if (a->data == NULL) { - OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); - return (0); - } - d = v; - if (d < 0) { - d = -d; - a->type = V_ASN1_NEG_ENUMERATED; - } - - for (i = 0; i < sizeof(long); i++) { - if (d == 0) - break; - buf[i] = (int)d & 0xff; - d >>= 8; - } - j = 0; - for (k = i - 1; k >= 0; k--) - a->data[j++] = buf[k]; - a->length = j; - return (1); -} - -long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) -{ - int neg = 0, i; - - if (a == NULL) - return (0L); - i = a->type; - if (i == V_ASN1_NEG_ENUMERATED) - neg = 1; - else if (i != V_ASN1_ENUMERATED) - return -1; - - OPENSSL_STATIC_ASSERT(sizeof(uint64_t) >= sizeof(long), - "long larger than uint64_t"); - - if (a->length > (int)sizeof(uint64_t)) { - /* hmm... a bit ugly */ - return -1; - } - - uint64_t r64 = 0; - if (a->data != NULL) { - for (i = 0; i < a->length; i++) { - r64 <<= 8; - r64 |= (unsigned char)a->data[i]; - } - - if (r64 > LONG_MAX) { - return -1; - } - } - - long r = (long) r64; - if (neg) - r = -r; - - return r; -} - -ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) -{ - ASN1_ENUMERATED *ret; - int len, j; - - if (ai == NULL) - ret = ASN1_ENUMERATED_new(); - else - ret = ai; - if (ret == NULL) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); - goto err; - } - if (BN_is_negative(bn)) - ret->type = V_ASN1_NEG_ENUMERATED; - else - ret->type = V_ASN1_ENUMERATED; - j = BN_num_bits(bn); - len = ((j == 0) ? 0 : ((j / 8) + 1)); - if (ret->length < len + 4) { - unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4); - if (!new_data) { - OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); - goto err; - } - ret->data = new_data; - } - - ret->length = BN_bn2bin(bn, ret->data); - return (ret); - err: - if (ret != ai) - ASN1_ENUMERATED_free(ret); - return (NULL); -} - -BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) -{ - BIGNUM *ret; - - if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) - OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB); - else if (ai->type == V_ASN1_NEG_ENUMERATED) - BN_set_negative(ret, 1); - return (ret); -} diff --git a/Sources/CCryptoBoringSSL/crypto/asn1/a_int.c b/Sources/CCryptoBoringSSL/crypto/asn1/a_int.c index a8754eed..95e8a7ac 100644 --- a/Sources/CCryptoBoringSSL/crypto/asn1/a_int.c +++ b/Sources/CCryptoBoringSSL/crypto/asn1/a_int.c @@ -59,8 +59,10 @@ #include #include +#include #include #include +#include #include "../internal.h" @@ -72,129 +74,110 @@ ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) { - int neg, ret; - /* Compare signs */ - neg = x->type & V_ASN1_NEG; + /* Compare signs. */ + int neg = x->type & V_ASN1_NEG; if (neg != (y->type & V_ASN1_NEG)) { - if (neg) - return -1; - else + return neg ? -1 : 1; + } + + int ret = ASN1_STRING_cmp(x, y); + if (neg) { + /* This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from + * returning |INT_MIN|. */ + if (ret < 0) { return 1; + } else if (ret > 0) { + return -1; + } else { + return 0; + } } - ret = ASN1_STRING_cmp(x, y); - - if (neg) - return -ret; - else - return ret; + return ret; } -/* - * This converts an ASN1 INTEGER into its content encoding. - * The internal representation is an ASN1_STRING whose data is a big endian - * representation of the value, ignoring the sign. The sign is determined by - * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. - * - * Positive integers are no problem: they are almost the same as the DER - * encoding, except if the first byte is >= 0x80 we need to add a zero pad. - * - * Negative integers are a bit trickier... - * The DER representation of negative integers is in 2s complement form. - * The internal form is converted by complementing each octet and finally - * adding one to the result. This can be done less messily with a little trick. - * If the internal form has trailing zeroes then they will become FF by the - * complement and 0 by the add one (due to carry) so just copy as many trailing - * zeros to the destination as there are in the source. The carry will add one - * to the last none zero octet: so complement this octet and add one and finally - * complement any left over until you get to the start of the string. - * - * Padding is a little trickier too. If the first bytes is > 0x80 then we pad - * with 0xff. However if the first byte is 0x80 and one of the following bytes - * is non-zero we pad with 0xff. The reason for this distinction is that 0x80 - * followed by optional zeros isn't padded. - */ - -int i2c_ASN1_INTEGER(const ASN1_INTEGER *a, unsigned char **pp) +/* negate_twos_complement negates |len| bytes from |buf| in-place, interpreted + * as a signed, big-endian two's complement value. */ +static void negate_twos_complement(uint8_t *buf, size_t len) { - int pad = 0, ret, i, neg; - unsigned char *p, *n, pb = 0; - - if (a == NULL) - return (0); - neg = a->type & V_ASN1_NEG; - if (a->length == 0) - ret = 1; - else { - ret = a->length; - i = a->data[0]; - if (ret == 1 && i == 0) - neg = 0; - if (!neg && (i > 127)) { - pad = 1; - pb = 0; - } else if (neg) { - if (i > 128) { - pad = 1; - pb = 0xFF; - } else if (i == 128) { - /* - * Special case: if any other bytes non zero we pad: - * otherwise we don't. - */ - for (i = 1; i < a->length; i++) - if (a->data[i]) { - pad = 1; - pb = 0xFF; - break; - } - } - } - ret += pad; + uint8_t borrow = 0; + for (size_t i = len - 1; i < len; i--) { + uint8_t t = buf[i]; + buf[i] = 0u - borrow - t; + borrow |= t != 0; } - if (pp == NULL) - return (ret); - p = *pp; - - if (pad) - *(p++) = pb; - if (a->length == 0) - *(p++) = 0; - else if (!neg) - OPENSSL_memcpy(p, a->data, (unsigned int)a->length); - else { - /* Begin at the end of the encoding */ - n = a->data + a->length - 1; - p += a->length - 1; - i = a->length; - /* Copy zeros to destination as long as source is zero */ - while (!*n && i > 1) { - *(p--) = 0; - n--; - i--; - } - /* Complement and increment next octet */ - *(p--) = ((*(n--)) ^ 0xff) + 1; - i--; - /* Complement any octets left */ - for (; i > 0; i--) - *(p--) = *(n--) ^ 0xff; - } - - *pp += ret; - return (ret); } -/* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */ +static int is_all_zeros(const uint8_t *in, size_t len) { + for (size_t i = 0; i < len; i++) { + if (in[i] != 0) { + return 0; + } + } + return 1; +} -ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, +int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) +{ + if (in == NULL) { + return 0; + } + + /* |ASN1_INTEGER|s should be represented minimally, but it is possible to + * construct invalid ones. Skip leading zeros so this does not produce an + * invalid encoding or break invariants. */ + int start = 0; + while (start < in->length && in->data[start] == 0) { + start++; + } + + int is_negative = (in->type & V_ASN1_NEG) != 0; + int pad; + if (start >= in->length) { + /* Zero is represented as a single byte. */ + is_negative = 0; + pad = 1; + } else if (is_negative) { + /* 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff + * through 0x00...01 and need an extra byte to be negative. + * 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff + * through 0x80...00 and can be negated as-is. */ + pad = in->data[start] > 0x80 || + (in->data[start] == 0x80 && + !is_all_zeros(in->data + start + 1, in->length - start - 1)); + } else { + /* If the high bit is set, the signed representation needs an extra + * byte to be positive. */ + pad = (in->data[start] & 0x80) != 0; + } + + if (in->length - start > INT_MAX - pad) { + OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW); + return 0; + } + int len = pad + in->length - start; + assert(len > 0); + if (outp == NULL) { + return len; + } + + if (pad) { + (*outp)[0] = 0; + } + OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start); + if (is_negative) { + negate_twos_complement(*outp, len); + assert((*outp)[0] >= 0x80); + } else { + assert((*outp)[0] < 0x80); + } + *outp += len; + return len; +} + +ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp, long len) { - ASN1_INTEGER *ret = NULL; - const unsigned char *p, *pend; - unsigned char *to, *s; - int i; - /* * This function can handle lengths up to INT_MAX - 1, but the rest of the * legacy ASN.1 code mixes integer types, so avoid exposing it to @@ -205,85 +188,69 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, return NULL; } - if ((a == NULL) || ((*a) == NULL)) { - if ((ret = ASN1_INTEGER_new()) == NULL) - return (NULL); - ret->type = V_ASN1_INTEGER; - } else - ret = (*a); - - p = *pp; - pend = p + len; - - /* - * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies - * a missing NULL parameter. - */ - s = (unsigned char *)OPENSSL_malloc((int)len + 1); - if (s == NULL) { - i = ERR_R_MALLOC_FAILURE; - goto err; + CBS cbs; + CBS_init(&cbs, *inp, (size_t)len); + int is_negative; + if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); + return NULL; } - to = s; - if (!len) { - /* - * Strictly speaking this is an illegal INTEGER but we tolerate it. - */ - ret->type = V_ASN1_INTEGER; - } else if (*p & 0x80) { /* a negative number */ - ret->type = V_ASN1_NEG_INTEGER; - if ((*p == 0xff) && (len != 1)) { - p++; - len--; - } - i = len; - p += i - 1; - to += i - 1; - while ((!*p) && i) { - *(to--) = 0; - i--; - p--; - } - /* - * Special case: if all zeros then the number will be of the form FF - * followed by n zero bytes: this corresponds to 1 followed by n zero - * bytes. We've already written n zeros so we just append an extra - * one and set the first byte to a 1. This is treated separately - * because it is the only case where the number of bytes is larger - * than len. - */ - if (!i) { - *s = 1; - s[len] = 0; - len++; - } else { - *(to--) = (*(p--) ^ 0xff) + 1; - i--; - for (; i > 0; i--) - *(to--) = *(p--) ^ 0xff; + + ASN1_INTEGER *ret = NULL; + if (out == NULL || *out == NULL) { + ret = ASN1_INTEGER_new(); + if (ret == NULL) { + return NULL; } } else { - ret->type = V_ASN1_INTEGER; - if ((*p == 0) && (len != 1)) { - p++; - len--; - } - OPENSSL_memcpy(s, p, (int)len); + ret = *out; } - if (ret->data != NULL) - OPENSSL_free(ret->data); - ret->data = s; - ret->length = (int)len; - if (a != NULL) - (*a) = ret; - *pp = pend; - return (ret); + /* Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First, + * determine the size needed for a minimal result. */ + if (is_negative) { + /* 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff + * through 0x000100...001 and need one leading zero removed. 0x8000...00 + * through 0xff00...00 have a two's complement of 0x8000...00 through + * 0x0100...00 and will be minimally-encoded as-is. */ + if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff && + !is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) { + CBS_skip(&cbs, 1); + } + } else { + /* Remove the leading zero byte, if any. */ + if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) { + CBS_skip(&cbs, 1); + } + } + + if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) { + goto err; + } + + if (is_negative) { + ret->type = V_ASN1_NEG_INTEGER; + negate_twos_complement(ret->data, ret->length); + } else { + ret->type = V_ASN1_INTEGER; + } + + /* The value should be minimally-encoded. */ + assert(ret->length == 0 || ret->data[0] != 0); + /* Zero is not negative. */ + assert(!is_negative || ret->length > 0); + + *inp += len; + if (out != NULL) { + *out = ret; + } + return ret; + err: - OPENSSL_PUT_ERROR(ASN1, i); - if ((ret != NULL) && ((a == NULL) || (*a != ret))) + if (ret != NULL && (out == NULL || *out != ret)) { ASN1_INTEGER_free(ret); - return (NULL); + } + return NULL; } int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) @@ -300,121 +267,196 @@ int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) return 1; } -int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v) +int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) { - uint8_t *const newdata = OPENSSL_malloc(sizeof(uint64_t)); - if (newdata == NULL) { - OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); + if (v >= 0) { + return ASN1_ENUMERATED_set_uint64(a, (uint64_t) v); + } + + if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t) v)) { return 0; } - OPENSSL_free(out->data); - out->data = newdata; - v = CRYPTO_bswap8(v); - memcpy(out->data, &v, sizeof(v)); - - out->type = V_ASN1_INTEGER; + a->type = V_ASN1_NEG_ENUMERATED; + return 1; +} +static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type) +{ + uint8_t buf[sizeof(uint64_t)]; + CRYPTO_store_u64_be(buf, v); size_t leading_zeros; - for (leading_zeros = 0; leading_zeros < sizeof(uint64_t) - 1; - leading_zeros++) { - if (out->data[leading_zeros] != 0) { - break; - } + for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) { + if (buf[leading_zeros] != 0) { + break; + } } - out->length = sizeof(uint64_t) - leading_zeros; - OPENSSL_memmove(out->data, out->data + leading_zeros, out->length); - + if (!ASN1_STRING_set(out, buf + leading_zeros, + sizeof(buf) - leading_zeros)) { + return 0; + } + out->type = type; return 1; } +int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v) +{ + return asn1_string_set_uint64(out, v, V_ASN1_INTEGER); +} + +int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v) +{ + return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED); +} + +static int asn1_string_get_abs_uint64(uint64_t *out, const ASN1_STRING *a, + int type) +{ + if ((a->type & ~V_ASN1_NEG) != type) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE); + return 0; + } + uint8_t buf[sizeof(uint64_t)] = {0}; + if (a->length > (int)sizeof(buf)) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); + return 0; + } + OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length); + *out = CRYPTO_load_u64_be(buf); + return 1; +} + +static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a, int type) +{ + if (!asn1_string_get_abs_uint64(out, a, type)) { + return 0; + } + if (a->type & V_ASN1_NEG) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); + return 0; + } + return 1; +} + +int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a) +{ + return asn1_string_get_uint64(out, a, V_ASN1_INTEGER); +} + +int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a) +{ + return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED); +} + +static long asn1_string_get_long(const ASN1_STRING *a, int type) +{ + if (a == NULL) { + return 0; + } + + uint64_t v; + if (!asn1_string_get_abs_uint64(&v, a, type)) { + goto err; + } + + int64_t i64; + int fits_in_i64; + /* Check |v != 0| to handle manually-constructed negative zeros. */ + if ((a->type & V_ASN1_NEG) && v != 0) { + i64 = (int64_t)(0u - v); + fits_in_i64 = i64 < 0; + } else { + i64 = (int64_t)v; + fits_in_i64 = i64 >= 0; + } + OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), "long is too big"); + + if (fits_in_i64 && LONG_MIN <= i64 && i64 <= LONG_MAX) { + return (long)i64; + } + +err: + /* This function's return value does not distinguish overflow from -1. */ + ERR_clear_error(); + return -1; +} + long ASN1_INTEGER_get(const ASN1_INTEGER *a) { - int neg = 0, i; - - if (a == NULL) - return (0L); - i = a->type; - if (i == V_ASN1_NEG_INTEGER) - neg = 1; - else if (i != V_ASN1_INTEGER) - return -1; - - OPENSSL_STATIC_ASSERT(sizeof(uint64_t) >= sizeof(long), - "long larger than uint64_t"); - - if (a->length > (int)sizeof(uint64_t)) { - /* hmm... a bit ugly, return all ones */ - return -1; - } - - uint64_t r64 = 0; - if (a->data != NULL) { - for (i = 0; i < a->length; i++) { - r64 <<= 8; - r64 |= (unsigned char)a->data[i]; - } - - if (r64 > LONG_MAX) { - return -1; - } - } - - long r = (long) r64; - if (neg) - r = -r; - - return r; + return asn1_string_get_long(a, V_ASN1_INTEGER); } -ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) +long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) +{ + return asn1_string_get_long(a, V_ASN1_ENUMERATED); +} + +static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai, + int type) { ASN1_INTEGER *ret; - int len, j; - - if (ai == NULL) - ret = ASN1_INTEGER_new(); - else + if (ai == NULL) { + ret = ASN1_STRING_type_new(type); + } else { ret = ai; + } if (ret == NULL) { OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); goto err; } - if (BN_is_negative(bn) && !BN_is_zero(bn)) - ret->type = V_ASN1_NEG_INTEGER; - else - ret->type = V_ASN1_INTEGER; - j = BN_num_bits(bn); - len = ((j == 0) ? 0 : ((j / 8) + 1)); - if (ret->length < len + 4) { - unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4); - if (!new_data) { - OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); - goto err; - } - ret->data = new_data; + + if (BN_is_negative(bn) && !BN_is_zero(bn)) { + ret->type = type | V_ASN1_NEG; + } else { + ret->type = type; } - ret->length = BN_bn2bin(bn, ret->data); - /* Correct zero case */ - if (!ret->length) { - ret->data[0] = 0; - ret->length = 1; + + int len = BN_num_bytes(bn); + if (!ASN1_STRING_set(ret, NULL, len) || + !BN_bn2bin_padded(ret->data, len, bn)) { + goto err; } - return (ret); + return ret; + err: - if (ret != ai) - ASN1_INTEGER_free(ret); - return (NULL); + if (ret != ai) { + ASN1_STRING_free(ret); + } + return NULL; +} + +ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) +{ + return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER); +} + +ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) +{ + return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED); +} + +static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type) +{ + if ((ai->type & ~V_ASN1_NEG) != type) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE); + return NULL; + } + + BIGNUM *ret; + if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) + OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB); + else if (ai->type & V_ASN1_NEG) + BN_set_negative(ret, 1); + return (ret); } BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) { - BIGNUM *ret; - - if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) - OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB); - else if (ai->type == V_ASN1_NEG_INTEGER) - BN_set_negative(ret, 1); - return (ret); + return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER); +} + +BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) +{ + return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED); } diff --git a/Sources/CCryptoBoringSSL/crypto/asn1/a_strex.c b/Sources/CCryptoBoringSSL/crypto/asn1/a_strex.c index b19fb147..fb83650e 100644 --- a/Sources/CCryptoBoringSSL/crypto/asn1/a_strex.c +++ b/Sources/CCryptoBoringSSL/crypto/asn1/a_strex.c @@ -574,7 +574,8 @@ int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) // their value, updates |v| and |len|, and returns one. Otherwise, returns // zero. static int consume_two_digits(int* out, const char **v, int *len) { - if (*len < 2|| !isdigit((*v)[0]) || !isdigit((*v)[1])) { + if (*len < 2 || !isdigit((unsigned char)((*v)[0])) || + !isdigit((unsigned char)((*v)[1]))) { return 0; } *out = ((*v)[0] - '0') * 10 + ((*v)[1] - '0'); diff --git a/Sources/CCryptoBoringSSL/crypto/asn1/asn1_lib.c b/Sources/CCryptoBoringSSL/crypto/asn1/asn1_lib.c index 81964333..db3bed99 100644 --- a/Sources/CCryptoBoringSSL/crypto/asn1/asn1_lib.c +++ b/Sources/CCryptoBoringSSL/crypto/asn1/asn1_lib.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include @@ -104,101 +104,54 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT) OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG) OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE) -static int asn1_get_length(const unsigned char **pp, long *rl, long max); static void asn1_put_length(unsigned char **pp, int length); -int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, - int *pclass, long omax) +int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag, + int *out_class, long in_len) { - int i, ret; - long l; - const unsigned char *p = *pp; - int tag, xclass; - long max = omax; - - if (!max) - goto err; - ret = (*p & V_ASN1_CONSTRUCTED); - xclass = (*p & V_ASN1_PRIVATE); - i = *p & V_ASN1_PRIMITIVE_TAG; - if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ - p++; - if (--max == 0) - goto err; - l = 0; - while (*p & 0x80) { - l <<= 7L; - l |= *(p++) & 0x7f; - if (--max == 0) - goto err; - if (l > (INT_MAX >> 7L)) - goto err; - } - l <<= 7L; - l |= *(p++) & 0x7f; - tag = (int)l; - if (--max == 0) - goto err; - } else { - tag = i; - p++; - if (--max == 0) - goto err; - } - - /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */ - if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL) - goto err; - - *ptag = tag; - *pclass = xclass; - if (!asn1_get_length(&p, plength, max)) - goto err; - - if (*plength > (omax - (p - *pp))) { - OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); + if (in_len < 0) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); return 0x80; } - *pp = p; - return ret; - err: - OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); - return 0x80; -} -static int asn1_get_length(const unsigned char **pp, long *rl, long max) -{ - const unsigned char *p = *pp; - unsigned long ret = 0; - unsigned long i; + /* TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to + * reject non-minimal lengths, which are only allowed in BER. However, + * Android sometimes needs allow a non-minimal length in certificate + * signature fields (see b/18228011). Make this only apply to that field, + * while requiring DER elsewhere. Better yet, it should be limited to an + * preprocessing step in that part of Android. */ + unsigned tag; + size_t header_len; + int indefinite; + CBS cbs, body; + CBS_init(&cbs, *inp, (size_t)in_len); + if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len, + /*out_ber_found=*/NULL, &indefinite) || + indefinite || + !CBS_skip(&body, header_len) || + /* Bound the length to comfortably fit in an int. Lengths in this + * module often switch between int and long without overflow checks. */ + CBS_len(&body) > INT_MAX / 2) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); + return 0x80; + } - if (max-- < 1) { - return 0; + /* Convert between tag representations. */ + int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT; + int constructed = (tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT; + int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK; + + /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */ + if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) { + OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); + return 0x80; } - if (*p == 0x80) { - /* We do not support BER indefinite-length encoding. */ - return 0; - } - i = *p & 0x7f; - if (*(p++) & 0x80) { - if (i > sizeof(ret) || max < (long)i) - return 0; - while (i-- > 0) { - ret <<= 8L; - ret |= *(p++); - } - } else { - ret = i; - } - /* - * Bound the length to comfortably fit in an int. Lengths in this module - * often switch between int and long without overflow checks. - */ - if (ret > INT_MAX / 2) - return 0; - *pp = p; - *rl = (long)ret; - return 1; + + *inp = CBS_data(&body); + *out_len = CBS_len(&body); + *out_tag = tag_number; + *out_class = tag_class; + return constructed; } /* diff --git a/Sources/CCryptoBoringSSL/crypto/asn1/tasn_utl.c b/Sources/CCryptoBoringSSL/crypto/asn1/tasn_utl.c index cbe6df65..e30cf26d 100644 --- a/Sources/CCryptoBoringSSL/crypto/asn1/tasn_utl.c +++ b/Sources/CCryptoBoringSSL/crypto/asn1/tasn_utl.c @@ -223,7 +223,6 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr) { const ASN1_ADB *adb; const ASN1_ADB_TABLE *atbl; - long selector; ASN1_VALUE **sfld; int i; if (!(tt->flags & ASN1_TFLG_ADB_MASK)) { @@ -244,14 +243,11 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, return adb->null_tt; } - /* Convert type to a long: + /* Convert type to a NID: * NB: don't check for NID_undef here because it * might be a legitimate value in the table */ - if (tt->flags & ASN1_TFLG_ADB_OID) { - selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); - } else { - selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); - } + assert(tt->flags & ASN1_TFLG_ADB_OID); + int selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); /* Try to find matching entry in table Maybe should check application types * first to allow application override? Might also be useful to have a flag diff --git a/Sources/CCryptoBoringSSL/crypto/bytestring/ber.c b/Sources/CCryptoBoringSSL/crypto/bytestring/ber.c index 57aa5777..f7ab6df0 100644 --- a/Sources/CCryptoBoringSSL/crypto/bytestring/ber.c +++ b/Sources/CCryptoBoringSSL/crypto/bytestring/ber.c @@ -69,9 +69,9 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) { CBS contents; unsigned tag; size_t header_len; - + int indefinite; if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len, - ber_found)) { + ber_found, &indefinite)) { return 0; } if (*ber_found) { @@ -119,11 +119,11 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag, CBS contents; unsigned tag, child_string_tag = string_tag; size_t header_len; - int ber_found; + int indefinite; CBB *out_contents, out_contents_storage; if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len, - &ber_found)) { + /*out_ber_found=*/NULL, &indefinite)) { return 0; } @@ -153,11 +153,9 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag, out_contents = &out_contents_storage; } - if (CBS_len(&contents) == header_len && header_len > 0 && - CBS_data(&contents)[header_len - 1] == 0x80) { - // This is an indefinite length element. + if (indefinite) { if (!cbs_convert_ber(in, out_contents, child_string_tag, - 1 /* looking for eoc */, depth + 1) || + /*looking_for_eoc=*/1, depth + 1) || !CBB_flush(out)) { return 0; } @@ -171,7 +169,7 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag, if (tag & CBS_ASN1_CONSTRUCTED) { // Recurse into children. if (!cbs_convert_ber(&contents, out_contents, child_string_tag, - 0 /* not looking for eoc */, depth + 1)) { + /*looking_for_eoc=*/0, depth + 1)) { return 0; } } else { diff --git a/Sources/CCryptoBoringSSL/crypto/bytestring/cbs.c b/Sources/CCryptoBoringSSL/crypto/bytestring/cbs.c index 9786f0ac..a38f27f6 100644 --- a/Sources/CCryptoBoringSSL/crypto/bytestring/cbs.c +++ b/Sources/CCryptoBoringSSL/crypto/bytestring/cbs.c @@ -285,7 +285,7 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) { static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, size_t *out_header_len, int *out_ber_found, - int ber_ok) { + int *out_indefinite, int ber_ok) { CBS header = *cbs; CBS throwaway; @@ -294,6 +294,10 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, } if (ber_ok) { *out_ber_found = 0; + *out_indefinite = 0; + } else { + assert(out_ber_found == NULL); + assert(out_indefinite == NULL); } unsigned tag; @@ -333,6 +337,7 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, *out_header_len = header_len; } *out_ber_found = 1; + *out_indefinite = 1; return CBS_get_bytes(cbs, out, header_len); } @@ -395,16 +400,18 @@ int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) { int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, size_t *out_header_len) { - return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, - NULL, 0 /* DER only */); + return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, NULL, NULL, + /*ber_ok=*/0); } int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, - size_t *out_header_len, int *out_ber_found) { + size_t *out_header_len, int *out_ber_found, + int *out_indefinite) { int ber_found_temp; return cbs_get_any_asn1_element( cbs, out, out_tag, out_header_len, - out_ber_found ? out_ber_found : &ber_found_temp, 1 /* BER allowed */); + out_ber_found ? out_ber_found : &ber_found_temp, out_indefinite, + /*ber_ok=*/1); } static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value, diff --git a/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S b/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S index 1ef34da3..84df13db 100644 --- a/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S @@ -67,7 +67,7 @@ Lshort: ldp x24,x25,[x3] // load key ldp x26,x27,[x3,#16] ldp x28,x30,[x4] // load counter -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x24,x24,#32 ror x25,x25,#32 ror x26,x26,#32 @@ -228,7 +228,7 @@ Loop: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -285,7 +285,7 @@ Less_than_64: add x15,x15,x16,lsl#32 add x17,x17,x19,lsl#32 add x20,x20,x21,lsl#32 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -351,7 +351,7 @@ ChaCha20_neon: ldp x28,x30,[x4] // load counter ld1 {v27.4s},[x4] ld1 {v31.4s},[x5] -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev64 v24.4s,v24.4s ror x24,x24,#32 ror x25,x25,#32 @@ -649,7 +649,7 @@ Loop_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -729,7 +729,7 @@ Ltail_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -847,7 +847,7 @@ L512_or_more_neon: ldp x28,x30,[x4] // load counter ld1 {v27.4s},[x4] ld1 {v31.4s},[x5] -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev64 v24.4s,v24.4s ror x24,x24,#32 ror x25,x25,#32 @@ -1360,7 +1360,7 @@ Loop_upper_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -1874,7 +1874,7 @@ Loop_lower_neon: add x1,x1,#64 add v21.4s,v21.4s,v25.4s -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 diff --git a/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S b/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S index 85628640..43cc0d2d 100644 --- a/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S @@ -68,7 +68,7 @@ ChaCha20_ctr32: ldp x24,x25,[x3] // load key ldp x26,x27,[x3,#16] ldp x28,x30,[x4] // load counter -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x24,x24,#32 ror x25,x25,#32 ror x26,x26,#32 @@ -229,7 +229,7 @@ ChaCha20_ctr32: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -286,7 +286,7 @@ ChaCha20_ctr32: add x15,x15,x16,lsl#32 add x17,x17,x19,lsl#32 add x20,x20,x21,lsl#32 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -352,7 +352,7 @@ ChaCha20_neon: ldp x28,x30,[x4] // load counter ld1 {v27.4s},[x4] ld1 {v31.4s},[x5] -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev64 v24.4s,v24.4s ror x24,x24,#32 ror x25,x25,#32 @@ -650,7 +650,7 @@ ChaCha20_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -730,7 +730,7 @@ ChaCha20_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -848,7 +848,7 @@ ChaCha20_512_neon: ldp x28,x30,[x4] // load counter ld1 {v27.4s},[x4] ld1 {v31.4s},[x5] -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev64 v24.4s,v24.4s ror x24,x24,#32 ror x25,x25,#32 @@ -1361,7 +1361,7 @@ ChaCha20_512_neon: add x20,x20,x21,lsl#32 ldp x19,x21,[x1,#48] add x1,x1,#64 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 @@ -1875,7 +1875,7 @@ ChaCha20_512_neon: add x1,x1,#64 add v21.4s,v21.4s,v25.4s -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ rev x5,x5 rev x7,x7 rev x9,x9 diff --git a/Sources/CCryptoBoringSSL/crypto/chacha/chacha.c b/Sources/CCryptoBoringSSL/crypto/chacha/chacha.c index 26da9090..de15c8e7 100644 --- a/Sources/CCryptoBoringSSL/crypto/chacha/chacha.c +++ b/Sources/CCryptoBoringSSL/crypto/chacha/chacha.c @@ -19,8 +19,6 @@ #include #include -#include - #include "../internal.h" #include "internal.h" diff --git a/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesccm.c b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesccm.c index 4f690650..5b85fb91 100644 --- a/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesccm.c +++ b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesccm.c @@ -50,7 +50,6 @@ #include -#include #include #include #include diff --git a/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesgcmsiv.c b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesgcmsiv.c index 2bd5e2bf..9ea43256 100644 --- a/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesgcmsiv.c +++ b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_aesgcmsiv.c @@ -17,11 +17,11 @@ #include #include -#include #include #include #include "../fipsmodule/cipher/internal.h" +#include "../internal.h" #define EVP_AEAD_AES_GCM_SIV_NONCE_LEN 12 @@ -857,22 +857,15 @@ static const EVP_AEAD aead_aes_256_gcm_siv = { #if defined(AES_GCM_SIV_ASM) -static char avx_aesni_capable(void) { - const uint32_t ecx = OPENSSL_ia32cap_P[1]; - - return (ecx & (1 << (57 - 32))) != 0 /* AESNI */ && - (ecx & (1 << 28)) != 0 /* AVX */; -} - const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) { - if (avx_aesni_capable()) { + if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { return &aead_aes_128_gcm_siv_asm; } return &aead_aes_128_gcm_siv; } const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) { - if (avx_aesni_capable()) { + if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { return &aead_aes_256_gcm_siv_asm; } return &aead_aes_256_gcm_siv; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_des.c b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_des.c similarity index 64% rename from Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_des.c rename to Sources/CCryptoBoringSSL/crypto/cipher_extra/e_des.c index 53915d4f..857fd4fd 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_des.c +++ b/Sources/CCryptoBoringSSL/crypto/cipher_extra/e_des.c @@ -59,7 +59,6 @@ #include #include "internal.h" -#include "../delocate.h" typedef struct { @@ -88,17 +87,21 @@ static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, return 1; } -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_cbc) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_cbc; - out->block_size = 8; - out->key_len = 8; - out->iv_len = 8; - out->ctx_size = sizeof(EVP_DES_KEY); - out->flags = EVP_CIPH_CBC_MODE; - out->init = des_init_key; - out->cipher = des_cbc_cipher; -} +static const EVP_CIPHER evp_des_cbc = { + /* nid = */ NID_des_cbc, + /* block_size = */ 8, + /* key_len = */ 8, + /* iv_len = */ 8, + /* ctx_size = */ sizeof(EVP_DES_KEY), + /* flags = */ EVP_CIPH_CBC_MODE, + /* app_data = */ NULL, + /* init = */ des_init_key, + /* cipher = */ des_cbc_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; + +const EVP_CIPHER *EVP_des_cbc(void) { return &evp_des_cbc; } static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len) { @@ -107,25 +110,29 @@ static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, } in_len -= ctx->cipher->block_size; - EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data; + EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) { - DES_ecb_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i), + DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), &dat->ks.ks, ctx->encrypt); } return 1; } -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ecb) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_ecb; - out->block_size = 8; - out->key_len = 8; - out->iv_len = 0; - out->ctx_size = sizeof(EVP_DES_KEY); - out->flags = EVP_CIPH_ECB_MODE; - out->init = des_init_key; - out->cipher = des_ecb_cipher; -} +static const EVP_CIPHER evp_des_ecb = { + /* nid = */ NID_des_ecb, + /* block_size = */ 8, + /* key_len = */ 8, + /* iv_len = */ 0, + /* ctx_size = */ sizeof(EVP_DES_KEY), + /* flags = */ EVP_CIPH_ECB_MODE, + /* app_data = */ NULL, + /* init = */ des_init_key, + /* cipher = */ des_ecb_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; + +const EVP_CIPHER *EVP_des_ecb(void) { return &evp_des_ecb; } typedef struct { union { @@ -137,7 +144,7 @@ typedef struct { static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv, int enc) { DES_cblock *deskey = (DES_cblock *)key; - DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data; + DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; DES_set_key(&deskey[0], &dat->ks.ks[0]); DES_set_key(&deskey[1], &dat->ks.ks[1]); @@ -147,8 +154,8 @@ static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, } static int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, - const uint8_t *in, size_t in_len) { - DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data; + const uint8_t *in, size_t in_len) { + DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; DES_ede3_cbc_encrypt(in, out, in_len, &dat->ks.ks[0], &dat->ks.ks[1], &dat->ks.ks[2], (DES_cblock *)ctx->iv, ctx->encrypt); @@ -156,22 +163,26 @@ static int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, return 1; } -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede3_cbc) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_ede3_cbc; - out->block_size = 8; - out->key_len = 24; - out->iv_len = 8; - out->ctx_size = sizeof(DES_EDE_KEY); - out->flags = EVP_CIPH_CBC_MODE; - out->init = des_ede3_init_key; - out->cipher = des_ede3_cbc_cipher; -} +static const EVP_CIPHER evp_des_ede3_cbc = { + /* nid = */ NID_des_ede3_cbc, + /* block_size = */ 8, + /* key_len = */ 24, + /* iv_len = */ 8, + /* ctx_size = */ sizeof(DES_EDE_KEY), + /* flags = */ EVP_CIPH_CBC_MODE, + /* app_data = */ NULL, + /* init = */ des_ede3_init_key, + /* cipher = */ des_ede3_cbc_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; + +const EVP_CIPHER *EVP_des_ede3_cbc(void) { return &evp_des_ede3_cbc; } static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, - const uint8_t *iv, int enc) { - DES_cblock *deskey = (DES_cblock *) key; - DES_EDE_KEY *dat = (DES_EDE_KEY *) ctx->cipher_data; + const uint8_t *iv, int enc) { + DES_cblock *deskey = (DES_cblock *)key; + DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data; DES_set_key(&deskey[0], &dat->ks.ks[0]); DES_set_key(&deskey[1], &dat->ks.ks[1]); @@ -180,17 +191,21 @@ static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, return 1; } -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede_cbc) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_ede_cbc; - out->block_size = 8; - out->key_len = 16; - out->iv_len = 8; - out->ctx_size = sizeof(DES_EDE_KEY); - out->flags = EVP_CIPH_CBC_MODE; - out->init = des_ede_init_key; - out->cipher = des_ede3_cbc_cipher; -} +static const EVP_CIPHER evp_des_ede_cbc = { + /* nid = */ NID_des_ede_cbc, + /* block_size = */ 8, + /* key_len = */ 16, + /* iv_len = */ 8, + /* ctx_size = */ sizeof(DES_EDE_KEY), + /* flags = */ EVP_CIPH_CBC_MODE, + /* app_data = */ NULL, + /* init = */ des_ede_init_key, + /* cipher = */ des_ede3_cbc_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; + +const EVP_CIPHER *EVP_des_ede_cbc(void) { return &evp_des_ede_cbc; } static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, size_t in_len) { @@ -208,30 +223,36 @@ static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, return 1; } -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_ede_ecb; - out->block_size = 8; - out->key_len = 16; - out->iv_len = 0; - out->ctx_size = sizeof(DES_EDE_KEY); - out->flags = EVP_CIPH_ECB_MODE; - out->init = des_ede_init_key; - out->cipher = des_ede_ecb_cipher; -} +static const EVP_CIPHER evp_des_ede = { + /* nid = */ NID_des_ede_ecb, + /* block_size = */ 8, + /* key_len = */ 16, + /* iv_len = */ 0, + /* ctx_size = */ sizeof(DES_EDE_KEY), + /* flags = */ EVP_CIPH_ECB_MODE, + /* app_data = */ NULL, + /* init = */ des_ede_init_key, + /* cipher = */ des_ede_ecb_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; -DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede3) { - memset(out, 0, sizeof(EVP_CIPHER)); - out->nid = NID_des_ede3_ecb; - out->block_size = 8; - out->key_len = 24; - out->iv_len = 0; - out->ctx_size = sizeof(DES_EDE_KEY); - out->flags = EVP_CIPH_ECB_MODE; - out->init = des_ede3_init_key; - out->cipher = des_ede_ecb_cipher; -} +const EVP_CIPHER *EVP_des_ede(void) { return &evp_des_ede; } -const EVP_CIPHER* EVP_des_ede3_ecb(void) { - return EVP_des_ede3(); -} +static const EVP_CIPHER evp_des_ede3 = { + /* nid = */ NID_des_ede3_ecb, + /* block_size = */ 8, + /* key_len = */ 24, + /* iv_len = */ 0, + /* ctx_size = */ sizeof(DES_EDE_KEY), + /* flags = */ EVP_CIPH_ECB_MODE, + /* app_data = */ NULL, + /* init = */ des_ede3_init_key, + /* cipher = */ des_ede_ecb_cipher, + /* cleanup = */ NULL, + /* ctrl = */ NULL, +}; + +const EVP_CIPHER *EVP_des_ede3(void) { return &evp_des_ede3; } + +const EVP_CIPHER *EVP_des_ede3_ecb(void) { return EVP_des_ede3(); } diff --git a/Sources/CCryptoBoringSSL/crypto/cipher_extra/internal.h b/Sources/CCryptoBoringSSL/crypto/cipher_extra/internal.h index 39c747a4..f54b55b1 100644 --- a/Sources/CCryptoBoringSSL/crypto/cipher_extra/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/cipher_extra/internal.h @@ -60,7 +60,6 @@ #include #include -#include #include #include "../internal.h" @@ -172,8 +171,7 @@ OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8, "wrong chacha20_poly1305_seal_data size"); OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) { - const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0; - return sse41_capable; + return CRYPTO_is_SSE4_1_capable(); } // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_apple.c b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_apple.c index 8eed3491..23386897 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_apple.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_apple.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" #if defined(OPENSSL_AARCH64) && defined(OPENSSL_APPLE) && \ !defined(OPENSSL_STATIC_ARMCAP) @@ -22,8 +22,6 @@ #include -#include "internal.h" - extern uint32_t OPENSSL_armcap_P; @@ -53,7 +51,8 @@ void OPENSSL_cpuid_setup(void) { // Apple ARM64 platforms have NEON and cryptography extensions available // statically, so we do not need to query them. In particular, there sometimes // are no sysctls corresponding to such features. See below. -#if !defined(__ARM_NEON) || !defined(__ARM_FEATURE_CRYPTO) +#if !defined(__ARM_NEON) || !defined(__ARM_FEATURE_AES) || \ + !defined(__ARM_FEATURE_SHA2) #error "NEON and crypto extensions should be statically available." #endif OPENSSL_armcap_P = diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_fuchsia.c b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_fuchsia.c index 747d47e8..a12f0219 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_fuchsia.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_fuchsia.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" #if defined(OPENSSL_AARCH64) && defined(OPENSSL_FUCHSIA) && \ !defined(OPENSSL_STATIC_ARMCAP) @@ -23,16 +23,14 @@ #include -#include "internal.h" - extern uint32_t OPENSSL_armcap_P; void OPENSSL_cpuid_setup(void) { uint32_t hwcap; zx_status_t rc = zx_system_get_features(ZX_FEATURE_KIND_CPU, &hwcap); if (rc != ZX_OK || (hwcap & ZX_ARM64_FEATURE_ISA_ASIMD) == 0) { - // Matching OpenSSL, if NEON/ASIMD is missing, don't report other features - // either. + // If NEON/ASIMD is missing, don't report other features either. This + // matches OpenSSL, and the other features depend on SIMD registers. return; } @@ -47,12 +45,12 @@ void OPENSSL_cpuid_setup(void) { if (hwcap & ZX_ARM64_FEATURE_ISA_SHA1) { OPENSSL_armcap_P |= ARMV8_SHA1; } - if (hwcap & ZX_ARM64_FEATURE_ISA_SHA2) { + if (hwcap & ZX_ARM64_FEATURE_ISA_SHA256) { OPENSSL_armcap_P |= ARMV8_SHA256; } - // As of writing, Fuchsia does not have a flag for ARMv8.2 SHA-512 - // extensions. When it does, add it here. See - // https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=90759. + if (hwcap & ZX_ARM64_FEATURE_ISA_SHA512) { + OPENSSL_armcap_P |= ARMV8_SHA512; + } } #endif // OPENSSL_AARCH64 && OPENSSL_FUCHSIA && !OPENSSL_STATIC_ARMCAP diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_linux.c b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_linux.c index a21d07e9..e41f71ee 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_linux.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_linux.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" #if defined(OPENSSL_AARCH64) && defined(OPENSSL_LINUX) && \ !defined(OPENSSL_STATIC_ARMCAP) @@ -21,8 +21,6 @@ #include -#include "internal.h" - extern uint32_t OPENSSL_armcap_P; diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_win.c b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_win.c index 3af3368b..436577c7 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_win.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_aarch64_win.c @@ -13,7 +13,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" #if defined(OPENSSL_AARCH64) && defined(OPENSSL_WINDOWS) && \ !defined(OPENSSL_STATIC_ARMCAP) @@ -22,8 +22,6 @@ #include -#include "internal.h" - extern uint32_t OPENSSL_armcap_P; void OPENSSL_cpuid_setup(void) { // We do not need to check for the presence of NEON, as Armv8-A always has it diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_arm.c b/Sources/CCryptoBoringSSL/crypto/cpu_arm.c index a532a700..9ee1e706 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_arm.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_arm.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \ !defined(OPENSSL_STATIC_ARMCAP) diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_arm_linux.c b/Sources/CCryptoBoringSSL/crypto/cpu_arm_linux.c index d8c98725..805bf46c 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_arm_linux.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_arm_linux.c @@ -12,9 +12,10 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include "internal.h" -#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP) +#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \ + !defined(OPENSSL_STATIC_ARMCAP) #include #include #include @@ -226,4 +227,4 @@ int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; } int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; } -#endif // OPENSSL_ARM && !OPENSSL_STATIC_ARMCAP +#endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_intel.c b/Sources/CCryptoBoringSSL/crypto/cpu_intel.c index f9baad19..4bb9a720 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_intel.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_intel.c @@ -54,8 +54,7 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] */ -#include - +#include #if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64)) diff --git a/Sources/CCryptoBoringSSL/crypto/cpu_ppc64le.c b/Sources/CCryptoBoringSSL/crypto/cpu_ppc64le.c index d47c4c39..ca95de41 100644 --- a/Sources/CCryptoBoringSSL/crypto/cpu_ppc64le.c +++ b/Sources/CCryptoBoringSSL/crypto/cpu_ppc64le.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include #if defined(OPENSSL_PPC64LE) diff --git a/Sources/CCryptoBoringSSL/crypto/crypto.c b/Sources/CCryptoBoringSSL/crypto/crypto.c index 833a320e..8b92a5bc 100644 --- a/Sources/CCryptoBoringSSL/crypto/crypto.c +++ b/Sources/CCryptoBoringSSL/crypto/crypto.c @@ -14,8 +14,6 @@ #include -#include - #include "fipsmodule/rand/fork_detect.h" #include "fipsmodule/rand/internal.h" #include "internal.h" @@ -88,23 +86,29 @@ HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0; #if defined(OPENSSL_STATIC_ARMCAP) +// See ARM ACLE for the definitions of these macros. Note |__ARM_FEATURE_AES| +// covers both AES and PMULL and |__ARM_FEATURE_SHA2| covers SHA-1 and SHA-256. +// https://developer.arm.com/architectures/system-architectures/software-standards/acle +// https://github.com/ARM-software/acle/issues/152 +// +// TODO(davidben): Do we still need |OPENSSL_STATIC_ARMCAP_*| or are the +// standard flags and -march sufficient? HIDDEN uint32_t OPENSSL_armcap_P = -#if defined(OPENSSL_STATIC_ARMCAP_NEON) || \ - (defined(__ARM_NEON__) || defined(__ARM_NEON)) +#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON) ARMV7_NEON | #endif -#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO) +#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES) ARMV8_AES | #endif -#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO) +#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES) + ARMV8_PMULL | +#endif +#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2) ARMV8_SHA1 | #endif -#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO) +#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2) ARMV8_SHA256 | #endif -#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO) - ARMV8_PMULL | -#endif #if defined(__ARM_FEATURE_SHA512) ARMV8_SHA512 | #endif diff --git a/Sources/CCryptoBoringSSL/crypto/curve25519/curve25519.c b/Sources/CCryptoBoringSSL/crypto/curve25519/curve25519.c index 03493faf..46dd457f 100644 --- a/Sources/CCryptoBoringSSL/crypto/curve25519/curve25519.c +++ b/Sources/CCryptoBoringSSL/crypto/curve25519/curve25519.c @@ -24,7 +24,6 @@ #include #include -#include #include #include #include @@ -503,27 +502,21 @@ static void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) { int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) { fe u; fe_loose v; - fe v3; + fe w; fe vxx; fe_loose check; fe_frombytes(&h->Y, s); fe_1(&h->Z); - fe_sq_tt(&v3, &h->Y); - fe_mul_ttt(&vxx, &v3, &d); - fe_sub(&v, &v3, &h->Z); // u = y^2-1 + fe_sq_tt(&w, &h->Y); + fe_mul_ttt(&vxx, &w, &d); + fe_sub(&v, &w, &h->Z); // u = y^2-1 fe_carry(&u, &v); fe_add(&v, &vxx, &h->Z); // v = dy^2+1 - fe_sq_tl(&v3, &v); - fe_mul_ttl(&v3, &v3, &v); // v3 = v^3 - fe_sq_tt(&h->X, &v3); - fe_mul_ttl(&h->X, &h->X, &v); - fe_mul_ttt(&h->X, &h->X, &u); // x = uv^7 - - fe_pow22523(&h->X, &h->X); // x = (uv^7)^((q-5)/8) - fe_mul_ttt(&h->X, &h->X, &v3); - fe_mul_ttt(&h->X, &h->X, &u); // x = uv^3(uv^7)^((q-5)/8) + fe_mul_ttl(&w, &u, &v); // w = u*v + fe_pow22523(&h->X, &w); // x = w^((q-5)/8) + fe_mul_ttt(&h->X, &h->X, &u); // x = u*w^((q-5)/8) fe_sq_tt(&vxx, &h->X); fe_mul_ttl(&vxx, &vxx, &v); diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/des/des.c b/Sources/CCryptoBoringSSL/crypto/des/des.c similarity index 100% rename from Sources/CCryptoBoringSSL/crypto/fipsmodule/des/des.c rename to Sources/CCryptoBoringSSL/crypto/des/des.c diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/des/internal.h b/Sources/CCryptoBoringSSL/crypto/des/internal.h similarity index 99% rename from Sources/CCryptoBoringSSL/crypto/fipsmodule/des/internal.h rename to Sources/CCryptoBoringSSL/crypto/des/internal.h index 0b642f1b..639d7e8c 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/des/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/des/internal.h @@ -59,7 +59,7 @@ #include -#include "../../internal.h" +#include "../internal.h" #if defined(__cplusplus) extern "C" { diff --git a/Sources/CCryptoBoringSSL/crypto/err/err_data.c b/Sources/CCryptoBoringSSL/crypto/err/err_data.c index 4e4c483c..ab11dce2 100644 --- a/Sources/CCryptoBoringSSL/crypto/err/err_data.c +++ b/Sources/CCryptoBoringSSL/crypto/err/err_data.c @@ -55,185 +55,188 @@ OPENSSL_STATIC_ASSERT(ERR_LIB_USER == 33, "library value changed"); OPENSSL_STATIC_ASSERT(ERR_NUM_LIBS == 34, "number of libraries changed"); const uint32_t kOpenSSLReasonValues[] = { - 0xc320862, - 0xc32887c, - 0xc33088b, - 0xc33889b, - 0xc3408aa, - 0xc3488c3, - 0xc3508cf, - 0xc3588ec, - 0xc36090c, - 0xc36891a, - 0xc37092a, - 0xc378937, - 0xc380947, - 0xc388952, - 0xc390968, - 0xc398977, - 0xc3a098b, - 0xc3a886f, + 0xc320885, + 0xc32889f, + 0xc3308ae, + 0xc3388be, + 0xc3408cd, + 0xc3488e6, + 0xc3508f2, + 0xc35890f, + 0xc36092f, + 0xc36893d, + 0xc37094d, + 0xc37895a, + 0xc38096a, + 0xc388975, + 0xc39098b, + 0xc39899a, + 0xc3a09ae, + 0xc3a8892, 0xc3b00f7, - 0xc3b88fe, - 0x1032086f, - 0x103295e5, - 0x103315f1, - 0x1033960a, - 0x1034161d, - 0x10348f4f, - 0x10350c88, - 0x10359630, - 0x1036165a, - 0x1036966d, - 0x1037168c, - 0x103796a5, - 0x103816ba, - 0x103896d8, - 0x103916e7, - 0x10399703, - 0x103a171e, - 0x103a972d, - 0x103b1749, - 0x103b9764, - 0x103c178a, + 0xc3b8921, + 0x10320892, + 0x10329608, + 0x10331614, + 0x1033962d, + 0x10341640, + 0x10348f72, + 0x10350cab, + 0x10359653, + 0x1036167d, + 0x10369690, + 0x103716af, + 0x103796c8, + 0x103816dd, + 0x103896fb, + 0x1039170a, + 0x10399726, + 0x103a1741, + 0x103a9750, + 0x103b176c, + 0x103b9787, + 0x103c17ad, 0x103c80f7, - 0x103d179b, - 0x103d97af, - 0x103e17ce, - 0x103e97dd, - 0x103f17f4, - 0x103f9807, - 0x10400c4c, - 0x1040981a, - 0x10411838, - 0x1041984b, - 0x10421865, - 0x10429875, - 0x10431889, - 0x1043989f, - 0x104418b7, - 0x104498cc, - 0x104518e0, - 0x104598f2, - 0x10460625, - 0x10468977, - 0x10471907, - 0x1047991e, - 0x10481933, - 0x10489941, - 0x10490e9b, - 0x1049977b, - 0x104a1645, - 0x14320c2f, - 0x14328c3d, - 0x14330c4c, - 0x14338c5e, + 0x103d17be, + 0x103d97d2, + 0x103e17f1, + 0x103e9800, + 0x103f1817, + 0x103f982a, + 0x10400c6f, + 0x1040983d, + 0x1041185b, + 0x1041986e, + 0x10421888, + 0x10429898, + 0x104318ac, + 0x104398c2, + 0x104418da, + 0x104498ef, + 0x10451903, + 0x10459915, + 0x10460635, + 0x1046899a, + 0x1047192a, + 0x10479941, + 0x10481956, + 0x10489964, + 0x10490ebe, + 0x1049979e, + 0x104a1668, + 0x14320c52, + 0x14328c60, + 0x14330c6f, + 0x14338c81, 0x143400b9, 0x143480f7, 0x18320090, - 0x18328fa5, + 0x18328fc8, 0x183300b9, - 0x18338fbb, - 0x18340fcf, + 0x18338fde, + 0x18340ff2, 0x183480f7, - 0x18350fee, - 0x18359006, - 0x1836101b, - 0x1836902f, - 0x18371067, - 0x1837907d, - 0x18381091, - 0x183890a1, - 0x18390a9d, - 0x183990b1, - 0x183a10d7, - 0x183a90fd, - 0x183b0ca7, - 0x183b914c, - 0x183c115e, - 0x183c9169, - 0x183d1179, - 0x183d918a, - 0x183e119b, - 0x183e91ad, - 0x183f11d6, - 0x183f91ef, - 0x18401207, - 0x184086fd, - 0x18411120, - 0x184190eb, - 0x1842110a, - 0x18428c94, - 0x184310c6, - 0x18439132, - 0x18440fe4, - 0x18449053, - 0x20321241, - 0x2032922e, - 0x2432124d, - 0x243289bd, - 0x2433125f, - 0x2433926c, - 0x24341279, - 0x2434928b, - 0x2435129a, - 0x243592b7, - 0x243612c4, - 0x243692d2, - 0x243712e0, - 0x243792ee, - 0x243812f7, - 0x24389304, - 0x24391317, - 0x28320c7c, - 0x28328ca7, - 0x28330c4c, - 0x28338cba, - 0x28340c88, + 0x18351011, + 0x18359029, + 0x1836103e, + 0x18369052, + 0x1837108a, + 0x183790a0, + 0x183810b4, + 0x183890c4, + 0x18390ac0, + 0x183990d4, + 0x183a10fa, + 0x183a9120, + 0x183b0cca, + 0x183b916f, + 0x183c1181, + 0x183c918c, + 0x183d119c, + 0x183d91ad, + 0x183e11be, + 0x183e91d0, + 0x183f11f9, + 0x183f9212, + 0x1840122a, + 0x1840870d, + 0x18411143, + 0x1841910e, + 0x1842112d, + 0x18428cb7, + 0x184310e9, + 0x18439155, + 0x18441007, + 0x18449076, + 0x20321264, + 0x20329251, + 0x24321270, + 0x243289e0, + 0x24331282, + 0x2433928f, + 0x2434129c, + 0x243492ae, + 0x243512bd, + 0x243592da, + 0x243612e7, + 0x243692f5, + 0x24371303, + 0x24379311, + 0x2438131a, + 0x24389327, + 0x2439133a, + 0x28320c9f, + 0x28328cca, + 0x28330c6f, + 0x28338cdd, + 0x28340cab, 0x283480b9, 0x283500f7, - 0x28358c94, - 0x2c323286, - 0x2c32932e, - 0x2c333294, - 0x2c33b2a6, - 0x2c3432ba, - 0x2c34b2cc, - 0x2c3532e7, - 0x2c35b2f9, - 0x2c363329, + 0x28358cb7, + 0x2c3232a7, + 0x2c329351, + 0x2c3332b5, + 0x2c33b2c7, + 0x2c3432db, + 0x2c34b2ed, + 0x2c353308, + 0x2c35b31a, + 0x2c36334a, 0x2c36833a, - 0x2c373336, - 0x2c37b362, - 0x2c383387, - 0x2c38b39e, - 0x2c3933bc, - 0x2c39b3cc, - 0x2c3a33de, - 0x2c3ab3f2, - 0x2c3b3403, - 0x2c3bb422, - 0x2c3c1340, - 0x2c3c9356, - 0x2c3d3436, - 0x2c3d936f, - 0x2c3e3453, - 0x2c3eb461, - 0x2c3f3479, - 0x2c3fb491, - 0x2c4034bb, - 0x2c409241, - 0x2c4134cc, - 0x2c41b4df, - 0x2c421207, - 0x2c42b4f0, - 0x2c43074a, - 0x2c43b414, - 0x2c443375, - 0x2c44b49e, - 0x2c45330c, - 0x2c45b348, - 0x2c4633ac, + 0x2c373357, + 0x2c37b383, + 0x2c3833a8, + 0x2c38b3bf, + 0x2c3933dd, + 0x2c39b3ed, + 0x2c3a33ff, + 0x2c3ab413, + 0x2c3b3424, + 0x2c3bb443, + 0x2c3c1363, + 0x2c3c9379, + 0x2c3d3488, + 0x2c3d9392, + 0x2c3e34b2, + 0x2c3eb4c0, + 0x2c3f34d8, + 0x2c3fb4f0, + 0x2c40351a, + 0x2c409264, + 0x2c41352b, + 0x2c41b53e, + 0x2c42122a, + 0x2c42b54f, + 0x2c43076d, + 0x2c43b435, + 0x2c443396, + 0x2c44b4fd, + 0x2c45332d, + 0x2c45b369, + 0x2c4633cd, + 0x2c46b457, + 0x2c47346c, + 0x2c47b4a5, 0x30320000, 0x30328015, 0x3033001f, @@ -278,528 +281,530 @@ const uint32_t kOpenSSLReasonValues[] = { 0x3046833a, 0x30470372, 0x30478384, - 0x30480392, - 0x304883a3, - 0x304903b2, - 0x304983ca, - 0x304a03dc, - 0x304a83f0, - 0x304b0408, - 0x304b841b, - 0x304c0426, - 0x304c8437, - 0x304d0443, - 0x304d8459, - 0x304e0467, - 0x304e847d, - 0x304f048f, - 0x304f84a1, - 0x305004c4, - 0x305084d7, - 0x305104e8, - 0x305184f8, - 0x30520510, - 0x30528525, - 0x3053053d, - 0x30538551, - 0x30540569, - 0x30548582, - 0x3055059b, - 0x305585b8, - 0x305605c3, - 0x305685db, - 0x305705eb, - 0x305785fc, - 0x3058060f, - 0x30588625, - 0x3059062e, - 0x30598643, - 0x305a0656, - 0x305a8665, - 0x305b0685, - 0x305b8694, - 0x305c06b5, - 0x305c86d1, - 0x305d06dd, - 0x305d86fd, - 0x305e0719, - 0x305e872a, - 0x305f0740, - 0x305f874a, - 0x306004b4, + 0x304803a2, + 0x304883b3, + 0x304903c2, + 0x304983da, + 0x304a03ec, + 0x304a8400, + 0x304b0418, + 0x304b842b, + 0x304c0436, + 0x304c8447, + 0x304d0453, + 0x304d8469, + 0x304e0477, + 0x304e848d, + 0x304f049f, + 0x304f84b1, + 0x305004d4, + 0x305084e7, + 0x305104f8, + 0x30518508, + 0x30520520, + 0x30528535, + 0x3053054d, + 0x30538561, + 0x30540579, + 0x30548592, + 0x305505ab, + 0x305585c8, + 0x305605d3, + 0x305685eb, + 0x305705fb, + 0x3057860c, + 0x3058061f, + 0x30588635, + 0x3059063e, + 0x30598653, + 0x305a0666, + 0x305a8675, + 0x305b0695, + 0x305b86a4, + 0x305c06c5, + 0x305c86e1, + 0x305d06ed, + 0x305d870d, + 0x305e0729, + 0x305e874d, + 0x305f0763, + 0x305f876d, + 0x306004c4, 0x3060804a, 0x30610357, - 0x34320b8d, - 0x34328ba1, - 0x34330bbe, - 0x34338bd1, - 0x34340be0, - 0x34348c19, - 0x34350bfd, + 0x3061873a, + 0x30620392, + 0x34320bb0, + 0x34328bc4, + 0x34330be1, + 0x34338bf4, + 0x34340c03, + 0x34348c3c, + 0x34350c20, 0x3c320090, - 0x3c328ce4, - 0x3c330cfd, - 0x3c338d18, - 0x3c340d35, - 0x3c348d5f, - 0x3c350d7a, - 0x3c358da0, - 0x3c360db9, - 0x3c368dd1, - 0x3c370de2, - 0x3c378df0, - 0x3c380dfd, - 0x3c388e11, - 0x3c390ca7, - 0x3c398e34, - 0x3c3a0e48, - 0x3c3a8937, - 0x3c3b0e58, - 0x3c3b8e73, - 0x3c3c0e85, - 0x3c3c8eb8, - 0x3c3d0ec2, - 0x3c3d8ed6, - 0x3c3e0ee4, - 0x3c3e8f09, - 0x3c3f0cd0, - 0x3c3f8ef2, + 0x3c328d07, + 0x3c330d20, + 0x3c338d3b, + 0x3c340d58, + 0x3c348d82, + 0x3c350d9d, + 0x3c358dc3, + 0x3c360ddc, + 0x3c368df4, + 0x3c370e05, + 0x3c378e13, + 0x3c380e20, + 0x3c388e34, + 0x3c390cca, + 0x3c398e57, + 0x3c3a0e6b, + 0x3c3a895a, + 0x3c3b0e7b, + 0x3c3b8e96, + 0x3c3c0ea8, + 0x3c3c8edb, + 0x3c3d0ee5, + 0x3c3d8ef9, + 0x3c3e0f07, + 0x3c3e8f2c, + 0x3c3f0cf3, + 0x3c3f8f15, 0x3c4000b9, 0x3c4080f7, - 0x3c410d50, - 0x3c418d8f, - 0x3c420e9b, - 0x3c428e25, - 0x403219d3, - 0x403299e9, - 0x40331a17, - 0x40339a21, - 0x40341a38, - 0x40349a56, - 0x40351a66, - 0x40359a78, - 0x40361a85, - 0x40369a91, - 0x40371aa6, - 0x40379ab8, - 0x40381ac3, - 0x40389ad5, - 0x40390f4f, - 0x40399ae5, - 0x403a1af8, - 0x403a9b19, - 0x403b1b2a, - 0x403b9b3a, + 0x3c410d73, + 0x3c418db2, + 0x3c420ebe, + 0x3c428e48, + 0x403219f6, + 0x40329a0c, + 0x40331a3a, + 0x40339a44, + 0x40341a5b, + 0x40349a79, + 0x40351a89, + 0x40359a9b, + 0x40361aa8, + 0x40369ab4, + 0x40371ac9, + 0x40379adb, + 0x40381ae6, + 0x40389af8, + 0x40390f72, + 0x40399b08, + 0x403a1b1b, + 0x403a9b3c, + 0x403b1b4d, + 0x403b9b5d, 0x403c0071, 0x403c8090, - 0x403d1b9b, - 0x403d9bb1, - 0x403e1bc0, - 0x403e9bf8, - 0x403f1c12, - 0x403f9c3a, - 0x40401c4f, - 0x40409c63, - 0x40411c9e, - 0x40419cb9, - 0x40421cd2, - 0x40429ce5, - 0x40431cf9, - 0x40439d27, - 0x40441d3e, + 0x403d1bbe, + 0x403d9bd4, + 0x403e1be3, + 0x403e9c1b, + 0x403f1c35, + 0x403f9c5d, + 0x40401c72, + 0x40409c86, + 0x40411cc1, + 0x40419cdc, + 0x40421cf5, + 0x40429d08, + 0x40431d1c, + 0x40439d4a, + 0x40441d61, 0x404480b9, - 0x40451d53, - 0x40459d65, - 0x40461d89, - 0x40469da9, - 0x40471db7, - 0x40479dde, - 0x40481e4f, - 0x40489f09, - 0x40491f20, - 0x40499f3a, - 0x404a1f51, - 0x404a9f6f, - 0x404b1f87, - 0x404b9fb4, - 0x404c1fca, - 0x404c9fdc, - 0x404d1ffd, - 0x404da036, - 0x404e204a, - 0x404ea057, - 0x404f20f1, - 0x404fa167, - 0x405021be, - 0x4050a1d2, - 0x40512205, - 0x40522215, - 0x4052a239, - 0x40532251, - 0x4053a264, - 0x40542279, - 0x4054a29c, - 0x405522c7, - 0x4055a304, - 0x40562329, - 0x4056a342, - 0x4057235a, - 0x4057a36d, - 0x40582382, - 0x4058a3a9, - 0x405923d8, - 0x4059a405, - 0x405a2419, - 0x405aa429, - 0x405b2441, - 0x405ba452, - 0x405c2465, - 0x405ca4a4, - 0x405d24b1, - 0x405da4d6, - 0x405e2514, - 0x405e8adb, - 0x405f254f, - 0x405fa55c, - 0x4060256a, - 0x4060a58c, - 0x406125ed, - 0x4061a625, - 0x4062263c, - 0x4062a64d, - 0x4063269a, - 0x4063a6af, - 0x406426c6, - 0x4064a6f2, - 0x4065270d, - 0x4065a724, - 0x4066273c, - 0x4066a766, - 0x40672791, - 0x4067a7d6, - 0x4068281e, - 0x4068a83f, - 0x40692871, - 0x4069a89f, - 0x406a28c0, - 0x406aa8e0, - 0x406b2a68, - 0x406baa8b, - 0x406c2aa1, - 0x406cadab, - 0x406d2dda, - 0x406dae02, - 0x406e2e30, - 0x406eae7d, - 0x406f2ed6, - 0x406faf0e, - 0x40702f21, - 0x4070af3e, - 0x4071082a, - 0x4071af50, - 0x40722f63, - 0x4072af99, - 0x40732fb1, - 0x40739540, - 0x40742fc5, - 0x4074afdf, - 0x40752ff0, - 0x4075b004, - 0x40763012, - 0x40769304, - 0x40773037, - 0x4077b077, - 0x40783092, - 0x4078b0cb, - 0x407930e2, - 0x4079b0f8, - 0x407a3124, - 0x407ab137, - 0x407b314c, - 0x407bb15e, - 0x407c318f, - 0x407cb198, - 0x407d285a, - 0x407da177, - 0x407e30a7, - 0x407ea3b9, - 0x407f1dcb, - 0x407f9f9e, - 0x40802101, - 0x40809df3, - 0x40812227, - 0x4081a0a5, - 0x40822e1b, - 0x40829b46, - 0x40832394, - 0x4083a6d7, - 0x40841e07, - 0x4084a3f1, - 0x40852476, - 0x4085a5b4, - 0x408624f6, - 0x4086a191, - 0x40872e61, - 0x4087a602, - 0x40881b84, - 0x4088a7e9, - 0x40891bd3, - 0x40899b60, - 0x408a2ad9, - 0x408a9958, - 0x408b3173, - 0x408baeeb, - 0x408c2486, - 0x408c9990, - 0x408d1eef, - 0x408d9e39, - 0x408e201f, - 0x408ea2e4, - 0x408f27fd, - 0x408fa5d0, - 0x409027b2, - 0x4090a4c8, - 0x40912ac1, - 0x409199b6, - 0x40921c20, - 0x4092ae9c, - 0x40932f7c, - 0x4093a1a2, - 0x40941e1b, - 0x4094aaf2, - 0x4095265e, - 0x4095b104, - 0x40962e48, - 0x4096a11a, - 0x409721ed, - 0x4097a06e, - 0x40981c80, - 0x4098a672, - 0x40992eb8, - 0x4099a311, - 0x409a22aa, - 0x409a9974, - 0x409b1e75, - 0x409b9ea0, - 0x409c3059, - 0x409c9ec8, - 0x409d20d6, - 0x409da0bb, - 0x409e1d11, - 0x409ea14f, - 0x409f2137, - 0x409f9e68, - 0x40a02535, - 0x40a0a088, - 0x41f42993, - 0x41f92a25, - 0x41fe2918, - 0x41feabce, - 0x41ff2cfc, - 0x420329ac, - 0x420829ce, - 0x4208aa0a, - 0x420928fc, - 0x4209aa44, - 0x420a2953, - 0x420aa933, - 0x420b2973, - 0x420ba9ec, - 0x420c2d18, - 0x420cab02, - 0x420d2bb5, - 0x420dabec, - 0x42122c1f, - 0x42172cdf, - 0x4217ac61, - 0x421c2c83, - 0x421f2c3e, - 0x42212d90, - 0x42262cc2, - 0x422b2d6e, - 0x422bab90, - 0x422c2d50, - 0x422cab43, - 0x422d2b1c, - 0x422dad2f, - 0x422e2b6f, - 0x42302c9e, - 0x4230ac06, - 0x44320755, - 0x44328764, - 0x44330770, - 0x4433877e, - 0x44340791, - 0x443487a2, - 0x443507a9, - 0x443587b3, - 0x443607c6, - 0x443687dc, - 0x443707ee, - 0x443787fb, - 0x4438080a, - 0x44388812, - 0x4439082a, - 0x44398838, - 0x443a084b, - 0x4832132e, - 0x48329340, - 0x48331356, - 0x4833936f, - 0x4c321394, - 0x4c3293a4, - 0x4c3313b7, - 0x4c3393d7, + 0x40451d76, + 0x40459d88, + 0x40461dac, + 0x40469dcc, + 0x40471dda, + 0x40479e01, + 0x40481e72, + 0x40489f2c, + 0x40491f43, + 0x40499f5d, + 0x404a1f74, + 0x404a9f92, + 0x404b1faa, + 0x404b9fd7, + 0x404c1fed, + 0x404c9fff, + 0x404d2020, + 0x404da059, + 0x404e206d, + 0x404ea07a, + 0x404f2114, + 0x404fa18a, + 0x405021f9, + 0x4050a20d, + 0x40512240, + 0x40522250, + 0x4052a274, + 0x4053228c, + 0x4053a29f, + 0x405422b4, + 0x4054a2d7, + 0x40552302, + 0x4055a33f, + 0x40562364, + 0x4056a37d, + 0x40572395, + 0x4057a3a8, + 0x405823bd, + 0x4058a3e4, + 0x40592413, + 0x4059a440, + 0x405a2454, + 0x405aa464, + 0x405b247c, + 0x405ba48d, + 0x405c24a0, + 0x405ca4df, + 0x405d24ec, + 0x405da511, + 0x405e254f, + 0x405e8afe, + 0x405f2570, + 0x405fa57d, + 0x4060258b, + 0x4060a5ad, + 0x4061260e, + 0x4061a646, + 0x4062265d, + 0x4062a66e, + 0x406326bb, + 0x4063a6d0, + 0x406426e7, + 0x4064a713, + 0x4065272e, + 0x4065a745, + 0x4066275d, + 0x4066a787, + 0x406727b2, + 0x4067a7f7, + 0x4068283f, + 0x4068a860, + 0x40692892, + 0x4069a8c0, + 0x406a28e1, + 0x406aa901, + 0x406b2a89, + 0x406baaac, + 0x406c2ac2, + 0x406cadcc, + 0x406d2dfb, + 0x406dae23, + 0x406e2e51, + 0x406eae9e, + 0x406f2ef7, + 0x406faf2f, + 0x40702f42, + 0x4070af5f, + 0x4071084d, + 0x4071af71, + 0x40722f84, + 0x4072afba, + 0x40732fd2, + 0x40739563, + 0x40742fe6, + 0x4074b000, + 0x40753011, + 0x4075b025, + 0x40763033, + 0x40769327, + 0x40773058, + 0x4077b098, + 0x407830b3, + 0x4078b0ec, + 0x40793103, + 0x4079b119, + 0x407a3145, + 0x407ab158, + 0x407b316d, + 0x407bb17f, + 0x407c31b0, + 0x407cb1b9, + 0x407d287b, + 0x407da1b2, + 0x407e30c8, + 0x407ea3f4, + 0x407f1dee, + 0x407f9fc1, + 0x40802124, + 0x40809e16, + 0x40812262, + 0x4081a0c8, + 0x40822e3c, + 0x40829b69, + 0x408323cf, + 0x4083a6f8, + 0x40841e2a, + 0x4084a42c, + 0x408524b1, + 0x4085a5d5, + 0x40862531, + 0x4086a1cc, + 0x40872e82, + 0x4087a623, + 0x40881ba7, + 0x4088a80a, + 0x40891bf6, + 0x40899b83, + 0x408a2afa, + 0x408a997b, + 0x408b3194, + 0x408baf0c, + 0x408c24c1, + 0x408c99b3, + 0x408d1f12, + 0x408d9e5c, + 0x408e2042, + 0x408ea31f, + 0x408f281e, + 0x408fa5f1, + 0x409027d3, + 0x4090a503, + 0x40912ae2, + 0x409199d9, + 0x40921c43, + 0x4092aebd, + 0x40932f9d, + 0x4093a1dd, + 0x40941e3e, + 0x4094ab13, + 0x4095267f, + 0x4095b125, + 0x40962e69, + 0x4096a13d, + 0x40972228, + 0x4097a091, + 0x40981ca3, + 0x4098a693, + 0x40992ed9, + 0x4099a34c, + 0x409a22e5, + 0x409a9997, + 0x409b1e98, + 0x409b9ec3, + 0x409c307a, + 0x409c9eeb, + 0x409d20f9, + 0x409da0de, + 0x409e1d34, + 0x409ea172, + 0x409f215a, + 0x409f9e8b, + 0x40a0219a, + 0x40a0a0ab, + 0x41f429b4, + 0x41f92a46, + 0x41fe2939, + 0x41feabef, + 0x41ff2d1d, + 0x420329cd, + 0x420829ef, + 0x4208aa2b, + 0x4209291d, + 0x4209aa65, + 0x420a2974, + 0x420aa954, + 0x420b2994, + 0x420baa0d, + 0x420c2d39, + 0x420cab23, + 0x420d2bd6, + 0x420dac0d, + 0x42122c40, + 0x42172d00, + 0x4217ac82, + 0x421c2ca4, + 0x421f2c5f, + 0x42212db1, + 0x42262ce3, + 0x422b2d8f, + 0x422babb1, + 0x422c2d71, + 0x422cab64, + 0x422d2b3d, + 0x422dad50, + 0x422e2b90, + 0x42302cbf, + 0x4230ac27, + 0x44320778, + 0x44328787, + 0x44330793, + 0x443387a1, + 0x443407b4, + 0x443487c5, + 0x443507cc, + 0x443587d6, + 0x443607e9, + 0x443687ff, + 0x44370811, + 0x4437881e, + 0x4438082d, + 0x44388835, + 0x4439084d, + 0x4439885b, + 0x443a086e, + 0x48321351, + 0x48329363, + 0x48331379, + 0x48339392, + 0x4c3213b7, + 0x4c3293c7, + 0x4c3313da, + 0x4c3393fa, 0x4c3400b9, 0x4c3480f7, - 0x4c3513e3, - 0x4c3593f1, - 0x4c36140d, - 0x4c369433, - 0x4c371442, - 0x4c379450, - 0x4c381465, - 0x4c389471, - 0x4c391491, - 0x4c3994bb, - 0x4c3a14d4, - 0x4c3a94ed, - 0x4c3b0625, - 0x4c3b9506, - 0x4c3c1518, - 0x4c3c9527, - 0x4c3d1540, - 0x4c3d8c6f, - 0x4c3e15ad, - 0x4c3e954f, - 0x4c3f15cf, - 0x4c3f9304, - 0x4c401565, - 0x4c409380, - 0x4c41159d, - 0x4c419420, - 0x4c421589, - 0x50323502, - 0x5032b511, - 0x5033351c, - 0x5033b52c, - 0x50343545, - 0x5034b55f, - 0x5035356d, - 0x5035b583, - 0x50363595, - 0x5036b5ab, - 0x503735c4, - 0x5037b5d7, - 0x503835ef, - 0x5038b600, - 0x50393615, - 0x5039b629, - 0x503a3649, - 0x503ab65f, - 0x503b3677, - 0x503bb689, - 0x503c36a5, - 0x503cb6bc, - 0x503d36d5, - 0x503db6eb, - 0x503e36f8, - 0x503eb70e, - 0x503f3720, - 0x503f83a3, - 0x50403733, - 0x5040b743, - 0x5041375d, - 0x5041b76c, - 0x50423786, - 0x5042b7a3, - 0x504337b3, - 0x5043b7c3, - 0x504437e0, - 0x50448459, - 0x504537f4, - 0x5045b812, - 0x50463825, - 0x5046b83b, - 0x5047384d, - 0x5047b862, - 0x50483888, - 0x5048b896, - 0x504938a9, - 0x5049b8be, - 0x504a38d4, - 0x504ab8e4, - 0x504b3904, - 0x504bb917, - 0x504c393a, - 0x504cb968, - 0x504d3995, - 0x504db9b2, - 0x504e39cd, - 0x504eb9e9, - 0x504f39fb, - 0x504fba12, - 0x50503a21, - 0x50508719, - 0x50513a34, - 0x5051b7d2, - 0x5052397a, - 0x58320f8d, - 0x68320f4f, - 0x68328ca7, - 0x68330cba, - 0x68338f5d, - 0x68340f6d, + 0x4c351406, + 0x4c359414, + 0x4c361430, + 0x4c369456, + 0x4c371465, + 0x4c379473, + 0x4c381488, + 0x4c389494, + 0x4c3914b4, + 0x4c3994de, + 0x4c3a14f7, + 0x4c3a9510, + 0x4c3b0635, + 0x4c3b9529, + 0x4c3c153b, + 0x4c3c954a, + 0x4c3d1563, + 0x4c3d8c92, + 0x4c3e15d0, + 0x4c3e9572, + 0x4c3f15f2, + 0x4c3f9327, + 0x4c401588, + 0x4c4093a3, + 0x4c4115c0, + 0x4c419443, + 0x4c4215ac, + 0x50323561, + 0x5032b570, + 0x5033357b, + 0x5033b58b, + 0x503435a4, + 0x5034b5be, + 0x503535cc, + 0x5035b5e2, + 0x503635f4, + 0x5036b60a, + 0x50373623, + 0x5037b636, + 0x5038364e, + 0x5038b65f, + 0x50393674, + 0x5039b688, + 0x503a36a8, + 0x503ab6be, + 0x503b36d6, + 0x503bb6e8, + 0x503c3704, + 0x503cb71b, + 0x503d3734, + 0x503db74a, + 0x503e3757, + 0x503eb76d, + 0x503f377f, + 0x503f83b3, + 0x50403792, + 0x5040b7a2, + 0x504137bc, + 0x5041b7cb, + 0x504237e5, + 0x5042b802, + 0x50433812, + 0x5043b822, + 0x5044383f, + 0x50448469, + 0x50453853, + 0x5045b871, + 0x50463884, + 0x5046b89a, + 0x504738ac, + 0x5047b8c1, + 0x504838e7, + 0x5048b8f5, + 0x50493908, + 0x5049b91d, + 0x504a3933, + 0x504ab943, + 0x504b3963, + 0x504bb976, + 0x504c3999, + 0x504cb9c7, + 0x504d39f4, + 0x504dba11, + 0x504e3a2c, + 0x504eba48, + 0x504f3a5a, + 0x504fba71, + 0x50503a80, + 0x50508729, + 0x50513a93, + 0x5051b831, + 0x505239d9, + 0x58320fb0, + 0x68320f72, + 0x68328cca, + 0x68330cdd, + 0x68338f80, + 0x68340f90, 0x683480f7, - 0x6c320f15, - 0x6c328c5e, - 0x6c330f20, - 0x6c338f39, - 0x74320a43, + 0x6c320f38, + 0x6c328c81, + 0x6c330f43, + 0x6c338f5c, + 0x74320a66, 0x743280b9, - 0x74330c6f, - 0x783209a8, - 0x783289bd, - 0x783309c9, + 0x74330c92, + 0x783209cb, + 0x783289e0, + 0x783309ec, 0x78338090, - 0x783409d8, - 0x783489ed, - 0x78350a0c, - 0x78358a2e, - 0x78360a43, - 0x78368a59, - 0x78370a69, - 0x78378a8a, - 0x78380a9d, - 0x78388aaf, - 0x78390abc, - 0x78398adb, - 0x783a0af0, - 0x783a8afe, - 0x783b0b08, - 0x783b8b1c, - 0x783c0b33, - 0x783c8b48, - 0x783d0b5f, - 0x783d8b74, - 0x783e0aca, - 0x783e8a7c, - 0x7c32121d, - 0x80321433, + 0x783409fb, + 0x78348a10, + 0x78350a2f, + 0x78358a51, + 0x78360a66, + 0x78368a7c, + 0x78370a8c, + 0x78378aad, + 0x78380ac0, + 0x78388ad2, + 0x78390adf, + 0x78398afe, + 0x783a0b13, + 0x783a8b21, + 0x783b0b2b, + 0x783b8b3f, + 0x783c0b56, + 0x783c8b6b, + 0x783d0b82, + 0x783d8b97, + 0x783e0aed, + 0x783e8a9f, + 0x7c321240, + 0x80321456, 0x80328090, - 0x80333255, + 0x80333276, 0x803380b9, - 0x80343264, - 0x8034b1cc, - 0x803531ea, - 0x8035b278, - 0x8036322c, - 0x8036b1db, - 0x8037321e, - 0x8037b1b9, - 0x8038323f, - 0x8038b1fb, - 0x80393210, + 0x80343285, + 0x8034b1ed, + 0x8035320b, + 0x8035b299, + 0x8036324d, + 0x8036b1fc, + 0x8037323f, + 0x8037b1da, + 0x80383260, + 0x8038b21c, + 0x80393231, }; const size_t kOpenSSLReasonValuesLen = sizeof(kOpenSSLReasonValues) / sizeof(kOpenSSLReasonValues[0]); @@ -851,6 +856,7 @@ const char kOpenSSLReasonStringData[] = "INVALID_BIT_STRING_PADDING\0" "INVALID_BMPSTRING\0" "INVALID_DIGIT\0" + "INVALID_INTEGER\0" "INVALID_MODIFIER\0" "INVALID_NUMBER\0" "INVALID_OBJECT_ENCODING\0" @@ -897,6 +903,7 @@ const char kOpenSSLReasonStringData[] = "UNSUPPORTED_ANY_DEFINED_BY_TYPE\0" "UNSUPPORTED_PUBLIC_KEY_TYPE\0" "UNSUPPORTED_TYPE\0" + "WRONG_INTEGER_TYPE\0" "WRONG_PUBLIC_KEY_TYPE\0" "WRONG_TAG\0" "WRONG_TYPE\0" @@ -1223,6 +1230,7 @@ const char kOpenSSLReasonStringData[] = "INVALID_ECH_CONFIG_LIST\0" "INVALID_ECH_PUBLIC_NAME\0" "INVALID_MESSAGE\0" + "INVALID_OUTER_EXTENSION\0" "INVALID_OUTER_RECORD_TYPE\0" "INVALID_SCT_LIST\0" "INVALID_SIGNATURE_ALGORITHM\0" @@ -1266,7 +1274,6 @@ const char kOpenSSLReasonStringData[] = "OLD_SESSION_CIPHER_NOT_RETURNED\0" "OLD_SESSION_PRF_HASH_MISMATCH\0" "OLD_SESSION_VERSION_NOT_RETURNED\0" - "OUTER_EXTENSION_NOT_FOUND\0" "PARSE_TLSEXT\0" "PATH_TOO_LONG\0" "PEER_DID_NOT_RETURN_A_CERTIFICATE\0" @@ -1417,7 +1424,10 @@ const char kOpenSSLReasonStringData[] = "LOADING_DEFAULTS\0" "NAME_TOO_LONG\0" "NEWER_CRL_NOT_NEWER\0" + "NO_CERTIFICATE_FOUND\0" + "NO_CERTIFICATE_OR_CRL_FOUND\0" "NO_CERT_SET_FOR_US_TO_VERIFY\0" + "NO_CRL_FOUND\0" "NO_CRL_NUMBER\0" "PUBLIC_KEY_DECODE_ERROR\0" "PUBLIC_KEY_ENCODE_ERROR\0" diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/aes.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/aes.c index f6a5fba1..ade87963 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/aes.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/aes.c @@ -50,8 +50,6 @@ #include -#include - #include "internal.h" #include "../modes/internal.h" diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/internal.h index fcb267c9..0685bc41 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aes/internal.h @@ -17,7 +17,7 @@ #include -#include +#include "../../internal.h" #if defined(__cplusplus) extern "C" { @@ -30,18 +30,14 @@ extern "C" { #define HWAES #define HWAES_ECB -OPENSSL_INLINE int hwaes_capable(void) { - return (OPENSSL_ia32cap_get()[1] & (1 << (57 - 32))) != 0; -} +OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_AESNI_capable(); } #define VPAES #if defined(OPENSSL_X86_64) #define VPAES_CTR32 #endif #define VPAES_CBC -OPENSSL_INLINE int vpaes_capable(void) { - return (OPENSSL_ia32cap_get()[1] & (1 << (41 - 32))) != 0; -} +OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_SSSE3_capable(); } #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) #define HWAES diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S index 97f9c148..dbeebd95 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S @@ -636,7 +636,7 @@ _aes_hw_ctr32_encrypt_blocks: // // [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice // [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev w8, w8 #endif add w10, w8, #1 diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S index 24471de3..c9f45270 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S @@ -637,7 +637,7 @@ aes_hw_ctr32_encrypt_blocks: // // [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice // [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev w8, w8 #endif add w10, w8, #1 diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/exponentiation.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/exponentiation.c index e5ea77ad..ebf7e3db 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/exponentiation.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/exponentiation.c @@ -112,7 +112,6 @@ #include #include -#include #include #include diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/rsaz_exp.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/rsaz_exp.h index 38e07798..190969d1 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/rsaz_exp.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/rsaz_exp.h @@ -16,9 +16,9 @@ #define OPENSSL_HEADER_BN_RSAZ_EXP_H #include -#include #include "internal.h" +#include "../../internal.h" #if defined(__cplusplus) extern "C" { @@ -41,18 +41,17 @@ void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16], const BN_ULONG base_norm[16], BN_ULONG storage_words[MOD_EXP_CTIME_STORAGE_LEN]); OPENSSL_INLINE int rsaz_avx2_capable(void) { - const uint32_t *cap = OPENSSL_ia32cap_get(); - return (cap[2] & (1 << 5)) != 0; // AVX2 + return CRYPTO_is_AVX2_capable(); } OPENSSL_INLINE int rsaz_avx2_preferred(void) { - const uint32_t *cap = OPENSSL_ia32cap_get(); - static const uint32_t kBMI2AndADX = (1 << 8) | (1 << 19); - if ((cap[2] & kBMI2AndADX) == kBMI2AndADX) { - // If BMI2 and ADX are available, x86_64-mont5.pl is faster. + if (CRYPTO_is_BMI1_capable() && CRYPTO_is_BMI2_capable() && + CRYPTO_is_ADX_capable()) { + // If BMI1, BMI2, and ADX are available, x86_64-mont5.pl is faster. See the + // .Lmulx4x_enter and .Lpowerx5_enter branches. return 0; } - return (cap[2] & (1 << 5)) != 0; // AVX2 + return CRYPTO_is_AVX2_capable(); } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/sqrt.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/sqrt.c index 0f47df7e..f3ec5aec 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/sqrt.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/bn/sqrt.c @@ -306,8 +306,7 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { } // x := a^((q-1)/2) - if (BN_is_zero(t)) // special case: p = 2^e + 1 - { + if (BN_is_zero(t)) { // special case: p = 2^e + 1 if (!BN_nnmod(t, A, p, ctx)) { goto end; } @@ -350,7 +349,6 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { // We have a*b = x^2, // y^2^(e-1) = -1, // b^2^(e-1) = 1. - if (BN_is_one(b)) { if (!BN_copy(ret, x)) { goto end; @@ -359,23 +357,26 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { goto vrfy; } - - // find smallest i such that b^(2^i) = 1 - i = 1; - if (!BN_mod_sqr(t, b, p, ctx)) { + // Find the smallest i, 0 < i < e, such that b^(2^i) = 1 + for (i = 1; i < e; i++) { + if (i == 1) { + if (!BN_mod_sqr(t, b, p, ctx)) { + goto end; + } + } else { + if (!BN_mod_mul(t, t, t, p, ctx)) { + goto end; + } + } + if (BN_is_one(t)) { + break; + } + } + // If not found, a is not a square or p is not a prime. + if (i >= e) { + OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); goto end; } - while (!BN_is_one(t)) { - i++; - if (i == e) { - OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); - goto end; - } - if (!BN_mod_mul(t, t, t, p, ctx)) { - goto end; - } - } - // t := y^2^(e - i - 1) if (!BN_copy(t, y)) { @@ -391,14 +392,15 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { !BN_mod_mul(b, b, y, p, ctx)) { goto end; } + + // e decreases each iteration, so this loop will terminate. + assert(i < e); e = i; } vrfy: if (!err) { - // verify the result -- the input might have been not a square - // (test added in 0.9.8) - + // Verify the result. The input might have been not a square. if (!BN_mod_sqr(x, ret, p, ctx)) { err = 1; } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/aead.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/aead.c index abd34767..a33916cf 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/aead.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/aead.c @@ -51,6 +51,9 @@ EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead, const uint8_t *key, } void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx) { + if (ctx == NULL) { + return; + } EVP_AEAD_CTX_cleanup(ctx); OPENSSL_free(ctx); } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_aes.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_aes.c index 6efbab17..43e5ab45 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_aes.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/cipher/e_aes.c @@ -52,7 +52,6 @@ #include #include #include -#include #include #include #include diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/dh.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/dh.c index f9fafaf0..6987c1e3 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/dh.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/dh.c @@ -64,6 +64,7 @@ #include #include +#include "internal.h" #include "../../internal.h" #include "../bn/internal.h" @@ -186,6 +187,8 @@ int DH_set_length(DH *dh, unsigned priv_length) { } int DH_generate_key(DH *dh) { + boringssl_ensure_ffdh_self_test(); + int ok = 0; int generate_new_key = 0; BN_CTX *ctx = NULL; @@ -322,7 +325,8 @@ static int dh_compute_key(DH *dh, BIGNUM *out_shared_key, return ret; } -int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) { +int dh_compute_key_padded_no_self_test(unsigned char *out, + const BIGNUM *peers_key, DH *dh) { BN_CTX *ctx = BN_CTX_new(); if (ctx == NULL) { return -1; @@ -343,7 +347,15 @@ int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) { return ret; } +int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) { + boringssl_ensure_ffdh_self_test(); + + return dh_compute_key_padded_no_self_test(out, peers_key, dh); +} + int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) { + boringssl_ensure_ffdh_self_test(); + BN_CTX *ctx = BN_CTX_new(); if (ctx == NULL) { return -1; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/internal.h new file mode 100644 index 00000000..fd56c83c --- /dev/null +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/dh/internal.h @@ -0,0 +1,36 @@ +/* Copyright (c) 2022, Google Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_DH_INTERNAL_H +#define OPENSSL_HEADER_CRYPTO_FIPSMODULE_DH_INTERNAL_H + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + + +// dh_compute_key_padded_no_self_test does the same as |DH_compute_key_padded|, +// but doesn't try to run the self-test first. This is for use in the self tests +// themselves, to prevent an infinite loop. +int dh_compute_key_padded_no_self_test(unsigned char *out, + const BIGNUM *peers_key, DH *dh); + + +#if defined(__cplusplus) +} +#endif + +#endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_DH_INTERNAL_H diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/digest/digest.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/digest/digest.c index 02530fcf..802ed178 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/digest/digest.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/digest/digest.c @@ -106,6 +106,11 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) { return 1; } +void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) { + OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); + EVP_MD_CTX_cleanup(ctx); +} + void EVP_MD_CTX_free(EVP_MD_CTX *ctx) { if (!ctx) { return; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec.c index 8a86bf47..978d947a 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec.c @@ -943,8 +943,9 @@ static int arbitrary_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out, return ok; } -int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, - const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx) { +int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, + const BIGNUM *g_scalar, const EC_POINT *p, + const BIGNUM *p_scalar, BN_CTX *ctx) { // Previously, this function set |r| to the point at infinity if there was // nothing to multiply. But, nobody should be calling this function with // nothing to multiply in the first place. @@ -1010,6 +1011,13 @@ err: return ret; } +int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, + const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx) { + boringssl_ensure_ecc_self_test(); + + return ec_point_mul_no_self_test(group, r, g_scalar, p, p_scalar, ctx); +} + int ec_point_mul_scalar_public(const EC_GROUP *group, EC_RAW_POINT *r, const EC_SCALAR *g_scalar, const EC_RAW_POINT *p, const EC_SCALAR *p_scalar) { diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec_key.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec_key.c index 26d8cdd4..9701d4ac 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec_key.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/ec_key.c @@ -308,6 +308,9 @@ int EC_KEY_check_key(const EC_KEY *eckey) { } // Check the public and private keys match. + // + // NOTE: this is a FIPS pair-wise consistency check for the ECDH case. See SP + // 800-56Ar3, page 36. if (eckey->priv_key != NULL) { EC_RAW_POINT point; if (!ec_point_mul_scalar_base(eckey->group, &point, @@ -339,9 +342,9 @@ int EC_KEY_check_fips(const EC_KEY *key) { if (key->priv_key) { uint8_t data[16] = {0}; ECDSA_SIG *sig = ECDSA_do_sign(data, sizeof(data), key); -#if defined(BORINGSSL_FIPS_BREAK_ECDSA_PWCT) - data[0] = ~data[0]; -#endif + if (boringssl_fips_break_test("ECDSA_PWCT")) { + data[0] = ~data[0]; + } int ok = sig != NULL && ECDSA_do_verify(data, sizeof(data), sig, key); ECDSA_SIG_free(sig); @@ -439,6 +442,8 @@ int EC_KEY_generate_key(EC_KEY *key) { } int EC_KEY_generate_key_fips(EC_KEY *eckey) { + boringssl_ensure_ecc_self_test(); + if (EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey)) { return 1; } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/internal.h index e8097a9e..da34fc3c 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/internal.h @@ -301,6 +301,13 @@ int ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out, int ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, const EC_FELEM *x, const EC_FELEM *y); +// ec_point_mul_no_self_test does the same as |EC_POINT_mul|, but doesn't try to +// run the self-test first. This is for use in the self tests themselves, to +// prevent an infinite loop. +int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, + const BIGNUM *g_scalar, const EC_POINT *p, + const BIGNUM *p_scalar, BN_CTX *ctx); + // ec_point_mul_scalar sets |r| to |p| * |scalar|. Both inputs are considered // secret. int ec_point_mul_scalar(const EC_GROUP *group, EC_RAW_POINT *r, diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/p256-x86_64.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/p256-x86_64.c index b6a17abb..906661f3 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/p256-x86_64.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ec/p256-x86_64.c @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -557,7 +556,7 @@ static void ecp_nistz256_inv0_mod_ord(const EC_GROUP *group, EC_SCALAR *out, static int ecp_nistz256_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *out, const EC_SCALAR *in) { - if ((OPENSSL_ia32cap_get()[1] & (1 << 28)) == 0) { + if (!CRYPTO_is_AVX_capable()) { // No AVX support; fallback to generic code. return ec_simple_scalar_to_montgomery_inv_vartime(group, out, in); } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdh/ecdh.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdh/ecdh.c index a1ffe922..2e1440eb 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdh/ecdh.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdh/ecdh.c @@ -75,10 +75,13 @@ #include #include "../ec/internal.h" +#include "../../internal.h" int ECDH_compute_key_fips(uint8_t *out, size_t out_len, const EC_POINT *pub_key, const EC_KEY *priv_key) { + boringssl_ensure_ecc_self_test(); + if (priv_key->priv_key == NULL) { OPENSSL_PUT_ERROR(ECDH, ECDH_R_NO_PRIVATE_VALUE); return 0; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/ecdsa.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/ecdsa.c index 7da65d9f..7e4f3aca 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/ecdsa.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/ecdsa.c @@ -151,8 +151,8 @@ int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) { return 1; } -int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, - const ECDSA_SIG *sig, const EC_KEY *eckey) { +int ecdsa_do_verify_no_self_test(const uint8_t *digest, size_t digest_len, + const ECDSA_SIG *sig, const EC_KEY *eckey) { const EC_GROUP *group = EC_KEY_get0_group(eckey); const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey); if (group == NULL || pub_key == NULL || sig == NULL) { @@ -198,6 +198,13 @@ int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, return 1; } +int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, + const ECDSA_SIG *sig, const EC_KEY *eckey) { + boringssl_ensure_ecc_self_test(); + + return ecdsa_do_verify_no_self_test(digest, digest_len, sig, eckey); +} + static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, const EC_SCALAR *priv_key, const EC_SCALAR *k, const uint8_t *digest, size_t digest_len) { @@ -292,12 +299,16 @@ ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest, ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing( const uint8_t *digest, size_t digest_len, const EC_KEY *eckey, const uint8_t *nonce, size_t nonce_len) { + boringssl_ensure_ecc_self_test(); + return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey, nonce, nonce_len); } ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, const EC_KEY *eckey) { + boringssl_ensure_ecc_self_test(); + if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); return NULL; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/internal.h index 717033ce..0a8def26 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ecdsa/internal.h @@ -31,6 +31,12 @@ ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest, const uint8_t *nonce, size_t nonce_len); +// ecdsa_do_verify_no_self_test does the same as |ECDSA_do_verify|, but doesn't +// try to run the self-test first. This is for use in the self tests themselves, +// to prevent an infinite loop. +int ecdsa_do_verify_no_self_test(const uint8_t *digest, size_t digest_len, + const ECDSA_SIG *sig, const EC_KEY *eckey); + #if defined(__cplusplus) } diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S index 62cdbdc6..fe350031 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S @@ -121,7 +121,7 @@ _gcm_gmult_v8: movi v19.16b,#0xe1 ld1 {v20.2d,v21.2d},[x1] //load twisted H, ... shl v19.2d,v19.2d,#57 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ext v3.16b,v17.16b,v17.16b,#8 @@ -146,7 +146,7 @@ _gcm_gmult_v8: eor v18.16b,v18.16b,v2.16b eor v0.16b,v0.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif ext v0.16b,v0.16b,v0.16b,#8 @@ -185,14 +185,14 @@ _gcm_ghash_v8: ext v0.16b,v0.16b,v0.16b,#8 //rotate Xi ld1 {v16.2d},[x2],#16 //load [rotated] I[0] shl v19.2d,v19.2d,#57 //compose 0xc2.0 constant -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v16.16b,v16.16b rev64 v0.16b,v0.16b #endif ext v3.16b,v16.16b,v16.16b,#8 //rotate I[0] b.lo Lodd_tail_v8 //x3 was less than 32 ld1 {v17.2d},[x2],x12 //load [rotated] I[1] -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ext v7.16b,v17.16b,v17.16b,#8 @@ -224,13 +224,13 @@ Loop_mod2x_v8: eor v18.16b,v0.16b,v2.16b eor v1.16b,v1.16b,v17.16b ld1 {v17.2d},[x2],x12 //load [rotated] I[i+3] -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v16.16b,v16.16b #endif eor v1.16b,v1.16b,v18.16b pmull v18.1q,v0.1d,v19.1d //1st phase of reduction -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ins v2.d[0],v1.d[1] @@ -280,7 +280,7 @@ Lodd_tail_v8: eor v0.16b,v0.16b,v18.16b Ldone_v8: -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif ext v0.16b,v0.16b,v0.16b,#8 @@ -299,7 +299,7 @@ Lgcm_ghash_v8_4x: shl v19.2d,v19.2d,#57 //compose 0xc2.0 constant ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b @@ -343,7 +343,7 @@ Loop4x: eor v16.16b,v4.16b,v0.16b ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64 ext v3.16b,v16.16b,v16.16b,#8 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b rev64 v7.16b,v7.16b @@ -426,7 +426,7 @@ Lthree: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d,v5.2d,v6.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b rev64 v4.16b,v4.16b @@ -478,7 +478,7 @@ Ltwo: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d,v5.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v4.16b,v4.16b #endif @@ -521,7 +521,7 @@ Lone: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v4.16b,v4.16b #endif @@ -561,7 +561,7 @@ Ldone4x: eor v0.16b,v0.16b,v18.16b ext v0.16b,v0.16b,v0.16b,#8 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif st1 {v0.2d},[x0] //write out Xi diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S index b104e64e..28d8579a 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S @@ -122,7 +122,7 @@ gcm_gmult_v8: movi v19.16b,#0xe1 ld1 {v20.2d,v21.2d},[x1] //load twisted H, ... shl v19.2d,v19.2d,#57 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ext v3.16b,v17.16b,v17.16b,#8 @@ -147,7 +147,7 @@ gcm_gmult_v8: eor v18.16b,v18.16b,v2.16b eor v0.16b,v0.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif ext v0.16b,v0.16b,v0.16b,#8 @@ -186,14 +186,14 @@ gcm_ghash_v8: ext v0.16b,v0.16b,v0.16b,#8 //rotate Xi ld1 {v16.2d},[x2],#16 //load [rotated] I[0] shl v19.2d,v19.2d,#57 //compose 0xc2.0 constant -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v16.16b,v16.16b rev64 v0.16b,v0.16b #endif ext v3.16b,v16.16b,v16.16b,#8 //rotate I[0] b.lo .Lodd_tail_v8 //x3 was less than 32 ld1 {v17.2d},[x2],x12 //load [rotated] I[1] -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ext v7.16b,v17.16b,v17.16b,#8 @@ -225,13 +225,13 @@ gcm_ghash_v8: eor v18.16b,v0.16b,v2.16b eor v1.16b,v1.16b,v17.16b ld1 {v17.2d},[x2],x12 //load [rotated] I[i+3] -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v16.16b,v16.16b #endif eor v1.16b,v1.16b,v18.16b pmull v18.1q,v0.1d,v19.1d //1st phase of reduction -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v17.16b,v17.16b #endif ins v2.d[0],v1.d[1] @@ -281,7 +281,7 @@ gcm_ghash_v8: eor v0.16b,v0.16b,v18.16b .Ldone_v8: -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif ext v0.16b,v0.16b,v0.16b,#8 @@ -300,7 +300,7 @@ gcm_ghash_v8_4x: shl v19.2d,v19.2d,#57 //compose 0xc2.0 constant ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b @@ -344,7 +344,7 @@ gcm_ghash_v8_4x: eor v16.16b,v4.16b,v0.16b ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64 ext v3.16b,v16.16b,v16.16b,#8 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b rev64 v7.16b,v7.16b @@ -427,7 +427,7 @@ gcm_ghash_v8_4x: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d,v5.2d,v6.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v6.16b,v6.16b rev64 v4.16b,v4.16b @@ -479,7 +479,7 @@ gcm_ghash_v8_4x: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d,v5.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v5.16b,v5.16b rev64 v4.16b,v4.16b #endif @@ -522,7 +522,7 @@ gcm_ghash_v8_4x: eor v1.16b,v1.16b,v17.16b ld1 {v4.2d},[x2] eor v1.16b,v1.16b,v18.16b -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v4.16b,v4.16b #endif @@ -562,7 +562,7 @@ gcm_ghash_v8_4x: eor v0.16b,v0.16b,v18.16b ext v0.16b,v0.16b,v0.16b,#8 -#ifndef __ARMEB__ +#ifndef __AARCH64EB__ rev64 v0.16b,v0.16b #endif st1 {v0.2d},[x0] //write out Xi diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/hmac/hmac.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/hmac/hmac.c index d2931e3a..3d4f89b2 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/hmac/hmac.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/hmac/hmac.c @@ -102,6 +102,13 @@ void HMAC_CTX_cleanup(HMAC_CTX *ctx) { OPENSSL_cleanse(ctx, sizeof(HMAC_CTX)); } +void HMAC_CTX_cleanse(HMAC_CTX *ctx) { + EVP_MD_CTX_cleanse(&ctx->i_ctx); + EVP_MD_CTX_cleanse(&ctx->o_ctx); + EVP_MD_CTX_cleanse(&ctx->md_ctx); + OPENSSL_cleanse(ctx, sizeof(HMAC_CTX)); +} + void HMAC_CTX_free(HMAC_CTX *ctx) { if (ctx == NULL) { return; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/gcm.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/gcm.c index a6e3b53a..83b9ab77 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/gcm.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/gcm.c @@ -52,7 +52,6 @@ #include #include -#include #include "internal.h" #include "../../internal.h" @@ -153,7 +152,7 @@ void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, #if defined(GHASH_ASM_X86_64) if (crypto_gcm_clmul_enabled()) { - if (((OPENSSL_ia32cap_get()[1] >> 22) & 0x41) == 0x41) { // AVX+MOVBE + if (CRYPTO_is_AVX_capable() && CRYPTO_is_MOVBE_capable()) { gcm_init_avx(out_table, H.u); *out_mult = gcm_gmult_avx; *out_hash = gcm_ghash_avx; @@ -165,7 +164,7 @@ void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, *out_hash = gcm_ghash_clmul; return; } - if (gcm_ssse3_capable()) { + if (CRYPTO_is_SSSE3_capable()) { gcm_init_ssse3(out_table, H.u); *out_mult = gcm_gmult_ssse3; *out_hash = gcm_ghash_ssse3; @@ -178,7 +177,7 @@ void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, *out_hash = gcm_ghash_clmul; return; } - if (gcm_ssse3_capable()) { + if (CRYPTO_is_SSSE3_capable()) { gcm_init_ssse3(out_table, H.u); *out_mult = gcm_gmult_ssse3; *out_hash = gcm_ghash_ssse3; @@ -723,9 +722,7 @@ void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) { #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) int crypto_gcm_clmul_enabled(void) { #if defined(GHASH_ASM_X86) || defined(GHASH_ASM_X86_64) - const uint32_t *ia32cap = OPENSSL_ia32cap_get(); - return (ia32cap[0] & (1 << 24)) && // check FXSR bit - (ia32cap[1] & (1 << 1)); // check PCLMULQDQ bit + return CRYPTO_is_FXSR_capable() && CRYPTO_is_PCLMUL_capable(); #else return 0; #endif diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/internal.h index 61d35ec1..8df5ce47 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/modes/internal.h @@ -52,7 +52,6 @@ #include #include -#include #include #include @@ -254,10 +253,6 @@ void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]); void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp, size_t len); -OPENSSL_INLINE char gcm_ssse3_capable(void) { - return (OPENSSL_ia32cap_get()[1] & (1 << (41 - 32))) != 0; -} - // |gcm_gmult_ssse3| and |gcm_ghash_ssse3| require |Htable| to be // 16-byte-aligned, but |gcm_init_ssse3| does not. void gcm_init_ssse3(u128 Htable[16], const uint64_t Xi[2]); diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/internal.h index 5a702730..1c656830 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/internal.h @@ -16,7 +16,6 @@ #define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H #include -#include #include "../../internal.h" #include "../modes/internal.h" @@ -144,15 +143,14 @@ OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg); #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) OPENSSL_INLINE int have_rdrand(void) { - return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0; + return CRYPTO_is_RDRAND_capable(); } // have_fast_rdrand returns true if RDRAND is supported and it's reasonably // fast. Concretely the latter is defined by whether the chip is Intel (fast) or // not (assumed slow). OPENSSL_INLINE int have_fast_rdrand(void) { - const uint32_t *const ia32cap = OPENSSL_ia32cap_get(); - return (ia32cap[1] & (1u << 30)) && (ia32cap[0] & (1u << 30)); + return CRYPTO_is_RDRAND_capable() && CRYPTO_is_intel_cpu(); } // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/rand.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/rand.c index 45bdeeea..8222ada1 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/rand.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rand/rand.c @@ -23,7 +23,6 @@ #endif #include -#include #include #include @@ -171,11 +170,11 @@ void CRYPTO_get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len, CRYPTO_sysrand_for_seed(out_entropy, out_entropy_len); } -#if defined(BORINGSSL_FIPS_BREAK_CRNG) - // This breaks the "continuous random number generator test" defined in FIPS - // 140-2, section 4.9.2, and implemented in |rand_get_seed|. - OPENSSL_memset(out_entropy, 0, out_entropy_len); -#endif + if (boringssl_fips_break_test("CRNG")) { + // This breaks the "continuous random number generator test" defined in FIPS + // 140-2, section 4.9.2, and implemented in |rand_get_seed|. + OPENSSL_memset(out_entropy, 0, out_entropy_len); + } } // In passive entropy mode, entropy is supplied from outside of the module via diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/internal.h b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/internal.h index 46c90079..9e097c40 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/internal.h @@ -124,6 +124,28 @@ extern const BN_ULONG kBoringSSLRSASqrtTwo[]; extern const size_t kBoringSSLRSASqrtTwoLen; +// Functions that avoid self-tests. +// +// Self-tests need to call functions that don't try and ensure that the +// self-tests have passed. These functions, in turn, need to limit themselves +// to such functions too. +// +// These functions are the same as their public versions, but skip the self-test +// check. + +int rsa_verify_no_self_test(int hash_nid, const uint8_t *digest, + size_t digest_len, const uint8_t *sig, + size_t sig_len, RSA *rsa); + +int rsa_verify_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, + size_t max_out, const uint8_t *in, + size_t in_len, int padding); + +int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, + unsigned digest_len, uint8_t *out, unsigned *out_len, + RSA *rsa); + + #if defined(__cplusplus) } // extern C #endif diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa.c index c9b30a7d..22b63b89 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa.c @@ -303,8 +303,9 @@ int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, return out_len; } -int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, - const uint8_t *in, size_t in_len, int padding) { +static int rsa_sign_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, + size_t max_out, const uint8_t *in, + size_t in_len, int padding) { if (rsa->meth->sign_raw) { return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding); } @@ -312,6 +313,13 @@ int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding); } +int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, + const uint8_t *in, size_t in_len, int padding) { + boringssl_ensure_rsa_self_test(); + return rsa_sign_raw_no_self_test(rsa, out_len, out, max_out, in, in_len, + padding); +} + int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding) { size_t out_len; @@ -523,8 +531,9 @@ int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, return 0; } -int RSA_sign(int hash_nid, const uint8_t *digest, unsigned digest_len, - uint8_t *out, unsigned *out_len, RSA *rsa) { +int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, + unsigned digest_len, uint8_t *out, unsigned *out_len, + RSA *rsa) { const unsigned rsa_size = RSA_size(rsa); int ret = 0; uint8_t *signed_msg = NULL; @@ -539,8 +548,9 @@ int RSA_sign(int hash_nid, const uint8_t *digest, unsigned digest_len, if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, &signed_msg_is_alloced, hash_nid, digest, digest_len) || - !RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg, - signed_msg_len, RSA_PKCS1_PADDING)) { + !rsa_sign_raw_no_self_test(rsa, &size_t_out_len, out, rsa_size, + signed_msg, signed_msg_len, + RSA_PKCS1_PADDING)) { goto err; } @@ -554,6 +564,13 @@ err: return ret; } +int RSA_sign(int hash_nid, const uint8_t *digest, unsigned digest_len, + uint8_t *out, unsigned *out_len, RSA *rsa) { + boringssl_ensure_rsa_self_test(); + + return rsa_sign_no_self_test(hash_nid, digest, digest_len, out, out_len, rsa); +} + int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *digest, size_t digest_len, const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len) { @@ -577,8 +594,9 @@ int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, return ret; } -int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, - const uint8_t *sig, size_t sig_len, RSA *rsa) { +int rsa_verify_no_self_test(int hash_nid, const uint8_t *digest, + size_t digest_len, const uint8_t *sig, + size_t sig_len, RSA *rsa) { if (rsa->n == NULL || rsa->e == NULL) { OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); return 0; @@ -602,12 +620,9 @@ int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, return 0; } - if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len, - RSA_PKCS1_PADDING)) { - goto out; - } - - if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, + if (!rsa_verify_raw_no_self_test(rsa, &len, buf, rsa_size, sig, sig_len, + RSA_PKCS1_PADDING) || + !RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, &signed_msg_is_alloced, hash_nid, digest, digest_len)) { goto out; @@ -630,6 +645,13 @@ out: return ret; } +int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, + const uint8_t *sig, size_t sig_len, RSA *rsa) { + boringssl_ensure_rsa_self_test(); + return rsa_verify_no_self_test(hash_nid, digest, digest_len, sig, sig_len, + rsa); +} + int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, size_t digest_len, const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len, const uint8_t *sig, size_t sig_len) { @@ -905,9 +927,9 @@ int RSA_check_fips(RSA *key) { ret = 0; goto cleanup; } -#if defined(BORINGSSL_FIPS_BREAK_RSA_PWCT) - data[0] = ~data[0]; -#endif + if (boringssl_fips_break_test("RSA_PWCT")) { + data[0] = ~data[0]; + } if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) { OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); ret = 0; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c index 61605002..addee125 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c @@ -261,6 +261,8 @@ size_t rsa_default_size(const RSA *rsa) { int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding) { + boringssl_ensure_rsa_self_test(); + if (!rsa_check_public_key(rsa)) { return 0; } @@ -528,6 +530,8 @@ err: int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, const uint8_t *in, size_t in_len, int padding) { + boringssl_ensure_rsa_self_test(); + const unsigned rsa_size = RSA_size(rsa); uint8_t *buf = NULL; int ret = 0; @@ -593,8 +597,9 @@ err: static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); -int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, - const uint8_t *in, size_t in_len, int padding) { +int rsa_verify_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, + size_t max_out, const uint8_t *in, + size_t in_len, int padding) { if (!rsa_check_public_key(rsa)) { return 0; } @@ -686,6 +691,14 @@ err: return ret; } +int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, + size_t max_out, const uint8_t *in, + size_t in_len, int padding) { + boringssl_ensure_rsa_self_test(); + return rsa_verify_raw_no_self_test(rsa, out_len, out, max_out, in, in_len, + padding); +} + int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) { if (rsa->n == NULL || rsa->d == NULL) { @@ -1324,6 +1337,8 @@ static void replace_bn_mont_ctx(BN_MONT_CTX **out, BN_MONT_CTX **in) { static int RSA_generate_key_ex_maybe_fips(RSA *rsa, int bits, const BIGNUM *e_value, BN_GENCB *cb, int check_fips) { + boringssl_ensure_rsa_self_test(); + RSA *tmp = NULL; uint32_t err; int ret = 0; diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/fips.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/fips.c index 6b5091a5..a7a0ff93 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/fips.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/fips.c @@ -28,6 +28,45 @@ int FIPS_mode(void) { int FIPS_mode_set(int on) { return on == FIPS_mode(); } +uint32_t FIPS_version(void) { + return 0; +} + +int FIPS_query_algorithm_status(const char *algorithm) { +#if defined(BORINGSSL_FIPS) + static const char kApprovedAlgorithms[][13] = { + "AES-CBC", + "AES-CCM", + "AES-CTR", + "AES-ECB", + "AES-GCM", + "AES-KW", + "AES-KWP", + "ctrDRBG", + "ECC-SSC", + "ECDSA-sign", + "ECDSA-verify", + "FFC-SSC", + "HMAC", + "RSA-sign", + "RSA-verify", + "SHA-1", + "SHA2-224", + "SHA2-256", + "SHA2-384", + "SHA2-512", + "SHA2-512/256", + }; + for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kApprovedAlgorithms); i++) { + if (strcmp(algorithm, kApprovedAlgorithms[i]) == 0) { + return 1; + } + } +#endif // BORINGSSL_FIPS + + return 0; +} + #if defined(BORINGSSL_FIPS_COUNTERS) size_t FIPS_read_counter(enum fips_counter_t counter) { diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/self_check.c b/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/self_check.c index 1662a652..8eaaf9ef 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/self_check.c +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/self_check/self_check.c @@ -20,20 +20,22 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include #include "../../internal.h" +#include "../dh/internal.h" #include "../ec/internal.h" #include "../ecdsa/internal.h" #include "../rand/internal.h" +#include "../rsa/internal.h" #include "../tls/internal.h" @@ -47,21 +49,6 @@ int BORINGSSL_self_test(void) { #else -#if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID) -// FIPS builds on Android will test for flag files, named after the module hash, -// in /dev/boringssl/selftest/. If such a flag file exists, it's assumed that -// self-tests have already passed and thus do not need to be repeated. (The -// integrity tests always run, however.) -// -// If self-tests complete successfully and the environment variable named in -// |kFlagWriteEnableEnvVar| is present, then the flag file will be created. The -// flag file isn't written without the environment variable being set in order -// to avoid SELinux violations on Android. -#define BORINGSSL_FIPS_SELF_TEST_FLAG_FILE -static const char kFlagPrefix[] = "/dev/boringssl/selftest/"; -static const char kFlagWriteEnableEnvVar[] = "BORINGSSL_SELF_TEST_CREATE_FLAG"; -#endif - static void hexdump(const uint8_t *in, size_t len) { for (size_t i = 0; i < len; i++) { fprintf(stderr, "%02x", in[i]); @@ -71,7 +58,7 @@ static void hexdump(const uint8_t *in, size_t len) { static int check_test(const void *expected, const void *actual, size_t expected_len, const char *name) { if (OPENSSL_memcmp(actual, expected, expected_len) != 0) { - fprintf(stderr, "%s failed.\nExpected: ", name); + fprintf(stderr, "%s failed.\nExpected: ", name); hexdump(expected, expected_len); fprintf(stderr, "\nCalculated: "); hexdump(actual, expected_len); @@ -87,6 +74,28 @@ static int set_bignum(BIGNUM **out, const uint8_t *in, size_t len) { return *out != NULL; } +static int serialize_ecdsa_sig(uint8_t *out, size_t out_len, + const ECDSA_SIG *sig) { + if ((out_len & 1) || // + !BN_bn2bin_padded(out, out_len / 2, sig->r) || + !BN_bn2bin_padded(out + out_len / 2, out_len / 2, sig->s)) { + return 0; + } + return 1; +} + +static ECDSA_SIG *parse_ecdsa_sig(const uint8_t *in, size_t in_len) { + ECDSA_SIG *ret = ECDSA_SIG_new(); + if (!ret || // + (in_len & 1) || + BN_bin2bn(in, in_len/2, ret->r) == NULL || + BN_bin2bn(in + in_len/2, in_len/2, ret->s) == NULL) { + ECDSA_SIG_free(ret); + ret = NULL; + } + return ret; +} + static RSA *self_test_rsa_key(void) { static const uint8_t kN[] = { 0xd3, 0x3a, 0x62, 0x9f, 0x07, 0x77, 0xb0, 0x18, 0xf3, 0xff, 0xfe, 0xcc, @@ -289,195 +298,185 @@ err: return NULL; } -#if defined(OPENSSL_ANDROID) -#define MODULE_DIGEST_SIZE SHA256_DIGEST_LENGTH -#else -#define MODULE_DIGEST_SIZE SHA512_DIGEST_LENGTH -#endif -int boringssl_fips_self_test( - const uint8_t *module_hash, size_t module_hash_len) { -#if defined(BORINGSSL_FIPS_SELF_TEST_FLAG_FILE) - char flag_path[sizeof(kFlagPrefix) + 2 * MODULE_DIGEST_SIZE]; - if (module_hash_len != 0) { - if (module_hash_len != MODULE_DIGEST_SIZE) { - fprintf(stderr, - "module hash of length %zu does not match expected length %d\n", - module_hash_len, MODULE_DIGEST_SIZE); - BORINGSSL_FIPS_abort(); - } +// Lazy self-tests +// +// Self tests that are slow are deferred until the corresponding algorithm is +// actually exercised, in FIPS mode. (In non-FIPS mode these tests are only run +// when requested by |BORINGSSL_self_test|.) - // Test whether the flag file exists. - memcpy(flag_path, kFlagPrefix, sizeof(kFlagPrefix) - 1); - static const char kHexTable[17] = "0123456789abcdef"; - for (size_t i = 0; i < MODULE_DIGEST_SIZE; i++) { - flag_path[sizeof(kFlagPrefix) - 1 + 2 * i] = - kHexTable[module_hash[i] >> 4]; - flag_path[sizeof(kFlagPrefix) - 1 + 2 * i + 1] = - kHexTable[module_hash[i] & 15]; - } - flag_path[sizeof(flag_path) - 1] = 0; +static int boringssl_self_test_rsa(void) { + int ret = 0; + uint8_t output[256]; - if (access(flag_path, F_OK) == 0) { - // Flag file found. Skip self-tests. - return 1; - } + RSA *const rsa_key = self_test_rsa_key(); + if (rsa_key == NULL) { + fprintf(stderr, "RSA key construction failed\n"); + goto err; } -#endif // BORINGSSL_FIPS_SELF_TEST_FLAG_FILE - static const uint8_t kAESKey[16] = "BoringCrypto Key"; - static const uint8_t kAESIV[16] = {0}; - static const uint8_t kPlaintext[64] = - "BoringCryptoModule FIPS KAT Encryption and Decryption Plaintext!"; - static const uint8_t kAESCBCCiphertext[64] = { - 0x87, 0x2d, 0x98, 0xc2, 0xcc, 0x31, 0x5b, 0x41, 0xe0, 0xfa, 0x7b, - 0x0a, 0x71, 0xc0, 0x42, 0xbf, 0x4f, 0x61, 0xd0, 0x0d, 0x58, 0x8c, - 0xf7, 0x05, 0xfb, 0x94, 0x89, 0xd3, 0xbc, 0xaa, 0x1a, 0x50, 0x45, - 0x1f, 0xc3, 0x8c, 0xb8, 0x98, 0x86, 0xa3, 0xe3, 0x6c, 0xfc, 0xad, - 0x3a, 0xb5, 0x59, 0x27, 0x7d, 0x21, 0x07, 0xca, 0x4c, 0x1d, 0x55, - 0x34, 0xdd, 0x5a, 0x2d, 0xc4, 0xb4, 0xf5, 0xa8, -#if !defined(BORINGSSL_FIPS_BREAK_AES_CBC) - 0x35 -#else - 0x00 -#endif + // RSA Sign KAT + + static const uint8_t kRSASignDigest[32] = { + 0xd2, 0xb5, 0x6e, 0x53, 0x30, 0x6f, 0x72, 0x0d, 0x79, 0x29, 0xd8, + 0x70, 0x8b, 0xf4, 0x6f, 0x1c, 0x22, 0x30, 0x03, 0x05, 0x58, 0x2b, + 0x11, 0x5b, 0xed, 0xca, 0xc7, 0x22, 0xd8, 0xaa, 0x5a, 0xb2, }; - static const uint8_t kAESGCMCiphertext[80] = { - 0x4a, 0xd8, 0xe7, 0x7d, 0x78, 0xd7, 0x7d, 0x5e, 0xb2, 0x11, 0xb6, 0xc9, - 0xa4, 0xbc, 0xb2, 0xae, 0xbe, 0x93, 0xd1, 0xb7, 0xfe, 0x65, 0xc1, 0x82, - 0x2a, 0xb6, 0x71, 0x5f, 0x1a, 0x7c, 0xe0, 0x1b, 0x2b, 0xe2, 0x53, 0xfa, - 0xa0, 0x47, 0xfa, 0xd7, 0x8f, 0xb1, 0x4a, 0xc4, 0xdc, 0x89, 0xf9, 0xb4, - 0x14, 0x4d, 0xde, 0x95, 0xea, 0x29, 0x69, 0x76, 0x81, 0xa3, 0x5c, 0x33, - 0xd8, 0x37, 0xd8, 0xfa, 0x47, 0x19, 0x46, 0x2f, 0xf1, 0x90, 0xb7, 0x61, - 0x8f, 0x6f, 0xdd, 0x31, 0x3f, 0x6a, 0x64, -#if !defined(BORINGSSL_FIPS_BREAK_AES_GCM) - 0x0d -#else - 0x00 -#endif + static const uint8_t kRSASignSignature[256] = { + 0x64, 0xce, 0xdd, 0x91, 0x27, 0xb0, 0x4f, 0xb9, 0x14, 0xea, 0xc0, 0xb4, + 0xa2, 0x06, 0xc5, 0xd8, 0x40, 0x0f, 0x6c, 0x54, 0xac, 0xf7, 0x02, 0xde, + 0x26, 0xbb, 0xfd, 0x33, 0xe5, 0x2f, 0x4d, 0xb1, 0x53, 0xc4, 0xff, 0xd0, + 0x5f, 0xea, 0x15, 0x89, 0x83, 0x4c, 0xe3, 0x80, 0x0b, 0xe9, 0x13, 0x82, + 0x1d, 0x71, 0x92, 0x1a, 0x03, 0x60, 0x2c, 0xaf, 0xe2, 0x16, 0xc7, 0x43, + 0x3f, 0xde, 0x6b, 0x94, 0xfd, 0x6e, 0x08, 0x7b, 0x11, 0xf1, 0x34, 0x52, + 0xe5, 0xc0, 0x97, 0x66, 0x4a, 0xe0, 0x91, 0x45, 0xc8, 0xb1, 0x3d, 0x6a, + 0x54, 0xc1, 0x32, 0x0f, 0x32, 0xad, 0x25, 0x11, 0x3e, 0x49, 0xad, 0x41, + 0xce, 0x7b, 0xca, 0x95, 0x6b, 0x54, 0x5e, 0x86, 0x1b, 0xce, 0xfa, 0x2a, + 0x60, 0xe8, 0xfa, 0xbb, 0x23, 0xb2, 0x41, 0xbc, 0x7c, 0x98, 0xec, 0x73, + 0x20, 0xed, 0xb3, 0xcf, 0xab, 0x07, 0x24, 0x85, 0x6a, 0x2a, 0x61, 0x76, + 0x28, 0xf8, 0x00, 0x80, 0xeb, 0xd9, 0x3a, 0x63, 0xe2, 0x01, 0xb1, 0xee, + 0x6d, 0xe9, 0x73, 0xe9, 0xb6, 0x75, 0x2e, 0xf9, 0x81, 0xd9, 0xa8, 0x79, + 0xf6, 0x8f, 0xe3, 0x02, 0x7d, 0xf6, 0xea, 0xdc, 0x35, 0xe4, 0x62, 0x0d, + 0x91, 0xba, 0x3e, 0x7d, 0x8b, 0x82, 0xbf, 0x15, 0x74, 0x6a, 0x4e, 0x29, + 0xf8, 0x9b, 0x2c, 0x94, 0x8d, 0xa7, 0x00, 0x4d, 0x7b, 0xbf, 0x35, 0x07, + 0xeb, 0xdd, 0x10, 0xef, 0xd5, 0x2f, 0xe6, 0x98, 0x4b, 0x7e, 0x24, 0x80, + 0xe2, 0x01, 0xf2, 0x66, 0xb7, 0xd3, 0x93, 0xfe, 0x2a, 0xb3, 0x74, 0xed, + 0xec, 0x4b, 0xb1, 0x5f, 0x5f, 0xee, 0x85, 0x44, 0xa7, 0x26, 0xdf, 0xc1, + 0x2e, 0x7a, 0xf3, 0xa5, 0x8f, 0xf8, 0x64, 0xda, 0x65, 0xad, 0x91, 0xe2, + 0x90, 0x94, 0x20, 0x16, 0xb8, 0x61, 0xa5, 0x0a, 0x7d, 0xb4, 0xbf, 0xc0, + 0x10, 0xaf, 0x72, 0x67, }; - static const DES_cblock kDESKey1 = {"BCMDESK1"}; - static const DES_cblock kDESKey2 = {"BCMDESK2"}; - static const DES_cblock kDESKey3 = {"BCMDESK3"}; - static const DES_cblock kDESIV = {"BCMDESIV"}; - static const uint8_t kDESCiphertext[64] = { - 0xa4, 0x30, 0x7a, 0x4c, 0x1f, 0x60, 0x16, 0xd7, 0x4f, 0x41, 0xe1, - 0xbb, 0x27, 0xc4, 0x27, 0x37, 0xd4, 0x7f, 0xb9, 0x10, 0xf8, 0xbc, - 0xaf, 0x93, 0x91, 0xb8, 0x88, 0x24, 0xb1, 0xf6, 0xf8, 0xbd, 0x31, - 0x96, 0x06, 0x76, 0xde, 0x32, 0xcd, 0x29, 0x29, 0xba, 0x70, 0x5f, - 0xea, 0xc0, 0xcb, 0xde, 0xc7, 0x75, 0x90, 0xe0, 0x0f, 0x5e, 0x2c, - 0x0d, 0x49, 0x20, 0xd5, 0x30, 0x83, 0xf8, 0x08, -#if !defined(BORINGSSL_FIPS_BREAK_DES) - 0x5a -#else - 0x00 -#endif + + unsigned sig_len; + if (!rsa_sign_no_self_test(NID_sha256, kRSASignDigest, sizeof(kRSASignDigest), + output, &sig_len, rsa_key) || + !check_test(kRSASignSignature, output, sizeof(kRSASignSignature), + "RSA-sign KAT")) { + fprintf(stderr, "RSA signing test failed.\n"); + goto err; + } + + // RSA Verify KAT + + static const uint8_t kRSAVerifyDigest[32] = { + 0x09, 0x65, 0x2f, 0xd8, 0xed, 0x9d, 0xc2, 0x6d, 0xbc, 0xbf, 0xf2, + 0xa7, 0xa5, 0xed, 0xe1, 0x37, 0x13, 0x78, 0x21, 0x36, 0xcf, 0x8d, + 0x22, 0x3d, 0xab, 0x93, 0xb4, 0x12, 0xa8, 0xb5, 0x15, 0x53, }; - static const uint8_t kPlaintextSHA1[20] = { - 0xc6, 0xf8, 0xc9, 0x63, 0x1c, 0x14, 0x23, 0x62, 0x9b, 0xbd, - 0x55, 0x82, 0xf4, 0xd6, 0x1d, 0xf2, 0xab, 0x7d, 0xc8, -#if !defined(BORINGSSL_FIPS_BREAK_SHA_1) - 0x28 -#else - 0x00 -#endif + static const uint8_t kRSAVerifySignature[256] = { + 0xab, 0xe2, 0xcb, 0xc1, 0x3d, 0x6b, 0xd3, 0x9d, 0x48, 0xdb, 0x53, 0x34, + 0xdd, 0xbf, 0x8d, 0x07, 0x0a, 0x93, 0xbd, 0xcb, 0x10, 0x4e, 0x2c, 0xc5, + 0xd0, 0xee, 0x48, 0x6e, 0xe2, 0x95, 0xf6, 0xb3, 0x1b, 0xda, 0x12, 0x6c, + 0x41, 0x89, 0x0b, 0x98, 0xb7, 0x3e, 0x70, 0xe6, 0xb6, 0x5d, 0x82, 0xf9, + 0x5c, 0x66, 0x31, 0x21, 0x75, 0x5a, 0x90, 0x74, 0x4c, 0x8d, 0x1c, 0x21, + 0x14, 0x8a, 0x19, 0x60, 0xbe, 0x0e, 0xca, 0x44, 0x6e, 0x9f, 0xf4, 0x97, + 0xf1, 0x34, 0x5c, 0x53, 0x7e, 0xf8, 0x11, 0x9b, 0x9a, 0x43, 0x98, 0xe9, + 0x5c, 0x5c, 0x6d, 0xe2, 0xb1, 0xc9, 0x55, 0x90, 0x5c, 0x52, 0x99, 0xd8, + 0xce, 0x7a, 0x3b, 0x6a, 0xb7, 0x63, 0x80, 0xd9, 0xba, 0xbd, 0xd1, 0x5f, + 0x61, 0x02, 0x37, 0xe1, 0xf3, 0xf2, 0xaa, 0x1c, 0x1f, 0x1e, 0x77, 0x0b, + 0x62, 0xfb, 0xb5, 0x96, 0x38, 0x1b, 0x2e, 0xbd, 0xd7, 0x7e, 0xce, 0xf9, + 0xc9, 0x0d, 0x4c, 0x92, 0xf7, 0xb6, 0xb0, 0x5f, 0xed, 0x29, 0x36, 0x28, + 0x5f, 0xa9, 0x48, 0x26, 0xe6, 0x20, 0x55, 0x32, 0x2a, 0x33, 0xb6, 0xf0, + 0x4c, 0x74, 0xce, 0x69, 0xe5, 0xd8, 0xd7, 0x37, 0xfb, 0x83, 0x8b, 0x79, + 0xd2, 0xd4, 0x8e, 0x3d, 0xaf, 0x71, 0x38, 0x75, 0x31, 0x88, 0x25, 0x31, + 0xa9, 0x5a, 0xc9, 0x64, 0xd0, 0x2e, 0xa4, 0x13, 0xbf, 0x85, 0x95, 0x29, + 0x82, 0xbb, 0xc0, 0x89, 0x52, 0x7d, 0xaf, 0xf5, 0xb8, 0x45, 0xc9, 0xa0, + 0xf4, 0xd1, 0x4e, 0xf1, 0x95, 0x6d, 0x9c, 0x3a, 0xca, 0xe8, 0x82, 0xd1, + 0x2d, 0xa6, 0x6d, 0xa0, 0xf3, 0x57, 0x94, 0xf5, 0xee, 0x32, 0x23, 0x23, + 0x33, 0x51, 0x7d, 0xb9, 0x31, 0x52, 0x32, 0xa1, 0x83, 0xb9, 0x91, 0x65, + 0x4d, 0xbe, 0xa4, 0x16, 0x15, 0x34, 0x5c, 0x88, 0x53, 0x25, 0x92, 0x67, + 0x44, 0xa5, 0x39, 0x15, }; - static const uint8_t kPlaintextSHA256[32] = { - 0x37, 0xbd, 0x70, 0x53, 0x72, 0xfc, 0xd4, 0x03, 0x79, 0x70, 0xfb, - 0x06, 0x95, 0xb1, 0x2a, 0x82, 0x48, 0xe1, 0x3e, 0xf2, 0x33, 0xfb, - 0xef, 0x29, 0x81, 0x22, 0x45, 0x40, 0x43, 0x70, 0xce, -#if !defined(BORINGSSL_FIPS_BREAK_SHA_256) - 0x0f -#else - 0x00 -#endif + if (!rsa_verify_no_self_test(NID_sha256, kRSAVerifyDigest, + sizeof(kRSAVerifyDigest), kRSAVerifySignature, + sizeof(kRSAVerifySignature), rsa_key)) { + fprintf(stderr, "RSA-verify KAT failed.\n"); + goto err; + } + + ret = 1; + +err: + RSA_free(rsa_key); + + return ret; +} + +static int boringssl_self_test_ecc(void) { + int ret = 0; + EC_KEY *ec_key = NULL; + EC_GROUP *ec_group = NULL; + EC_POINT *ec_point_in = NULL; + EC_POINT *ec_point_out = NULL; + BIGNUM *ec_scalar = NULL; + ECDSA_SIG *sig = NULL; + + ec_key = self_test_ecdsa_key(); + if (ec_key == NULL) { + fprintf(stderr, "ECDSA KeyGen failed\n"); + goto err; + } + + // ECDSA Sign/Verify KAT + + static const uint8_t kECDSASignDigest[32] = { + 0x1e, 0x35, 0x93, 0x0b, 0xe8, 0x60, 0xd0, 0x94, 0x2c, 0xa7, 0xbb, + 0xd6, 0xf6, 0xde, 0xd8, 0x7f, 0x15, 0x7e, 0x4d, 0xe2, 0x4f, 0x81, + 0xed, 0x4b, 0x87, 0x5c, 0x0e, 0x01, 0x8e, 0x89, 0xa8, 0x1f, }; - static const uint8_t kPlaintextSHA512[64] = { - 0x08, 0x6a, 0x1c, 0x84, 0x61, 0x9d, 0x8e, 0xb3, 0xc0, 0x97, 0x4e, - 0xa1, 0x9f, 0x9c, 0xdc, 0xaf, 0x3b, 0x5c, 0x31, 0xf0, 0xf2, 0x74, - 0xc3, 0xbd, 0x6e, 0xd6, 0x1e, 0xb2, 0xbb, 0x34, 0x74, 0x72, 0x5c, - 0x51, 0x29, 0x8b, 0x87, 0x3a, 0xa3, 0xf2, 0x25, 0x23, 0xd4, 0x1c, - 0x82, 0x1b, 0xfe, 0xd3, 0xc6, 0xee, 0xb5, 0xd6, 0xaf, 0x07, 0x7b, - 0x98, 0xca, 0xa7, 0x01, 0xf3, 0x94, 0xf3, 0x68, -#if !defined(BORINGSSL_FIPS_BREAK_SHA_512) - 0x14 -#else - 0x00 -#endif - }; - static const uint8_t kRSASignature[256] = { - 0x62, 0x66, 0x4b, 0xe3, 0xb1, 0xd2, 0x83, 0xf1, 0xa8, 0x56, 0x2b, 0x33, - 0x60, 0x1e, 0xdb, 0x1e, 0x06, 0xf7, 0xa7, 0x1e, 0xa8, 0xef, 0x03, 0x4d, - 0x0c, 0xf6, 0x83, 0x75, 0x7a, 0xf0, 0x14, 0xc7, 0xe2, 0x94, 0x3a, 0xb5, - 0x67, 0x56, 0xa5, 0x48, 0x7f, 0x3a, 0xa5, 0xbf, 0xf7, 0x1d, 0x44, 0xa6, - 0x34, 0xed, 0x9b, 0xd6, 0x51, 0xaa, 0x2c, 0x4e, 0xce, 0x60, 0x5f, 0xe9, - 0x0e, 0xd5, 0xcd, 0xeb, 0x23, 0x27, 0xf8, 0xfb, 0x45, 0xe5, 0x34, 0x63, - 0x77, 0x7f, 0x2e, 0x80, 0xcf, 0x9d, 0x2e, 0xfc, 0xe2, 0x50, 0x75, 0x29, - 0x46, 0xf4, 0xaf, 0x91, 0xed, 0x36, 0xe1, 0x5e, 0xef, 0x66, 0xa1, 0xff, - 0x27, 0xfc, 0x87, 0x7e, 0x60, 0x84, 0x0f, 0x54, 0x51, 0x56, 0x0f, 0x68, - 0x99, 0xc0, 0x3f, 0xeb, 0xa5, 0xa0, 0x46, 0xb0, 0x86, 0x02, 0xb0, 0xc8, - 0xe8, 0x46, 0x13, 0x06, 0xcd, 0xb7, 0x8a, 0xd0, 0x3b, 0x46, 0xd0, 0x14, - 0x64, 0x53, 0x9b, 0x5b, 0x5e, 0x02, 0x45, 0xba, 0x6e, 0x7e, 0x0a, 0xb9, - 0x9e, 0x62, 0xb7, 0xd5, 0x7a, 0x87, 0xea, 0xd3, 0x24, 0xa5, 0xef, 0xb3, - 0xdc, 0x05, 0x9c, 0x04, 0x60, 0x4b, 0xde, 0xa8, 0x90, 0x08, 0x7b, 0x6a, - 0x5f, 0xb4, 0x3f, 0xda, 0xc5, 0x1f, 0x6e, 0xd6, 0x15, 0xde, 0x65, 0xa4, - 0x6e, 0x62, 0x9d, 0x8f, 0xa8, 0xbe, 0x86, 0xf6, 0x09, 0x90, 0x40, 0xa5, - 0xf4, 0x23, 0xc5, 0xf6, 0x38, 0x86, 0x0d, 0x1c, 0xed, 0x4a, 0x0a, 0xae, - 0xa4, 0x26, 0xc2, 0x2e, 0xd3, 0x13, 0x66, 0x61, 0xea, 0x35, 0x01, 0x0e, - 0x13, 0xda, 0x78, 0x20, 0xae, 0x59, 0x5f, 0x9b, 0xa9, 0x6c, 0xf9, 0x1b, - 0xdf, 0x76, 0x53, 0xc8, 0xa7, 0xf5, 0x63, 0x6d, 0xf3, 0xff, 0xfd, 0xaf, - 0x75, 0x4b, 0xac, 0x67, 0xb1, 0x3c, 0xbf, 0x5e, 0xde, 0x73, 0x02, 0x6d, - 0xd2, 0x0c, 0xb1, -#if !defined(BORINGSSL_FIPS_BREAK_RSA_SIG) - 0x64 -#else - 0x00 -#endif - }; - const uint8_t kDRBGEntropy[48] = - "BCM Known Answer Test DBRG Initial Entropy "; - const uint8_t kDRBGPersonalization[18] = "BCMPersonalization"; - const uint8_t kDRBGAD[16] = "BCM DRBG KAT AD "; - const uint8_t kDRBGOutput[64] = { - 0x1d, 0x63, 0xdf, 0x05, 0x51, 0x49, 0x22, 0x46, 0xcd, 0x9b, 0xc5, - 0xbb, 0xf1, 0x5d, 0x44, 0xae, 0x13, 0x78, 0xb1, 0xe4, 0x7c, 0xf1, - 0x96, 0x33, 0x3d, 0x60, 0xb6, 0x29, 0xd4, 0xbb, 0x6b, 0x44, 0xf9, - 0xef, 0xd9, 0xf4, 0xa2, 0xba, 0x48, 0xea, 0x39, 0x75, 0x59, 0x32, - 0xf7, 0x31, 0x2c, 0x98, 0x14, 0x2b, 0x49, 0xdf, 0x02, 0xb6, 0x5d, - 0x71, 0x09, 0x50, 0xdb, 0x23, 0xdb, 0xe5, 0x22, -#if !defined(BORINGSSL_FIPS_BREAK_DRBG) - 0x95 -#else - 0x00 -#endif - }; - const uint8_t kDRBGEntropy2[48] = - "BCM Known Answer Test DBRG Reseed Entropy "; - const uint8_t kDRBGReseedOutput[64] = { - 0xa4, 0x77, 0x05, 0xdb, 0x14, 0x11, 0x76, 0x71, 0x42, 0x5b, 0xd8, - 0xd7, 0xa5, 0x4f, 0x8b, 0x39, 0xf2, 0x10, 0x4a, 0x50, 0x5b, 0xa2, - 0xc8, 0xf0, 0xbb, 0x3e, 0xa1, 0xa5, 0x90, 0x7d, 0x54, 0xd9, 0xc6, - 0xb0, 0x96, 0xc0, 0x2b, 0x7e, 0x9b, 0xc9, 0xa1, 0xdd, 0x78, 0x2e, - 0xd5, 0xa8, 0x66, 0x16, 0xbd, 0x18, 0x3c, 0xf2, 0xaa, 0x7a, 0x2b, - 0x37, 0xf9, 0xab, 0x35, 0x64, 0x15, 0x01, 0x3f, 0xc4, - }; - const uint8_t kECDSASigR[32] = { + static const uint8_t kECDSASignSig[64] = { 0x67, 0x80, 0xc5, 0xfc, 0x70, 0x27, 0x5e, 0x2c, 0x70, 0x61, 0xa0, 0xe7, 0x87, 0x7b, 0xb1, 0x74, 0xde, 0xad, 0xeb, 0x98, 0x87, 0x02, - 0x7f, 0x3f, 0xa8, 0x36, 0x54, 0x15, 0x8b, 0xa7, 0xf5, -#if !defined(BORINGSSL_FIPS_BREAK_ECDSA_SIG) - 0x0c, -#else - 0x00, -#endif + 0x7f, 0x3f, 0xa8, 0x36, 0x54, 0x15, 0x8b, 0xa7, 0xf5, 0x0c, 0x68, + 0x04, 0x73, 0x40, 0x94, 0xb2, 0xd1, 0x90, 0xac, 0x2d, 0x0c, 0xd7, + 0xa5, 0x7f, 0x2f, 0x2e, 0xb2, 0x62, 0xb0, 0x09, 0x16, 0xe1, 0xa6, + 0x70, 0xb5, 0xbb, 0x0d, 0xfd, 0x8e, 0x0c, 0x02, 0x3f, }; - const uint8_t kECDSASigS[32] = { - 0xa5, 0x93, 0xe0, 0x23, 0x91, 0xe7, 0x4b, 0x8d, 0x77, 0x25, 0xa6, - 0xba, 0x4d, 0xd9, 0x86, 0x77, 0xda, 0x7d, 0x8f, 0xef, 0xc4, 0x1a, - 0xf0, 0xcc, 0x81, 0xe5, 0xea, 0x3f, 0xc2, 0x41, 0x7f, 0xd8, + + // The 'k' value for ECDSA is fixed to avoid an entropy draw. + uint8_t ecdsa_k[32] = {0}; + ecdsa_k[31] = 42; + + sig = ecdsa_sign_with_nonce_for_known_answer_test( + kECDSASignDigest, sizeof(kECDSASignDigest), ec_key, ecdsa_k, + sizeof(ecdsa_k)); + + uint8_t ecdsa_sign_output[64]; + if (sig == NULL || + !serialize_ecdsa_sig(ecdsa_sign_output, sizeof(ecdsa_sign_output), sig) || + !check_test(kECDSASignSig, ecdsa_sign_output, sizeof(ecdsa_sign_output), + "ECDSA-sign signature")) { + fprintf(stderr, "ECDSA-sign KAT failed.\n"); + goto err; + } + + static const uint8_t kECDSAVerifyDigest[32] = { + 0x78, 0x7c, 0x50, 0x5c, 0x60, 0xc9, 0xe4, 0x13, 0x6c, 0xe4, 0x48, + 0xba, 0x93, 0xff, 0x71, 0xfa, 0x9c, 0x18, 0xf4, 0x17, 0x09, 0x4f, + 0xdf, 0x5a, 0xe2, 0x75, 0xc0, 0xcc, 0xd2, 0x67, 0x97, 0xad, }; + static const uint8_t kECDSAVerifySig[64] = { + 0x67, 0x80, 0xc5, 0xfc, 0x70, 0x27, 0x5e, 0x2c, 0x70, 0x61, 0xa0, + 0xe7, 0x87, 0x7b, 0xb1, 0x74, 0xde, 0xad, 0xeb, 0x98, 0x87, 0x02, + 0x7f, 0x3f, 0xa8, 0x36, 0x54, 0x15, 0x8b, 0xa7, 0xf5, 0x0c, 0x2d, + 0x36, 0xe5, 0x79, 0x97, 0x90, 0xbf, 0xbe, 0x21, 0x83, 0xd3, 0x3e, + 0x96, 0xf3, 0xc5, 0x1f, 0x6a, 0x23, 0x2f, 0x2a, 0x24, 0x48, 0x8c, + 0x8e, 0x5f, 0x64, 0xc3, 0x7e, 0xa2, 0xcf, 0x05, 0x29, + }; + + ECDSA_SIG_free(sig); + sig = parse_ecdsa_sig(kECDSAVerifySig, sizeof(kECDSAVerifySig)); + if (!sig || + !ecdsa_do_verify_no_self_test(kECDSAVerifyDigest, + sizeof(kECDSAVerifyDigest), sig, ec_key)) { + fprintf(stderr, "ECDSA-verify KAT failed.\n"); + goto err; + } + + // Primitive Z Computation KAT (IG 9.6). + // kP256Point is SHA256("Primitive Z Computation KAT")×G within P-256. - const uint8_t kP256Point[65] = { + static const uint8_t kP256Point[65] = { 0x04, 0x4e, 0xc1, 0x94, 0x8c, 0x5c, 0xf4, 0x37, 0x35, 0x0d, 0xa3, 0xf9, 0x55, 0xf9, 0x8b, 0x26, 0x23, 0x5c, 0x43, 0xe0, 0x83, 0x51, 0x2b, 0x0d, 0x4b, 0x56, 0x24, 0xc3, 0xe4, 0xa5, 0xa8, 0xe2, 0xe9, @@ -486,50 +485,64 @@ int boringssl_fips_self_test( 0x79, 0x93, 0x7c, 0x0b, 0x92, 0x2b, 0x7f, 0x17, 0xa5, 0x80, }; // kP256Scalar is SHA256("Primitive Z Computation KAT scalar"). - const uint8_t kP256Scalar[32] = { + static const uint8_t kP256Scalar[32] = { 0xe7, 0x60, 0x44, 0x91, 0x26, 0x9a, 0xfb, 0x5b, 0x10, 0x2d, 0x6e, 0xa5, 0x2c, 0xb5, 0x9f, 0xeb, 0x70, 0xae, 0xde, 0x6c, 0xe3, 0xbf, 0xb3, 0xe0, 0x10, 0x54, 0x85, 0xab, 0xd8, 0x61, 0xd7, 0x7b, }; // kP256PointResult is |kP256Scalar|×|kP256Point|. - const uint8_t kP256PointResult[65] = { + static const uint8_t kP256PointResult[65] = { 0x04, 0xf1, 0x63, 0x00, 0x88, 0xc5, 0xd5, 0xe9, 0x05, 0x52, 0xac, 0xb6, 0xec, 0x68, 0x76, 0xb8, 0x73, 0x7f, 0x0f, 0x72, 0x34, 0xe6, 0xbb, 0x30, 0x32, 0x22, 0x37, 0xb6, 0x2a, 0x80, 0xe8, 0x9e, 0x6e, 0x6f, 0x36, 0x02, 0xe7, 0x21, 0xd2, 0x31, 0xdb, 0x94, 0x63, 0xb7, 0xd8, 0x19, 0x0e, 0xc2, 0xc0, 0xa7, 0x2f, 0x15, 0x49, 0x1a, 0xa2, - 0x7c, 0x41, 0x8f, 0xaf, 0x9c, 0x40, 0xaf, 0x2e, 0x4a, -#if !defined(BORINGSSL_FIPS_BREAK_Z_COMPUTATION) - 0x0c, -#else - 0x00, -#endif - }; - const uint8_t kTLSOutput[32] = { - 0x67, 0x85, 0xde, 0x60, 0xfc, 0x0a, 0x83, 0xe9, 0xa2, 0x2a, 0xb3, - 0xf0, 0x27, 0x0c, 0xba, 0xf7, 0xfa, 0x82, 0x3d, 0x14, 0x77, 0x1d, - 0x86, 0x29, 0x79, 0x39, 0x77, 0x8a, 0xd5, 0x0e, 0x9d, -#if !defined(BORINGSSL_FIPS_BREAK_TLS_KDF) - 0x32, -#else - 0x00, -#endif - }; - const uint8_t kTLSSecret[32] = { - 0xbf, 0xe4, 0xb7, 0xe0, 0x26, 0x55, 0x5f, 0x6a, 0xdf, 0x5d, 0x27, - 0xd6, 0x89, 0x99, 0x2a, 0xd6, 0xf7, 0x65, 0x66, 0x07, 0x4b, 0x55, - 0x5f, 0x64, 0x55, 0xcd, 0xd5, 0x77, 0xa4, 0xc7, 0x09, 0x61, - }; - const char kTLSLabel[] = "FIPS self test"; - const uint8_t kTLSSeed1[16] = { - 0x8f, 0x0d, 0xe8, 0xb6, 0x90, 0x8f, 0xb1, 0xd2, - 0x6d, 0x51, 0xf4, 0x79, 0x18, 0x63, 0x51, 0x65, - }; - const uint8_t kTLSSeed2[16] = { - 0x7d, 0x24, 0x1a, 0x9d, 0x3c, 0x59, 0xbf, 0x3c, - 0x31, 0x1e, 0x2b, 0x21, 0x41, 0x8d, 0x32, 0x81, + 0x7c, 0x41, 0x8f, 0xaf, 0x9c, 0x40, 0xaf, 0x2e, 0x4a, 0x0c, }; + ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); + if (ec_group == NULL) { + fprintf(stderr, "Failed to create P-256 group.\n"); + goto err; + } + ec_point_in = EC_POINT_new(ec_group); + ec_point_out = EC_POINT_new(ec_group); + ec_scalar = BN_new(); + uint8_t z_comp_result[65]; + if (ec_point_in == NULL || ec_point_out == NULL || ec_scalar == NULL || + !EC_POINT_oct2point(ec_group, ec_point_in, kP256Point, sizeof(kP256Point), + NULL) || + !BN_bin2bn(kP256Scalar, sizeof(kP256Scalar), ec_scalar) || + !ec_point_mul_no_self_test(ec_group, ec_point_out, NULL, ec_point_in, + ec_scalar, NULL) || + !EC_POINT_point2oct(ec_group, ec_point_out, POINT_CONVERSION_UNCOMPRESSED, + z_comp_result, sizeof(z_comp_result), NULL) || + !check_test(kP256PointResult, z_comp_result, sizeof(z_comp_result), + "Z Computation Result")) { + fprintf(stderr, "Z-computation KAT failed.\n"); + goto err; + } + + ret = 1; + +err: + EC_KEY_free(ec_key); + EC_POINT_free(ec_point_in); + EC_POINT_free(ec_point_out); + EC_GROUP_free(ec_group); + BN_free(ec_scalar); + ECDSA_SIG_free(sig); + + return ret; +} + +static int boringssl_self_test_ffdh(void) { + int ret = 0; + DH *dh = NULL; + BIGNUM *ffdhe2048_value = NULL; + + // FFC Diffie-Hellman KAT + // kFFDHE2048PublicValueData is an arbitrary public value, mod // kFFDHE2048Data. (The private key happens to be 4096.) static const BN_ULONG kFFDHE2048PublicValueData[] = { @@ -550,8 +563,7 @@ int boringssl_fips_self_test( TOBN(0xbae7b0b3, 0x6e362dc0), TOBN(0xa57c73bd, 0xdc70fb82), TOBN(0xfaff50d2, 0x9d573457), TOBN(0x352bd399, 0xbe84058e), }; - - const uint8_t kDHOutput[2048 / 8] = { + static const uint8_t kDHOutput[2048 / 8] = { 0x2a, 0xe6, 0xd3, 0xa6, 0x13, 0x58, 0x8e, 0xce, 0x53, 0xaa, 0xf6, 0x5d, 0x9a, 0xae, 0x02, 0x12, 0xf5, 0x80, 0x3d, 0x06, 0x09, 0x76, 0xac, 0x57, 0x37, 0x9e, 0xab, 0x38, 0x62, 0x25, 0x05, 0x1d, 0xf3, 0xa9, 0x39, 0x60, @@ -573,23 +585,144 @@ int boringssl_fips_self_test( 0x06, 0x80, 0x2a, 0x4e, 0x5a, 0xf0, 0x1e, 0xaa, 0xcb, 0xab, 0x06, 0x0e, 0x27, 0x0f, 0xd9, 0x88, 0xd9, 0x01, 0xe3, 0x07, 0xeb, 0xdf, 0xc3, 0x12, 0xe3, 0x40, 0x88, 0x7b, 0x5f, 0x59, 0x78, 0x6e, 0x26, 0x20, 0xc3, 0xdf, - 0xc8, 0xe4, 0x5e, -#if !defined(BORINGSSL_FIPS_BREAK_FFC_DH) - 0xb8, -#else - 0x00, -#endif + 0xc8, 0xe4, 0x5e, 0xb8, }; + ffdhe2048_value = BN_new(); + if (ffdhe2048_value) { + bn_set_static_words(ffdhe2048_value, kFFDHE2048PublicValueData, + OPENSSL_ARRAY_SIZE(kFFDHE2048PublicValueData)); + } + + dh = self_test_dh(); + uint8_t dh_out[sizeof(kDHOutput)]; + if (dh == NULL || ffdhe2048_value == NULL || sizeof(dh_out) != DH_size(dh) || + dh_compute_key_padded_no_self_test(dh_out, ffdhe2048_value, dh) != + sizeof(dh_out) || + !check_test(kDHOutput, dh_out, sizeof(dh_out), "FFC DH")) { + fprintf(stderr, "FFDH failed.\n"); + goto err; + } + + ret = 1; + +err: + DH_free(dh); + BN_free(ffdhe2048_value); + + return ret; +} + +#if defined(BORINGSSL_FIPS) + +static void run_self_test_rsa(void) { + if (!boringssl_self_test_rsa()) { + BORINGSSL_FIPS_abort(); + } +} + +DEFINE_STATIC_ONCE(g_self_test_once_rsa); + +void boringssl_ensure_rsa_self_test(void) { + CRYPTO_once(g_self_test_once_rsa_bss_get(), run_self_test_rsa); +} + +static void run_self_test_ecc(void) { + if (!boringssl_self_test_ecc()) { + BORINGSSL_FIPS_abort(); + } +} + +DEFINE_STATIC_ONCE(g_self_test_once_ecc); + +void boringssl_ensure_ecc_self_test(void) { + CRYPTO_once(g_self_test_once_ecc_bss_get(), run_self_test_ecc); +} + +static void run_self_test_ffdh(void) { + if (!boringssl_self_test_ffdh()) { + BORINGSSL_FIPS_abort(); + } +} + +DEFINE_STATIC_ONCE(g_self_test_once_ffdh); + +void boringssl_ensure_ffdh_self_test(void) { + CRYPTO_once(g_self_test_once_ffdh_bss_get(), run_self_test_ffdh); +} + +#endif // BORINGSSL_FIPS + + +// Startup self tests. +// +// These tests are run at process start when in FIPS mode. + +int boringssl_self_test_sha256(void) { + static const uint8_t kInput[16] = { + 0xff, 0x3b, 0x85, 0x7d, 0xa7, 0x23, 0x6a, 0x2b, + 0xaa, 0x0f, 0x39, 0x6b, 0x51, 0x52, 0x22, 0x17, + }; + static const uint8_t kPlaintextSHA256[32] = { + 0x7f, 0xe4, 0xd5, 0xf1, 0xa1, 0xe3, 0x82, 0x87, 0xd9, 0x58, 0xf5, + 0x11, 0xc7, 0x1d, 0x5e, 0x27, 0x5e, 0xcc, 0xd2, 0x66, 0xcf, 0xb9, + 0xc8, 0xc6, 0x60, 0xd8, 0x92, 0x1e, 0x57, 0xfd, 0x46, 0x75, + }; + uint8_t output[SHA256_DIGEST_LENGTH]; + + // SHA-256 KAT + SHA256(kInput, sizeof(kInput), output); + return check_test(kPlaintextSHA256, output, sizeof(kPlaintextSHA256), + "SHA-256 KAT"); +} + +int boringssl_self_test_sha512(void) { + static const uint8_t kInput[16] = { + 0x21, 0x25, 0x12, 0xf8, 0xd2, 0xad, 0x83, 0x22, + 0x78, 0x1c, 0x6c, 0x4d, 0x69, 0xa9, 0xda, 0xa1, + }; + static const uint8_t kPlaintextSHA512[64] = { + 0x29, 0x3c, 0x94, 0x35, 0x4e, 0x98, 0x83, 0xe5, 0xc2, 0x78, 0x36, + 0x7a, 0xe5, 0x18, 0x90, 0xbf, 0x35, 0x41, 0x01, 0x64, 0x19, 0x8d, + 0x26, 0xeb, 0xe1, 0xf8, 0x2f, 0x04, 0x8e, 0xfa, 0x8b, 0x2b, 0xc6, + 0xb2, 0x9d, 0x5d, 0x46, 0x76, 0x5a, 0xc8, 0xb5, 0x25, 0xa3, 0xea, + 0x52, 0x84, 0x47, 0x6d, 0x6d, 0xf4, 0xc9, 0x71, 0xf3, 0x3d, 0x89, + 0x4c, 0x3b, 0x20, 0x8c, 0x5b, 0x75, 0xe8, 0xf8, 0x7c, + }; + uint8_t output[SHA512_DIGEST_LENGTH]; + + // SHA-512 KAT + SHA512(kInput, sizeof(kInput), output); + return check_test(kPlaintextSHA512, output, sizeof(kPlaintextSHA512), + "SHA-512 KAT"); +} + +int boringssl_self_test_hmac_sha256(void) { + static const uint8_t kInput[16] = { + 0xda, 0xd9, 0x12, 0x93, 0xdf, 0xcf, 0x2a, 0x7c, + 0x8e, 0xcd, 0x13, 0xfe, 0x35, 0x3f, 0xa7, 0x5b, + }; + static const uint8_t kPlaintextHMACSHA256[32] = { + 0x36, 0x5f, 0x5b, 0xd5, 0xf5, 0xeb, 0xfd, 0xc7, 0x6e, 0x53, 0xa5, + 0x73, 0x6d, 0x73, 0x20, 0x13, 0xaa, 0xd3, 0xbc, 0x86, 0x4b, 0xb8, + 0x84, 0x94, 0x16, 0x46, 0x88, 0x9c, 0x48, 0xee, 0xa9, 0x0e, + }; + uint8_t output[EVP_MAX_MD_SIZE]; + + unsigned output_len; + HMAC(EVP_sha256(), kInput, sizeof(kInput), kInput, sizeof(kInput), output, + &output_len); + return output_len == sizeof(kPlaintextHMACSHA256) && + check_test(kPlaintextHMACSHA256, output, sizeof(kPlaintextHMACSHA256), + "HMAC-SHA-256 KAT"); +} + +static int boringssl_self_test_fast(void) { + static const uint8_t kAESKey[16] = "BoringCrypto Key"; + static const uint8_t kAESIV[16] = {0}; + EVP_AEAD_CTX aead_ctx; EVP_AEAD_CTX_zero(&aead_ctx); - RSA *rsa_key = NULL; - EC_KEY *ec_key = NULL; - EC_GROUP *ec_group = NULL; - EC_POINT *ec_point_in = NULL; - EC_POINT *ec_point_out = NULL; - BIGNUM *ec_scalar = NULL; - ECDSA_SIG *sig = NULL; int ret = 0; AES_KEY aes_key; @@ -597,28 +730,48 @@ int boringssl_fips_self_test( uint8_t output[256]; // AES-CBC Encryption KAT + static const uint8_t kAESCBCEncPlaintext[32] = { + 0x07, 0x86, 0x09, 0xa6, 0xc5, 0xac, 0x25, 0x44, 0x69, 0x9a, 0xdf, + 0x68, 0x2f, 0xa3, 0x77, 0xf9, 0xbe, 0x8a, 0xb6, 0xae, 0xf5, 0x63, + 0xe8, 0xc5, 0x6a, 0x36, 0xb8, 0x4f, 0x55, 0x7f, 0xad, 0xd3, + }; + static const uint8_t kAESCBCEncCiphertext[sizeof(kAESCBCEncPlaintext)] = { + 0x56, 0x46, 0xc1, 0x41, 0xf4, 0x13, 0xd6, 0xff, 0x62, 0x92, 0x41, + 0x7a, 0x26, 0xc6, 0x86, 0xbd, 0x30, 0x5f, 0xb6, 0x57, 0xa7, 0xd2, + 0x50, 0x3a, 0xc5, 0x5e, 0x8e, 0x93, 0x40, 0xf2, 0x10, 0xd8, + }; memcpy(aes_iv, kAESIV, sizeof(kAESIV)); if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { fprintf(stderr, "AES_set_encrypt_key failed.\n"); goto err; } - AES_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &aes_key, aes_iv, - AES_ENCRYPT); - if (!check_test(kAESCBCCiphertext, output, sizeof(kAESCBCCiphertext), - "AES-CBC Encryption KAT")) { + AES_cbc_encrypt(kAESCBCEncPlaintext, output, sizeof(kAESCBCEncPlaintext), + &aes_key, aes_iv, AES_ENCRYPT); + if (!check_test(kAESCBCEncCiphertext, output, sizeof(kAESCBCEncCiphertext), + "AES-CBC-encrypt KAT")) { goto err; } // AES-CBC Decryption KAT + static const uint8_t kAESCBCDecCiphertext[32] = { + 0x34, 0x7a, 0xa5, 0xa0, 0x24, 0xb2, 0x82, 0x57, 0xb3, 0x65, 0x10, + 0xbe, 0x58, 0x3d, 0x4f, 0x47, 0xad, 0xb7, 0xbb, 0xee, 0xdc, 0x60, + 0x05, 0xbb, 0xbd, 0x0d, 0x0a, 0x9f, 0x06, 0xbb, 0x7b, 0x10, + }; + static const uint8_t kAESCBCDecPlaintext[sizeof(kAESCBCDecCiphertext)] = { + 0x51, 0xa7, 0xa0, 0x1f, 0x6b, 0x79, 0x6c, 0xcd, 0x48, 0x03, 0xa1, + 0x41, 0xdc, 0x56, 0xa6, 0xc2, 0x16, 0xb5, 0xd1, 0xd3, 0xb7, 0x06, + 0xb2, 0x25, 0x6f, 0xa6, 0xd0, 0xd2, 0x0e, 0x6f, 0x19, 0xb5, + }; memcpy(aes_iv, kAESIV, sizeof(kAESIV)); if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { fprintf(stderr, "AES_set_decrypt_key failed.\n"); goto err; } - AES_cbc_encrypt(kAESCBCCiphertext, output, sizeof(kAESCBCCiphertext), + AES_cbc_encrypt(kAESCBCDecCiphertext, output, sizeof(kAESCBCDecCiphertext), &aes_key, aes_iv, AES_DECRYPT); - if (!check_test(kPlaintext, output, sizeof(kPlaintext), - "AES-CBC Decryption KAT")) { + if (!check_test(kAESCBCDecPlaintext, output, sizeof(kAESCBCDecPlaintext), + "AES-CBC-decrypt KAT")) { goto err; } @@ -632,194 +785,115 @@ int boringssl_fips_self_test( } // AES-GCM Encryption KAT + static const uint8_t kAESGCMEncPlaintext[32] = { + 0x8f, 0xcc, 0x40, 0x99, 0x80, 0x8e, 0x75, 0xca, 0xaf, 0xf5, 0x82, + 0x89, 0x88, 0x48, 0xa8, 0x8d, 0x80, 0x8b, 0x55, 0xab, 0x4e, 0x93, + 0x70, 0x79, 0x7d, 0x94, 0x0b, 0xe8, 0xcc, 0x1d, 0x78, 0x84, + }; + static const uint8_t kAESGCMCiphertext[sizeof(kAESGCMEncPlaintext) + 16] = { + 0x87, 0x7b, 0xd5, 0x8d, 0x96, 0x3e, 0x4b, 0xe6, 0x64, 0x94, 0x40, 0x2f, + 0x61, 0x9b, 0x7e, 0x56, 0x52, 0x7d, 0xa4, 0x5a, 0xf9, 0xa6, 0xe2, 0xdb, + 0x1c, 0x63, 0x2e, 0x97, 0x93, 0x0f, 0xfb, 0xed, 0xb5, 0x9e, 0x1c, 0x20, + 0xb2, 0xb0, 0x58, 0xda, 0x48, 0x07, 0x2d, 0xbd, 0x96, 0x0d, 0x34, 0xc6, + }; if (!EVP_AEAD_CTX_seal(&aead_ctx, output, &out_len, sizeof(output), nonce, EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()), - kPlaintext, sizeof(kPlaintext), NULL, 0) || + kAESGCMEncPlaintext, sizeof(kAESGCMEncPlaintext), NULL, + 0) || !check_test(kAESGCMCiphertext, output, sizeof(kAESGCMCiphertext), - "AES-GCM Encryption KAT")) { + "AES-GCM-encrypt KAT")) { fprintf(stderr, "EVP_AEAD_CTX_seal for AES-128-GCM failed.\n"); goto err; } // AES-GCM Decryption KAT + static const uint8_t kAESGCMDecCiphertext[48] = { + 0x35, 0xf3, 0x05, 0x8f, 0x87, 0x57, 0x60, 0xff, 0x09, 0xd3, 0x12, 0x0f, + 0x70, 0xc4, 0xbc, 0x9e, 0xd7, 0xa8, 0x68, 0x72, 0xe1, 0x34, 0x52, 0x20, + 0x21, 0x76, 0xf7, 0x37, 0x1a, 0xe0, 0x4f, 0xaa, 0xe1, 0xdd, 0x39, 0x19, + 0x20, 0xf5, 0xd1, 0x39, 0x53, 0xd8, 0x96, 0x78, 0x59, 0x94, 0x82, 0x3c, + }; + static const uint8_t kAESGCMDecPlaintext[sizeof(kAESGCMDecCiphertext) - 16] = + { + 0x3d, 0x44, 0x90, 0x9b, 0x91, 0xe7, 0x5e, 0xd3, 0xc2, 0xb2, 0xd0, + 0xa9, 0x99, 0x17, 0x6a, 0x45, 0x05, 0x5e, 0x99, 0x83, 0x56, 0x01, + 0xc0, 0x82, 0x40, 0x81, 0xd2, 0x48, 0x45, 0xf2, 0xcc, 0xc3, + }; if (!EVP_AEAD_CTX_open(&aead_ctx, output, &out_len, sizeof(output), nonce, EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()), - kAESGCMCiphertext, sizeof(kAESGCMCiphertext), NULL, - 0) || - !check_test(kPlaintext, output, sizeof(kPlaintext), - "AES-GCM Decryption KAT")) { - fprintf(stderr, "EVP_AEAD_CTX_open for AES-128-GCM failed.\n"); - goto err; - } - - DES_key_schedule des1, des2, des3; - DES_cblock des_iv; - DES_set_key(&kDESKey1, &des1); - DES_set_key(&kDESKey2, &des2); - DES_set_key(&kDESKey3, &des3); - - // 3DES Encryption KAT - memcpy(&des_iv, &kDESIV, sizeof(des_iv)); - DES_ede3_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &des1, &des2, - &des3, &des_iv, DES_ENCRYPT); - if (!check_test(kDESCiphertext, output, sizeof(kDESCiphertext), - "3DES Encryption KAT")) { - goto err; - } - - // 3DES Decryption KAT - memcpy(&des_iv, &kDESIV, sizeof(des_iv)); - DES_ede3_cbc_encrypt(kDESCiphertext, output, sizeof(kDESCiphertext), &des1, - &des2, &des3, &des_iv, DES_DECRYPT); - if (!check_test(kPlaintext, output, sizeof(kPlaintext), - "3DES Decryption KAT")) { + kAESGCMDecCiphertext, sizeof(kAESGCMDecCiphertext), + NULL, 0) || + !check_test(kAESGCMDecPlaintext, output, sizeof(kAESGCMDecPlaintext), + "AES-GCM-decrypt KAT")) { + fprintf(stderr, + "AES-GCM-decrypt KAT failed because EVP_AEAD_CTX_open failed.\n"); goto err; } // SHA-1 KAT - SHA1(kPlaintext, sizeof(kPlaintext), output); - if (!check_test(kPlaintextSHA1, output, sizeof(kPlaintextSHA1), + static const uint8_t kSHA1Input[16] = { + 0x13, 0x2f, 0xd9, 0xba, 0xd5, 0xc1, 0x82, 0x62, + 0x63, 0xba, 0xfb, 0xb6, 0x99, 0xf7, 0x07, 0xa5, + }; + static const uint8_t kSHA1Digest[20] = { + 0x94, 0x19, 0x55, 0x93, 0x0a, 0x58, 0x29, 0x38, 0xeb, 0xf5, + 0x09, 0x11, 0x6d, 0x1a, 0xfd, 0x0f, 0x1e, 0x11, 0xe3, 0xcb, + }; + SHA1(kSHA1Input, sizeof(kSHA1Input), output); + if (!check_test(kSHA1Digest, output, sizeof(kSHA1Digest), "SHA-1 KAT")) { goto err; } - // SHA-256 KAT - SHA256(kPlaintext, sizeof(kPlaintext), output); - if (!check_test(kPlaintextSHA256, output, sizeof(kPlaintextSHA256), - "SHA-256 KAT")) { - goto err; - } - - // SHA-512 KAT - SHA512(kPlaintext, sizeof(kPlaintext), output); - if (!check_test(kPlaintextSHA512, output, sizeof(kPlaintextSHA512), - "SHA-512 KAT")) { - goto err; - } - - rsa_key = self_test_rsa_key(); - if (rsa_key == NULL) { - fprintf(stderr, "RSA KeyGen failed\n"); - goto err; - } - - // RSA Sign KAT - unsigned sig_len; - - // Disable blinding for the power-on tests because it's not needed and - // triggers an entropy draw. - rsa_key->flags |= RSA_FLAG_NO_BLINDING; - - if (!RSA_sign(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), output, - &sig_len, rsa_key) || - !check_test(kRSASignature, output, sizeof(kRSASignature), - "RSA Sign KAT")) { - fprintf(stderr, "RSA signing test failed.\n"); - goto err; - } - - // RSA Verify KAT - if (!RSA_verify(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), - kRSASignature, sizeof(kRSASignature), rsa_key)) { - fprintf(stderr, "RSA Verify KAT failed.\n"); - goto err; - } - - ec_key = self_test_ecdsa_key(); - if (ec_key == NULL) { - fprintf(stderr, "ECDSA KeyGen failed\n"); - goto err; - } - - // ECDSA Sign/Verify KAT - - // The 'k' value for ECDSA is fixed to avoid an entropy draw. - uint8_t ecdsa_k[32] = {0}; - ecdsa_k[31] = 42; - - sig = ecdsa_sign_with_nonce_for_known_answer_test( - kPlaintextSHA256, sizeof(kPlaintextSHA256), ec_key, ecdsa_k, - sizeof(ecdsa_k)); - - uint8_t ecdsa_r_bytes[sizeof(kECDSASigR)]; - uint8_t ecdsa_s_bytes[sizeof(kECDSASigS)]; - if (sig == NULL || - BN_num_bytes(sig->r) != sizeof(ecdsa_r_bytes) || - !BN_bn2bin(sig->r, ecdsa_r_bytes) || - BN_num_bytes(sig->s) != sizeof(ecdsa_s_bytes) || - !BN_bn2bin(sig->s, ecdsa_s_bytes) || - !check_test(kECDSASigR, ecdsa_r_bytes, sizeof(kECDSASigR), "ECDSA R") || - !check_test(kECDSASigS, ecdsa_s_bytes, sizeof(kECDSASigS), "ECDSA S")) { - fprintf(stderr, "ECDSA signature KAT failed.\n"); - goto err; - } - - if (!ECDSA_do_verify(kPlaintextSHA256, sizeof(kPlaintextSHA256), sig, - ec_key)) { - fprintf(stderr, "ECDSA verification KAT failed.\n"); - goto err; - } - - // Primitive Z Computation KAT (IG 9.6). - ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); - if (ec_group == NULL) { - fprintf(stderr, "Failed to create P-256 group.\n"); - goto err; - } - ec_point_in = EC_POINT_new(ec_group); - ec_point_out = EC_POINT_new(ec_group); - ec_scalar = BN_new(); - uint8_t z_comp_result[65]; - if (ec_point_in == NULL || ec_point_out == NULL || ec_scalar == NULL || - !EC_POINT_oct2point(ec_group, ec_point_in, kP256Point, sizeof(kP256Point), - NULL) || - !BN_bin2bn(kP256Scalar, sizeof(kP256Scalar), ec_scalar) || - !EC_POINT_mul(ec_group, ec_point_out, NULL, ec_point_in, ec_scalar, - NULL) || - !EC_POINT_point2oct(ec_group, ec_point_out, POINT_CONVERSION_UNCOMPRESSED, - z_comp_result, sizeof(z_comp_result), NULL) || - !check_test(kP256PointResult, z_comp_result, sizeof(z_comp_result), - "Z Computation Result")) { - fprintf(stderr, "Z Computation KAT failed.\n"); - goto err; - } - - // FFC Diffie-Hellman KAT - - BIGNUM *const ffdhe2048_value = BN_new(); - DH *const dh = self_test_dh(); - int dh_ok = 0; - if (ffdhe2048_value && dh) { - bn_set_static_words(ffdhe2048_value, kFFDHE2048PublicValueData, - OPENSSL_ARRAY_SIZE(kFFDHE2048PublicValueData)); - - uint8_t dh_out[sizeof(kDHOutput)]; - dh_ok = - sizeof(dh_out) == DH_size(dh) && - DH_compute_key_padded(dh_out, ffdhe2048_value, dh) == sizeof(dh_out) && - check_test(kDHOutput, dh_out, sizeof(dh_out), "FFC DH"); - } - - BN_free(ffdhe2048_value); - DH_free(dh); - if (!dh_ok) { - fprintf(stderr, "FFDH failed.\n"); + if (!boringssl_self_test_sha256() || + !boringssl_self_test_sha512() || + !boringssl_self_test_hmac_sha256()) { goto err; } // DBRG KAT + static const uint8_t kDRBGEntropy[48] = { + 0xc4, 0xda, 0x07, 0x40, 0xd5, 0x05, 0xf1, 0xee, 0x28, 0x0b, 0x95, 0xe5, + 0x8c, 0x49, 0x31, 0xac, 0x6d, 0xe8, 0x46, 0xa0, 0x15, 0x2f, 0xbb, 0x4a, + 0x3f, 0x17, 0x4c, 0xf4, 0x78, 0x7a, 0x4f, 0x1a, 0x40, 0xc2, 0xb5, 0x0b, + 0xab, 0xe1, 0x4a, 0xae, 0x53, 0x0b, 0xe5, 0x88, 0x6d, 0x91, 0x0a, 0x27, + }; + static const uint8_t kDRBGPersonalization[18] = "BCMPersonalization"; + static const uint8_t kDRBGAD[16] = "BCM DRBG KAT AD "; + static const uint8_t kDRBGOutput[64] = { + 0x19, 0x1f, 0x2b, 0x49, 0x76, 0x85, 0xfd, 0x51, 0xb6, 0x56, 0xbc, + 0x1c, 0x7d, 0xd5, 0xdd, 0x44, 0x76, 0xa3, 0x5e, 0x17, 0x9b, 0x8e, + 0xb8, 0x98, 0x65, 0x12, 0xca, 0x35, 0x6c, 0xa0, 0x6f, 0xa0, 0x22, + 0xe4, 0xf6, 0xd8, 0x43, 0xed, 0x4e, 0x2d, 0x97, 0x39, 0x43, 0x3b, + 0x57, 0xfc, 0x23, 0x3f, 0x71, 0x0a, 0xe0, 0xed, 0xfe, 0xd5, 0xb8, + 0x67, 0x7a, 0x00, 0x39, 0xb2, 0x6e, 0xa9, 0x25, 0x97, + }; + static const uint8_t kDRBGEntropy2[48] = { + 0xc7, 0x16, 0x1c, 0xa3, 0x6c, 0x23, 0x09, 0xb7, 0x16, 0xe9, 0x85, 0x9b, + 0xb9, 0x6c, 0x6d, 0x49, 0xbd, 0xc8, 0x35, 0x21, 0x03, 0xa1, 0x8c, 0xd2, + 0x4e, 0xf4, 0x2e, 0xc9, 0x7e, 0xf4, 0x6b, 0xf4, 0x46, 0xeb, 0x1a, 0x45, + 0x76, 0xc1, 0x86, 0xe9, 0x35, 0x18, 0x03, 0x76, 0x3a, 0x79, 0x12, 0xfe, + }; + static const uint8_t kDRBGReseedOutput[64] = { + 0x00, 0xf2, 0x05, 0xaa, 0xfd, 0x11, 0x6c, 0x77, 0xbc, 0x81, 0x86, + 0x99, 0xca, 0x51, 0xcf, 0x80, 0x15, 0x9f, 0x02, 0x9e, 0x0b, 0xcd, + 0x26, 0xc8, 0x4b, 0x87, 0x8a, 0x15, 0x1a, 0xdd, 0xf2, 0xf3, 0xeb, + 0x94, 0x0b, 0x08, 0xc8, 0xc9, 0x57, 0xa4, 0x0b, 0x4b, 0x0f, 0x13, + 0xde, 0x7c, 0x0c, 0x6a, 0xac, 0x34, 0x4a, 0x9a, 0xf2, 0xd0, 0x83, + 0x02, 0x05, 0x17, 0xc9, 0x81, 0x8f, 0x2a, 0x81, 0x92, + }; CTR_DRBG_STATE drbg; if (!CTR_DRBG_init(&drbg, kDRBGEntropy, kDRBGPersonalization, sizeof(kDRBGPersonalization)) || !CTR_DRBG_generate(&drbg, output, sizeof(kDRBGOutput), kDRBGAD, sizeof(kDRBGAD)) || !check_test(kDRBGOutput, output, sizeof(kDRBGOutput), - "DBRG Generate KAT") || + "DRBG Generate KAT") || !CTR_DRBG_reseed(&drbg, kDRBGEntropy2, kDRBGAD, sizeof(kDRBGAD)) || !CTR_DRBG_generate(&drbg, output, sizeof(kDRBGReseedOutput), kDRBGAD, sizeof(kDRBGAD)) || !check_test(kDRBGReseedOutput, output, sizeof(kDRBGReseedOutput), - "DRBG Reseed KAT")) { + "DRBG-reseed KAT")) { fprintf(stderr, "CTR-DRBG failed.\n"); goto err; } @@ -832,43 +906,59 @@ int boringssl_fips_self_test( } // TLS KDF KAT + static const uint8_t kTLSSecret[32] = { + 0xab, 0xc3, 0x65, 0x7b, 0x09, 0x4c, 0x76, 0x28, 0xa0, 0xb2, 0x82, + 0x99, 0x6f, 0xe7, 0x5a, 0x75, 0xf4, 0x98, 0x4f, 0xd9, 0x4d, 0x4e, + 0xcc, 0x2f, 0xcf, 0x53, 0xa2, 0xc4, 0x69, 0xa3, 0xf7, 0x31, + }; + static const char kTLSLabel[] = "FIPS self test"; + static const uint8_t kTLSSeed1[16] = { + 0x8f, 0x0d, 0xe8, 0xb6, 0x90, 0x8f, 0xb1, 0xd2, + 0x6d, 0x51, 0xf4, 0x79, 0x18, 0x63, 0x51, 0x65, + }; + static const uint8_t kTLSSeed2[16] = { + 0x7d, 0x24, 0x1a, 0x9d, 0x3c, 0x59, 0xbf, 0x3c, + 0x31, 0x1e, 0x2b, 0x21, 0x41, 0x8d, 0x32, 0x81, + }; + static const uint8_t kTLSOutput[32] = { + 0xe2, 0x1d, 0xd6, 0xc2, 0x68, 0xc7, 0x57, 0x03, 0x2c, 0x2c, 0xeb, + 0xbb, 0xb8, 0xa9, 0x7d, 0xe9, 0xee, 0xe6, 0xc9, 0x47, 0x83, 0x0a, + 0xbd, 0x11, 0x60, 0x5d, 0xd5, 0x2c, 0x47, 0xb6, 0x05, 0x88, + }; uint8_t tls_output[sizeof(kTLSOutput)]; if (!CRYPTO_tls1_prf(EVP_sha256(), tls_output, sizeof(tls_output), kTLSSecret, sizeof(kTLSSecret), kTLSLabel, sizeof(kTLSLabel), kTLSSeed1, sizeof(kTLSSeed1), kTLSSeed2, sizeof(kTLSSeed2)) || - !check_test(kTLSOutput, tls_output, sizeof(kTLSOutput), "TLS KDF KAT")) { + !check_test(kTLSOutput, tls_output, sizeof(kTLSOutput), "TLS-KDF KAT")) { fprintf(stderr, "TLS KDF failed.\n"); goto err; } ret = 1; -#if defined(BORINGSSL_FIPS_SELF_TEST_FLAG_FILE) - // Tests were successful. Write flag file if requested. - if (module_hash_len != 0 && getenv(kFlagWriteEnableEnvVar) != NULL) { - const int fd = open(flag_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd >= 0) { - close(fd); - } - } -#endif // BORINGSSL_FIPS_SELF_TEST_FLAG_FILE - err: EVP_AEAD_CTX_cleanup(&aead_ctx); - RSA_free(rsa_key); - EC_KEY_free(ec_key); - EC_POINT_free(ec_point_in); - EC_POINT_free(ec_point_out); - EC_GROUP_free(ec_group); - BN_free(ec_scalar); - ECDSA_SIG_free(sig); return ret; } int BORINGSSL_self_test(void) { - return boringssl_fips_self_test(NULL, 0); + if (!boringssl_self_test_fast() || + // When requested to run self tests, also run the lazy tests. + !boringssl_self_test_rsa() || + !boringssl_self_test_ecc() || + !boringssl_self_test_ffdh()) { + return 0; + } + + return 1; } +#if defined(BORINGSSL_FIPS) +int boringssl_self_test_startup(void) { + return boringssl_self_test_fast(); +} +#endif + #endif // !_MSC_VER diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S index 1b6f8fd3..c84b830a 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S @@ -53,7 +53,7 @@ Loop: movz w28,#0x7999 sub x2,x2,#1 movk w28,#0x5a82,lsl#16 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x3,x3,#32 #else rev32 x3,x3 @@ -71,7 +71,7 @@ Loop: ror w21,w21,#2 add w23,w23,w4 // future e+=X[i] add w24,w24,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x5,x5,#32 #else rev32 x5,x5 @@ -96,7 +96,7 @@ Loop: ror w24,w24,#2 add w21,w21,w6 // future e+=X[i] add w22,w22,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x7,x7,#32 #else rev32 x7,x7 @@ -121,7 +121,7 @@ Loop: ror w22,w22,#2 add w24,w24,w8 // future e+=X[i] add w20,w20,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x9,x9,#32 #else rev32 x9,x9 @@ -146,7 +146,7 @@ Loop: ror w20,w20,#2 add w22,w22,w10 // future e+=X[i] add w23,w23,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x11,x11,#32 #else rev32 x11,x11 @@ -171,7 +171,7 @@ Loop: ror w23,w23,#2 add w20,w20,w12 // future e+=X[i] add w21,w21,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x13,x13,#32 #else rev32 x13,x13 @@ -196,7 +196,7 @@ Loop: ror w21,w21,#2 add w23,w23,w14 // future e+=X[i] add w24,w24,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x15,x15,#32 #else rev32 x15,x15 @@ -221,7 +221,7 @@ Loop: ror w24,w24,#2 add w21,w21,w16 // future e+=X[i] add w22,w22,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x17,x17,#32 #else rev32 x17,x17 diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S index 76bc9ce6..929894ca 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S @@ -54,7 +54,7 @@ sha1_block_data_order: movz w28,#0x7999 sub x2,x2,#1 movk w28,#0x5a82,lsl#16 -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x3,x3,#32 #else rev32 x3,x3 @@ -72,7 +72,7 @@ sha1_block_data_order: ror w21,w21,#2 add w23,w23,w4 // future e+=X[i] add w24,w24,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x5,x5,#32 #else rev32 x5,x5 @@ -97,7 +97,7 @@ sha1_block_data_order: ror w24,w24,#2 add w21,w21,w6 // future e+=X[i] add w22,w22,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x7,x7,#32 #else rev32 x7,x7 @@ -122,7 +122,7 @@ sha1_block_data_order: ror w22,w22,#2 add w24,w24,w8 // future e+=X[i] add w20,w20,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x9,x9,#32 #else rev32 x9,x9 @@ -147,7 +147,7 @@ sha1_block_data_order: ror w20,w20,#2 add w22,w22,w10 // future e+=X[i] add w23,w23,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x11,x11,#32 #else rev32 x11,x11 @@ -172,7 +172,7 @@ sha1_block_data_order: ror w23,w23,#2 add w20,w20,w12 // future e+=X[i] add w21,w21,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x13,x13,#32 #else rev32 x13,x13 @@ -197,7 +197,7 @@ sha1_block_data_order: ror w21,w21,#2 add w23,w23,w14 // future e+=X[i] add w24,w24,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x15,x15,#32 #else rev32 x15,x15 @@ -222,7 +222,7 @@ sha1_block_data_order: ror w24,w24,#2 add w21,w21,w16 // future e+=X[i] add w22,w22,w25 // e+=F(b,c,d) -#ifdef __ARMEB__ +#ifdef __AARCH64EB__ ror x17,x17,#32 #else rev32 x17,x17 diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S index 55b8205c..ccd71ef1 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S @@ -27,6 +27,8 @@ sha256_block_data_order: movl 0(%r11),%r9d movl 4(%r11),%r10d movl 8(%r11),%r11d + testl $536870912,%r11d + jnz .Lshaext_shortcut andl $1073741824,%r9d andl $268435968,%r10d orl %r9d,%r10d @@ -1783,6 +1785,215 @@ K256: .long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908 .long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908 .byte 83,72,65,50,53,54,32,98,108,111,99,107,32,116,114,97,110,115,102,111,114,109,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 +.type sha256_block_data_order_shaext,@function +.align 64 +sha256_block_data_order_shaext: +.cfi_startproc +.Lshaext_shortcut: + leaq K256+128(%rip),%rcx + movdqu (%rdi),%xmm1 + movdqu 16(%rdi),%xmm2 + movdqa 512-128(%rcx),%xmm7 + + pshufd $0x1b,%xmm1,%xmm0 + pshufd $0xb1,%xmm1,%xmm1 + pshufd $0x1b,%xmm2,%xmm2 + movdqa %xmm7,%xmm8 +.byte 102,15,58,15,202,8 + punpcklqdq %xmm0,%xmm2 + jmp .Loop_shaext + +.align 16 +.Loop_shaext: + movdqu (%rsi),%xmm3 + movdqu 16(%rsi),%xmm4 + movdqu 32(%rsi),%xmm5 +.byte 102,15,56,0,223 + movdqu 48(%rsi),%xmm6 + + movdqa 0-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 102,15,56,0,231 + movdqa %xmm2,%xmm10 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + nop + movdqa %xmm1,%xmm9 +.byte 15,56,203,202 + + movdqa 32-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 102,15,56,0,239 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + leaq 64(%rsi),%rsi +.byte 15,56,204,220 +.byte 15,56,203,202 + + movdqa 64-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 102,15,56,0,247 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + + movdqa 96-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 128-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 160-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 + nop + paddd %xmm7,%xmm6 +.byte 15,56,204,220 +.byte 15,56,203,202 + movdqa 192-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,205,245 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + movdqa 224-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 256-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 288-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 + nop + paddd %xmm7,%xmm6 +.byte 15,56,204,220 +.byte 15,56,203,202 + movdqa 320-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,205,245 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + movdqa 352-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 384-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 416-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 +.byte 15,56,203,202 + paddd %xmm7,%xmm6 + + movdqa 448-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 +.byte 15,56,205,245 + movdqa %xmm8,%xmm7 +.byte 15,56,203,202 + + movdqa 480-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 + nop +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + decq %rdx + nop +.byte 15,56,203,202 + + paddd %xmm10,%xmm2 + paddd %xmm9,%xmm1 + jnz .Loop_shaext + + pshufd $0xb1,%xmm2,%xmm2 + pshufd $0x1b,%xmm1,%xmm7 + pshufd $0xb1,%xmm1,%xmm1 + punpckhqdq %xmm2,%xmm1 +.byte 102,15,58,15,215,8 + + movdqu %xmm1,(%rdi) + movdqu %xmm2,16(%rdi) + .byte 0xf3,0xc3 +.cfi_endproc +.size sha256_block_data_order_shaext,.-sha256_block_data_order_shaext .type sha256_block_data_order_ssse3,@function .align 64 sha256_block_data_order_ssse3: diff --git a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S index dc41bbea..9e9682b0 100644 --- a/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S +++ b/Sources/CCryptoBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S @@ -26,6 +26,8 @@ _sha256_block_data_order: movl 0(%r11),%r9d movl 4(%r11),%r10d movl 8(%r11),%r11d + testl $536870912,%r11d + jnz L$shaext_shortcut andl $1073741824,%r9d andl $268435968,%r10d orl %r9d,%r10d @@ -1783,6 +1785,215 @@ K256: .long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908 .byte 83,72,65,50,53,54,32,98,108,111,99,107,32,116,114,97,110,115,102,111,114,109,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 +.p2align 6 +sha256_block_data_order_shaext: + +L$shaext_shortcut: + leaq K256+128(%rip),%rcx + movdqu (%rdi),%xmm1 + movdqu 16(%rdi),%xmm2 + movdqa 512-128(%rcx),%xmm7 + + pshufd $0x1b,%xmm1,%xmm0 + pshufd $0xb1,%xmm1,%xmm1 + pshufd $0x1b,%xmm2,%xmm2 + movdqa %xmm7,%xmm8 +.byte 102,15,58,15,202,8 + punpcklqdq %xmm0,%xmm2 + jmp L$oop_shaext + +.p2align 4 +L$oop_shaext: + movdqu (%rsi),%xmm3 + movdqu 16(%rsi),%xmm4 + movdqu 32(%rsi),%xmm5 +.byte 102,15,56,0,223 + movdqu 48(%rsi),%xmm6 + + movdqa 0-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 102,15,56,0,231 + movdqa %xmm2,%xmm10 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + nop + movdqa %xmm1,%xmm9 +.byte 15,56,203,202 + + movdqa 32-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 102,15,56,0,239 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + leaq 64(%rsi),%rsi +.byte 15,56,204,220 +.byte 15,56,203,202 + + movdqa 64-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 102,15,56,0,247 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + + movdqa 96-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 128-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 160-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 + nop + paddd %xmm7,%xmm6 +.byte 15,56,204,220 +.byte 15,56,203,202 + movdqa 192-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,205,245 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + movdqa 224-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 256-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 288-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 + nop + paddd %xmm7,%xmm6 +.byte 15,56,204,220 +.byte 15,56,203,202 + movdqa 320-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,205,245 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm6,%xmm7 +.byte 102,15,58,15,253,4 + nop + paddd %xmm7,%xmm3 +.byte 15,56,204,229 +.byte 15,56,203,202 + movdqa 352-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 +.byte 15,56,205,222 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm3,%xmm7 +.byte 102,15,58,15,254,4 + nop + paddd %xmm7,%xmm4 +.byte 15,56,204,238 +.byte 15,56,203,202 + movdqa 384-128(%rcx),%xmm0 + paddd %xmm3,%xmm0 +.byte 15,56,205,227 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm4,%xmm7 +.byte 102,15,58,15,251,4 + nop + paddd %xmm7,%xmm5 +.byte 15,56,204,243 +.byte 15,56,203,202 + movdqa 416-128(%rcx),%xmm0 + paddd %xmm4,%xmm0 +.byte 15,56,205,236 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + movdqa %xmm5,%xmm7 +.byte 102,15,58,15,252,4 +.byte 15,56,203,202 + paddd %xmm7,%xmm6 + + movdqa 448-128(%rcx),%xmm0 + paddd %xmm5,%xmm0 +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 +.byte 15,56,205,245 + movdqa %xmm8,%xmm7 +.byte 15,56,203,202 + + movdqa 480-128(%rcx),%xmm0 + paddd %xmm6,%xmm0 + nop +.byte 15,56,203,209 + pshufd $0x0e,%xmm0,%xmm0 + decq %rdx + nop +.byte 15,56,203,202 + + paddd %xmm10,%xmm2 + paddd %xmm9,%xmm1 + jnz L$oop_shaext + + pshufd $0xb1,%xmm2,%xmm2 + pshufd $0x1b,%xmm1,%xmm7 + pshufd $0xb1,%xmm1,%xmm1 + punpckhqdq %xmm2,%xmm1 +.byte 102,15,58,15,215,8 + + movdqu %xmm1,(%rdi) + movdqu %xmm2,16(%rdi) + .byte 0xf3,0xc3 + + + .p2align 6 sha256_block_data_order_ssse3: diff --git a/Sources/CCryptoBoringSSL/crypto/hpke/hpke.c b/Sources/CCryptoBoringSSL/crypto/hpke/hpke.c index c3175090..bee05de7 100644 --- a/Sources/CCryptoBoringSSL/crypto/hpke/hpke.c +++ b/Sources/CCryptoBoringSSL/crypto/hpke/hpke.c @@ -30,7 +30,7 @@ #include "../internal.h" -// This file implements draft-irtf-cfrg-hpke-12. +// This file implements RFC 9180. #define MAX_SEED_LEN X25519_PRIVATE_KEY_LEN #define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH @@ -115,7 +115,7 @@ static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key, // KEM implementations. // dhkem_extract_and_expand implements the ExtractAndExpand operation in the -// DHKEM construction. See section 4.1 of draft-irtf-cfrg-hpke-12. +// DHKEM construction. See section 4.1 of RFC 9180. static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md, uint8_t *out_key, size_t out_len, const uint8_t *dh, size_t dh_len, diff --git a/Sources/CCryptoBoringSSL/crypto/hrss/hrss.c b/Sources/CCryptoBoringSSL/crypto/hrss/hrss.c index 58aa125a..edd028a3 100644 --- a/Sources/CCryptoBoringSSL/crypto/hrss/hrss.c +++ b/Sources/CCryptoBoringSSL/crypto/hrss/hrss.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -39,8 +38,7 @@ #include #endif -#if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \ - (defined(__ARM_NEON__) || defined(__ARM_NEON)) +#if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && defined(__ARM_NEON) #include #endif @@ -190,8 +188,7 @@ static inline vec_t vec_broadcast_bit(vec_t a) { // compiler requires that |i| be a compile-time constant.) #define vec_get_word(v, i) _mm_extract_epi16(v, i) -#elif (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \ - (defined(__ARM_NEON__) || defined(__ARM_NEON)) +#elif (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && defined(__ARM_NEON) #define HRSS_HAVE_VECTOR_UNIT typedef uint16x8_t vec_t; @@ -1318,8 +1315,7 @@ static void poly_mul_novec(struct POLY_MUL_SCRATCH *scratch, struct poly *out, static void poly_mul(struct POLY_MUL_SCRATCH *scratch, struct poly *r, const struct poly *a, const struct poly *b) { #if defined(POLY_RQ_MUL_ASM) - const int has_avx2 = (OPENSSL_ia32cap_P[2] & (1 << 5)) != 0; - if (has_avx2) { + if (CRYPTO_is_AVX2_capable()) { poly_Rq_mul(r->v, a->v, b->v, scratch->u.rq); return; } diff --git a/Sources/CCryptoBoringSSL/crypto/internal.h b/Sources/CCryptoBoringSSL/crypto/internal.h index 81813aba..e8088ee2 100644 --- a/Sources/CCryptoBoringSSL/crypto/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/internal.h @@ -121,6 +121,10 @@ #include #endif +#if defined(BORINGSSL_FIPS_BREAK_TESTS) +#include +#endif + #if !defined(__cplusplus) #if defined(_MSC_VER) #define alignas(x) __declspec(align(x)) @@ -932,19 +936,50 @@ static inline uint64_t CRYPTO_rotr_u64(uint64_t value, int shift) { // FIPS functions. #if defined(BORINGSSL_FIPS) + // BORINGSSL_FIPS_abort is called when a FIPS power-on or continuous test // fails. It prevents any further cryptographic operations by the current // process. void BORINGSSL_FIPS_abort(void) __attribute__((noreturn)); -#endif -// boringssl_fips_self_test runs the FIPS KAT-based self tests. It returns one -// on success and zero on error. The argument is the integrity hash of the FIPS -// module and may be used to check and write flag files to suppress duplicate -// self-tests. If |module_hash_len| is zero then no flag file will be checked -// nor written and tests will always be run. -int boringssl_fips_self_test(const uint8_t *module_hash, - size_t module_hash_len); +// boringssl_self_test_startup runs all startup self tests and returns one on +// success or zero on error. Startup self tests do not include lazy tests. +// Call |BORINGSSL_self_test| to run every self test. +int boringssl_self_test_startup(void); + +// boringssl_ensure_rsa_self_test checks whether the RSA self-test has been run +// in this address space. If not, it runs it and crashes the address space if +// unsuccessful. +void boringssl_ensure_rsa_self_test(void); + +// boringssl_ensure_ecc_self_test checks whether the ECDSA and ECDH self-test +// has been run in this address space. If not, it runs it and crashes the +// address space if unsuccessful. +void boringssl_ensure_ecc_self_test(void); + +// boringssl_ensure_ffdh_self_test checks whether the FFDH self-test has been +// run in this address space. If not, it runs it and crashes the address space +// if unsuccessful. +void boringssl_ensure_ffdh_self_test(void); + +#else + +// Outside of FIPS mode, the lazy tests are no-ops. + +OPENSSL_INLINE void boringssl_ensure_rsa_self_test(void) {} +OPENSSL_INLINE void boringssl_ensure_ecc_self_test(void) {} +OPENSSL_INLINE void boringssl_ensure_ffdh_self_test(void) {} + +#endif // FIPS + +// boringssl_self_test_sha256 performs a SHA-256 KAT. +int boringssl_self_test_sha256(void); + +// boringssl_self_test_sha512 performs a SHA-512 KAT. +int boringssl_self_test_sha512(void); + +// boringssl_self_test_hmac_sha256 performs an HMAC-SHA-256 KAT. +int boringssl_self_test_hmac_sha256(void); #if defined(BORINGSSL_FIPS_COUNTERS) void boringssl_fips_inc_counter(enum fips_counter_t counter); @@ -952,6 +987,260 @@ void boringssl_fips_inc_counter(enum fips_counter_t counter); OPENSSL_INLINE void boringssl_fips_inc_counter(enum fips_counter_t counter) {} #endif +#if defined(BORINGSSL_FIPS_BREAK_TESTS) +OPENSSL_INLINE int boringssl_fips_break_test(const char *test) { + const char *const value = getenv("BORINGSSL_FIPS_BREAK_TEST"); + return value != NULL && strcmp(value, test) == 0; +} +#else +OPENSSL_INLINE int boringssl_fips_break_test(const char *test) { + return 0; +} +#endif // BORINGSSL_FIPS_BREAK_TESTS + + +// Runtime CPU feature support + +#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) +// OPENSSL_ia32cap_P contains the Intel CPUID bits when running on an x86 or +// x86-64 system. +// +// Index 0: +// EDX for CPUID where EAX = 1 +// Bit 20 is always zero +// Bit 28 is adjusted to reflect whether the data cache is shared between +// multiple logical cores +// Bit 30 is used to indicate an Intel CPU +// Index 1: +// ECX for CPUID where EAX = 1 +// Bit 11 is used to indicate AMD XOP support, not SDBG +// Index 2: +// EBX for CPUID where EAX = 7 +// Index 3: +// ECX for CPUID where EAX = 7 +// +// Note: the CPUID bits are pre-adjusted for the OSXSAVE bit and the YMM and XMM +// bits in XCR0, so it is not necessary to check those. +extern uint32_t OPENSSL_ia32cap_P[4]; + +#if defined(BORINGSSL_FIPS) && !defined(BORINGSSL_SHARED_LIBRARY) +// The FIPS module, as a static library, requires an out-of-line version of +// |OPENSSL_ia32cap_get| so accesses can be rewritten by delocate. Mark the +// function const so multiple accesses can be optimized together. +const uint32_t *OPENSSL_ia32cap_get(void) __attribute__((const)); +#else +OPENSSL_INLINE const uint32_t *OPENSSL_ia32cap_get(void) { + return OPENSSL_ia32cap_P; +} +#endif + +// See Intel manual, volume 2A, table 3-11. + +OPENSSL_INLINE int CRYPTO_is_FXSR_capable(void) { +#if defined(__FXSR__) + return 1; +#else + return (OPENSSL_ia32cap_get()[0] & (1 << 24)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_intel_cpu(void) { + // The reserved bit 30 is used to indicate an Intel CPU. + return (OPENSSL_ia32cap_get()[0] & (1 << 30)) != 0; +} + +// See Intel manual, volume 2A, table 3-10. + +OPENSSL_INLINE int CRYPTO_is_PCLMUL_capable(void) { +#if defined(__PCLMUL__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1 << 1)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_SSSE3_capable(void) { +#if defined(__SSSE3__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1 << 9)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_SSE4_1_capable(void) { +#if defined(__SSE4_1__) + return 1; +#else + return (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_MOVBE_capable(void) { +#if defined(__MOVBE__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1 << 22)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_AESNI_capable(void) { +#if defined(__AES__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1 << 25)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_AVX_capable(void) { +#if defined(__AVX__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1 << 28)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_RDRAND_capable(void) { + // The GCC/Clang feature name and preprocessor symbol for RDRAND are "rdrnd" + // and |__RDRND__|, respectively. +#if defined(__RDRND__) + return 1; +#else + return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0; +#endif +} + +// See Intel manual, volume 2A, table 3-8. + +OPENSSL_INLINE int CRYPTO_is_BMI1_capable(void) { +#if defined(__BMI1__) + return 1; +#else + return (OPENSSL_ia32cap_get()[2] & (1 << 3)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_AVX2_capable(void) { +#if defined(__AVX2__) + return 1; +#else + return (OPENSSL_ia32cap_get()[2] & (1 << 5)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_BMI2_capable(void) { +#if defined(__BMI2__) + return 1; +#else + return (OPENSSL_ia32cap_get()[2] & (1 << 8)) != 0; +#endif +} + +OPENSSL_INLINE int CRYPTO_is_ADX_capable(void) { +#if defined(__ADX__) + return 1; +#else + return (OPENSSL_ia32cap_get()[2] & (1 << 19)) != 0; +#endif +} + +#endif // OPENSSL_X86 || OPENSSL_X86_64 + +#if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) + +#if defined(OPENSSL_APPLE) && defined(OPENSSL_ARM) +// We do not detect any features at runtime for Apple's 32-bit ARM platforms. On +// 64-bit ARM, we detect some post-ARMv8.0 features. +#define OPENSSL_STATIC_ARMCAP +#endif + +// Normalize some older feature flags to their modern ACLE values. +// https://developer.arm.com/architectures/system-architectures/software-standards/acle +#if defined(__ARM_NEON__) && !defined(__ARM_NEON) +#define __ARM_NEON 1 +#endif +#if defined(__ARM_FEATURE_CRYPTO) +#if !defined(__ARM_FEATURE_AES) +#define __ARM_FEATURE_AES 1 +#endif +#if !defined(__ARM_FEATURE_SHA2) +#define __ARM_FEATURE_SHA2 1 +#endif +#endif + +#if !defined(OPENSSL_STATIC_ARMCAP) +// CRYPTO_is_NEON_capable_at_runtime returns true if the current CPU has a NEON +// unit. Note that |OPENSSL_armcap_P| also exists and contains the same +// information in a form that's easier for assembly to use. +OPENSSL_EXPORT int CRYPTO_is_NEON_capable_at_runtime(void); + +// CRYPTO_is_ARMv8_AES_capable_at_runtime returns true if the current CPU +// supports the ARMv8 AES instruction. +int CRYPTO_is_ARMv8_AES_capable_at_runtime(void); + +// CRYPTO_is_ARMv8_PMULL_capable_at_runtime returns true if the current CPU +// supports the ARMv8 PMULL instruction. +int CRYPTO_is_ARMv8_PMULL_capable_at_runtime(void); +#endif // !OPENSSL_STATIC_ARMCAP + +// CRYPTO_is_NEON_capable returns true if the current CPU has a NEON unit. If +// this is known statically, it is a constant inline function. +OPENSSL_INLINE int CRYPTO_is_NEON_capable(void) { +#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON) + return 1; +#elif defined(OPENSSL_STATIC_ARMCAP) + return 0; +#else + return CRYPTO_is_NEON_capable_at_runtime(); +#endif +} + +OPENSSL_INLINE int CRYPTO_is_ARMv8_AES_capable(void) { +#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES) + return 1; +#elif defined(OPENSSL_STATIC_ARMCAP) + return 0; +#else + return CRYPTO_is_ARMv8_AES_capable_at_runtime(); +#endif +} + +OPENSSL_INLINE int CRYPTO_is_ARMv8_PMULL_capable(void) { +#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES) + return 1; +#elif defined(OPENSSL_STATIC_ARMCAP) + return 0; +#else + return CRYPTO_is_ARMv8_PMULL_capable_at_runtime(); +#endif +} + +#endif // OPENSSL_ARM || OPENSSL_AARCH64 + +#if defined(OPENSSL_PPC64LE) + +// CRYPTO_is_PPC64LE_vcrypto_capable returns true iff the current CPU supports +// the Vector.AES category of instructions. +int CRYPTO_is_PPC64LE_vcrypto_capable(void); + +extern unsigned long OPENSSL_ppc64le_hwcap2; + +#endif // OPENSSL_PPC64LE + +#if defined(BORINGSSL_DISPATCH_TEST) +// Runtime CPU dispatch testing support + +// BORINGSSL_function_hit is an array of flags. The following functions will +// set these flags if BORINGSSL_DISPATCH_TEST is defined. +// 0: aes_hw_ctr32_encrypt_blocks +// 1: aes_hw_encrypt +// 2: aesni_gcm_encrypt +// 3: aes_hw_set_encrypt_key +// 4: vpaes_encrypt +// 5: vpaes_set_encrypt_key +extern uint8_t BORINGSSL_function_hit[7]; +#endif // BORINGSSL_DISPATCH_TEST + + #if defined(__cplusplus) } // extern C #endif diff --git a/Sources/CCryptoBoringSSL/crypto/mem.c b/Sources/CCryptoBoringSSL/crypto/mem.c index 5c8afec2..c1750d8b 100644 --- a/Sources/CCryptoBoringSSL/crypto/mem.c +++ b/Sources/CCryptoBoringSSL/crypto/mem.c @@ -132,7 +132,7 @@ static const uint8_t kBoringSSLBinaryTag[18] = { 0x8c, 0x62, 0x20, 0x0b, 0xd2, 0xa0, 0x72, 0x58, 0x44, 0xa8, 0x96, 0x69, 0xad, 0x55, 0x7e, 0xec, // Current source iteration. Incremented ~monthly. - 2, 0, + 3, 0, }; void *OPENSSL_malloc(size_t size) { diff --git a/Sources/CCryptoBoringSSL/crypto/pem/pem_all.c b/Sources/CCryptoBoringSSL/crypto/pem/pem_all.c index 2a726927..d0284b66 100644 --- a/Sources/CCryptoBoringSSL/crypto/pem/pem_all.c +++ b/Sources/CCryptoBoringSSL/crypto/pem/pem_all.c @@ -200,7 +200,7 @@ DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) - IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) +IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) { EVP_PKEY *pktmp; @@ -237,7 +237,7 @@ EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb, IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY, ECPrivateKey) - IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY) +IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY) EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb, void *u) { @@ -247,6 +247,6 @@ EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb, } -IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) +IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) - IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) +IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) diff --git a/Sources/CCryptoBoringSSL/crypto/pem/pem_pkey.c b/Sources/CCryptoBoringSSL/crypto/pem/pem_pkey.c index 0f500f18..c163634f 100644 --- a/Sources/CCryptoBoringSSL/crypto/pem/pem_pkey.c +++ b/Sources/CCryptoBoringSSL/crypto/pem/pem_pkey.c @@ -176,39 +176,3 @@ int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, BIO_free(b); return ret; } - - -/* Transparently read in PKCS#3 or X9.42 DH parameters */ - -DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u) -{ - char *nm = NULL; - const unsigned char *p = NULL; - unsigned char *data = NULL; - long len; - DH *ret = NULL; - - if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u)) - return NULL; - p = data; - - ret = d2i_DHparams(x, &p, len); - - if (ret == NULL) - OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB); - OPENSSL_free(nm); - OPENSSL_free(data); - return ret; -} - -DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u) -{ - BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); - if (b == NULL) { - OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); - return NULL; - } - DH *ret = PEM_read_bio_DHparams(b, x, cb, u); - BIO_free(b); - return ret; -} diff --git a/Sources/CCryptoBoringSSL/crypto/poly1305/poly1305.c b/Sources/CCryptoBoringSSL/crypto/poly1305/poly1305.c index f5b7e721..3ee5a552 100644 --- a/Sources/CCryptoBoringSSL/crypto/poly1305/poly1305.c +++ b/Sources/CCryptoBoringSSL/crypto/poly1305/poly1305.c @@ -20,8 +20,6 @@ #include -#include - #include "internal.h" #include "../internal.h" diff --git a/Sources/CCryptoBoringSSL/crypto/pool/internal.h b/Sources/CCryptoBoringSSL/crypto/pool/internal.h index f3ea349e..dab6b35e 100644 --- a/Sources/CCryptoBoringSSL/crypto/pool/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/pool/internal.h @@ -39,6 +39,7 @@ struct crypto_buffer_st { struct crypto_buffer_pool_st { LHASH_OF(CRYPTO_BUFFER) *bufs; CRYPTO_MUTEX lock; + const uint64_t hash_key[2]; }; diff --git a/Sources/CCryptoBoringSSL/crypto/pool/pool.c b/Sources/CCryptoBoringSSL/crypto/pool/pool.c index ca524564..f1575c1c 100644 --- a/Sources/CCryptoBoringSSL/crypto/pool/pool.c +++ b/Sources/CCryptoBoringSSL/crypto/pool/pool.c @@ -19,6 +19,8 @@ #include #include +#include +#include #include #include "../internal.h" @@ -26,10 +28,13 @@ static uint32_t CRYPTO_BUFFER_hash(const CRYPTO_BUFFER *buf) { - return OPENSSL_hash32(buf->data, buf->len); + return (uint32_t)SIPHASH_24(buf->pool->hash_key, buf->data, buf->len); } static int CRYPTO_BUFFER_cmp(const CRYPTO_BUFFER *a, const CRYPTO_BUFFER *b) { + // Only |CRYPTO_BUFFER|s from the same pool have compatible hashes. + assert(a->pool != NULL); + assert(a->pool == b->pool); if (a->len != b->len) { return 1; } @@ -50,6 +55,7 @@ CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void) { } CRYPTO_MUTEX_init(&pool->lock); + RAND_bytes((uint8_t *)&pool->hash_key, sizeof(pool->hash_key)); return pool; } @@ -84,6 +90,7 @@ static CRYPTO_BUFFER *crypto_buffer_new(const uint8_t *data, size_t len, CRYPTO_BUFFER tmp; tmp.data = (uint8_t *) data; tmp.len = len; + tmp.pool = pool; CRYPTO_MUTEX_lock_read(&pool->lock); CRYPTO_BUFFER *duplicate = lh_CRYPTO_BUFFER_retrieve(pool->bufs, &tmp); diff --git a/Sources/CCryptoBoringSSL/crypto/x509/by_dir.c b/Sources/CCryptoBoringSSL/crypto/x509/by_dir.c index 5d4ae606..965f31ae 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/by_dir.c +++ b/Sources/CCryptoBoringSSL/crypto/x509/by_dir.c @@ -438,6 +438,13 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, ok = 1; ret->type = tmp->type; OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data)); + + /* + * Clear any errors that might have been raised processing empty + * or malformed files. + */ + ERR_clear_error(); + /* * If we were going to up the reference count, we would need * to do it on a perl 'type' basis diff --git a/Sources/CCryptoBoringSSL/crypto/x509/by_file.c b/Sources/CCryptoBoringSSL/crypto/x509/by_file.c index 87d75824..edd54279 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/by_file.c +++ b/Sources/CCryptoBoringSSL/crypto/x509/by_file.c @@ -126,8 +126,6 @@ int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) int i, count = 0; X509 *x = NULL; - if (file == NULL) - return (1); in = BIO_new(BIO_s_file()); if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { @@ -171,6 +169,11 @@ int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) OPENSSL_PUT_ERROR(X509, X509_R_BAD_X509_FILETYPE); goto err; } + + if (ret == 0) { + OPENSSL_PUT_ERROR(X509, X509_R_NO_CERTIFICATE_FOUND); + } + err: if (x != NULL) X509_free(x); @@ -186,8 +189,6 @@ int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) int i, count = 0; X509_CRL *x = NULL; - if (file == NULL) - return (1); in = BIO_new(BIO_s_file()); if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { @@ -231,6 +232,11 @@ int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) OPENSSL_PUT_ERROR(X509, X509_R_BAD_X509_FILETYPE); goto err; } + + if (ret == 0) { + OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_FOUND); + } + err: if (x != NULL) X509_CRL_free(x); @@ -246,6 +252,7 @@ int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) BIO *in; size_t i; int count = 0; + if (type != X509_FILETYPE_PEM) return X509_load_cert_file(ctx, file, type); in = BIO_new_file(file, "r"); @@ -262,14 +269,24 @@ int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) for (i = 0; i < sk_X509_INFO_num(inf); i++) { itmp = sk_X509_INFO_value(inf, i); if (itmp->x509) { - X509_STORE_add_cert(ctx->store_ctx, itmp->x509); + if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) { + goto err; + } count++; } if (itmp->crl) { - X509_STORE_add_crl(ctx->store_ctx, itmp->crl); + if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) { + goto err; + } count++; } } + + if (count == 0) { + OPENSSL_PUT_ERROR(X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND); + } + +err: sk_X509_INFO_pop_free(inf, X509_INFO_free); return count; } diff --git a/Sources/CCryptoBoringSSL/crypto/x509/internal.h b/Sources/CCryptoBoringSSL/crypto/x509/internal.h index cbb059bf..d06a5fcb 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/x509/internal.h @@ -106,13 +106,14 @@ struct x509_attributes_st { STACK_OF(ASN1_TYPE) *set; } /* X509_ATTRIBUTE */; -struct x509_cert_aux_st { +typedef struct x509_cert_aux_st { STACK_OF(ASN1_OBJECT) *trust; // trusted uses STACK_OF(ASN1_OBJECT) *reject; // rejected uses ASN1_UTF8STRING *alias; // "friendly name" ASN1_OCTET_STRING *keyid; // key id of private key - STACK_OF(X509_ALGOR) *other; // other unspecified info -} /* X509_CERT_AUX */; +} X509_CERT_AUX; + +DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX) struct X509_extension_st { ASN1_OBJECT *object; @@ -370,6 +371,8 @@ struct x509_store_ctx_st { ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf); +int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent); + /* RSA-PSS functions. */ diff --git a/Sources/CCryptoBoringSSL/crypto/x509/t_x509.c b/Sources/CCryptoBoringSSL/crypto/x509/t_x509.c index c0131b10..393bad61 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/t_x509.c +++ b/Sources/CCryptoBoringSSL/crypto/x509/t_x509.c @@ -134,13 +134,12 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, } const ASN1_INTEGER *serial = X509_get0_serialNumber(x); - /* |ASN1_INTEGER_get| returns -1 on overflow, so this check skips - * negative and large serial numbers. */ - l = ASN1_INTEGER_get(serial); - if (l >= 0) { + uint64_t serial_u64; + if (ASN1_INTEGER_get_uint64(&serial_u64, serial)) { assert(serial->type != V_ASN1_NEG_INTEGER); - if (BIO_printf(bp, " %ld (0x%lx)\n", l, (unsigned long)l) <= 0) { - goto err; + if (BIO_printf(bp, " %" PRIu64 " (0x%" PRIx64 ")\n", serial_u64, + serial_u64) <= 0) { + goto err; } } else { neg = (serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : ""; diff --git a/Sources/CCryptoBoringSSL/crypto/x509/x509_lu.c b/Sources/CCryptoBoringSSL/crypto/x509/x509_lu.c index 3693f5ea..6e926516 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/x509_lu.c +++ b/Sources/CCryptoBoringSSL/crypto/x509/x509_lu.c @@ -332,76 +332,54 @@ int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, return 1; } -int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) +static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) { - X509_OBJECT *obj; - int ret = 1; - - if (x == NULL) + if (x == NULL) { return 0; - obj = (X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT)); + } + + X509_OBJECT *const obj = (X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT)); if (obj == NULL) { OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); return 0; } - obj->type = X509_LU_X509; - obj->data.x509 = x; + + if (is_crl) { + obj->type = X509_LU_CRL; + obj->data.crl = (X509_CRL *)x; + } else { + obj->type = X509_LU_X509; + obj->data.x509 = (X509 *)x; + } + X509_OBJECT_up_ref_count(obj); CRYPTO_MUTEX_lock_write(&ctx->objs_lock); - X509_OBJECT_up_ref_count(obj); - - if (X509_OBJECT_retrieve_match(ctx->objs, obj)) { - X509_OBJECT_free_contents(obj); - OPENSSL_free(obj); - OPENSSL_PUT_ERROR(X509, X509_R_CERT_ALREADY_IN_HASH_TABLE); - ret = 0; - } else if (!sk_X509_OBJECT_push(ctx->objs, obj)) { - X509_OBJECT_free_contents(obj); - OPENSSL_free(obj); - OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); - ret = 0; + int ret = 1; + int added = 0; + /* Duplicates are silently ignored */ + if (!X509_OBJECT_retrieve_match(ctx->objs, obj)) { + ret = added = (sk_X509_OBJECT_push(ctx->objs, obj) != 0); } CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); + if (!added) { + X509_OBJECT_free_contents(obj); + OPENSSL_free(obj); + } + return ret; } +int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) +{ + return x509_store_add(ctx, x, /*is_crl=*/0); +} + int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) { - X509_OBJECT *obj; - int ret = 1; - - if (x == NULL) - return 0; - obj = (X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT)); - if (obj == NULL) { - OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); - return 0; - } - obj->type = X509_LU_CRL; - obj->data.crl = x; - - CRYPTO_MUTEX_lock_write(&ctx->objs_lock); - - X509_OBJECT_up_ref_count(obj); - - if (X509_OBJECT_retrieve_match(ctx->objs, obj)) { - X509_OBJECT_free_contents(obj); - OPENSSL_free(obj); - OPENSSL_PUT_ERROR(X509, X509_R_CERT_ALREADY_IN_HASH_TABLE); - ret = 0; - } else if (!sk_X509_OBJECT_push(ctx->objs, obj)) { - X509_OBJECT_free_contents(obj); - OPENSSL_free(obj); - OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE); - ret = 0; - } - - CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); - - return ret; + return x509_store_add(ctx, x, /*is_crl=*/1); } int X509_OBJECT_up_ref_count(X509_OBJECT *a) diff --git a/Sources/CCryptoBoringSSL/crypto/x509/x_x509a.c b/Sources/CCryptoBoringSSL/crypto/x509/x_x509a.c index 22025bdb..8c8ca68a 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509/x_x509a.c +++ b/Sources/CCryptoBoringSSL/crypto/x509/x_x509a.c @@ -78,7 +78,6 @@ ASN1_SEQUENCE(X509_CERT_AUX) = { ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0), ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING), ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING), - ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1) } ASN1_SEQUENCE_END(X509_CERT_AUX) IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX) @@ -95,6 +94,9 @@ static X509_CERT_AUX *aux_get(X509 *x) int X509_alias_set1(X509 *x, const unsigned char *name, int len) { X509_CERT_AUX *aux; + /* TODO(davidben): Empty aliases are not meaningful in PKCS#12, and the + * getters cannot quite represent them. Also erase the object if |len| is + * zero. */ if (!name) { if (!x || !x->aux || !x->aux->alias) return 1; @@ -112,6 +114,9 @@ int X509_alias_set1(X509 *x, const unsigned char *name, int len) int X509_keyid_set1(X509 *x, const unsigned char *id, int len) { X509_CERT_AUX *aux; + /* TODO(davidben): Empty key IDs are not meaningful in PKCS#12, and the + * getters cannot quite represent them. Also erase the object if |len| is + * zero. */ if (!id) { if (!x || !x->aux || !x->aux->keyid) return 1; @@ -126,22 +131,22 @@ int X509_keyid_set1(X509 *x, const unsigned char *id, int len) return ASN1_STRING_set(aux->keyid, id, len); } -unsigned char *X509_alias_get0(X509 *x, int *len) +unsigned char *X509_alias_get0(X509 *x, int *out_len) { - if (!x->aux || !x->aux->alias) - return NULL; - if (len) - *len = x->aux->alias->length; - return x->aux->alias->data; + const ASN1_UTF8STRING *alias = x->aux != NULL ? x->aux->alias : NULL; + if (out_len != NULL) { + *out_len = alias != NULL ? alias->length : 0; + } + return alias != NULL ? alias->data : NULL; } -unsigned char *X509_keyid_get0(X509 *x, int *len) +unsigned char *X509_keyid_get0(X509 *x, int *out_len) { - if (!x->aux || !x->aux->keyid) - return NULL; - if (len) - *len = x->aux->keyid->length; - return x->aux->keyid->data; + const ASN1_OCTET_STRING *keyid = x->aux != NULL ? x->aux->keyid : NULL; + if (out_len != NULL) { + *out_len = keyid != NULL ? keyid->length : 0; + } + return keyid != NULL ? keyid->data : NULL; } int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj) diff --git a/Sources/CCryptoBoringSSL/crypto/x509v3/internal.h b/Sources/CCryptoBoringSSL/crypto/x509v3/internal.h index fb213b18..479245ea 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509v3/internal.h +++ b/Sources/CCryptoBoringSSL/crypto/x509v3/internal.h @@ -70,21 +70,21 @@ extern "C" { #endif -// x509v3_bytes_to_hex encodes |len| bytes from |buffer| to hex and returns a +// x509v3_bytes_to_hex encodes |len| bytes from |in| to hex and returns a // newly-allocated NUL-terminated string containing the result, or NULL on // allocation error. // -// Note this function was historically named |hex_to_string| in OpenSSL, not -// |string_to_hex|. -char *x509v3_bytes_to_hex(const unsigned char *buffer, long len); +// This function was historically named |hex_to_string| in OpenSSL. Despite the +// name, |hex_to_string| converted to hex. +OPENSSL_EXPORT char *x509v3_bytes_to_hex(const uint8_t *in, size_t len); // x509v3_hex_string_to_bytes decodes |str| in hex and returns a newly-allocated // array containing the result, or NULL on error. On success, it sets |*len| to // the length of the result. Colon separators between bytes in the input are // allowed and ignored. // -// Note this function was historically named |string_to_hex| in OpenSSL, not -// |hex_to_string|. +// This function was historically named |string_to_hex| in OpenSSL. Despite the +// name, |string_to_hex| converted from hex. unsigned char *x509v3_hex_to_bytes(const char *str, long *len); // x509v3_name_cmp returns zero if |name| is equal to |cmp| or begins with |cmp| diff --git a/Sources/CCryptoBoringSSL/crypto/x509v3/v3_akey.c b/Sources/CCryptoBoringSSL/crypto/x509v3/v3_akey.c index 27355319..b8222190 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509v3/v3_akey.c +++ b/Sources/CCryptoBoringSSL/crypto/x509v3/v3_akey.c @@ -93,10 +93,10 @@ static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, STACK_OF(CONF_VALUE) *extlist) { - char *tmp = NULL; int extlist_was_null = extlist == NULL; if (akeyid->keyid) { - tmp = x509v3_bytes_to_hex(akeyid->keyid->data, akeyid->keyid->length); + char *tmp = x509v3_bytes_to_hex(akeyid->keyid->data, + akeyid->keyid->length); int ok = tmp != NULL && X509V3_add_value("keyid", tmp, &extlist); OPENSSL_free(tmp); if (!ok) { @@ -112,10 +112,7 @@ static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, extlist = tmpextlist; } if (akeyid->serial) { - tmp = x509v3_bytes_to_hex(akeyid->serial->data, akeyid->serial->length); - int ok = tmp != NULL && X509V3_add_value("serial", tmp, &extlist); - OPENSSL_free(tmp); - if (!ok) { + if (!X509V3_add_value_int("serial", akeyid->serial, &extlist)) { goto err; } } diff --git a/Sources/CCryptoBoringSSL/crypto/x509v3/v3_utl.c b/Sources/CCryptoBoringSSL/crypto/x509v3/v3_utl.c index 9b4041e7..4326148f 100644 --- a/Sources/CCryptoBoringSSL/crypto/x509v3/v3_utl.c +++ b/Sources/CCryptoBoringSSL/crypto/x509v3/v3_utl.c @@ -63,6 +63,7 @@ #include #include +#include #include #include #include @@ -467,33 +468,33 @@ static char *strip_spaces(char *name) /* hex string utilities */ -/* - * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its - * hex representation @@@ (Contents of buffer are always kept in ASCII, also - * on EBCDIC machines) - */ - -char *x509v3_bytes_to_hex(const unsigned char *buffer, long len) +char *x509v3_bytes_to_hex(const uint8_t *in, size_t len) { - char *tmp, *q; - const unsigned char *p; - int i; - static const char hexdig[] = "0123456789ABCDEF"; - if (!buffer || !len) - return NULL; - if (!(tmp = OPENSSL_malloc(len * 3 + 1))) { - OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); - return NULL; + CBB cbb; + if (!CBB_init(&cbb, len * 3 + 1)) { + goto err; } - q = tmp; - for (i = 0, p = buffer; i < len; i++, p++) { - *q++ = hexdig[(*p >> 4) & 0xf]; - *q++ = hexdig[*p & 0xf]; - *q++ = ':'; + for (size_t i = 0; i < len; i++) { + static const char hex[] = "0123456789ABCDEF"; + if ((i > 0 && !CBB_add_u8(&cbb, ':')) || + !CBB_add_u8(&cbb, hex[in[i] >> 4]) || + !CBB_add_u8(&cbb, hex[in[i] & 0xf])) { + goto err; + } + } + uint8_t *ret; + size_t unused_len; + if (!CBB_add_u8(&cbb, 0) || + !CBB_finish(&cbb, &ret, &unused_len)) { + goto err; } - q[-1] = 0; - return tmp; + return (char *)ret; + +err: + OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); + CBB_cleanup(&cbb); + return NULL; } unsigned char *x509v3_hex_to_bytes(const char *str, long *len) diff --git a/Sources/CCryptoBoringSSL/hash.txt b/Sources/CCryptoBoringSSL/hash.txt index 0afbd983..d62e9053 100644 --- a/Sources/CCryptoBoringSSL/hash.txt +++ b/Sources/CCryptoBoringSSL/hash.txt @@ -1 +1 @@ -This directory is derived from BoringSSL cloned from https://boringssl.googlesource.com/boringssl at revision 295b31324f8c557dcd3c1c831857e33a7f23bc52 +This directory is derived from BoringSSL cloned from https://boringssl.googlesource.com/boringssl at revision 8bbefbfeee609b17622deedd100163c12f5c95dc diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h index 8e4a86a7..7215f62e 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h @@ -50,58 +50,15 @@ * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). */ -#if __arm__ || __arm64__ || __aarch64__ #ifndef OPENSSL_HEADER_ARM_ARCH_H #define OPENSSL_HEADER_ARM_ARCH_H -#if !defined(__ARM_ARCH__) -# if defined(__CC_ARM) -# define __ARM_ARCH__ __TARGET_ARCH_ARM -# if defined(__BIG_ENDIAN) -# define __ARMEB__ -# else -# define __ARMEL__ -# endif -# elif defined(__GNUC__) -# if defined(__aarch64__) -# define __ARM_ARCH__ 8 -# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -# define __ARMEB__ -# else -# define __ARMEL__ -# endif - // Why doesn't gcc define __ARM_ARCH__? Instead it defines - // bunch of below macros. See all_architectires[] table in - // gcc/config/arm/arm.c. On a side note it defines - // __ARMEL__/__ARMEB__ for little-/big-endian. -# elif defined(__ARM_ARCH) -# define __ARM_ARCH__ __ARM_ARCH -# elif defined(__ARM_ARCH_8A__) -# define __ARM_ARCH__ 8 -# elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \ - defined(__ARM_ARCH_7R__)|| defined(__ARM_ARCH_7M__) || \ - defined(__ARM_ARCH_7EM__) -# define __ARM_ARCH__ 7 -# elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \ - defined(__ARM_ARCH_6K__)|| defined(__ARM_ARCH_6M__) || \ - defined(__ARM_ARCH_6Z__)|| defined(__ARM_ARCH_6ZK__) || \ - defined(__ARM_ARCH_6T2__) -# define __ARM_ARCH__ 6 -# elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || \ - defined(__ARM_ARCH_5E__)|| defined(__ARM_ARCH_5TE__) || \ - defined(__ARM_ARCH_5TEJ__) -# define __ARM_ARCH__ 5 -# elif defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) -# define __ARM_ARCH__ 4 -# else -# error "unsupported ARM architecture" -# endif -# endif -#endif +// arm_arch.h contains symbols used by ARM assembly, and the C code that calls +// it. It is included as a public header to simplify the build, but is not +// intended for external use. -// Even when building for 32-bit ARM, support for aarch64 crypto instructions -// will be included. -#define __ARM_MAX_ARCH__ 8 +#if defined(__ARMEL__) || defined(_M_ARM) || defined(__AARCH64EL__) || \ + defined(_M_ARM64) // ARMV7_NEON is true when a NEON unit is present in the current CPU. #define ARMV7_NEON (1 << 0) @@ -123,6 +80,23 @@ #if defined(__ASSEMBLER__) +// We require the ARM assembler provide |__ARM_ARCH| from Arm C Language +// Extensions (ACLE). This is supported in GCC 4.8+ and Clang 3.2+. MSVC does +// not implement ACLE, but we require Clang's assembler on Windows. +#if !defined(__ARM_ARCH) +#error "ARM assembler must define __ARM_ARCH" +#endif + +// __ARM_ARCH__ is used by OpenSSL assembly to determine the minimum target ARM +// version. +// +// TODO(davidben): Switch the assembly to use |__ARM_ARCH| directly. +#define __ARM_ARCH__ __ARM_ARCH + +// Even when building for 32-bit ARM, support for aarch64 crypto instructions +// will be included. +#define __ARM_MAX_ARCH__ 8 + // Support macros for // - Armv8.3-A Pointer Authentication and // - Armv8.5-A Branch Target Identification @@ -239,7 +213,8 @@ .popsection; #endif -#endif /* defined __ASSEMBLER__ */ +#endif // __ASSEMBLER__ + +#endif // __ARMEL__ || _M_ARM || __AARCH64EL__ || _M_ARM64 #endif // OPENSSL_HEADER_ARM_ARCH_H -#endif // __arm__ || __arm64__ || __aarch64__ diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h index 8e595071..bf45ebca 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h @@ -605,7 +605,9 @@ OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str); OPENSSL_EXPORT int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b); // ASN1_STRING_set sets the contents of |str| to a copy of |len| bytes from -// |data|. It returns one on success and zero on error. +// |data|. It returns one on success and zero on error. If |data| is NULL, it +// updates the length and allocates the buffer as needed, but does not +// initialize the contents. OPENSSL_EXPORT int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len); // ASN1_STRING_set0 sets the contents of |str| to |len| bytes from |data|. It @@ -1014,6 +1016,12 @@ OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str, // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, while negative values have a type of // |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. Note this differs from DER's // two's complement representation. +// +// The data in the |ASN1_STRING| may not have leading zeros. Note this means +// zero is represented as the empty string. Parsing functions will never return +// invalid representations. If an invalid input is constructed, the marshaling +// functions will skip leading zeros, however other functions, such as +// |ASN1_INTEGER_cmp| or |ASN1_INTEGER_get|, may not return the correct result. DEFINE_STACK_OF(ASN1_INTEGER) @@ -1068,16 +1076,25 @@ OPENSSL_EXPORT int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); // |ASN1_INTEGER*|. DECLARE_ASN1_ITEM(ASN1_INTEGER) -// ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on -// success and zero on error. -OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); - // ASN1_INTEGER_set_uint64 sets |a| to an INTEGER with value |v|. It returns one // on success and zero on error. OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v); +// ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on +// success and zero on error. +OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); + +// ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns +// one and sets |*out| to the result. If |a| did not fit or has the wrong type, +// it returns zero. +OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out, + const ASN1_INTEGER *a); + // ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of // range or the wrong type. +// +// WARNING: This function's return value cannot distinguish errors from -1. +// Prefer |ASN1_INTEGER_get_uint64|. OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a); // BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai| @@ -1123,18 +1140,31 @@ OPENSSL_EXPORT int i2d_ASN1_ENUMERATED(const ASN1_ENUMERATED *in, // |ASN1_ENUMERATED*|. DECLARE_ASN1_ITEM(ASN1_ENUMERATED) +// ASN1_ENUMERATED_set_uint64 sets |a| to an ENUMERATED with value |v|. It +// returns one on success and zero on error. +OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v); + // ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one // on success and zero on error. OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); +// ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it +// returns one and sets |*out| to the result. If |a| did not fit or has the +// wrong type, it returns zero. +OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out, + const ASN1_ENUMERATED *a); + // ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out // of range or the wrong type. +// +// WARNING: This function's return value cannot distinguish errors from -1. +// Prefer |ASN1_ENUMERATED_get_uint64|. OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); // BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns // |ai| on success or NULL or error. If |ai| is NULL, it returns a -// newly-allocated |ASN1_INTEGER| on success instead, which the caller must -// release with |ASN1_INTEGER_free|. +// newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must +// release with |ASN1_ENUMERATED_free|. OPENSSL_EXPORT ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai); @@ -2035,5 +2065,7 @@ BSSL_NAMESPACE_END #define ASN1_R_NESTED_TOO_DEEP 192 #define ASN1_R_BAD_TEMPLATE 193 #define ASN1_R_INVALID_BIT_STRING_PADDING 194 +#define ASN1_R_WRONG_INTEGER_TYPE 195 +#define ASN1_R_INVALID_INTEGER 196 #endif diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1t.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1t.h index d29c19e7..2b8491fd 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1t.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1t.h @@ -260,7 +260,6 @@ typedef struct ASN1_TLC_st ASN1_TLC; /* Any defined by macros: the field used is in the table itself */ #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } -#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } /* Plain simple type */ #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type) @@ -377,7 +376,7 @@ struct ASN1_ADB_st { }; struct ASN1_ADB_TABLE_st { - long value; /* NID for an object or value for an int */ + int value; /* NID for an object */ const ASN1_TEMPLATE tt; /* item for this value */ }; @@ -442,8 +441,6 @@ struct ASN1_ADB_TABLE_st { #define ASN1_TFLG_ADB_OID (0x1<<8) -#define ASN1_TFLG_ADB_INT (0x1<<9) - /* This flag means a parent structure is passed * instead of the field: this is useful is a * SEQUENCE is being combined with a CHOICE for diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_base.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_base.h index 5dfc13be..eaa2b768 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_base.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_base.h @@ -203,7 +203,7 @@ extern "C" { // A consumer may use this symbol in the preprocessor to temporarily build // against multiple revisions of BoringSSL at the same time. It is not // recommended to do so for longer than is necessary. -#define BORINGSSL_API_VERSION 16 +#define BORINGSSL_API_VERSION 17 #if defined(BORINGSSL_SHARED_LIBRARY) @@ -456,7 +456,6 @@ typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER; typedef struct trust_token_method_st TRUST_TOKEN_METHOD; typedef struct v3_ext_ctx X509V3_CTX; typedef struct x509_attributes_st X509_ATTRIBUTE; -typedef struct x509_cert_aux_st X509_CERT_AUX; typedef struct x509_crl_method_st X509_CRL_METHOD; typedef struct x509_lookup_st X509_LOOKUP; typedef struct x509_lookup_method_st X509_LOOKUP_METHOD; diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h index 44126778..8015e6df 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h @@ -126,6 +126,7 @@ #include "CCryptoBoringSSL_base.h" #include "CCryptoBoringSSL_thread.h" +#include // for PRIu64 and friends #include #include // for FILE* @@ -148,14 +149,14 @@ extern "C" { // Projects which use |BN_*_FMT*| with outdated C headers may need to define it // externally. #if defined(OPENSSL_64_BIT) -#define BN_ULONG uint64_t +typedef uint64_t BN_ULONG; #define BN_BITS2 64 #define BN_DEC_FMT1 "%" PRIu64 #define BN_DEC_FMT2 "%019" PRIu64 #define BN_HEX_FMT1 "%" PRIx64 #define BN_HEX_FMT2 "%016" PRIx64 #elif defined(OPENSSL_32_BIT) -#define BN_ULONG uint32_t +typedef uint32_t BN_ULONG; #define BN_BITS2 32 #define BN_DEC_FMT1 "%" PRIu32 #define BN_DEC_FMT2 "%09" PRIu32 @@ -584,9 +585,14 @@ OPENSSL_EXPORT int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m); // BN_mod_sqrt returns a newly-allocated |BIGNUM|, r, such that -// r^2 == a (mod p). |p| must be a prime. It returns NULL on error or if |a| is -// not a square mod |p|. In the latter case, it will add |BN_R_NOT_A_SQUARE| to -// the error queue. +// r^2 == a (mod p). It returns NULL on error or if |a| is not a square mod |p|. +// In the latter case, it will add |BN_R_NOT_A_SQUARE| to the error queue. +// If |a| is a square and |p| > 2, there are two possible square roots. This +// function may return either and may even select one non-deterministically. +// +// This function only works if |p| is a prime. If |p| is composite, it may fail +// or return an arbitrary value. Callers should not pass attacker-controlled +// values of |p|. OPENSSL_EXPORT BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols.h index 42085f1b..976cdf08 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols.h @@ -49,9 +49,11 @@ #define ASN1_BOOLEAN_it BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_BOOLEAN_it) #define ASN1_ENUMERATED_free BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_free) #define ASN1_ENUMERATED_get BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_get) +#define ASN1_ENUMERATED_get_uint64 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_get_uint64) #define ASN1_ENUMERATED_it BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_it) #define ASN1_ENUMERATED_new BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_new) #define ASN1_ENUMERATED_set BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_set) +#define ASN1_ENUMERATED_set_uint64 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_set_uint64) #define ASN1_ENUMERATED_to_BN BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_ENUMERATED_to_BN) #define ASN1_FBOOLEAN_it BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_FBOOLEAN_it) #define ASN1_GENERALIZEDTIME_adj BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_GENERALIZEDTIME_adj) @@ -72,6 +74,7 @@ #define ASN1_INTEGER_dup BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_dup) #define ASN1_INTEGER_free BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_free) #define ASN1_INTEGER_get BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_get) +#define ASN1_INTEGER_get_uint64 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_get_uint64) #define ASN1_INTEGER_it BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_it) #define ASN1_INTEGER_new BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_new) #define ASN1_INTEGER_set BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ASN1_INTEGER_set) @@ -994,6 +997,7 @@ #define EVP_HPKE_KEY_public_key BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_HPKE_KEY_public_key) #define EVP_HPKE_KEY_zero BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_HPKE_KEY_zero) #define EVP_MD_CTX_block_size BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_block_size) +#define EVP_MD_CTX_cleanse BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_cleanse) #define EVP_MD_CTX_cleanup BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_cleanup) #define EVP_MD_CTX_copy BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_copy) #define EVP_MD_CTX_copy_ex BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_copy_ex) @@ -1196,7 +1200,9 @@ #define EXTENDED_KEY_USAGE_new BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EXTENDED_KEY_USAGE_new) #define FIPS_mode BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, FIPS_mode) #define FIPS_mode_set BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, FIPS_mode_set) +#define FIPS_query_algorithm_status BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, FIPS_query_algorithm_status) #define FIPS_read_counter BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, FIPS_read_counter) +#define FIPS_version BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, FIPS_version) #define GENERAL_NAMES_free BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, GENERAL_NAMES_free) #define GENERAL_NAMES_it BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, GENERAL_NAMES_it) #define GENERAL_NAMES_new BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, GENERAL_NAMES_new) @@ -1217,6 +1223,7 @@ #define HKDF_expand BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HKDF_expand) #define HKDF_extract BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HKDF_extract) #define HMAC BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HMAC) +#define HMAC_CTX_cleanse BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HMAC_CTX_cleanse) #define HMAC_CTX_cleanup BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HMAC_CTX_cleanup) #define HMAC_CTX_copy BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HMAC_CTX_copy) #define HMAC_CTX_copy_ex BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, HMAC_CTX_copy_ex) @@ -2317,7 +2324,9 @@ #define bn_uadd_consttime BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_uadd_consttime) #define bn_usub_consttime BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_usub_consttime) #define bn_wexpand BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_wexpand) -#define boringssl_fips_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, boringssl_fips_self_test) +#define boringssl_self_test_hmac_sha256 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, boringssl_self_test_hmac_sha256) +#define boringssl_self_test_sha256 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, boringssl_self_test_sha256) +#define boringssl_self_test_sha512 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, boringssl_self_test_sha512) #define c2i_ASN1_BIT_STRING BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_BIT_STRING) #define c2i_ASN1_INTEGER BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_INTEGER) #define c2i_ASN1_OBJECT BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_OBJECT) @@ -2453,6 +2462,7 @@ #define d2i_X509_VAL BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, d2i_X509_VAL) #define d2i_X509_bio BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, d2i_X509_bio) #define d2i_X509_fp BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, d2i_X509_fp) +#define dh_compute_key_padded_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, dh_compute_key_padded_no_self_test) #define dsa_asn1_meth BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, dsa_asn1_meth) #define dsa_check_parameters BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, dsa_check_parameters) #define ec_GFp_mont_add BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_GFp_mont_add) @@ -2512,6 +2522,7 @@ #define ec_jacobian_to_affine_batch BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_jacobian_to_affine_batch) #define ec_pkey_meth BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_pkey_meth) #define ec_point_from_uncompressed BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_point_from_uncompressed) +#define ec_point_mul_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_point_mul_no_self_test) #define ec_point_mul_scalar BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_point_mul_scalar) #define ec_point_mul_scalar_base BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_point_mul_scalar_base) #define ec_point_mul_scalar_batch BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_point_mul_scalar_batch) @@ -2540,6 +2551,7 @@ #define ec_set_to_safe_point BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_set_to_safe_point) #define ec_simple_scalar_inv0_montgomery BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_simple_scalar_inv0_montgomery) #define ec_simple_scalar_to_montgomery_inv_vartime BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ec_simple_scalar_to_montgomery_inv_vartime) +#define ecdsa_do_verify_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ecdsa_do_verify_no_self_test) #define ecdsa_sign_with_nonce_for_known_answer_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ecdsa_sign_with_nonce_for_known_answer_test) #define ecp_nistz256_avx2_select_w7 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ecp_nistz256_avx2_select_w7) #define ecp_nistz256_mul_mont BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, ecp_nistz256_mul_mont) @@ -2766,6 +2778,9 @@ #define rsa_default_sign_raw BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_default_sign_raw) #define rsa_default_size BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_default_size) #define rsa_pkey_meth BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_pkey_meth) +#define rsa_sign_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_sign_no_self_test) +#define rsa_verify_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_verify_no_self_test) +#define rsa_verify_raw_no_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsa_verify_raw_no_self_test) #define rsaz_1024_gather5_avx2 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsaz_1024_gather5_avx2) #define rsaz_1024_mul_avx2 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsaz_1024_mul_avx2) #define rsaz_1024_norm2red_avx2 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, rsaz_1024_norm2red_avx2) diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols_asm.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols_asm.h index 05c29747..8f1bbc79 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols_asm.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_boringssl_prefix_symbols_asm.h @@ -54,9 +54,11 @@ #define _ASN1_BOOLEAN_it BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_BOOLEAN_it) #define _ASN1_ENUMERATED_free BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_free) #define _ASN1_ENUMERATED_get BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_get) +#define _ASN1_ENUMERATED_get_uint64 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_get_uint64) #define _ASN1_ENUMERATED_it BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_it) #define _ASN1_ENUMERATED_new BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_new) #define _ASN1_ENUMERATED_set BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_set) +#define _ASN1_ENUMERATED_set_uint64 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_set_uint64) #define _ASN1_ENUMERATED_to_BN BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_ENUMERATED_to_BN) #define _ASN1_FBOOLEAN_it BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_FBOOLEAN_it) #define _ASN1_GENERALIZEDTIME_adj BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_GENERALIZEDTIME_adj) @@ -77,6 +79,7 @@ #define _ASN1_INTEGER_dup BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_dup) #define _ASN1_INTEGER_free BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_free) #define _ASN1_INTEGER_get BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_get) +#define _ASN1_INTEGER_get_uint64 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_get_uint64) #define _ASN1_INTEGER_it BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_it) #define _ASN1_INTEGER_new BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_new) #define _ASN1_INTEGER_set BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ASN1_INTEGER_set) @@ -999,6 +1002,7 @@ #define _EVP_HPKE_KEY_public_key BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_HPKE_KEY_public_key) #define _EVP_HPKE_KEY_zero BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_HPKE_KEY_zero) #define _EVP_MD_CTX_block_size BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_block_size) +#define _EVP_MD_CTX_cleanse BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_cleanse) #define _EVP_MD_CTX_cleanup BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_cleanup) #define _EVP_MD_CTX_copy BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_copy) #define _EVP_MD_CTX_copy_ex BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_copy_ex) @@ -1201,7 +1205,9 @@ #define _EXTENDED_KEY_USAGE_new BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EXTENDED_KEY_USAGE_new) #define _FIPS_mode BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, FIPS_mode) #define _FIPS_mode_set BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, FIPS_mode_set) +#define _FIPS_query_algorithm_status BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, FIPS_query_algorithm_status) #define _FIPS_read_counter BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, FIPS_read_counter) +#define _FIPS_version BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, FIPS_version) #define _GENERAL_NAMES_free BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, GENERAL_NAMES_free) #define _GENERAL_NAMES_it BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, GENERAL_NAMES_it) #define _GENERAL_NAMES_new BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, GENERAL_NAMES_new) @@ -1222,6 +1228,7 @@ #define _HKDF_expand BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HKDF_expand) #define _HKDF_extract BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HKDF_extract) #define _HMAC BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HMAC) +#define _HMAC_CTX_cleanse BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HMAC_CTX_cleanse) #define _HMAC_CTX_cleanup BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HMAC_CTX_cleanup) #define _HMAC_CTX_copy BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HMAC_CTX_copy) #define _HMAC_CTX_copy_ex BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, HMAC_CTX_copy_ex) @@ -2322,7 +2329,9 @@ #define _bn_uadd_consttime BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_uadd_consttime) #define _bn_usub_consttime BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_usub_consttime) #define _bn_wexpand BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_wexpand) -#define _boringssl_fips_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, boringssl_fips_self_test) +#define _boringssl_self_test_hmac_sha256 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, boringssl_self_test_hmac_sha256) +#define _boringssl_self_test_sha256 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, boringssl_self_test_sha256) +#define _boringssl_self_test_sha512 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, boringssl_self_test_sha512) #define _c2i_ASN1_BIT_STRING BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_BIT_STRING) #define _c2i_ASN1_INTEGER BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_INTEGER) #define _c2i_ASN1_OBJECT BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_OBJECT) @@ -2458,6 +2467,7 @@ #define _d2i_X509_VAL BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, d2i_X509_VAL) #define _d2i_X509_bio BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, d2i_X509_bio) #define _d2i_X509_fp BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, d2i_X509_fp) +#define _dh_compute_key_padded_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, dh_compute_key_padded_no_self_test) #define _dsa_asn1_meth BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, dsa_asn1_meth) #define _dsa_check_parameters BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, dsa_check_parameters) #define _ec_GFp_mont_add BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_GFp_mont_add) @@ -2517,6 +2527,7 @@ #define _ec_jacobian_to_affine_batch BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_jacobian_to_affine_batch) #define _ec_pkey_meth BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_pkey_meth) #define _ec_point_from_uncompressed BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_point_from_uncompressed) +#define _ec_point_mul_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_point_mul_no_self_test) #define _ec_point_mul_scalar BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_point_mul_scalar) #define _ec_point_mul_scalar_base BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_point_mul_scalar_base) #define _ec_point_mul_scalar_batch BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_point_mul_scalar_batch) @@ -2545,6 +2556,7 @@ #define _ec_set_to_safe_point BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_set_to_safe_point) #define _ec_simple_scalar_inv0_montgomery BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_simple_scalar_inv0_montgomery) #define _ec_simple_scalar_to_montgomery_inv_vartime BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ec_simple_scalar_to_montgomery_inv_vartime) +#define _ecdsa_do_verify_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ecdsa_do_verify_no_self_test) #define _ecdsa_sign_with_nonce_for_known_answer_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ecdsa_sign_with_nonce_for_known_answer_test) #define _ecp_nistz256_avx2_select_w7 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ecp_nistz256_avx2_select_w7) #define _ecp_nistz256_mul_mont BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, ecp_nistz256_mul_mont) @@ -2771,6 +2783,9 @@ #define _rsa_default_sign_raw BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_default_sign_raw) #define _rsa_default_size BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_default_size) #define _rsa_pkey_meth BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_pkey_meth) +#define _rsa_sign_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_sign_no_self_test) +#define _rsa_verify_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_verify_no_self_test) +#define _rsa_verify_raw_no_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsa_verify_raw_no_self_test) #define _rsaz_1024_gather5_avx2 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsaz_1024_gather5_avx2) #define _rsaz_1024_mul_avx2 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsaz_1024_mul_avx2) #define _rsaz_1024_norm2red_avx2 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, rsaz_1024_norm2red_avx2) diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bytestring.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bytestring.h index 4b3aa212..017577f9 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bytestring.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bytestring.h @@ -259,15 +259,17 @@ OPENSSL_EXPORT int CBS_get_any_asn1_element(CBS *cbs, CBS *out, // CBS_get_any_ber_asn1_element acts the same as |CBS_get_any_asn1_element| but // also allows indefinite-length elements to be returned and does not enforce -// that lengths are minimal. For indefinite-lengths, |*out_header_len| and +// that lengths are minimal. It sets |*out_indefinite| to one if the length was +// indefinite and zero otherwise. If indefinite, |*out_header_len| and // |CBS_len(out)| will be equal as only the header is returned (although this is -// also true for empty elements so the length must be checked too). If +// also true for empty elements so |*out_indefinite| should be checked). If // |out_ber_found| is not NULL then it is set to one if any case of invalid DER // but valid BER is found, and to zero otherwise. OPENSSL_EXPORT int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, size_t *out_header_len, - int *out_ber_found); + int *out_ber_found, + int *out_indefinite); // CBS_get_asn1_uint64 gets an ASN.1 INTEGER from |cbs| using |CBS_get_asn1| // and sets |*out| to its value. It returns one on success and zero on error, diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_cpu.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_cpu.h index 6090a248..d4226e02 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_cpu.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_cpu.h @@ -1,203 +1,18 @@ -/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) - * All rights reserved. +/* Copyright (c) 2014, Google Inc. * - * This package is an SSL implementation written - * by Eric Young (eay@cryptsoft.com). - * The implementation was written so as to conform with Netscapes SSL. + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * This library is free for commercial and non-commercial use as long as - * the following conditions are aheared to. The following conditions - * apply to all code found in this distribution, be it the RC4, RSA, - * lhash, DES, etc., code; not just the SSL code. The SSL documentation - * included with this distribution is covered by the same copyright terms - * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * - * Copyright remains Eric Young's, and as such any Copyright notices in - * the code are not to be removed. - * If this package is used in a product, Eric Young should be given attribution - * as the author of the parts of the library used. - * This can be in the form of a textual message at program startup or - * in documentation (online or textual) provided with the package. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * "This product includes cryptographic software written by - * Eric Young (eay@cryptsoft.com)" - * The word 'cryptographic' can be left out if the rouines from the library - * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from - * the apps directory (application code) you must include an acknowledgement: - * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * - * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * The licence and distribution terms for any publically available version or - * derivative of this code cannot be changed. i.e. this code cannot simply be - * copied and put under another distribution licence - * [including the GNU Public Licence.] - * - * This product includes cryptographic software written by Eric Young - * (eay@cryptsoft.com). This product includes software written by Tim - * Hudson (tjh@cryptsoft.com). */ + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef OPENSSL_HEADER_CPU_H -#define OPENSSL_HEADER_CPU_H +// This header is provided for compatibility with older revisions of BoringSSL. +// TODO(davidben): Remove this header. -#include "CCryptoBoringSSL_base.h" - -#if defined(__cplusplus) -extern "C" { -#endif - - -// Runtime CPU feature support - - -#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) -// OPENSSL_ia32cap_P contains the Intel CPUID bits when running on an x86 or -// x86-64 system. -// -// Index 0: -// EDX for CPUID where EAX = 1 -// Bit 20 is always zero -// Bit 28 is adjusted to reflect whether the data cache is shared between -// multiple logical cores -// Bit 30 is used to indicate an Intel CPU -// Index 1: -// ECX for CPUID where EAX = 1 -// Bit 11 is used to indicate AMD XOP support, not SDBG -// Index 2: -// EBX for CPUID where EAX = 7 -// Index 3: -// ECX for CPUID where EAX = 7 -// -// Note: the CPUID bits are pre-adjusted for the OSXSAVE bit and the YMM and XMM -// bits in XCR0, so it is not necessary to check those. -extern uint32_t OPENSSL_ia32cap_P[4]; - -#if defined(BORINGSSL_FIPS) && !defined(BORINGSSL_SHARED_LIBRARY) -const uint32_t *OPENSSL_ia32cap_get(void); -#else -OPENSSL_INLINE const uint32_t *OPENSSL_ia32cap_get(void) { - return OPENSSL_ia32cap_P; -} -#endif - -#endif - -#if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) - -#if defined(OPENSSL_APPLE) && defined(OPENSSL_ARM) -// We do not detect any features at runtime for Apple's 32-bit ARM platforms. On -// 64-bit ARM, we detect some post-ARMv8.0 features. -#define OPENSSL_STATIC_ARMCAP -#endif - -#if !defined(OPENSSL_STATIC_ARMCAP) -// CRYPTO_is_NEON_capable_at_runtime returns true if the current CPU has a NEON -// unit. Note that |OPENSSL_armcap_P| also exists and contains the same -// information in a form that's easier for assembly to use. -OPENSSL_EXPORT int CRYPTO_is_NEON_capable_at_runtime(void); - -// CRYPTO_is_ARMv8_AES_capable_at_runtime returns true if the current CPU -// supports the ARMv8 AES instruction. -int CRYPTO_is_ARMv8_AES_capable_at_runtime(void); - -// CRYPTO_is_ARMv8_PMULL_capable_at_runtime returns true if the current CPU -// supports the ARMv8 PMULL instruction. -int CRYPTO_is_ARMv8_PMULL_capable_at_runtime(void); - -#if defined(OPENSSL_ARM) -// CRYPTO_has_broken_NEON returns one if the current CPU is known to have a -// broken NEON unit. See https://crbug.com/341598. -OPENSSL_EXPORT int CRYPTO_has_broken_NEON(void); - -// CRYPTO_needs_hwcap2_workaround returns one if the ARMv8 AArch32 AT_HWCAP2 -// workaround was needed. See https://crbug.com/boringssl/46. -OPENSSL_EXPORT int CRYPTO_needs_hwcap2_workaround(void); -#endif -#endif // !OPENSSL_STATIC_ARMCAP - -// CRYPTO_is_NEON_capable returns true if the current CPU has a NEON unit. If -// this is known statically, it is a constant inline function. -OPENSSL_INLINE int CRYPTO_is_NEON_capable(void) { -#if defined(__ARM_NEON__) || defined(__ARM_NEON) || \ - defined(OPENSSL_STATIC_ARMCAP_NEON) - return 1; -#elif defined(OPENSSL_STATIC_ARMCAP) - return 0; -#else - return CRYPTO_is_NEON_capable_at_runtime(); -#endif -} - -OPENSSL_INLINE int CRYPTO_is_ARMv8_AES_capable(void) { -#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO) - return 1; -#elif defined(OPENSSL_STATIC_ARMCAP) - return 0; -#else - return CRYPTO_is_ARMv8_AES_capable_at_runtime(); -#endif -} - -OPENSSL_INLINE int CRYPTO_is_ARMv8_PMULL_capable(void) { -#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO) - return 1; -#elif defined(OPENSSL_STATIC_ARMCAP) - return 0; -#else - return CRYPTO_is_ARMv8_PMULL_capable_at_runtime(); -#endif -} - -#endif // OPENSSL_ARM || OPENSSL_AARCH64 - -#if defined(OPENSSL_PPC64LE) - -// CRYPTO_is_PPC64LE_vcrypto_capable returns true iff the current CPU supports -// the Vector.AES category of instructions. -int CRYPTO_is_PPC64LE_vcrypto_capable(void); - -extern unsigned long OPENSSL_ppc64le_hwcap2; - -#endif // OPENSSL_PPC64LE - -#if defined(BORINGSSL_DISPATCH_TEST) -// Runtime CPU dispatch testing support - -// BORINGSSL_function_hit is an array of flags. The following functions will -// set these flags if BORINGSSL_DISPATCH_TEST is defined. -// 0: aes_hw_ctr32_encrypt_blocks -// 1: aes_hw_encrypt -// 2: aesni_gcm_encrypt -// 3: aes_hw_set_encrypt_key -// 4: vpaes_encrypt -// 5: vpaes_set_encrypt_key -extern uint8_t BORINGSSL_function_hit[7]; -#endif // BORINGSSL_DISPATCH_TEST - - -#if defined(__cplusplus) -} // extern C -#endif - -#endif // OPENSSL_HEADER_CPU_H +#include "CCryptoBoringSSL_crypto.h" diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_crypto.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_crypto.h index feb0a012..d4f92f8b 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_crypto.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_crypto.h @@ -59,6 +59,12 @@ OPENSSL_EXPORT int CRYPTO_has_asm(void); // success and zero on error. OPENSSL_EXPORT int BORINGSSL_self_test(void); +// BORINGSSL_integrity_test triggers the module's integrity test where the code +// and data of the module is matched against a hash injected at build time. It +// returns one on success or zero if there's a mismatch. This function only +// exists if the module was built in FIPS mode without ASAN. +OPENSSL_EXPORT int BORINGSSL_integrity_test(void); + // CRYPTO_pre_sandbox_init initializes the crypto library, pre-acquiring some // unusual resources to aid running in sandboxed environments. It is safe to // call this function multiple times and concurrently from multiple threads. @@ -67,6 +73,17 @@ OPENSSL_EXPORT int BORINGSSL_self_test(void); // SANDBOXING.md in the source tree. OPENSSL_EXPORT void CRYPTO_pre_sandbox_init(void); +#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \ + !defined(OPENSSL_STATIC_ARMCAP) +// CRYPTO_has_broken_NEON returns one if the current CPU is known to have a +// broken NEON unit. See https://crbug.com/341598. +OPENSSL_EXPORT int CRYPTO_has_broken_NEON(void); + +// CRYPTO_needs_hwcap2_workaround returns one if the ARMv8 AArch32 AT_HWCAP2 +// workaround was needed. See https://crbug.com/boringssl/46. +OPENSSL_EXPORT int CRYPTO_needs_hwcap2_workaround(void); +#endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP + // FIPS monitoring @@ -161,6 +178,18 @@ OPENSSL_EXPORT void OPENSSL_cleanup(void); // |BORINGSSL_FIPS| and zero otherwise. OPENSSL_EXPORT int FIPS_mode_set(int on); +// FIPS_version returns the version of the FIPS module, or zero if the build +// isn't exactly at a verified version. The version, expressed in base 10, will +// be a date in the form yyyymmddXX where XX is often "00", but can be +// incremented if multiple versions are defined on a single day. +// +// (This format exceeds a |uint32_t| in the year 4294.) +OPENSSL_EXPORT uint32_t FIPS_version(void); + +// FIPS_query_algorithm_status returns one if |algorithm| is FIPS validated in +// the current BoringSSL and zero otherwise. +OPENSSL_EXPORT int FIPS_query_algorithm_status(const char *algorithm); + #if defined(__cplusplus) } // extern C diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_digest.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_digest.h index d10468be..99b50d85 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_digest.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_digest.h @@ -117,6 +117,13 @@ OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void); // freshly initialised state. It does not free |ctx| itself. It returns one. OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx); +// EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the +// actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain +// more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but +// this function does not zero out more than just the digest state even in that +// case. +OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx); + // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself. OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx); diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec.h index 5f33e87b..ed9adca7 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec.h @@ -323,7 +323,15 @@ OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, // |EC_GROUP_cmp| (even to themselves). |EC_GROUP_get_curve_name| will always // return |NID_undef|. // -// Avoid using arbitrary curves and use |EC_GROUP_new_by_curve_name| instead. +// This function is provided for compatibility with some legacy applications +// only. Avoid using arbitrary curves and use |EC_GROUP_new_by_curve_name| +// instead. This ensures the result meets preconditions necessary for +// elliptic curve algorithms to function correctly and securely. +// +// Given invalid parameters, this function may fail or it may return an +// |EC_GROUP| which breaks these preconditions. Subsequent operations may then +// return arbitrary, incorrect values. Callers should not pass +// attacker-controlled values to this function. OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec_key.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec_key.h index 766ea475..be79b189 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec_key.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_ec_key.h @@ -167,8 +167,9 @@ OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key, // about the problem can be found on the error stack. OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key); -// EC_KEY_check_fips performs a signing pairwise consistency test (FIPS 140-2 -// 4.9.2). It returns one if it passes and zero otherwise. +// EC_KEY_check_fips performs both a signing pairwise consistency test +// (FIPS 140-2 4.9.2) and the consistency test from SP 800-56Ar3 section +// 5.6.2.1.4. It returns one if it passes and zero otherwise. OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key); // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to @@ -194,7 +195,9 @@ OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key, OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key); // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs -// additional checks for FIPS compliance. +// additional checks for FIPS compliance. This function is applicable when +// generating keys for either signing/verification or key agreement because +// both types of consistency check (PCT) are performed. OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key); // EC_KEY_derive_from_secret deterministically derives a private key for |group| diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hmac.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hmac.h index f84dc901..77f0dbb2 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hmac.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hmac.h @@ -98,6 +98,10 @@ OPENSSL_EXPORT HMAC_CTX *HMAC_CTX_new(void); // HMAC_CTX_cleanup frees data owned by |ctx|. It does not free |ctx| itself. OPENSSL_EXPORT void HMAC_CTX_cleanup(HMAC_CTX *ctx); +// HMAC_CTX_cleanse zeros the digest state from |ctx| and then performs the +// actions of |HMAC_CTX_cleanup|. +OPENSSL_EXPORT void HMAC_CTX_cleanse(HMAC_CTX *ctx); + // HMAC_CTX_free calls |HMAC_CTX_cleanup| and then frees |ctx| itself. OPENSSL_EXPORT void HMAC_CTX_free(HMAC_CTX *ctx); diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hpke.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hpke.h index 1b973b5b..928abc94 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hpke.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_hpke.h @@ -30,7 +30,7 @@ extern "C" { // Hybrid Public Key Encryption (HPKE) enables a sender to encrypt messages to a // receiver with a public key. // -// See https://tools.ietf.org/html/draft-irtf-cfrg-hpke-12. +// See RFC 9180. // Parameters. diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_thread.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_thread.h index 45eac242..b17a8c32 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_thread.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_thread.h @@ -77,14 +77,13 @@ typedef struct crypto_mutex_st { typedef union crypto_mutex_st { void *handle; } CRYPTO_MUTEX; -#elif defined(__MACH__) && defined(__APPLE__) +#elif !defined(__GLIBC__) typedef pthread_rwlock_t CRYPTO_MUTEX; #else -// It is reasonable to include pthread.h on non-Windows systems, however the -// |pthread_rwlock_t| that we need is hidden under feature flags, and we can't -// ensure that we'll be able to get it. It's statically asserted that this -// structure is large enough to contain a |pthread_rwlock_t| by -// thread_pthread.c. +// On glibc, |pthread_rwlock_t| is hidden under feature flags, and we can't +// ensure that we'll be able to get it from a public header. It's statically +// asserted that this structure is large enough to contain a |pthread_rwlock_t| +// by thread_pthread.c. typedef union crypto_mutex_st { double alignment; uint8_t padding[3*sizeof(int) + 5*sizeof(unsigned) + 16 + 8]; diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_x509.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_x509.h index 3353ee27..73cb04d8 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_x509.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_x509.h @@ -858,7 +858,6 @@ DECLARE_ASN1_FUNCTIONS(X509_NAME) OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, X509_NAME *name); DECLARE_ASN1_FUNCTIONS(X509) -DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX) // X509_up_ref adds one to the reference count of |x509| and returns one. OPENSSL_EXPORT int X509_up_ref(X509 *x509); @@ -869,9 +868,6 @@ OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_free *free_func); OPENSSL_EXPORT int X509_set_ex_data(X509 *r, int idx, void *arg); OPENSSL_EXPORT void *X509_get_ex_data(X509 *r, int idx); -OPENSSL_EXPORT int i2d_X509_AUX(X509 *a, unsigned char **pp); -OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, - long length); // i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described // in |i2d_SAMPLE|. @@ -924,19 +920,84 @@ OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig, // a known NID. OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509); -OPENSSL_EXPORT int X509_alias_set1(X509 *x, const unsigned char *name, int len); -OPENSSL_EXPORT int X509_keyid_set1(X509 *x, const unsigned char *id, int len); -OPENSSL_EXPORT unsigned char *X509_alias_get0(X509 *x, int *len); -OPENSSL_EXPORT unsigned char *X509_keyid_get0(X509 *x, int *len); -OPENSSL_EXPORT int (*X509_TRUST_set_default(int (*trust)(int, X509 *, - int)))(int, X509 *, - int); -OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust); + +// Auxiliary properties. +// +// |X509| objects optionally maintain auxiliary properties. These are not part +// of the certificates themselves, and thus are not covered by signatures or +// preserved by the standard serialization. They are used as inputs or outputs +// to other functions in this library. + +// i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), +// followed optionally by a separate, OpenSSL-specific structure with auxiliary +// properties. It behaves as described in |i2d_SAMPLE|. +// +// Unlike similarly-named functions, this function does not output a single +// ASN.1 element. Directly embedding the output in a larger ASN.1 structure will +// not behave correctly. +OPENSSL_EXPORT int i2d_X509_AUX(X509 *x509, unsigned char **outp); + +// d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509 +// Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific +// structure with auxiliary properties. It behaves as described in +// |d2i_SAMPLE_with_reuse|. +// +// Some auxiliary properties affect trust decisions, so this function should not +// be used with untrusted input. +// +// Unlike similarly-named functions, this function does not parse a single +// ASN.1 element. Trying to parse data directly embedded in a larger ASN.1 +// structure will not behave correctly. +OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const unsigned char **inp, + long length); + +// X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is +// NULL, the alias is cleared instead. Aliases are not part of the certificate +// itself and will not be serialized by |i2d_X509|. +OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const unsigned char *name, + int len); + +// X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is +// NULL, the key ID is cleared instead. Key IDs are not part of the certificate +// itself and will not be serialized by |i2d_X509|. +OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const unsigned char *id, + int len); + +// X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the +// alias's length and returns a pointer to a buffer containing the contents. If +// not found, it outputs the empty string by returning NULL and setting +// |*out_len| to zero. +// +// If |x509| was parsed from a PKCS#12 structure (see +// |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName +// attribute (RFC 2985). +// +// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was +// missing. Callers that target both OpenSSL and BoringSSL should set the value +// to zero before calling this function. +OPENSSL_EXPORT unsigned char *X509_alias_get0(X509 *x509, int *out_len); + +// X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the +// key ID's length and returns a pointer to a buffer containing the contents. If +// not found, it outputs the empty string by returning NULL and setting +// |*out_len| to zero. +// +// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was +// missing. Callers that target both OpenSSL and BoringSSL should set the value +// to zero before calling this function. +OPENSSL_EXPORT unsigned char *X509_keyid_get0(X509 *x509, int *out_len); + OPENSSL_EXPORT int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj); OPENSSL_EXPORT int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj); OPENSSL_EXPORT void X509_trust_clear(X509 *x); OPENSSL_EXPORT void X509_reject_clear(X509 *x); + +OPENSSL_EXPORT int (*X509_TRUST_set_default(int (*trust)(int, X509 *, + int)))(int, X509 *, + int); +OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust); + DECLARE_ASN1_FUNCTIONS(X509_REVOKED) DECLARE_ASN1_FUNCTIONS(X509_CRL) @@ -1300,7 +1361,6 @@ OPENSSL_EXPORT int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag, unsigned long cflag); OPENSSL_EXPORT int X509_print(BIO *bp, X509 *x); OPENSSL_EXPORT int X509_ocspid_print(BIO *bp, X509 *x); -OPENSSL_EXPORT int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent); OPENSSL_EXPORT int X509_CRL_print(BIO *bp, X509_CRL *x); OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag, unsigned long cflag); @@ -2404,5 +2464,8 @@ BSSL_NAMESPACE_END #define X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER 138 #define X509_R_INVALID_FIELD_FOR_VERSION 139 #define X509_R_INVALID_VERSION 140 +#define X509_R_NO_CERTIFICATE_FOUND 141 +#define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142 +#define X509_R_NO_CRL_FOUND 143 #endif diff --git a/Sources/CCryptoBoringSSL/include/boringssl_prefix_symbols_nasm.inc b/Sources/CCryptoBoringSSL/include/boringssl_prefix_symbols_nasm.inc index 193103f8..28c4f1b5 100644 --- a/Sources/CCryptoBoringSSL/include/boringssl_prefix_symbols_nasm.inc +++ b/Sources/CCryptoBoringSSL/include/boringssl_prefix_symbols_nasm.inc @@ -46,9 +46,11 @@ %xdefine _ASN1_BOOLEAN_it _ %+ BORINGSSL_PREFIX %+ _ASN1_BOOLEAN_it %xdefine _ASN1_ENUMERATED_free _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_free %xdefine _ASN1_ENUMERATED_get _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_get +%xdefine _ASN1_ENUMERATED_get_uint64 _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_get_uint64 %xdefine _ASN1_ENUMERATED_it _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_it %xdefine _ASN1_ENUMERATED_new _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_new %xdefine _ASN1_ENUMERATED_set _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_set +%xdefine _ASN1_ENUMERATED_set_uint64 _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_set_uint64 %xdefine _ASN1_ENUMERATED_to_BN _ %+ BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_to_BN %xdefine _ASN1_FBOOLEAN_it _ %+ BORINGSSL_PREFIX %+ _ASN1_FBOOLEAN_it %xdefine _ASN1_GENERALIZEDTIME_adj _ %+ BORINGSSL_PREFIX %+ _ASN1_GENERALIZEDTIME_adj @@ -69,6 +71,7 @@ %xdefine _ASN1_INTEGER_dup _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_dup %xdefine _ASN1_INTEGER_free _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_free %xdefine _ASN1_INTEGER_get _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_get +%xdefine _ASN1_INTEGER_get_uint64 _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_get_uint64 %xdefine _ASN1_INTEGER_it _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_it %xdefine _ASN1_INTEGER_new _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_new %xdefine _ASN1_INTEGER_set _ %+ BORINGSSL_PREFIX %+ _ASN1_INTEGER_set @@ -991,6 +994,7 @@ %xdefine _EVP_HPKE_KEY_public_key _ %+ BORINGSSL_PREFIX %+ _EVP_HPKE_KEY_public_key %xdefine _EVP_HPKE_KEY_zero _ %+ BORINGSSL_PREFIX %+ _EVP_HPKE_KEY_zero %xdefine _EVP_MD_CTX_block_size _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_block_size +%xdefine _EVP_MD_CTX_cleanse _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_cleanse %xdefine _EVP_MD_CTX_cleanup _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_cleanup %xdefine _EVP_MD_CTX_copy _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_copy %xdefine _EVP_MD_CTX_copy_ex _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_copy_ex @@ -1193,7 +1197,9 @@ %xdefine _EXTENDED_KEY_USAGE_new _ %+ BORINGSSL_PREFIX %+ _EXTENDED_KEY_USAGE_new %xdefine _FIPS_mode _ %+ BORINGSSL_PREFIX %+ _FIPS_mode %xdefine _FIPS_mode_set _ %+ BORINGSSL_PREFIX %+ _FIPS_mode_set +%xdefine _FIPS_query_algorithm_status _ %+ BORINGSSL_PREFIX %+ _FIPS_query_algorithm_status %xdefine _FIPS_read_counter _ %+ BORINGSSL_PREFIX %+ _FIPS_read_counter +%xdefine _FIPS_version _ %+ BORINGSSL_PREFIX %+ _FIPS_version %xdefine _GENERAL_NAMES_free _ %+ BORINGSSL_PREFIX %+ _GENERAL_NAMES_free %xdefine _GENERAL_NAMES_it _ %+ BORINGSSL_PREFIX %+ _GENERAL_NAMES_it %xdefine _GENERAL_NAMES_new _ %+ BORINGSSL_PREFIX %+ _GENERAL_NAMES_new @@ -1214,6 +1220,7 @@ %xdefine _HKDF_expand _ %+ BORINGSSL_PREFIX %+ _HKDF_expand %xdefine _HKDF_extract _ %+ BORINGSSL_PREFIX %+ _HKDF_extract %xdefine _HMAC _ %+ BORINGSSL_PREFIX %+ _HMAC +%xdefine _HMAC_CTX_cleanse _ %+ BORINGSSL_PREFIX %+ _HMAC_CTX_cleanse %xdefine _HMAC_CTX_cleanup _ %+ BORINGSSL_PREFIX %+ _HMAC_CTX_cleanup %xdefine _HMAC_CTX_copy _ %+ BORINGSSL_PREFIX %+ _HMAC_CTX_copy %xdefine _HMAC_CTX_copy_ex _ %+ BORINGSSL_PREFIX %+ _HMAC_CTX_copy_ex @@ -2314,7 +2321,9 @@ %xdefine _bn_uadd_consttime _ %+ BORINGSSL_PREFIX %+ _bn_uadd_consttime %xdefine _bn_usub_consttime _ %+ BORINGSSL_PREFIX %+ _bn_usub_consttime %xdefine _bn_wexpand _ %+ BORINGSSL_PREFIX %+ _bn_wexpand -%xdefine _boringssl_fips_self_test _ %+ BORINGSSL_PREFIX %+ _boringssl_fips_self_test +%xdefine _boringssl_self_test_hmac_sha256 _ %+ BORINGSSL_PREFIX %+ _boringssl_self_test_hmac_sha256 +%xdefine _boringssl_self_test_sha256 _ %+ BORINGSSL_PREFIX %+ _boringssl_self_test_sha256 +%xdefine _boringssl_self_test_sha512 _ %+ BORINGSSL_PREFIX %+ _boringssl_self_test_sha512 %xdefine _c2i_ASN1_BIT_STRING _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_BIT_STRING %xdefine _c2i_ASN1_INTEGER _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_INTEGER %xdefine _c2i_ASN1_OBJECT _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_OBJECT @@ -2450,6 +2459,7 @@ %xdefine _d2i_X509_VAL _ %+ BORINGSSL_PREFIX %+ _d2i_X509_VAL %xdefine _d2i_X509_bio _ %+ BORINGSSL_PREFIX %+ _d2i_X509_bio %xdefine _d2i_X509_fp _ %+ BORINGSSL_PREFIX %+ _d2i_X509_fp +%xdefine _dh_compute_key_padded_no_self_test _ %+ BORINGSSL_PREFIX %+ _dh_compute_key_padded_no_self_test %xdefine _dsa_asn1_meth _ %+ BORINGSSL_PREFIX %+ _dsa_asn1_meth %xdefine _dsa_check_parameters _ %+ BORINGSSL_PREFIX %+ _dsa_check_parameters %xdefine _ec_GFp_mont_add _ %+ BORINGSSL_PREFIX %+ _ec_GFp_mont_add @@ -2509,6 +2519,7 @@ %xdefine _ec_jacobian_to_affine_batch _ %+ BORINGSSL_PREFIX %+ _ec_jacobian_to_affine_batch %xdefine _ec_pkey_meth _ %+ BORINGSSL_PREFIX %+ _ec_pkey_meth %xdefine _ec_point_from_uncompressed _ %+ BORINGSSL_PREFIX %+ _ec_point_from_uncompressed +%xdefine _ec_point_mul_no_self_test _ %+ BORINGSSL_PREFIX %+ _ec_point_mul_no_self_test %xdefine _ec_point_mul_scalar _ %+ BORINGSSL_PREFIX %+ _ec_point_mul_scalar %xdefine _ec_point_mul_scalar_base _ %+ BORINGSSL_PREFIX %+ _ec_point_mul_scalar_base %xdefine _ec_point_mul_scalar_batch _ %+ BORINGSSL_PREFIX %+ _ec_point_mul_scalar_batch @@ -2537,6 +2548,7 @@ %xdefine _ec_set_to_safe_point _ %+ BORINGSSL_PREFIX %+ _ec_set_to_safe_point %xdefine _ec_simple_scalar_inv0_montgomery _ %+ BORINGSSL_PREFIX %+ _ec_simple_scalar_inv0_montgomery %xdefine _ec_simple_scalar_to_montgomery_inv_vartime _ %+ BORINGSSL_PREFIX %+ _ec_simple_scalar_to_montgomery_inv_vartime +%xdefine _ecdsa_do_verify_no_self_test _ %+ BORINGSSL_PREFIX %+ _ecdsa_do_verify_no_self_test %xdefine _ecdsa_sign_with_nonce_for_known_answer_test _ %+ BORINGSSL_PREFIX %+ _ecdsa_sign_with_nonce_for_known_answer_test %xdefine _ecp_nistz256_avx2_select_w7 _ %+ BORINGSSL_PREFIX %+ _ecp_nistz256_avx2_select_w7 %xdefine _ecp_nistz256_mul_mont _ %+ BORINGSSL_PREFIX %+ _ecp_nistz256_mul_mont @@ -2763,6 +2775,9 @@ %xdefine _rsa_default_sign_raw _ %+ BORINGSSL_PREFIX %+ _rsa_default_sign_raw %xdefine _rsa_default_size _ %+ BORINGSSL_PREFIX %+ _rsa_default_size %xdefine _rsa_pkey_meth _ %+ BORINGSSL_PREFIX %+ _rsa_pkey_meth +%xdefine _rsa_sign_no_self_test _ %+ BORINGSSL_PREFIX %+ _rsa_sign_no_self_test +%xdefine _rsa_verify_no_self_test _ %+ BORINGSSL_PREFIX %+ _rsa_verify_no_self_test +%xdefine _rsa_verify_raw_no_self_test _ %+ BORINGSSL_PREFIX %+ _rsa_verify_raw_no_self_test %xdefine _rsaz_1024_gather5_avx2 _ %+ BORINGSSL_PREFIX %+ _rsaz_1024_gather5_avx2 %xdefine _rsaz_1024_mul_avx2 _ %+ BORINGSSL_PREFIX %+ _rsaz_1024_mul_avx2 %xdefine _rsaz_1024_norm2red_avx2 _ %+ BORINGSSL_PREFIX %+ _rsaz_1024_norm2red_avx2 @@ -2896,9 +2911,11 @@ %xdefine ASN1_BOOLEAN_it BORINGSSL_PREFIX %+ _ASN1_BOOLEAN_it %xdefine ASN1_ENUMERATED_free BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_free %xdefine ASN1_ENUMERATED_get BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_get +%xdefine ASN1_ENUMERATED_get_uint64 BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_get_uint64 %xdefine ASN1_ENUMERATED_it BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_it %xdefine ASN1_ENUMERATED_new BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_new %xdefine ASN1_ENUMERATED_set BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_set +%xdefine ASN1_ENUMERATED_set_uint64 BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_set_uint64 %xdefine ASN1_ENUMERATED_to_BN BORINGSSL_PREFIX %+ _ASN1_ENUMERATED_to_BN %xdefine ASN1_FBOOLEAN_it BORINGSSL_PREFIX %+ _ASN1_FBOOLEAN_it %xdefine ASN1_GENERALIZEDTIME_adj BORINGSSL_PREFIX %+ _ASN1_GENERALIZEDTIME_adj @@ -2919,6 +2936,7 @@ %xdefine ASN1_INTEGER_dup BORINGSSL_PREFIX %+ _ASN1_INTEGER_dup %xdefine ASN1_INTEGER_free BORINGSSL_PREFIX %+ _ASN1_INTEGER_free %xdefine ASN1_INTEGER_get BORINGSSL_PREFIX %+ _ASN1_INTEGER_get +%xdefine ASN1_INTEGER_get_uint64 BORINGSSL_PREFIX %+ _ASN1_INTEGER_get_uint64 %xdefine ASN1_INTEGER_it BORINGSSL_PREFIX %+ _ASN1_INTEGER_it %xdefine ASN1_INTEGER_new BORINGSSL_PREFIX %+ _ASN1_INTEGER_new %xdefine ASN1_INTEGER_set BORINGSSL_PREFIX %+ _ASN1_INTEGER_set @@ -3841,6 +3859,7 @@ %xdefine EVP_HPKE_KEY_public_key BORINGSSL_PREFIX %+ _EVP_HPKE_KEY_public_key %xdefine EVP_HPKE_KEY_zero BORINGSSL_PREFIX %+ _EVP_HPKE_KEY_zero %xdefine EVP_MD_CTX_block_size BORINGSSL_PREFIX %+ _EVP_MD_CTX_block_size +%xdefine EVP_MD_CTX_cleanse BORINGSSL_PREFIX %+ _EVP_MD_CTX_cleanse %xdefine EVP_MD_CTX_cleanup BORINGSSL_PREFIX %+ _EVP_MD_CTX_cleanup %xdefine EVP_MD_CTX_copy BORINGSSL_PREFIX %+ _EVP_MD_CTX_copy %xdefine EVP_MD_CTX_copy_ex BORINGSSL_PREFIX %+ _EVP_MD_CTX_copy_ex @@ -4043,7 +4062,9 @@ %xdefine EXTENDED_KEY_USAGE_new BORINGSSL_PREFIX %+ _EXTENDED_KEY_USAGE_new %xdefine FIPS_mode BORINGSSL_PREFIX %+ _FIPS_mode %xdefine FIPS_mode_set BORINGSSL_PREFIX %+ _FIPS_mode_set +%xdefine FIPS_query_algorithm_status BORINGSSL_PREFIX %+ _FIPS_query_algorithm_status %xdefine FIPS_read_counter BORINGSSL_PREFIX %+ _FIPS_read_counter +%xdefine FIPS_version BORINGSSL_PREFIX %+ _FIPS_version %xdefine GENERAL_NAMES_free BORINGSSL_PREFIX %+ _GENERAL_NAMES_free %xdefine GENERAL_NAMES_it BORINGSSL_PREFIX %+ _GENERAL_NAMES_it %xdefine GENERAL_NAMES_new BORINGSSL_PREFIX %+ _GENERAL_NAMES_new @@ -4064,6 +4085,7 @@ %xdefine HKDF_expand BORINGSSL_PREFIX %+ _HKDF_expand %xdefine HKDF_extract BORINGSSL_PREFIX %+ _HKDF_extract %xdefine HMAC BORINGSSL_PREFIX %+ _HMAC +%xdefine HMAC_CTX_cleanse BORINGSSL_PREFIX %+ _HMAC_CTX_cleanse %xdefine HMAC_CTX_cleanup BORINGSSL_PREFIX %+ _HMAC_CTX_cleanup %xdefine HMAC_CTX_copy BORINGSSL_PREFIX %+ _HMAC_CTX_copy %xdefine HMAC_CTX_copy_ex BORINGSSL_PREFIX %+ _HMAC_CTX_copy_ex @@ -5164,7 +5186,9 @@ %xdefine bn_uadd_consttime BORINGSSL_PREFIX %+ _bn_uadd_consttime %xdefine bn_usub_consttime BORINGSSL_PREFIX %+ _bn_usub_consttime %xdefine bn_wexpand BORINGSSL_PREFIX %+ _bn_wexpand -%xdefine boringssl_fips_self_test BORINGSSL_PREFIX %+ _boringssl_fips_self_test +%xdefine boringssl_self_test_hmac_sha256 BORINGSSL_PREFIX %+ _boringssl_self_test_hmac_sha256 +%xdefine boringssl_self_test_sha256 BORINGSSL_PREFIX %+ _boringssl_self_test_sha256 +%xdefine boringssl_self_test_sha512 BORINGSSL_PREFIX %+ _boringssl_self_test_sha512 %xdefine c2i_ASN1_BIT_STRING BORINGSSL_PREFIX %+ _c2i_ASN1_BIT_STRING %xdefine c2i_ASN1_INTEGER BORINGSSL_PREFIX %+ _c2i_ASN1_INTEGER %xdefine c2i_ASN1_OBJECT BORINGSSL_PREFIX %+ _c2i_ASN1_OBJECT @@ -5300,6 +5324,7 @@ %xdefine d2i_X509_VAL BORINGSSL_PREFIX %+ _d2i_X509_VAL %xdefine d2i_X509_bio BORINGSSL_PREFIX %+ _d2i_X509_bio %xdefine d2i_X509_fp BORINGSSL_PREFIX %+ _d2i_X509_fp +%xdefine dh_compute_key_padded_no_self_test BORINGSSL_PREFIX %+ _dh_compute_key_padded_no_self_test %xdefine dsa_asn1_meth BORINGSSL_PREFIX %+ _dsa_asn1_meth %xdefine dsa_check_parameters BORINGSSL_PREFIX %+ _dsa_check_parameters %xdefine ec_GFp_mont_add BORINGSSL_PREFIX %+ _ec_GFp_mont_add @@ -5359,6 +5384,7 @@ %xdefine ec_jacobian_to_affine_batch BORINGSSL_PREFIX %+ _ec_jacobian_to_affine_batch %xdefine ec_pkey_meth BORINGSSL_PREFIX %+ _ec_pkey_meth %xdefine ec_point_from_uncompressed BORINGSSL_PREFIX %+ _ec_point_from_uncompressed +%xdefine ec_point_mul_no_self_test BORINGSSL_PREFIX %+ _ec_point_mul_no_self_test %xdefine ec_point_mul_scalar BORINGSSL_PREFIX %+ _ec_point_mul_scalar %xdefine ec_point_mul_scalar_base BORINGSSL_PREFIX %+ _ec_point_mul_scalar_base %xdefine ec_point_mul_scalar_batch BORINGSSL_PREFIX %+ _ec_point_mul_scalar_batch @@ -5387,6 +5413,7 @@ %xdefine ec_set_to_safe_point BORINGSSL_PREFIX %+ _ec_set_to_safe_point %xdefine ec_simple_scalar_inv0_montgomery BORINGSSL_PREFIX %+ _ec_simple_scalar_inv0_montgomery %xdefine ec_simple_scalar_to_montgomery_inv_vartime BORINGSSL_PREFIX %+ _ec_simple_scalar_to_montgomery_inv_vartime +%xdefine ecdsa_do_verify_no_self_test BORINGSSL_PREFIX %+ _ecdsa_do_verify_no_self_test %xdefine ecdsa_sign_with_nonce_for_known_answer_test BORINGSSL_PREFIX %+ _ecdsa_sign_with_nonce_for_known_answer_test %xdefine ecp_nistz256_avx2_select_w7 BORINGSSL_PREFIX %+ _ecp_nistz256_avx2_select_w7 %xdefine ecp_nistz256_mul_mont BORINGSSL_PREFIX %+ _ecp_nistz256_mul_mont @@ -5613,6 +5640,9 @@ %xdefine rsa_default_sign_raw BORINGSSL_PREFIX %+ _rsa_default_sign_raw %xdefine rsa_default_size BORINGSSL_PREFIX %+ _rsa_default_size %xdefine rsa_pkey_meth BORINGSSL_PREFIX %+ _rsa_pkey_meth +%xdefine rsa_sign_no_self_test BORINGSSL_PREFIX %+ _rsa_sign_no_self_test +%xdefine rsa_verify_no_self_test BORINGSSL_PREFIX %+ _rsa_verify_no_self_test +%xdefine rsa_verify_raw_no_self_test BORINGSSL_PREFIX %+ _rsa_verify_raw_no_self_test %xdefine rsaz_1024_gather5_avx2 BORINGSSL_PREFIX %+ _rsaz_1024_gather5_avx2 %xdefine rsaz_1024_mul_avx2 BORINGSSL_PREFIX %+ _rsaz_1024_mul_avx2 %xdefine rsaz_1024_norm2red_avx2 BORINGSSL_PREFIX %+ _rsaz_1024_norm2red_avx2 diff --git a/scripts/patch-1-inttypes.patch b/scripts/patch-1-inttypes.patch index 5a8ec152..e2e68a4d 100644 --- a/scripts/patch-1-inttypes.patch +++ b/scripts/patch-1-inttypes.patch @@ -14,11 +14,11 @@ diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h b/Sources/CC index c86c1ef..7013140 100644 --- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h +++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_bn.h -@@ -126,7 +126,7 @@ +@@ -126,7 +126,8 @@ #include "CCryptoBoringSSL_base.h" #include "CCryptoBoringSSL_thread.h" --#include // for PRIu64 and friends + #include // for PRIu64 and friends +#include #include // for FILE* diff --git a/scripts/patch-2-arm-arch.patch b/scripts/patch-2-arm-arch.patch deleted file mode 100644 index 7a601e0e..00000000 --- a/scripts/patch-2-arm-arch.patch +++ /dev/null @@ -1,17 +0,0 @@ -diff --git a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h -index faa2655..0e76796 100644 ---- a/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h -+++ b/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_arm_arch.h -@@ -50,6 +50,7 @@ - * (eay@cryptsoft.com). This product includes software written by Tim - * Hudson (tjh@cryptsoft.com). */ - -+#if __arm__ || __arm64__ || __aarch64__ - #ifndef OPENSSL_HEADER_ARM_ARCH_H - #define OPENSSL_HEADER_ARM_ARCH_H - -@@ -171,3 +172,4 @@ - #endif /* defined __ASSEMBLER__ */ - - #endif // OPENSSL_HEADER_ARM_ARCH_H -+#endif // __arm__ || __arm64__ || __aarch64__ diff --git a/scripts/vendor-boringssl.sh b/scripts/vendor-boringssl.sh index 2c52668f..3e6bd6bb 100755 --- a/scripts/vendor-boringssl.sh +++ b/scripts/vendor-boringssl.sh @@ -290,7 +290,6 @@ echo "PROTECTING against executable stacks" echo "PATCHING BoringSSL" git apply "${HERE}/scripts/patch-1-inttypes.patch" -git apply "${HERE}/scripts/patch-2-arm-arch.patch" # We need BoringSSL to be modularised echo "MODULARISING BoringSSL"