Skip to content

Commit

Permalink
Fix(SHA): Use correct length padding for SHA384 and SHA512.
Browse files Browse the repository at this point in the history
- This fixes a long running issue with SHA384 and SHA512, where some digest of specific sizes wouldn't compute correctly, by changing the padding length of the size field.
  • Loading branch information
AnthonyGrondin committed Aug 10, 2024
1 parent 941b86c commit 500b73f
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 359 deletions.
7 changes: 4 additions & 3 deletions esp-hal/src/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ pub trait Sha<'d, DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
}
self.process_buffer();

// Wait until buffer has completely processed
while self.is_busy() {}
// Save the content of the current hash for interleaving operation.
#[cfg(not(esp32))]
{
// Wait until buffer has completely processed
while self.is_busy() {}

let mut saved_digest = [0u8; 64];
self.alignment_helper.volatile_read_regset(
sha.h_mem(0).as_ptr(),
Expand Down Expand Up @@ -303,7 +304,7 @@ pub trait Sha<'d, DM: crate::Mode>: core::ops::DerefMut<Target = Context<DM>> {
debug_assert!(self.cursor % 4 == 0);

let mod_cursor = self.cursor % chunk_len;
if (chunk_len - mod_cursor) < core::mem::size_of::<u64>() {
if (chunk_len - mod_cursor) < chunk_len / 8 {
// Zero out remaining data if buffer is almost full (>=448/896), and process
// buffer
let pad_len = chunk_len - mod_cursor;
Expand Down
3 changes: 1 addition & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ embedded-io-async = "0.6.1"
embedded-storage = "0.3.1"
esp-alloc = { path = "../esp-alloc" }
esp-backtrace = { path = "../esp-backtrace", features = ["exception-handler", "panic-handler", "println"] }
esp-hal = { path = "../esp-hal", features = ["log", "digest"] }
esp-hal = { path = "../esp-hal", features = ["log"] }
esp-hal-embassy = { path = "../esp-hal-embassy", optional = true }
esp-hal-smartled = { path = "../esp-hal-smartled", optional = true }
esp-ieee802154 = { path = "../esp-ieee802154", optional = true }
Expand All @@ -49,7 +49,6 @@ nb = "1.1.0"
p192 = { version = "0.13.0", default-features = false, features = ["arithmetic"] }
p256 = { version = "0.13.2", default-features = false, features = ["arithmetic"] }
portable-atomic = { version = "1.6.0", default-features = false }
sha1 = { version = "0.10.6", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
smart-leds = "0.4.0"
smoltcp = { version = "0.11.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] }
Expand Down
347 changes: 0 additions & 347 deletions examples/src/bin/sha_fuzz.rs

This file was deleted.

2 changes: 2 additions & 0 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ hex-literal = "0.4.1"
nb = "1.1.0"
p192 = { version = "0.13.0", default-features = false, features = ["arithmetic"] }
p256 = { version = "0.13.2", default-features = false, features = ["arithmetic"] }
sha1 = { version = "0.10.6", default-features = false }
sha2 = { version = "0.10.8", default-features = false }

[features]
default = ["async", "embassy"]
Expand Down
Loading

0 comments on commit 500b73f

Please sign in to comment.