11244 Commits

Author SHA1 Message Date
Brian Smith
1879c0555d RSA: Flatten API by removing public and keypair submodules.
When looking at how this would generlaize to the other public key
cryptosystems (ECDSA, ED25519, etc.), I think having fewer submodules
involved makes more sense.
2021-09-29 13:01:25 -07:00
Brian Smith
88c281e3ea RSA: Move rsa::keypair::Components to rsa::KeyPairComponents.
Take a step towards flattening (and simplifying) the public API of
the RSA submodule. This is done as a separate step from the rest of
the work so that the Git history will correctly reflect that signing.rs
gets renamed to keypair.rs, with only minimial modifications, in the
next commit. (If this were merged with the following commit, then Git
would report the new keypair.rs as a new file without any history from
signing.rs.)
2021-09-29 13:01:25 -07:00
Brian Smith
6cde4b8142 RSA: Derive Clone and Copy more instead of manually implementing them. 2021-09-28 15:02:24 -07:00
Brian Smith
d608eeba3e Refactor RSA PSS MGF processing to enable future use by OAEP.
Previously, `mgf1()` wrote the mask to the buffer, and then we XOR'd
the data onto the mask. Now, `mgf1()` XOR's the mask onto the data
that is already in the `out` buffer.
2021-09-28 12:47:44 -07:00
Brian Smith
766d5be0dd RSA: Remove misleading documentation about side channels protection.
When *ring* first started, BoringSSL and OpenSSL upstream were both
using an implementation of constant-time-ish exponentiation that took
shortcuts that made it clearly not constant-time. Long ago, that code
was replaced here and in BoringSSL (and probably OpenSSL upstream), so
this comment is no longer correct.
2021-09-28 11:18:40 -07:00
Brian Smith
b6fe6caf77 RSA: Split RSA public key arithmetic from signature verification logic.
`Exponent::exponentiate` will eventually be used for both signature
verification and encrypting using a public key.
2021-09-28 10:18:50 -07:00
Brian Smith
8c89dbddf6 RSA/bigint: Thread proof of CPU feature detection through bigint.
The tests of `bigint` were not doing CPU feature detection themselves.
Thus they were depending on some other tests that run before them to do
it, or else they were not making use of all the CPU optimizations
possible, and thus not testing all the interesting code paths.

Also, as we are expanding the functionality of the RSA module, it has
become more difficult to track where CPU feature detection has been done
and where it needs to be done. Move the proof that the CPU feature
detection has been done down into the callers of the `bn_` functions
that need CPU feature detection to have been done.

This will also be helpful if/when we expand the use of the `bigint`
module beyond RSA.
2021-09-27 19:09:08 -07:00
Brian Smith
93d7db9e88 RSA: Remove From<Exponent> for NonZeroU64 implementation.
We shouldn't promise to external users that the exponent will always
fit in a 64-bit integer as in the future we may need to allow larger
exponents.
2021-09-27 19:08:44 -07:00
Brian Smith
751c014e1b RSA internals: Move digest instead of passing a reference when doing padding.
The digest is never used after encoding, so move it instead of referencing it.
This is more correct since for signing (and soon encryption) the padded value
is only supposed to be used once.
2021-09-27 14:57:01 -07:00
Brian Smith
f172986113 RSA: Replace RsaSubjectPublicKey with rsa::public::Key. 2021-09-27 13:09:01 -07:00
Brian Smith
0df07b8483 Eliminate type-level type constraint on UnparsedPublicKey types.
Idiomatic practice in Rust is to avoid type-level constraints in favor
of impl-level constraints so that things aren't over-constrained.

Derive `Clone` and `Copy` instead of explicitly implementing them,
which is now possible after implementing the type-level constraints.
2021-09-27 12:09:42 -07:00
Brian Smith
d33461b877 Derive Clone qnd Copy more instead of explicitly defining them. 2021-09-27 12:09:42 -07:00
Brian Smith
fed775d2c3 RSA: Make rsa::public::Key clonable. 2021-09-24 17:04:16 -07:00
Brian Smith
511740c29f RSA: Replace RsaSubjectPublicKey::{modulus,exponent} with rsa::Public::{Modulus,Exponent}::be_bytes().
This is a step towards removing the heap-allocated and usually-unnecessary
`public_key: RsaSubjectPublicKey` field. The new API allows the caller to
better control how it stores/allocates the component values. This also removes
a couple of infallible `unwrap()`s.

