From dace2189598b5f16a72094c355b1178ba1ad08c1 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 24 Apr 2023 21:36:53 -0600 Subject: [PATCH] Improved public key checks Adds the following checks: - `n` is odd - `e` is odd - `e` < `n` Closes #99 --- src/key.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/key.rs b/src/key.rs index a1fa6c5d..597aa9f1 100644 --- a/src/key.rs +++ b/src/key.rs @@ -3,6 +3,7 @@ use core::ops::Deref; use num_bigint::traits::ModInverse; use num_bigint::Sign::Plus; use num_bigint::{BigInt, BigUint}; +use num_integer::Integer; use num_traits::{FromPrimitive, One, ToPrimitive}; use rand_core::CryptoRngCore; #[cfg(feature = "serde")] @@ -442,6 +443,14 @@ fn check_public_with_max_size(public_key: &impl PublicKeyParts, max_size: usize) .to_u64() .ok_or(Error::PublicExponentTooLarge)?; + if public_key.e() >= public_key.n() || public_key.n().is_even() { + return Err(Error::InvalidModulus); + } + + if public_key.e().is_even() { + return Err(Error::InvalidExponent); + } + if e < RsaPublicKey::MIN_PUB_EXPONENT { return Err(Error::PublicExponentTooSmall); }