Skip to content

Commit cbd6849

Browse files
authored
Sha3 refactor unsafe (RustCrypto#338)
1 parent 894f62f commit cbd6849

File tree

3 files changed

+9
-41
lines changed

3 files changed

+9
-41
lines changed

sha3/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
6363
html_root_url = "https://docs.rs/sha3/0.10.0"
6464
)]
65-
#![deny(unsafe_code)]
65+
#![forbid(unsafe_code)]
6666
#![warn(missing_docs, rust_2018_idioms)]
6767

6868
pub use digest::{self, Digest};

sha3/src/macros.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ macro_rules! impl_sha3 {
4646

4747
self.state.absorb_block(block);
4848

49-
let n = out.len();
50-
self.state.as_bytes(|state| {
51-
out.copy_from_slice(&state[..n]);
52-
});
49+
self.state.as_bytes(out);
5350
}
5451
}
5552

@@ -183,10 +180,7 @@ macro_rules! impl_shake {
183180
#[inline]
184181
fn read_block(&mut self) -> Block<Self> {
185182
let mut block = Block::<Self>::default();
186-
let n = block.len();
187-
self.state.as_bytes(|state| {
188-
block.copy_from_slice(&state[..n]);
189-
});
183+
self.state.as_bytes(&mut block);
190184
self.state.apply_f();
191185
block
192186
}

sha3/src/state.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,18 @@ impl Sha3State {
1212
pub(crate) fn absorb_block(&mut self, block: &[u8]) {
1313
debug_assert_eq!(block.len() % 8, 0);
1414

15-
if cfg!(target_endian = "little") {
16-
#[allow(unsafe_code)]
17-
let state = unsafe { &mut *(self.state.as_mut_ptr() as *mut [u8; 8 * PLEN]) };
18-
for (d, i) in state.iter_mut().zip(block) {
19-
*d ^= *i;
20-
}
21-
} else if cfg!(target_endian = "big") {
22-
let n = block.len() / 8;
23-
let mut buf = [0u64; 21];
24-
let buf = &mut buf[..n];
25-
for (o, chunk) in buf.iter_mut().zip(block.chunks_exact(8)) {
26-
*o = u64::from_le_bytes(chunk.try_into().unwrap());
27-
}
28-
for (d, i) in self.state[..n].iter_mut().zip(buf) {
29-
*d ^= *i;
30-
}
15+
for (b, s) in block.chunks_exact(8).zip(self.state.iter_mut()) {
16+
*s ^= u64::from_le_bytes(b.try_into().unwrap());
3117
}
3218

3319
keccak::f1600(&mut self.state);
3420
}
3521

3622
#[inline(always)]
37-
pub(crate) fn as_bytes<F: FnOnce(&[u8; 8 * PLEN])>(&self, f: F) {
38-
let mut data_copy;
39-
let data_ref: &[u8; 8 * PLEN] = if cfg!(target_endian = "little") {
40-
#[allow(unsafe_code)]
41-
unsafe {
42-
&*(self.state.as_ptr() as *const [u8; 8 * PLEN])
43-
}
44-
} else {
45-
data_copy = [0u8; 8 * PLEN];
46-
47-
for (chunk, v) in data_copy.chunks_exact_mut(8).zip(self.state.iter()) {
48-
chunk.copy_from_slice(&v.to_le_bytes());
49-
}
50-
&data_copy
51-
};
52-
f(data_ref);
23+
pub(crate) fn as_bytes(&self, out: &mut [u8]) {
24+
for (o, s) in out.chunks_mut(8).zip(self.state.iter()) {
25+
o.copy_from_slice(&s.to_le_bytes()[..o.len()]);
26+
}
5327
}
5428

5529
#[inline(always)]

0 commit comments

Comments
 (0)