This is a step towards removing `io::Positive` from the public API.

This is a breaking API change.
2021-09-24 14:08:08 -07:00
Brian Smith
dd14e593e1 Testing: Test iterator cloning. 2021-09-24 14:08:08 -07:00
Brian Smith
d3d17338c4 Polyfill: Add LeadingZerosStripped. 2021-09-24 14:08:08 -07:00
Brian Smith
92e15f23dc Make ArrayFlatMap compatible with Rust 1.52.1 (current MSRV). 2021-09-23 16:20:33 -07:00
Brian Smith
b8b35ff124 Internal: Replace some manual big-endian encoding w/ standard library use.
Refactor `limb::big_endian_from_limbs` to use an approach based on
iterators. We will be then be able to use the new `limb::be_bytes`
to implement `rsa::public::Exponent::be_bytes()` and
`rsa::public::Modulus::be_bytes()` and eventually other similar functions.
We want those functions to return `ExactSizeIterator`s.

This is also part of an ongoing process to eliminate replace all the
big-endian/little-endian encoding logic in *ring* w/ use of core APIs.
2021-09-23 16:20:33 -07:00
Brian Smith
0e6be9778d RSA: Add a public RsaKeyPair::from_components function. 2021-09-23 10:44:03 -07:00
Brian Smith
466b724335 Allow private and public RSA key pair components to be of different
types.

In particular, allow the type for the public components to implement
`Debug` without requiring the type for the private components to
implement `Debug`, for the purpose of implementing `Debug` for the
`Components` type itself.
2021-09-23 10:44:03 -07:00
Brian Smith
4a3a17fd21 RSA: Split PKCS#8-specific logic out from general RSA private key construction logic.
Prepare for the introduction of a public API for constructing RSA key pairs from
components, and for the introduction of RSA encryption APIs.
2021-09-23 10:44:03 -07:00
Brian Smith
617b09baa6 RSA: Deprecate and replace RsaKeyPair::public_modulus_len. 2021-09-23 10:43:13 -07:00
Brian Smith
e05f7e1273 Make rsa::{self, public::{self, *}} public. 2021-09-23 10:43:13 -07:00
Brian Smith
6a6ea5893e RSA: Reduce visibility of pub(in crate::rsa) items.
Avoid `pub(in crate::rsa)` in favor of relative visibility constraints.
2021-09-23 10:43:13 -07:00
Brian Smith
d881b64792 Make bits::{self, BitLength} public. 2021-09-23 10:43:13 -07:00
Brian Smith
8ac90f4c3e RSA: Move test_signature_rsa_pkcs1_sign_output_buffer_len to tests/.
It's only using the public API so it should be an integration test.
2021-09-22 13:55:57 -07:00
Brian Smith
562b5e4500 RSA: Clarify validity checking for exponents.
Move the comments citing the standard to more appropriate places. Do the
steps in order.
2021-09-22 11:26:57 -07:00
Brian Smith
b3f07e3df1 Factor out rsa::public::Modulus.
This is a step towards making `rsa::public::Key` public.
2021-09-22 09:49:03 -07:00
Brian Smith
ebb7ba1235 AEAD: Fix rustdoc warnings.
The types `OpeningKey` and `SealingKey` are not in scope so we need to
qualify them.
2021-09-22 08:01:08 -07:00
David Barton
5d8dcf160c Typo in Readme 2021-09-21 13:09:47 -07:00
Brian Smith
4d71f100de RSA: Clarify that the elem_exp_vartime is only called for the public modulus. 2021-09-20 14:59:34 -07:00
Brian Smith
2c7e46bd7f RSA: Factor out common call to into_unencoded().
All the callers of `elem_exp_vartime` call `into_unencoded()` on the result,
so just do that within `elem_exp_vartime`.

