Skip to content

Commit

Permalink
feat: Block on hmac calls, remove Option<> results
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Jan 7, 2025
1 parent 49d1630 commit f9dbfd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
35 changes: 12 additions & 23 deletions esp-hal/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,26 @@ impl<'d> Hmac<'d> {
}

/// Process the msg block after block
///
/// Call this function as many times as necessary (msg.len() > 0)
pub fn update<'a>(&mut self, msg: &'a [u8]) -> Option<&'a [u8]> {
if self.is_busy() {
return None;
}
pub fn update<'a>(&mut self, msg: &'a [u8]) -> &'a [u8] {
while (&mut *self).is_busy() {}

self.next_command();

let remaining = self.write_data(msg).unwrap();
let remaining = self.write_data(msg);

Some(remaining)
remaining
}

/// Finalizes the HMAC computation and retrieves the resulting hash output.
pub fn finalize(&mut self, output: &mut [u8]) -> Option<()> {
if self.is_busy() {
return None;
}
pub fn finalize(&mut self, output: &mut [u8]) {
while (&mut *self).is_busy() {}

self.next_command();

let msg_len = self.byte_written as u64;

self.write_data(&[0x80])?;
self.flush_data()?;
self.write_data(&[0x80]);
self.flush_data();
self.next_command();
debug_assert!(self.byte_written % 4 == 0);

Expand All @@ -202,7 +196,6 @@ impl<'d> Hmac<'d> {
.write(|w| w.set_result_end().set_bit());
self.byte_written = 64;
self.next_command = NextCommand::None;
Some(())
}

fn is_busy(&mut self) -> bool {
Expand All @@ -226,7 +219,7 @@ impl<'d> Hmac<'d> {
self.next_command = NextCommand::None;
}

fn write_data<'a>(&mut self, incoming: &'a [u8]) -> Option<&'a [u8]> {
fn write_data<'a>(&mut self, incoming: &'a [u8]) -> &'a [u8] {
let mod_length = self.byte_written % 64;

let (remaining, bound_reached) = self.alignment_helper.aligned_volatile_copy(
Expand Down Expand Up @@ -255,13 +248,11 @@ impl<'d> Hmac<'d> {
}
}

Some(remaining)
remaining
}

fn flush_data(&mut self) -> Option<()> {
if self.is_busy() {
return None;
}
fn flush_data(&mut self) {
while self.is_busy() {}

let flushed = self.alignment_helper.flush_to(
#[cfg(esp32s2)]
Expand All @@ -279,8 +270,6 @@ impl<'d> Hmac<'d> {
while self.is_busy() {}
self.next_command = NextCommand::MessagePad;
}

Some(())
}

fn padding(&mut self, msg_len: u64) {
Expand Down
6 changes: 3 additions & 3 deletions examples/src/bin/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ fn main() -> ! {
.configure(HmacPurpose::ToUser, KeyId::Key0)
.expect("Key purpose mismatch");
let pre_hw_hmac = esp_hal::time::now();
while let Some(rem) = hw_hmac.update(remaining) {
remaining = rem;
while remaining.len() > 0 {
remaining = hw_hmac.update(remaining);
}
while hw_hmac.finalize(output.as_mut_slice()).is_none() {}
hw_hmac.finalize(output.as_mut_slice());
let post_hw_hmac = esp_hal::time::now();
let hw_time = post_hw_hmac - pre_hw_hmac;
let mut sw_hmac = HmacSha256::new_from_slice(key).expect("HMAC can take key of any size");
Expand Down

0 comments on commit f9dbfd1

Please sign in to comment.