Skip to content

Commit b13d772

Browse files
authored
blake2: fix KeySize (#349)
1 parent 4e5ca4c commit b13d772

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blake2/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ 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+
## 0.10.2 (2022-01-09)
9+
## Fixed
10+
- Rare compilation error by adding `'static` bound on `OutSize`. ([#347])
11+
- Values of `KeySize` associated type. ([#349])
12+
13+
[#347]: https://github.com/RustCrypto/hashes/pull/347
14+
[#349]: https://github.com/RustCrypto/hashes/pull/349
15+
816
## 0.10.1 (2022-01-05)
917
## Fixed
1018
- Compilation error with enabled `reset` feature. ([#342])

blake2/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
2-
name = "blake2" # Also update html_root_url in lib.rs when bumping this
3-
version = "0.10.1" # Also update html_root_url in lib.rs when bumping this
2+
name = "blake2"
3+
version = "0.10.2" # Also update html_root_url in lib.rs when bumping this
44
description = "BLAKE2 hash functions"
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"

blake2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#![doc(
7373
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
7474
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
75-
html_root_url = "https://docs.rs/blake2/0.10.1"
75+
html_root_url = "https://docs.rs/blake2/0.10.2"
7676
)]
7777
#![warn(missing_docs, rust_2018_idioms)]
7878
#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]

blake2/src/macros.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ macro_rules! blake2_mac_impl {
265265
core: $hash,
266266
buffer: LazyBuffer<<$hash as BlockSizeUser>::BlockSize>,
267267
#[cfg(feature = "reset")]
268-
key_block: Block<$hash>,
268+
key_block: Key<Self>,
269269
_out: PhantomData<OutSize>,
270270
}
271271

@@ -291,14 +291,17 @@ macro_rules! blake2_mac_impl {
291291
if kl > bs || salt.len() > qbs || persona.len() > qbs {
292292
return Err(InvalidLength);
293293
}
294-
let mut key_block = Block::<$hash>::default();
295-
key_block[..kl].copy_from_slice(key);
296-
let buffer = LazyBuffer::new(&key_block);
294+
let mut padded_key = Block::<$hash>::default();
295+
padded_key[..kl].copy_from_slice(key);
297296
Ok(Self {
298297
core: <$hash>::new_with_params(salt, persona, key.len(), OutSize::USIZE),
299-
buffer,
298+
buffer: LazyBuffer::new(&padded_key),
300299
#[cfg(feature = "reset")]
301-
key_block,
300+
key_block: {
301+
let mut t = Key::<Self>::default();
302+
t[..kl].copy_from_slice(key);
303+
t
304+
},
302305
_out: PhantomData,
303306
})
304307
}
@@ -309,7 +312,7 @@ macro_rules! blake2_mac_impl {
309312
OutSize: ArrayLength<u8> + IsLessOrEqual<$max_size>,
310313
LeEq<OutSize, $max_size>: NonZero,
311314
{
312-
type KeySize = <$hash as BlockSizeUser>::BlockSize;
315+
type KeySize = $max_size;
313316
}
314317

315318
impl<OutSize> KeyInit for $name<OutSize>
@@ -318,9 +321,12 @@ macro_rules! blake2_mac_impl {
318321
LeEq<OutSize, $max_size>: NonZero,
319322
{
320323
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);
321327
Self {
322328
core: <$hash>::new_with_params(key, &[], key.len(), OutSize::USIZE),
323-
buffer: LazyBuffer::new(key),
329+
buffer: LazyBuffer::new(&padded_key),
324330
#[cfg(feature = "reset")]
325331
key_block: key.clone(),
326332
_out: PhantomData,
@@ -329,16 +335,20 @@ macro_rules! blake2_mac_impl {
329335

330336
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
331337
let kl = key.len();
332-
if kl > <$hash as BlockSizeUser>::BlockSize::USIZE {
338+
if kl > <Self as KeySizeUser>::KeySize::USIZE {
333339
return Err(InvalidLength);
334340
}
335-
let mut key_block = Block::<$hash>::default();
336-
key_block[..kl].copy_from_slice(key);
341+
let mut padded_key = Block::<$hash>::default();
342+
padded_key[..kl].copy_from_slice(key);
337343
Ok(Self {
338344
core: <$hash>::new_with_params(&[], &[], key.len(), OutSize::USIZE),
339-
buffer: LazyBuffer::new(&key_block),
345+
buffer: LazyBuffer::new(&padded_key),
340346
#[cfg(feature = "reset")]
341-
key_block,
347+
key_block: {
348+
let mut t = Key::<Self>::default();
349+
t[..kl].copy_from_slice(key);
350+
t
351+
},
342352
_out: PhantomData,
343353
})
344354
}
@@ -386,7 +396,10 @@ macro_rules! blake2_mac_impl {
386396
{
387397
fn reset(&mut self) {
388398
self.core.reset();
389-
self.buffer = LazyBuffer::new(&self.key_block);
399+
let kl = self.key_block.len();
400+
let mut padded_key = Block::<$hash>::default();
401+
padded_key[..kl].copy_from_slice(&self.key_block);
402+
self.buffer = LazyBuffer::new(&padded_key);
390403
}
391404
}
392405

@@ -398,17 +411,11 @@ macro_rules! blake2_mac_impl {
398411
{
399412
#[inline]
400413
fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
401-
let Self {
402-
core,
403-
buffer,
404-
key_block,
405-
..
406-
} = self;
414+
let Self { core, buffer, .. } = self;
407415
let mut full_res = Default::default();
408416
core.finalize_variable_core(buffer, &mut full_res);
409417
out.copy_from_slice(&full_res[..OutSize::USIZE]);
410-
core.reset();
411-
*buffer = LazyBuffer::new(key_block);
418+
self.reset();
412419
}
413420
}
414421

0 commit comments

Comments
 (0)