The default value of the `Encoding` type parameter is `Unencoded`, so elide
it.
2021-09-20 14:59:34 -07:00
Brian Smith
bdeea72ead Move bigint::PublicExponent to rsa::public::Exponent.
The bounds checking that `bigint::PublicExponent`'s constructor is doing is
specific to RSA. The correctness of the exponentiation arithmetic doesn't
depend on those additional checks. Move all that bounds checking to RSA.
Soon, there will be `rsa::public::{Key, Modulus}` to complement `Exponent`.

Move `bigint::elem_exp_vartime` to `rsa`. The performance analysis is only
valid for RSA.
2021-09-20 14:59:34 -07:00
Brian Smith
e4d06962db bigint: Use PublicExponent as the type of the minimum allowed exponent.
Replace some dynamic checks with compile-time type safety + unit tests.

This makes it clearer that the assertions can never fail.
2021-09-20 10:37:19 -07:00
Brian Smith
f974b8de29 bigint: GivePublicExponent::MAX type PublicExponent. 2021-09-20 10:37:19 -07:00
Brian Smith
8149edd790 bigint: Make PUBLIC_EXPONENT_MAX_VALUE an associated const. 2021-09-20 10:37:19 -07:00
ComplexSpaces
2896a014de Add open_in_place_separate_tag API to LessSafeKey
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2021-09-16 13:24:26 -07:00
Brian Smith
d8378acc60 bigint: Remove unnecessary and misleading assertion in elem_exp_vartime_.
The assertion made sense when the function was only for the exponentiation
in RSA public key operations. However, this assertion is nonsensical for
the other use of the function to construct the montgomery constant for the
modulus.

Add more documentation about the performance.

Rather than trying to "improve" the assertion, just remove it.
`PUBLIC_EXPONENT_MAX_VALUE` is just a bit smaller of bits smaller than
what the type naturally enforces.

The added documentation should help us reason about whether the assertion
could ever fail. Because we constrain the maximum modulus (bit) length,
the maximum value of the exponent is for the Montgomery setup case is
less than `PUBLIC_EXPONENT_MAX_VALUE`.
2021-09-16 13:17:35 -07:00
Brian Smith
53ebaf7302 bigint: Move dynamic check of nonzero exponent out of elem_exp_vartime_().
Use `NonZerou64` to encode the fact that the exponent is nonzero, so that
we can remove an assertion that would never fail.

This is a non-functional change.
2021-09-16 13:17:35 -07:00
Brian Smith
7c478c457b bigint: Clarify LG_BASE is a constant. 2021-09-16 13:17:35 -07:00
Brian Smith
a22977f421 bigint: Fix typo in comment about lg_base. 2021-09-16 13:17:35 -07:00
Brian Smith
6ae2dea1a8 bigint: Replace as conversion with u64_from_usize. 2021-09-16 13:17:35 -07:00
Brian Smith
022f4dbe8e Bump libc version requirement. 2021-08-23 20:14:17 -07:00
Brian Smith
bf3e8582d6 Bump wasm-bindgen-test to 0.3.26. 2021-08-23 20:14:17 -07:00
Brian Smith
d77d152546 Bump web-sys to 0.3.51. 2021-08-23 20:14:17 -07:00
Brian Smith
e33675400a Bump once_cell to 1.8.0. 2021-08-23 20:14:17 -07:00
Brian Smith
9e6ea77bf1 Bump Criterion.rs version. 2021-08-23 20:14:17 -07:00
Brian Smith
3b8ae3ab12 CI: Stop excluding Rust 1.47.0 builds since they are never attempted.
This logic is a holdover from when Rust 1.47.0 was the MSRV.
2021-08-23 16:48:03 -07:00
Brian Smith
5412ef9559
Merge pull request #1365 from briansmith/b/coverage-nightly
CI: Use a slightly older Nightly Rust for coverage measurement.
2021-08-23 16:44:00 -07:00
Brian Smith
83c204342e CI: Use a slightly older Nightly Rust for coverage measurement. 2021-08-23 14:38:38 -07:00