7851 Commits

Author SHA1 Message Date
Nabil Wadih b0a026f854 add rust bindings for ed25519
Change-Id: I7458b1d7aa1736d586dc80660d59c07fa2ac1c8a
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59805
Reviewed-by: Bob Beck <bbe@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-26 20:55:21 +00:00
David Benjamin e30750c9f4 Add an ABI test for x25519_NEON
Change-Id: I84dd6750276b334bf3349b4592fee6227403580c
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60145
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-26 18:46:26 +00:00
David Benjamin da757e6010 Add constant-time validation for curve25519
Also add some tests for X25519_public_from_private, as we apparently
weren't directly testing it with test vectors.

Change-Id: I1b73a9655323d507a8e022c62530ddd4610db4b9
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60109
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-26 18:29:59 +00:00
Ilya Tokar 5fcd47d137 Add prefetch to aes_hw_ctr32_encrypt_blocks
Similar idea to https://boringssl-review.googlesource.com/c/boringssl/+/55466

Results are pretty close to the current state, AMD (rome):
BM_Encrypt/64/0          344ns ± 3%   343ns ± 1%    ~     (p=0.728 n=20+19)
BM_Encrypt/64/1          394ns ± 2%   394ns ± 3%    ~     (p=0.919 n=18+20)
BM_Encrypt/64/8          391ns ± 1%   390ns ± 2%    ~     (p=0.165 n=17+19)
BM_Encrypt/64/64         342ns ± 3%   341ns ± 2%    ~     (p=0.686 n=19+19)
BM_Encrypt/64/97         393ns ± 1%   394ns ± 3%    ~     (p=0.639 n=17+19)
BM_Encrypt/512/0         437ns ± 2%   437ns ± 1%    ~     (p=0.819 n=20+19)
BM_Encrypt/512/1         566ns ± 1%   551ns ± 3%  -2.65%  (p=0.000 n=18+18)
BM_Encrypt/512/8         563ns ± 2%   555ns ± 4%  -1.48%  (p=0.003 n=18+20)
BM_Encrypt/512/64        434ns ± 3%   439ns ± 3%  +1.03%  (p=0.008 n=19+20)
BM_Encrypt/512/97        565ns ± 2%   555ns ± 4%  -1.88%  (p=0.001 n=18+20)
BM_Encrypt/4k/0         1.03µs ± 2%  0.99µs ± 2%  -4.29%  (p=0.000 n=20+20)
BM_Encrypt/4k/1         1.18µs ± 3%  1.11µs ± 3%  -5.66%  (p=0.000 n=20+20)
BM_Encrypt/4k/8         1.17µs ± 3%  1.11µs ± 2%  -5.51%  (p=0.000 n=20+20)
BM_Encrypt/4k/64        1.03µs ± 1%  0.99µs ± 1%  -4.08%  (p=0.000 n=19+19)
BM_Encrypt/4k/97        1.17µs ± 3%  1.11µs ± 2%  -5.65%  (p=0.000 n=20+19)
BM_Encrypt/32k/0        5.26µs ± 1%  5.19µs ± 2%  -1.29%  (p=0.000 n=19+20)
BM_Encrypt/32k/1        5.49µs ± 2%  5.38µs ± 1%  -2.01%  (p=0.000 n=20+20)
BM_Encrypt/32k/8        5.45µs ± 2%  5.34µs ± 1%  -2.12%  (p=0.000 n=20+19)
BM_Encrypt/32k/64       5.28µs ± 1%  5.19µs ± 1%  -1.66%  (p=0.000 n=19+20)
BM_Encrypt/32k/97       5.49µs ± 1%  5.38µs ± 1%  -2.02%  (p=0.000 n=20+17)
BM_Encrypt/256k/0       38.9µs ± 1%  38.5µs ± 2%  -1.09%  (p=0.000 n=20+20)
BM_Encrypt/256k/1       40.3µs ± 2%  39.6µs ± 1%  -1.74%  (p=0.000 n=20+20)
BM_Encrypt/256k/8       39.7µs ± 2%  39.0µs ± 1%  -1.82%  (p=0.000 n=19+18)
BM_Encrypt/256k/64      38.9µs ± 1%  38.4µs ± 1%  -1.35%  (p=0.000 n=20+18)
BM_Encrypt/256k/97      40.1µs ± 1%  39.6µs ± 1%  -1.32%  (p=0.000 n=20+20)
BM_Encrypt/1M/0          154µs ± 1%   153µs ± 1%  -0.62%  (p=0.001 n=17+18)
BM_Encrypt/1M/1          160µs ± 2%   158µs ± 1%  -1.44%  (p=0.000 n=19+20)
BM_Encrypt/1M/8          158µs ± 1%   155µs ± 1%  -1.62%  (p=0.000 n=20+19)
BM_Encrypt/1M/64         155µs ± 2%   153µs ± 1%  -1.48%  (p=0.000 n=20+20)
BM_Encrypt/1M/97         160µs ± 1%   158µs ± 2%  -1.46%  (p=0.000 n=20+20)
BM_EncryptCord/1/0       310ns ± 3%   307ns ± 4%    ~     (p=0.101 n=19+20)

Intel (skylake):

