Skip to main content

lib_q_keccak_digest/
lib.rs

1//! Pre–FIPS **Keccak** fixed digests: original Keccak padding, **not** FIPS 202 **SHA-3** (use [`lib_q_sha3`](https://docs.rs/lib-q-sha3) for `Sha3_256` and the SHA-3 family).
2//!
3//! # Types
4//!
5//! - [`Keccak224`] … [`Keccak512`], [`Keccak256Full`] (200-byte output).
6//! - One-shot: [`keccak_224`], [`keccak_256`], [`keccak_384`], [`keccak_512`], [`keccak_256_full`].
7//!
8//! # Security
9//!
10//! - [`Keccak256`](crate::Keccak256) and any [`lib_q_sha3::Sha3_256`](https://docs.rs/lib-q-sha3) produce **different** digests for the same input (different padding).
11//! - Do not substitute this crate for SHA-3 in a protocol without explicit specification.
12//!
13//! See the crate **README** for links, architecture ADR, and follow-up dependency extraction ADR.
14
15#![no_std]
16#![doc = include_str!("../README.md")]
17#![forbid(unsafe_code)]
18#![warn(missing_docs, missing_debug_implementations)]
19
20pub use digest::Digest;
21use digest::consts::{
22    U28,
23    U32,
24    U48,
25    U64,
26    U72,
27    U104,
28    U136,
29    U144,
30    U200,
31};
32use lib_q_sha3::block_api::{
33    KECCAK_DIGEST_PAD,
34    SpongeHasherCore,
35};
36
37digest::buffer_fixed!(
38    /// Non-standard 200-byte output Keccak-256 (e.g. CryptoNight-style). **Not** FIPS SHA3-256—see the crate README.
39    pub struct Keccak256Full(SpongeHasherCore<U136, U200, KECCAK_DIGEST_PAD>);
40    impl: FixedHashTraits;
41);
42digest::buffer_fixed!(
43    /// Keccak-224 (original Keccak padding, **not** SHA3-224).
44    pub struct Keccak224(SpongeHasherCore<U144, U28, KECCAK_DIGEST_PAD>);
45    impl: FixedHashTraits;
46);
47digest::buffer_fixed!(
48    /// Keccak-256 (original Keccak padding, **not** SHA3-256).
49    pub struct Keccak256(SpongeHasherCore<U136, U32, KECCAK_DIGEST_PAD>);
50    impl: FixedHashTraits;
51);
52digest::buffer_fixed!(
53    /// Keccak-384 (original Keccak padding, **not** SHA3-384).
54    pub struct Keccak384(SpongeHasherCore<U104, U48, KECCAK_DIGEST_PAD>);
55    impl: FixedHashTraits;
56);
57digest::buffer_fixed!(
58    /// Keccak-512 (original Keccak padding, **not** SHA3-512).
59    pub struct Keccak512(SpongeHasherCore<U72, U64, KECCAK_DIGEST_PAD>);
60    impl: FixedHashTraits;
61);
62
63mod one_shot;
64
65pub use one_shot::{
66    keccak_224,
67    keccak_256,
68    keccak_256_full,
69    keccak_384,
70    keccak_512,
71};
72
73#[cfg(test)]
74mod tests {
75    use digest::Digest;
76
77    use super::*;
78
79    #[test]
80    fn keccak256_and_sha3_256_differ_on_same_input() {
81        let d = b"lib-q keccak-digest != sha3";
82        let k = Keccak256::digest(d);
83        let s = lib_q_sha3::Sha3_256::digest(d);
84        assert_ne!(k.as_slice(), s.as_slice());
85    }
86}