diff --git a/blake2/CHANGELOG.md b/blake2/CHANGELOG.md index 022ba5768..7c71254c2 100644 --- a/blake2/CHANGELOG.md +++ b/blake2/CHANGELOG.md @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 0.11.1 (UNRELEASED) ### Fixed - Zeroization with enabled `reset` crate feature ([#966]) +- Panic in `new_with_salt_and_personal` on too long key, salt or persona ([#969]) [#966]: https://github.com/RustCrypto/hashes/pull/966 +[#969]: https://github.com/RustCrypto/hashes/pull/969 ## 0.11.0 (2026-08-26) ### Added diff --git a/blake2/src/macros.rs b/blake2/src/macros.rs index 641d143f1..6a9d15a83 100644 --- a/blake2/src/macros.rs +++ b/blake2/src/macros.rs @@ -298,8 +298,8 @@ macro_rules! blake2_mac_impl { /// # Errors /// /// If key is `Some`, then its length should not be zero or bigger - /// than the block size. The salt and persona length should not be - /// bigger than quarter of block size. If any of those conditions is + /// than the key size. The salt and persona length should not be + /// bigger than quarter of key size. If any of those conditions is /// false the method will return an error. #[inline] pub fn new_with_salt_and_personal( @@ -308,9 +308,9 @@ macro_rules! blake2_mac_impl { persona: &[u8], ) -> Result { let kl = key.map_or(0, |k| k.len()); - let bs = <$hash as BlockSizeUser>::BlockSize::USIZE; - let qbs = bs / 4; - if key.is_some() && kl == 0 || kl > bs || salt.len() > qbs || persona.len() > qbs { + let ks = ::KeySize::USIZE; + let qks = ks / 4; + if key.is_some() && kl == 0 || kl > ks || salt.len() > qks || persona.len() > qks { return Err(InvalidLength); } let buffer = if let Some(k) = key { diff --git a/blake2/tests/persona.rs b/blake2/tests/persona.rs index dd9766dad..668268e55 100644 --- a/blake2/tests/persona.rs +++ b/blake2/tests/persona.rs @@ -49,3 +49,27 @@ fn blake2b_mac_persona() { ), ); } + +#[test] +fn blake2b_mac_salt_personal_limits() { + let key = [1u8; 128]; + // Maximum key size is 64 bytes, maximum salt and persona size is 16 bytes. + assert!( + Blake2bMac512::new_with_salt_and_personal(Some(&key[..64]), &[0; 16], &[0; 16]).is_ok() + ); + assert!(Blake2bMac512::new_with_salt_and_personal(Some(&key[..65]), &[], &[]).is_err()); + assert!(Blake2bMac512::new_with_salt_and_personal(Some(&key[..128]), &[], &[]).is_err()); + assert!(Blake2bMac512::new_with_salt_and_personal(None, &[0; 17], &[]).is_err()); + assert!(Blake2bMac512::new_with_salt_and_personal(None, &[], &[0; 32]).is_err()); +} + +#[test] +fn blake2s_mac_salt_personal_limits() { + let key = [1u8; 64]; + // Maximum key size is 32 bytes, maximum salt and persona size is 8 bytes. + assert!(Blake2sMac256::new_with_salt_and_personal(Some(&key[..32]), &[0; 8], &[0; 8]).is_ok()); + assert!(Blake2sMac256::new_with_salt_and_personal(Some(&key[..33]), &[], &[]).is_err()); + assert!(Blake2sMac256::new_with_salt_and_personal(Some(&key[..64]), &[], &[]).is_err()); + assert!(Blake2sMac256::new_with_salt_and_personal(None, &[0; 9], &[]).is_err()); + assert!(Blake2sMac256::new_with_salt_and_personal(None, &[], &[0; 16]).is_err()); +}