Skip to content

Cap modulus size on private-key component construction (DoS hardening; complements #793) - #805

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

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

Conversation

@Portll

@Portll Portll commented Dec 3, 2026

Copy link
Copy Markdown

Problem

The public-key path caps the modulus size — PublicKey::new rejects a modulus above PublicKey::MAX_SIZE (8192 bits) with Error::ModulusTooLarge before doing any heavy work. The private-key component-construction path does not. PrivateKey::from_components / from_p_q / from_primes (the shared private constructor in src/private_key.rs, ~line 240) proceed straight into BoxedMontyParams::new(n) (Montgomery setup) and the O(bits²) prime-product check on a caller-supplied modulus of any size, before validation can reject it.

So an application that constructs a PrivateKey from untrusted components (e.g. a key-management or import surface that accepts raw n, e, d, primes) can be made to burn unbounded CPU/memory on an attacker-sized modulus — a DoS — even though the equivalent public modulus would be rejected immediately.

Honest scope

Residual confirmed present on master 90fb187: the only ModulusTooLarge reference in private_key.rs is a test of PublicKey::new; the private constructor has no size guard.

Change

Adds the same bound the public path uses, early in the private component constructor, before the Montgomery setup and product:

if n_bits as usize > PublicKey::MAX_SIZE {
    return Err(Error::ModulusTooLarge);
}

Tests

from_components_rejects_oversized_modulus — a 9000-bit modulus (> MAX_SIZE 8192) passed to from_components returns Err(Error::ModulusTooLarge) before the heavy work runs. cargo test --lib: 69 passed, 0 failed.

One file, src/private_key.rs. Mirrors PublicKey::new; reuses the existing Error::ModulusTooLarge variant.


Opened as a draft for maintainer review. Reported by Portll john@portll.net.

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; the private component-construction API applied no such
bound, letting from_components 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_inner, before the heavy
path. RustCrypto#793 applies the same bound at the PKCS#1 decode layer; this covers the
direct component-construction API (from_components / _with_large_exponent /
_with_wide_primes) too.
@Portll

Portll commented Dec 3, 2026

Copy link
Copy Markdown
Author

Closing this — on reflection this DoS residual (reachable in released 0.10.0) is better routed through the project's security reporting than a public PR. Will re-raise via the appropriate private channel. Apologies for the noise.

@Portll Portll closed this Dec 3, 2026
@Portll
Portll deleted the fix/private-import-modulus-cap branch December 3, 2026 18:46
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