12837 Commits

Author SHA1 Message Date
Brian Smith
af28001bf3 NFC: Use the correct variable name m instead of n in comments. 2023-11-15 19:55:14 -08:00
Jonah Petri
445de2fb9c getauxval isn't defined on uclibc, so disable dynamic feature detection 2023-11-14 14:08:24 -08:00
Jorge Aparicio
7fa58cc7f7 rename feature
and have it apply only when `target_os = "none"`
2023-11-14 13:22:44 -08:00
Jorge Aparicio
9195b4a33f add an opt-in less-safe-getrandom-custom feature
This Cargo feature treats a user-provided `getrandom` implementation as
a secure random number generator (`SecureRandom`). The feature only has
effect on targets not supported by `getrandom`.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2023-11-14 13:22:44 -08:00
Brian Smith
07aff69462 EC: Fix support for curves like P-521 in the code generator.
Values for P-521 have an odd number of limbs in 32-bit mode, which
means we can't keep using `TOBN`, and also Montgomery-encoded
values are different for 32-bit and 64-bit.
2023-11-13 12:45:39 -08:00
Brian Smith
d87972edc9 EC: Add C code generation to mk/generate_curves.py.
Generate some of the C boilerplate, particularly the large constants.
The output is written into target/curves/, and can be merged into
the actual code in crypto/fipsmodule/ec/ using a two-way merge tool;
this is the same as the Rust code generation.

Changes to gfp_p{256,384}.c are due to differences in the generator's
output:

* The generator doesn't generate trailing commas in arrays.
* The generator consistently avoids adding leading zeros to hex
  constants, and consistently format values less than 10 in decimal;
  the exiting code used a mix of styles.
* The generator wraps arrays consistently; the existing code used a
  mix of wrapping styles.
* The generator does not nest constants in the functions that need
  them. This was changed to support future refactorings.
2023-11-13 12:45:39 -08:00
Brian Smith
75cbe475ff NFC: Address a Clippy lint in limbs_mul.
This isn't caught by `mk/clippy.sh` usually since this code isn't used
for x86-64 targets yet.
2023-11-11 10:08:23 -08:00
Brian Smith
61d1da61ea NFC bigint: Implement Copy for N0. 2023-11-11 10:08:23 -08:00
Brian Smith
cfa3737947 RSA: Support RSA key pairs where q < p without converting to p > q.
Previously we swapped p and q and calcualted a new qInv if p < q so
that we could avoid doing a redunction during the CRT computation.
Instead, just do the reduction during CRT as it's cheap. This
notably reduces the number of operations we need in `bigint`, and
it eliminates the need for the `Prime` modulus marker type.

Now there are more things that can go wrong during CRT. First, we
may wrongly forget to reduce m_2 mod p; before this wasn't necessary
since every element of q was an element of p. Next, we may wrongly
use the the value of m_2 mod p instead of m_2 later; before we could
do this since previously m_2 mod p == m_2 since m_2 < q < p. Add
tests for these cases.

Rewrite the tests for `elem_reduced_once` given its new constraints.
2023-11-10 17:10:07 -08:00
Brian Smith
23975ff236 RSA: Remove q_mod_n from RsaKeyPair.
Reduce the size of RsaKeyPair by about 15%.

Importantly, this was the only non-temporary (`'static`) `Elem`
other than `One`.
2023-11-10 09:17:50 -08:00
Brian Smith
946ce877d2 RSA: Remove QQ from RsaKeyPair.
QQ comprised almost 25% of the bulk of RsaKeyPair and is actually
completely unnecessary since `elem_reduced` can do the whole
reduction itself.

This has the nice and important side effect of eliminating some
conversion operations between `bigint` types.

This is also a step towards eliminating some of the `unsafe trait`
stuff that kinda-but-not-really modeled modulus relationships.
2023-11-09 14:31:31 -08:00
Brian Smith
cbcac26d00 bigint: Add modulus bit length to Modulus. 2023-11-07 16:59:10 -08:00
Brian Smith
5ed0a45c65 RSA: Rearrange private prime validity checks.
Move all the checks that are done for each private prime into
the `PrivatePrime` constructor, to eliminate duplication.

This causes the 512-bit-ness check to be done earlier than before,
which affects some of the tests..
2023-11-07 16:59:10 -08:00
Brian Smith
2f01ebfe32 bigint: Store bit length of modulus in OwnedModulusWithOne. 2023-11-07 16:59:10 -08:00
Brian Smith
d8e9a9172c RSA: Eliminate a redundant clone.
Commit be27e8e25946b2e975258cfb1ea21f6cc4731d8c made this clone
unnecessary.
2023-11-06 22:23:10 -08:00
Brian Smith
e51c88a986 Rename PartialModulus to Modulus, Modulus to OwnedModulusWithOne.
Originally we only had `Modulus`. Then we had a need for a
temporary `Modulus` without `oneRR` so we created `PartialModulus`.
However, there is really nothing "partial" about them. So, improve
the naming by renaming `PartialModulus` to `Modulus` and `Modulus`
to `OwnedModulusWithOne`. In the future we may refactor things
further to separate the ownership aspect from the "has oneRR"
aspect.

