Skip to content

Commit

Permalink
update VariableOutputCore impls
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 30, 2021
1 parent 0671b21 commit 4e2abaf
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 72 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use digest::{
consts::{U128, U32, U4, U64},
core_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper,
CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, UpdateCore,
CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore,
VariableOutputCore,
},
crypto_common::{InvalidLength, Key, KeyInit, KeySizeUser},
Expand Down
27 changes: 18 additions & 9 deletions blake2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ macro_rules! blake2_impl {
&mut self,
final_block: &GenericArray<u8, $block_size>,
flag: $word,
) -> GenericArray<u8, $bytes> {
out: &mut Output<Self>,
) {
self.compress(final_block, !0, flag);
let buf = [self.h[0].to_le(), self.h[1].to_le()];
GenericArray::clone_from_slice(buf.as_bytes())
out.copy_from_slice(buf.as_bytes())
}

fn compress(&mut self, block: &Block<Self>, f0: $word, f1: $word) {
Expand Down Expand Up @@ -200,24 +201,30 @@ macro_rules! blake2_impl {
}
}

impl OutputSizeUser for $name {
type OutputSize = $bytes;
}

impl VariableOutputCore for $name {
type MaxOutputSize = $bytes;
const TRUNC_SIDE: TruncSide = TruncSide::Left;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
if output_size > Self::MaxOutputSize::USIZE {
if output_size > Self::OutputSize::USIZE {
return Err(InvalidOutputSize);
}
Ok(Self::new_with_params(&[], &[], 0, output_size))
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(
&mut self,
buffer: &mut Buffer<Self>,
out: &mut Output<Self>,
) {
self.t += buffer.get_pos() as u64;
let block = buffer.pad_with_zeros();
let res = self.finalize_with_flag(block, 0);
assert!(res.len() >= out.len());
out.copy_from_slice(&res[..out.len()]);
self.finalize_with_flag(block, 0, out);
}
}

Expand Down Expand Up @@ -365,7 +372,9 @@ macro_rules! blake2_mac_impl {
#[inline]
fn finalize_into(mut self, out: &mut Output<Self>) {
let Self { core, buffer, .. } = &mut self;
core.finalize_variable_core(buffer, out);
let mut full_res = Default::default();
core.finalize_variable_core(buffer, &mut full_res);
out.copy_from_slice(&full_res[..OutSize::USIZE]);
}
}

Expand Down
39 changes: 20 additions & 19 deletions groestl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@

pub use digest::{self, Digest};

use core::{cmp::min, fmt};
use core::fmt;
use digest::{
block_buffer::Eager,
core_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper,
CtVariableCoreWrapper, RtVariableCoreWrapper, UpdateCore, VariableOutputCore,
CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore,
VariableOutputCore,
},
generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64},
HashMarker, InvalidOutputSize,
HashMarker, InvalidOutputSize, Output,
};

mod compress1024;
Expand Down Expand Up @@ -78,12 +79,16 @@ impl UpdateCore for GroestlShortVarCore {
}
}

impl OutputSizeUser for GroestlShortVarCore {
type OutputSize = U32;
}

impl VariableOutputCore for GroestlShortVarCore {
type MaxOutputSize = U32;
const TRUNC_SIDE: TruncSide = TruncSide::Right;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
if output_size > Self::MaxOutputSize::USIZE {
if output_size > Self::OutputSize::USIZE {
return Err(InvalidOutputSize);
}
let mut state = [0; compress512::COLS];
Expand All @@ -93,22 +98,18 @@ impl VariableOutputCore for GroestlShortVarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let blocks_len = if buffer.remaining() <= 8 {
self.blocks_len + 2
} else {
self.blocks_len + 1
};
buffer.len64_padding_be(blocks_len, |b| compress512::compress(&mut self.state, b));
let res = compress512::p(&self.state);
let mut buf = [0; Self::MaxOutputSize::USIZE];
let n = compress512::COLS / 2;
for (chunk, v) in buf.chunks_exact_mut(8).zip(res[n..].iter()) {
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
let n = min(buf.len(), out.len());
let m = buf.len() - out.len();
out[..n].copy_from_slice(&buf[m..]);
}
}

Expand Down Expand Up @@ -164,12 +165,16 @@ impl UpdateCore for GroestlLongVarCore {
}
}

impl OutputSizeUser for GroestlLongVarCore {
type OutputSize = U64;
}

impl VariableOutputCore for GroestlLongVarCore {
type MaxOutputSize = U64;
const TRUNC_SIDE: TruncSide = TruncSide::Right;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
if output_size > Self::MaxOutputSize::USIZE {
if output_size > Self::OutputSize::USIZE {
return Err(InvalidOutputSize);
}
let mut state = [0; compress1024::COLS];
Expand All @@ -179,22 +184,18 @@ impl VariableOutputCore for GroestlLongVarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let blocks_len = if buffer.remaining() <= 8 {
self.blocks_len + 2
} else {
self.blocks_len + 1
};
buffer.len64_padding_be(blocks_len, |b| compress1024::compress(&mut self.state, b));
let res = compress1024::p(&self.state);
let mut buf = [0; Self::MaxOutputSize::USIZE];
let n = compress1024::COLS / 2;
for (chunk, v) in buf.chunks_exact_mut(8).zip(res[n..].iter()) {
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
let n = min(buf.len(), out.len());
let m = buf.len() - out.len();
out[..n].copy_from_slice(&buf[m..]);
}
}

Expand Down
35 changes: 18 additions & 17 deletions sha2/src/core_api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{consts, sha256::compress256, sha512::compress512};
use core::{cmp::min, fmt, slice::from_ref};
use core::{fmt, slice::from_ref};
use digest::{
block_buffer::Eager,
core_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, UpdateCore, VariableOutputCore,
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide,
UpdateCore, VariableOutputCore,
},
generic_array::typenum::{Unsigned, U128, U32, U64},
HashMarker, InvalidOutputSize,
HashMarker, InvalidOutputSize, Output,
};

