diff options
| author | Caleb Garrett <[email protected]> | 2024-02-12 20:33:04 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-02-12 20:33:04 -0500 |
| commit | d8b4922b3ced8645a6225dcb0e8b873364bc8337 (patch) | |
| tree | 0dc147bf6d88b45af57c19d23c596947b2ae5684 | |
| parent | 8c82d1bcbcb8b04f18e9fd2a2d388a316bc50b49 (diff) | |
Add STM32 HMAC function.
| -rw-r--r-- | embassy-stm32/src/hash/mod.rs | 73 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/hash.rs | 2 |
2 files changed, 61 insertions, 14 deletions
diff --git a/embassy-stm32/src/hash/mod.rs b/embassy-stm32/src/hash/mod.rs index f0c2c839a..bae538b5f 100644 --- a/embassy-stm32/src/hash/mod.rs +++ b/embassy-stm32/src/hash/mod.rs | |||
| @@ -100,8 +100,9 @@ pub enum DataType { | |||
| 100 | 100 | ||
| 101 | /// Stores the state of the HASH peripheral for suspending/resuming | 101 | /// Stores the state of the HASH peripheral for suspending/resuming |
| 102 | /// digest calculation. | 102 | /// digest calculation. |
| 103 | pub struct Context { | 103 | pub struct Context<'c> { |
| 104 | first_word_sent: bool, | 104 | first_word_sent: bool, |
| 105 | key_sent: bool, | ||
| 105 | buffer: [u8; HASH_BUFFER_LEN], | 106 | buffer: [u8; HASH_BUFFER_LEN], |
| 106 | buflen: usize, | 107 | buflen: usize, |
| 107 | algo: Algorithm, | 108 | algo: Algorithm, |
| @@ -110,8 +111,11 @@ pub struct Context { | |||
| 110 | str: u32, | 111 | str: u32, |
| 111 | cr: u32, | 112 | cr: u32, |
| 112 | csr: [u32; NUM_CONTEXT_REGS], | 113 | csr: [u32; NUM_CONTEXT_REGS], |
| 114 | key: HmacKey<'c>, | ||
| 113 | } | 115 | } |
| 114 | 116 | ||
| 117 | type HmacKey<'k> = Option<&'k [u8]>; | ||
| 118 | |||
| 115 | /// HASH driver. | 119 | /// HASH driver. |
| 116 | pub struct Hash<'d, T: Instance, D = NoDma> { | 120 | pub struct Hash<'d, T: Instance, D = NoDma> { |
| 117 | _peripheral: PeripheralRef<'d, T>, | 121 | _peripheral: PeripheralRef<'d, T>, |
| @@ -140,10 +144,11 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 140 | } | 144 | } |
| 141 | 145 | ||
| 142 | /// Starts computation of a new hash and returns the saved peripheral state. | 146 | /// Starts computation of a new hash and returns the saved peripheral state. |
| 143 | pub fn start(&mut self, algorithm: Algorithm, format: DataType) -> Context { | 147 | pub fn start<'c>(&mut self, algorithm: Algorithm, format: DataType, key: HmacKey<'c>) -> Context<'c> { |
| 144 | // Define a context for this new computation. | 148 | // Define a context for this new computation. |
| 145 | let mut ctx = Context { | 149 | let mut ctx = Context { |
| 146 | first_word_sent: false, | 150 | first_word_sent: false, |
| 151 | key_sent: false, | ||
| 147 | buffer: [0; HASH_BUFFER_LEN], | 152 | buffer: [0; HASH_BUFFER_LEN], |
| 148 | buflen: 0, | 153 | buflen: 0, |
| 149 | algo: algorithm, | 154 | algo: algorithm, |
| @@ -152,6 +157,7 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 152 | str: 0, | 157 | str: 0, |
| 153 | cr: 0, | 158 | cr: 0, |
| 154 | csr: [0; NUM_CONTEXT_REGS], | 159 | csr: [0; NUM_CONTEXT_REGS], |
| 160 | key, | ||
| 155 | }; | 161 | }; |
| 156 | 162 | ||
| 157 | // Set the data type in the peripheral. | 163 | // Set the data type in the peripheral. |
| @@ -181,6 +187,14 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 181 | #[cfg(any(hash_v3, hash_v4))] | 187 | #[cfg(any(hash_v3, hash_v4))] |
| 182 | T::regs().cr().modify(|w| w.set_algo(ctx.algo as u8)); | 188 | T::regs().cr().modify(|w| w.set_algo(ctx.algo as u8)); |
| 183 | 189 | ||
| 190 | // Configure HMAC mode if a key is provided. | ||
| 191 | if let Some(key) = ctx.key { | ||
| 192 | T::regs().cr().modify(|w| w.set_mode(true)); | ||
| 193 | if key.len() > 64 { | ||
| 194 | T::regs().cr().modify(|w| w.set_lkey(true)); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 184 | T::regs().cr().modify(|w| w.set_init(true)); | 198 | T::regs().cr().modify(|w| w.set_init(true)); |
| 185 | 199 | ||
| 186 | // Store and return the state of the peripheral. | 200 | // Store and return the state of the peripheral. |
| @@ -191,18 +205,30 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 191 | /// Restores the peripheral state using the given context, | 205 | /// Restores the peripheral state using the given context, |
| 192 | /// then updates the state with the provided data. | 206 | /// then updates the state with the provided data. |
| 193 | /// Peripheral state is saved upon return. | 207 | /// Peripheral state is saved upon return. |
| 194 | pub fn update_blocking(&mut self, ctx: &mut Context, input: &[u8]) { | 208 | pub fn update_blocking<'c>(&mut self, ctx: &mut Context<'c>, input: &[u8]) { |
| 209 | // Restore the peripheral state. | ||
| 210 | self.load_context(&ctx); | ||
| 211 | |||
| 212 | // Load the HMAC key if provided. | ||
| 213 | if !ctx.key_sent { | ||
| 214 | if let Some(key) = ctx.key { | ||
| 215 | self.accumulate_blocking(key); | ||
| 216 | T::regs().str().write(|w| w.set_dcal(true)); | ||
| 217 | // Block waiting for digest. | ||
| 218 | while !T::regs().sr().read().dcis() {} | ||
| 219 | } | ||
| 220 | ctx.key_sent = true; | ||
| 221 | } | ||
| 222 | |||
| 195 | let mut data_waiting = input.len() + ctx.buflen; | 223 | let mut data_waiting = input.len() + ctx.buflen; |
| 196 | if data_waiting < DIGEST_BLOCK_SIZE || (data_waiting < ctx.buffer.len() && !ctx.first_word_sent) { | 224 | if data_waiting < DIGEST_BLOCK_SIZE || (data_waiting < ctx.buffer.len() && !ctx.first_word_sent) { |
| 197 | // There isn't enough data to digest a block, so append it to the buffer. | 225 | // There isn't enough data to digest a block, so append it to the buffer. |
| 198 | ctx.buffer[ctx.buflen..ctx.buflen + input.len()].copy_from_slice(input); | 226 | ctx.buffer[ctx.buflen..ctx.buflen + input.len()].copy_from_slice(input); |
| 199 | ctx.buflen += input.len(); | 227 | ctx.buflen += input.len(); |
| 228 | self.store_context(ctx); | ||
| 200 | return; | 229 | return; |
| 201 | } | 230 | } |
| 202 | 231 | ||
| 203 | // Restore the peripheral state. | ||
| 204 | self.load_context(&ctx); | ||
| 205 | |||
| 206 | let mut ilen_remaining = input.len(); | 232 | let mut ilen_remaining = input.len(); |
| 207 | let mut input_start = 0; | 233 | let mut input_start = 0; |
| 208 | 234 | ||
| @@ -261,21 +287,30 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 261 | /// then updates the state with the provided data. | 287 | /// then updates the state with the provided data. |
| 262 | /// Peripheral state is saved upon return. | 288 | /// Peripheral state is saved upon return. |
| 263 | #[cfg(hash_v2)] | 289 | #[cfg(hash_v2)] |
| 264 | pub async fn update(&mut self, ctx: &mut Context, input: &[u8]) | 290 | pub async fn update<'c>(&mut self, ctx: &mut Context<'c>, input: &[u8]) |
| 265 | where | 291 | where |
| 266 | D: crate::hash::Dma<T>, | 292 | D: crate::hash::Dma<T>, |
| 267 | { | 293 | { |
| 294 | // Restore the peripheral state. | ||
| 295 | self.load_context(&ctx); | ||
| 296 | |||
| 297 | // Load the HMAC key if provided. | ||
| 298 | if !ctx.key_sent { | ||
| 299 | if let Some(key) = ctx.key { | ||
| 300 | self.accumulate(key).await; | ||
| 301 | } | ||
| 302 | ctx.key_sent = true; | ||
| 303 | } | ||
| 304 | |||
| 268 | let data_waiting = input.len() + ctx.buflen; | 305 | let data_waiting = input.len() + ctx.buflen; |
| 269 | if data_waiting < DIGEST_BLOCK_SIZE { | 306 | if data_waiting < DIGEST_BLOCK_SIZE { |
| 270 | // There isn't enough data to digest a block, so append it to the buffer. | 307 | // There isn't enough data to digest a block, so append it to the buffer. |
| 271 | ctx.buffer[ctx.buflen..ctx.buflen + input.len()].copy_from_slice(input); | 308 | ctx.buffer[ctx.buflen..ctx.buflen + input.len()].copy_from_slice(input); |
| 272 | ctx.buflen += input.len(); | 309 | ctx.buflen += input.len(); |
| 310 | self.store_context(ctx); | ||
| 273 | return; | 311 | return; |
| 274 | } | 312 | } |
| 275 | 313 | ||
| 276 | // Restore the peripheral state. | ||
| 277 | self.load_context(&ctx); | ||
| 278 | |||
| 279 | // Enable multiple DMA transfers. | 314 | // Enable multiple DMA transfers. |
| 280 | T::regs().cr().modify(|w| w.set_mdmat(true)); | 315 | T::regs().cr().modify(|w| w.set_mdmat(true)); |
| 281 | 316 | ||
| @@ -319,7 +354,7 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 319 | /// The digest buffer must be large enough to accomodate a digest for the selected algorithm. | 354 | /// The digest buffer must be large enough to accomodate a digest for the selected algorithm. |
| 320 | /// The largest returned digest size is 128 bytes for SHA-512. | 355 | /// The largest returned digest size is 128 bytes for SHA-512. |
| 321 | /// Panics if the supplied digest buffer is too short. | 356 | /// Panics if the supplied digest buffer is too short. |
| 322 | pub fn finish_blocking(&mut self, mut ctx: Context, digest: &mut [u8]) -> usize { | 357 | pub fn finish_blocking<'c>(&mut self, mut ctx: Context<'c>, digest: &mut [u8]) -> usize { |
| 323 | // Restore the peripheral state. | 358 | // Restore the peripheral state. |
| 324 | self.load_context(&ctx); | 359 | self.load_context(&ctx); |
| 325 | 360 | ||
| @@ -333,6 +368,13 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 333 | // Block waiting for digest. | 368 | // Block waiting for digest. |
| 334 | while !T::regs().sr().read().dcis() {} | 369 | while !T::regs().sr().read().dcis() {} |
| 335 | 370 | ||
| 371 | // Load the HMAC key if provided. | ||
| 372 | if let Some(key) = ctx.key { | ||
| 373 | self.accumulate_blocking(key); | ||
| 374 | T::regs().str().write(|w| w.set_dcal(true)); | ||
| 375 | while !T::regs().sr().read().dcis() {} | ||
| 376 | } | ||
| 377 | |||
| 336 | // Return the digest. | 378 | // Return the digest. |
| 337 | let digest_words = match ctx.algo { | 379 | let digest_words = match ctx.algo { |
| 338 | Algorithm::SHA1 => 5, | 380 | Algorithm::SHA1 => 5, |
| @@ -370,7 +412,7 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 370 | /// The largest returned digest size is 128 bytes for SHA-512. | 412 | /// The largest returned digest size is 128 bytes for SHA-512. |
| 371 | /// Panics if the supplied digest buffer is too short. | 413 | /// Panics if the supplied digest buffer is too short. |
| 372 | #[cfg(hash_v2)] | 414 | #[cfg(hash_v2)] |
| 373 | pub async fn finish(&mut self, mut ctx: Context, digest: &mut [u8]) -> usize | 415 | pub async fn finish<'c>(&mut self, mut ctx: Context<'c>, digest: &mut [u8]) -> usize |
| 374 | where | 416 | where |
| 375 | D: crate::hash::Dma<T>, | 417 | D: crate::hash::Dma<T>, |
| 376 | { | 418 | { |
| @@ -384,6 +426,11 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 384 | self.accumulate(&ctx.buffer[0..ctx.buflen]).await; | 426 | self.accumulate(&ctx.buffer[0..ctx.buflen]).await; |
| 385 | ctx.buflen = 0; | 427 | ctx.buflen = 0; |
| 386 | 428 | ||
| 429 | // Load the HMAC key if provided. | ||
| 430 | if let Some(key) = ctx.key { | ||
| 431 | self.accumulate(key).await; | ||
| 432 | } | ||
| 433 | |||
| 387 | // Wait for completion. | 434 | // Wait for completion. |
| 388 | poll_fn(|cx| { | 435 | poll_fn(|cx| { |
| 389 | // Check if already done. | 436 | // Check if already done. |
| @@ -484,7 +531,7 @@ impl<'d, T: Instance, D> Hash<'d, T, D> { | |||
| 484 | } | 531 | } |
| 485 | 532 | ||
| 486 | /// Save the peripheral state to a context. | 533 | /// Save the peripheral state to a context. |
| 487 | fn store_context(&mut self, ctx: &mut Context) { | 534 | fn store_context<'c>(&mut self, ctx: &mut Context<'c>) { |
| 488 | // Block waiting for data in ready. | 535 | // Block waiting for data in ready. |
| 489 | while !T::regs().sr().read().dinis() {} | 536 | while !T::regs().sr().read().dinis() {} |
| 490 | 537 | ||
diff --git a/examples/stm32f7/src/bin/hash.rs b/examples/stm32f7/src/bin/hash.rs index 96e50f84b..cbb880353 100644 --- a/examples/stm32f7/src/bin/hash.rs +++ b/examples/stm32f7/src/bin/hash.rs | |||
| @@ -26,7 +26,7 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 26 | let hw_start_time = Instant::now(); | 26 | let hw_start_time = Instant::now(); |
| 27 | 27 | ||
| 28 | // Compute a digest in hardware. | 28 | // Compute a digest in hardware. |
| 29 | let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8); | 29 | let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8, None); |
| 30 | hw_hasher.update(&mut context, test_1).await; | 30 | hw_hasher.update(&mut context, test_1).await; |
| 31 | hw_hasher.update(&mut context, test_2).await; | 31 | hw_hasher.update(&mut context, test_2).await; |
| 32 | let mut hw_digest: [u8; 32] = [0; 32]; | 32 | let mut hw_digest: [u8; 32] = [0; 32]; |
