12742 Commits

Author SHA1 Message Date
Brian Smith
21289d3dcd Ignore BoringSSL 'e0d601a'..'7cb90e0' 2023-09-27 21:04:15 -07:00
Brian Smith
4c6fe71650 No-op merge of BoringSSL 'db10ae5': Cap Montgomery moduli to 8 KiB.
This is the same limit we already implement.
2023-09-27 21:02:10 -07:00
Alexis (Poliorcetics) Bourget
7bbc307f57 deps: Move from winapi to windows-sys
`winapi` is in maintenance mode and the new blessed way to access Windows APIs are the `windows`
and `windows-sys` crates. I don't think any types of `winapi` were exposed in the public API so
I used `windows-sys` since it has much faster compile times.
2023-09-26 19:40:45 -07:00
Brian Smith
cfbd46b774 32-bit ARM AES-GCM: Fix use of BSAES.
The slice indexing was wrong. CI did not catch this because it only
uses the implementations that the feature detection logic chooses,
and in CI it uses the emulator that always chooses the hardware AES
implementation.

The consequence is that on 32-bit ARM targets with NEON but without
hardware AES, the encryption/decryption is done incorrectly.

The change that introduced this problem was
74aa79b650f8a1f751993a56046ca43166ae9934. I checked (and tested) the
latest release (0.16.20) to verify that it is correct. So this only
affects 0.17, which wasn't been released yet.

The fix mirrors what we were doing before in 0.16.20.
2023-09-26 19:39:52 -07:00
Brian Smith
38409e109b NFC: Minimize visibility of aes::Key. 2023-09-26 19:39:52 -07:00
Brian Smith
8020c1b634 Tests: Move bigint tests to where BoringSSL puts them.
BoringSSL split up there bn_tests.txt into multiple files, which we had
done previously. Prepare to merge that BoringSSL change by putting the
test input files in the same places.
2023-09-26 19:39:52 -07:00
Bob Beck
d24a38200f Sync pki to chromium eddbcb143c7462e0b8d60e859b96d678ca0c013c
This removes one more patch, and adapts import to deal with gmock from chrome
which is now included in boring.

Bug: chromium:1322914
Change-Id: I2a5957f741252941fea76205a21e98fd655f8cae
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63225
Commit-Queue: Adam Langley <agl@google.com>
Auto-Submit: Bob Beck <bbe@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-09-27 02:13:00 +00:00
Brian Smith
f9378e8ed5 NFC: Move low-level Montgomery arithmetic out of bigint.
When the `alloc` feature is disabled, on lesser-used targets we don't
build `bigint` but we still need some of the Montgomery arithmetic.

```
 git diff \
    HEAD^1:src/arithmetic/bigint/bn_mul_mont_fallback.rs \
    src/arithmetic/montgomery.rs
```

```
 git diff \
    HEAD^1:src/arithmetic/bigint.rs \
    src/arithmetic/montgomery.rs
```
2023-09-26 17:45:59 -07:00
David Benjamin
764e6a319b Stop rewriting ret to repz ret on x86_64
This is an old workaround from the AMD K8 days. GCC stopped doing it for
their generic output in 2017.
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=4ca47ced33cc0d6f9e336930d628a6fdbf22f6e2

b/65150507#comment2 says LLVM has never done it.

We can retire this now and recover a small handful of bytes.

