From c3c9dbd62335512909a91536b0a79058fa84188e Mon Sep 17 00:00:00 2001 From: lamafab <42901763+lamafab@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:01:28 +0000 Subject: [PATCH 1/4] add section on MAC in README --- blake2/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/blake2/README.md b/blake2/README.md index 91a92212e..ca2f70950 100644 --- a/blake2/README.md +++ b/blake2/README.md @@ -73,6 +73,24 @@ let res = hasher.finalize(); assert_eq!(res, hex!("2cc55c84e416924e6400")); ``` +### Message Authentication Code (MAC) + +BLAKE2 can be used as a MAC with variable output size set at run time: + +```rust +use blake2::Blake2bMac; +use blake2::digest::{Update, FixedOutput, consts::U16}; +use hex_literal::hex; + +let mut hasher: Blake2bMac = + // Skip optional salt and optional persona. + Blake2bMac::new_with_salt_and_personal(b"my_key", &[], &[]).unwrap(); +hasher.update(b"my_input"); +let res = hasher.finalize_fixed(); + +assert_eq!(res.as_ref(), hex!("3c3869ce1c58d0569827a731d8eab099")); +``` + ## Minimum Supported Rust Version Rust **1.71** or higher. From 4146f4f6b2448482849d058a77237e3602dc32da Mon Sep 17 00:00:00 2001 From: lamafab <42901763+lamafab@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:28:33 +0000 Subject: [PATCH 2/4] expand blake2_mac_impl! macro with new_with_key convenience method, add simple test --- blake2/README.md | 4 +--- blake2/src/macros.rs | 10 +++++++++- blake2/tests/mac.rs | 11 +++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/blake2/README.md b/blake2/README.md index ca2f70950..5232c94d9 100644 --- a/blake2/README.md +++ b/blake2/README.md @@ -82,9 +82,7 @@ use blake2::Blake2bMac; use blake2::digest::{Update, FixedOutput, consts::U16}; use hex_literal::hex; -let mut hasher: Blake2bMac = - // Skip optional salt and optional persona. - Blake2bMac::new_with_salt_and_personal(b"my_key", &[], &[]).unwrap(); +let mut hasher: Blake2bMac = Blake2bMac::new_with_key(b"my_key").unwrap(); hasher.update(b"my_input"); let res = hasher.finalize_fixed(); diff --git a/blake2/src/macros.rs b/blake2/src/macros.rs index b17adb987..d13dc015e 100644 --- a/blake2/src/macros.rs +++ b/blake2/src/macros.rs @@ -284,7 +284,7 @@ macro_rules! blake2_mac_impl { OutSize: ArraySize + IsLessOrEqual<$max_size>, LeEq: NonZero, { - /// Create new instance using provided key, salt, and persona. + /// Create new instance using the provided key, salt, and persona. /// /// # Errors /// @@ -318,6 +318,14 @@ macro_rules! blake2_mac_impl { _out: PhantomData, }) } + /// Creates a new instance using the provided key, skipping the salt + /// and personal. This method is equivalent to calling + /// [`new_with_salt_and_personal`](Self::new_with_salt_and_personal) + /// with empty slices for the salt and personal. + #[inline] + pub fn new_with_key(key: &[u8]) -> Result { + Self::new_with_salt_and_personal(key, &[], &[]) + } } impl KeySizeUser for $name diff --git a/blake2/tests/mac.rs b/blake2/tests/mac.rs index 7a166a047..4a83ae700 100644 --- a/blake2/tests/mac.rs +++ b/blake2/tests/mac.rs @@ -33,3 +33,14 @@ fn mac_refuses_empty_keys() { assert!(blake2::Blake2bMac512::new_with_salt_and_personal(&[], b"salt", b"persona").is_err()); assert!(blake2::Blake2sMac256::new_with_salt_and_personal(&[], b"salt", b"persona").is_err()); } + +#[test] +fn blake2b_with_key_equivalence() { + use blake2::digest::FixedOutput; + + let key = b"my_key"; + // Those two calls are equivalent. + let ctx1 = blake2::Blake2bMac512::new_with_salt_and_personal(key, &[], &[]).unwrap(); + let ctx2 = blake2::Blake2bMac512::new_with_key(key).unwrap(); + assert_eq!(ctx1.finalize_fixed(), ctx2.finalize_fixed(),); +} From 8b055ae528c48faac7c3e0bf601bfedd7b86cf9e Mon Sep 17 00:00:00 2001 From: lamafab <42901763+lamafab@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:30:39 +0000 Subject: [PATCH 3/4] typo --- blake2/src/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blake2/src/macros.rs b/blake2/src/macros.rs index d13dc015e..9b1bf61e6 100644 --- a/blake2/src/macros.rs +++ b/blake2/src/macros.rs @@ -319,9 +319,9 @@ macro_rules! blake2_mac_impl { }) } /// Creates a new instance using the provided key, skipping the salt - /// and personal. This method is equivalent to calling + /// and persona. This method is equivalent to calling /// [`new_with_salt_and_personal`](Self::new_with_salt_and_personal) - /// with empty slices for the salt and personal. + /// with empty slices for the salt and persona. #[inline] pub fn new_with_key(key: &[u8]) -> Result { Self::new_with_salt_and_personal(key, &[], &[]) From bfb39d0e498bc93a6973dc31b5be26582d849828 Mon Sep 17 00:00:00 2001 From: lamafab <42901763+lamafab@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:48:30 +0000 Subject: [PATCH 4/4] set at compile time, not run time --- blake2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blake2/README.md b/blake2/README.md index 5232c94d9..29c294bfb 100644 --- a/blake2/README.md +++ b/blake2/README.md @@ -75,7 +75,7 @@ assert_eq!(res, hex!("2cc55c84e416924e6400")); ### Message Authentication Code (MAC) -BLAKE2 can be used as a MAC with variable output size set at run time: +BLAKE2 can be used as a MAC with variable output size set at compile time: ```rust use blake2::Blake2bMac;