Test: Testing different key nonce size combinations. Allowing dst_ciphertext to be greater than PT too, and testing length restrictions.
@@ -143,7 +143,7 @@ pub fn chacha20_encrypt(
plaintext: &[u8],
dst_ciphertext: &mut [u8],
) -> Result<(), UnknownCryptoError> {
- if plaintext.len() != dst_ciphertext.len() {
+ if dst_ciphertext.len() < plaintext.len() {
return Err(UnknownCryptoError);
}
// Err on empty `plaintext` because the `dst_ciphertext` is user-controlled, so if we
@@ -224,6 +224,16 @@ fn test_bad_key_nonce_size() {
assert!(chacha_state.init_state(&[0u8; 30], &[0u8; 15]).is_err());
}
+#[test]
+fn test_diff_ct_pt_len() {
+
+ let mut dst = [0u8; 64];
+
+ assert!(chacha20_encrypt(&[0u8; 32], &[0u8; 12], 0, &[0u8; 65], &mut dst).is_err());
+ assert!(chacha20_encrypt(&[0u8; 32], &[0u8; 12], 0, &[0u8; 63], &mut dst).is_ok());
+ assert!(chacha20_encrypt(&[0u8; 32], &[0u8; 12], 0, &[0u8; 64], &mut dst).is_ok());
+}
+