Change-Id: I37ef47038b6b3a1a7500bcea8cbd1beefc83121c
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63205
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
2023-09-26 19:51:28 +00:00
Brian Smith
b04bed15be
Merge pull request #1651 from briansmith/b/merge-boringssl-11
Merge BoringSSL through 27e45c43420125ed293d4646ddf8ff2c321f01b9
2023-09-26 08:39:48 -07:00
Alice Wang
cfcb954901 Make bssl-crypto no_std compatible
Bug: 649
Change-Id: Ib47e843496e58a5cdb3cd04b3929e0a08ba09744
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63145
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2023-09-25 18:20:34 +00:00
Brian Smith
0ae93f0b15 No-op merge of BoringSSL '27e45c4'. 2023-09-24 16:01:45 -07:00
Brian Smith
62655ad141 Merge BoringSSL '7ab49bf': Fix comments now BN_mod_exp_mont_consttime is not cache-line-sensitive. 2023-09-24 16:01:13 -07:00
Brian Smith
4a8ea2a832 Skip BoringSSL 'a880d2a'. 2023-09-24 15:53:00 -07:00
Brian Smith
9e93637357 Merge BoringSSL 'e0bb21b': Update x86_64-mont5.pl and RSAZ comments a bit. 2023-09-24 15:49:52 -07:00
Brian Smith
6c90cce7b8 Ignore BoringSSL 'a1dffbf'..'e5c86b7'. 2023-09-24 15:46:46 -07:00
Brian Smith
6678808009 Merge BoringSSL '7ac94aa': More -Wshorten-64-to-32 fixes. 2023-09-24 15:43:35 -07:00
Brian Smith
ba1c0f5934 Ignore BoringSSL '41eb357'..'dd81bf7'. 2023-09-24 15:42:15 -07:00
Brian Smith
20b1810a3b Merge BoringSSL '0faffc7': Fix the comment in ecp_nistz256_ord_sqr_mont to match code and prototype. 2023-09-24 15:40:07 -07:00
Brian Smith
6874e555a5 Ignore BoringSSL '361e3e0'..'6b785f6'. 2023-09-24 15:36:50 -07:00
Brian Smith
00401a0afe Skip BoringSSL '91e0b11'. 2023-09-24 15:33:18 -07:00
Brian Smith
97a526c010 Merge BoringSSL '1b2b7b2': Various -Wshorten-64-to-32 fixes. 2023-09-24 15:31:41 -07:00
Brian Smith
b08f7da343 Ignore BoringSSL 'fd52296'..'bbd9f18' 2023-09-24 15:27:42 -07:00
Brian Smith
75d34bc1a8 Merge BoringSSL 7b2795a: Replace even more ad-hoc bytes/integer conversions. 2023-09-24 15:26:51 -07:00
Brian Smith
946731e6d0 Ignore BoringSSL 'f4cdf91..9f426b6'. 2023-09-24 15:05:18 -07:00
Brian Smith
7e7d44c0ec No=op merge of BoringSSL '5c2ef10'. 2023-09-24 15:00:04 -07:00
Brian Smith
6afe1dd20a Ignore BoringSSL 28883d4..2cc2aa9. 2023-09-24 14:54:18 -07:00
Brian Smith
ad59665506
Merge pull request #1650 from briansmith/b/merge-boringssl-10
Merge BoringSSL through 4d7b383e989eff619a60eb1da1b18f613af65daf
2023-09-23 20:28:49 -07:00
Brian Smith
65fb8b80f9 Ignore BoringSSL 'fdeb4aa..4d7b383'. 2023-09-23 15:50:16 -07:00
Brian Smith
5233928eb9 Take BoringSSL '0378578': Dedup a few more load/store implementations. 2023-09-23 15:48:18 -07:00
Brian Smith
6ccdf7bd12 Merge BoringSSL '6c2af68': Remove a few more unions. 2023-09-23 15:12:24 -07:00
David Benjamin
584f1e1016 Cherry-pick BoringSSL ca45987: Move load/store helpers to crypto/internal.h.
These are needed for the next merge from BoringSSL.
2023-09-23 15:03:59 -07:00
Brian Smith
6e9f1b7d8a
Merge pull request #1648 from briansmith/b/merge-boringssl-9
Merge BoringSSL through 0f2c55cb748651833af247bbed43e.
2023-09-21 09:54:42 -07:00
printfn
20ba41d67e Fix broken URL in PKCS#8 docs 2023-09-19 10:19:14 -07:00
Brian Smith
c9fd0ba48f NFC: Remove unneeded #cfg(...) in digest_tests.rs
The code is always enabled since "wasm32" was spelled wrongly. Apparently
the wasm-bindgen compatibility issue is no longer an issue.
2023-09-19 10:16:01 -07:00
Nikolay Arhipov
4983338b68 PS Vita support 2023-09-19 08:35:58 -07:00
Brian Smith
f812f37aba Merge commit '0f2c55cb748651833af247bbed43e' into b/merge-boringssl-9.
Take the changes from BoringSSL, except use `limbs_copy` and `limbs_zero`.
2023-09-18 17:53:44 -07:00
Brian Smith
7cf6516da2 Ignore BoringSSL `41eb890..e3ebb9e'. 2023-09-18 17:19:56 -07:00
David Benjamin
a1843d660b Bump the minimum CMake version to 3.12
CMake 3.12 was released July 2018, so it meets our five year rule. [0]
also requires a CMake version newer than 3.12.

[0] https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

Change-Id: I2b21d68e3a379108edde9c7d13450bba52f41235
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63105
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
2023-09-18 22:22:58 +00:00
David Benjamin
340fe150b7 CMake doesn't have an error function
Instead the spelling is message(FATAL_ERROR "blah"). Although
error("blah") also works because it just complains that error doesn't
exist.

Change-Id: I80384e0198a9013f93f9403d0a4c256749905045
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/63106
Reviewed-by: Bob Beck <bbe@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
2023-09-18 21:15:53 +00:00
Brian Smith
e48fdcf782
Merge pull request #1645 from briansmith/b/merge-boringssl-8
Merge BoringSSL through 167f1760ddfeaea1ee1a671ca88aafcccfe30ee0
2023-09-17 12:17:58 -07:00
Brian Smith
ba41827040 Ignore BoringSSL '0ebd69b..167f176'. 2023-09-17 12:03:28 -07:00
Brian Smith
29b441cee1 No-op merge of BoringSSL 'efd09b7'.
This was already done in *ring*'s Rust code.
2023-09-17 12:02:20 -07:00
Brian Smith
c8bbdf9567 Ignore BoringSSL `572c416..d27a01e' 2023-09-17 12:01:04 -07:00
Brian Smith
98ae3272e7 Skip BoringSSL '1e469e4': Replace some more C unions.
None of the changes seem useful to *ring* due to previous oxidation.
2023-09-17 12:00:24 -07:00
Brian Smith
ce97384c1a Ignore BoringSSL 8ba90d1..f575d9b. 2023-09-17 11:45:08 -07:00
Brian Smith
e11e97ea82 No-op merge of BoringSSL 'b8a6514'.
*ring* doesn't use RSAZ. *ring* already uses this simpler loop.
2023-09-17 11:42:50 -07:00
Brian Smith
73d9069605 No-op merge of BoringSSL 'c7de4fe'.
*ring*'s Rust code already did this.
2023-09-17 11:42:01 -07:00
Brian Smith
106235e3e3 No-op merge of BoringSSL 801a801: Add an extra reduction step to the end of RSAZ.
The test case that is enabled in the merged BoringSSL change was already enabled in
*ring*, so nothing changed regarding the test cases. *ring* doesn't use RSAZ.

