Skip to content

Commit e4b9630

Browse files
author
Jeff Frontz
committed
Use ciphertext buffer length for determining ciphertext length
1 parent 38c2170 commit e4b9630

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ pub trait AesBlockCipher: FfiAesCipher {
8181

8282
/// Decrypt the contents of `ciphertext` and place the result in the `plaintext` out parameter
8383
fn decrypt(&self, ciphertext: &[u8], plaintext: &mut [u8]) -> Result<(), Error> {
84-
if plaintext.len() % AES_BLOCK_SIZE != 0 {
84+
if ciphertext.len() % AES_BLOCK_SIZE != 0 {
8585
return Err(Error::NonBlockSizeAlignedBuffer);
8686
}
8787
if plaintext.len() < ciphertext.len() {
8888
return Err(Error::InsufficientBufferSize);
8989
}
9090

91-
let num_blocks = plaintext.len() / AES_BLOCK_SIZE;
91+
let num_blocks = ciphertext.len() / AES_BLOCK_SIZE;
9292
self.ffi_decrypt(num_blocks, plaintext, ciphertext);
9393

9494
Ok(())
@@ -238,14 +238,14 @@ pub trait AesCbcBlockCipher: FfiAesCbcCipher {
238238
}
239239

240240
fn decrypt(&self, ciphertext: &[u8], plaintext: &mut [u8]) -> Result<(), Error> {
241-
if plaintext.len() % AES_BLOCK_SIZE != 0 {
241+
if ciphertext.len() % AES_BLOCK_SIZE != 0 {
242242
return Err(Error::NonBlockSizeAlignedBuffer);
243243
}
244244
if plaintext.len() < ciphertext.len() {
245245
return Err(Error::InsufficientBufferSize);
246246
}
247247

248-
let num_blocks = plaintext.len() / AES_BLOCK_SIZE;
248+
let num_blocks = ciphertext.len() / AES_BLOCK_SIZE;
249249
self.ffi_decrypt(num_blocks, plaintext, ciphertext);
250250

251251
Ok(())
@@ -461,6 +461,11 @@ mod test {
461461
cipher.decrypt([0u8; 64].as_slice(), [0u8; 32].as_mut_slice()),
462462
Err(Error::InsufficientBufferSize)
463463
));
464+
assert!(matches!(cipher.decrypt([0u8; 0].as_slice(), [0u8; 32768].as_mut_slice()), Ok(())));
465+
assert!(matches!(
466+
cipher.decrypt([0u8; crate::AES_BLOCK_SIZE].as_slice(), [0u8; 32768].as_mut_slice()),
467+
Ok(())
468+
));
464469

465470
let cipher = Aes128Cbc::new([0u8; 16].as_slice(), [0u8; 16].as_slice()).unwrap();
466471
assert!(matches!(
@@ -475,5 +480,10 @@ mod test {
475480
cipher.decrypt([0u8; 64].as_slice(), [0u8; 32].as_mut_slice()),
476481
Err(Error::InsufficientBufferSize)
477482
));
483+
assert!(matches!(cipher.decrypt([0u8; 0].as_slice(), [0u8; 32768].as_mut_slice()), Ok(())));
484+
assert!(matches!(
485+
cipher.decrypt([0u8; crate::AES_BLOCK_SIZE].as_slice(), [0u8; 32768].as_mut_slice()),
486+
Ok(())
487+
));
478488
}
479489
}

0 commit comments

Comments
 (0)