From a3958c42366490a233d9c235a3073f02e07453d4 Mon Sep 17 00:00:00 2001 From: Portll Date: Thu, 3 Dec 2026 22:06:40 +1030 Subject: [PATCH] Reject RSA keys whose primes are too close (FIPS 186-5 gap check) Key validation only enforced that p != q. FIPS 186-5 (A.1.5) and SP 800-56B (6.4.1.2) additionally require |p - q| > 2^(nlen/2 - 100): when the factors are close, Fermat factorization recovers the modulus cheaply (Bock et al., "Fermat Factorization in the Wild", eprint 2023/026). Go's crypto/rsa enforces the same bound in checkPrivateKey via `diff.BitLenVarTime() <= N.BitLen()/2-100`. Add the check to validate_private_key_parts for the two-prime case, mirroring Go's signed comparison so small test keys are unaffected. The bound is a public property of the modulus size and |p - q| need not be secret for it, so vartime BoxedUint ops are used. Multiprime keys have no standardized bound and are left unchanged. --- src/private_key.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/private_key.rs b/src/private_key.rs index d0252180..33612d63 100644 --- a/src/private_key.rs +++ b/src/private_key.rs @@ -707,6 +707,32 @@ fn validate_private_key_parts(key: &PrivateKey) -> Result<()> { return Err(Error::InvalidModulus); } + // FIPS 186-5 §A.1.5 and SP 800-56B §6.4.1.2: for two-prime keys the factors must not + // be too close together. If |p - q| is small, Fermat factorization recovers the modulus + // cheaply (Bock et al., "Fermat Factorization in the Wild", eprint 2023/026). Reject when + // |p - q| <= 2^(nlen/2 - 100), i.e. bit_length(|p - q|) <= nlen/2 - 100. This mirrors Go's + // crypto/rsa check `diff.BitLenVarTime() <= N.BitLen()/2-100`. + // + // The bound depends only on the public modulus size, and |p - q| need not be kept secret + // for this comparison (a passing key reveals only that the gap exceeds the public bound), + // so variable-time operations are acceptable here. The multiprime case (len > 2) has no + // standardized gap bound and is left unchanged. + if key.primes.len() == 2 { + let p = key.primes[0].as_ref(); + let q = key.primes[1].as_ref(); + let diff = if p.cmp_vartime(q).is_ge() { + p.wrapping_sub(q) + } else { + q.wrapping_sub(p) + }; + let nlen = key.pubkey_components.n.bit_width_vartime(); + // Signed arithmetic mirrors Go: for small moduli `nlen/2 - 100` is negative and the + // comparison is vacuously false, so short test keys are not rejected. + if i64::from(diff.bit_width_vartime()) <= (i64::from(nlen) >> 1) - 100 { + return Err(Error::InvalidPrime); + } + } + // Check that de ≡ 1 mod p-1, for each prime. // This implies that e is coprime to each p-1 as e has a multiplicative // inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) = @@ -779,6 +805,56 @@ mod tests { #[cfg(feature = "encoding")] use pkcs8::DecodePrivateKey; + #[test] + fn rejects_primes_that_are_too_close() { + // FIPS 186-5 gap check: |p - q| must exceed 2^(nlen/2 - 100). + // These are two consecutive 512-bit primes (|p - q| == 20, so bit_length == 5), + // giving a 1024-bit modulus whose bound is 2^(512 - 100). The key is otherwise valid + // (e = 65537 is coprime to p-1 and q-1), so the gap check is the only failing check. + let p = BoxedUint::from_be_slice( + &hex!( + "d1359a8f6c8042c56814a2bc786a6d2df26fff4cc4fd394d4c10a4fe30cffdda" + "6dbca290a9eab7061f00bca0042db9232c61275c9e6b6cf8950e87d7f5606697" + ), + 512, + ) + .unwrap(); + let q = BoxedUint::from_be_slice( + &hex!( + "d1359a8f6c8042c56814a2bc786a6d2df26fff4cc4fd394d4c10a4fe30cffdda" + "6dbca290a9eab7061f00bca0042db9232c61275c9e6b6cf8950e87d7f56066ab" + ), + 512, + ) + .unwrap(); + let e = BoxedUint::from(65537u64); + assert_eq!(PrivateKey::from_p_q(p, q, e), Err(Error::InvalidPrime)); + } + + #[test] + fn accepts_well_separated_primes() { + // Control for `rejects_primes_that_are_too_close`: two independent 512-bit primes with + // |p - q| of bit_length 510, far above the 2^(nlen/2 - 100) bound, must validate. + let p = BoxedUint::from_be_slice( + &hex!( + "8ca7caa2adba52ce4e385994ebac94af6f28d015a2aa0b9d6c50afb6e9fb123d" + "c34bd8e2fe5213e529610ae0eed8f1e7d4c08880a5a4666d54760e7fbc051cc3" + ), + 512, + ) + .unwrap(); + let q = BoxedUint::from_be_slice( + &hex!( + "b03a83f5c5b864d733176469aa6ef6308860a84722025e0511dc6f3fcb57d5d8" + "6df216c33f8f3201d998efd82733e93357de8c051d4b7ef2c675ce05588f896d" + ), + 512, + ) + .unwrap(); + let e = BoxedUint::from(65537u64); + assert!(PrivateKey::from_p_q(p, q, e).is_ok()); + } + #[test] fn non_coprime_factors_precompute_error() { // Distinct odd composite factors can pass validation: 3 * 19 = 1 mod 8 and mod 14.