From 201ad812a0fdbd43808b1717a66936a0348cf1b1 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 13 Nov 2024 20:46:48 +0100 Subject: [PATCH] fix: handle tiny keys --- src/algorithms/pkcs1v15.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/algorithms/pkcs1v15.rs b/src/algorithms/pkcs1v15.rs index c1f0779a..e813de11 100644 --- a/src/algorithms/pkcs1v15.rs +++ b/src/algorithms/pkcs1v15.rs @@ -41,7 +41,7 @@ pub(crate) fn pkcs1v15_encrypt_pad( where R: CryptoRngCore + ?Sized, { - if msg.len() > k - 11 { + if msg.len() + 11 > k { return Err(Error::MessageTooLong); } @@ -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)); + } }