[go: up one dir, main page]

sha3/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
5    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
6)]
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8#![forbid(unsafe_code)]
9#![warn(missing_docs)]
10
11pub use digest::{self, CustomizedInit, Digest};
12
13use core::fmt;
14use digest::{
15    HashMarker, Output,
16    array::typenum::Unsigned,
17    block_buffer::Eager,
18    consts::{U28, U32, U48, U64, U72, U104, U136, U144, U168, U200},
19    core_api::{
20        AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper,
21        ExtendableOutputCore, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, XofReaderCore,
22        XofReaderCoreWrapper,
23    },
24    crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
25};
26
27#[cfg(feature = "oid")]
28use digest::const_oid::{AssociatedOid, ObjectIdentifier};
29#[cfg(feature = "zeroize")]
30use digest::zeroize::{Zeroize, ZeroizeOnDrop};
31
32#[macro_use]
33mod macros;
34mod state;
35
36use crate::state::Sha3State;
37
38// Paddings
39const KECCAK: u8 = 0x01;
40const SHA3: u8 = 0x06;
41const SHAKE: u8 = 0x1f;
42const CSHAKE: u8 = 0x4;
43
44// Round counts
45const TURBO_SHAKE_ROUND_COUNT: usize = 12;
46
47impl_sha3!(Keccak224Core, Keccak224, U28, U144, KECCAK, "Keccak-224");
48impl_sha3!(Keccak256Core, Keccak256, U32, U136, KECCAK, "Keccak-256");
49impl_sha3!(Keccak384Core, Keccak384, U48, U104, KECCAK, "Keccak-384");
50impl_sha3!(Keccak512Core, Keccak512, U64, U72, KECCAK, "Keccak-512");
51
52impl_sha3!(
53    Keccak256FullCore,
54    Keccak256Full,
55    U200,
56    U136,
57    KECCAK,
58    "SHA-3 CryptoNight variant",
59);
60
61impl_sha3!(
62    Sha3_224Core,
63    Sha3_224,
64    U28,
65    U144,
66    SHA3,
67    "SHA-3-224",
68    "2.16.840.1.101.3.4.2.7",
69);
70impl_sha3!(
71    Sha3_256Core,
72    Sha3_256,
73    U32,
74    U136,
75    SHA3,
76    "SHA-3-256",
77    "2.16.840.1.101.3.4.2.8",
78);
79impl_sha3!(
80    Sha3_384Core,
81    Sha3_384,
82    U48,
83    U104,
84    SHA3,
85    "SHA-3-384",
86    "2.16.840.1.101.3.4.2.9",
87);
88impl_sha3!(
89    Sha3_512Core,
90    Sha3_512,
91    U64,
92    U72,
93    SHA3,
94    "SHA-3-512",
95    "2.16.840.1.101.3.4.2.10",
96);
97
98impl_shake!(
99    Shake128Core,
100    Shake128,
101    Shake128ReaderCore,
102    Shake128Reader,
103    U168,
104    SHAKE,
105    "SHAKE128",
106    "2.16.840.1.101.3.4.2.11",
107);
108impl_shake!(
109    Shake256Core,
110    Shake256,
111    Shake256ReaderCore,
112    Shake256Reader,
113    U136,
114    SHAKE,
115    "SHAKE256",
116    "2.16.840.1.101.3.4.2.11",
117);
118
119impl_turbo_shake!(
120    TurboShake128Core,
121    TurboShake128,
122    TurboShake128ReaderCore,
123    TurboShake128Reader,
124    U168,
125    "TurboSHAKE128",
126);
127impl_turbo_shake!(
128    TurboShake256Core,
129    TurboShake256,
130    TurboShake256ReaderCore,
131    TurboShake256Reader,
132    U136,
133    "TurboSHAKE256",
134);
135
136impl_cshake!(
137    CShake128Core,
138    CShake128,
139    CShake128ReaderCore,
140    CShake128Reader,
141    U168,
142    SHAKE,
143    CSHAKE,
144    "CSHAKE128",
145);
146impl_cshake!(
147    CShake256Core,
148    CShake256,
149    CShake256ReaderCore,
150    CShake256Reader,
151    U136,
152    SHAKE,
153    CSHAKE,
154    "CSHAKE256",
155);
156
157#[inline(always)]
158pub(crate) fn left_encode(val: u64, b: &mut [u8; 9]) -> &[u8] {
159    b[1..].copy_from_slice(&val.to_be_bytes());
160    let i = b[1..8].iter().take_while(|&&a| a == 0).count();
161    b[i] = (8 - i) as u8;
162    &b[i..]
163}