/// Core block-level SHA-256 hasher with variable output size.
Expand Down Expand Up @@ -37,8 +38,12 @@ impl UpdateCore for Sha256VarCore {
}
}

impl OutputSizeUser for Sha256VarCore {
type OutputSize = U32;
}

impl VariableOutputCore for Sha256VarCore {
type MaxOutputSize = U32;
const TRUNC_SIDE: TruncSide = TruncSide::Left;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
Expand All @@ -52,18 +57,14 @@ impl VariableOutputCore for Sha256VarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64;
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
buffer.len64_padding_be(bit_len, |b| compress256(&mut self.state, from_ref(b)));

let mut buf = [0u8; Self::MaxOutputSize::USIZE];
for (chunk, v) in buf.chunks_exact_mut(4).zip(self.state.iter()) {
for (chunk, v) in out.chunks_exact_mut(4).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}

let n = min(out.len(), buf.len());
out[..n].copy_from_slice(&buf[..n]);
}
}

Expand Down Expand Up @@ -109,8 +110,12 @@ impl UpdateCore for Sha512VarCore {
}
}

impl OutputSizeUser for Sha512VarCore {
type OutputSize = U64;
}

impl VariableOutputCore for Sha512VarCore {
type MaxOutputSize = U64;
const TRUNC_SIDE: TruncSide = TruncSide::Left;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
Expand All @@ -126,18 +131,14 @@ impl VariableOutputCore for Sha512VarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64 as u128;
let bit_len = 8 * (buffer.get_pos() as u128 + bs * self.block_len);
buffer.len128_padding_be(bit_len, |b| compress512(&mut self.state, from_ref(b)));

let mut buf = [0u8; Self::MaxOutputSize::USIZE];
for (chunk, v) in buf.chunks_exact_mut(8).zip(self.state.iter()) {
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}

let n = min(out.len(), buf.len());
out[..n].copy_from_slice(&buf[..n]);
}
}

Expand Down
28 changes: 13 additions & 15 deletions shabal/src/core_api.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::consts;
use core::{cmp::min, convert::TryInto, fmt, mem, num::Wrapping};
use core::{convert::TryInto, fmt, mem, num::Wrapping};
use digest::{
block_buffer::Eager,
consts::U64,
core_api::{
AlgorithmName, BlockSizeUser, Buffer, BufferKindUser, UpdateCore, VariableOutputCore,
AlgorithmName, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide,
UpdateCore, VariableOutputCore,
},
generic_array::{
typenum::{Unsigned, U64},
GenericArray,
},
HashMarker, InvalidOutputSize,
generic_array::GenericArray,
HashMarker, InvalidOutputSize, Output,
};

type BlockSize = U64;
Expand Down Expand Up @@ -181,8 +180,12 @@ impl UpdateCore for ShabalVarCore {
}
}

impl OutputSizeUser for ShabalVarCore {
type OutputSize = U64;
}

impl VariableOutputCore for ShabalVarCore {
type MaxOutputSize = U64;
const TRUNC_SIDE: TruncSide = TruncSide::Right;

#[inline]
#[allow(clippy::needless_range_loop)]
Expand Down Expand Up @@ -211,7 +214,7 @@ impl VariableOutputCore for ShabalVarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let pos = buffer.get_pos();
let block = buffer.pad_with_zeros();
block[pos] = 0x80;
Expand All @@ -226,14 +229,9 @@ impl VariableOutputCore for ShabalVarCore {
self.perm(&m);
}

let mut buf = [0u8; Self::MaxOutputSize::USIZE];
for (chunk, v) in buf.chunks_exact_mut(4).zip(self.b.iter()) {
for (chunk, v) in out.chunks_exact_mut(4).zip(self.b.iter()) {
chunk.copy_from_slice(&v.0.to_le_bytes());
}

let n = min(out.len(), buf.len());
let m = buf.len() - n;
out[..n].copy_from_slice(&buf[m..]);
}
}

Expand Down
19 changes: 10 additions & 9 deletions streebog/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use digest::{
block_buffer::Eager,
consts::U64,
core_api::{
AlgorithmName, Block as GenBlock, BlockSizeUser, Buffer, BufferKindUser, UpdateCore,
VariableOutputCore,
AlgorithmName, Block as GenBlock, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser,
TruncSide, UpdateCore, VariableOutputCore,
},
HashMarker, InvalidOutputSize,
HashMarker, InvalidOutputSize, Output,
};

use crate::consts::{BLOCK_SIZE, C};
Expand Down Expand Up @@ -120,8 +120,12 @@ impl UpdateCore for StreebogVarCore {
}
}

impl OutputSizeUser for StreebogVarCore {
type OutputSize = U64;
}

impl VariableOutputCore for StreebogVarCore {
type MaxOutputSize = U64;
const TRUNC_SIDE: TruncSide = TruncSide::Right;

#[inline]
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
Expand All @@ -135,17 +139,14 @@ impl VariableOutputCore for StreebogVarCore {
}

#[inline]
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut [u8]) {
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let pos = buffer.get_pos();
let block = buffer.pad_with_zeros();
block[pos] = 1;
self.compress(block, pos as u64);
self.g(&[0u8; 64], &to_bytes(&self.n));
self.g(&[0u8; 64], &to_bytes(&self.sigma));

let bs = self.h.len();
let n = core::cmp::min(out.len(), bs);
out[..n].copy_from_slice(&self.h[bs - n..]);
out.copy_from_slice(&self.h);
}
}

Expand Down

0 comments on commit 4e2abaf

Please sign in to comment.