From fc543bc5ef84f4bf9371dede09a146a86fdfe2c4 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Mon, 14 Nov 2022 10:40:48 +0000 Subject: [PATCH] blake2: implement KeyInit::new in terms of KeyInit::new_from_slice (#435) --- blake2/CHANGELOG.md | 6 ++++++ blake2/src/macros.rs | 13 +++---------- blake2/tests/mac.rs | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/blake2/CHANGELOG.md b/blake2/CHANGELOG.md index fab24393..398ba62e 100644 --- a/blake2/CHANGELOG.md +++ b/blake2/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## UNREALEASED +### Changed +- Implement `KeyInit::new` in terms of `KeyInit::new_from_slice` ([#435]) + +[#435]: https://github.com/RustCrypto/hashes/pull/435 + ## 0.10.5 (2022-11-11) ### Fixed - Implementation of the `KeyInit::new` method for MAC types ([#432]) diff --git a/blake2/src/macros.rs b/blake2/src/macros.rs index b8e8a6f0..e185207c 100644 --- a/blake2/src/macros.rs +++ b/blake2/src/macros.rs @@ -320,19 +320,12 @@ macro_rules! blake2_mac_impl { OutSize: ArrayLength + IsLessOrEqual<$max_size>, LeEq: NonZero, { + #[inline] fn new(key: &Key) -> Self { - let kl = key.len(); - let mut padded_key = Block::<$hash>::default(); - padded_key[..kl].copy_from_slice(key); - Self { - core: <$hash>::new_with_params(&[], &[], key.len(), OutSize::USIZE), - buffer: LazyBuffer::new(&padded_key), - #[cfg(feature = "reset")] - key_block: key.clone(), - _out: PhantomData, - } + Self::new_from_slice(key).expect("Key has correct length") } + #[inline] fn new_from_slice(key: &[u8]) -> Result { let kl = key.len(); if kl > ::KeySize::USIZE { diff --git a/blake2/tests/mac.rs b/blake2/tests/mac.rs index ec35a4cc..97fbc9d7 100644 --- a/blake2/tests/mac.rs +++ b/blake2/tests/mac.rs @@ -5,3 +5,25 @@ use digest::new_resettable_mac_test as new_test; new_test!(blake2b_mac, "blake2b/mac", blake2::Blake2bMac512); new_test!(blake2s_mac, "blake2s/mac", blake2::Blake2sMac256); + +#[test] +fn blake2b_new_test() { + use blake2::digest::{generic_array::GenericArray, KeyInit, Mac}; + + fn run(key: &[u8]) { + const DATA: &[u8] = &[42; 300]; + let res1 = ::new(GenericArray::from_slice(key)) + .chain_update(DATA) + .finalize() + .into_bytes(); + let res2 = ::new_from_slice(&key) + .unwrap() + .chain_update(DATA) + .finalize() + .into_bytes(); + assert_eq!(res1, res2); + } + + run::(&[0x42; 32]); + run::(&[0x42; 64]); +}