diff options
| author | Matt Johnston <[email protected]> | 2025-08-12 17:13:21 +0800 |
|---|---|---|
| committer | Matt Johnston <[email protected]> | 2025-08-15 17:16:53 +0800 |
| commit | d939d901e245c30f924e30a7605e378be0be5cca (patch) | |
| tree | 6b8ac18ab14d02b606200d0cb426a80e54ecb768 /tests/stm32/src | |
| parent | 37707a7c7c34100e00d803717bcf11836b708380 (diff) | |
stm32: Add hash test for sha512, varying lengths
sha512 is only supported by hash_v3 and hash_v4, so add a feature for
those chips.
Diffstat (limited to 'tests/stm32/src')
| -rw-r--r-- | tests/stm32/src/bin/hash.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/stm32/src/bin/hash.rs b/tests/stm32/src/bin/hash.rs index 2499f42ed..bb08d0cf1 100644 --- a/tests/stm32/src/bin/hash.rs +++ b/tests/stm32/src/bin/hash.rs | |||
| @@ -94,6 +94,34 @@ fn test_interrupt(hw_hasher: &mut Hash<'_, peripherals::HASH, Blocking>) { | |||
| 94 | defmt::assert!(hw_hmac == sw_hmac[..]); | 94 | defmt::assert!(hw_hmac == sw_hmac[..]); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | // This uses sha512, so only supported on hash_v3 and up | ||
| 98 | #[cfg(feature = "hash-v34")] | ||
| 99 | fn test_sizes(hw_hasher: &mut Hash<'_, peripherals::HASH, Blocking>) { | ||
| 100 | let in1 = b"4BPuGudaDK"; | ||
| 101 | let in2 = b"cfFIGf0XSNhFBQ5LaIqzjnRKDRkoWweJI06HLUcicIUGjpuDNfOTQNSrRxDoveDPlazeZtt06SIYO5CvHvsJ98XSfO9yJEMHoDpDAmNQtwZOPlKmdiagRXsJ7w7IjdKpQH6I2t"; | ||
| 102 | |||
| 103 | for i in 1..10 { | ||
| 104 | // sha512 block size is 128, so test around there | ||
| 105 | for j in [1, 1, 2, 3, 4, 5, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133] { | ||
| 106 | info!("test_sizes i {} j {}", i, j); | ||
| 107 | let mut sw = sha2::Sha512::new(); | ||
| 108 | let mut ctx = hw_hasher.start(Algorithm::SHA512, DataType::Width8, None); | ||
| 109 | |||
| 110 | sw.update(&in1[..i]); | ||
| 111 | sw.update(&in2[..j]); | ||
| 112 | hw_hasher.update_blocking(&mut ctx, &in1[..i]); | ||
| 113 | hw_hasher.update_blocking(&mut ctx, &in2[..j]); | ||
| 114 | |||
| 115 | let sw_digest = sw.finalize(); | ||
| 116 | let mut hw_digest = [0u8; 64]; | ||
| 117 | hw_hasher.finish_blocking(ctx, &mut hw_digest); | ||
| 118 | info!("Hardware: {:?}", hw_digest); | ||
| 119 | info!("Software: {:?}", sw_digest[..]); | ||
| 120 | defmt::assert!(hw_digest == *sw_digest); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 97 | #[embassy_executor::main] | 125 | #[embassy_executor::main] |
| 98 | async fn main(_spawner: Spawner) { | 126 | async fn main(_spawner: Spawner) { |
| 99 | let p: embassy_stm32::Peripherals = init(); | 127 | let p: embassy_stm32::Peripherals = init(); |
| @@ -103,6 +131,9 @@ async fn main(_spawner: Spawner) { | |||
| 103 | // Run it a second time to check hash-after-hmac | 131 | // Run it a second time to check hash-after-hmac |
| 104 | test_interrupt(&mut hw_hasher); | 132 | test_interrupt(&mut hw_hasher); |
| 105 | 133 | ||
| 134 | #[cfg(feature = "hash-v34")] | ||
| 135 | test_sizes(&mut hw_hasher); | ||
| 136 | |||
| 106 | info!("Test OK"); | 137 | info!("Test OK"); |
| 107 | cortex_m::asm::bkpt(); | 138 | cortex_m::asm::bkpt(); |
| 108 | } | 139 | } |
