diff options
| -rw-r--r-- | embassy-stm32/src/cryp/mod.rs | 1 | ||||
| -rw-r--r-- | tests/stm32/Cargo.toml | 11 | ||||
| -rw-r--r-- | tests/stm32/src/bin/cryp.rs | 71 |
3 files changed, 80 insertions, 3 deletions
diff --git a/embassy-stm32/src/cryp/mod.rs b/embassy-stm32/src/cryp/mod.rs index fef5def6a..bb64fa423 100644 --- a/embassy-stm32/src/cryp/mod.rs +++ b/embassy-stm32/src/cryp/mod.rs | |||
| @@ -5,7 +5,6 @@ use core::marker::PhantomData; | |||
| 5 | 5 | ||
| 6 | use embassy_hal_internal::{into_ref, PeripheralRef}; | 6 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 7 | 7 | ||
| 8 | use crate::rcc::sealed::RccPeripheral; | ||
| 9 | use crate::{interrupt, pac, peripherals, Peripheral}; | 8 | use crate::{interrupt, pac, peripherals, Peripheral}; |
| 10 | 9 | ||
| 11 | const DES_BLOCK_SIZE: usize = 8; // 64 bits | 10 | const DES_BLOCK_SIZE: usize = 8; // 64 bits |
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 828a28e2c..37519ba11 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -16,8 +16,8 @@ stm32f767zi = ["embassy-stm32/stm32f767zi", "chrono", "not-gpdma", "eth", "rng"] | |||
| 16 | stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac"] | 16 | stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac"] |
| 17 | stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"] | 17 | stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"] |
| 18 | stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash"] | 18 | stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash"] |
| 19 | stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash"] | 19 | stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash", "cryp"] |
| 20 | stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash"] | 20 | stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash", "cryp"] |
| 21 | stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"] | 21 | stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"] |
| 22 | stm32l073rz = ["embassy-stm32/stm32l073rz", "cm0", "not-gpdma", "rng"] | 22 | stm32l073rz = ["embassy-stm32/stm32l073rz", "cm0", "not-gpdma", "rng"] |
| 23 | stm32l152re = ["embassy-stm32/stm32l152re", "chrono", "not-gpdma"] | 23 | stm32l152re = ["embassy-stm32/stm32l152re", "chrono", "not-gpdma"] |
| @@ -33,6 +33,7 @@ stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"] | |||
| 33 | stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"] | 33 | stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"] |
| 34 | stm32h503rb = ["embassy-stm32/stm32h503rb", "rng"] | 34 | stm32h503rb = ["embassy-stm32/stm32h503rb", "rng"] |
| 35 | 35 | ||
| 36 | cryp = [] | ||
| 36 | hash = [] | 37 | hash = [] |
| 37 | eth = ["embassy-executor/task-arena-size-16384"] | 38 | eth = ["embassy-executor/task-arena-size-16384"] |
| 38 | rng = [] | 39 | rng = [] |
| @@ -80,6 +81,7 @@ portable-atomic = { version = "1.5", features = [] } | |||
| 80 | chrono = { version = "^0.4", default-features = false, optional = true} | 81 | chrono = { version = "^0.4", default-features = false, optional = true} |
| 81 | sha2 = { version = "0.10.8", default-features = false } | 82 | sha2 = { version = "0.10.8", default-features = false } |
| 82 | hmac = "0.12.1" | 83 | hmac = "0.12.1" |
| 84 | aes-gcm = {version = "0.10.3", default-features = false, features = ["aes", "heapless"] } | ||
| 83 | 85 | ||
| 84 | # BEGIN TESTS | 86 | # BEGIN TESTS |
| 85 | # Generated by gen_test.py. DO NOT EDIT. | 87 | # Generated by gen_test.py. DO NOT EDIT. |
| @@ -89,6 +91,11 @@ path = "src/bin/can.rs" | |||
| 89 | required-features = [ "can",] | 91 | required-features = [ "can",] |
| 90 | 92 | ||
| 91 | [[bin]] | 93 | [[bin]] |
| 94 | name = "cryp" | ||
| 95 | path = "src/bin/cryp.rs" | ||
| 96 | required-features = [ "hash",] | ||
| 97 | |||
| 98 | [[bin]] | ||
| 92 | name = "dac" | 99 | name = "dac" |
| 93 | path = "src/bin/dac.rs" | 100 | path = "src/bin/dac.rs" |
| 94 | required-features = [ "dac",] | 101 | required-features = [ "dac",] |
diff --git a/tests/stm32/src/bin/cryp.rs b/tests/stm32/src/bin/cryp.rs new file mode 100644 index 000000000..59c85f258 --- /dev/null +++ b/tests/stm32/src/bin/cryp.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | // required-features: cryp | ||
| 2 | #![no_std] | ||
| 3 | #![no_main] | ||
| 4 | |||
| 5 | #[path = "../common.rs"] | ||
| 6 | mod common; | ||
| 7 | |||
| 8 | use aes_gcm::aead::heapless::Vec; | ||
| 9 | use aes_gcm::aead::{AeadInPlace, KeyInit}; | ||
| 10 | use aes_gcm::Aes128Gcm; | ||
| 11 | use common::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_stm32::cryp::*; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let p: embassy_stm32::Peripherals = embassy_stm32::init(config()); | ||
| 19 | |||
| 20 | const PAYLOAD1: &[u8] = b"payload data 1 ;zdfhzdfhS;GKJASBDG;ASKDJBAL,zdfhzdfhzdfhzdfhvljhb,jhbjhb,sdhsdghsdhsfhsghzdfhzdfhzdfhzdfdhsdthsthsdhsgaadfhhgkdgfuoyguoft6783567"; | ||
| 21 | const PAYLOAD2: &[u8] = b"payload data 2 ;SKEzdfhzdfhzbhgvljhb,jhbjhb,sdhsdghsdhsfhsghshsfhshstsdthadfhsdfjhsfgjsfgjxfgjzdhgDFghSDGHjtfjtjszftjzsdtjhstdsdhsdhsdhsdhsdthsthsdhsgfh"; | ||
| 22 | const AAD1: &[u8] = b"additional data 1 stdargadrhaethaethjatjatjaetjartjstrjsfkk;'jopofyuisrteytweTASTUIKFUKIXTRDTEREharhaeryhaterjartjarthaethjrtjarthaetrhartjatejatrjsrtjartjyt1"; | ||
| 23 | const AAD2: &[u8] = b"additional data 2 stdhthsthsthsrthsrthsrtjdykjdukdyuldadfhsdghsdghsdghsadghjk'hioethjrtjarthaetrhartjatecfgjhzdfhgzdfhzdfghzdfhzdfhzfhjatrjsrtjartjytjfytjfyg"; | ||
| 24 | |||
| 25 | let hw_cryp = Cryp::new(p.CRYP); | ||
| 26 | let key: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | ||
| 27 | let mut ciphertext: [u8; PAYLOAD1.len() + PAYLOAD2.len()] = [0; PAYLOAD1.len() + PAYLOAD2.len()]; | ||
| 28 | let mut plaintext: [u8; PAYLOAD1.len() + PAYLOAD2.len()] = [0; PAYLOAD1.len() + PAYLOAD2.len()]; | ||
| 29 | let iv: [u8; 12] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | ||
| 30 | |||
| 31 | // Encrypt in hardware using AES-GCM 128-bit | ||
| 32 | let aes_gcm = AesGcm::new(&key, &iv); | ||
| 33 | let mut gcm_encrypt = hw_cryp.start(&aes_gcm, Direction::Encrypt); | ||
| 34 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD1, false); | ||
| 35 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD2, true); | ||
| 36 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD1, &mut ciphertext[..PAYLOAD1.len()], false); | ||
| 37 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD2, &mut ciphertext[PAYLOAD1.len()..], true); | ||
| 38 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); | ||
| 39 | |||
| 40 | // Decrypt in hardware using AES-GCM 128-bit | ||
| 41 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt); | ||
| 42 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD1, false); | ||
| 43 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD2, true); | ||
| 44 | hw_cryp.payload_blocking(&mut gcm_decrypt, &ciphertext, &mut plaintext, true); | ||
| 45 | let decrypt_tag = hw_cryp.finish_blocking(gcm_decrypt); | ||
| 46 | |||
| 47 | info!("AES-GCM Ciphertext: {:?}", ciphertext); | ||
| 48 | info!("AES-GCM Plaintext: {:?}", plaintext); | ||
| 49 | defmt::assert!(PAYLOAD1 == &plaintext[..PAYLOAD1.len()]); | ||
| 50 | defmt::assert!(PAYLOAD2 == &plaintext[PAYLOAD1.len()..]); | ||
| 51 | defmt::assert!(encrypt_tag == decrypt_tag); | ||
| 52 | |||
| 53 | // Encrypt in software using AES-GCM 128-bit | ||
| 54 | let mut payload_vec: Vec<u8, { PAYLOAD1.len() + PAYLOAD2.len() + 16 }> = Vec::from_slice(&PAYLOAD1).unwrap(); | ||
| 55 | payload_vec.extend_from_slice(&PAYLOAD2).unwrap(); | ||
| 56 | let cipher = Aes128Gcm::new(&key.into()); | ||
| 57 | let mut aad: Vec<u8, { AAD1.len() + AAD2.len() }> = Vec::from_slice(&AAD1).unwrap(); | ||
| 58 | aad.extend_from_slice(&AAD2).unwrap(); | ||
| 59 | let _ = cipher.encrypt_in_place(&iv.into(), &aad, &mut payload_vec); | ||
| 60 | |||
| 61 | defmt::assert!(ciphertext == payload_vec[0..ciphertext.len()]); | ||
| 62 | defmt::assert!( | ||
| 63 | encrypt_tag == payload_vec[ciphertext.len()..ciphertext.len() + encrypt_tag.len()] | ||
| 64 | ); | ||
| 65 | |||
| 66 | // Decrypt in software using AES-GCM 128-bit | ||
| 67 | let _ = cipher.decrypt_in_place(&iv.into(), &aad, &mut payload_vec); | ||
| 68 | |||
| 69 | info!("Test OK"); | ||
| 70 | cortex_m::asm::bkpt(); | ||
| 71 | } | ||
