Skip to content

Commit

Permalink
test: Cover all aes modes (esp-rs#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Apr 12, 2024
1 parent 5d61074 commit 3068d25
Show file tree
Hide file tree
Showing 4 changed files with 138 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
4 changes: 4 additions & 0 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ publish = false
name = "aes"
harness = false

[[test]]
name = "clock_monitor"
harness = false

[[test]]
name = "crc"
harness = false
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 @@ -38,19 +38,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 @@ -60,20 +58,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);
}
}
52 changes: 52 additions & 0 deletions hil-test/tests/clock_monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Clock Monitor Test

//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

#![no_std]
#![no_main]

use defmt_rtt as _;
use esp_backtrace as _;
use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, rtc_cntl::Rtc};

struct Context<'a> {
rtc: Rtc<'a>,
}

impl Context<'_> {
pub fn init() -> Self {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
ClockControl::boot_defaults(system.clock_control).freeze();

let rtc = Rtc::new(peripherals.LPWR, None);

Context { rtc }
}
}

#[cfg(test)]
#[embedded_test::tests]
mod tests {
use super::*;

#[init]
fn init() -> Context<'static> {
Context::init()
}

#[test]
fn test_estimated_clock(mut ctx: Context<'static>) {
// We call the function twice since sometimes the first call gives a
// wrong result
ctx.rtc.estimate_xtal_frequency();
let estimated_xtal_freq = ctx.rtc.estimate_xtal_frequency();

#[cfg(feature = "esp32c2")] // 26 MHz
assert!(estimated_xtal_freq > 22 && estimated_xtal_freq < 30);
#[cfg(feature = "esp32h2")] // 32 MHz
assert!(estimated_xtal_freq > 26 && estimated_xtal_freq < 36);
#[cfg(not(any(feature = "esp32h2", feature = "esp32c2")))] // 40 MHz
assert!(estimated_xtal_freq > 34 && estimated_xtal_freq < 44);
}
}

0 comments on commit 3068d25

Please sign in to comment.