BM_Encrypt/64/0          326ns ± 5%   325ns ± 4%    ~     (p=0.817 n=16+17)
BM_Encrypt/64/1          368ns ± 2%   387ns ±13%    ~     (p=0.845 n=17+20)
BM_Encrypt/64/8          385ns ±14%   365ns ± 3%  -5.12%  (p=0.013 n=20+18)
BM_Encrypt/64/64         325ns ± 4%   325ns ± 6%    ~     (p=0.621 n=18+16)
BM_Encrypt/64/97         367ns ± 3%   366ns ± 3%    ~     (p=0.963 n=18+18)
BM_Encrypt/512/0         504ns ± 4%   456ns ± 3%  -9.52%  (p=0.000 n=17+20)
BM_Encrypt/512/1         568ns ± 2%   528ns ± 4%  -7.09%  (p=0.000 n=15+17)
BM_Encrypt/512/8         580ns ± 3%   541ns ± 4%  -6.66%  (p=0.000 n=20+17)
BM_Encrypt/512/64        500ns ± 3%   454ns ± 4%  -9.26%  (p=0.000 n=17+17)
BM_Encrypt/512/97        564ns ± 2%   526ns ± 4%  -6.82%  (p=0.000 n=18+17)
BM_Encrypt/4k/0         1.26µs ± 2%  1.23µs ± 5%  -2.77%  (p=0.000 n=19+18)
BM_Encrypt/4k/1         1.33µs ± 2%  1.28µs ± 3%  -4.34%  (p=0.000 n=18+18)
BM_Encrypt/4k/8         1.35µs ± 3%  1.29µs ± 3%  -4.31%  (p=0.000 n=19+17)
BM_Encrypt/4k/64        1.27µs ± 3%  1.23µs ± 4%  -3.32%  (p=0.000 n=18+18)
BM_Encrypt/4k/97        1.34µs ± 3%  1.29µs ± 3%  -3.98%  (p=0.000 n=18+16)
BM_Encrypt/32k/0        8.24µs ± 4%  7.99µs ± 5%  -3.00%  (p=0.001 n=17+16)
BM_Encrypt/32k/1        8.23µs ± 2%  7.99µs ± 5%  -2.95%  (p=0.000 n=17+16)
BM_Encrypt/32k/8        8.64µs ±15%  8.05µs ± 5%  -6.92%  (p=0.000 n=20+18)
BM_Encrypt/32k/64       8.14µs ± 3%  7.96µs ± 3%  -2.23%  (p=0.000 n=18+17)
BM_Encrypt/32k/97       8.72µs ±14%  8.01µs ± 4%  -8.20%  (p=0.000 n=20+17)
BM_Encrypt/256k/0       63.2µs ± 4%  61.7µs ± 3%  -2.35%  (p=0.003 n=19+18)
BM_Encrypt/256k/1       63.5µs ± 4%  61.8µs ± 3%  -2.75%  (p=0.000 n=17+19)
BM_Encrypt/256k/8       63.6µs ± 9%  61.0µs ± 1%  -4.08%  (p=0.000 n=18+16)
BM_Encrypt/256k/64      63.1µs ± 3%  61.5µs ± 5%  -2.60%  (p=0.001 n=18+16)
BM_Encrypt/256k/97      65.6µs ±16%  61.6µs ± 4%  -6.09%  (p=0.000 n=19+17)
BM_Encrypt/1M/0          253µs ± 5%   246µs ± 5%  -2.88%  (p=0.001 n=19+19)
BM_Encrypt/1M/1          253µs ± 6%   244µs ± 1%  -3.71%  (p=0.000 n=16+17)
BM_Encrypt/1M/8          254µs ± 5%   244µs ± 3%  -4.15%  (p=0.000 n=18+18)
BM_Encrypt/1M/64         253µs ± 4%   245µs ± 4%  -3.10%  (p=0.000 n=19+19)
BM_Encrypt/1M/97         267µs ±14%   246µs ± 4%  -8.13%  (p=0.000 n=20+18)

But on AMD with prefetchers disabled and large enough data size,
to force cache misses this gives >2x improvement:
BM_Encrypt/64/0          342ns ± 1%   336ns ± 1%   -1.63%  (p=0.000 n=19+19)
BM_Encrypt/64/1          485ns ± 2%   484ns ± 2%     ~     (p=0.396 n=19+20)
BM_Encrypt/64/8          490ns ± 1%   488ns ± 2%     ~     (p=0.098 n=18+19)
BM_Encrypt/64/64         340ns ± 2%   335ns ± 1%   -1.50%  (p=0.000 n=19+19)
BM_Encrypt/64/97         483ns ± 1%   483ns ± 1%     ~     (p=0.912 n=16+20)
BM_Encrypt/512/0         566ns ± 3%   521ns ± 2%   -7.99%  (p=0.000 n=18+20)
BM_Encrypt/512/1         744ns ± 2%   667ns ± 1%  -10.31%  (p=0.000 n=20+20)
BM_Encrypt/512/8         745ns ± 1%   666ns ± 1%  -10.53%  (p=0.000 n=18+20)
BM_Encrypt/512/64        566ns ± 3%   520ns ± 2%   -8.05%  (p=0.000 n=17+19)
BM_Encrypt/512/97        740ns ± 1%   666ns ± 1%   -9.92%  (p=0.000 n=18+19)
BM_Encrypt/4k/0         2.50µs ± 1%  1.35µs ± 1%  -45.82%  (p=0.000 n=19+19)
BM_Encrypt/4k/1         2.65µs ± 3%  1.50µs ± 1%  -43.50%  (p=0.000 n=19+19)
BM_Encrypt/4k/8         2.66µs ± 1%  1.49µs ± 1%  -43.71%  (p=0.000 n=19+19)
BM_Encrypt/4k/64        2.47µs ± 4%  1.36µs ± 1%  -45.05%  (p=0.000 n=20+20)
BM_Encrypt/4k/97        2.66µs ± 1%  1.50µs ± 2%  -43.54%  (p=0.000 n=18+19)
BM_Encrypt/32k/0        18.0µs ± 1%   8.0µs ± 1%  -55.38%  (p=0.000 n=18+19)
BM_Encrypt/32k/1        18.2µs ± 1%   8.2µs ± 1%  -54.91%  (p=0.000 n=14+20)
BM_Encrypt/32k/8        18.2µs ± 1%   8.2µs ± 1%  -54.93%  (p=0.000 n=19+18)
BM_Encrypt/32k/64       18.0µs ± 1%   8.0µs ± 1%  -55.35%  (p=0.000 n=16+20)
BM_Encrypt/32k/97       18.1µs ± 3%   8.2µs ± 1%  -54.84%  (p=0.000 n=20+19)
BM_Encrypt/256k/0        148µs ± 1%    63µs ± 1%  -57.59%  (p=0.000 n=18+19)
BM_Encrypt/256k/1        150µs ± 1%    63µs ± 1%  -57.78%  (p=0.000 n=16+20)
BM_Encrypt/256k/8        147µs ± 5%    63µs ± 1%  -56.95%  (p=0.000 n=20+20)
BM_Encrypt/256k/64       148µs ± 2%    63µs ± 1%  -57.40%  (p=0.000 n=18+20)
BM_Encrypt/256k/97       146µs ± 4%    63µs ± 1%  -56.82%  (p=0.000 n=20+19)
BM_Encrypt/1M/0          595µs ± 1%   254µs ± 1%  -57.33%  (p=0.000 n=19+20)
BM_Encrypt/1M/1          590µs ± 4%   255µs ± 1%  -56.78%  (p=0.000 n=20+20)
BM_Encrypt/1M/8          593µs ± 2%   254µs ± 1%  -57.10%  (p=0.000 n=18+19)
BM_Encrypt/1M/64         595µs ± 1%   254µs ± 1%  -57.34%  (p=0.000 n=16+19)
BM_Encrypt/1M/97         589µs ± 4%   255µs ± 1%  -56.74%  (p=0.000 n=20+20)

