Repository navigation
Add rsa::KeyPair::from_components for constructing RSA key pairs from raw components - #1175
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1175 +/- ##
==========================================
- Coverage 95.80% 92.82% -2.98%
==========================================
Files 61 72 +11
Lines 8143 10586 +2443
Branches 0 10586 +10586
==========================================
+ Hits 7801 9826 +2025
- Misses 342 455 +113
- Partials 0 305 +305 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
justsmth
force-pushed
the
rsa-from-components
branch
from
July 15, 2026 14:56
0618dcd to
94254ee
Compare
…ents Implements the ring-compatible rsa::KeyPairComponents struct and rsa::KeyPair::from_components API (issue aws#791), allowing an RSA key pair to be constructed from its raw big-endian-encoded components without a DER-encoding round trip. - KeyPairComponents matches ring's type: same path (rsa module), same fields (public_key, d, p, q, dP, dQ, qInv), Clone + Copy, and a Debug impl that redacts the private components. - CRT parameters are installed via RSA_set0_crt_params so signing uses the CRT path and RSA_check_key validates all components (n == p*q, d*e consistency, dP/dQ/qInv consistency) at construction. - Tests cover round-trip against the same key parsed from DER, sign/verify, trait impls, Debug redaction, and rejection of inconsistent components.
justsmth
force-pushed
the
rsa-from-components
branch
from
August 5, 2026 13:03
94254ee to
4fde08b
Compare
Enforce the documented component encoding, document how this API differs from ring's, and close gaps in the test coverage. - PublicKeyComponents documents n and e as being encoded without leading zeros, and PublicKeyComponents::build_rsa enforces that, but from_components did not. The same PublicKeyComponents value was therefore accepted by from_components while being rejected by AsDer, TryInto<PublicEncryptingKey>, and verification. Hoist the check into a shared public_component_to_bn helper used by both paths, reporting InvalidEncoding to match ring. Leading zeros remain permitted on the private components, where the type makes no such claim. - Add a ring compatibility section to from_components. It covers both the cases where we are more permissive (modulus up to 8192 bits, no minimum public exponent) and the two where we are stricter: we fully validate d, which ring never uses and does not check, and we validate the CRT parameters at construction instead of deferring to the first signature. - Note why EVP_PKEY_set1_RSA is used here while the surrounding code uses EVP_PKEY_assign_RSA. - Assert the specific KeyRejected variant in the negative tests, and add a regression test that the public key APIs agree with from_components on leading zeros. - Add data/rsa_from_components_tests.txt, covering the 2048 to 8192 bit key size window and the public exponent rules. The valid keys are the components of the PKCS#8 test keys already in tests/data, so no new key material is introduced.
skmcgrail
approved these changes
Aug 5, 2026
Comment on lines
+116
to
+123
| /// `p`'s CRT exponent: `d mod (p - 1)`. | ||
| pub dP: Private, | ||
|
|
||
| /// `q`'s CRT exponent: `d mod (q - 1)`. | ||
| pub dQ: Private, | ||
|
|
||
| /// The CRT coefficient: `q**-1 mod p`. | ||
| pub qInv: Private, |
Member
There was a problem hiding this comment.
I believe these components are optional? Could we reflect that in the API to allow creation from d, p, and q alone? Or are we trying to be strictly compatible with a ring API here?
prasden
approved these changes
Aug 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issues:
Resolves #791
Description of changes:
Adds
rsa::KeyPairComponentsandrsa::KeyPair::from_components, matching ring 0.17's API for constructing RSA key pairs from raw components. This allows callers to construct a signing key directly from formats such as JWK without first encoding the components as DER.This also removes unnecessary generic bounds from
PublicKeyComponentsand applies the existing canonical encoding requirements fornandeconsistently across RSA construction paths.Call-outs:
dand the CRT parameters at construction usingRSA_check_key. Keys with inconsistent or placeholder values that ring accepts may therefore be rejected.nande, consistent with their existing API contract and ring's behavior. Leading zeros remain permitted for private components.pandqare not primality tested, matching ring.EVP_PKEY_set1_RSAand the ownership handling aroundRSA_set0_*are intentional to avoid leaks or double frees.Testing:
Adds coverage for supported key-size boundaries, public exponent and encoding rules, corrupted components, DER parity, signing and verification, trait bounds, and
Debugredaction.Verified with
cargo testunder the default andfipsfeatures,make clippy,make format, and rustdoc with-D warnings.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.