Skip to content

Commit

Permalink
sw-emulator: Limit HMAC KV access to 48 bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Heymans <arthur.heymans@9elements.com>
  • Loading branch information
ArthurHeymans committed Nov 19, 2024
1 parent 08a1740 commit 78571d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion drivers/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Hmac {

// Configure the hardware so that the output tag is stored at a location specified by the
// caller.
if matches!(&mut tag, HmacTag::Array4x12(_)) {
if matches!(&mut tag, HmacTag::Array4x12(_) | HmacTag::Array4x16(_)) {
KvAccess::begin_copy_to_arr(hmac.hmac512_kv_wr_status(), hmac.hmac512_kv_wr_ctrl())?;
}

Expand Down
9 changes: 6 additions & 3 deletions sw-emulator/lib/periph/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ register_bitfields! [
/// HMAC384 Key Size.
const HMAC_KEY_SIZE_384: usize = 48;

/// TODO MAX KV KEY SIZE (needs KV supporting 64 bytes?)
const MAX_KV_KEY_SIZE: usize = 48;

/// HMAC512 Key Size.
const HMAC_KEY_SIZE_512: usize = 64;

Expand Down Expand Up @@ -562,10 +565,10 @@ impl HmacSha {

if let Some(key) = &key {
self.key_from_kv = true;
let key_len = self.key_len();
let key_len = self.key_len().min(MAX_KV_KEY_SIZE);
self.key[..key_len]
.as_bytes_mut()
.copy_from_slice(&key[..key_len * 4]);
.copy_from_slice(&key[..key_len]);
}

self.key_read_status.reg.modify(
Expand Down Expand Up @@ -641,7 +644,7 @@ impl HmacSha {
.key_vault
.write_key(
key_id,
&self.tag.as_bytes()[..self.key_len() * 4],
&self.tag.as_bytes()[..MAX_KV_KEY_SIZE],
self.tag_write_ctrl.reg.read(TagWriteControl::USAGE),
)
.err()
Expand Down

0 comments on commit 78571d9

Please sign in to comment.