sha3_asm/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![warn(missing_docs, rust_2018_idioms)]
4#![allow(rustdoc::broken_intra_doc_links)]
5
6/// SHA-3 state buffer.
7pub type Buffer = [u64; 25];
8
9// Derived from OpenSSL:
10// https://github.com/openssl/openssl/blob/60421893a286bb9eb7fb7c2454b84af9778ffca4/crypto/sha/keccak1600.c#L14-L17
11#[link(name = "keccak", kind = "static")]
12extern "C" {
13    /// SHA-3 absorb, defined in assembly.
14    ///
15    /// `r` is the rate (block size) of the function in bytes.
16    ///
17    /// C signature:
18    ///
19    /// ```c
20    /// size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
21    ///                    size_t r);
22    /// ```
23    #[link_name = "KECCAK_ASM_SHA3_absorb"]
24    pub fn SHA3_absorb(a: *mut Buffer, inp: *const u8, len: usize, r: usize) -> usize;
25
26    /// SHA-3 squeeze, defined in assembly.
27    ///
28    /// `r` is the rate (block size) of the function in bytes.
29    ///
30    /// C signature:
31    ///
32    /// ```c
33    /// void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
34    /// ```
35    #[link_name = "KECCAK_ASM_SHA3_squeeze"]
36    pub fn SHA3_squeeze(a: *mut Buffer, out: *mut u8, len: usize, r: usize);
37}
38
39/// Safe wrapper for [`SHA3_absorb`]. See its docs for more.
40#[inline(always)]
41pub fn sha3_absorb(a: &mut Buffer, inp: &[u8], r: usize) -> usize {
42    unsafe { SHA3_absorb(a, inp.as_ptr(), inp.len(), r) }
43}
44
45/// Safe wrapper for [`SHA3_squeeze`]. See its docs for more.
46#[inline(always)]
47pub fn sha3_squeeze(a: &mut Buffer, out: &mut [u8], r: usize) {
48    unsafe { SHA3_squeeze(a, out.as_mut_ptr(), out.len(), r) }
49}
50
51#[doc(hidden)]
52pub const IMPL: &str = env!("SHA3_ASM_SRC");