Instead of just doing a straightforward rename, take this
opportunity to refactor the code so that it uses the new `Modulus`
whenever `oneRR()` isn't used. This eliminates the duplication of
the APIs of the two modulus types, and the duplication of
`elem_mul` and `elem_mul_`.
2023-11-06 12:52:27 -08:00
Brian Smith
69d1dd34e6 NFC RsaKeyPair: Add notes about wastefulness of qq and q_mod_n. 2023-11-06 12:52:27 -08:00
Brian Smith
1659f14295 bigint elem_exp_consttime: Make implementations more consistent.
Use the same argument order, naming, etc. as the x86-64 version.
2023-11-04 10:21:59 -07:00
Brian Smith
3044ee705d bigint elem_exp_consttime: Avoid one slice copy.
Take a step towards aligning the two implementations.
2023-11-04 10:21:59 -07:00
Brian Smith
4658c27d52 NFC bigint: Remove Width.
The original idea of `Width` was that we'd support operatings that
worked on multiple same-width but different-modulus values, and/or
we'd support splitting a 2N-limb `BoxedLimb` into two N-limb
`&[Limb]`, etc. However, as things are now, `Width` doesn't really
serve a useful purpose.
2023-11-04 10:21:10 -07:00
Brian Smith
8ed48604d0 RSA signature verification: Avoid wasteful key re-serialization.
When we added `rsa::PublicKey` we changed the `ring::signature` RSA
implementation to construct an `rsa::PublicKey` and then verify the
signature using it. Unfortunately for backward compatibility with old
uses of `RsaKeyPair`, `rsa::PublicKey` constructor constructs (and
allocates) a copy of the ASN.1-serialized public key. This is not
acceptable for users who are using `ring::signature` to verify a
single signature. Refactor `PublicKey` so that it can be bypassed
by the `ring::signature` implementation.

This is a step towards implementing allocation-free RSA signature
verification.
2023-11-02 17:26:13 -07:00
Brian Smith
6920c4fc40 Digest/Polyfill: Remove SHA-1 use of ChunksFixed and delete it.
This is the last step in the removal of `ChunksFixed`, which contains
one line of `unsafe` code.
2023-10-31 11:15:25 -07:00
Brian Smith
6c1d378d8d Chacha20-Poly1305-OpenSSH: Remove use of ChunksFixed.
Take a step towards removing the `unsafe` in `ChunksFixed`'s implementation.
2023-10-31 11:15:25 -07:00
Jiaqi Gao
a9b88826e7 third_party/fiat: replace memcpy with OPENSSL_memcpy
Align with the other use of `OPENSSL_memcpy` in `curve25519_64_adx.h`.
`string.h` will no longer be needed.

Signed-off-by: Jiaqi Gao <jiaqi.gao@intel.com>
2023-10-30 20:01:15 -07:00
Brian Smith
1432533ce9 Enforce clippy::cast_possible_truncation.
For now, just put `#[allow(...)]` directives in the places where the
conversions are done. We'll follow up in the future with the correct
replacement for `as` for each case, as several PRs.
2023-10-30 19:17:48 -07:00
Brian Smith
2b1194c845 NFC: Use pointer::cast instead of as for pointer casts.
Enforce this pattern with Clippy.
2023-10-30 19:17:48 -07:00
Brian Smith
1fa6d09eef Have Clippy block most use of as conversions. 2023-10-30 19:17:48 -07:00
Brian Smith
ad356c99ac Limit libc dependency to ARM and AArch64 targets. 2023-10-30 18:38:06 -07:00
Brian Smith
fce7153096 Include arm_arch.h in crypto/internal.h.
Do it because BoringSSL does it. BoringSSL has some other headers it
includes here but we intentionally do not have them and/or we
intentionally do not include them here (string.h and assert.h).
2023-10-30 16:01:42 -07:00
Brian Smith
acf9f0df2d Generalize agreement benchmarks to support all algorithms. 2023-10-30 15:41:34 -07:00
Vlad Krasnov
0223beea17 Benchmarks: add ECDSA benchmarks
Fixes: #1772
2023-10-30 13:37:11 -07:00
Brian Smith
19a93d2b10 Curve25519: Add missing static assertion.
Sync with BoringSSL.
2023-10-30 13:23:18 -07:00
Qiu Chaofan
b6c35d54e3 Enable getrandom for AIX 2023-10-30 12:52:46 -07:00
Brian Smith
711d9fc062 AEAD benchmarks: Rewrite to avoid macros.
When this benchmark was imported from crypto-bench to *ring* and
ported from the libtest `#[bench]` framework to Criterion.rs, we
kept the macro-based structure from the original benchmarks. However,
Criterion.rs actually supports the kind of parameterized benchmarking
we do much more naturally, and so we don't need the macros. Get rid of
them.

