12921 Commits

Author SHA1 Message Date
Brian Smith
0227a0614c CI: Use macOS 13 in GitHub Actions. 2023-12-01 19:17:31 -08:00
Brian Smith
febe76dc77 cpu arm: Fix static feature detection initialization.
Commit f932b941bd1f59782cb3db8f7cd7b8b2c9842ee9 was incomplete and
wrong. On targets where we do any static or dynamic feature detection
and where we have these global variables, we need to unconditionally
write the detected features to the global variable so that assembly
can see them. Since we do static feature detection regardless of
operating system, the initialization of the global most be done
without any conditions on the operating system.
2023-12-01 17:57:15 -08:00
Brian Smith
ff1050e161 Silent unused macro warning on Aarch64. 2023-12-01 17:55:56 -08:00
Brian Smith
e530025acd CI: Use briansmith/actions-cache@v3. 2023-11-30 15:43:06 -08:00
Brian Smith
83979189b3 CI: Use briansmith/actions-checkout@v4.
briansmith/actions-checkout was updated from actions/checkout.
2023-11-30 15:21:38 -08:00
Brian Smith
6bd1a210e3 CI: Use rustup directly to install toolchains. 2023-11-30 14:39:25 -08:00
Brian Smith
b349cecd06 CI: Avoid actions-rs/toolchain when we only need to use stable Rust.
GitHub Actions runners already have rustup with the stable toolchain
installed, apparently. actions-rs is going away and we don't want to
keep maintaining a fork with an unsupported upstream, so start the
process of dropping it.
2023-11-30 13:07:34 -08:00
Adolfo Ochagavía
dc035d9fc3 bench aead: Do Cartesian product, not zip
It looks like this was missed by 6cda863
2023-11-30 12:20:45 -08:00
Brian Smith
464d367252 0.17.6. 2023-11-28 13:19:11 -08:00
Brian Smith
f932b941bd Stop exporting unmangled symbols on ARM/AArch64.
We want all of our internal symbols to be internal so that none of
these internal symbols leak from a static/dynamic library that is
built with *ring* inside.
2023-11-27 12:57:54 -08:00
Brian Smith
b3d0b6f4b3 AES-GCM: Clarify that we need AVX (AVX1), not AVX2.
The function was named `is_avx2()` but the test is for AVX1. Fix the name.
2023-11-27 10:59:38 -08:00
Brian Smith
6c29bf61cd bigint: Remove Nonnegative.
Inline and simplify the logic into the one test-only function that
uses it.
2023-11-24 19:19:47 -08:00
Brian Smith
fbb8cf62f5 bigint: Fold Modulus constructors together.
Now we have exactly one way to construct a `Modulus` so we can
simplify the constructors.
2023-11-24 19:19:47 -08:00
Brian Smith
dc99f6681b RSA: Stop deriving Clone and Copy for P, Q, and N.
This was an artifact of some old logic that's no longer needed.
2023-11-24 19:19:47 -08:00
Brian Smith
76ce919997 rsa: Stop using Nonnegative in RsaKeyPair to check d.
Check `d` by processing it as a `OwnedModulus` like we do for the
other moduli. This should make the checking more consistent.

As a nice side effect, this eliminates the last non-test usage of
`Nonnegative` and elimnates more now-dead `Nonnegative` code.
2023-11-24 18:31:33 -08:00
Brian Smith
2ad2fcb912 rsa: Use dynamic checks for widening conversions.
Remove `SmallerModulus` and instead do the check dynamically. This
eliminates the last `unsafe impl` regarding the modulus
relationships. The uses of `elem_widen` won't ever fail but since
they are in an already-fallible function they wo't hurt.
2023-11-24 17:39:13 -08:00
Brian Smith
1855573098 rsa: Replace use of SmallerModulus with dynamic checks.
The dynamic checks should never fail but since they are added in
already-fallible functions they won't cause any trouble. This
facilitates future changes where the dynmic checks are required.
2023-11-24 17:39:13 -08:00
Jose Quintana
ae02e961cf Docs: Add clang path for Windows ARM64 hosts [skip ci] 2023-11-24 09:41:08 -08:00
Brian Smith
3145a7928a RSA: Precompute R**3 and store it instead of R**2.
This saves two private-modulus-length multiplications per RSA
private key operation at the cost of two private-modulus-length
squarings per `RsaKeyPair` construction.
2023-11-22 19:16:40 -08:00
Brian Smith
9b8d4d7b14 rsa: Split PrivatePrime construction.
Split the checking of the private modulus from the checking of the
private exponent so that we can do things in the order recommended
in the NIST spec.

