Skip to content

Commit

Permalink
blake2: fix KeySize (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jan 9, 2022
1 parent 4e5ca4c commit b13d772
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions blake2/CHANGELOG.md
Expand Up @@ -5,6 +5,14 @@ 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).

## 0.10.2 (2022-01-09)
## Fixed
- Rare compilation error by adding `'static` bound on `OutSize`. ([#347])
- Values of `KeySize` associated type. ([#349])

[#347]: https://github.com/RustCrypto/hashes/pull/347
[#349]: https://github.com/RustCrypto/hashes/pull/349

## 0.10.1 (2022-01-05)
## Fixed
- Compilation error with enabled `reset` feature. ([#342])
Expand Down
4 changes: 2 additions & 2 deletions blake2/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "blake2" # Also update html_root_url in lib.rs when bumping this
version = "0.10.1" # Also update html_root_url in lib.rs when bumping this
name = "blake2"
version = "0.10.2" # Also update html_root_url in lib.rs when bumping this
description = "BLAKE2 hash functions"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion blake2/src/lib.rs
Expand Up @@ -72,7 +72,7 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_root_url = "https://docs.rs/blake2/0.10.1"
html_root_url = "https://docs.rs/blake2/0.10.2"
)]
#![warn(missing_docs, rust_2018_idioms)]
#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
Expand Down
51 changes: 29 additions & 22 deletions blake2/src/macros.rs
Expand Up @@ -265,7 +265,7 @@ macro_rules! blake2_mac_impl {
core: $hash,
buffer: LazyBuffer<<$hash as BlockSizeUser>::BlockSize>,
#[cfg(feature = "reset")]
key_block: Block<$hash>,
key_block: Key<Self>,
_out: PhantomData<OutSize>,
}

Expand All @@ -291,14 +291,17 @@ macro_rules! blake2_mac_impl {
if kl > bs || salt.len() > qbs || persona.len() > qbs {
return Err(InvalidLength);
}
let mut key_block = Block::<$hash>::default();
key_block[..kl].copy_from_slice(key);
let buffer = LazyBuffer::new(&key_block);
let mut padded_key = Block::<$hash>::default();
padded_key[..kl].copy_from_slice(key);
Ok(Self {
core: <$hash>::new_with_params(salt, persona, key.len(), OutSize::USIZE),
buffer,
buffer: LazyBuffer::new(&padded_key),
#[cfg(feature = "reset")]
key_block,
key_block: {
let mut t = Key::<Self>::default();
t[..kl].copy_from_slice(key);
t
},
_out: PhantomData,
})
}
Expand All @@ -309,7 +312,7 @@ macro_rules! blake2_mac_impl {
OutSize: ArrayLength<u8> + IsLessOrEqual<$max_size>,
LeEq<OutSize, $max_size>: NonZero,
{
type KeySize = <$hash as BlockSizeUser>::BlockSize;
type KeySize = $max_size;
}

impl<OutSize> KeyInit for $name<OutSize>
Expand All @@ -318,9 +321,12 @@ macro_rules! blake2_mac_impl {
LeEq<OutSize, $max_size>: NonZero,
{
fn new(key: &Key<Self>) -> 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, &[], key.len(), OutSize::USIZE),
buffer: LazyBuffer::new(key),
buffer: LazyBuffer::new(&padded_key),
#[cfg(feature = "reset")]
key_block: key.clone(),
_out: PhantomData,
Expand All @@ -329,16 +335,20 @@ macro_rules! blake2_mac_impl {

fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
let kl = key.len();
if kl > <$hash as BlockSizeUser>::BlockSize::USIZE {
if kl > <Self as KeySizeUser>::KeySize::USIZE {
return Err(InvalidLength);
}
let mut key_block = Block::<$hash>::default();
key_block[..kl].copy_from_slice(key);
let mut padded_key = Block::<$hash>::default();
padded_key[..kl].copy_from_slice(key);
Ok(Self {
core: <$hash>::new_with_params(&[], &[], key.len(), OutSize::USIZE),
buffer: LazyBuffer::new(&key_block),
buffer: LazyBuffer::new(&padded_key),
#[cfg(feature = "reset")]
key_block,
key_block: {
let mut t = Key::<Self>::default();
t[..kl].copy_from_slice(key);
t
},
_out: PhantomData,
})
}
Expand Down Expand Up @@ -386,7 +396,10 @@ macro_rules! blake2_mac_impl {
{
fn reset(&mut self) {
self.core.reset();
self.buffer = LazyBuffer::new(&self.key_block);
let kl = self.key_block.len();
let mut padded_key = Block::<$hash>::default();
padded_key[..kl].copy_from_slice(&self.key_block);
self.buffer = LazyBuffer::new(&padded_key);
}
}

Expand All @@ -398,17 +411,11 @@ macro_rules! blake2_mac_impl {
{
#[inline]
fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
let Self {
core,
buffer,
key_block,
..
} = self;
let Self { core, buffer, .. } = self;
let mut full_res = Default::default();
core.finalize_variable_core(buffer, &mut full_res);
out.copy_from_slice(&full_res[..OutSize::USIZE]);
core.reset();
*buffer = LazyBuffer::new(key_block);
self.reset();
}
}

Expand Down

0 comments on commit b13d772

Please sign in to comment.