Change-Id: I13c783ad261093009b2aa5ff56ce569f45ed3300
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60027
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2023-05-24 19:12:59 +00:00
David Benjamin 825bec8c88 Remove variable expansion from CONF fuzzer
CONF supports a variable expansion feature, which allows a config file
to easily grow exponentially.

2d05568a7b (upstream's
6a6213556a80ab0a9eb926a1d6023b8bf44f2afd) capped the expansion to 65536
bytes, but this still allows a small input to produce a fairly large
output. (Once we have one large value, it only takes five characters
make a new key that references it, which is an expansion factor of
around 13,000.) This, combined with the string-based extensions
machinery's many quadratic behaviors (see
b92fcfdc17), means fuzzers hit timeouts.

We have very few uses of these APIs left, and none of them use this
feature. Just remove it. While none of these super-linear behaviors are
DoS bugs per se (they should never be passed untrusted input), there's
no sense in carrying an unused feature that only frustrates the fuzzers.

Update-Note: NCONF_load and NCONF_load_bio no longer support the $foo
variable expansion syntax. If you are using these functions in your
project, remove them.

Bug: oss-fuzz:59049
Change-Id: I85832ae1d05373ee420f4fb68197786736ca8272
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60105
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-24 17:25:03 +00:00
David Benjamin f31b45dbd1 Remove some unreachable character types in the CONF parser
DQUOTE and FCOMMENT are remnants of a second parser in OpenSSL. OpenSSL
has CONF_type_default, which is the one we imported, and CONF_type_win32
which is a different syntax which looks to be modeled after Windows ini
files. DQUOTE and FCOMMENT only exist in this one.

Change-Id: Iffa01fcb012b0f3e7f8dbf87a01ba867bdc7bcc7
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60087
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-24 17:17:14 +00:00
David Benjamin 5aefcba15a Remove some unnecessary NULL checks in conf.c.
This file needs more work, but apply some small, local simplifications.

Change-Id: Ia2b93f847e67ae7738afc791acb9ce3cc6cc0342
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60086
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-24 16:56:34 +00:00
David Benjamin 83a6ba1d9a Test the CONF parser more extensively
Change-Id: I8792f118d281bc7a407dfbabe1c8b8e63f9eed9f
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60085
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-24 16:27:06 +00:00
David Benjamin 7e56051791 Miscellaneous size_t truncation fixes
Bug: 516
Change-Id: I3cc7e85687a29201a325b498eecf3694e0429ebc
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60067
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-23 17:28:55 +00:00
David Benjamin 89a6253757 Add int casts as needed around STACK_OF(T) sizes and indices
We now ensure STACK_OF(T) sizes and indices fit in INT_MAX, so it's safe
to cast to int.

Bug: 516
Change-Id: I33dd1de6d60a852d510b9b5c3ac70e2eacbc8905
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60066
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-23 17:17:10 +00:00
David Benjamin 8c7e925b5d Bound STACK_OF(T) sizes by int
Although we've switched STACK_OF(T) to use size_t, OpenSSL used int
pervasively. Much of crypto/x509 and third-party callers use int
indices. As much of that is in the public API now, ensure that
STACK_OF(T) can never exceed INT_MAX elements.

Bug: 516
Change-Id: I26b8fe590655f8c3e449b749b5d0222e28c413f8
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60065
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-23 16:58:21 +00:00
Adam Langley cf3851c6c9 acvptool: clean up better.
The Close() method of the middle often wasn't getting called because
`os.Exit(0)` was used in some places. Once that's fixed, it's clear that
the queue of pending reads needed to be closed before waiting for the
reader goroutine to finish. Lastly, don't bother trying to record the
error that the reader saw: just panic the process if the modulewrapper
dies during processing.

Change-Id: Icf077cefd0ace2ef721a493f99fede6269531257
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60045
Commit-Queue: David Benjamin <davidben@google.com>
Auto-Submit: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2023-05-23 14:00:53 +00:00
David Benjamin f712c86eda Avoid locks in CRYPTO_free_ex_data
Every time we free a type with ex_data (RSA, EC_KEY, DSA, SSL_CTX, SSL,
SSL_SESSION, X509, X509_STORE), we allocate and take a read lock. The
allocation means, if we believe in malloc failures, it is possible to
leak memory on malloc failure. The read lock causes an unnecessary bit
of contention writing to the cache line.

Instead, since we never remove ex_data entries, just thread them in a
singly-linked list. This way we only need to synchronize when to stop
iterating. Add a counter to synchronize that. (Or we could make each
'next' pointers atomic, but this seemed more straightforward.)

(I suspect this doesn't matter much, but it was shorter and we were
already allocating the funcs structures anyway.)

