Skip to content

Commit

Permalink
fix: handle tiny keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 18, 2024
1 parent a801f53 commit 201ad81
Showing 1 changed file with 10 additions and 1 deletion.
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));
}
}

0 comments on commit 201ad81

Please # to comment.