Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into const-crypto-biguint
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 17, 2024
2 parents ffdc5c2 + 9956b8f commit d675b13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/algorithms/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) fn pkcs1v15_encrypt_pad<R>(
where
R: CryptoRngCore + ?Sized,
{
if msg.len() > k - 11 {
if msg.len() + 11 > k {
return Err(Error::MessageTooLong);
}

Expand Down Expand Up @@ -195,4 +195,13 @@ mod tests {
}
}
}

#[test]
fn test_encrypt_tiny_no_crash() {
let mut rng = ChaCha8Rng::from_seed([42; 32]);
let k = 8;
let message = vec![1u8; 4];
let res = pkcs1v15_encrypt_pad(&mut rng, &message, k);
assert_eq!(res, Err(Error::MessageTooLong));
}
}
12 changes: 4 additions & 8 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ impl RsaPrivateKey {
let n_params = BoxedMontyParams::new(n.clone());
let n_c = NonZero::new(n.as_ref().clone()).unwrap();

let mut should_validate = false;

if primes.len() < 2 {
if !primes.is_empty() {
return Err(Error::NprimesTooSmall);
Expand All @@ -295,7 +293,6 @@ impl RsaPrivateKey {
let (p, q) = recover_primes(&n_c, &e, &d)?;
primes.push(p);
primes.push(q);
should_validate = true;
}

let mut k = RsaPrivateKey {
Expand All @@ -309,10 +306,8 @@ impl RsaPrivateKey {
precomputed: None,
};

// Validate the key if we had to recover the primes.
if should_validate {
k.validate()?;
}
// Alaways validate the key, to ensure precompute can't fail
k.validate()?;

// precompute when possible, ignore error otherwise.
let _ = k.precompute();
Expand Down Expand Up @@ -877,7 +872,8 @@ mod tests {
.iter()
.map(|p| BoxedUint::from_be_slice(p, bits / 2).unwrap())
.collect();
RsaPrivateKey::from_components(n, e, d, primes).unwrap();
let res = RsaPrivateKey::from_components(n, e, d, primes);
assert_eq!(res, Err(Error::InvalidModulus));
}

#[test]
Expand Down

0 comments on commit d675b13

Please sign in to comment.