Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section on Blake2 MAC with variable output + convenience method #602

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions blake2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ 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 compile time:

```rust
use blake2::Blake2bMac;
use blake2::digest::{Update, FixedOutput, consts::U16};
use hex_literal::hex;

let mut hasher: Blake2bMac<U16> = Blake2bMac::new_with_key(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.
Expand Down
10 changes: 9 additions & 1 deletion blake2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ macro_rules! blake2_mac_impl {
OutSize: ArraySize + IsLessOrEqual<$max_size>,
LeEq<OutSize, $max_size>: NonZero,
{
/// Create new instance using provided key, salt, and persona.
/// Create new instance using the provided key, salt, and persona.
///
/// # Errors
///
Expand Down Expand Up @@ -318,6 +318,14 @@ macro_rules! blake2_mac_impl {
_out: PhantomData,
})
}
/// Creates a new instance using the provided key, skipping the salt
/// 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 persona.
#[inline]
pub fn new_with_key(key: &[u8]) -> Result<Self, InvalidLength> {
Self::new_with_salt_and_personal(key, &[], &[])
}
}

impl<OutSize> KeySizeUser for $name<OutSize>
Expand Down
11 changes: 11 additions & 0 deletions blake2/tests/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),);
}
Loading