Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add initial support for the ESP32-H2 (#513)
- Add bare-bones PSRAM support for ESP32-S3 (#517)
- Add async support to the I2C driver (#519)
- Add initial support for AES in ESP32-H2 (#528)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/devices/esp32h2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cores = "single_core"

peripherals = [
# Peripherals available in the PAC:
# "aes",
"aes",
"apb_saradc",
# "assist_debug",
# "ds",
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
#[cfg_attr(esp32s2, path = "esp32s2.rs")]
#[cfg_attr(esp32c3, path = "esp32cX.rs")]
#[cfg_attr(esp32c6, path = "esp32cX.rs")]
#[cfg_attr(esp32h2, path = "esp32cX.rs")]
mod aes_spec_impl;

const ALIGN_SIZE: usize = core::mem::size_of::<u32>();
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/soc/esp32h2/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use pac::Interrupt;
pub(crate) use self::peripherals::*;

crate::peripherals! {
// AES => true,
AES => true,
APB_SARADC => true,
// ASSIST_DEBUG => true,
// DS => true,
Expand Down
110 changes: 110 additions & 0 deletions esp32h2-hal/examples/aes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#![no_std]
#![no_main]
use aes::{
cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit},
Aes128 as Aes128SW,
};
use esp32h2_hal::{
aes::{Aes, Aes128, Cipher, Key},
clock::ClockControl,
peripherals::Peripherals,
prelude::*,
systimer::SystemTimer,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;

rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

let mut aes = Aes::new(peripherals.AES, &mut system.peripheral_clock_control);

let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();

// 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 key = Key::<Aes128>::from(&keybuf);
let mut cipher = Cipher::new(&mut aes, &key);
let mut block = block_buf.clone();
let pre_hw_encrypt = SystemTimer::now();
cipher.encrypt_block(&mut block);
let post_hw_encrypt = SystemTimer::now();
println!(
"it took {} cycles for hw encrypt",
post_hw_encrypt - pre_hw_encrypt
);
let hw_encrypted = block.clone();
let pre_hw_decrypt = SystemTimer::now();
cipher.decrypt_block(&mut block);
let post_hw_decrypt = SystemTimer::now();
println!(
"it took {} cycles for hw decrypt",
post_hw_decrypt - pre_hw_decrypt
);
let hw_decrypted = block;

let key = GenericArray::from(keybuf);
let mut block = GenericArray::from(block_buf);
let cipher = Aes128SW::new(&key);
let pre_sw_encrypt = SystemTimer::now();
cipher.encrypt_block(&mut block);
let post_sw_encrypt = SystemTimer::now();
println!(
"it took {} cycles for sw encrypt",
post_sw_encrypt - pre_sw_encrypt
);
let sw_encrypted = block.clone();
let pre_sw_decrypt = SystemTimer::now();
cipher.decrypt_block(&mut block);
let post_sw_decrypt = SystemTimer::now();
println!(
"it took {} cycles for sw decrypt",
post_sw_decrypt - pre_sw_decrypt
);
let sw_decrypted = block;

assert!(eq(&sw_encrypted.into(), &hw_encrypted));
assert!(eq(&sw_decrypted.into(), &hw_decrypted));

println!("done");

loop {}
}
fn eq(slice1: &[u8; 16], slice2: &[u8; 16]) -> bool {
slice1.iter().zip(slice2.iter()).all(|(a, b)| a == b)
}