Skip to content

Commit fc543bc

Browse files
authored
blake2: implement KeyInit::new in terms of KeyInit::new_from_slice (#435)
1 parent 712f3df commit fc543bc

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

blake2/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## UNREALEASED
9+
### Changed
10+
- Implement `KeyInit::new` in terms of `KeyInit::new_from_slice` ([#435])
11+
12+
[#435]: https://github.com/RustCrypto/hashes/pull/435
13+
814
## 0.10.5 (2022-11-11)
915
### Fixed
1016
- Implementation of the `KeyInit::new` method for MAC types ([#432])

blake2/src/macros.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,12 @@ macro_rules! blake2_mac_impl {
320320
OutSize: ArrayLength<u8> + IsLessOrEqual<$max_size>,
321321
LeEq<OutSize, $max_size>: NonZero,
322322
{
323+
#[inline]
323324
fn new(key: &Key<Self>) -> Self {
324-
let kl = key.len();
325-
let mut padded_key = Block::<$hash>::default();
326-
padded_key[..kl].copy_from_slice(key);
327-
Self {
328-
core: <$hash>::new_with_params(&[], &[], key.len(), OutSize::USIZE),
329-
buffer: LazyBuffer::new(&padded_key),
330-
#[cfg(feature = "reset")]
331-
key_block: key.clone(),
332-
_out: PhantomData,
333-
}
325+
Self::new_from_slice(key).expect("Key has correct length")
334326
}
335327

328+
#[inline]
336329
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
337330
let kl = key.len();
338331
if kl > <Self as KeySizeUser>::KeySize::USIZE {

blake2/tests/mac.rs

+22
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,25 @@ use digest::new_resettable_mac_test as new_test;
55

66
new_test!(blake2b_mac, "blake2b/mac", blake2::Blake2bMac512);
77
new_test!(blake2s_mac, "blake2s/mac", blake2::Blake2sMac256);
8+
9+
#[test]
10+
fn blake2b_new_test() {
11+
use blake2::digest::{generic_array::GenericArray, KeyInit, Mac};
12+
13+
fn run<T: Mac + KeyInit>(key: &[u8]) {
14+
const DATA: &[u8] = &[42; 300];
15+
let res1 = <T as Mac>::new(GenericArray::from_slice(key))
16+
.chain_update(DATA)
17+
.finalize()
18+
.into_bytes();
19+
let res2 = <T as Mac>::new_from_slice(&key)
20+
.unwrap()
21+
.chain_update(DATA)
22+
.finalize()
23+
.into_bytes();
24+
assert_eq!(res1, res2);
25+
}
26+
27+
run::<blake2::Blake2sMac256>(&[0x42; 32]);
28+
run::<blake2::Blake2bMac512>(&[0x42; 64]);
29+
}

0 commit comments

Comments
 (0)