Skip to content

Commit 9c0ccd4

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

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/lib.rs

Lines changed: 20 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,14 @@ mod test {
461461
cipher.decrypt([0u8; 64].as_slice(), [0u8; 32].as_mut_slice()),
462462
Err(Error::InsufficientBufferSize)
463463
));
464+
assert!(matches!(
465+
cipher.decrypt([0u8; 0].as_slice(), [0u8; 32768].as_mut_slice()),
466+
Ok(())
467+
));
468+
assert!(matches!(
469+
cipher.decrypt([0u8; crate::AES_BLOCK_SIZE].as_slice(), [0u8; 32768].as_mut_slice()),
470+
Ok(())
471+
));
464472

465473
let cipher = Aes128Cbc::new([0u8; 16].as_slice(), [0u8; 16].as_slice()).unwrap();
466474
assert!(matches!(
@@ -475,5 +483,13 @@ mod test {
475483
cipher.decrypt([0u8; 64].as_slice(), [0u8; 32].as_mut_slice()),
476484
Err(Error::InsufficientBufferSize)
477485
));
486+
assert!(matches!(
487+
cipher.decrypt([0u8; 0].as_slice(), [0u8; 32768].as_mut_slice()),
488+
Ok(())
489+
));
490+
assert!(matches!(
491+
cipher.decrypt([0u8; crate::AES_BLOCK_SIZE].as_slice(), [0u8; 32768].as_mut_slice()),
492+
Ok(())
493+
));
478494
}
479495
}

0 commit comments

Comments
 (0)