Bug: 570
Change-Id: Ie7ba5cc44f2b71ebd79c8971e784912d53af7f5c
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60025
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-23 01:04:21 +00:00
David Benjamin b0251b1295 Disable TLS_RSA_WITH_3DES_EDE_CBC_SHA by default
3DES has long been obsolete. It uses a small block size, making it
vulnerable to attacks at sufficiently high volumes (see
https://sweet32.info/, CVE-2016-6329). On top of this, it is slow even
without constant-time protections, making it a DoS risk for server
operators.

Since the alias "3DES" has existed in OpenSSL for a long time, keep that
one working, to reduce the risk of breaking someone who specifically
wanted 3DES enabled.

Update-Note: This CL disables TLS_RSA_WITH_3DES_EDE_CBC_SHA by default.
Specifically, it will not be included unless explicitly listed in the
cipher config, as "TLS_RSA_WITH_3DES_EDE_CBC_SHA", its legacy OpenSSL
name "DES-CBC3-SHA", or the alias "3DES". To restore it, add one of the
above to your cipher config.

Bug: 599
Change-Id: Ib94a2f149b3bfa240ef1008b9f3729a9c10368fb
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59425
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-23 00:57:56 +00:00
David Benjamin 8abcb6fb41 Don't include <stdatomic.h> in C++
OPENSSL_C11_ATOMIC is both computed in crypto/internal.h and also
defined externally. This is a remnant of C11 atomics were an opt-in
feature.

If defined externally, this means OPENSSL_C11_ATOMIC might be defined
when built as C++. That, in turn, causes <stdatomic.h> to be included in
C++ mode. At least one of our users toolchains has a <stdatomic.h> that
is incompatible with C++.  We don't get anything out of including it, so
just gate the include on !defined(__cplusplus) for now.

Things to look into as follow-up:

- Fix build files to stop defining OPENSSL_C11_ATOMIC. Prior to
  https://boringssl-review.googlesource.com/c/boringssl/+/59847, it was
  still serving a purpose: in server builds, if autodetection fails, we
  would rather fail to build than accidentally fallback to locks.
  There is no lock fallback anymore.

- Fix that toolchain so their <stdatomic.h> is C++-compatible. It's
  certainly not C++23-conformant. I suspect it's also not
  C++11-conformant, but I'm not positive.

Change-Id: I13bcd8380efeb87b9f9cc439fe24a743e48aec60
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59985
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
2023-05-19 20:22:42 +00:00
David Benjamin 74a75b3959 Add a comment about the Chromium sandbox for macOS sysctls
https://crrev.com/c/4415225 had to react to us querying these. Add a
comment so we can fix this proactively in the future.

Bug: chromium:1432323
Change-Id: I2ffe4d90e32215b521815a25f3448502da2156bf
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59925
Reviewed-by: Adam Langley <agl@google.com>
2023-05-18 23:47:10 +00:00
Nicky Mouha 47b2fefb03 Update hkdf.c to avoid potentially vulnerable code pattern.
Change-Id: I190fcdb0b9667b0ac6f490b36edc63237af7fffb
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59905
Reviewed-by: David Benjamin <davidben@google.com>
2023-05-18 23:05:56 +00:00
David Benjamin dd5219451c Fix RAND_enable_fork_unsafe_buffering when called after fork
If a process calls fork(), then the child process never forks again, the
child may wish to call RAND_enable_fork_unsafe_buffering(). However,
doing so exposes a bug: we assume that, if the flag is set, we don't
need to worry about fork-safety. But it is possible that the PRNG state
was cloned from another process which does not work.

Concretely, consider a zygote process, e.g. Chromium's. A zygote process
would retain fork-safety, but pass along its PRNG state to each of its
children. If the children never fork, they might disable fork-safety,
hitting this bug. (Chromium does not call this API. This is just a
hypothetical scenario.)

Fix this by reseeding whenever the fork-safety bit changes. This fix
does not strictly depend on the atomics work, but it causes us to
unconditionally sample rand_fork_unsafe_buffering_enabled(). This no
longer causes contention because it's just another atomic load.

This only affects systems without MADV_WIPEONFORK and without fast
RDRAND. If RDRAND is fast, we're always fork-safe and MADV_WIPEONFORK
allows us to efficiently detect forks.

Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: I6d0c471c62c951254faf85420a7dc3f4a9d65ee0
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59850
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-16 21:55:13 +00:00
David Benjamin 2eaf07075a Add a thread test for ex_data
This is intended to be run under TSan. ex_data currently works by taking
a global read lock every time an RSA, SSL, SSL_CTX, etc., is freed. We
should be able to fix that but, first, make sure we have test coverage
for the threading requirements.

Bug: 570
Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: I0e12907e116481d88e45191a1f15f3a51833bf6d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59865
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-16 21:26:16 +00:00
David Benjamin 1599fea870 Remove read locks from PRNG steady state
We don't take write locks in the PRNG, steady state, but we do take some
read locks: computing fork generation, reading the fork-unsafe buffering
flag, and a FIPS-only artifact of some global state clearing mess. That
last one is completely useless, but it's a consequence of FIPS's
understanding of process exit being comically inconsistent with reality.

Taking read locks is, in principle, parallel, but the cacheline write
causes some contention, even in newer glibcs with faster read locks. Fix
these:

- Use atomic reads to check the fork generation. We only need to lock
  when we observe a fork.

- Replace the fork-unsafe buffering flag with an atomic altogether.

- Split state_clear_all_lock into a per-rand_thread_state lock. We still
  need a read lock, but a completely uncontended one until process exit.

With many threads, this gives a significant perf boost.

x86_64, non-FIPS, Intel(R) Xeon(R) Gold 6154 CPU @ 3.00GHz, 30 threads:
Before:
Did 45131875 RNG (16 bytes) operations in 300039649us (150419.7 ops/sec): 2.4 MB/s
Did 44089000 RNG (32 bytes) operations in 300053237us (146937.3 ops/sec): 4.7 MB/s
Did 43328000 RNG (256 bytes) operations in 300058423us (144398.5 ops/sec): 37.0 MB/s
Did 45857000 RNG (1350 bytes) operations in 300095943us (152807.8 ops/sec): 206.3 MB/s
Did 43249000 RNG (8192 bytes) operations in 300102698us (144114.0 ops/sec): 1180.6 MB/s
After:
Did 296204000 RNG (16 bytes) operations in 300009524us (987315.3 ops/sec): 15.8 MB/s
Did 311347000 RNG (32 bytes) operations in 300014396us (1037773.5 ops/sec): 33.2 MB/s
Did 295104000 RNG (256 bytes) operations in 300012657us (983638.5 ops/sec): 251.8 MB/s
Did 255721000 RNG (1350 bytes) operations in 300016481us (852356.5 ops/sec): 1150.7 MB/s
Did 103339000 RNG (8192 bytes) operations in 300040059us (344417.3 ops/sec): 2821.5 MB/s

(Smaller PRNG draws are more impacted because they spend less time in the
DRBG. But they're also more likely because you rarely need to pull 8K of
data out at once.)

x86_64, FIPS, Intel(R) Xeon(R) Gold 6154 CPU @ 3.00GHz, 30 threads:
Before:
Did 29060000 RNG (16 bytes) operations in 300081190us (96840.5 ops/sec): 1.5 MB/s
Did 31882000 RNG (32 bytes) operations in 300118031us (106231.5 ops/sec): 3.4 MB/s
Did 30925000 RNG (256 bytes) operations in 300113646us (103044.3 ops/sec): 26.4 MB/s
Did 31969000 RNG (1350 bytes) operations in 300096688us (106529.0 ops/sec): 143.8 MB/s
Did 33434000 RNG (8192 bytes) operations in 300093240us (111412.0 ops/sec): 912.7 MB/s
After:
Did 299013000 RNG (16 bytes) operations in 300012167us (996669.6 ops/sec): 15.9 MB/s
Did 289788000 RNG (32 bytes) operations in 300014611us (965913.0 ops/sec): 30.9 MB/s
Did 298699000 RNG (256 bytes) operations in 300013443us (995618.7 ops/sec): 254.9 MB/s
Did 247061000 RNG (1350 bytes) operations in 300018215us (823486.7 ops/sec): 1111.7 MB/s
Did 100479000 RNG (8192 bytes) operations in 300037708us (334887.9 ops/sec): 2743.4 MB/s

On an M1 Pro, it's mostly a wash by default (fewer threads because this chip has fewer cores)

aarch64, M1 Pro, 8 threads:
Before:
Did 23218000 RNG (16 bytes) operations in 80009131us (290191.9 ops/sec): 4.6 MB/s
Did 23021000 RNG (256 bytes) operations in 80007544us (287735.4 ops/sec): 73.7 MB/s
Did 22853000 RNG (1350 bytes) operations in 80013184us (285615.4 ops/sec): 385.6 MB/s
Did 25407000 RNG (8192 bytes) operations in 80008371us (317554.3 ops/sec): 2601.4 MB/s
Did 22128000 RNG (16384 bytes) operations in 80013269us (276554.1 ops/sec): 4531.1 MB/s
After:
Did 23303000 RNG (16 bytes) operations in 80011433us (291245.9 ops/sec): 4.7 MB/s
Did 23072000 RNG (256 bytes) operations in 80008755us (288368.4 ops/sec): 73.8 MB/s
Did 22807000 RNG (1350 bytes) operations in 80013355us (285039.9 ops/sec): 384.8 MB/s
Did 23759000 RNG (8192 bytes) operations in 80010212us (296949.6 ops/sec): 2432.6 MB/s
Did 23193000 RNG (16384 bytes) operations in 80011537us (289870.7 ops/sec): 4749.2 MB/s

This is likely because, without RDRAND or MADV_WIPEONFORK, we draw from
the OS on every call. We're likely bottlenecked by getentropy, whether
it's some internal synchronization or syscall overherad. With
fork-unsafe buffering enabled, this change shows even more significant
wins on the M1 Pro.

aarch64, fork-unsafe buffering, M1 Pro, 8 threads:
Before:
Did 25727000 RNG (16 bytes) operations in 80010579us (321545.0 ops/sec): 5.1 MB/s
Did 25776000 RNG (32 bytes) operations in 80008587us (322165.4 ops/sec): 10.3 MB/s
Did 25780000 RNG (256 bytes) operations in 80006127us (322225.3 ops/sec): 82.5 MB/s
Did 33171250 RNG (1350 bytes) operations in 80002532us (414627.5 ops/sec): 559.7 MB/s
Did 54784000 RNG (8192 bytes) operations in 80005706us (684751.2 ops/sec): 5609.5 MB/s
After:
Did 573826000 RNG (16 bytes) operations in 80000668us (7172765.1 ops/sec): 114.8 MB/s
Did 571329000 RNG (32 bytes) operations in 80000423us (7141574.7 ops/sec): 228.5 MB/s
Did 435043750 RNG (256 bytes) operations in 80000214us (5438032.3 ops/sec): 1392.1 MB/s
Did 229536000 RNG (1350 bytes) operations in 80001888us (2869132.3 ops/sec): 3873.3 MB/s
Did 57253000 RNG (8192 bytes) operations in 80004974us (715618.0 ops/sec): 5862.3 MB/s

Note that, on hardware with RDRAND, the read lock in
rand_fork_unsafe_buffering_enabled() doesn't do much. But without
RDRAND, we hit that on every RAND_bytes call. More importantly, the
subsequent CL will fix a bug that will require us to hit it more
frequently.

I've removed the volatile on g_fork_detect_addr because I think we
didn't need it and this avoids thinking about the interaction between
volatile and atomics. The pointer is passed into madvise, so the
compiler knows the pointer escapes. For it to be invalid, the compiler
would need to go out of its way to model madvise as not remembering the
pointer, which would be incorrect of it for MADV_WIPEONFORK.

Bug: 570
Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: Ie6977acd1b8e7639aaa419cf6f4f5f0645bde9d1
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59849
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-16 21:21:48 +00:00
David Benjamin a05691d5d8 Add an atomics library to crypto/internal.h
refcount.c is now a single, generic file that calls into C11-atomic-like
APIs. Behind the scenes, this selects one of C11 atomics, Windows
interlocked APIs, or unsynchronized reads/writes (in the no-threads
build).

This frees us up to use atomics elsewhere in the library. For now, this
only binds sequentially consistent atomics, but we can add other memory
orders if needed. In particular, I believe up_ref only needs relaxed
atomics. Some of the later change I think only need acquire and release,
but I'm not positive.

Bug: 570
Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: Ifcd7357611bb7a8cd14b82c23ad080d1a2df1386
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59848
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-16 21:05:47 +00:00
David Benjamin 8a85012bc4 Remove the lock-based atomics fallback
On Windows, we can rely on Interlocked APIs. On non-Windows builds, we
currently require C11 but permit C11 atomics to be missing, via
__STDC_NO_ATOMICS__. This CL tightens this so C11 atomics are required
on non-MSVC builds.

My hope is that, now that we require C11 on non-Windows, this is a
fairly safe requirement. We already require pthreads on any platform
where this might apply, and it's hard to imagine someone has C11,
pthreads, but not C11 atomics.

This change means that, in later work, we can refactor the refcount
logic to instead be a <stdatomic.h> compatibility layer, and then an
atomics-targetting CRYPTO_refcount_t implementation. With a
<stdatomic.h> compatibility layer, we can use atomics in more places,
notably where our uses of read locks are causing cacheline contention.

The platform restriction isn't *strictly* necessary. We could, like with
refcounts, emulate <stdatomic.h> with a single, global lock. Indeed any
platforms in this situation have already been living with that lock for
refcounts without noticing. But then later work to add "atomics" to read
locks would regress contention for those platforms. So I'm starting by
rejecting this, so if any such platform exists, we can understand their
performance needs before doing that.

Update-Note: On non-Windows platforms, we now require C11 atomics
support. Note we already require C11 itself. If this affects your build,
get in touch with BoringSSL maintainers.

Bug: 570
Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: I868fa4ba87ed73dfc9d52e80d46853ef56715a5f
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59847
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-16 19:17:40 +00:00
David Benjamin 5b845de636 Use Windows Interlocked* APIs for refcounts when C11 isn't available
Right now, MSVC has to fallback to refcount_lock.c, which uses a single,
global lock for all refcount operations. Instead, use the Interlocked*
APIs to implement them.

The motivation is two-fold. First, this removes a performance cliff when
building for Windows on a non-Clang compiler. (Although I've not been
able to measure it in an end-to-end EVP benchmark, only a synthetic
refcount-only benchmark.)

More importantly, it gets us closer to assuming atomics support on all
non-NO_THREADS configurations. (The next CL will clear through that.)
That, in turn, will make it easier to add an atomics-like abstractions
to some of our hotter synchronization points. (Even in newer glibc, with
its better rwlock, read locks fundamentally need to write to memory, so
we have some cacheline contention on shared locks.)

Annoyingly, the Windows atomic_load replacement is not quite right. I've
used a "no-op" InterlockedCompareExchange(p, 0, 0) which, empirically,
still results in a write. But a write to the refcount cacheline is
surely better than taking a global exclusive lock. See comments in file
for details. OpenSSL uses InterlockedOr(p, 0), but that actually results
in even worse code. (InterlockedOr needs a retry loop when the
underlying cmpxchg fails, whereas InterlockedCompareExchange is a single
cmpxchg.)

Hopefully, in the future (perhaps when we require VS 2022's successor,
based on [1]), this can be removed in favor of C11 atomics everywhere.

[1] https://devblogs.microsoft.com/cppblog/c11-atomics-in-visual-studio-2022-version-17-5-preview-2/

Bug: 570
Cq-Include-Trybots: luci.boringssl.try:linux_clang_rel_tsan
Change-Id: I125da139e2fd3ae51e54309309fda16ba97ccf20
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59846
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-16 18:53:51 +00:00
David Benjamin dd9ee60686 Reject RSA keys under 512 bits
512-bit RSA was factored in 1999, so this limit barely means anything.
But establish some limit now to ratchet in what we can. We'll raise this
limit as we clear through further rounds of bad keys in tests.

As part of this, I've touched up rsa_test.cc a bit. All the functions
that made assumptions on key size now use std::vector with RSA_size.
kKey1 and kKey2 were also 512- and 400-bit RSA, respectively. In
principle, we could keep kKey1 for now, but the next stage will break it
anyway. I've replaced them with kFIPSKey (which was "FIPS-compliant" but
actually 1024-bit) and kTwoPrime (remnant of multi-prime RSA, 2048-bit).
As neither name makes sense, they're just the new kKey1 and kKey2.

