Skip to content

Commit 201ad81

Browse files
fix: handle tiny keys
1 parent a801f53 commit 201ad81

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/algorithms/pkcs1v15.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn pkcs1v15_encrypt_pad<R>(
4141
where
4242
R: CryptoRngCore + ?Sized,
4343
{
44-
if msg.len() > k - 11 {
44+
if msg.len() + 11 > k {
4545
return Err(Error::MessageTooLong);
4646
}
4747

@@ -195,4 +195,13 @@ mod tests {
195195
}
196196
}
197197
}
198+
199+
#[test]
200+
fn test_encrypt_tiny_no_crash() {
201+
let mut rng = ChaCha8Rng::from_seed([42; 32]);
202+
let k = 8;
203+
let message = vec![1u8; 4];
204+
let res = pkcs1v15_encrypt_pad(&mut rng, &message, k);
205+
assert_eq!(res, Err(Error::MessageTooLong));
206+
}
198207
}

0 commit comments

Comments
 (0)