esp-hal/hil-test/tests/aes.rs
Sergio Gasquez Arcos baea915935
Add HIL testing (#1297)
* Create the `hil-test` package

* Add a simple integration test to verify basic GPIO functionality

* WIP

* feat: Update with esp-hal unification

* build: Update dependencies

* feat: Add a simple CI workflow test

* ci: Avoid using a gh-hosted-runner to build

* ci: Remove building bins in gh-hosted-runner

* ci: Remove HIL Gpio CI test

* ci: Test all the available tests

* test: Add spi_full_duplex test

* docs: Add documentation

* test: Add uart test

* style: Remove unused imports

* docs: Update wiring, document H2 VM

* ci: Enable H2 tests

* ci: Add rust-cache action

* docs: Document H2 vm

* test: Add timeout

* ci: Enable ESP32-C3 tests

* feat: Add timeouts

* feat: Add aes test

* ci: Avoid running CI workflow when we change hil-test stuff

* test: Remove warnings

* feat: Address feedback

* feat: Update features names and spi methods

* ci: Remove rust-cache action

* Update HIL to probe-rs#2292 (#1307)

* feat: Update probe-rs/embedded-test to probe-rs#2292

* feat: Remove lib

* ci: Use a matrix

* ci: Enable ESP32C3

* feat: Add a way to cfg away test for unsuported peripherals

* ci: Update trigger conditions

* feat: Update pins to make it work on s3

* feat: Changes enabling S3

* feat: Remove log feature

* feat: Adapt for rebase

* feat: Remove env

* feat: enable S3

* chore: Remove todo

* build: Pin dependencies

* feat: Add target alias

* docs: Update readme

* fix: Fix traits imports after rebase. Use debug

* build: Remove lto

* feat: Build tests on release mode

---------

Co-authored-by: Jesse Braham <jesse@beta7.io>
2024-03-21 14:28:27 +00:00

100 lines
2.3 KiB
Rust

//! AES Test
#![no_std]
#![no_main]
use defmt_rtt as _;
use esp_hal::{
aes::{Aes, Mode},
peripherals::Peripherals,
};
struct Context<'a> {
aes: Aes<'a>,
}
impl Context<'_> {
pub fn init() -> Self {
let peripherals = Peripherals::take();
let aes = Aes::new(peripherals.AES);
Context { aes }
}
}
#[cfg(not(any(
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s3"
)))]
mod not_test {
#[esp_hal::entry]
fn main() -> ! {
semihosting::process::exit(0)
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
}
#[cfg(test)]
#[cfg(any(
feature = "esp32c3",
feature = "esp32c6",
feature = "esp32h2",
feature = "esp32s3"
))]
#[embedded_test::tests]
mod tests {
use defmt::assert_eq;
use super::*;
#[init]
fn init() -> Context<'static> {
Context::init()
}
#[test]
fn test_aes_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);
let mut block = block_buf.clone();
ctx.aes.process(&mut block, Mode::Encryption128, &keybuf);
assert_eq!(block, encrypted_message);
}
#[test]
fn test_aes_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);
}
}