Skip to content

Commit b96e0bc

Browse files
Yiming Linjustsmth
authored andcommitted
Adds AES CBC mode
1 parent 08c02c9 commit b96e0bc

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

aws-lc-rs/src/cipher.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,23 @@ impl EncryptingKey {
548548
Self::new(key, OperatingMode::CFB128)
549549
}
550550

551+
/// Constructs an `EncryptingKey` operating in cipher block chaining (CBC) mode using the provided key.
552+
///
553+
/// # ☠️ ️️️DANGER ☠️
554+
/// Offered for computability purposes only. This is an extremely dangerous mode, and
555+
/// very likely not what you want to use.
556+
///
557+
// # FIPS
558+
// Use this function with an `UnboundCipherKey` constructed with one of the following algorithms:
559+
// * `AES_128`
560+
// * `AES_256`
561+
//
562+
/// # Errors
563+
/// * [`Unspecified`]: Returned if there is an error constructing the `EncryptingKey`.
564+
pub fn cbc(key: UnboundCipherKey) -> Result<Self, Unspecified> {
565+
Self::new(key, OperatingMode::CBC)
566+
}
567+
551568
/// Constructs an `EncryptingKey` operating in electronic code book mode (ECB) using the provided key.
552569
///
553570
/// # ☠️ ️️️DANGER ☠️
@@ -672,6 +689,23 @@ impl DecryptingKey {
672689
Self::new(key, OperatingMode::CFB128)
673690
}
674691

692+
/// Constructs an `DecryptingKey` operating in cipher block chaining (CBC) mode using the provided key and context.
693+
///
694+
/// # ☠️ ️️️DANGER ☠️
695+
/// Offered for computability purposes only. This is an extremely dangerous mode, and
696+
/// very likely not what you want to use.
697+
///
698+
// # FIPS
699+
// Use this function with an `UnboundCipherKey` constructed with one of the following algorithms:
700+
// * `AES_128`
701+
// * `AES_256`
702+
//
703+
/// # Errors
704+
/// * [`Unspecified`]: Returned if there is an error during decryption.
705+
pub fn cbc(key: UnboundCipherKey) -> Result<DecryptingKey, Unspecified> {
706+
Self::new(key, OperatingMode::CBC)
707+
}
708+
675709
/// Constructs an `DecryptingKey` operating in electronic code book (ECB) mode using the provided key.
676710
///
677711
/// # ☠️ ️️️DANGER ☠️
@@ -944,6 +978,27 @@ mod tests {
944978
}
945979
}
946980

981+
#[test]
982+
fn test_aes_128_cbc() {
983+
let key = from_hex("000102030405060708090a0b0c0d0e0f").unwrap();
984+
// CBC mode requires input to be a multiple of block size (16 bytes)
985+
for i in 0..=3 {
986+
let size = i * 16; // Test with 0, 16, 32, 48 bytes
987+
helper_test_cipher_n_bytes(key.as_slice(), &AES_128, OperatingMode::CBC, size);
988+
}
989+
}
990+
991+
#[test]
992+
fn test_aes_256_cbc() {
993+
let key =
994+
from_hex("000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f").unwrap();
995+
// CBC mode requires input to be a multiple of block size (16 bytes)
996+
for i in 0..=3 {
997+
let size = i * 16; // Test with 0, 16, 32, 48 bytes
998+
helper_test_cipher_n_bytes(key.as_slice(), &AES_256, OperatingMode::CBC, size);
999+
}
1000+
}
1001+
9471002
#[test]
9481003
fn test_aes_128_ecb() {
9491004
let key = from_hex("000102030405060708090a0b0c0d0e0f").unwrap();

0 commit comments

Comments
 (0)