From b139322a86cf8b755b3477f015120f3d3ee10452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 14 Nov 2022 13:18:49 +0300 Subject: [PATCH 1/3] blake2: implement KeyInit::new in terms of KeyInit::new_from_slice --- blake2/CHANGELOG.md | 6 ++++++ blake2/src/macros.rs | 13 +++---------- 2 files changed, 9 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 { From c78366c0f3c431b811fc85f150edc8df0172ea55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 14 Nov 2022 13:32:13 +0300 Subject: [PATCH 2/3] Add KeyInit::new test --- blake2/tests/mac.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/blake2/tests/mac.rs b/blake2/tests/mac.rs index ec35a4cc..031f0e4f 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]) { + let data = [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]); +} From b0a768eb78831f06284983181be040d622639612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 14 Nov 2022 13:35:51 +0300 Subject: [PATCH 3/3] fix test MSRV --- blake2/tests/mac.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blake2/tests/mac.rs b/blake2/tests/mac.rs index 031f0e4f..97fbc9d7 100644 --- a/blake2/tests/mac.rs +++ b/blake2/tests/mac.rs @@ -11,14 +11,14 @@ fn blake2b_new_test() { use blake2::digest::{generic_array::GenericArray, KeyInit, Mac}; fn run(key: &[u8]) { - let data = [42; 300]; + const DATA: &[u8] = &[42; 300]; let res1 = ::new(GenericArray::from_slice(key)) - .chain_update(&data) + .chain_update(DATA) .finalize() .into_bytes(); let res2 = ::new_from_slice(&key) .unwrap() - .chain_update(&data) + .chain_update(DATA) .finalize() .into_bytes(); assert_eq!(res1, res2);