aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaleb Garrett <[email protected]>2024-02-24 16:31:43 -0500
committerCaleb Garrett <[email protected]>2024-02-25 20:59:07 -0500
commit236fc6f650af41980af05ef03a3901b2dfcfc381 (patch)
tree844bd267f8858a5241c9c50358880d355c35a76c
parentf352b6d68b17fee886af58494b7e793cea3ea383 (diff)
Add CRYP test.
-rw-r--r--embassy-stm32/src/cryp/mod.rs1
-rw-r--r--tests/stm32/Cargo.toml11
-rw-r--r--tests/stm32/src/bin/cryp.rs71
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
6use embassy_hal_internal::{into_ref, PeripheralRef}; 6use embassy_hal_internal::{into_ref, PeripheralRef};
7 7
8use crate::rcc::sealed::RccPeripheral;
9use crate::{interrupt, pac, peripherals, Peripheral}; 8use crate::{interrupt, pac, peripherals, Peripheral};
10 9
11const DES_BLOCK_SIZE: usize = 8; // 64 bits 10const 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"]
16stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac"] 16stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac"]
17stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"] 17stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"]
18stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash"] 18stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash"]
19stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash"] 19stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash", "cryp"]
20stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash"] 20stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash", "cryp"]
21stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"] 21stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"]
22stm32l073rz = ["embassy-stm32/stm32l073rz", "cm0", "not-gpdma", "rng"] 22stm32l073rz = ["embassy-stm32/stm32l073rz", "cm0", "not-gpdma", "rng"]
23stm32l152re = ["embassy-stm32/stm32l152re", "chrono", "not-gpdma"] 23stm32l152re = ["embassy-stm32/stm32l152re", "chrono", "not-gpdma"]
@@ -33,6 +33,7 @@ stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"]
33stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"] 33stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"]
34stm32h503rb = ["embassy-stm32/stm32h503rb", "rng"] 34stm32h503rb = ["embassy-stm32/stm32h503rb", "rng"]
35 35
36cryp = []
36hash = [] 37hash = []
37eth = ["embassy-executor/task-arena-size-16384"] 38eth = ["embassy-executor/task-arena-size-16384"]
38rng = [] 39rng = []
@@ -80,6 +81,7 @@ portable-atomic = { version = "1.5", features = [] }
80chrono = { version = "^0.4", default-features = false, optional = true} 81chrono = { version = "^0.4", default-features = false, optional = true}
81sha2 = { version = "0.10.8", default-features = false } 82sha2 = { version = "0.10.8", default-features = false }
82hmac = "0.12.1" 83hmac = "0.12.1"
84aes-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"
89required-features = [ "can",] 91required-features = [ "can",]
90 92
91[[bin]] 93[[bin]]
94name = "cryp"
95path = "src/bin/cryp.rs"
96required-features = [ "hash",]
97
98[[bin]]
92name = "dac" 99name = "dac"
93path = "src/bin/dac.rs" 100path = "src/bin/dac.rs"
94required-features = [ "dac",] 101required-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"]
6mod common;
7
8use aes_gcm::aead::heapless::Vec;
9use aes_gcm::aead::{AeadInPlace, KeyInit};
10use aes_gcm::Aes128Gcm;
11use common::*;
12use embassy_executor::Spawner;
13use embassy_stm32::cryp::*;
14use {defmt_rtt as _, panic_probe as _};
15
16#[embassy_executor::main]
17async 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}