1#![doc(html_root_url = "https://danipopes.github.io/xkcp-rs")]
6#![warn(
7 missing_debug_implementations,
8 missing_docs,
9 rust_2018_idioms,
10 unreachable_pub,
11 rustdoc::all
12)]
13#![cfg_attr(not(test), warn(unused_crate_dependencies))]
14#![cfg_attr(not(feature = "std"), no_std)]
15#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
16
17pub extern crate xkcp_sys as ffi;
18
19mod error;
20mod keccak;
21
22pub use error::{Error, Result};
23pub use keccak::KeccakHash;
24
25#[inline]
27pub fn keccak_sponge(
28 rate: u32,
29 capacity: u32,
30 suffix: u8,
31 input: &[u8],
32 output: &mut [u8],
33) -> Result<()> {
34 Error::from_int(unsafe {
35 ffi::KeccakWidth1600_Sponge(
36 rate,
37 capacity,
38 input.as_ptr(),
39 input.len(),
40 suffix,
41 output.as_mut_ptr(),
42 output.len(),
43 )
44 })
45}
46
47#[inline]
49pub fn shake128(input: &[u8], output: &mut [u8]) -> Result<()> {
50 keccak_sponge(1344, 256, 0x1F, input, output)
51}
52
53#[inline]
55pub fn shake256(input: &[u8], output: &mut [u8]) -> Result<()> {
56 keccak_sponge(1088, 512, 0x1F, input, output)
57}
58
59#[inline]
61#[cfg_attr(debug_assertions, track_caller)]
62pub fn sha3_224(input: &[u8], output: &mut [u8; 28]) {
63 let result = keccak_sponge(1152, 448, 0x06, input, output);
64 debug_assert_eq!(result, Ok(()));
65}
66
67#[inline]
69#[cfg_attr(debug_assertions, track_caller)]
70pub fn sha3_256(input: &[u8], output: &mut [u8; 32]) {
71 let result = keccak_sponge(1088, 512, 0x06, input, output);
72 debug_assert_eq!(result, Ok(()));
73}
74
75#[inline]
77#[cfg_attr(debug_assertions, track_caller)]
78pub fn sha3_384(input: &[u8], output: &mut [u8; 48]) {
79 let result = keccak_sponge(832, 768, 0x06, input, output);
80 debug_assert_eq!(result, Ok(()));
81}
82
83#[inline]
85#[cfg_attr(debug_assertions, track_caller)]
86pub fn sha3_512(input: &[u8], output: &mut [u8; 64]) {
87 let result = keccak_sponge(576, 1024, 0x06, input, output);
88 debug_assert_eq!(result, Ok(()));
89}
90
91#[inline]
93#[cfg_attr(debug_assertions, track_caller)]
94pub fn keccak224(input: &[u8], output: &mut [u8; 28]) {
95 let result = keccak_sponge(1152, 448, 0x01, input, output);
96 debug_assert_eq!(result, Ok(()));
97}
98
99#[inline]
101#[cfg_attr(debug_assertions, track_caller)]
102pub fn keccak256(input: &[u8], output: &mut [u8; 32]) {
103 let result = keccak_sponge(1088, 512, 0x01, input, output);
104 debug_assert_eq!(result, Ok(()));
105}
106
107#[inline]
109#[cfg_attr(debug_assertions, track_caller)]
110pub fn keccak384(input: &[u8], output: &mut [u8; 48]) {
111 let result = keccak_sponge(832, 768, 0x01, input, output);
112 debug_assert_eq!(result, Ok(()));
113}
114
115#[inline]
117#[cfg_attr(debug_assertions, track_caller)]
118pub fn keccak512(input: &[u8], output: &mut [u8; 64]) {
119 let result = keccak_sponge(576, 1024, 0x01, input, output);
120 debug_assert_eq!(result, Ok(()));
121}