aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/cryp
diff options
context:
space:
mode:
authorCaleb Garrett <[email protected]>2024-03-12 14:52:34 -0400
committerCaleb Garrett <[email protected]>2024-03-12 14:52:34 -0400
commit1ec9fc58f44987c11ac1e093f117679c56dbe2ed (patch)
treeaba18c0ff6f23c65c305c92505b1b2fca14b08f2 /embassy-stm32/src/cryp
parent61050a16d5f02a7db718c6e39c811e6e434b032b (diff)
Add async CRYP to test.
Diffstat (limited to 'embassy-stm32/src/cryp')
-rw-r--r--embassy-stm32/src/cryp/mod.rs52
1 files changed, 22 insertions, 30 deletions
diff --git a/embassy-stm32/src/cryp/mod.rs b/embassy-stm32/src/cryp/mod.rs
index 1a601533d..aa4c2a024 100644
--- a/embassy-stm32/src/cryp/mod.rs
+++ b/embassy-stm32/src/cryp/mod.rs
@@ -98,7 +98,7 @@ pub trait Cipher<'c> {
98 DmaOut: crate::cryp::DmaOut<T>, 98 DmaOut: crate::cryp::DmaOut<T>,
99 {} 99 {}
100 100
101 /// Called prior to processing the first associated data block for cipher-specific operations. 101 /// Returns the AAD header block as required by the cipher.
102 fn get_header_block(&self) -> &[u8] { 102 fn get_header_block(&self) -> &[u8] {
103 return [0; 0].as_slice(); 103 return [0; 0].as_slice();
104 } 104 }
@@ -500,7 +500,7 @@ impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesGcm<'c, KEY_SIZE> {
500 } 500 }
501 501
502 #[cfg(cryp_v3)] 502 #[cfg(cryp_v3)]
503 fn pre_final_block(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] { 503 fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
504 //Handle special GCM partial block process. 504 //Handle special GCM partial block process.
505 p.cr().modify(|w| w.set_npblb(padding_len as u8)); 505 p.cr().modify(|w| w.set_npblb(padding_len as u8));
506 [0; 4] 506 [0; 4]
@@ -643,7 +643,7 @@ impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesGmac<'c, KEY_SIZE> {
643 } 643 }
644 644
645 #[cfg(cryp_v3)] 645 #[cfg(cryp_v3)]
646 fn pre_final_block(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] { 646 fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
647 //Handle special GCM partial block process. 647 //Handle special GCM partial block process.
648 p.cr().modify(|w| w.set_npblb(padding_len as u8)); 648 p.cr().modify(|w| w.set_npblb(padding_len as u8));
649 [0; 4] 649 [0; 4]
@@ -861,7 +861,7 @@ impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize, const IV_SIZE: usize> Cip
861 } 861 }
862 862
863 #[cfg(cryp_v3)] 863 #[cfg(cryp_v3)]
864 fn pre_final_block(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] { 864 fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
865 //Handle special GCM partial block process. 865 //Handle special GCM partial block process.
866 p.cr().modify(|w| w.set_npblb(padding_len as u8)); 866 p.cr().modify(|w| w.set_npblb(padding_len as u8));
867 [0; 4] 867 [0; 4]
@@ -1039,10 +1039,7 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1039 instance 1039 instance
1040 } 1040 }
1041 1041
1042 /// Start a new cipher operation. 1042 /// Start a new encrypt or decrypt operation for the given cipher.
1043 /// Key size must be 128, 192, or 256 bits.
1044 /// Initialization vector must only be supplied if necessary.
1045 /// Panics if there is any mismatch in parameters, such as an incorrect IV length or invalid mode.
1046 pub fn start_blocking<'c, C: Cipher<'c> + CipherSized + IVSized>(&self, cipher: &'c C, dir: Direction) -> Context<'c, C> { 1043 pub fn start_blocking<'c, C: Cipher<'c> + CipherSized + IVSized>(&self, cipher: &'c C, dir: Direction) -> Context<'c, C> {
1047 let mut ctx: Context<'c, C> = Context { 1044 let mut ctx: Context<'c, C> = Context {
1048 dir, 1045 dir,
@@ -1117,10 +1114,7 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1117 ctx 1114 ctx
1118 } 1115 }
1119 1116
1120 /// Start a new cipher operation. 1117 /// Start a new encrypt or decrypt operation for the given cipher.
1121 /// Key size must be 128, 192, or 256 bits.
1122 /// Initialization vector must only be supplied if necessary.
1123 /// Panics if there is any mismatch in parameters, such as an incorrect IV length or invalid mode.
1124 pub async fn start<'c, C: Cipher<'c> + CipherSized + IVSized>(&mut self, cipher: &'c C, dir: Direction) -> Context<'c, C> 1118 pub async fn start<'c, C: Cipher<'c> + CipherSized + IVSized>(&mut self, cipher: &'c C, dir: Direction) -> Context<'c, C>
1125 where 1119 where
1126 DmaIn: crate::cryp::DmaIn<T>, 1120 DmaIn: crate::cryp::DmaIn<T>,
@@ -1201,10 +1195,9 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1201 1195
1202 #[cfg(any(cryp_v2, cryp_v3))] 1196 #[cfg(any(cryp_v2, cryp_v3))]
1203 /// Controls the header phase of cipher processing. 1197 /// Controls the header phase of cipher processing.
1204 /// This function is only valid for GCM, CCM, and GMAC modes. 1198 /// This function is only valid for authenticated ciphers including GCM, CCM, and GMAC.
1205 /// It only needs to be called if using one of these modes and there is associated data. 1199 /// All additional associated data (AAD) must be supplied to this function prior to starting the payload phase with `payload_blocking`.
1206 /// All AAD must be supplied to this function prior to starting the payload phase with `payload_blocking`. 1200 /// The AAD must be supplied in multiples of the block size (128-bits for AES, 64-bits for DES), except when supplying the last block.
1207 /// The AAD must be supplied in multiples of the block size (128 bits), except when supplying the last block.
1208 /// When supplying the last block of AAD, `last_aad_block` must be `true`. 1201 /// When supplying the last block of AAD, `last_aad_block` must be `true`.
1209 pub fn aad_blocking< 1202 pub fn aad_blocking<
1210 'c, 1203 'c,
@@ -1299,10 +1292,9 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1299 1292
1300 #[cfg(any(cryp_v2, cryp_v3))] 1293 #[cfg(any(cryp_v2, cryp_v3))]
1301 /// Controls the header phase of cipher processing. 1294 /// Controls the header phase of cipher processing.
1302 /// This function is only valid for GCM, CCM, and GMAC modes. 1295 /// This function is only valid for authenticated ciphers including GCM, CCM, and GMAC.
1303 /// It only needs to be called if using one of these modes and there is associated data. 1296 /// All additional associated data (AAD) must be supplied to this function prior to starting the payload phase with `payload`.
1304 /// All AAD must be supplied to this function prior to starting the payload phase with `payload_blocking`. 1297 /// The AAD must be supplied in multiples of the block size (128-bits for AES, 64-bits for DES), except when supplying the last block.
1305 /// The AAD must be supplied in multiples of the block size (128 bits), except when supplying the last block.
1306 /// When supplying the last block of AAD, `last_aad_block` must be `true`. 1298 /// When supplying the last block of AAD, `last_aad_block` must be `true`.
1307 pub async fn aad< 1299 pub async fn aad<
1308 'c, 1300 'c,
@@ -1402,7 +1394,7 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1402 /// The context determines algorithm, mode, and state of the crypto accelerator. 1394 /// The context determines algorithm, mode, and state of the crypto accelerator.
1403 /// When the last piece of data is supplied, `last_block` should be `true`. 1395 /// When the last piece of data is supplied, `last_block` should be `true`.
1404 /// This function panics under various mismatches of parameters. 1396 /// This function panics under various mismatches of parameters.
1405 /// Input and output buffer lengths must match. 1397 /// Output buffer must be at least as long as the input buffer.
1406 /// Data must be a multiple of block size (128-bits for AES, 64-bits for DES) for CBC and ECB modes. 1398 /// Data must be a multiple of block size (128-bits for AES, 64-bits for DES) for CBC and ECB modes.
1407 /// Padding or ciphertext stealing must be managed by the application for these modes. 1399 /// Padding or ciphertext stealing must be managed by the application for these modes.
1408 /// Data must also be a multiple of block size unless `last_block` is `true`. 1400 /// Data must also be a multiple of block size unless `last_block` is `true`.
@@ -1455,9 +1447,9 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1455 for block in 0..num_full_blocks { 1447 for block in 0..num_full_blocks {
1456 let index = block * C::BLOCK_SIZE; 1448 let index = block * C::BLOCK_SIZE;
1457 // Write block in 1449 // Write block in
1458 self.write_bytes_blocking(C::BLOCK_SIZE, &input[index..index + 4]); 1450 self.write_bytes_blocking(C::BLOCK_SIZE, &input[index..index + C::BLOCK_SIZE]);
1459 // Read block out 1451 // Read block out
1460 self.read_bytes_blocking(C::BLOCK_SIZE, &mut output[index..index + 4]); 1452 self.read_bytes_blocking(C::BLOCK_SIZE, &mut output[index..index + C::BLOCK_SIZE]);
1461 } 1453 }
1462 1454
1463 // Handle the final block, which is incomplete. 1455 // Handle the final block, which is incomplete.
@@ -1491,7 +1483,7 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1491 /// The context determines algorithm, mode, and state of the crypto accelerator. 1483 /// The context determines algorithm, mode, and state of the crypto accelerator.
1492 /// When the last piece of data is supplied, `last_block` should be `true`. 1484 /// When the last piece of data is supplied, `last_block` should be `true`.
1493 /// This function panics under various mismatches of parameters. 1485 /// This function panics under various mismatches of parameters.
1494 /// Input and output buffer lengths must match. 1486 /// Output buffer must be at least as long as the input buffer.
1495 /// Data must be a multiple of block size (128-bits for AES, 64-bits for DES) for CBC and ECB modes. 1487 /// Data must be a multiple of block size (128-bits for AES, 64-bits for DES) for CBC and ECB modes.
1496 /// Padding or ciphertext stealing must be managed by the application for these modes. 1488 /// Padding or ciphertext stealing must be managed by the application for these modes.
1497 /// Data must also be a multiple of block size unless `last_block` is `true`. 1489 /// Data must also be a multiple of block size unless `last_block` is `true`.
@@ -1548,9 +1540,9 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1548 for block in 0..num_full_blocks { 1540 for block in 0..num_full_blocks {
1549 let index = block * C::BLOCK_SIZE; 1541 let index = block * C::BLOCK_SIZE;
1550 // Read block out 1542 // Read block out
1551 let read = Self::read_bytes(&mut self.outdma, C::BLOCK_SIZE, &mut output[index..index + 4]); 1543 let read = Self::read_bytes(&mut self.outdma, C::BLOCK_SIZE, &mut output[index..index + C::BLOCK_SIZE]);
1552 // Write block in 1544 // Write block in
1553 let write = Self::write_bytes(&mut self.indma, C::BLOCK_SIZE, &input[index..index + 4]); 1545 let write = Self::write_bytes(&mut self.indma, C::BLOCK_SIZE, &input[index..index + C::BLOCK_SIZE]);
1554 embassy_futures::join::join(read, write).await; 1546 embassy_futures::join::join(read, write).await;
1555 } 1547 }
1556 1548
@@ -1583,8 +1575,8 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1583 } 1575 }
1584 1576
1585 #[cfg(any(cryp_v2, cryp_v3))] 1577 #[cfg(any(cryp_v2, cryp_v3))]
1586 /// This function only needs to be called for GCM, CCM, and GMAC modes to 1578 /// Generates an authentication tag for authenticated ciphers including GCM, CCM, and GMAC.
1587 /// generate an authentication tag. 1579 /// Called after the all data has been encrypted/decrypted by `payload`.
1588 pub fn finish_blocking< 1580 pub fn finish_blocking<
1589 'c, 1581 'c,
1590 const TAG_SIZE: usize, 1582 const TAG_SIZE: usize,
@@ -1629,8 +1621,8 @@ impl<'d, T: Instance, DmaIn, DmaOut> Cryp<'d, T, DmaIn, DmaOut> {
1629 } 1621 }
1630 1622
1631 #[cfg(any(cryp_v2, cryp_v3))] 1623 #[cfg(any(cryp_v2, cryp_v3))]
1632 /// This function only needs to be called for GCM, CCM, and GMAC modes to 1624 // Generates an authentication tag for authenticated ciphers including GCM, CCM, and GMAC.
1633 /// generate an authentication tag. 1625 /// Called after the all data has been encrypted/decrypted by `payload`.
1634 pub async fn finish<'c, const TAG_SIZE: usize, C: Cipher<'c> + CipherSized + IVSized + CipherAuthenticated<TAG_SIZE>>(&mut self, mut ctx: Context<'c, C>) -> [u8; TAG_SIZE] 1626 pub async fn finish<'c, const TAG_SIZE: usize, C: Cipher<'c> + CipherSized + IVSized + CipherAuthenticated<TAG_SIZE>>(&mut self, mut ctx: Context<'c, C>) -> [u8; TAG_SIZE]
1635 where 1627 where
1636 DmaIn: crate::cryp::DmaIn<T>, 1628 DmaIn: crate::cryp::DmaIn<T>,