I've also switched from string literals to arrays, which avoids the
pesky trailing NUL. Sadly, it is a bit more verbose. Maybe we should
switch to writing something like:

  const std::vector<uint8_t> kKey1 = MustDecodeHex("abcdef1234...");

Static initializers don't matter in tests, after all.

Update-Note: We no longer accept 511-bit RSA and below. If you run into
this, update test keys to more modern sizes as we plan to raise the
limit beyond 512-bit RSA in the future. 512-bit RSA was factored in
1999, so keys at or near this limit have been obsolete for a very, very
long time.

Bug: 607
Change-Id: I13c3366d7e5f326710f1d1b298f4150a4e8e4d78
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59827
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-15 21:51:01 +00:00
David Benjamin 786554f8f4 Check public components in freeze_private_key
We currently don't enforce rsa_check_public_key invariants on private
key operations, only public key operations and RSA_check_key. This
means it was actually possible, in some corner cases, to perform
operations with oversized e or n. Fix this.

This gets us a bit closer to aligning the mess of RSA invariants.
(RSA_check_key still performs some extra checks, but most of those
should be redundant with the CRT self-check.)

Update-Note: Manually constructed RSA private keys with invalid n or e
will now fail private key operations. Such keys would always fail at
public key operations (so the signatures would never verify). They also
already failed RSA_check_key and parsing.

