diff options
| author | Caleb Garrett <[email protected]> | 2024-02-20 15:26:31 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-02-25 20:59:07 -0500 |
| commit | f64a62149e423f6fdb643f7343d971eedc4a3a12 (patch) | |
| tree | 4c7a2b4d86a920316444e7d5ce010c2c43c91b16 | |
| parent | 1e21b758f795b5cc8a2331aacbc2a9a39bb7a7fb (diff) | |
Corrected CCM partial block ops.
| -rw-r--r-- | embassy-stm32/src/cryp/mod.rs | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/embassy-stm32/src/cryp/mod.rs b/embassy-stm32/src/cryp/mod.rs index 81446e39e..634c85883 100644 --- a/embassy-stm32/src/cryp/mod.rs +++ b/embassy-stm32/src/cryp/mod.rs | |||
| @@ -327,14 +327,16 @@ impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesGmac<'c, KEY_SIZE> { | |||
| 327 | dir: Direction, | 327 | dir: Direction, |
| 328 | int_data: &mut [u8; AES_BLOCK_SIZE], | 328 | int_data: &mut [u8; AES_BLOCK_SIZE], |
| 329 | _temp1: [u32; 4], | 329 | _temp1: [u32; 4], |
| 330 | _padding_mask: [u8; 16], | 330 | padding_mask: [u8; AES_BLOCK_SIZE], |
| 331 | ) { | 331 | ) { |
| 332 | if dir == Direction::Encrypt { | 332 | if dir == Direction::Encrypt { |
| 333 | //Handle special GCM partial block process. | 333 | //Handle special GCM partial block process. |
| 334 | p.cr().modify(|w| w.set_crypen(false)); | 334 | p.cr().modify(|w| w.set_crypen(false)); |
| 335 | p.cr().write(|w| w.set_algomode3(true)); | 335 | p.cr().modify(|w| w.set_algomode3(true)); |
| 336 | p.cr().write(|w| w.set_algomode0(0)); | 336 | p.cr().modify(|w| w.set_algomode0(0)); |
| 337 | p.init(1).ivrr().write_value(2); | 337 | for i in 0..AES_BLOCK_SIZE { |
| 338 | int_data[i] = int_data[i] & padding_mask[i]; | ||
| 339 | } | ||
| 338 | p.cr().modify(|w| w.set_crypen(true)); | 340 | p.cr().modify(|w| w.set_crypen(true)); |
| 339 | p.cr().modify(|w| w.set_gcm_ccmph(3)); | 341 | p.cr().modify(|w| w.set_gcm_ccmph(3)); |
| 340 | let mut index = 0; | 342 | let mut index = 0; |
| @@ -479,10 +481,10 @@ impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesCcm<'c, KEY_SIZE> { | |||
| 479 | if dir == Direction::Decrypt { | 481 | if dir == Direction::Decrypt { |
| 480 | p.cr().modify(|w| w.set_crypen(false)); | 482 | p.cr().modify(|w| w.set_crypen(false)); |
| 481 | let iv1temp = p.init(1).ivrr().read(); | 483 | let iv1temp = p.init(1).ivrr().read(); |
| 482 | temp1[0] = p.csgcmccmr(0).read(); | 484 | temp1[0] = p.csgcmccmr(0).read().swap_bytes(); |
| 483 | temp1[1] = p.csgcmccmr(1).read(); | 485 | temp1[1] = p.csgcmccmr(1).read().swap_bytes(); |
| 484 | temp1[2] = p.csgcmccmr(2).read(); | 486 | temp1[2] = p.csgcmccmr(2).read().swap_bytes(); |
| 485 | temp1[3] = p.csgcmccmr(3).read(); | 487 | temp1[3] = p.csgcmccmr(3).read().swap_bytes(); |
| 486 | p.init(1).ivrr().write_value(iv1temp); | 488 | p.init(1).ivrr().write_value(iv1temp); |
| 487 | p.cr().modify(|w| w.set_algomode3(false)); | 489 | p.cr().modify(|w| w.set_algomode3(false)); |
| 488 | p.cr().modify(|w| w.set_algomode0(6)); | 490 | p.cr().modify(|w| w.set_algomode0(6)); |
| @@ -501,27 +503,27 @@ impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesCcm<'c, KEY_SIZE> { | |||
| 501 | ) { | 503 | ) { |
| 502 | if dir == Direction::Decrypt { | 504 | if dir == Direction::Decrypt { |
| 503 | //Handle special CCM partial block process. | 505 | //Handle special CCM partial block process. |
| 504 | let mut intdata_o: [u32; 4] = [0; 4]; | ||
| 505 | for i in 0..intdata_o.len() { | ||
| 506 | intdata_o[i] = p.dout().read(); | ||
| 507 | } | ||
| 508 | let mut temp2 = [0; 4]; | 506 | let mut temp2 = [0; 4]; |
| 509 | temp2[0] = p.csgcmccmr(0).read(); | 507 | temp2[0] = p.csgcmccmr(0).read().swap_bytes(); |
| 510 | temp2[1] = p.csgcmccmr(1).read(); | 508 | temp2[1] = p.csgcmccmr(1).read().swap_bytes(); |
| 511 | temp2[2] = p.csgcmccmr(2).read(); | 509 | temp2[2] = p.csgcmccmr(2).read().swap_bytes(); |
| 512 | temp2[3] = p.csgcmccmr(3).read(); | 510 | temp2[3] = p.csgcmccmr(3).read().swap_bytes(); |
| 513 | p.cr().write(|w| w.set_algomode3(true)); | 511 | p.cr().modify(|w| w.set_algomode3(true)); |
| 514 | p.cr().write(|w| w.set_algomode0(1)); | 512 | p.cr().modify(|w| w.set_algomode0(1)); |
| 515 | p.cr().modify(|w| w.set_gcm_ccmph(3)); | 513 | p.cr().modify(|w| w.set_gcm_ccmph(3)); |
| 516 | // Header phase | 514 | // Header phase |
| 517 | p.cr().modify(|w| w.set_gcm_ccmph(1)); | 515 | p.cr().modify(|w| w.set_gcm_ccmph(1)); |
| 516 | for i in 0..AES_BLOCK_SIZE { | ||
| 517 | int_data[i] = int_data[i] & padding_mask[i]; | ||
| 518 | } | ||
| 518 | let mut in_data: [u32; 4] = [0; 4]; | 519 | let mut in_data: [u32; 4] = [0; 4]; |
| 519 | for i in 0..in_data.len() { | 520 | for i in 0..in_data.len() { |
| 520 | let mut mask_bytes: [u8; 4] = [0; 4]; | 521 | let mut int_bytes: [u8; 4] = [0; 4]; |
| 521 | mask_bytes.copy_from_slice(&padding_mask[(i * 4)..(i * 4) + 4]); | 522 | int_bytes.copy_from_slice(&int_data[(i * 4)..(i * 4) + 4]); |
| 522 | let mask_word = u32::from_le_bytes(mask_bytes); | 523 | let int_word = u32::from_le_bytes(int_bytes); |
| 523 | in_data[i] = intdata_o[i] & mask_word; | 524 | in_data[i] = int_word; |
| 524 | in_data[i] = in_data[i] ^ temp1[i] ^ temp2[i]; | 525 | in_data[i] = in_data[i] ^ temp1[i] ^ temp2[i]; |
| 526 | p.din().write_value(in_data[i]); | ||
| 525 | } | 527 | } |
| 526 | } | 528 | } |
| 527 | } | 529 | } |
