Repository navigation
Conversation
from_components_inner builds Montgomery parameters and (for the 2-prime case) an O(bits^2) product of the supplied primes before any validation runs. On the public-key path PublicKey::new rejects a modulus over MAX_SIZE first, so that heavy work is bounded; from_components applied no such bound, letting it run seconds of CPU on an attacker-sized modulus before rejecting it (CPU-exhaustion DoS on untrusted private-key import). Reject n over PublicKey::MAX_SIZE in from_components, before the heavy path. RustCrypto#793 applies the same bound at the PKCS#1 decode layer, which goes through from_components; this also covers direct calls. Keys larger than MAX_SIZE remain supported where the caller opts in or chose the size: from_components_with_wide_primes and the hazmat from_components_with_large_exponent are unbounded, and key generation (PrivateKey::new and friends) skips the import limit.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #806 +/- ##
==========================================
+ Coverage 93.14% 93.33% +0.19%
==========================================
Files 29 29
Lines 4097 4184 +87
==========================================
+ Hits 3816 3905 +89
+ Misses 281 279 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Portll
marked this pull request as ready for review
December 4, 2026 13:01
This branch has not been deployed
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.
Summary
from_components_innerbuildsBoxedMontyParams::new(n)and, for the 2-prime case, an O(bits²) product of the supplied primes before any validation runs. The public-key path bounds the identical primitive —PublicKey::newrejects a modulus overMAX_SIZEfirst — but the private component-construction API (from_components/_with_large_exponent/_with_wide_primes) had no such bound, so it runs that heavy work on an attacker-sized modulus before rejecting it (CPU-exhaustion DoS on untrusted private-key import).This adds the cap in
from_components_inner, before the heavy path, rejecting a modulus overPublicKey::MAX_SIZEwithError::ModulusTooLarge.Detail
On the private path,
validate()callscheck_public(self)=check_public_with_max_size(n, e, None)(src/public_key.rs:227-229); withmax_size = Nonethe size branch is skipped, so no maximum is ever enforced. Meanwhilefrom_components_innerhas already run:BoxedMontyParams::new(n)(src/private_key.rs:253), andprimes.fold(one, |acc, p| acc.concatenating_mul(&p)) != n— a schoolbook O(bits²) multiply (src/private_key.rs:271-277).An application importing untrusted private-key components (JWK/JSON, an HSM read-out, a custom key format) can be fed a ~1 MB (~4,000,000-bit) modulus and spend seconds of CPU per call before the key is rejected. Input length bounds the modulus, so this is asymmetric super-linear-work DoS (compute ≫ input) — availability only, not key recovery.
Version scope (stated honestly)
rsa0.10.0 (crates.io): vulnerable via both untrusted DER/PKCS#8 import and thefrom_components*API — Bound PKCS#1 modulus sizes before bigint conversion #793 is not in the released crate.90fb187): Bound PKCS#1 modulus sizes before bigint conversion #793 already bounds the modulus at the PKCS#1 decode layer (encoded_modulus_to_uint). The residual this PR targets is the direct component-construction API, which decode does not cover.Not a timing/network side channel and not a Marvin regression.
Tests
Adds
from_components_rejects_oversized_modulus(constructs a 9000-bit modulus, assertsError::ModulusTooLarge). 69/69 lib tests pass.Open question for maintainers
Whether a dedicated private-side
MAX_SIZEoverride (analogue ofPublicKey::new_with_size_limits) is wanted for callers who legitimately import moduli outside1024..=8192. This PR enforces the same hard cap the public default uses.Secondary and more debatable (not included here): the private path also skips the 1024-bit minimum the public path enforces, so sub-1024-bit private keys import silently — a weak-key acceptance, not a DoS. Worth a separate discussion.