The one incompatibility of note is keys with only n and d, constructed
by reaching into the internal RSA struct, no longer work. Instead, use
RSA_new_private_key_no_e. Conscrypt used to do this but has since been
migrated to the new API.

Bug: 316
Change-Id: I062fdad924b8698e257dab9760687e4b381c970d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59826
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-15 21:24:57 +00:00
David Benjamin 15a0c9a8e6 Remove now redundant RSA test
RSATest.MissingParameters tests this case a bit more extensively.

Change-Id: I61e38bd139c6334ff9d5c9636a159cb20bcd7f7b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59825
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-15 20:46:48 +00:00
Bob Beck 706846d7a8 Add documentation for X509_STORE_CTX_set_verify_cb and friends
Change-Id: Ibcc3faa4c3e03713a0c8f6dc24119e4579a5b4e4
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59025
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2023-05-15 17:35:09 +00:00
David Benjamin c1f5d795c2 Add a multi-threaded mode to bssl speed
This is inspired somewhat from how https://github.com/google/benchmark's
threaded benchmark support works. (It seems to spawn a bunch of threads,
latch them all together, and then run.)

This adds a TimeFunctionParallel which runs multiple copies of the
benchmark in parallel, after waiting for all the threads to synchronize.
Some functions had to be tweaked so they don't write to a single, shared
output buffer.

This probably could use some improvement. In playing with it, the
numbers are pretty unstable. We also don't currently benchmark anything
that captures EVP's internal refcounts. But hopefully it's enough to get
a start. I am able to measure impacts from the PRNG locks at least.

Bug: 570
Change-Id: I92c29a05ba082fc45701afd6f0effe23f7b148bd
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59845
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-14 16:50:42 +00:00
Adam Langley c215ce7e82 Use a helper function to implement get_all_foo_names functions.
Saves some duplicated logic.

Change-Id: I202fa92a88101f9ad735648bc414ab05752641da
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59685
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-12 18:08:37 +00:00
David Benjamin b92fcfdc17 Cap the input size to the conf fuzzer
Trying to fix all the places where these formats go quadratic isn't a
good use of time. We've already documented that they're not safe for use
with untrusted inputs. Even without such DoS issues, they cannot be
safely used anyway. (E.g. RUSTSEC-2023-0023.)

Just cap the fuzzer input. It'd be nice if we could avoid this more
systematically in the function, but they're not structured to make this
easy to do, and anyone concerned about DoS in this function has worse
problems.

Bug: chromium:1444420, oss-fuzz:56048, 611
Change-Id: I53eeb346f59278ec2db3aac4a92573b927ed8003
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59785
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-12 02:01:34 +00:00
Adam Langley e24491a09c acvptool: implement pipelining.
This causes avcptool to send requests without blocking on responses. See
the diff in ACVP.md for details of how to use this feature.