Also remove distinction between TLS 1.2 and TLS 1.3 AAD. These
benchmarks were originally written long ago when the TLS 1.3 draft
specified a different AAD format.

I hope this will serve as a better example of how to write such
benchmarks than it previously did.
2023-10-27 10:08:58 -07:00
Brian Smith
e7cf02f403 Agreement docs: Eliminate redundant nested peer_public_key in example. 2023-10-26 16:10:15 -07:00
Brian Smith
61ad435f09 EC: Add scalar base point multiplication test case generator.
Add a new scalar base point multiplication test case generator that
where the points are *not* Montgomery-encoded. This way we don't need
to generate different test data files when the Montgomery encoding
for a curve isn't the same for 32-bit and 64-bit targets (P-521).

This version of the generator produces the test cases for all the
scalars that the current P-256 and P-384 tests generate, in the same
format; the only exception is that the point is not
Montgomery-encoded.
2023-10-26 10:25:46 -07:00
David Benjamin
4f2adbd947 Cherry-pick BoringSSL 50418af: Add some EC base point multiplication test vectors.
`git cherry-pick 50418afb7f7e9467bd9b01b42b7732ef46b96baa`.

Bring in the test vector generator unmodified.
2023-10-26 10:25:46 -07:00
Brian Smith
6613481632 Add P-521 to mk/generate_curves.py.
Add preliminary support for P-521 but don't sync the existing
(semi-)generated code with it.
2023-10-25 13:46:08 -07:00
Brian Smith
e83cf0c0bb Add simplified elliptic curve code generator.
Add a tool to generate and/or verify NIST curve boilerplate.

Sync the existing P-256 and P-384 code with the generator.
2023-10-25 13:46:08 -07:00
Brian Smith
3565fe830f EC Suite B: Remove reudndant "point_mul" tests.
When I generated these test vectors, I gave all of them the same point: the
generator of the curve. Consequently these input files are 100% redundant
with the `point_mul_base.txt` input files. So just remove them and use the
`point_mul_base.txt` files instead.
2023-10-25 10:03:48 -07:00
Brian Smith
be27e8e259 RSA signature verification: Save one multiplication.
Use David Benjamin's idea.
2023-10-20 18:05:47 -07:00
Brian Smith
9dd6141f91 NFC EC: Reduce scope of constant ONE. 2023-10-19 13:12:12 -07:00
Brian Smith
c3fda8b4dd 0.17.5. 2023-10-18 20:58:28 -07:00
Brian Smith
a86b7fabb9 0.17.5-alpha.1. 2023-10-18 20:58:28 -07:00
Brian Smith
27aa5386a0 EC P-384: Use array types for crossing Rust<->C boundary.
Avoid using the P384_POINT type on the C side. It seems to work for all
the targets we support, for P-384, but this pattern probably doesn't
work in general. Especially due to alignment issues for 32-bit targets,
it is doubtful it would work for P-521.
2023-10-18 11:39:04 -07:00
Brian Smith
327831e6b3 P-256 nistz: Use arrays instead of P256_POINT in boundary functions.
Better match the Rust declarations of these functions. Prepare to
support more target platforms and more weird things (like P-521) that
by avoiding any kind of alignment assumptions at the language
boundary (or elsewhere).
2023-10-18 11:39:04 -07:00
Brian Smith
159f52c769 Eliminate gathering during table construction.
When `elem_exp_consttime` replaced `BN_mod_exp_mont_consttime` I did
not fully understand the way the table was constructed in the original
function. Recent BoringSSL changes clarify the table construction. Do
it the same way, to restore performance to what it was previously.

This addresses the `// TODO: Optimize this to avoid gathering`.
2023-10-18 11:14:38 -07:00
Brian Smith
19fe19124b bigint elem_exp_consttime: Get rid of entry/entry_mut indexing.
When this code was written, it wasn't clear which assembly language
functions took a pointer to the entire state vs. just a pointer to
the accumulator (etc.). Now upstream clarified things and we can
clarify this code.
2023-10-18 11:14:38 -07:00
Brian Smith
d4bf41eb00 bigint elem_exp_consttime: Add some clarifying comments. 2023-10-18 11:14:38 -07:00
Brian Smith
9a49f379e3 P-256 ECDSA verification: Clarify multiplication.
Move more of the logic for the nistz256 multiplication into Rust.
2023-10-18 09:49:30 -07:00