Skip to content

Commit

Permalink
Improved public key checks
Browse files Browse the repository at this point in the history
Adds the following checks:

- `n` is odd
- `e` is odd
- `e` < `n`

Closes #99
  • Loading branch information
tarcieri committed Apr 25, 2023
1 parent bd14ee9 commit dace218
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit dace218

Please # to comment.