Skip to content

Commit 510b388

Browse files
Add Customized trait for blake2 (#664)
Allow to use Blake2 with a customized string: `Blake2b128::new_customized(persona)` as described in the paper and as for cSHAKE. It needs a new trait `VarOutputCustomized` implemented by: RustCrypto/traits#1787
1 parent e6bc891 commit 510b388

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ members = [
2727

2828
[profile.dev]
2929
opt-level = 2
30+
31+
[patch.crates-io]
32+
# https://github.com/RustCrypto/traits/pull/1787
33+
digest = { git = "https://github.com/RustCrypto/traits" }

blake2/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use digest::{self, Digest};
1717

1818
use core::{fmt, marker::PhantomData, ops::Div};
1919
use digest::{
20-
FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update,
20+
FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update, VarOutputCustomized,
2121
array::{Array, ArraySize},
2222
block_buffer::{Lazy, LazyBuffer},
2323
consts::{U4, U16, U32, U64, U128},

blake2/src/macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ macro_rules! blake2_impl {
256256
}
257257
}
258258
}
259+
260+
impl VarOutputCustomized for $name {
261+
#[inline]
262+
fn new_customized(customization: &[u8], output_size: usize) -> Self {
263+
Self::new_with_params(&[], customization, 0, output_size)
264+
}
265+
}
266+
259267
#[cfg(feature = "zeroize")]
260268
impl ZeroizeOnDrop for $name {}
261269
};

blake2/tests/persona.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use blake2::{Blake2bMac512, Blake2sMac256, digest::FixedOutput};
1+
use blake2::digest::CustomizedInit;
2+
use blake2::{Blake2b128, Blake2bMac512, Blake2sMac256, digest::FixedOutput};
3+
use digest::Update;
24
use hex_literal::hex;
35

46
#[test]
@@ -20,6 +22,17 @@ fn blake2s_persona() {
2022

2123
#[test]
2224
fn blake2b_persona() {
25+
let persona = b"personal";
26+
let mut ctx = Blake2b128::new_customized(persona);
27+
ctx.update(b"hello");
28+
assert_eq!(
29+
ctx.finalize_fixed(),
30+
hex!("5a5eb0aecc053af1ce6de25354c1c761"),
31+
);
32+
}
33+
34+
#[test]
35+
fn blake2b_mac_persona() {
2336
let key = hex!(
2437
"000102030405060708090a0b0c0d0e0f"
2538
"101112131415161718191a1b1c1d1e1f"

0 commit comments

Comments
 (0)