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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use hacl_sys::{
    Hacl_Hash_SHA2_hash_224, Hacl_Hash_SHA2_hash_256, Hacl_Hash_SHA2_hash_384,
    Hacl_Hash_SHA2_hash_512,
};

/// SHA2 224
///
/// Note the function panics when `payload` is larger than 2^32 bytes.
pub fn sha224(payload: &[u8]) -> [u8; 28] {
    let mut digest = [0u8; 28];
    unsafe {
        Hacl_Hash_SHA2_hash_224(
            digest.as_mut_ptr(),
            payload.as_ptr() as _,
            payload.len().try_into().unwrap(),
        );
    }
    digest
}

/// SHA2 256
///
/// Note the function panics when `payload` is larger than 2^32 bytes.
pub fn sha256(payload: &[u8]) -> [u8; 32] {
    let mut digest = [0u8; 32];
    unsafe {
        Hacl_Hash_SHA2_hash_256(
            digest.as_mut_ptr(),
            payload.as_ptr() as _,
            payload.len().try_into().unwrap(),
        );
    }
    digest
}

/// SHA2 384
///
/// Note the function panics when `payload` is larger than 2^32 bytes.
pub fn sha384(payload: &[u8]) -> [u8; 48] {
    let mut digest = [0u8; 48];
    unsafe {
        Hacl_Hash_SHA2_hash_384(
            digest.as_mut_ptr(),
            payload.as_ptr() as _,
            payload.len().try_into().unwrap(),
        );
    }
    digest
}

/// SHA2 512
///
/// Note the function panics when `payload` is larger than 2^32 bytes.
pub fn sha512(payload: &[u8]) -> [u8; 64] {
    let mut digest = [0u8; 64];
    unsafe {
        Hacl_Hash_SHA2_hash_512(
            digest.as_mut_ptr(),
            payload.as_ptr() as _,
            payload.len().try_into().unwrap(),
        );
    }
    digest
}

pub mod streaming {
    use hacl_sys::{
        Hacl_Hash_SHA2_digest_224, Hacl_Hash_SHA2_digest_256, Hacl_Hash_SHA2_digest_384,
        Hacl_Hash_SHA2_digest_512, Hacl_Hash_SHA2_free_224, Hacl_Hash_SHA2_free_256,
        Hacl_Hash_SHA2_free_384, Hacl_Hash_SHA2_free_512, Hacl_Hash_SHA2_malloc_224,
        Hacl_Hash_SHA2_malloc_256, Hacl_Hash_SHA2_malloc_384, Hacl_Hash_SHA2_malloc_512,
        Hacl_Hash_SHA2_reset_224, Hacl_Hash_SHA2_reset_256, Hacl_Hash_SHA2_reset_384,
        Hacl_Hash_SHA2_reset_512, Hacl_Hash_SHA2_state_t_224, Hacl_Hash_SHA2_state_t_384,
        Hacl_Hash_SHA2_update_224, Hacl_Hash_SHA2_update_256, Hacl_Hash_SHA2_update_384,
        Hacl_Hash_SHA2_update_512,
    };

    macro_rules! impl_streaming {
        ($name:ident, $digest_size:literal, $state:ty, $malloc:expr, $reset:expr, $update:expr, $digest:expr, $free:expr) => {
            pub struct $name {
                state: *mut $state,
            }

            impl $name {
                /// Initialize a new digest state.
                pub fn new() -> $name {
                    let state = $name {
                        state: unsafe { $malloc() },
                    };
                    unsafe { $reset(state.state) };
                    state
                }

                /// Add the `payload` to the digest.
                pub fn update(&mut self, payload: &[u8]) {
                    // Note that we don't really need mut here because the mutability is
                    // only in unsafe C code.
                    // But this way we force the borrow checker to do the right thing.
                    unsafe { $update(self.state, payload.as_ptr() as _, payload.len() as u32) };
                }

                /// Get the digest.
                ///
                /// Note that the digest state can be continued to be used, to extend the
                /// digest.
                pub fn finish(&mut self) -> [u8; $digest_size] {
                    // Note that we don't really need mut here because the mutability is
                    // only in unsafe C code.
                    // But this way we force the borrow checker to do the right thing.
                    let mut digest = [0u8; $digest_size];
                    unsafe {
                        $digest(self.state, digest.as_mut_ptr());
                    }
                    digest
                }
            }

            impl Drop for $name {
                fn drop(&mut self) {
                    unsafe { $free(self.state) };
                }
            }
        };
    }

    impl_streaming!(
        Sha224,
        28,
        Hacl_Hash_SHA2_state_t_224,
        Hacl_Hash_SHA2_malloc_224,
        Hacl_Hash_SHA2_reset_224,
        Hacl_Hash_SHA2_update_224,
        Hacl_Hash_SHA2_digest_224,
        Hacl_Hash_SHA2_free_224
    );

    impl_streaming!(
        Sha256,
        32,
        Hacl_Hash_SHA2_state_t_224,
        Hacl_Hash_SHA2_malloc_256,
        Hacl_Hash_SHA2_reset_256,
        Hacl_Hash_SHA2_update_256,
        Hacl_Hash_SHA2_digest_256,
        Hacl_Hash_SHA2_free_256
    );

    impl_streaming!(
        Sha384,
        48,
        Hacl_Hash_SHA2_state_t_384,
        Hacl_Hash_SHA2_malloc_384,
        Hacl_Hash_SHA2_reset_384,
        Hacl_Hash_SHA2_update_384,
        Hacl_Hash_SHA2_digest_384,
        Hacl_Hash_SHA2_free_384
    );

    impl_streaming!(
        Sha512,
        64,
        Hacl_Hash_SHA2_state_t_384,
        Hacl_Hash_SHA2_malloc_512,
        Hacl_Hash_SHA2_reset_512,
        Hacl_Hash_SHA2_update_512,
        Hacl_Hash_SHA2_digest_512,
        Hacl_Hash_SHA2_free_512
    );
}