1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use hacl_sys::{Hacl_Hash_Blake2b_hash_with_key, Hacl_Hash_Blake2s_hash_with_key};

/// BLAKE2b
///
/// Note that the output can not be more than 64. If the requested output is
/// larger than 64, the 64-byte output value is written to the first 64 bytes of
/// the output.
///
/// The payload and key are truncated at 2^32 bytes.
///
/// The `key` may be an empty slice.
pub fn blake2b<const LEN: usize>(payload: &[u8], key: &[u8]) -> [u8; LEN] {
    let nn = if LEN > 64 { 64u32 } else { LEN as u32 };
    let mut digest = [0u8; LEN];
    unsafe {
        Hacl_Hash_Blake2b_hash_with_key(
            digest.as_mut_ptr(),
            nn,
            payload.as_ptr() as _,
            payload.len() as u32,
            key.as_ptr() as _,
            key.len() as u32,
        )
    }
    digest
}

#[cfg(simd256)]
pub mod simd256 {
    use hacl_sys::Hacl_Hash_Blake2b_Simd256_hash_with_key;

    /// BLAKE2b
    ///
    /// Note that the output can not be more than 64. If the requested output is
    /// larger than 64, the 64-byte output value is written to the first 64 bytes of
    /// the output.
    ///
    /// The input and key are truncated at 2^32 bytes.
    ///
    /// The `key` may be an empty slice.
    pub fn blake2b<const LEN: usize>(payload: &[u8], key: &[u8]) -> [u8; LEN] {
        let nn = if LEN > 64 { 64u32 } else { LEN as u32 };
        let mut digest = [0u8; LEN];
        unsafe {
            Hacl_Hash_Blake2b_Simd256_hash_with_key(
                digest.as_mut_ptr(),
                nn,
                payload.as_ptr() as _,
                payload.len() as u32,
                key.as_ptr() as _,
                key.len() as u32,
            )
        }
        digest
    }
}

/// BLAKE2s
///
/// Note that the output can not be more than 32. If the requested output is
/// larger than 32, the 32-byte output value is written to the first 32 bytes of
/// the output.
///
/// The input and key are truncated at 2^32 bytes.
///
/// The `key` may be an empty slice.
pub fn blake2s<const LEN: usize>(payload: &[u8], key: &[u8]) -> [u8; LEN] {
    let nn = if LEN > 32 { 32u32 } else { LEN as u32 };
    let mut digest = [0u8; LEN];
    unsafe {
        Hacl_Hash_Blake2s_hash_with_key(
            digest.as_mut_ptr(),
            nn,
            payload.as_ptr() as _,
            payload.len() as u32,
            key.as_ptr() as _,
            key.len() as u32,
        )
    }
    digest
}

#[cfg(simd128)]
pub mod simd128 {
    use hacl_sys::Hacl_Hash_Blake2s_Simd128_hash_with_key;

    /// BLAKE2s
    ///
    /// Note that the output can not be more than 32. If the requested output is
    /// larger than 32, the 32-byte output value is written to the first 32 bytes of
    /// the output.
    ///
    /// The input and key are truncated at 2^32 bytes.
    ///
    /// The `key` may be an empty slice.
    pub fn blake2s<const LEN: usize>(payload: &[u8], key: &[u8]) -> [u8; LEN] {
        let nn = if LEN > 64 { 64u32 } else { LEN as u32 };
        let mut digest = [0u8; LEN];
        unsafe {
            Hacl_Hash_Blake2s_Simd128_hash_with_key(
                digest.as_mut_ptr(),
                nn,
                payload.as_ptr() as _,
                payload.len() as u32,
                key.as_ptr() as _,
                key.len() as u32,
            )
        }
        digest
    }
}