Skip to content

Commit 8f5155c

Browse files
authored
sha3: use KeccakP1600 struct for state (#790)
Added in RustCrypto/sponges#107 This struct can automatially make use of CPU intrinsics when they are available. Currently only supports ARMv8's `FEAT_SHA3` intrinsics.
1 parent 71113e2 commit 8f5155c

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ opt-level = 2
3434
sha1 = { path = "sha1" }
3535
sha3 = { path = "sha3" }
3636
whirlpool = { path = "whirlpool" }
37+
38+
keccak = { git = "https://github.com/RustCrypto/sponges" }

sha3/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ default = ["alloc", "oid"]
2929
alloc = ["digest/alloc"]
3030
oid = ["digest/oid"] # Enable OID support.
3131
zeroize = ["digest/zeroize"]
32-
asm = ["keccak/asm"] # Enable ASM (currently ARMv8 only).
3332

3433
[package.metadata.docs.rs]
3534
all-features = true

sha3/src/block_api.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use digest::{
1313
},
1414
typenum::{IsLessOrEqual, True, U0, U200},
1515
};
16+
use keccak::KeccakP1600;
1617

1718
pub use crate::cshake::{CShake128Core, CShake256Core};
1819

@@ -27,7 +28,7 @@ pub struct Sha3HasherCore<
2728
Rate: BlockSizes + IsLessOrEqual<U200, Output = True>,
2829
OutputSize: ArraySize + IsLessOrEqual<U200, Output = True>,
2930
{
30-
state: [u64; PLEN],
31+
state: KeccakP1600,
3132
_pd: PhantomData<(Rate, OutputSize)>,
3233
}
3334

@@ -75,8 +76,8 @@ where
7576
#[inline]
7677
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
7778
for block in blocks {
78-
xor_block(&mut self.state, block);
79-
keccak::p1600(&mut self.state, ROUNDS);
79+
xor_block(self.state.as_mut(), block);
80+
self.state.p1600(ROUNDS);
8081
}
8182
}
8283
}
@@ -95,10 +96,10 @@ where
9596
let n = block.len();
9697
block[n - 1] |= 0x80;
9798

98-
xor_block(&mut self.state, &block);
99-
keccak::p1600(&mut self.state, ROUNDS);
99+
xor_block(self.state.as_mut(), &block);
100+
self.state.p1600(ROUNDS);
100101

101-
for (o, s) in out.chunks_mut(8).zip(self.state.iter()) {
102+
for (o, s) in out.chunks_mut(8).zip(self.state.as_mut().iter()) {
102103
o.copy_from_slice(&s.to_le_bytes()[..o.len()]);
103104
}
104105
}
@@ -119,10 +120,10 @@ where
119120
let n = block.len();
120121
block[n - 1] |= 0x80;
121122

122-
xor_block(&mut self.state, &block);
123-
keccak::p1600(&mut self.state, ROUNDS);
123+
xor_block(self.state.as_mut(), &block);
124+
self.state.p1600(ROUNDS);
124125

125-
Sha3ReaderCore::new(&self.state)
126+
Sha3ReaderCore::new(self.state.as_ref())
126127
}
127128
}
128129

@@ -185,7 +186,7 @@ where
185186
#[cfg(feature = "zeroize")]
186187
{
187188
use digest::zeroize::Zeroize;
188-
self.state.zeroize();
189+
self.state.as_mut().zeroize();
189190
}
190191
}
191192
}
@@ -210,7 +211,7 @@ where
210211
fn serialize(&self) -> SerializedState<Self> {
211212
let mut serialized_state = SerializedState::<Self>::default();
212213
let chunks = serialized_state.chunks_exact_mut(8);
213-
for (val, chunk) in self.state.iter().zip(chunks) {
214+
for (val, chunk) in self.state.as_ref().iter().zip(chunks) {
214215
chunk.copy_from_slice(&val.to_le_bytes());
215216
}
216217

@@ -227,7 +228,7 @@ where
227228
}
228229

229230
Ok(Self {
230-
state,
231+
state: KeccakP1600::from(state),
231232
_pd: PhantomData,
232233
})
233234
}

0 commit comments

Comments
 (0)