Skip to content

Commit 7c26608

Browse files
committed
fix skein
1 parent 696ee27 commit 7c26608

File tree

4 files changed

+137
-39
lines changed

4 files changed

+137
-39
lines changed

skein/src/block_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ macro_rules! define_hasher {
107107
block[..8].copy_from_slice(&(i as u64).to_le_bytes());
108108
Self::process_block(&mut ctr, &block, 8);
109109

110-
for (src, dst) in ctr.x.iter().zip(chunk.chunks_exact_mut(8)) {
111-
dst.copy_from_slice(&src.to_le_bytes());
110+
for (src, dst) in ctr.x.iter().zip(chunk.chunks_mut(8)) {
111+
dst.copy_from_slice(&src.to_le_bytes()[..dst.len()]);
112112
}
113113
}
114114
}

skein/src/lib.rs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,22 @@ pub use digest::{
1414
};
1515

1616
mod block_api;
17+
mod newtype;
1718
pub use block_api::{Skein256Core, Skein512Core, Skein1024Core};
1819

19-
use digest::core_api::CoreWrapper;
20+
pub use newtype::{Skein256, Skein512, Skein1024};
2021

21-
digest::newtype!(
22-
/// Skein-256-256 hasher.
23-
pub struct Skein256_256(CoreWrapper<Skein256Core<U32>>);
24-
delegate_template: FixedOutputHash
25-
);
26-
digest::newtype!(
27-
/// Skein-256-512 hasher.
28-
pub struct Skein256_512(CoreWrapper<Skein256Core<U64>>);
29-
delegate_template: FixedOutputHash
30-
);
31-
digest::newtype!(
32-
/// Skein-512-256 hasher.
33-
pub struct Skein512_256(CoreWrapper<Skein512Core<U64>>);
34-
delegate_template: FixedOutputHash
35-
);
36-
digest::newtype!(
37-
/// Skein-512-512 hasher.
38-
pub struct Skein512_512(CoreWrapper<Skein512Core<U64>>);
39-
delegate_template: FixedOutputHash
40-
);
41-
digest::newtype!(
42-
/// Skein-1024-256 hasher.
43-
pub struct Skein1024_256(CoreWrapper<Skein1024Core<U32>>);
44-
delegate_template: FixedOutputHash
45-
);
46-
digest::newtype!(
47-
/// Skein-1024-512 hasher.
48-
pub struct Skein1024_512(CoreWrapper<Skein1024Core<U64>>);
49-
delegate_template: FixedOutputHash
50-
);
51-
digest::newtype!(
52-
/// Skein-1024-1024 hasher.
53-
pub struct Skein1024_1024(CoreWrapper<Skein1024Core<U128>>);
54-
delegate_template: FixedOutputHash
55-
);
22+
/// Skein-256-256 hasher.
23+
pub type Skein256_256 = Skein256<U32>;
24+
/// Skein-256-512 hasher.
25+
pub type Skein256_512 = Skein256<U64>;
26+
/// Skein-512-256 hasher.
27+
pub type Skein512_256 = Skein512<U32>;
28+
/// Skein-512-512 hasher.
29+
pub type Skein512_512 = Skein512<U64>;
30+
/// Skein-1024-256 hasher.
31+
pub type Skein1024_256 = Skein1024<U32>;
32+
/// Skein-1024-512 hasher.
33+
pub type Skein1024_512 = Skein1024<U64>;
34+
/// Skein-1024-1024 hasher.
35+
pub type Skein1024_1024 = Skein1024<U128>;

