diff --git a/Cargo.lock b/Cargo.lock index 419890947..e2384780b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,7 +74,7 @@ dependencies = [ [[package]] name = "digest" version = "0.9.0-pre" -source = "git+https://github.com/rustcrypto/traits#7f2b98d31f704b55c502a685632bd2ea4bd5dd6c" +source = "git+https://github.com/RustCrypto/traits#b6c5fad59c4079b915a1df20c47bf3154b3612b1" dependencies = [ "blobby", "generic-array", diff --git a/blake2/src/blake2.rs b/blake2/src/blake2.rs index 0e302abd7..f6eec5564 100644 --- a/blake2/src/blake2.rs +++ b/blake2/src/blake2.rs @@ -9,7 +9,7 @@ macro_rules! blake2_impl { use $crate::simd::{Vector4, $vec}; use byteorder::{ByteOrder, LittleEndian}; - use digest::{Update, BlockInput, FixedOutput, VariableOutput, Reset}; + use digest::{Update, BlockInput, FixedOutputDirty, VariableOutputDirty, Reset}; use digest::InvalidOutputSize; use digest::generic_array::GenericArray; use digest::generic_array::typenum::{U4, Unsigned}; @@ -204,12 +204,12 @@ macro_rules! blake2_impl { } #[doc(hidden)] - pub fn finalize_last_node(self) -> Output { + pub fn finalize_last_node(mut self) -> Output { self.finalize_with_flag(!0) } - fn finalize_with_flag(mut self, f1: $word) -> Output { + fn finalize_with_flag(&mut self, f1: $word) -> Output { let off = self.t as usize % (2 * $bytes::to_usize()); if off != 0 { zero(&mut self.m.as_mut_bytes()[off..]); @@ -278,7 +278,7 @@ macro_rules! blake2_impl { } } - impl VariableOutput for $state { + impl VariableOutputDirty for $state { fn new(output_size: usize) -> Result { if output_size == 0 || output_size > $bytes::to_usize() { return Err(InvalidOutputSize); @@ -290,14 +290,14 @@ macro_rules! blake2_impl { self.n } - fn finalize_variable(self, f: F) { + fn finalize_variable_dirty(&mut self, f: impl FnOnce(&[u8])) { let n = self.n; let res = self.finalize_with_flag(0); f(&res[..n]); } } - impl Reset for $state { + impl Reset for $state { fn reset(&mut self) { self.t = self.t0; self.m = self.m0; @@ -340,15 +340,15 @@ macro_rules! blake2_impl { } } - impl FixedOutput for $fix_state { + impl FixedOutputDirty for $fix_state { type OutputSize = $bytes; - fn finalize_fixed(self) -> Output { - self.state.finalize_with_flag(0) + fn finalize_into_dirty(&mut self, out: &mut Output) { + out.copy_from_slice(&self.state.finalize_with_flag(0)); } } - impl Reset for $fix_state { + impl Reset for $fix_state { fn reset(&mut self) { self.state.reset() } @@ -381,7 +381,7 @@ macro_rules! blake2_impl { ::reset(self) } - fn finalize(self) -> crypto_mac::Output { + fn finalize(mut self) -> crypto_mac::Output { crypto_mac::Output::new(self.state.finalize_with_flag(0)) } } diff --git a/gost94/src/gost94.rs b/gost94/src/gost94.rs index b732422ee..b905ba05c 100644 --- a/gost94/src/gost94.rs +++ b/gost94/src/gost94.rs @@ -3,10 +3,9 @@ use block_buffer::block_padding::ZeroPadding; use block_buffer::byteorder::{ByteOrder, LE}; use block_buffer::BlockBuffer; -use digest::generic_array::typenum::U32; -use digest::generic_array::GenericArray; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{consts::U32, generic_array::GenericArray}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; pub(crate) type Block = [u8; 32]; @@ -238,31 +237,30 @@ impl Update for Gost94 { } } -impl FixedOutput for Gost94 { +impl FixedOutputDirty for Gost94 { type OutputSize = U32; - fn finalize_fixed(mut self) -> GenericArray { - { - let self_state = &mut self.state; + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { + let self_state = &mut self.state; - if self.buffer.position() != 0 { - let block = self - .buffer - .pad_with::() - .expect("we never use input_lazy"); - self_state.process_block(block); - } + if self.buffer.position() != 0 { + let block = self + .buffer + .pad_with::() + .expect("we never use input_lazy"); - let mut buf = Block::default(); + self_state.process_block(block); + } - LE::write_u64_into(&self_state.n, &mut buf); - self_state.f(&buf); + let mut buf = Block::default(); - LE::write_u64_into(&self_state.sigma, &mut buf); - self_state.f(&buf); - } + LE::write_u64_into(&self_state.n, &mut buf); + self_state.f(&buf); + + LE::write_u64_into(&self_state.sigma, &mut buf); + self_state.f(&buf); - GenericArray::clone_from_slice(&self.state.h) + out.copy_from_slice(&self.state.h); } } diff --git a/gost94/src/macros.rs b/gost94/src/macros.rs index e2a696611..09ba786b4 100644 --- a/gost94/src/macros.rs +++ b/gost94/src/macros.rs @@ -1,9 +1,8 @@ macro_rules! gost94_impl { ($state:ident, $sbox:expr) => { - use digest::generic_array::typenum::U32; - use digest::generic_array::GenericArray; use digest::impl_write; - use digest::{BlockInput, FixedOutput, Reset, Update}; + use digest::{consts::U32, generic_array::GenericArray}; + use digest::{BlockInput, FixedOutputDirty, Reset, Update}; use $crate::gost94::{Block, Gost94, SBox}; /// GOST94 state @@ -31,11 +30,11 @@ macro_rules! gost94_impl { } } - impl FixedOutput for $state { + impl FixedOutputDirty for $state { type OutputSize = U32; - fn finalize_fixed(self) -> GenericArray { - self.sh.finalize_fixed() + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { + self.sh.finalize_into_dirty(out) } } diff --git a/groestl/src/lib.rs b/groestl/src/lib.rs index c43126233..896c16736 100755 --- a/groestl/src/lib.rs +++ b/groestl/src/lib.rs @@ -45,12 +45,6 @@ extern crate std; pub use digest::{self, Digest}; -use digest::generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64}; -use digest::generic_array::GenericArray; -use digest::impl_write; -use digest::InvalidOutputSize; -use digest::{BlockInput, FixedOutput, Reset, Update, VariableOutput}; - mod consts; mod groestl; mod matrix; @@ -59,6 +53,10 @@ mod state; mod macros; use crate::groestl::Groestl; +use digest::consts::{U128, U28, U32, U48, U64}; +use digest::generic_array::typenum::Unsigned; +use digest::impl_write; +use digest::{BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty}; impl_groestl!(Groestl512, U64, U128); impl_groestl!(Groestl384, U48, U128); diff --git a/groestl/src/macros.rs b/groestl/src/macros.rs index e2f57a528..53e0d8e9d 100644 --- a/groestl/src/macros.rs +++ b/groestl/src/macros.rs @@ -24,13 +24,13 @@ macro_rules! impl_groestl { } } - impl FixedOutput for $state { + impl FixedOutputDirty for $state { type OutputSize = $output; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { let block = self.groestl.finalize(); let n = block.len() - Self::OutputSize::to_usize(); - GenericArray::clone_from_slice(&block[n..]) + out.copy_from_slice(&block[n..]) } } @@ -62,7 +62,7 @@ macro_rules! impl_variable_groestl { } } - impl VariableOutput for $state { + impl VariableOutputDirty for $state { fn new(output_size: usize) -> Result { if output_size == $min || output_size > $max { return Err(InvalidOutputSize); @@ -76,7 +76,7 @@ macro_rules! impl_variable_groestl { self.groestl.output_size } - fn finalize_variable(mut self, f: F) { + fn finalize_variable_dirty(&mut self, f: impl FnOnce(&[u8])) { let block = self.groestl.finalize(); let n = block.len() - self.groestl.output_size; f(&block[n..]); diff --git a/k12/Cargo.toml b/k12/Cargo.toml index fe2d14c55..9cd5f4d9f 100644 --- a/k12/Cargo.toml +++ b/k12/Cargo.toml @@ -15,7 +15,7 @@ categories = ["cryptography", "no-std"] digest = { version = "= 0.9.0-pre", features = ["alloc"] } [dev-dependencies] -digest = { version = "= 0.9.0-pre", features = ["dev"] } +digest = { version = "= 0.9.0-pre", features = ["alloc", "dev"] } hex-literal = "0.2" [features] diff --git a/k12/src/lib.rs b/k12/src/lib.rs index 2fd53651d..df2194cb8 100644 --- a/k12/src/lib.rs +++ b/k12/src/lib.rs @@ -10,10 +10,11 @@ #![no_std] #![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] // TODO(tarcieri): eliminate alloc requirement +#[macro_use] extern crate alloc; pub use digest; @@ -23,8 +24,8 @@ mod lanes; // TODO(tarcieri): eliminate usage of `Vec` use alloc::vec::Vec; -use core::{cmp::min, convert::TryInto}; -use digest::{ExtendableOutput, Update, XofReader}; +use core::{cmp::min, convert::TryInto, mem}; +use digest::{ExtendableOutputDirty, Reset, Update, XofReader}; /// The KangarooTwelve extendable-output function (XOF). #[derive(Debug, Default)] @@ -60,18 +61,30 @@ impl Update for KangarooTwelve { } } -impl ExtendableOutput for KangarooTwelve { +impl ExtendableOutputDirty for KangarooTwelve { type Reader = Reader; - fn finalize_xof(self) -> Self::Reader { + fn finalize_xof_dirty(&mut self) -> Self::Reader { + let mut buffer = vec![]; + let mut customization = vec![]; + + mem::swap(&mut self.buffer, &mut buffer); + mem::swap(&mut self.customization, &mut customization); + Reader { - buffer: self.buffer, - customization: self.customization, + buffer, + customization, finished: false, } } } +impl Reset for KangarooTwelve { + fn reset(&mut self) { + self.buffer.clear(); + } +} + /// Extensible output reader. /// /// NOTE: this presently only supports one invocation and will *panic* if diff --git a/k12/tests/lib.rs b/k12/tests/lib.rs index 85b89795b..a71c0a2cd 100644 --- a/k12/tests/lib.rs +++ b/k12/tests/lib.rs @@ -4,7 +4,7 @@ use k12::{ KangarooTwelve, }; -fn read_bytes>(s: T) -> Vec { +fn read_bytes>(s: T) -> Box<[u8]> { fn b(c: u8) -> u8 { match c { b'0'..=b'9' => c - b'0', @@ -13,9 +13,11 @@ fn read_bytes>(s: T) -> Vec { _ => unreachable!(), } } + let s = s.as_ref(); let mut i = 0; let mut v = Vec::new(); + while i < s.len() { if s[i] == b' ' || s[i] == b'\n' { i += 1; @@ -26,14 +28,15 @@ fn read_bytes>(s: T) -> Vec { v.push(n); i += 2; } - v + + v.into_boxed_slice() } #[test] fn empty() { // Source: reference paper assert_eq!( - KangarooTwelve::new().chain(b"").finalize_vec(32), + KangarooTwelve::new().chain(b"").finalize_boxed(32), read_bytes( "1a c2 d4 50 fc 3b 42 05 d1 9d a7 bf ca 1b 37 51 3c 08 03 57 7a c7 16 7f 06 fe 2c e1 f0 ef 39 e5" @@ -41,7 +44,7 @@ fn empty() { ); assert_eq!( - KangarooTwelve::new().chain(b"").finalize_vec(64), + KangarooTwelve::new().chain(b"").finalize_boxed(64), read_bytes( "1a c2 d4 50 fc 3b 42 05 d1 9d a7 bf ca 1b 37 51 3c 08 03 57 7a c7 16 7f 06 fe 2c e1 f0 ef 39 e5 42 69 c0 56 b8 c8 2e @@ -50,7 +53,7 @@ fn empty() { ); assert_eq!( - KangarooTwelve::new().chain(b"").finalize_vec(10032)[10000..], + KangarooTwelve::new().chain(b"").finalize_boxed(10032)[10000..], read_bytes( "e8 dc 56 36 42 f7 22 8c 84 68 4c 89 84 05 d3 a8 34 79 91 58 c0 79 b1 28 80 27 7a 1d 28 e2 ff 6d" @@ -81,7 +84,7 @@ fn pat_m() { { let len = 17usize.pow(i); let m: Vec = (0..len).map(|j| (j % 251) as u8).collect(); - let result = KangarooTwelve::new().chain(&m).finalize_vec(32); + let result = KangarooTwelve::new().chain(&m).finalize_boxed(32); assert_eq!(result, read_bytes(expected[i as usize])); } } @@ -104,7 +107,7 @@ fn pat_c() { let c: Vec = (0..len).map(|j| (j % 251) as u8).collect(); let result = KangarooTwelve::new_with_customization(c) .chain(&m) - .finalize_vec(32); + .finalize_boxed(32); assert_eq!(result, read_bytes(expected[i as usize])); } } diff --git a/md2/src/lib.rs b/md2/src/lib.rs index a772989f0..8d0e8ba27 100644 --- a/md2/src/lib.rs +++ b/md2/src/lib.rs @@ -36,12 +36,10 @@ extern crate std; pub use digest::{self, Digest}; -use block_buffer::block_padding::Pkcs7; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::U16; -use digest::generic_array::GenericArray; +use block_buffer::{block_padding::Pkcs7, BlockBuffer}; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{consts::U16, generic_array::GenericArray}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; mod consts; @@ -107,18 +105,21 @@ impl Update for Md2 { } } -impl FixedOutput for Md2 { +impl FixedOutputDirty for Md2 { type OutputSize = U16; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { let buf = self .buffer .pad_with::() .expect("we never use input_lazy"); + self.state.process_block(buf); + let checksum = self.state.checksum; self.state.process_block(&checksum); - GenericArray::clone_from_slice(&self.state.x[0..16]) + + out.copy_from_slice(&self.state.x[0..16]); } } diff --git a/md4/src/lib.rs b/md4/src/lib.rs index f7c75614c..0938f6ba7 100644 --- a/md4/src/lib.rs +++ b/md4/src/lib.rs @@ -31,7 +31,6 @@ #[macro_use] extern crate opaque_debug; -extern crate fake_simd as simd; #[cfg(feature = "std")] extern crate std; @@ -41,10 +40,13 @@ pub use digest::{self, Digest}; use crate::simd::u32x4; use block_buffer::byteorder::{ByteOrder, LE}; use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U16, U64}; -use digest::generic_array::GenericArray; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{ + consts::{U16, U64}, + generic_array::GenericArray, +}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; +use fake_simd as simd; // initial values for Md4State const S: u32x4 = u32x4(0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476); @@ -164,19 +166,16 @@ impl Update for Md4 { } } -impl FixedOutput for Md4 { +impl FixedOutputDirty for Md4 { type OutputSize = U16; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.finalize_inner(); - let mut out = GenericArray::default(); LE::write_u32(&mut out[0..4], self.state.s.0); LE::write_u32(&mut out[4..8], self.state.s.1); LE::write_u32(&mut out[8..12], self.state.s.2); LE::write_u32(&mut out[12..16], self.state.s.3); - - out } } diff --git a/md5/src/lib.rs b/md5/src/lib.rs index 9bf8c22c0..fce208169 100644 --- a/md5/src/lib.rs +++ b/md5/src/lib.rs @@ -49,7 +49,7 @@ use block_buffer::BlockBuffer; use digest::generic_array::typenum::{U16, U64}; use digest::generic_array::GenericArray; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; mod consts; @@ -106,15 +106,13 @@ impl Update for Md5 { } } -impl FixedOutput for Md5 { +impl FixedOutputDirty for Md5 { type OutputSize = U16; #[inline] - fn finalize_fixed(mut self) -> GenericArray { - let mut out = GenericArray::default(); + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { self.finalize_inner(); - LE::write_u32_into(&self.state, &mut out); - out + LE::write_u32_into(&self.state, out); } } diff --git a/ripemd160/src/lib.rs b/ripemd160/src/lib.rs index 76379a5e8..046eb7490 100644 --- a/ripemd160/src/lib.rs +++ b/ripemd160/src/lib.rs @@ -30,20 +30,22 @@ #[macro_use] extern crate opaque_debug; + #[cfg(feature = "std")] extern crate std; -pub use digest::{self, Digest}; +mod block; -use block_buffer::byteorder::{ByteOrder, LE}; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U20, U64}; -use digest::generic_array::GenericArray; -use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +pub use digest::{self, Digest}; -mod block; use crate::block::{process_msg_block, DIGEST_BUF_LEN, H0}; +use block_buffer::{ + byteorder::{ByteOrder, LE}, + BlockBuffer, +}; +use digest::consts::{U20, U64}; +use digest::impl_write; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; /// Structure representing the state of a Ripemd160 computation #[derive(Clone)] @@ -77,20 +79,16 @@ impl Update for Ripemd160 { } } -impl FixedOutput for Ripemd160 { +impl FixedOutputDirty for Ripemd160 { type OutputSize = U20; - fn finalize_fixed(mut self) -> GenericArray { - { - let h = &mut self.h; - let l = self.len << 3; - self.buffer - .len64_padding::(l, |b| process_msg_block(h, b)); - } + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { + let h = &mut self.h; + let l = self.len << 3; + self.buffer + .len64_padding::(l, |b| process_msg_block(h, b)); - let mut out = GenericArray::default(); LE::write_u32_into(&self.h, &mut out[..]); - out } } diff --git a/ripemd320/src/lib.rs b/ripemd320/src/lib.rs index 099096ce1..a88e44bbe 100644 --- a/ripemd320/src/lib.rs +++ b/ripemd320/src/lib.rs @@ -41,12 +41,13 @@ pub use digest::{self, Digest}; use crate::block::{process_msg_block, DIGEST_BUF_LEN, H0}; -use block_buffer::byteorder::{ByteOrder, LE}; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U40, U64}; -use digest::generic_array::GenericArray; +use block_buffer::{ + byteorder::{ByteOrder, LE}, + BlockBuffer, +}; +use digest::consts::{U40, U64}; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; /// Structure representing the state of a ripemd320 computation #[derive(Clone)] @@ -80,20 +81,16 @@ impl Update for Ripemd320 { } } -impl FixedOutput for Ripemd320 { +impl FixedOutputDirty for Ripemd320 { type OutputSize = U40; - fn finalize_fixed(mut self) -> GenericArray { - { - let h = &mut self.h; - let l = self.len << 3; - self.buffer - .len64_padding::(l, |b| process_msg_block(h, b)); - } + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { + let h = &mut self.h; + let l = self.len << 3; + self.buffer + .len64_padding::(l, |b| process_msg_block(h, b)); - let mut out = GenericArray::default(); LE::write_u32_into(&self.h, &mut out[..]); - out } } diff --git a/sha1/src/lib.rs b/sha1/src/lib.rs index 3814fed5c..8a537c204 100644 --- a/sha1/src/lib.rs +++ b/sha1/src/lib.rs @@ -70,13 +70,13 @@ mod utils; pub use digest::{self, Digest}; use crate::consts::{H, STATE_LEN}; - -use block_buffer::byteorder::{ByteOrder, BE}; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U20, U64}; -use digest::generic_array::GenericArray; +use block_buffer::{ + byteorder::{ByteOrder, BE}, + BlockBuffer, +}; +use digest::consts::{U20, U64}; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; #[cfg(not(feature = "asm"))] use crate::utils::compress; @@ -84,6 +84,9 @@ use crate::utils::compress; #[cfg(any(not(feature = "asm"), feature = "asm-aarch64"))] use fake_simd as simd; +#[cfg(feature = "asm")] +use digest::generic_array::GenericArray; + /// Structure representing the state of a SHA-1 computation #[derive(Clone)] pub struct Sha1 { @@ -116,19 +119,17 @@ impl Update for Sha1 { } } -impl FixedOutput for Sha1 { +impl FixedOutputDirty for Sha1 { type OutputSize = U20; - fn finalize_fixed(mut self) -> GenericArray { - { - let state = &mut self.h; - let l = self.len << 3; - self.buffer - .len64_padding::(l, |d| compress(state, d)); - } - let mut out = GenericArray::default(); - BE::write_u32_into(&self.h, &mut out); - out + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { + let state = &mut self.h; + let l = self.len << 3; + + self.buffer + .len64_padding::(l, |d| compress(state, d)); + + BE::write_u32_into(&self.h, out); } } diff --git a/sha2/src/sha256.rs b/sha2/src/sha256.rs index 9e9c297b8..ddc74333b 100644 --- a/sha2/src/sha256.rs +++ b/sha2/src/sha256.rs @@ -1,14 +1,20 @@ -use block_buffer::byteorder::{ByteOrder, BE}; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U28, U32, U64}; -use digest::generic_array::GenericArray; -use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +//! SHA-256 use crate::consts::{H224, H256, STATE_LEN}; +use block_buffer::{ + byteorder::{ByteOrder, BE}, + BlockBuffer, +}; +use digest::impl_write; +use digest::{ + consts::{U28, U32, U64}, + generic_array::GenericArray, +}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; #[cfg(not(feature = "asm"))] use crate::sha256_utils::compress256; + #[cfg(feature = "asm")] use sha2_asm::compress256; @@ -111,14 +117,12 @@ impl Update for Sha256 { } } -impl FixedOutput for Sha256 { +impl FixedOutputDirty for Sha256 { type OutputSize = U32; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); BE::write_u32_into(&self.engine.state.h, out.as_mut_slice()); - out } } @@ -153,14 +157,12 @@ impl Update for Sha224 { } } -impl FixedOutput for Sha224 { +impl FixedOutputDirty for Sha224 { type OutputSize = U28; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); BE::write_u32_into(&self.engine.state.h[..7], out.as_mut_slice()); - out } } diff --git a/sha2/src/sha512.rs b/sha2/src/sha512.rs index 5941ed932..0a4a76080 100644 --- a/sha2/src/sha512.rs +++ b/sha2/src/sha512.rs @@ -1,14 +1,20 @@ -use block_buffer::byteorder::{ByteOrder, BE}; -use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U128, U28, U32, U48, U64}; -use digest::generic_array::GenericArray; -use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +//! SHA-512 use crate::consts::{H384, H512, H512_TRUNC_224, H512_TRUNC_256, STATE_LEN}; +use block_buffer::{ + byteorder::{ByteOrder, BE}, + BlockBuffer, +}; +use digest::impl_write; +use digest::{ + consts::{U128, U28, U32, U48, U64}, + generic_array::GenericArray, +}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; #[cfg(any(not(feature = "asm"), target_arch = "aarch64"))] use crate::sha512_utils::compress512; + #[cfg(all(feature = "asm", not(target_arch = "aarch64")))] use sha2_asm::compress512; @@ -99,15 +105,12 @@ impl Update for Sha512 { } } -impl FixedOutput for Sha512 { +impl FixedOutputDirty for Sha512 { type OutputSize = U64; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - - let mut out = GenericArray::default(); BE::write_u64_into(&self.engine.state.h[..], out.as_mut_slice()); - out } } @@ -142,15 +145,12 @@ impl Update for Sha384 { } } -impl FixedOutput for Sha384 { +impl FixedOutputDirty for Sha384 { type OutputSize = U48; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - - let mut out = GenericArray::default(); BE::write_u64_into(&self.engine.state.h[..6], out.as_mut_slice()); - out } } @@ -185,15 +185,12 @@ impl Update for Sha512Trunc256 { } } -impl FixedOutput for Sha512Trunc256 { +impl FixedOutputDirty for Sha512Trunc256 { type OutputSize = U32; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - - let mut out = GenericArray::default(); BE::write_u64_into(&self.engine.state.h[..4], out.as_mut_slice()); - out } } @@ -228,16 +225,13 @@ impl Update for Sha512Trunc224 { } } -impl FixedOutput for Sha512Trunc224 { +impl FixedOutputDirty for Sha512Trunc224 { type OutputSize = U28; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - - let mut out = GenericArray::default(); BE::write_u64_into(&self.engine.state.h[..3], &mut out[..24]); BE::write_u32(&mut out[24..28], (self.engine.state.h[3] >> 32) as u32); - out } } diff --git a/sha3/src/lib.rs b/sha3/src/lib.rs index c57be3d1e..6e0eff24e 100644 --- a/sha3/src/lib.rs +++ b/sha3/src/lib.rs @@ -52,12 +52,10 @@ extern crate std; pub use digest::{self, Digest}; use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{ - Unsigned, U104, U136, U144, U168, U200, U28, U32, U48, U64, U72, -}; -use digest::generic_array::GenericArray; +use digest::consts::{U104, U136, U144, U168, U200, U28, U32, U48, U64, U72}; +use digest::generic_array::typenum::Unsigned; use digest::impl_write; -use digest::{BlockInput, ExtendableOutput, FixedOutput, Reset, Update}; +use digest::{BlockInput, ExtendableOutputDirty, FixedOutputDirty, Reset, Update}; mod paddings; #[macro_use] diff --git a/sha3/src/macros.rs b/sha3/src/macros.rs index 1f9b08c1d..d24764f9a 100644 --- a/sha3/src/macros.rs +++ b/sha3/src/macros.rs @@ -38,18 +38,16 @@ macro_rules! sha3_impl { } } - impl FixedOutput for $state { + impl FixedOutputDirty for $state { type OutputSize = $output_size; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.apply_padding(); - let mut out = GenericArray::default(); let n = out.len(); self.state.as_bytes(|state| { out.copy_from_slice(&state[..n]); }); - out } } @@ -75,10 +73,10 @@ macro_rules! shake_impl { } } - impl ExtendableOutput for $state { + impl ExtendableOutputDirty for $state { type Reader = Sha3XofReader; - fn finalize_xof(mut self) -> Sha3XofReader { + fn finalize_xof_dirty(&mut self) -> Sha3XofReader { self.apply_padding(); let r = $rate::to_usize(); Sha3XofReader::new(self.state.clone(), r) diff --git a/shabal/src/shabal.rs b/shabal/src/shabal.rs index fbf34ec67..37b45cc2f 100644 --- a/shabal/src/shabal.rs +++ b/shabal/src/shabal.rs @@ -1,10 +1,14 @@ +//! Shabal + use block_buffer::block_padding::Iso7816; use block_buffer::byteorder::{ByteOrder, LE}; use block_buffer::BlockBuffer; -use digest::generic_array::typenum::{U24, U28, U32, U48, U64}; -use digest::generic_array::GenericArray; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{ + consts::{U24, U28, U32, U48, U64}, + generic_array::GenericArray, +}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; use crate::consts::{ A_INIT_192, A_INIT_224, A_INIT_256, A_INIT_384, A_INIT_512, B_INIT_192, B_INIT_224, B_INIT_256, @@ -277,14 +281,12 @@ impl Update for Shabal512 { } } -impl FixedOutput for Shabal512 { +impl FixedOutputDirty for Shabal512 { type OutputSize = U64; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); LE::write_u32_into(&self.engine.state.b[0..16], out.as_mut_slice()); - out } } @@ -319,14 +321,12 @@ impl Update for Shabal384 { } } -impl FixedOutput for Shabal384 { +impl FixedOutputDirty for Shabal384 { type OutputSize = U48; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); LE::write_u32_into(&self.engine.state.b[4..16], out.as_mut_slice()); - out } } @@ -361,14 +361,12 @@ impl Update for Shabal256 { } } -impl FixedOutput for Shabal256 { +impl FixedOutputDirty for Shabal256 { type OutputSize = U32; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); - LE::write_u32_into(&self.engine.state.b[8..16], out.as_mut_slice()); - out + LE::write_u32_into(&self.engine.state.b[8..16], out); } } @@ -403,14 +401,12 @@ impl Update for Shabal224 { } } -impl FixedOutput for Shabal224 { +impl FixedOutputDirty for Shabal224 { type OutputSize = U28; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); - LE::write_u32_into(&self.engine.state.b[9..16], out.as_mut_slice()); - out + LE::write_u32_into(&self.engine.state.b[9..16], out); } } @@ -445,14 +441,12 @@ impl Update for Shabal192 { } } -impl FixedOutput for Shabal192 { +impl FixedOutputDirty for Shabal192 { type OutputSize = U24; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut digest::Output) { self.engine.finish(); - let mut out = GenericArray::default(); - LE::write_u32_into(&self.engine.state.b[10..16], out.as_mut_slice()); - out + LE::write_u32_into(&self.engine.state.b[10..16], out); } } diff --git a/streebog/src/lib.rs b/streebog/src/lib.rs index 2d6c3336b..b88f14875 100644 --- a/streebog/src/lib.rs +++ b/streebog/src/lib.rs @@ -58,8 +58,9 @@ mod table; pub use digest::{self, Digest}; -use digest::generic_array::typenum::{U32, U64}; +use digest::consts::{U32, U64}; use digest::impl_write; + #[cfg(feature = "std")] use digest::Update; diff --git a/streebog/src/streebog.rs b/streebog/src/streebog.rs index 5c6aabab4..6a9261262 100644 --- a/streebog/src/streebog.rs +++ b/streebog/src/streebog.rs @@ -1,11 +1,15 @@ -use block_buffer::block_padding::ZeroPadding; -use block_buffer::byteorder::{ByteOrder, LE}; -use block_buffer::BlockBuffer; +//! Streebog (GOST R 34.11-2012) + +use block_buffer::{ + block_padding::ZeroPadding, + byteorder::{ByteOrder, LE}, + BlockBuffer, +}; use byte_tools::copy; use core::marker::PhantomData; -use digest::generic_array::typenum::U64; +use digest::consts::U64; use digest::generic_array::{ArrayLength, GenericArray}; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; use crate::consts::{BLOCK_SIZE, C}; use crate::table::SHUFFLED_LIN_TABLE; @@ -143,13 +147,13 @@ where } } -impl FixedOutput for Streebog +impl FixedOutputDirty for Streebog where N: ArrayLength + Copy, { type OutputSize = N; - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { let mut self_state = self.state; let pos = self.buffer.position(); @@ -166,7 +170,7 @@ where self_state.g(&[0u8; 64], &sigma); let n = BLOCK_SIZE - Self::OutputSize::to_usize(); - GenericArray::clone_from_slice(&self_state.h[n..]) + out.copy_from_slice(&self_state.h[n..]) } } diff --git a/whirlpool/src/lib.rs b/whirlpool/src/lib.rs index 34862fa34..4caf545cb 100644 --- a/whirlpool/src/lib.rs +++ b/whirlpool/src/lib.rs @@ -52,22 +52,21 @@ use whirlpool_asm as utils; #[cfg(not(feature = "asm"))] mod utils; +#[cfg(not(feature = "asm"))] +mod consts; + pub use digest::Digest; use crate::utils::compress; -use block_buffer::block_padding::Iso7816; -#[cfg(not(feature = "asm"))] -use block_buffer::byteorder::{ByteOrder, BE}; -use block_buffer::BlockBuffer; +use block_buffer::{block_padding::Iso7816, BlockBuffer}; use byte_tools::zero; -use digest::generic_array::typenum::U64; -use digest::generic_array::GenericArray; use digest::impl_write; -use digest::{BlockInput, FixedOutput, Reset, Update}; +use digest::{consts::U64, generic_array::GenericArray}; +use digest::{BlockInput, FixedOutputDirty, Reset, Update}; #[cfg(not(feature = "asm"))] -mod consts; +use block_buffer::byteorder::{ByteOrder, BE}; type BlockSize = U64; @@ -172,22 +171,19 @@ impl Update for Whirlpool { } } -impl FixedOutput for Whirlpool { +impl FixedOutputDirty for Whirlpool { type OutputSize = U64; #[cfg(not(feature = "asm"))] - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { self.finalize_inner(); - - let mut out = GenericArray::default(); - BE::write_u64_into(&self.hash[..], &mut out); - out + BE::write_u64_into(&self.hash[..], out); } #[cfg(feature = "asm")] - fn finalize_fixed(mut self) -> GenericArray { + fn finalize_into_dirty(&mut self, out: &mut GenericArray) { self.finalize_inner(); - GenericArray::clone_from_slice(&self.hash) + out.copy_from_slice(&self.hash) } }