Skip to content

Commit

Permalink
Sha3 refactor unsafe (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
aewag authored Dec 15, 2021
1 parent 894f62f commit cbd6849
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
2 changes: 1 addition & 1 deletion sha3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_root_url = "https://docs.rs/sha3/0.10.0"
)]
#![deny(unsafe_code)]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

pub use digest::{self, Digest};
Expand Down
10 changes: 2 additions & 8 deletions sha3/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ macro_rules! impl_sha3 {

self.state.absorb_block(block);

let n = out.len();
self.state.as_bytes(|state| {
out.copy_from_slice(&state[..n]);
});
self.state.as_bytes(out);
}
}

Expand Down Expand Up @@ -183,10 +180,7 @@ macro_rules! impl_shake {
#[inline]
fn read_block(&mut self) -> Block<Self> {
let mut block = Block::<Self>::default();
let n = block.len();
self.state.as_bytes(|state| {
block.copy_from_slice(&state[..n]);
});
self.state.as_bytes(&mut block);
self.state.apply_f();
block
}
Expand Down
38 changes: 6 additions & 32 deletions sha3/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,18 @@ impl Sha3State {
pub(crate) fn absorb_block(&mut self, block: &[u8]) {
debug_assert_eq!(block.len() % 8, 0);

if cfg!(target_endian = "little") {
#[allow(unsafe_code)]
let state = unsafe { &mut *(self.state.as_mut_ptr() as *mut [u8; 8 * PLEN]) };
for (d, i) in state.iter_mut().zip(block) {
*d ^= *i;
}
} else if cfg!(target_endian = "big") {
let n = block.len() / 8;
let mut buf = [0u64; 21];
let buf = &mut buf[..n];
for (o, chunk) in buf.iter_mut().zip(block.chunks_exact(8)) {
*o = u64::from_le_bytes(chunk.try_into().unwrap());
}
for (d, i) in self.state[..n].iter_mut().zip(buf) {
*d ^= *i;
}
for (b, s) in block.chunks_exact(8).zip(self.state.iter_mut()) {
*s ^= u64::from_le_bytes(b.try_into().unwrap());
}

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

#[inline(always)]
pub(crate) fn as_bytes<F: FnOnce(&[u8; 8 * PLEN])>(&self, f: F) {
let mut data_copy;
let data_ref: &[u8; 8 * PLEN] = if cfg!(target_endian = "little") {
#[allow(unsafe_code)]
unsafe {
&*(self.state.as_ptr() as *const [u8; 8 * PLEN])
}
} else {
data_copy = [0u8; 8 * PLEN];

for (chunk, v) in data_copy.chunks_exact_mut(8).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
&data_copy
};
f(data_ref);
pub(crate) fn as_bytes(&self, out: &mut [u8]) {
for (o, s) in out.chunks_mut(8).zip(self.state.iter()) {
o.copy_from_slice(&s.to_le_bytes()[..o.len()]);
}
}

#[inline(always)]
Expand Down

0 comments on commit cbd6849

Please sign in to comment.