```
git difftool \
    801a801:crypto/fipsmodule/bn/bn_tests.txt \
    .\src\arithmetic\bigint_elem_exp_consttime_tests.txt
```
2023-09-17 11:41:53 -07:00
David Benjamin
76e98c4351 Always end BN_mod_exp_mont_consttime with normal Montgomery reduction.
This partially fixes a bug where, on x86_64, BN_mod_exp_mont_consttime
would sometimes return m, the modulus, when it should have returned
zero. Thanks to Guido Vranken for reporting it. It is only a partial fix
because the same bug also exists in the "rsaz" codepath. That will be
fixed in the subsequent CL. (See the commented out test.)

The bug only affects zero outputs (with non-zero inputs), so we believe
it has no security impact on our cryptographic functions. BoringSSL
calls BN_mod_exp_mont_consttime in the following cases:

- RSA private key operations
- Primality testing, raising the witness to the odd part of p-1
- DSA keygen and key import, pub = g^priv (mod p)
- DSA signing, r = g^k (mod p)
- DH keygen, pub = g^priv (mod p)
- Diffie-Hellman, secret = peer^priv (mod p)

It is not possible in the RSA private key operation, provided p and q
are primes. If using CRT, we are working modulo a prime, so zero output
with non-zero input is impossible. If not using CRT, we work mod n.
While there are nilpotent values mod n, none of them hit zero by
exponentiating. (Both p and q would need to divide the input, which
means n divides the input.)