Change-Id: I922b3bd2383cb7d22a5d12ead49d2fa733ee1b97
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55345
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-11 22:20:00 +00:00
David Benjamin bf80254924 Update googletest
It's been a couple years since we did that.

I had to upstream a fix to CreateArgvFromArgs, but that landed quickly,
so we're actually carrying no googletest patches with this.

Change-Id: Ifaf3476f2079d444512a235756cfe54f492b9c92
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59745
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
2023-05-11 17:40:32 +00:00
David Benjamin 4137759c20 Update build tools
Change-Id: I1a4914cc4859ed1e0797a046a1abec8921c4a9cf
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59746
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-11 17:39:11 +00:00
Bob Beck 4540c3caf0 OpenBSD Support
Includes bits by me and Robert Nagy <robert.nagy@gmail.com> who has a
google CLA.

Update-Note: Additionally, BoringSSL now requires macOS 10.12 or later
for getentropy support. This is consistent with
https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

WANT_LGTM=all

Change-Id: I5ab74fa8a6677fac29c316aa29a954df401ba647
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59225
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-11 16:37:32 +00:00
David Benjamin c6dd304d2c Remove unions in BLAKE2b implementation
Bug: 574
Change-Id: Ief8030a4c9257a05defdc2dc02d496c63f06626a
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59545
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-10 21:18:38 +00:00
David Benjamin 2aae3f58b4 Bump the minimum supported MSVC version to VS2019
This aligns with https://github.com/google/oss-policies-info/pull/8 and
https://github.com/grpc/grpc/pull/32614. VS2019 adds a C11 mode, which
is useful for us, because it means stdalign.h works correctly.

Also bump the minimum Windows SDK to
https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/.
If you have a new MSVC, CMake will enable C11 mode by default. But if
C11 mode is enabled but your Windows SDK is too old, things break.

After this change, the CI will include some redundant configurations.
All the VS2017 configurations will start testing on VS2019, so the
VS2019-specific configurations won't do anything. I'll follow this up
with a change to bump those to VS2022, where we're currently missing
coverage.

Update-Note: BoringSSL now requires VS2019 or later and no longer
supports VS2017. VS2017 has been past its "mainstream end date" for over
a year now, per
https://learn.microsoft.com/en-us/lifecycle/products/visual-studio-2017

Change-Id: I3f359e8ea7c9428ddaa9fcc4ffead2ef903398be
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59665
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-08 21:46:15 +00:00
David Benjamin a972b78d1b Add APIs to query a list of possible strings for TLS features
Envoy needs to have the possible cipher, etc., strings predeclared to
reduce synchronization needs in the steady state. It currently does this
by (1) iterating over SSL_CTX_get_ciphers at SSL_CTX creation time and
(2) hard-coding a lists of known TLS 1.3 ciphers, TLS versions,
NamedGroups, etc.

(1) would work for some applications, but it breaks any applications
that configure ciphers on the SSL on a certificate callback, etc. If the
callback configures a cipher that wasn't configured on the SSL_CTX (e.g.
if the SSL_CTX were left at defaults), Envoy's logging breaks and we hit
an ENVOY_BUG assertion.

(2) breaks whenever BoringSSL adds a new feature. In principle, we could
update Envoy when updating BoringSSL, but this is an unresasonable
development overhead for just one of many BoringSSL consumers to impose.
Such costs are particularly high when considering needing to coordinate
updates to Envoy and BoringSSL across different repositories.

Add APIs to enumerate the possible strings these functions can return.
These string lists are a superset of those that any one application may
care about (e.g. we may have a deprecated cipher that Envoy no longer
needs, or an experimental cipher that's not yet ready for Envoy's
stability goals), but this is fine provided this is just used to
initialize the table. In particular, they are *not* intended to
enumerate supported features.

Bump BORINGSSL_API_VERSION to aid in patching these into Envoy.

Bug: b:280350955
Change-Id: I4d11db980eebed5620d3657778c09dbec004653c
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59667
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-08 17:49:32 +00:00
Adam Langley b1c6f45f1f Add back support for TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
This TLS 1.2 algorithm is substantially inferior to AES-GCM and should
never be used. It will not be available unless configured by name.
However, in can be used to provide backwards-compatibility with devices
that cannot be updated if so needed.

Change-Id: I1fd78efeb33aceca76ec2e7cb76b70f761ed1af8
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59585
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: Adam Langley <agl@google.com>
2023-05-05 22:52:23 +00:00
David Benjamin 722f5d878d Fix PKCS8Test to not rely on RSA-512
rsa_test.cc and a few of the tests in evp_tests.txt still do, but
refresh the easy one. I switched it to a P-256 key to keep the input
small.

Bug: 607
Change-Id: Ie0614280d12012bbd928f095eb4531e4d7550ae1
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59666
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-05 21:11:34 +00:00
David Benjamin 4d30888508 Add a more general mechanism for deprecating TLS ciphers
This generalizes the scheme we previously had with
TLS_RSA_WITH_NULL_SHA, in preparation for TLS_RSA_WITH_3DES_EDE_CBC_SHA
(to be deprecated in a later CL) and
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 (to regretably be added back in,
but in a deprecated state).

The story is that deprecated ciphers can always be listed by name, but
by default, selectors will not match them when adding ciphers. They will
still match them when removing ciphers. This is so instructions like
"@STRENGTH" or "!RSA" will still sort or disable the deprecated ciphers,
rather than accidentally leaving them at the front or enabled.

Additionally, a selector can mark itself as including deprecated
ciphers. This is specifically for TLS_RSA_WITH_3DES_EDE_CBC_SHA, because
many callers reference it with just "3DES". As an added quirk,
"RSA+3DES" will also match it. (The rule is that, if any selector matches
deprecated ciphers, we'll allow the overall expression to match it. This
is slightly weird, but keeps "RSA+3DES" working.)

Note, in this CL, 3DES is not actually deprecated. This just sets up the
machinery and doesn't do anything with it. But the blockers for
deprecating that should hopefully be resolved soon.

Bug: 599
Change-Id: I7212bdc879b0e49c6742025644f3100026f24228
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59646
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-05 20:32:59 +00:00
David Benjamin 6d55dc38bd Add APIs to support RSA keys with large e.
We cap e in RSA for DoS reasons. draft-amjad-cfrg-partially-blind-rsa
needs to create RSA keys with very large e. To support this, add an API
which disables this check.