This also facilitates storing R**3 instead of R**2 in the
`RsaKeyPair`. (We need R**2 during `RsaKeyPair` construction, but
R**3 afterwards.)
2023-11-22 19:16:40 -08:00
Brian Smith
ca043567e6 bigint: Stop implementing Debug for OwnedModulus.
This was necessary at some point in the past, but no longer is. It is
better to avoid depending on any of the `core::fmt` machinery in these
lower layers if we can avoid it.
2023-11-22 19:15:58 -08:00
Brian Smith
6de27244ff bigint: NFC: Take oneRR out of OwnedModulus.
`PublicModulus` and `PrivatePrime` are basically duplicates of
`OwnedModulusWithOne`. In the future we would like to create an
`OwnedModulus` that doesn't need 1RR to be calculated. Also in the
future we'd like to be able to "take" 1RR from a public modulus.
This change is a step towards those ends.
2023-11-22 18:07:16 -08:00
Brian Smith
986fe1f5ff rsa: NFC: Eliminate unnecessary Nonnegative -> Modulus conversion.
After 5ed0a45c65074a0640e6f9e21512a8c4d9540f1e we no longer needs `p`
or `q` in `Nonnegative` form.
2023-11-22 15:32:41 -08:00
Brian Smith
bc00f7e58c ec: NFC: Refactor scalar_sum to eliminate LIMBS_add_mod use.
Use the pattern we typically use where one argument is passed by value.

This lets us use `limbs_add_assign_mod`, eliminating the `unsafe`
direct use of `LIMBS_add_mod`. This will make future refactoring easier.

This also eliminates the need to construct and zeroize a new scalar `r`
for the result.
2023-11-22 14:38:39 -08:00
Brian Smith
3afbcc5dc5 Refactor away parse_big_endian_in_range_partially_reduced_and_pad_consttime.
It only had one caller and it contained unreached code.
2023-11-22 12:58:26 -08:00
Brian Smith
57fc4860d0 Remove ArrayEncoding usage from Digest and delete ArrayEncoding.
Note: I originally tried an alternative implementation using `flat_map` that
ended up being materially slower. To fix that performance regression I had to
make the following change:

```
     let mut output = Output([0; MAX_OUTPUT_LEN]);
     output
         .0
-        .iter_mut()
-        .zip(input.iter().copied().flat_map(|Wrapping(w)| f(w)))
+        .chunks_mut(N)
+        .zip(input.iter().copied().map(|Wrapping(w)| f(w)))
         .for_each(|(o, i)| {
-            *o = i;
+            o.copy_from_slice(&i);
         });
     output
 }
```

I verified that this generates the same assembly code as the original code
on x86-64 using Rust 1.74.0, except that there are two additional 128-bit
moves in `sha256_formta_output` to zero out the latter half of `Output`,
which was intended.
2023-11-22 09:35:47 -08:00
Brian Smith
ff0ed4ca9d Digest: Remove repr(c) from Output union. 2023-11-22 09:35:47 -08:00
Brian Smith
1598583c82 bench: Add digest overhead benchmarks. 2023-11-21 13:26:02 -08:00
Brian Smith
a5dbb01f63 bench aead: Do Cartesian product, not zip.
The `zip()` was written intending it to work like a Cartesian product,
but of course `zip()` doesn't do that. Do an actual Cartesian product.
2023-11-21 13:25:22 -08:00
Brian Smith
4f825b77eb bigint: Use a better Montgomery RR doubling-vs-squaring trade-off.
Clarify how the math works, and use a slightly better trade-off of
doubling vs squaring. On 64-bit targets RSA verification is now
less than 10% faster. On 32-bit targets its over 20% faster. I
expect that we can improve the performance further by optimizing
the doubling implementation.

Also the new implementation avoids allocating/cloning any temporary
`Elem`s, unlike the previous implementation.
2023-11-20 19:05:51 -08:00
Brian Smith
90dd9218cd RSA: Add benchmarks for signature verification. 2023-11-20 16:49:29 -08:00
Brian Smith
25112e9546 bigint: Calculate 1*R mod m without multiplication by 1*RR.
Save two private-modulus Montgomery multiplications per RSA exponentiation
at the cost of approximately two modulus-wide XORs.

The new new `oneR()` is extracted from the Montgomery RR setup.

Remove the use of `One<RR>` in `elem_exp_consttime`.
2023-11-15 19:55:14 -08:00
Brian Smith
81e17e4b10 bigint: Save one modular doubling in Montgomery RR setup.
Eliminate one modular doubling in Montgomery RR setup. This saves one
public modulus modular doubling per RSA signature verification, at the
cost of approximately one public-modulus-wide XOR. RsaKeyPair also sees
similar savings per Modulus.
2023-11-15 19:55:14 -08:00
Brian Smith
0349d2a332 bigint: NFC: Write "doubling" instead of "shift" or "mul_by_2".
Rename `elem_mul_by_2` to `elem_double`.

Use the term "doubling" instead of "shift."
2023-11-15 19:55:14 -08:00
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