|
1 | 1 | #![cfg(chacha20_poly1305)] |
2 | 2 |
|
3 | 3 | use wolfssl_wolfcrypt::chacha20_poly1305::*; |
| 4 | +use wolfssl_wolfcrypt::sys; |
4 | 5 |
|
5 | 6 | #[test] |
6 | 7 | fn test_chacha20_poly1305_1() { |
@@ -274,6 +275,34 @@ fn test_xchacha20_poly1305() { |
274 | 275 | assert_eq!(plaintext_buffer, PLAINTEXT); |
275 | 276 | } |
276 | 277 |
|
| 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 | + |
277 | 306 | // --------------------------------------------------------------------------- |
278 | 307 | // ChaCha20-Poly1305 aead trait implementations |
279 | 308 | // --------------------------------------------------------------------------- |
|
0 commit comments