In primality testing, this can only be hit when the input was composite.
But as the rest of the loop cannot then hit 1, we'll correctly report it
as composite anyway.

DSA and DH work modulo a prime, where this case cannot happen.

Analysis:

This bug is the result of sloppiness with the looser bounds from "almost
Montgomery multiplication", described in
https://eprint.iacr.org/2011/239. Prior to upstream's
ec9cc70f72454b8d4a84247c86159613cee83b81, I believe x86_64-mont5.pl
implemented standard Montgomery reduction (the left half of figure 3 in
the paper).

Though it did not document this, ec9cc70f7245 changed it to implement
the "almost" variant (the right half of the figure.) The difference is
that, rather than subtracting if T >= m, it subtracts if T >= R. In
code, it is the difference between something like our bn_reduce_once,
vs. subtracting based only on T's carry bit. (Interestingly, the
.Lmul_enter branch of bn_mul_mont_gather5 seems to still implement
normal reduction, but the .Lmul4x_enter branch is an almost reduction.)

That means none of the intermediate values here are bounded by m. They
are only bounded by R. Accordingly, Figure 2 in the paper ends with
step 10: REDUCE h modulo m. BN_mod_exp_mont_consttime is missing this
step. The bn_from_montgomery call only implements step 9, AMM(h, 1).
(x86_64-mont5.pl's bn_from_montgomery only implements an almost
reduction.)

The impact depends on how unreduced AMM(h, 1) can be. Remark 1 of the
paper discusses this, but is ambiguous about the scope of its 2^(n-1) <
m < 2^n precondition. The m+1 bound appears to be unconditional:

Montgomery reduction ultimately adds some 0 <= Y < m*R to T, to get a
multiple of R, and then divides by R. The output, pre-subtraction, is
thus less than m + T/R. MM works because T < mR => T' < m + mR/R = 2m.
A single subtraction of m if T' >= m gives T'' < m. AMM works because
T < R^2 => T' < m + R^2/R = m + R. A single subtraction of m if T' >= R
gives T'' < R. See also Lemma 1, Section 3 and Section 4 of the paper,
though their formulation is more complicated to capture the word-by-word
algorithm. It's ultimately the same adjustment to T.

But in AMM(h, 1), T = h*1 = h < R, so AMM(h, 1) < m + R/R = m + 1. That
is, AMM(h, 1) <= m. So the only case when AMM(h, 1) isn't fully reduced
is if it outputs m. Thus, our limited impact. Indeed, Remark 1 mentions
step 10 isn't necessary because m is a prime and the inputs are
non-zero. But that doesn't apply here because BN_mod_exp_mont_consttime
may be called elsewhere.

Fix:

To fix this, we could add the missing step 10, but a full division would
not be constant-time. The analysis above says it could be a single
subtraction, bn_reduce_once, but then we could integrate it into
the subtraction already in plain Montgomery reduction, implemented by
uppercase BN_from_montgomery. h*1 = h < R <= m*R, so we are within
bounds.

Thus, we delete lowercase bn_from_montgomery altogether, and have the
mont5 path use the same BN_from_montgomery ending as the non-mont5 path.
This only impacts the final step of the whole exponentiation and has no
measurable perf impact.

In doing so, add comments describing these looser bounds.  This includes
one subtlety that BN_mod_exp_mont_consttime actually mixes bn_mul_mont
(MM) with bn_mul_mont_gather5/bn_power5 (AMM). But this is fine because
MM is AMM-compatible; when passed AMM's looser inputs, it will still
produce a correct looser output.

Ideally we'd drop the "almost" reduction and stick to the more
straightforward bounds. As this only impacts the final subtraction in
each reduction, I would be surprised if it actually had a real
performance impact. But this would involve deeper change to
x86_64-mont5.pl, so I haven't tried this yet.

I believe this is basically the same bug as
https://github.com/golang/go/issues/13907 from Go.

Change-Id: I06f879777bb2ef181e9da7632ec858582e2afa38
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/52825
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-09-15 17:01:39 -07:00