Also add some missing checks for negative n and negative e. (Already
rejected by the parser, just not at this layer.)

Change-Id: Ia996bb1b46fc8b73db704f492b3df72b254a15a4
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59645
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-05 15:56:31 +00:00
David Benjamin 5b32e81407 Remove unions in GCM implementation
This was a bit of a mess. There are three assembly functions to juggle
here. Their current type signatures are:

 void gcm_init_v8(u128 Htable[16], const uint64_t H[2]);
 void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
 void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
                   size_t len);

Except for gcm_nohw.c, this is all assembly, so they don't follow the C
abstract machine's theory of typed memory. That means types are mostly
arbitrary and we have room to rearrange them. They do carry an implicit
alignment requirement, but none of these assembly files care about
this[*].

Values passed to gcm_gmult and gcm_ghash get XORed byte-by-byte in
places, which is inconvenient to do as uint64_t. They also get passed to
AES functions, which want bytes. Thus I think uint8_t[16] is the most
natural and convenient type to use.

H in gcm_init is interesting. gcm_init already doesn't take a GHASH key
in the natural byte representation. The two 8-byte halves are
byte-swapped, but the halves are not swapped, so it's not quite a byte
reversal. I opted to leave that as uint64_t[2], mostly to capture that
something odd is happening here.

[*] We only have GHASH assembly for x86, x86_64, armv7, and aarch64. We
used to have armv4 GHASH assembly, but that's been removed from
gcm_nohw.c. Thus we can assume none of these files care about alignment
for plain scalar loads. Alignment does matter for vmovdqa vs vmovdqu,
but that requires 16-byte alignment and uint64_t only implies 4- or
8-byte alignment on these architectures.

Bug: 574
Change-Id: If7dba9b41ff62204f4cf8fcd54eb4a4c54214c6e
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59528
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-05-05 04:00:51 +00:00
David Benjamin 5e988c4055 Rename EC_RAW_POINT to EC_JACOBIAN
EC_RAW_POINT is a confusing name. It's mostly about whether this is
stack-allocated EC_POINT without the EC_GROUP pointer. Now that we have
EC_AFFINE, EC_JACOBIAN captures what it's doing a bit better.

Fixed: 326
Change-Id: I5b71a387e899a94c79be8cd5e0b54b8432f7d5da
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59565
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-04 20:15:19 +00:00
David Benjamin 2e565ef29f Clarify that X25519 also supports EVP_PKEY_new_raw_private_key, etc
Fixed: 605
Change-Id: Id2b7d0221c3e43c102ce77c800f7ab65c74e0582
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59625
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-05-04 19:54:11 +00:00
David Benjamin 1b724a6b8c Align Kyber names with draft-tls-westerbaan-xyber768d00
The initial codepoint is called X25519Kyber786Draft00 in the draft, so
align with that name for this version. Also remove the placeholder bits
for the other combinations, which haven't gotten that far yet.

Update-Note: Update references to NID_X25519Kyber768 to
NID_X25519Kyber768Draft00. For now, the old name is available as an
alias.

Change-Id: I2e531947f41e589cec61607944dca844722f0947
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59605
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-04 02:28:38 +00:00
David Benjamin 85e6453cc3 Remove unions in polyval implementation
This can be done with just memcpy, which tempts the compiler slightly
less.

Bug: 574
Change-Id: I7b46c2f2578abc85621834426de20d5eaf492a74
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59527
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-03 20:03:31 +00:00
David Benjamin e7c3f473b9 Remove H from GCM128_KEY
This was originally removed in
https://boringssl-review.googlesource.com/12477, but restored in
https://boringssl-review.googlesource.com/c/boringssl/+/13122, which
also restored a comment the assembly code secretly relies on the struct
layout.

Our comment references the MOVBE-based assembly, which could mean either
the stitched AES+GCM code, or the GHASH-only code. I don't see an
obvious place where the GHASH-only code does this. The stitched ones
(both x86_64 and aarch64, counter to the comment) did this, but the
preceding CLs fix this. I think we can now remove this comment.

In OpenSSL, this comment dates to
d8d958323bb116bf9f88137ba46948dcb1691a77 in upstream. That commit also
removed a field, so we can assume no assembly prior to that change
relied on that layout.

That commit immediately preceded
1e86318091817459351a19e72f7d12959e9ec570, which was likely the
motivation. At the time, gcm_gmult_neon had some code with a comment
"point at H in GCM128_CTX". At the time, Htable wasn't even filled in,
and the function relied on H being 16 bytes before Htable.
gcm_ghash_neon also relies on them being reachable from Xi.

This code changed in f8cee9d08181f9e966ef01d3b69ba78b6cb7c8a8 and, at a
glance, the file doesn't seem to be making this assumption anymore. I
think, prior to that change, Htable wasn't filled in for the NEON code
at all.

With all this now resolved, remove the comment and unused copy of H in
GCM128_KEY.

Change-Id: I4eb16cfff5dd41a59a0231a998d5502057336215
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59526
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-03 18:09:26 +00:00
David Benjamin 62f9751ade Don't make assumptions about GCM128_CONTEXT layout in aesni-gcm-x86_64.pl
This is a little trickier because Intel architectures are so
inconveniently register-starved. This code was already using every
register. However, since Xi is only needed at the start and end of the
function, I just swapped the Xi and Htable parameters. Xi is passed on
the stack, so we don't need to explicitly spill it.

Change-Id: I2ef4552fc181a5350c9b1c733cf2319377a06b74
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59525
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-03 18:04:01 +00:00
David Benjamin a7f83c4ec1 Don't make assumptions about GCM128_CONTEXT layout in aesv8-gcm-armv8.pl
This isn't captured by the comments, the ABI tests have technically been
going out of bounds, and is entirely unnecessary. It can just take
Htable as a parameter.

Change-Id: Iad748d5b649333985ebaa1f84031fbe9a2339a85
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59505
Auto-Submit: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2023-05-03 16:49:59 +00:00
David Benjamin 4c8bcf0da2 Implement the AuthEncap/AuthDecap HPKE modes
Relevant spec bits:
https://www.rfc-editor.org/rfc/rfc9180.html#section-4.1
https://www.rfc-editor.org/rfc/rfc9180.html#section-5.1.3

Change-Id: Iddb151afc92f7a91beb9ca52caceec6cb5383206
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59387
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-05-02 19:22:24 +00:00