Compare commits

..
9 Commits
Author SHA1 Message Date
Cory Benfield d9825fa541 Update BoringSSL to f961de5c47ed265c3e758ec70dd15ece20809962 (#115)
This patch also cleans up an include issue. The actual code change is in
`scripts/vendor_boringssl.sh`, the rest is just the update.
2022-04-21 10:39:48 +01:00
Cory Benfield d180057628 Update BoringSSL to 2fc6d38391cb76839c76b2a462619e7d69fd998d (#113) 2022-04-20 03:50:13 -07:00
Cory Benfield 17d95a6782 Avoid unnecessary copies of BoringSSL (#94)
Motivation:

When using only the CryptoKit API on Apple platforms we have always
thunked through to the CryptoKit interface and implementation. However,
we didn't do a thorough job of preventing the BoringSSL target from
getting compiled and linked.

We can do a better job now, which will save compile times and binary
sizes in many cases.

Modifications:

- Change Package.swift to express a target specific dependency in most
  cases.
- Preserve a development mode which overrides that target specific
  dependency.
- Add the missing compile guards.

Results:

Smaller binaries and faster compiles on Apple platforms.
2022-04-20 11:47:19 +01:00
Fabian Fett 12a1784190 Drop support for Swift 5.2 and 5.3 (#111)
As outlined in a [Swift forums post in November ’21](https://forums.swift.org/t/swiftnio-swift-version-support/53232), SwiftNIO will only support the latest non-patch Swift release and the 2 immediately prior non-patch versions.

In this commit we drop support for Swift 5.2 and 5.3. We update CI for Swift 5.4 to run on bionic instead of focal to ensure that we still test bionic.
2022-04-20 08:17:41 +01:00
Fabian Fett ce8d18032f Use SPM API diff checker (#112) 2022-04-19 17:15:40 +01:00
Cory Benfield 067254c794 Update BoringSSL to 8bbefbfeee609b17622deedd100163c12f5c95dc (#110) 2022-03-22 13:43:20 +01:00
tomer doron 476397d837 ci update (#108)
motivation: 5.6 is out

changes:
* use release version of 5.6
* add docker setup for 5.7 (using nightly for now)
2022-03-16 21:30:22 -07:00
Saleem Abdulrasool facc82fbb4 build: attempt to workaround linker issues on Darwin (#105)
The use of a non-cctools librarian causes ld64 some heartburn.
Explicitly opt into using the cctools librarian.

rdar://87850031
2022-02-25 17:46:28 +00:00
Cory Benfield 3fb2ea5d41 Add 5.6 nightly CI (#103)
The 5.6 nightly images are available, let's use them.
2022-01-18 09:39:36 +00:00
145 changed files with 13788 additions and 5457 deletions

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
# file options
--swiftversion 5.2
--swiftversion 5.4
--exclude .build
# format options
+5
View File
@@ -37,6 +37,11 @@ if(BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND NOT CMAKE_CROSSCOMPILING)
set(CMAKE_AR "/usr/bin/ar")
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> crs <TARGET> <LINK_FLAGS> <OBJECTS>")
endif()
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
find_package(dispatch CONFIG)
find_package(Foundation CONFIG)
+64 -19
View File
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.4
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
@@ -20,15 +20,38 @@
// Sources/CCryptoBoringSSL directory. The source repository is at
// https://boringssl.googlesource.com/boringssl.
//
// BoringSSL Commit: 295b31324f8c557dcd3c1c831857e33a7f23bc52
// BoringSSL Commit: f961de5c47ed265c3e758ec70dd15ece20809962
import PackageDescription
let swiftSettings: [SwiftSetting] = [
.define("CRYPTO_IN_SWIFTPM"),
// To develop this on Apple platforms, uncomment this define.
// .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
]
// To develop this on Apple platforms, set this to true
let development = false
let swiftSettings: [SwiftSetting]
let dependencies: [Target.Dependency]
if development {
swiftSettings = [
.define("CRYPTO_IN_SWIFTPM"),
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
]
dependencies = [
"CCryptoBoringSSL",
"CCryptoBoringSSLShims"
]
} else {
swiftSettings = [
.define("CRYPTO_IN_SWIFTPM"),
]
let platforms: [Platform] = [
Platform.linux,
Platform.android,
Platform.windows,
]
dependencies = [
.target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)),
.target(name: "CCryptoBoringSSLShims", condition: .when(platforms: platforms))
]
}
let package = Package(
name: "swift-crypto",
@@ -48,20 +71,42 @@ let package = Package(
dependencies: [],
targets: [
.target(
name: "CCryptoBoringSSL",
cSettings: [
/*
* This define is required on Windows, but because we need older
* versions of SPM, we cannot conditionally define this on Windows
* only. Unconditionally define it instead.
*/
.define("WIN32_LEAN_AND_MEAN"),
]
name: "CCryptoBoringSSL",
exclude: [
"hash.txt",
"include/boringssl_prefix_symbols_nasm.inc",
"CMakeLists.txt",
],
cSettings: [
/*
* This define is required on Windows, but because we need older
* versions of SPM, we cannot conditionally define this on Windows
* only. Unconditionally define it instead.
*/
.define("WIN32_LEAN_AND_MEAN"),
]
),
.target(
name: "CCryptoBoringSSLShims",
dependencies: ["CCryptoBoringSSL"],
exclude: [
"CMakeLists.txt"
]
),
.target(
name: "Crypto",
dependencies: dependencies,
exclude: [
"CMakeLists.txt",
"AEADs/Nonces.swift.gyb",
"Digests/Digests.swift.gyb",
"Key Agreement/ECDH.swift.gyb",
"Signatures/ECDSA.swift.gyb",
],
swiftSettings: swiftSettings
),
.target(name: "CCryptoBoringSSLShims", dependencies: ["CCryptoBoringSSL"]),
.target(name: "Crypto", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims"], swiftSettings: swiftSettings),
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "Crypto"]),
.target(name: "crypto-shasum", dependencies: ["Crypto"]),
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
.testTarget(name: "CryptoTests", dependencies: ["Crypto"], swiftSettings: swiftSettings),
.testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]),
],
+1 -1
View File
@@ -108,7 +108,7 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo
### Swift versions
Swift Crypto supports Swift 5.2 and later. Swift Crypto supported Swift 5.1 until version 1.2.
Swift Crypto supports Swift 5.4 and later. Swift Crypto supported Swift 5.2 and 5.3 until version 2.1 and Swift 5.1 until version 2.0.
### Compatibility
+7 -4
View File
@@ -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"
@@ -159,7 +158,7 @@ add_library(CCryptoBoringSSL STATIC
"crypto/fipsmodule/ec/felem.c"
"crypto/fipsmodule/ec/oct.c"
"crypto/fipsmodule/ec/p224-64.c"
"crypto/fipsmodule/ec/p256-x86_64.c"
"crypto/fipsmodule/ec/p256-nistz.c"
"crypto/fipsmodule/ec/p256.c"
"crypto/fipsmodule/ec/scalar.c"
"crypto/fipsmodule/ec/simple.c"
@@ -365,6 +364,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm
crypto/fipsmodule/armv8-mont.ios.aarch64.S
crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S
crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S
crypto/fipsmodule/p256-armv8-asm.ios.aarch64.S
crypto/fipsmodule/p256_beeu-armv8-asm.ios.aarch64.S
crypto/fipsmodule/sha1-armv8.ios.aarch64.S
crypto/fipsmodule/sha256-armv8.ios.aarch64.S
crypto/fipsmodule/sha512-armv8.ios.aarch64.S
@@ -376,6 +377,8 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATC
crypto/fipsmodule/armv8-mont.linux.aarch64.S
crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S
crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S
crypto/fipsmodule/p256-armv8-asm.linux.aarch64.S
crypto/fipsmodule/p256_beeu-armv8-asm.linux.aarch64.S
crypto/fipsmodule/sha1-armv8.linux.aarch64.S
crypto/fipsmodule/sha256-armv8.linux.aarch64.S
crypto/fipsmodule/sha512-armv8.linux.aarch64.S
@@ -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 <CCryptoBoringSSL_asn1.h>
#include <limits.h>
#include <string.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
#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);
}
+312 -270
View File
@@ -59,8 +59,10 @@
#include <string.h>
#include <limits.h>
#include <CCryptoBoringSSL_bytestring.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_type_check.h>
#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);
}
@@ -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');
+41 -88
View File
@@ -59,7 +59,7 @@
#include <limits.h>
#include <string.h>
#include <CCryptoBoringSSL_asn1_mac.h>
#include <CCryptoBoringSSL_bytestring.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
@@ -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;
}
/*
@@ -74,8 +74,6 @@
*/
#define ASN1_MAX_CONSTRUCTED_NEST 30
static int asn1_check_eoc(const unsigned char **in, long len);
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
char *cst, const unsigned char **in, long len,
int exptag, int expclass, char opt, ASN1_TLC *ctx);
@@ -373,13 +371,6 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
if (!len)
break;
q = p;
/* TODO(https://crbug.com/boringssl/455): Although we've removed
* indefinite-length support, this check is not quite a no-op.
* Reject [UNIVERSAL 0] in the tag parsers themselves. */
if (asn1_check_eoc(&p, len)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
goto err;
}
/*
* This determines the OPTIONAL flag value. The field cannot be
* omitted if it is the last of a SEQUENCE and there is still
@@ -592,13 +583,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
while (len > 0) {
ASN1_VALUE *skfield;
const unsigned char *q = p;
/* TODO(https://crbug.com/boringssl/455): Although we've removed
* indefinite-length support, this check is not quite a no-op.
* Reject [UNIVERSAL 0] in the tag parsers themselves. */
if (asn1_check_eoc(&p, len)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
goto err;
}
skfield = NULL;
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
-1, 0, 0, ctx, depth)) {
@@ -868,21 +852,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
return ret;
}
/* Check for ASN1 EOC and swallow it if found */
static int asn1_check_eoc(const unsigned char **in, long len)
{
const unsigned char *p;
if (len < 2)
return 0;
p = *in;
if (!p[0] && !p[1]) {
*in += 2;
return 1;
}
return 0;
}
/*
* Check an ASN1 tag and length: a bit like ASN1_get_object but it handles
* the ASN1_TLC cache and checks the expected tag.
@@ -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
@@ -71,18 +71,6 @@ int BIO_printf(BIO *bio, const char *format, ...) {
va_start(args, format);
out_len = vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
#if defined(OPENSSL_WINDOWS)
// On Windows, vsnprintf returns -1 rather than the requested length on
// truncation
if (out_len < 0) {
va_start(args, format);
out_len = _vscprintf(format, args);
va_end(args);
assert(out_len >= (int)sizeof(buf));
}
#endif
if (out_len < 0) {
return -1;
}
@@ -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) {
@@ -93,11 +93,14 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
return 1;
}
// is_eoc returns true if |header_len| and |contents|, as returned by
// |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value.
static char is_eoc(size_t header_len, CBS *contents) {
return header_len == 2 && CBS_len(contents) == 2 &&
OPENSSL_memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
// cbs_get_eoc returns one if |cbs| begins with an "end of contents" (EOC) value
// and zero otherwise. If an EOC was found, it advances |cbs| past it.
static int cbs_get_eoc(CBS *cbs) {
if (CBS_len(cbs) >= 2 &&
CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) {
return CBS_skip(cbs, 2);
}
return 0;
}
// cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
@@ -116,21 +119,20 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
}
while (CBS_len(in) > 0) {
if (looking_for_eoc && cbs_get_eoc(in)) {
return 1;
}
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;
}
if (is_eoc(header_len, &contents)) {
return looking_for_eoc;
}
if (string_tag != 0) {
// This is part of a constructed string. All elements must match
// |string_tag| up to the constructed bit and get appended to |out|
@@ -153,11 +155,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 +171,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 {
@@ -279,13 +279,20 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
tag |= tag_number;
// Tag [UNIVERSAL 0] is reserved for use by the encoding. Reject it here to
// avoid some ambiguity around ANY values and BER indefinite-length EOCs. See
// https://crbug.com/boringssl/455.
if ((tag & ~CBS_ASN1_CONSTRUCTED) == 0) {
return 0;
}
*out = tag;
return 1;
}
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 +301,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 +344,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 +407,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,
@@ -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
@@ -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
@@ -19,8 +19,6 @@
#include <assert.h>
#include <string.h>
#include <CCryptoBoringSSL_cpu.h>
#include "../internal.h"
#include "internal.h"
@@ -50,7 +50,6 @@
#include <assert.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_cipher.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
@@ -17,11 +17,11 @@
#include <assert.h>
#include <CCryptoBoringSSL_cipher.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_crypto.h>
#include <CCryptoBoringSSL_err.h>
#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;
@@ -59,7 +59,6 @@
#include <CCryptoBoringSSL_nid.h>
#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(); }
@@ -60,7 +60,6 @@
#include <stdlib.h>
#include <CCryptoBoringSSL_base.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_type_check.h>
#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
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_APPLE) && \
!defined(OPENSSL_STATIC_ARMCAP)
@@ -22,8 +22,6 @@
#include <CCryptoBoringSSL_arm_arch.h>
#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 =
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_FUCHSIA) && \
!defined(OPENSSL_STATIC_ARMCAP)
@@ -23,16 +23,14 @@
#include <CCryptoBoringSSL_arm_arch.h>
#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
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_LINUX) && \
!defined(OPENSSL_STATIC_ARMCAP)
@@ -21,8 +21,6 @@
#include <CCryptoBoringSSL_arm_arch.h>
#include "internal.h"
extern uint32_t OPENSSL_armcap_P;
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_WINDOWS) && \
!defined(OPENSSL_STATIC_ARMCAP)
@@ -22,8 +22,6 @@
#include <CCryptoBoringSSL_arm_arch.h>
#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
+1 -1
View File
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_STATIC_ARMCAP)
@@ -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 <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP)
#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \
!defined(OPENSSL_STATIC_ARMCAP)
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
@@ -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
+1 -2
View File
@@ -54,8 +54,7 @@
* copied and put under another distribution licence
* [including the GNU Public Licence.] */
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_base.h>
#if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
@@ -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 <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_base.h>
#if defined(OPENSSL_PPC64LE)
+14 -10
View File
@@ -14,8 +14,6 @@
#include <CCryptoBoringSSL_crypto.h>
#include <CCryptoBoringSSL_cpu.h>
#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
@@ -24,7 +24,6 @@
#include <assert.h>
#include <string.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_rand.h>
#include <CCryptoBoringSSL_sha.h>
@@ -37,6 +36,10 @@
// Various pre-computed constants.
#include "./curve25519_tables.h"
#if defined(OPENSSL_NO_ASM)
#define FIAT_25519_NO_ASM
#endif
#if defined(BORINGSSL_CURVE25519_64BIT)
#include "../../third_party/fiat/curve25519_64.h"
#else
@@ -503,27 +506,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);
@@ -59,7 +59,7 @@
#include <CCryptoBoringSSL_base.h>
#include "../../internal.h"
#include "../internal.h"
#if defined(__cplusplus)
extern "C" {
File diff suppressed because it is too large. Load diff
@@ -50,8 +50,6 @@
#include <assert.h>
#include <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#include "../modes/internal.h"
@@ -17,7 +17,7 @@
#include <stdlib.h>
#include <CCryptoBoringSSL_cpu.h>
#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
@@ -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
@@ -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
@@ -112,7 +112,6 @@
#include <stdlib.h>
#include <string.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
@@ -16,9 +16,9 @@
#define OPENSSL_HEADER_BN_RSAZ_EXP_H
#include <CCryptoBoringSSL_bn.h>
#include <CCryptoBoringSSL_cpu.h>
#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();
}
@@ -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;
}
@@ -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);
}
@@ -52,7 +52,6 @@
#include <CCryptoBoringSSL_aead.h>
#include <CCryptoBoringSSL_aes.h>
#include <CCryptoBoringSSL_cipher.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_nid.h>
@@ -64,6 +64,7 @@
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_thread.h>
#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;
@@ -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 <CCryptoBoringSSL_base.h>
#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
@@ -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;
@@ -246,7 +246,8 @@ DEFINE_METHOD_FUNCTION(struct built_in_curves, OPENSSL_built_in_curves) {
out->curves[2].param_len = 32;
out->curves[2].params = kP256Params;
out->curves[2].method =
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
#if !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL)
EC_GFp_nistz256_method();
#else
@@ -943,8 +944,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 +1012,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) {
@@ -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;
}
@@ -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,
@@ -9,7 +9,7 @@
*/
// This is the precomputed constant time access table for the code in
// p256-x86_64.c, for the default generator. The table consists of 37
// p256-nistz.c, for the default generator. The table consists of 37
// subtables, each subtable contains 64 affine points. The affine points are
// encoded as eight uint64's, four for the x coordinate and four for the y.
// Both values are in little-endian order. There are 37 tables because a
@@ -23,20 +23,17 @@
#include <string.h>
#include <CCryptoBoringSSL_bn.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_crypto.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_cpu.h>
#include "../bn/internal.h"
#include "../delocate.h"
#include "../../internal.h"
#include "internal.h"
#include "p256-x86_64.h"
#include "p256-nistz.h"
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
#if !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL)
typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
@@ -48,7 +45,7 @@ static const BN_ULONG ONE[P256_LIMBS] = {
};
// Precomputed tables for the default generator
#include "p256-x86_64-table.h"
#include "p256-nistz-table.h"
// Recode window to a signed digit, see |ec_GFp_nistp_recode_scalar_bits| in
// util.c for details
@@ -557,10 +554,12 @@ 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 defined(OPENSSL_X86_64)
if (!CRYPTO_is_AVX_capable()) {
// No AVX support; fallback to generic code.
return ec_simple_scalar_to_montgomery_inv_vartime(group, out, in);
}
#endif
assert(group->order.width == P256_LIMBS);
if (!beeu_mod_inverse_vartime(out->words, in->words, group->order.d)) {
@@ -631,5 +630,6 @@ DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistz256_method) {
out->cmp_x_coordinate = ecp_nistz256_cmp_x_coordinate;
}
#endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
#endif /* !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL) */
@@ -30,7 +30,8 @@ extern "C" {
#endif
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
#if !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL)
// P-256 field operations.
@@ -142,8 +143,9 @@ void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a,
void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a,
const P256_POINT_AFFINE *b);
#endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
!defined(OPENSSL_SMALL) */
#endif /* !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL) */
#if defined(__cplusplus)
@@ -31,8 +31,10 @@
#include "../delocate.h"
#include "./internal.h"
#if defined(OPENSSL_NO_ASM)
#define FIAT_P256_NO_ASM
#endif
// MSVC does not implement uint128_t, and crashes with intrinsics
#if defined(BORINGSSL_HAS_UINT128)
#define BORINGSSL_NISTP256_64BIT 1
#include "../../../third_party/fiat/p256_64.h"
@@ -75,10 +75,13 @@
#include <CCryptoBoringSSL_sha.h>
#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;
@@ -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;
@@ -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)
}
@@ -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
@@ -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
@@ -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;
@@ -52,7 +52,6 @@
#include <string.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_cpu.h>
#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
@@ -52,7 +52,6 @@
#include <CCryptoBoringSSL_base.h>
#include <CCryptoBoringSSL_aes.h>
#include <CCryptoBoringSSL_cpu.h>
#include <stdlib.h>
#include <string.h>
@@ -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]);
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,324 @@
#define BORINGSSL_PREFIX CCryptoBoringSSL
#if defined(__aarch64__) && defined(__APPLE__)
// This file is generated from a similarly-named Perl script in the BoringSSL
// source tree. Do not edit by hand.
#if !defined(__has_feature)
#define __has_feature(x) 0
#endif
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
#define OPENSSL_NO_ASM
#endif
#if !defined(OPENSSL_NO_ASM)
#if defined(BORINGSSL_PREFIX)
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
#endif
#include "CCryptoBoringSSL_arm_arch.h"
.text
.globl _beeu_mod_inverse_vartime
.private_extern _beeu_mod_inverse_vartime
.align 4
_beeu_mod_inverse_vartime:
// Reserve enough space for 14 8-byte registers on the stack
// in the first stp call for x29, x30.
// Then store the remaining callee-saved registers.
//
// | x29 | x30 | x19 | x20 | ... | x27 | x28 | x0 | x2 |
// ^ ^
// sp <------------------- 112 bytes ----------------> old sp
// x29 (FP)
//
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-112]!
add x29,sp,#0
stp x19,x20,[sp,#16]
stp x21,x22,[sp,#32]
stp x23,x24,[sp,#48]
stp x25,x26,[sp,#64]
stp x27,x28,[sp,#80]
stp x0,x2,[sp,#96]
// B = b3..b0 := a
ldp x25,x26,[x1]
ldp x27,x28,[x1,#16]
// n3..n0 := n
// Note: the value of input params are changed in the following.
ldp x0,x1,[x2]
ldp x2,x30,[x2,#16]
// A = a3..a0 := n
mov x21, x0
mov x22, x1
mov x23, x2
mov x24, x30
// X = x4..x0 := 1
mov x3, #1
eor x4, x4, x4
eor x5, x5, x5
eor x6, x6, x6
eor x7, x7, x7
// Y = y4..y0 := 0
eor x8, x8, x8
eor x9, x9, x9
eor x10, x10, x10
eor x11, x11, x11
eor x12, x12, x12
Lbeeu_loop:
// if B == 0, jump to .Lbeeu_loop_end
orr x14, x25, x26
orr x14, x14, x27
// reverse the bit order of x25. This is needed for clz after this macro
rbit x15, x25
orr x14, x14, x28
cbz x14,Lbeeu_loop_end
// 0 < B < |n|,
// 0 < A <= |n|,
// (1) X*a == B (mod |n|),
// (2) (-1)*Y*a == A (mod |n|)
// Now divide B by the maximum possible power of two in the
// integers, and divide X by the same value mod |n|.
// When we're done, (1) still holds.
// shift := number of trailing 0s in x25
// ( = number of leading 0s in x15; see the "rbit" instruction in TEST_B_ZERO)
clz x13, x15
// If there is no shift, goto shift_A_Y
cbz x13, Lbeeu_shift_A_Y
// Shift B right by "x13" bits
neg x14, x13
lsr x25, x25, x13
lsl x15, x26, x14
lsr x26, x26, x13
lsl x19, x27, x14
orr x25, x25, x15
lsr x27, x27, x13
lsl x20, x28, x14
orr x26, x26, x19
lsr x28, x28, x13
orr x27, x27, x20
// Shift X right by "x13" bits, adding n whenever X becomes odd.
// x13--;
// x14 := 0; needed in the addition to the most significant word in SHIFT1
eor x14, x14, x14
Lbeeu_shift_loop_X:
tbz x3, #0, Lshift1_0
adds x3, x3, x0
adcs x4, x4, x1
adcs x5, x5, x2
adcs x6, x6, x30
adc x7, x7, x14
Lshift1_0:
// var0 := [var1|var0]<64..1>;
// i.e. concatenate var1 and var0,
// extract bits <64..1> from the resulting 128-bit value
// and put them in var0
extr x3, x4, x3, #1
extr x4, x5, x4, #1
extr x5, x6, x5, #1
extr x6, x7, x6, #1
lsr x7, x7, #1
subs x13, x13, #1
bne Lbeeu_shift_loop_X
// Note: the steps above perform the same sequence as in p256_beeu-x86_64-asm.pl
// with the following differences:
// - "x13" is set directly to the number of trailing 0s in B
// (using rbit and clz instructions)
// - The loop is only used to call SHIFT1(X)
// and x13 is decreased while executing the X loop.
// - SHIFT256(B, x13) is performed before right-shifting X; they are independent
Lbeeu_shift_A_Y:
// Same for A and Y.
// Afterwards, (2) still holds.
// Reverse the bit order of x21
// x13 := number of trailing 0s in x21 (= number of leading 0s in x15)
rbit x15, x21
clz x13, x15
// If there is no shift, goto |B-A|, X+Y update
cbz x13, Lbeeu_update_B_X_or_A_Y
// Shift A right by "x13" bits
neg x14, x13
lsr x21, x21, x13
lsl x15, x22, x14
lsr x22, x22, x13
lsl x19, x23, x14
orr x21, x21, x15
lsr x23, x23, x13
lsl x20, x24, x14
orr x22, x22, x19
lsr x24, x24, x13
orr x23, x23, x20
// Shift Y right by "x13" bits, adding n whenever Y becomes odd.
// x13--;
// x14 := 0; needed in the addition to the most significant word in SHIFT1
eor x14, x14, x14
Lbeeu_shift_loop_Y:
tbz x8, #0, Lshift1_1
adds x8, x8, x0
adcs x9, x9, x1
adcs x10, x10, x2
adcs x11, x11, x30
adc x12, x12, x14
Lshift1_1:
// var0 := [var1|var0]<64..1>;
// i.e. concatenate var1 and var0,
// extract bits <64..1> from the resulting 128-bit value
// and put them in var0
extr x8, x9, x8, #1
extr x9, x10, x9, #1
extr x10, x11, x10, #1
extr x11, x12, x11, #1
lsr x12, x12, #1
subs x13, x13, #1
bne Lbeeu_shift_loop_Y
Lbeeu_update_B_X_or_A_Y:
// Try T := B - A; if cs, continue with B > A (cs: carry set = no borrow)
// Note: this is a case of unsigned arithmetic, where T fits in 4 64-bit words
// without taking a sign bit if generated. The lack of a carry would
// indicate a negative result. See, for example,
// https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/condition-codes-1-condition-flags-and-codes
subs x14, x25, x21
sbcs x15, x26, x22
sbcs x19, x27, x23
sbcs x20, x28, x24
bcs Lbeeu_B_greater_than_A
// Else A > B =>
// A := A - B; Y := Y + X; goto beginning of the loop
subs x21, x21, x25
sbcs x22, x22, x26
sbcs x23, x23, x27
sbcs x24, x24, x28
adds x8, x8, x3
adcs x9, x9, x4
adcs x10, x10, x5
adcs x11, x11, x6
adc x12, x12, x7
b Lbeeu_loop
Lbeeu_B_greater_than_A:
// Continue with B > A =>
// B := B - A; X := X + Y; goto beginning of the loop
mov x25, x14
mov x26, x15
mov x27, x19
mov x28, x20
adds x3, x3, x8
adcs x4, x4, x9
adcs x5, x5, x10
adcs x6, x6, x11
adc x7, x7, x12
b Lbeeu_loop
Lbeeu_loop_end:
// The Euclid's algorithm loop ends when A == gcd(a,n);
// this would be 1, when a and n are co-prime (i.e. do not have a common factor).
// Since (-1)*Y*a == A (mod |n|), Y>0
// then out = -Y mod n
// Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|)
// Is A-1 == 0?
// If not, fail.
sub x14, x21, #1
orr x14, x14, x22
orr x14, x14, x23
orr x14, x14, x24
cbnz x14, Lbeeu_err
// If Y>n ==> Y:=Y-n
Lbeeu_reduction_loop:
// x_i := y_i - n_i (X is no longer needed, use it as temp)
// (x14 = 0 from above)
subs x3, x8, x0
sbcs x4, x9, x1
sbcs x5, x10, x2
sbcs x6, x11, x30
sbcs x7, x12, x14
// If result is non-negative (i.e., cs = carry set = no borrow),
// y_i := x_i; goto reduce again
// else
// y_i := y_i; continue
csel x8, x3, x8, cs
csel x9, x4, x9, cs
csel x10, x5, x10, cs
csel x11, x6, x11, cs
csel x12, x7, x12, cs
bcs Lbeeu_reduction_loop
// Now Y < n (Y cannot be equal to n, since the inverse cannot be 0)
// out = -Y = n-Y
subs x8, x0, x8
sbcs x9, x1, x9
sbcs x10, x2, x10
sbcs x11, x30, x11
// Save Y in output (out (x0) was saved on the stack)
ldr x3, [sp,#96]
stp x8, x9, [x3]
stp x10, x11, [x3,#16]
// return 1 (success)
mov x0, #1
b Lbeeu_finish
Lbeeu_err:
// return 0 (error)
eor x0, x0, x0
Lbeeu_finish:
// Restore callee-saved registers, except x0, x2
add sp,x29,#0
ldp x19,x20,[sp,#16]
ldp x21,x22,[sp,#32]
ldp x23,x24,[sp,#48]
ldp x25,x26,[sp,#64]
ldp x27,x28,[sp,#80]
ldp x29,x30,[sp],#112
AARCH64_VALIDATE_LINK_REGISTER
ret
#endif // !OPENSSL_NO_ASM
#endif // defined(__aarch64__) && defined(__APPLE__)
#if defined(__linux__) && defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endif
@@ -0,0 +1,327 @@
#define BORINGSSL_PREFIX CCryptoBoringSSL
#if defined(__aarch64__) && defined(__linux__)
// This file is generated from a similarly-named Perl script in the BoringSSL
// source tree. Do not edit by hand.
#if !defined(__has_feature)
#define __has_feature(x) 0
#endif
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
#define OPENSSL_NO_ASM
#endif
#if !defined(OPENSSL_NO_ASM)
#if defined(__aarch64__)
#if defined(BORINGSSL_PREFIX)
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
#endif
#include "CCryptoBoringSSL_arm_arch.h"
.text
.globl beeu_mod_inverse_vartime
.hidden beeu_mod_inverse_vartime
.type beeu_mod_inverse_vartime, %function
.align 4
beeu_mod_inverse_vartime:
// Reserve enough space for 14 8-byte registers on the stack
// in the first stp call for x29, x30.
// Then store the remaining callee-saved registers.
//
// | x29 | x30 | x19 | x20 | ... | x27 | x28 | x0 | x2 |
// ^ ^
// sp <------------------- 112 bytes ----------------> old sp
// x29 (FP)
//
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-112]!
add x29,sp,#0
stp x19,x20,[sp,#16]
stp x21,x22,[sp,#32]
stp x23,x24,[sp,#48]
stp x25,x26,[sp,#64]
stp x27,x28,[sp,#80]
stp x0,x2,[sp,#96]
// B = b3..b0 := a
ldp x25,x26,[x1]
ldp x27,x28,[x1,#16]
// n3..n0 := n
// Note: the value of input params are changed in the following.
ldp x0,x1,[x2]
ldp x2,x30,[x2,#16]
// A = a3..a0 := n
mov x21, x0
mov x22, x1
mov x23, x2
mov x24, x30
// X = x4..x0 := 1
mov x3, #1
eor x4, x4, x4
eor x5, x5, x5
eor x6, x6, x6
eor x7, x7, x7
// Y = y4..y0 := 0
eor x8, x8, x8
eor x9, x9, x9
eor x10, x10, x10
eor x11, x11, x11
eor x12, x12, x12
.Lbeeu_loop:
// if B == 0, jump to .Lbeeu_loop_end
orr x14, x25, x26
orr x14, x14, x27
// reverse the bit order of x25. This is needed for clz after this macro
rbit x15, x25
orr x14, x14, x28
cbz x14,.Lbeeu_loop_end
// 0 < B < |n|,
// 0 < A <= |n|,
// (1) X*a == B (mod |n|),
// (2) (-1)*Y*a == A (mod |n|)
// Now divide B by the maximum possible power of two in the
// integers, and divide X by the same value mod |n|.
// When we're done, (1) still holds.
// shift := number of trailing 0s in x25
// ( = number of leading 0s in x15; see the "rbit" instruction in TEST_B_ZERO)
clz x13, x15
// If there is no shift, goto shift_A_Y
cbz x13, .Lbeeu_shift_A_Y
// Shift B right by "x13" bits
neg x14, x13
lsr x25, x25, x13
lsl x15, x26, x14
lsr x26, x26, x13
lsl x19, x27, x14
orr x25, x25, x15
lsr x27, x27, x13
lsl x20, x28, x14
orr x26, x26, x19
lsr x28, x28, x13
orr x27, x27, x20
// Shift X right by "x13" bits, adding n whenever X becomes odd.
// x13--;
// x14 := 0; needed in the addition to the most significant word in SHIFT1
eor x14, x14, x14
.Lbeeu_shift_loop_X:
tbz x3, #0, .Lshift1_0
adds x3, x3, x0
adcs x4, x4, x1
adcs x5, x5, x2
adcs x6, x6, x30
adc x7, x7, x14
.Lshift1_0:
// var0 := [var1|var0]<64..1>;
// i.e. concatenate var1 and var0,
// extract bits <64..1> from the resulting 128-bit value
// and put them in var0
extr x3, x4, x3, #1
extr x4, x5, x4, #1
extr x5, x6, x5, #1
extr x6, x7, x6, #1
lsr x7, x7, #1
subs x13, x13, #1
bne .Lbeeu_shift_loop_X
// Note: the steps above perform the same sequence as in p256_beeu-x86_64-asm.pl
// with the following differences:
// - "x13" is set directly to the number of trailing 0s in B
// (using rbit and clz instructions)
// - The loop is only used to call SHIFT1(X)
// and x13 is decreased while executing the X loop.
// - SHIFT256(B, x13) is performed before right-shifting X; they are independent
.Lbeeu_shift_A_Y:
// Same for A and Y.
// Afterwards, (2) still holds.
// Reverse the bit order of x21
// x13 := number of trailing 0s in x21 (= number of leading 0s in x15)
rbit x15, x21
clz x13, x15
// If there is no shift, goto |B-A|, X+Y update
cbz x13, .Lbeeu_update_B_X_or_A_Y
// Shift A right by "x13" bits
neg x14, x13
lsr x21, x21, x13
lsl x15, x22, x14
lsr x22, x22, x13
lsl x19, x23, x14
orr x21, x21, x15
lsr x23, x23, x13
lsl x20, x24, x14
orr x22, x22, x19
lsr x24, x24, x13
orr x23, x23, x20
// Shift Y right by "x13" bits, adding n whenever Y becomes odd.
// x13--;
// x14 := 0; needed in the addition to the most significant word in SHIFT1
eor x14, x14, x14
.Lbeeu_shift_loop_Y:
tbz x8, #0, .Lshift1_1
adds x8, x8, x0
adcs x9, x9, x1
adcs x10, x10, x2
adcs x11, x11, x30
adc x12, x12, x14
.Lshift1_1:
// var0 := [var1|var0]<64..1>;
// i.e. concatenate var1 and var0,
// extract bits <64..1> from the resulting 128-bit value
// and put them in var0
extr x8, x9, x8, #1
extr x9, x10, x9, #1
extr x10, x11, x10, #1
extr x11, x12, x11, #1
lsr x12, x12, #1
subs x13, x13, #1
bne .Lbeeu_shift_loop_Y
.Lbeeu_update_B_X_or_A_Y:
// Try T := B - A; if cs, continue with B > A (cs: carry set = no borrow)
// Note: this is a case of unsigned arithmetic, where T fits in 4 64-bit words
// without taking a sign bit if generated. The lack of a carry would
// indicate a negative result. See, for example,
// https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/condition-codes-1-condition-flags-and-codes
subs x14, x25, x21
sbcs x15, x26, x22
sbcs x19, x27, x23
sbcs x20, x28, x24
bcs .Lbeeu_B_greater_than_A
// Else A > B =>
// A := A - B; Y := Y + X; goto beginning of the loop
subs x21, x21, x25
sbcs x22, x22, x26
sbcs x23, x23, x27
sbcs x24, x24, x28
adds x8, x8, x3
adcs x9, x9, x4
adcs x10, x10, x5
adcs x11, x11, x6
adc x12, x12, x7
b .Lbeeu_loop
.Lbeeu_B_greater_than_A:
// Continue with B > A =>
// B := B - A; X := X + Y; goto beginning of the loop
mov x25, x14
mov x26, x15
mov x27, x19
mov x28, x20
adds x3, x3, x8
adcs x4, x4, x9
adcs x5, x5, x10
adcs x6, x6, x11
adc x7, x7, x12
b .Lbeeu_loop
.Lbeeu_loop_end:
// The Euclid's algorithm loop ends when A == gcd(a,n);
// this would be 1, when a and n are co-prime (i.e. do not have a common factor).
// Since (-1)*Y*a == A (mod |n|), Y>0
// then out = -Y mod n
// Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|)
// Is A-1 == 0?
// If not, fail.
sub x14, x21, #1
orr x14, x14, x22
orr x14, x14, x23
orr x14, x14, x24
cbnz x14, .Lbeeu_err
// If Y>n ==> Y:=Y-n
.Lbeeu_reduction_loop:
// x_i := y_i - n_i (X is no longer needed, use it as temp)
// (x14 = 0 from above)
subs x3, x8, x0
sbcs x4, x9, x1
sbcs x5, x10, x2
sbcs x6, x11, x30
sbcs x7, x12, x14
// If result is non-negative (i.e., cs = carry set = no borrow),
// y_i := x_i; goto reduce again
// else
// y_i := y_i; continue
csel x8, x3, x8, cs
csel x9, x4, x9, cs
csel x10, x5, x10, cs
csel x11, x6, x11, cs
csel x12, x7, x12, cs
bcs .Lbeeu_reduction_loop
// Now Y < n (Y cannot be equal to n, since the inverse cannot be 0)
// out = -Y = n-Y
subs x8, x0, x8
sbcs x9, x1, x9
sbcs x10, x2, x10
sbcs x11, x30, x11
// Save Y in output (out (x0) was saved on the stack)
ldr x3, [sp,#96]
stp x8, x9, [x3]
stp x10, x11, [x3,#16]
// return 1 (success)
mov x0, #1
b .Lbeeu_finish
.Lbeeu_err:
// return 0 (error)
eor x0, x0, x0
.Lbeeu_finish:
// Restore callee-saved registers, except x0, x2
add sp,x29,#0
ldp x19,x20,[sp,#16]
ldp x21,x22,[sp,#32]
ldp x23,x24,[sp,#48]
ldp x25,x26,[sp,#64]
ldp x27,x28,[sp,#80]
ldp x29,x30,[sp],#112
AARCH64_VALIDATE_LINK_REGISTER
ret
.size beeu_mod_inverse_vartime,.-beeu_mod_inverse_vartime
#endif
#endif // !OPENSSL_NO_ASM
.section .note.GNU-stack,"",%progbits
#endif // defined(__aarch64__) && defined(__linux__)
#if defined(__linux__) && defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endif
@@ -16,7 +16,6 @@
#define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
#include <CCryptoBoringSSL_aes.h>
#include <CCryptoBoringSSL_cpu.h>
#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
@@ -23,7 +23,6 @@
#endif
#include <CCryptoBoringSSL_chacha.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_type_check.h>
@@ -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
@@ -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
@@ -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;
@@ -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;
@@ -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) {
File diff suppressed because it is too large. Load diff
@@ -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
@@ -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
@@ -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:
@@ -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:
+2 -2
View File
@@ -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,
+52 -15
View File
@@ -20,7 +20,6 @@
#include <stdlib.h>
#include <CCryptoBoringSSL_bn.h>
#include <CCryptoBoringSSL_cpu.h>
#include <CCryptoBoringSSL_hmac.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_rand.h>
@@ -39,8 +38,7 @@
#include <emmintrin.h>
#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 <arm_neon.h>
#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;
@@ -930,6 +927,20 @@ struct poly {
#endif
};
// poly_normalize zeros out the excess elements of |x| which are included only
// for alignment.
static void poly_normalize(struct poly *x) {
OPENSSL_memset(&x->v[N], 0, 3 * sizeof(uint16_t));
}
// poly_assert_normalized asserts that the excess elements of |x| are zeroed out
// for the cases that case. (E.g. |poly_mul_vec|.)
static void poly_assert_normalized(const struct poly *x) {
assert(x->v[N] == 0);
assert(x->v[N + 1] == 0);
assert(x->v[N + 2] == 0);
}
OPENSSL_UNUSED static void poly_print(const struct poly *p) {
printf("[");
for (unsigned i = 0; i < N; i++) {
@@ -1216,13 +1227,12 @@ static void poly_mul_vec_aux(vec_t *restrict out, vec_t *restrict scratch,
// poly_mul_vec sets |*out| to |x|×|y| mod (𝑥^n - 1).
static void poly_mul_vec(struct POLY_MUL_SCRATCH *scratch, struct poly *out,
const struct poly *x, const struct poly *y) {
OPENSSL_memset((uint16_t *)&x->v[N], 0, 3 * sizeof(uint16_t));
OPENSSL_memset((uint16_t *)&y->v[N], 0, 3 * sizeof(uint16_t));
OPENSSL_STATIC_ASSERT(sizeof(out->v) == sizeof(vec_t) * VECS_PER_POLY,
"struct poly is the wrong size");
OPENSSL_STATIC_ASSERT(alignof(struct poly) == alignof(vec_t),
"struct poly has incorrect alignment");
poly_assert_normalized(x);
poly_assert_normalized(y);
vec_t *const prod = scratch->u.vec.prod;
vec_t *const aux_scratch = scratch->u.vec.scratch;
@@ -1318,22 +1328,24 @@ 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;
}
poly_normalize(r);
} else
#endif
#if defined(HRSS_HAVE_VECTOR_UNIT)
if (vec_capable()) {
poly_mul_vec(scratch, r, a, b);
return;
}
} else
#endif
// Fallback, non-vector case.
poly_mul_novec(scratch, r, a, b);
{
poly_mul_novec(scratch, r, a, b);
}
poly_assert_normalized(r);
}
// poly_mul_x_minus_1 sets |p| to |p|×(𝑥 - 1) mod (𝑥^n - 1).
@@ -1498,6 +1510,8 @@ static void poly_from_poly2(struct poly *out, const struct poly2 *in) {
shift = 0;
}
}
poly_normalize(out);
}
static void poly_from_poly3(struct poly *out, const struct poly3 *in) {
@@ -1522,6 +1536,8 @@ static void poly_from_poly3(struct poly *out, const struct poly3 *in) {
shift = 0;
}
}
poly_normalize(out);
}
// Polynomial inversion
@@ -1575,6 +1591,7 @@ static void poly_invert_mod2(struct poly *out, const struct poly *in) {
assert(f.v[0] & 1);
poly2_reverse_700(&v, &v);
poly_from_poly2(out, &v);
poly_assert_normalized(out);
}
// poly_invert sets |*out| to |in^-1| (i.e. such that |*out|×|in| = 1 mod Φ(N)).
@@ -1588,6 +1605,7 @@ static void poly_invert(struct POLY_MUL_SCRATCH *scratch, struct poly *out,
for (unsigned i = 0; i < N; i++) {
a.v[i] = -in->v[i];
}
poly_normalize(&a);
// b = in^-1 mod 2.
b = out;
@@ -1600,6 +1618,8 @@ static void poly_invert(struct POLY_MUL_SCRATCH *scratch, struct poly *out,
tmp.v[0] += 2;
poly_mul(scratch, b, b, &tmp);
}
poly_assert_normalized(out);
}
// Marshal and unmarshal functions for various basic types.
@@ -1689,6 +1709,7 @@ static int poly_unmarshal(struct poly *out, const uint8_t in[POLY_BYTES]) {
}
out->v[N - 1] = (uint16_t)(0u - sum);
poly_normalize(out);
return 1;
}
@@ -1740,6 +1761,7 @@ static void poly_short_sample(struct poly *out,
out->v[i] = v;
}
out->v[N - 1] = 0;
poly_normalize(out);
}
// poly_short_sample_plus performs the T+ sample as defined in [HRSSNIST],
@@ -1762,6 +1784,7 @@ static void poly_short_sample_plus(struct poly *out,
for (unsigned i = 0; i < N; i += 2) {
out->v[i] = (unsigned) out->v[i] * scale;
}
poly_assert_normalized(out);
}
// poly_lift computes the function discussed in [HRSS], appendix B.
@@ -1877,6 +1900,7 @@ static void poly_lift(struct poly *out, const struct poly *a) {
}
poly_mul_x_minus_1(out);
poly_normalize(out);
}
struct public_key {
@@ -1956,6 +1980,10 @@ int HRSS_generate_key(
return 0;
}
#if !defined(NDEBUG)
OPENSSL_memset(vars, 0xff, sizeof(struct vars));
#endif
OPENSSL_memcpy(priv->hmac_key, in + 2 * HRSS_SAMPLE_BYTES,
sizeof(priv->hmac_key));
@@ -2015,6 +2043,10 @@ int HRSS_encap(uint8_t out_ciphertext[POLY_BYTES], uint8_t out_shared_key[32],
return 0;
}
#if !defined(NDEBUG)
OPENSSL_memset(vars, 0xff, sizeof(struct vars));
#endif
poly_short_sample(&vars->m, in);
poly_short_sample(&vars->r, in + HRSS_SAMPLE_BYTES);
poly_lift(&vars->m_lifted, &vars->m);
@@ -2072,6 +2104,10 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
return 0;
}
#if !defined(NDEBUG)
OPENSSL_memset(vars, 0xff, sizeof(struct vars));
#endif
// This is HMAC, expanded inline rather than using the |HMAC| function so that
// we can avoid dealing with possible allocation failures and so keep this
// function infallible.
@@ -2122,6 +2158,7 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
for (unsigned i = 0; i < N; i++) {
vars->r.v[i] = vars->c.v[i] - vars->m_lifted.v[i];
}
poly_normalize(&vars->r);
poly_mul(&vars->scratch, &vars->r, &vars->r, &priv->ph_inverse);
poly_mod_phiN(&vars->r);
poly_clamp(&vars->r);
+306 -9
View File
@@ -121,11 +121,23 @@
#include <valgrind/memcheck.h>
#endif
#if defined(BORINGSSL_FIPS_BREAK_TESTS)
#include <stdlib.h>
#endif
#if !defined(__cplusplus)
#if defined(_MSC_VER)
#if defined(_MSC_VER) && !defined(__clang__)
#define alignas(x) __declspec(align(x))
#define alignof __alignof
#else
// With the exception of MSVC, we require C11 to build the library. C11 is a
// prerequisite for improved refcounting performance. All our supported C
// compilers have long implemented C11 and made it default. The most likely
// cause of pre-C11 modes is stale -std=c99 or -std=gnu99 flags in build
// configuration. Such flags can be removed.
#if __STDC_VERSION__ < 201112L
#error "BoringSSL must be built in C11 mode or higher."
#endif
#include <stdalign.h>
#endif
#endif
@@ -932,19 +944,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 +995,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
+7 -1
View File
@@ -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) {
@@ -180,11 +180,17 @@ void OPENSSL_free(void *orig_ptr) {
size_t size = *(size_t *)ptr;
OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
// ASan knows to intercept malloc and free, but not sdallocx.
#if defined(OPENSSL_ASAN)
free(ptr);
#else
if (sdallocx) {
sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
} else {
free(ptr);
}
#endif
}
void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
@@ -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)
@@ -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;
}
@@ -993,8 +993,8 @@ int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
// add_bag_attributes adds the bagAttributes field of a SafeBag structure,
// containing the specified friendlyName and localKeyId attributes.
static int add_bag_attributes(CBB *bag, const char *name, const uint8_t *key_id,
size_t key_id_len) {
static int add_bag_attributes(CBB *bag, const char *name, size_t name_len,
const uint8_t *key_id, size_t key_id_len) {
if (name == NULL && key_id_len == 0) {
return 1; // Omit the OPTIONAL SET.
}
@@ -1003,7 +1003,7 @@ static int add_bag_attributes(CBB *bag, const char *name, const uint8_t *key_id,
if (!CBB_add_asn1(bag, &attrs, CBS_ASN1_SET)) {
return 0;
}
if (name != NULL) {
if (name_len != 0) {
// See https://tools.ietf.org/html/rfc2985, section 5.5.1.
if (!CBB_add_asn1(&attrs, &attr, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1(&attr, &oid, CBS_ASN1_OBJECT) ||
@@ -1014,7 +1014,7 @@ static int add_bag_attributes(CBB *bag, const char *name, const uint8_t *key_id,
}
// Convert the friendly name to a BMPString.
CBS name_cbs;
CBS_init(&name_cbs, (const uint8_t *)name, strlen(name));
CBS_init(&name_cbs, (const uint8_t *)name, name_len);
while (CBS_len(&name_cbs) != 0) {
uint32_t c;
if (!cbs_get_utf8(&name_cbs, &c) ||
@@ -1059,10 +1059,24 @@ static int add_cert_bag(CBB *cbb, X509 *cert, const char *name,
}
uint8_t *buf;
int len = i2d_X509(cert, NULL);
int int_name_len = 0;
const char *cert_name = (const char *)X509_alias_get0(cert, &int_name_len);
size_t name_len = int_name_len;
if (name) {
if (name_len != 0) {
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_AMBIGUOUS_FRIENDLY_NAME);
return 0;
}
name_len = strlen(name);
} else {
name = cert_name;
}
if (len < 0 ||
!CBB_add_space(&cert_value, &buf, (size_t)len) ||
i2d_X509(cert, &buf) < 0 ||
!add_bag_attributes(&bag, name, key_id, key_id_len) ||
!add_bag_attributes(&bag, name, name_len, key_id, key_id_len) ||
!CBB_flush(cbb)) {
return 0;
}
@@ -1323,7 +1337,11 @@ PKCS12 *PKCS12_create(const char *password, const char *name,
goto err;
}
}
if (!add_bag_attributes(&bag, name, key_id, key_id_len) ||
size_t name_len = 0;
if (name) {
name_len = strlen(name);
}
if (!add_bag_attributes(&bag, name, name_len, key_id, key_id_len) ||
!CBB_flush(&content_infos)) {
goto err;
}
@@ -20,8 +20,6 @@
#include <string.h>
#include <CCryptoBoringSSL_cpu.h>
#include "internal.h"
#include "../internal.h"
@@ -206,6 +204,11 @@ void CRYPTO_poly1305_update(poly1305_state *statep, const uint8_t *in,
size_t in_len) {
struct poly1305_state_st *state = poly1305_aligned_state(statep);
// Work around a C language bug. See https://crbug.com/1019588.
if (in_len == 0) {
return;
}
#if defined(OPENSSL_POLY1305_NEON)
if (CRYPTO_is_NEON_capable()) {
CRYPTO_poly1305_update_neon(statep, in, in_len);
@@ -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];
};
+8 -1
View File
@@ -19,6 +19,8 @@
#include <CCryptoBoringSSL_bytestring.h>
#include <CCryptoBoringSSL_mem.h>
#include <CCryptoBoringSSL_rand.h>
#include <CCryptoBoringSSL_siphash.h>
#include <CCryptoBoringSSL_thread.h>
#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);
@@ -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
+23 -6
View File
@@ -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;
}
@@ -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;
@@ -155,7 +156,7 @@ struct x509_st {
STACK_OF(DIST_POINT) *crldp;
STACK_OF(GENERAL_NAME) *altname;
NAME_CONSTRAINTS *nc;
unsigned char sha1_hash[SHA_DIGEST_LENGTH];
unsigned char cert_hash[SHA256_DIGEST_LENGTH];
X509_CERT_AUX *aux;
CRYPTO_BUFFER *buf;
CRYPTO_MUTEX lock;
@@ -218,7 +219,7 @@ struct X509_crl_st {
// CRL and base CRL numbers for delta processing
ASN1_INTEGER *crl_number;
ASN1_INTEGER *base_crl_number;
unsigned char sha1_hash[SHA_DIGEST_LENGTH];
unsigned char crl_hash[SHA256_DIGEST_LENGTH];
STACK_OF(GENERAL_NAMES) *issuers;
const X509_CRL_METHOD *meth;
void *meth_data;
@@ -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. */
+6 -11
View File
@@ -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)" : "";
@@ -323,9 +322,7 @@ int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
{
char *s, *c, *b;
int ret = 0, l, i;
l = 80 - 2 - obase;
int ret = 0, i;
b = X509_NAME_oneline(name, NULL, 0);
if (!b)
@@ -352,12 +349,10 @@ int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
if (BIO_write(bp, ", ", 2) != 2)
goto err;
}
l--;
}
if (*s == '\0')
break;
s++;
l--;
}
ret = 1;
@@ -101,7 +101,7 @@ int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
{
return OPENSSL_memcmp(a->sha1_hash, b->sha1_hash, 20);
return OPENSSL_memcmp(a->crl_hash, b->crl_hash, SHA256_DIGEST_LENGTH);
}
X509_NAME *X509_get_issuer_name(const X509 *a)
@@ -154,7 +154,7 @@ unsigned long X509_subject_name_hash_old(X509 *x)
*/
int X509_cmp(const X509 *a, const X509 *b)
{
/* Fill in the |sha1_hash| fields.
/* Fill in the |cert_hash| fields.
*
* TODO(davidben): This may fail, in which case the the hash will be all
* zeros. This produces a consistent comparison (failures are sticky), but
@@ -165,7 +165,7 @@ int X509_cmp(const X509 *a, const X509 *b)
x509v3_cache_extensions((X509 *)a);
x509v3_cache_extensions((X509 *)b);
int rv = OPENSSL_memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
int rv = OPENSSL_memcmp(a->cert_hash, b->cert_hash, SHA256_DIGEST_LENGTH);
if (rv)
return rv;
/* Check for match against stored encoding too */
+30 -52
View File
@@ -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)
@@ -71,7 +71,6 @@ static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
static int obj_trust(int id, X509 *x, int flags);
static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
/*
* WARNING: the following table should be kept in order of trust and without
@@ -106,14 +105,6 @@ static int tr_cmp(const X509_TRUST **a, const X509_TRUST **b)
return (*a)->trust - (*b)->trust;
}
int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
int) {
int (*oldtrust) (int, X509 *, int);
oldtrust = default_trust;
default_trust = trust;
return oldtrust;
}
int X509_check_trust(X509 *x, int id, int flags)
{
X509_TRUST *pt;
@@ -130,7 +121,7 @@ int X509_check_trust(X509 *x, int id, int flags)
}
idx = X509_TRUST_get_by_id(id);
if (idx == -1)
return default_trust(id, x, flags);
return obj_trust(id, x, flags);
pt = X509_TRUST_get0(idx);
return pt->check_trust(pt, x, flags);
}
+1 -1
View File
@@ -251,7 +251,7 @@ static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
break;
case ASN1_OP_D2I_POST:
if (!X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL)) {
if (!X509_CRL_digest(crl, EVP_sha256(), crl->crl_hash, NULL)) {
return 0;
}
+18 -13
View File
@@ -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)
@@ -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|
@@ -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;
}
}
@@ -437,7 +437,7 @@ int x509v3_cache_extensions(X509 *x)
return (x->ex_flags & EXFLAG_INVALID) == 0;
}
if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
if (!X509_digest(x, EVP_sha256(), x->cert_hash, NULL))
x->ex_flags |= EXFLAG_INVALID;
/* V1 should mean no extensions ... */
if (X509_get_version(x) == X509_VERSION_1)
+24 -23
View File
@@ -63,6 +63,7 @@
#include <string.h>
#include <CCryptoBoringSSL_bn.h>
#include <CCryptoBoringSSL_bytestring.h>
#include <CCryptoBoringSSL_conf.h>
#include <CCryptoBoringSSL_err.h>
#include <CCryptoBoringSSL_mem.h>
@@ -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)
+1 -1
View File
@@ -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 f961de5c47ed265c3e758ec70dd15ece20809962
Loaded 100 of 145 files, more files were not shown because too many files have changed in this diff. Show more