From 9362815b9360454c1d4efb4a338ff2d256b5ff6b Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Sat, 5 Sep 2026 12:13:01 +0300 Subject: [PATCH 1/2] test(blake2): reject empty MAC keys Cover the generic KeyInit constructor alongside the parameterized constructor so both BLAKE2 variants enforce the same key invariant. Signed-off-by: Ville Vesilehto --- blake2/tests/mac.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/blake2/tests/mac.rs b/blake2/tests/mac.rs index 2deca402f..69e47f0ec 100644 --- a/blake2/tests/mac.rs +++ b/blake2/tests/mac.rs @@ -32,6 +32,10 @@ fn blake2b_new_test() { #[test] fn mac_refuses_empty_keys() { + use blake2::digest::KeyInit; + + assert!(blake2::Blake2bMac512::new_from_slice(&[]).is_err()); + assert!(blake2::Blake2sMac256::new_from_slice(&[]).is_err()); assert!( blake2::Blake2bMac512::new_with_salt_and_personal(Some(&[]), b"salt", b"persona").is_err() ); From f878965f124032f9cb4c5baff7cd163c39e541bc Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Sat, 5 Sep 2026 12:13:36 +0300 Subject: [PATCH 2/2] fix(blake2): reject empty MAC keys Treat zero-length keys as invalid through the generic KeyInit API. Match the parameterized constructor and keep unkeyed hashing explicit. Signed-off-by: Ville Vesilehto --- blake2/src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blake2/src/macros.rs b/blake2/src/macros.rs index ea0f26532..e0277c4d8 100644 --- a/blake2/src/macros.rs +++ b/blake2/src/macros.rs @@ -353,7 +353,7 @@ macro_rules! blake2_mac_impl { #[inline] fn new_from_slice(key: &[u8]) -> Result { let kl = key.len(); - if kl > ::KeySize::USIZE { + if kl == 0 || kl > ::KeySize::USIZE { return Err(InvalidLength); } let mut padded_key = Block::<$hash>::default();