Skip to content

fix: cap modulus size on private-key component construction - #806

Open
Portll wants to merge 1 commit into
RustCrypto:masterfrom
Portll:fix/private-import-modulus-cap
Open

Portll wants to merge 1 commit into
RustCrypto:masterfrom
Portll:fix/private-import-modulus-cap

Conversation

@Portll

@Portll Portll commented Dec 4, 2026

Copy link
Copy Markdown

Summary

from_components_inner builds BoxedMontyParams::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::new rejects a modulus over MAX_SIZE first — 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 over PublicKey::MAX_SIZE with Error::ModulusTooLarge.

Detail

On the private path, validate() calls check_public(self) = check_public_with_max_size(n, e, None) (src/public_key.rs:227-229); with max_size = None the size branch is skipped, so no maximum is ever enforced. Meanwhile from_components_inner has already run:

  • BoxedMontyParams::new(n) (src/private_key.rs:253), and
  • for the 2-prime case, primes.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)

Not a timing/network side channel and not a Marvin regression.

Tests

Adds from_components_rejects_oversized_modulus (constructs a 9000-bit modulus, asserts Error::ModulusTooLarge). 69/69 lib tests pass.

Open question for maintainers

Whether a dedicated private-side MAX_SIZE override (analogue of PublicKey::new_with_size_limits) is wanted for callers who legitimately import moduli outside 1024..=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.

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

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.33%. Comparing base (90fb187) to head (b96488f).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Portll
Portll marked this pull request as ready for review December 4, 2026 13:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant