#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![warn(missing_docs, missing_debug_implementations)]
pub use digest::{
self,
CollisionResistance,
CustomizedInit,
Digest,
ExtendableOutput,
ExtendableOutputReset,
Update,
XofReader,
};
pub mod block_core;
pub mod cshake;
pub mod turbo_shake;
use block_core::{
SpongeHasherCore,
SpongeReaderCore,
};
#[doc(inline)]
pub use cshake::{
CShake128,
CShake128Reader,
CShake256,
CShake256Reader,
};
use digest::consts::{
U0,
U16,
U28,
U32,
U48,
U64,
U72,
U104,
U136,
U144,
U168,
};
#[doc(inline)]
pub use turbo_shake::{
TurboShake128,
TurboShake128Reader,
TurboShake256,
TurboShake256Reader,
};
#[inline(always)]
pub fn sha3_256(data: &[u8]) -> [u8; 32] {
let mut hasher = Sha3_256::new();
Digest::update(&mut hasher, data);
hasher.finalize().into()
}
const SHA3_PAD: u8 = 0x06;
const SHAKE_PAD: u8 = 0x1F;
const CSHAKE_PAD: u8 = 0x04;
const PLEN: usize = 25;
const DEFAULT_ROUND_COUNT: usize = 24;
pub(crate) fn xor_block(state: &mut [u64; PLEN], block: &[u8]) {
assert!(block.len() < 8 * PLEN);
let mut chunks = block.chunks_exact(8);
for (s, chunk) in state.iter_mut().zip(&mut chunks) {
let mut lane = [0u8; 8];
lane.copy_from_slice(chunk);
*s ^= u64::from_le_bytes(lane);
}
let rem = chunks.remainder();
if !rem.is_empty() {
let mut buf = [0u8; 8];
buf[..rem.len()].copy_from_slice(rem);
let n = block.len() / 8;
state[n] ^= u64::from_le_bytes(buf);
}
}
digest::buffer_fixed!(
pub struct Sha3_224(SpongeHasherCore<U144, U28, SHA3_PAD>);
oid: "2.16.840.1.101.3.4.2.7";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
pub struct Sha3_256(SpongeHasherCore<U136, U32, SHA3_PAD>);
oid: "2.16.840.1.101.3.4.2.8";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
pub struct Sha3_384(SpongeHasherCore<U104, U48, SHA3_PAD>);
oid: "2.16.840.1.101.3.4.2.9";
impl: FixedHashTraits;
);
digest::buffer_fixed!(
pub struct Sha3_512(SpongeHasherCore<U72, U64, SHA3_PAD>);
oid: "2.16.840.1.101.3.4.2.10";
impl: FixedHashTraits;
);
digest::buffer_xof!(
pub struct Shake128(SpongeHasherCore<U168, U0, SHAKE_PAD>);
oid: "2.16.840.1.101.3.4.2.11";
impl: XofHasherTraits;
pub struct Shake128Reader(SpongeReaderCore<U168>);
impl: XofReaderTraits;
);
digest::buffer_xof!(
pub struct Shake256(SpongeHasherCore<U136, U0, SHAKE_PAD>);
oid: "2.16.840.1.101.3.4.2.12";
impl: XofHasherTraits;
pub struct Shake256Reader(SpongeReaderCore<U136>);
impl: XofReaderTraits;
);
impl CollisionResistance for Shake128 {
type CollisionResistance = U16;
}
impl CollisionResistance for Shake256 {
type CollisionResistance = U32;
}