skein/src/newtype.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use crate::{Skein256Core, Skein512Core, Skein1024Core};
2+
use core::fmt;
3+
use digest::{
4+
FixedOutput, FixedOutputReset, HashMarker, Output, OutputSizeUser, Reset, Update,
5+
array::ArraySize,
6+
core_api::{AlgorithmName, BlockSizeUser, CoreWrapper},
7+
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
8+
};
9+
10+
macro_rules! newtype {
11+
($name:ident<$n:ident>, $inner:ty, $alg_name:literal) => {
12+
#[doc = $alg_name]
13+
#[doc = " hasher generic over output size"]
14+
pub struct $name<$n: ArraySize>(CoreWrapper<$inner>);
15+
16+
impl<$n: ArraySize> fmt::Debug for $name<$n> {
17+
#[inline]
18+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19+
write!(f, "{}<{}> {{ ... }}", stringify!($name), N::USIZE)
20+
}
21+
}
22+
23+
impl<$n: ArraySize> AlgorithmName for $name<$n> {
24+
#[inline]
25+
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
26+
write!(f, "{}-{}", $alg_name, N::USIZE)
27+
}
28+
}
29+
30+
impl<$n: ArraySize> Clone for $name<$n> {
31+
#[inline]
32+
fn clone(&self) -> Self {
33+
Self(self.0.clone())
34+
}
35+
}
36+
37+
impl<$n: ArraySize> Default for $name<$n> {
38+
#[inline]
39+
fn default() -> Self {
40+
Self(Default::default())
41+
}
42+
}
43+
44+
impl<$n: ArraySize> Reset for $name<$n> {
45+
#[inline]
46+
fn reset(&mut self) {
47+
Reset::reset(&mut self.0);
48+
}
49+
}
50+
51+
impl<$n: ArraySize> Update for $name<$n> {
52+
#[inline]
53+
fn update(&mut self, data: &[u8]) {
54+
Update::update(&mut self.0, data);
55+
}
56+
}
57+
58+
impl<$n: ArraySize> FixedOutput for $name<$n> {
59+
#[inline]
60+
fn finalize_into(self, out: &mut Output<Self>) {
61+
FixedOutput::finalize_into(self.0, out);
62+
}
63+
}
64+
65+
impl<$n: ArraySize> FixedOutputReset for $name<$n> {
66+
#[inline]
67+
fn finalize_into_reset(&mut self, out: &mut Output<Self>) {
68+
FixedOutputReset::finalize_into_reset(&mut self.0, out);
69+
}
70+
}
71+
72+
impl<$n: ArraySize> HashMarker for $name<$n> {}
73+
74+
impl<$n: ArraySize> BlockSizeUser for $name<$n> {
75+
type BlockSize = <$inner as BlockSizeUser>::BlockSize;
76+
}
77+
78+
impl<$n: ArraySize> OutputSizeUser for $name<$n> {
79+
type OutputSize = <$inner as OutputSizeUser>::OutputSize;
80+
}
81+
82+
impl<$n: ArraySize> SerializableState for $name<$n> {
83+
type SerializedStateSize =
84+
<CoreWrapper<$inner> as SerializableState>::SerializedStateSize;
85+
86+
#[inline]
87+
fn serialize(&self) -> SerializedState<Self> {
88+
self.0.serialize()
89+
}
90+
91+
#[inline]
92+
fn deserialize(
93+
serialized_state: &SerializedState<Self>,
94+
) -> Result<Self, DeserializeStateError> {
95+
SerializableState::deserialize(serialized_state).map(Self)
96+
}
97+
}
98+
};
99+
}
100+
101+
newtype!(Skein256<N>, Skein256Core<N>, "Skein-256");
102+
newtype!(Skein512<N>, Skein512Core<N>, "Skein-512");
103+
newtype!(Skein1024<N>, Skein1024Core<N>, "Skein-1024");

skein/tests/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ new_test!(
1414
);
1515
new_test!(
1616
skein512_256,
17-
"skein512_512",
17+
"skein512_256",
1818
skein::Skein512_256,
1919
fixed_test,
2020
);
@@ -42,3 +42,18 @@ new_test!(
4242
skein::Skein1024_1024,
4343
fixed_test,
4444
);
45+
46+
/// Regression tests for https://github.com/RustCrypto/hashes/issues/681
47+
#[test]
48+
fn skein_uncommon_sizes() {
49+
use digest::{Digest, consts::U7};
50+
use hex_literal::hex;
51+
52+
let s = "hello world";
53+
let h = skein::Skein256::<U7>::digest(s);
54+
assert_eq!(h[..], hex!("31bffb70f5dafe")[..]);
55+
let h = skein::Skein512::<U7>::digest(s);
56+
assert_eq!(h[..], hex!("ee6004efedd69c")[..]);
57+
let h = skein::Skein1024::<U7>::digest(s);
58+
assert_eq!(h[..], hex!("a2808b638681c6")[..]);
59+
}

0 commit comments

Comments
 (0)