Skip to content

Commit 26bea13

Browse files
authored
Use new *Dirty traits from the digest crate (#153)
Updates all of the hash implementations in this repo to use the new `*Dirty` traits which support blanket impls of the original traits, which either consume the hasher instance or can be reset. This will also provide a marginal efficiency boost, at least until placement return lands (which, as it were, may be soon).
1 parent 2c14fad commit 26bea13

File tree

23 files changed

+232
-240
lines changed

23 files changed

+232
-240
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/src/blake2.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! blake2_impl {
99
use $crate::simd::{Vector4, $vec};
1010

1111
use byteorder::{ByteOrder, LittleEndian};
12-
use digest::{Update, BlockInput, FixedOutput, VariableOutput, Reset};
12+
use digest::{Update, BlockInput, FixedOutputDirty, VariableOutputDirty, Reset};
1313
use digest::InvalidOutputSize;
1414
use digest::generic_array::GenericArray;
1515
use digest::generic_array::typenum::{U4, Unsigned};
@@ -204,12 +204,12 @@ macro_rules! blake2_impl {
204204
}
205205

206206
#[doc(hidden)]
207-
pub fn finalize_last_node(self) -> Output {
207+
pub fn finalize_last_node(mut self) -> Output {
208208
self.finalize_with_flag(!0)
209209
}
210210

211211

212-
fn finalize_with_flag(mut self, f1: $word) -> Output {
212+
fn finalize_with_flag(&mut self, f1: $word) -> Output {
213213
let off = self.t as usize % (2 * $bytes::to_usize());
214214
if off != 0 {
215215
zero(&mut self.m.as_mut_bytes()[off..]);
@@ -278,7 +278,7 @@ macro_rules! blake2_impl {
278278
}
279279
}
280280

281-
impl VariableOutput for $state {
281+
impl VariableOutputDirty for $state {
282282
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
283283
if output_size == 0 || output_size > $bytes::to_usize() {
284284
return Err(InvalidOutputSize);
@@ -290,14 +290,14 @@ macro_rules! blake2_impl {
290290
self.n
291291
}
292292

293-
fn finalize_variable<F: FnOnce(&[u8])>(self, f: F) {
293+
fn finalize_variable_dirty(&mut self, f: impl FnOnce(&[u8])) {
294294
let n = self.n;
295295
let res = self.finalize_with_flag(0);
296296
f(&res[..n]);
297297
}
298298
}
299299

300-
impl Reset for $state {
300+
impl Reset for $state {
301301
fn reset(&mut self) {
302302
self.t = self.t0;
303303
self.m = self.m0;
@@ -340,15 +340,15 @@ macro_rules! blake2_impl {
340340
}
341341
}
342342

343-
impl FixedOutput for $fix_state {
343+
impl FixedOutputDirty for $fix_state {
344344
type OutputSize = $bytes;
345345

346-
fn finalize_fixed(self) -> Output {
347-
self.state.finalize_with_flag(0)
346+
fn finalize_into_dirty(&mut self, out: &mut Output) {
347+
out.copy_from_slice(&self.state.finalize_with_flag(0));
348348
}
349349
}
350350

351-
impl Reset for $fix_state {
351+
impl Reset for $fix_state {
352352
fn reset(&mut self) {
353353
self.state.reset()
354354
}
@@ -381,7 +381,7 @@ macro_rules! blake2_impl {
381381
<Self as Reset>::reset(self)
382382
}
383383

384-
fn finalize(self) -> crypto_mac::Output<Self> {
384+
fn finalize(mut self) -> crypto_mac::Output<Self> {
385385
crypto_mac::Output::new(self.state.finalize_with_flag(0))
386386
}
387387
}

gost94/src/gost94.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use block_buffer::block_padding::ZeroPadding;
44
use block_buffer::byteorder::{ByteOrder, LE};
55
use block_buffer::BlockBuffer;
6-
use digest::generic_array::typenum::U32;
7-
use digest::generic_array::GenericArray;
86
use digest::impl_write;
9-
use digest::{BlockInput, FixedOutput, Reset, Update};
7+
use digest::{consts::U32, generic_array::GenericArray};
8+
use digest::{BlockInput, FixedOutputDirty, Reset, Update};
109

1110
pub(crate) type Block = [u8; 32];
1211

@@ -238,31 +237,30 @@ impl Update for Gost94 {
238237
}
239238
}
240239

241-
impl FixedOutput for Gost94 {
240+
impl FixedOutputDirty for Gost94 {
242241
type OutputSize = U32;
243242

244-
fn finalize_fixed(mut self) -> GenericArray<u8, U32> {
245-
{
246-
let self_state = &mut self.state;
243+
fn finalize_into_dirty(&mut self, out: &mut GenericArray<u8, U32>) {
244+
let self_state = &mut self.state;
247245

248-
if self.buffer.position() != 0 {
249-
let block = self
250-
.buffer
251-
.pad_with::<ZeroPadding>()
252-
.expect("we never use input_lazy");
253-
self_state.process_block(block);
254-
}
246+
if self.buffer.position() != 0 {
247+
let block = self
248+
.buffer
249+
.pad_with::<ZeroPadding>()
250+
.expect("we never use input_lazy");
255251

256-
let mut buf = Block::default();
252+
self_state.process_block(block);
253+
}
257254

258-
LE::write_u64_into(&self_state.n, &mut buf);
259-
self_state.f(&buf);
255+
let mut buf = Block::default();
260256

261-
LE::write_u64_into(&self_state.sigma, &mut buf);
262-
self_state.f(&buf);
263-
}
257+
LE::write_u64_into(&self_state.n, &mut buf);
258+
self_state.f(&buf);
259+
260+
LE::write_u64_into(&self_state.sigma, &mut buf);
261+
self_state.f(&buf);
264262

265-
GenericArray::clone_from_slice(&self.state.h)
263+
out.copy_from_slice(&self.state.h);
266264
}
267265
}
268266

gost94/src/macros.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
macro_rules! gost94_impl {
22
($state:ident, $sbox:expr) => {
3-
use digest::generic_array::typenum::U32;
4-
use digest::generic_array::GenericArray;
53
use digest::impl_write;
6-
use digest::{BlockInput, FixedOutput, Reset, Update};
4+
use digest::{consts::U32, generic_array::GenericArray};
5+
use digest::{BlockInput, FixedOutputDirty, Reset, Update};
76
use $crate::gost94::{Block, Gost94, SBox};
87

98
/// GOST94 state
@@ -31,11 +30,11 @@ macro_rules! gost94_impl {
3130
}
3231
}
3332

34-
impl FixedOutput for $state {
33+
impl FixedOutputDirty for $state {
3534
type OutputSize = U32;
3635

37-
fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize> {
38-
self.sh.finalize_fixed()
36+
fn finalize_into_dirty(&mut self, out: &mut GenericArray<u8, U32>) {
37+
self.sh.finalize_into_dirty(out)
3938
}
4039
}
4140

groestl/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ extern crate std;
4545

4646
pub use digest::{self, Digest};
4747

48-
use digest::generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64};
49-
use digest::generic_array::GenericArray;
50-
use digest::impl_write;
51-
use digest::InvalidOutputSize;
52-
use digest::{BlockInput, FixedOutput, Reset, Update, VariableOutput};
53-
5448
mod consts;
5549
mod groestl;
5650
mod matrix;
@@ -59,6 +53,10 @@ mod state;
5953
mod macros;
6054

6155
use crate::groestl::Groestl;
56+
use digest::consts::{U128, U28, U32, U48, U64};
57+
use digest::generic_array::typenum::Unsigned;
58+
use digest::impl_write;
59+
use digest::{BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty};
6260

6361
impl_groestl!(Groestl512, U64, U128);
6462
impl_groestl!(Groestl384, U48, U128);

groestl/src/macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ macro_rules! impl_groestl {
2424
}
2525
}
2626

27-
impl FixedOutput for $state {
27+
impl FixedOutputDirty for $state {
2828
type OutputSize = $output;
2929

30-
fn finalize_fixed(mut self) -> GenericArray<u8, Self::OutputSize> {
30+
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
3131
let block = self.groestl.finalize();
3232
let n = block.len() - Self::OutputSize::to_usize();
33-
GenericArray::clone_from_slice(&block[n..])
33+
out.copy_from_slice(&block[n..])
3434
}
3535
}
3636

@@ -62,7 +62,7 @@ macro_rules! impl_variable_groestl {
6262
}
6363
}
6464

65-
impl VariableOutput for $state {
65+
impl VariableOutputDirty for $state {
6666
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
6767
if output_size == $min || output_size > $max {
6868
return Err(InvalidOutputSize);
@@ -76,7 +76,7 @@ macro_rules! impl_variable_groestl {
7676
self.groestl.output_size
7777
}
7878

79-
fn finalize_variable<F: FnOnce(&[u8])>(mut self, f: F) {
79+
fn finalize_variable_dirty(&mut self, f: impl FnOnce(&[u8])) {
8080
let block = self.groestl.finalize();
8181
let n = block.len() - self.groestl.output_size;
8282
f(&block[n..]);

k12/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ categories = ["cryptography", "no-std"]
1515
digest = { version = "= 0.9.0-pre", features = ["alloc"] }
1616

1717
[dev-dependencies]
18-
digest = { version = "= 0.9.0-pre", features = ["dev"] }
18+
digest = { version = "= 0.9.0-pre", features = ["alloc", "dev"] }
1919
hex-literal = "0.2"
2020

2121
[features]

k12/src/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
#![no_std]
1212
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
13-
#![deny(unsafe_code)]
13+
#![forbid(unsafe_code)]
1414
#![warn(missing_docs, rust_2018_idioms)]
1515

1616
// TODO(tarcieri): eliminate alloc requirement
17+
#[macro_use]
1718
extern crate alloc;
1819

1920
pub use digest;
@@ -23,8 +24,8 @@ mod lanes;
2324

2425
// TODO(tarcieri): eliminate usage of `Vec`
2526
use alloc::vec::Vec;
26-
use core::{cmp::min, convert::TryInto};
27-
use digest::{ExtendableOutput, Update, XofReader};
27+
use core::{cmp::min, convert::TryInto, mem};
28+
use digest::{ExtendableOutputDirty, Reset, Update, XofReader};
2829

2930
/// The KangarooTwelve extendable-output function (XOF).
3031
#[derive(Debug, Default)]
@@ -60,18 +61,30 @@ impl Update for KangarooTwelve {
6061
}
6162
}
6263

63-
impl ExtendableOutput for KangarooTwelve {
64+
impl ExtendableOutputDirty for KangarooTwelve {
6465
type Reader = Reader;
6566

66-
fn finalize_xof(self) -> Self::Reader {
67+
fn finalize_xof_dirty(&mut self) -> Self::Reader {
68+
let mut buffer = vec![];
69+
let mut customization = vec![];
70+
71+
mem::swap(&mut self.buffer, &mut buffer);
72+
mem::swap(&mut self.customization, &mut customization);
73+
6774
Reader {
68-
buffer: self.buffer,
69-
customization: self.customization,
75+
buffer,
76+
customization,
7077
finished: false,
7178
}
7279
}
7380
}
7481

82+
impl Reset for KangarooTwelve {
83+
fn reset(&mut self) {
84+
self.buffer.clear();
85+
}
86+
}
87+
7588
/// Extensible output reader.
7689
///
7790
/// NOTE: this presently only supports one invocation and will *panic* if

k12/tests/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use k12::{
44
KangarooTwelve,
55
};
66

7-
fn read_bytes<T: AsRef<[u8]>>(s: T) -> Vec<u8> {
7+
fn read_bytes<T: AsRef<[u8]>>(s: T) -> Box<[u8]> {
88
fn b(c: u8) -> u8 {
99
match c {
1010
b'0'..=b'9' => c - b'0',
@@ -13,9 +13,11 @@ fn read_bytes<T: AsRef<[u8]>>(s: T) -> Vec<u8> {
1313
_ => unreachable!(),
1414
}
1515
}
16+
1617
let s = s.as_ref();
1718
let mut i = 0;
1819
let mut v = Vec::new();
20+
1921
while i < s.len() {
2022
if s[i] == b' ' || s[i] == b'\n' {
2123
i += 1;
@@ -26,22 +28,23 @@ fn read_bytes<T: AsRef<[u8]>>(s: T) -> Vec<u8> {
2628
v.push(n);
2729
i += 2;
2830
}
29-
v
31+
32+
v.into_boxed_slice()
3033
}
3134

3235
#[test]
3336
fn empty() {
3437
// Source: reference paper
3538
assert_eq!(
36-
KangarooTwelve::new().chain(b"").finalize_vec(32),
39+
KangarooTwelve::new().chain(b"").finalize_boxed(32),
3740
read_bytes(
3841
"1a c2 d4 50 fc 3b 42 05 d1 9d a7 bf ca
3942
1b 37 51 3c 08 03 57 7a c7 16 7f 06 fe 2c e1 f0 ef 39 e5"
4043
)
4144
);
4245

4346
assert_eq!(
44-
KangarooTwelve::new().chain(b"").finalize_vec(64),
47+
KangarooTwelve::new().chain(b"").finalize_boxed(64),
4548
read_bytes(
4649
"1a c2 d4 50 fc 3b 42 05 d1 9d a7 bf ca
4750
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() {
5053
);
5154

5255
assert_eq!(
53-
KangarooTwelve::new().chain(b"").finalize_vec(10032)[10000..],
56+
KangarooTwelve::new().chain(b"").finalize_boxed(10032)[10000..],
5457
read_bytes(
5558
"e8 dc 56 36 42 f7 22 8c 84
5659
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() {
8184
{
8285
let len = 17usize.pow(i);
8386
let m: Vec<u8> = (0..len).map(|j| (j % 251) as u8).collect();
84-
let result = KangarooTwelve::new().chain(&m).finalize_vec(32);
87+
let result = KangarooTwelve::new().chain(&m).finalize_boxed(32);
8588
assert_eq!(result, read_bytes(expected[i as usize]));
8689
}
8790
}
@@ -104,7 +107,7 @@ fn pat_c() {
104107
let c: Vec<u8> = (0..len).map(|j| (j % 251) as u8).collect();
105108
let result = KangarooTwelve::new_with_customization(c)
106109
.chain(&m)
107-
.finalize_vec(32);
110+
.finalize_boxed(32);
108111
assert_eq!(result, read_bytes(expected[i as usize]));
109112
}
110113
}

0 commit comments

Comments
 (0)