Skip to content

Commit c6f8e84

Browse files
Merge pull request #10267 from holtrop-wolfssl/rust-chacha20_poly1305-oneshot-buffer-length-check
Rust wrapper: add buffer size checks in Rust wrapper for ChaCha20_Poly1305 one-shot encrypt/decrypt wrappers
2 parents 98cd7fe + a2b1f58 commit c6f8e84

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

wrapper/rust/wolfssl-wolfcrypt/src/chacha20_poly1305.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ impl ChaCha20Poly1305 {
7474
if auth_tag.len() != Self::AUTH_TAG_SIZE {
7575
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
7676
}
77+
if plaintext.len() < ciphertext.len() {
78+
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
79+
}
7780
let aad_size = crate::buffer_len_to_u32(aad.len())?;
7881
let ciphertext_size = crate::buffer_len_to_u32(ciphertext.len())?;
7982
let rc = unsafe {
@@ -116,6 +119,9 @@ impl ChaCha20Poly1305 {
116119
if auth_tag.len() != Self::AUTH_TAG_SIZE {
117120
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
118121
}
122+
if ciphertext.len() < plaintext.len() {
123+
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
124+
}
119125
let aad_size = crate::buffer_len_to_u32(aad.len())?;
120126
let plaintext_size = crate::buffer_len_to_u32(plaintext.len())?;
121127
let rc = unsafe {

wrapper/rust/wolfssl-wolfcrypt/tests/test_chacha20_poly1305.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![cfg(chacha20_poly1305)]
22

33
use wolfssl_wolfcrypt::chacha20_poly1305::*;
4+
use wolfssl_wolfcrypt::sys;
45

56
#[test]
67
fn test_chacha20_poly1305_1() {
@@ -274,6 +275,34 @@ fn test_xchacha20_poly1305() {
274275
assert_eq!(plaintext_buffer, PLAINTEXT);
275276
}
276277

278+
#[test]
279+
fn test_chacha20_poly1305_encrypt_short_ciphertext_buffer() {
280+
let key = [0x55u8; ChaCha20Poly1305::KEYSIZE];
281+
let iv = [0x66u8; ChaCha20Poly1305::IV_SIZE];
282+
let aad = [];
283+
let plaintext = [0u8; 32];
284+
let mut ciphertext = [0u8; 16]; /* shorter than plaintext */
285+
let mut auth_tag = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
286+
let rc = ChaCha20Poly1305::encrypt(&key, &iv, &aad, &plaintext,
287+
&mut ciphertext, &mut auth_tag)
288+
.expect_err("encrypt() should fail with short ciphertext buffer");
289+
assert_eq!(rc, sys::wolfCrypt_ErrorCodes_BUFFER_E);
290+
}
291+
292+
#[test]
293+
fn test_chacha20_poly1305_decrypt_short_plaintext_buffer() {
294+
let key = [0x55u8; ChaCha20Poly1305::KEYSIZE];
295+
let iv = [0x66u8; ChaCha20Poly1305::IV_SIZE];
296+
let aad = [];
297+
let ciphertext = [0u8; 32];
298+
let mut plaintext = [0u8; 16]; /* shorter than ciphertext */
299+
let auth_tag = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
300+
let rc = ChaCha20Poly1305::decrypt(&key, &iv, &aad, &ciphertext,
301+
&auth_tag, &mut plaintext)
302+
.expect_err("decrypt() should fail with short plaintext buffer");
303+
assert_eq!(rc, sys::wolfCrypt_ErrorCodes_BUFFER_E);
304+
}
305+
277306
// ---------------------------------------------------------------------------
278307
// ChaCha20-Poly1305 aead trait implementations
279308
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)