Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
playfulFence committed Apr 16, 2024
2 parents 5867440 + a143373 commit 6358c81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed writes to SPI not flushing before attempting to write, causing corrupted writes (#1381)
- fix AdcConfig::adc_calibrate for xtensa targets (#1379)
- Fixed a divide by zero panic when setting the LEDC duty cycle to 0 with `SetDutyCycle::set_duty_cycle` (#1403)
- Support 192 and 256-bit keys for AES (#1316)

### Changed

Expand Down
14 changes: 10 additions & 4 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ impl<'d> Aes<'d> {
}

/// Encrypts/Decrypts the given buffer based on `mode` parameter
pub fn process(&mut self, block: &mut [u8; 16], mode: Mode, key: &[u8; 16]) {
pub fn process(&mut self, block: &mut [u8; 16], mode: Mode, key: &[u8]) {
assert!(
key.len() == 16
|| (cfg!(any(feature = "esp32", feature = "esp32s2")) && key.len() == 24)
|| key.len() == 32,
"Invalid key size"
);
self.write_key(key);
self.set_mode(mode as u8);
self.set_block(block);
Expand Down Expand Up @@ -396,7 +402,7 @@ pub mod dma {
read_buffer: &'t mut RXBUF,
mode: Mode,
cipher_mode: CipherMode,
key: [u8; 16],
key: &[u8],
) -> Result<AesDmaTransferRxTx<'t, 'd, C>, crate::dma::DmaError>
where
TXBUF: ReadBuffer<Word = u8>,
Expand Down Expand Up @@ -427,7 +433,7 @@ pub mod dma {
read_buffer_len: usize,
mode: Mode,
cipher_mode: CipherMode,
key: [u8; 16],
key: &[u8],
) -> Result<(), crate::dma::DmaError> {
// AES has to be restarted after each calculation
self.reset_aes();
Expand Down Expand Up @@ -457,7 +463,7 @@ pub mod dma {
self.enable_interrupt();
self.set_mode(mode);
self.set_cipher_mode(cipher_mode);
self.write_key(&key);
self.write_key(key);

// TODO: verify 16?
self.set_num_block(16);
Expand Down
8 changes: 2 additions & 6 deletions examples/src/bin/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,10 @@ fn main() -> ! {
);
let sw_decrypted = block;

assert!(eq(&sw_encrypted.into(), &hw_encrypted));
assert!(eq(&sw_decrypted.into(), &hw_decrypted));
assert!(&sw_encrypted as &[u8] == &hw_encrypted);
assert!(&sw_decrypted as &[u8] == &hw_decrypted);

println!("done");

loop {}
}

fn eq(slice1: &[u8; 16], slice2: &[u8; 16]) -> bool {
slice1.iter().zip(slice2.iter()).all(|(a, b)| a == b)
}
12 changes: 4 additions & 8 deletions examples/src/bin/aes_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() -> ! {
&mut output,
Mode::Encryption128,
CipherMode::Ecb,
keytext,
&keytext,
)
.unwrap();
transfer.wait().unwrap();
Expand All @@ -72,7 +72,7 @@ fn main() -> ! {
&mut output,
Mode::Decryption128,
CipherMode::Ecb,
keytext,
&keytext,
)
.unwrap();
transfer.wait().unwrap();
Expand Down Expand Up @@ -109,13 +109,9 @@ fn main() -> ! {
);
let sw_decrypted = block.clone();

assert!(eq(&sw_encrypted.into(), &hw_encrypted));
assert!(eq(&sw_decrypted.into(), &hw_decrypted));
assert!(&sw_encrypted as &[u8] == &hw_encrypted);
assert!(&sw_decrypted as &[u8] == &hw_decrypted);

println!("done");
loop {}
}

fn eq(slice1: &[u8; 16], slice2: &[u8; 16]) -> bool {
slice1.iter().zip(slice2.iter()).all(|(a, b)| a == b)
}

0 comments on commit 6358c81

Please sign in to comment.