Skip to content

Commit

Permalink
test: Cover all aes modes
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Apr 11, 2024
1 parent dfc6d86 commit 1e73187
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
4 changes: 4 additions & 0 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ const ALIGN_SIZE: usize = core::mem::size_of::<u32>();

pub enum Mode {
Encryption128 = 0,
#[cfg(any(esp32, esp32s2))]
Encryption192 = 1,
Encryption256 = 2,
Decryption128 = 4,
#[cfg(any(esp32, esp32s2))]
Decryption192 = 5,
Decryption256 = 6,
}

Expand Down
83 changes: 78 additions & 5 deletions hil-test/tests/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ mod tests {
}

#[test]
fn test_aes_encryption(mut ctx: Context<'static>) {
fn test_aes_128_encryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let encrypted_message = [
0xb3, 0xc8, 0xd2, 0x3b, 0xa7, 0x36, 0x5f, 0x18, 0x61, 0x70, 0x0, 0x3e, 0xd9, 0x3a,
0x31, 0x96,
];

// create an array with aes128 key size
let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

// create an array with aes block size
let mut block_buf = [0_u8; 16];
block_buf[..plaintext.len()].copy_from_slice(plaintext);

Expand All @@ -59,20 +57,95 @@ mod tests {
}

#[test]
fn test_aes_decryption(mut ctx: Context<'static>) {
fn test_aes_128_decryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let mut encrypted_message = [
0xb3, 0xc8, 0xd2, 0x3b, 0xa7, 0x36, 0x5f, 0x18, 0x61, 0x70, 0x0, 0x3e, 0xd9, 0x3a,
0x31, 0x96,
];

// create an array with aes128 key size
let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

ctx.aes
.process(&mut encrypted_message, Mode::Decryption128, &keybuf);
assert_eq!(&encrypted_message[..plaintext.len()], plaintext);
}

#[test]
#[cfg(any(feature = "esp32", feature = "esp32s2"))]
fn test_aes_192_encryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let encrypted_message = [
0x79, 0x88, 0x3f, 0x9d, 0x67, 0x27, 0xf4, 0x18, 0x3, 0xe3, 0xc6, 0x6a, 0x2e, 0x76,
0xb6, 0xf7,
];

let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

let mut block_buf = [0_u8; 16];
block_buf[..plaintext.len()].copy_from_slice(plaintext);

let mut block = block_buf.clone();
ctx.aes.process(&mut block, Mode::Encryption192, &keybuf);
assert_eq!(block, encrypted_message);
}

#[test]
#[cfg(any(feature = "esp32", feature = "esp32s2"))]
fn test_aes_192_decryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let mut encrypted_message = [
0x79, 0x88, 0x3f, 0x9d, 0x67, 0x27, 0xf4, 0x18, 0x3, 0xe3, 0xc6, 0x6a, 0x2e, 0x76,
0xb6, 0xf7,
];

let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

ctx.aes
.process(&mut encrypted_message, Mode::Decryption192, &keybuf);
assert_eq!(&encrypted_message[..plaintext.len()], plaintext);
}

#[test]
fn test_aes_256_encryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let encrypted_message = [
0x0, 0x63, 0x3f, 0x2, 0xa4, 0x53, 0x9, 0x72, 0x20, 0x6d, 0xc9, 0x8, 0x7c, 0xe5, 0xfd,
0xc,
];

let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

let mut block_buf = [0_u8; 16];
block_buf[..plaintext.len()].copy_from_slice(plaintext);

let mut block = block_buf.clone();
ctx.aes.process(&mut block, Mode::Encryption256, &keybuf);
assert_eq!(block, encrypted_message);
}

#[test]
fn test_aes_256_decryption(mut ctx: Context<'static>) {
let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();
let mut encrypted_message = [
0x0, 0x63, 0x3f, 0x2, 0xa4, 0x53, 0x9, 0x72, 0x20, 0x6d, 0xc9, 0x8, 0x7c, 0xe5, 0xfd,
0xc,
];

let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

ctx.aes
.process(&mut encrypted_message, Mode::Decryption256, &keybuf);
assert_eq!(&encrypted_message[..plaintext.len()], plaintext);
}
}

0 comments on commit 1e73187

Please sign in to comment.