• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) 2016-2022 INRIA, CMU and Microsoft Corporation
4  * Copyright (c) 2022-2023 HACL* Contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 
26 #ifndef __Hacl_Hash_SHA2_H
27 #define __Hacl_Hash_SHA2_H
28 
29 #if defined(__cplusplus)
30 extern "C" {
31 #endif
32 
33 #include <string.h>
34 #include "python_hacl_namespaces.h"
35 #include "krml/types.h"
36 #include "krml/lowstar_endianness.h"
37 #include "krml/internal/target.h"
38 
39 #include "Hacl_Streaming_Types.h"
40 
41 
42 typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_224;
43 
44 typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA2_state_t_256;
45 
46 typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_384;
47 
48 typedef Hacl_Streaming_MD_state_64 Hacl_Hash_SHA2_state_t_512;
49 
50 /**
51 Allocate initial state for the SHA2_256 hash. The state is to be freed by
52 calling `free_256`.
53 */
54 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_256(void);
55 
56 /**
57 Copies the state passed as argument into a newly allocated state (deep copy).
58 The state is to be freed by calling `free_256`. Cloning the state this way is
59 useful, for instance, if your control-flow diverges and you need to feed
60 more (different) data into the hash in each branch.
61 */
62 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_copy_256(Hacl_Streaming_MD_state_32 *state);
63 
64 /**
65 Reset an existing state to the initial hash state with empty data.
66 */
67 void Hacl_Hash_SHA2_reset_256(Hacl_Streaming_MD_state_32 *state);
68 
69 /**
70 Feed an arbitrary amount of data into the hash. This function returns 0 for
71 success, or 1 if the combined length of all of the data passed to `update_256`
72 (since the last call to `reset_256`) exceeds 2^61-1 bytes.
73 
74 This function is identical to the update function for SHA2_224.
75 */
76 Hacl_Streaming_Types_error_code
77 Hacl_Hash_SHA2_update_256(
78   Hacl_Streaming_MD_state_32 *state,
79   uint8_t *input,
80   uint32_t input_len
81 );
82 
83 /**
84 Write the resulting hash into `output`, an array of 32 bytes. The state remains
85 valid after a call to `digest_256`, meaning the user may feed more data into
86 the hash via `update_256`. (The digest_256 function operates on an internal copy of
87 the state and therefore does not invalidate the client-held state `p`.)
88 */
89 void Hacl_Hash_SHA2_digest_256(Hacl_Streaming_MD_state_32 *state, uint8_t *output);
90 
91 /**
92 Free a state allocated with `malloc_256`.
93 
94 This function is identical to the free function for SHA2_224.
95 */
96 void Hacl_Hash_SHA2_free_256(Hacl_Streaming_MD_state_32 *state);
97 
98 /**
99 Hash `input`, of len `input_len`, into `output`, an array of 32 bytes.
100 */
101 void Hacl_Hash_SHA2_hash_256(uint8_t *output, uint8_t *input, uint32_t input_len);
102 
103 Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA2_malloc_224(void);
104 
105 void Hacl_Hash_SHA2_reset_224(Hacl_Streaming_MD_state_32 *state);
106 
107 Hacl_Streaming_Types_error_code
108 Hacl_Hash_SHA2_update_224(
109   Hacl_Streaming_MD_state_32 *state,
110   uint8_t *input,
111   uint32_t input_len
112 );
113 
114 /**
115 Write the resulting hash into `output`, an array of 28 bytes. The state remains
116 valid after a call to `digest_224`, meaning the user may feed more data into
117 the hash via `update_224`.
118 */
119 void Hacl_Hash_SHA2_digest_224(Hacl_Streaming_MD_state_32 *state, uint8_t *output);
120 
121 void Hacl_Hash_SHA2_free_224(Hacl_Streaming_MD_state_32 *state);
122 
123 /**
124 Hash `input`, of len `input_len`, into `output`, an array of 28 bytes.
125 */
126 void Hacl_Hash_SHA2_hash_224(uint8_t *output, uint8_t *input, uint32_t input_len);
127 
128 Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_512(void);
129 
130 /**
131 Copies the state passed as argument into a newly allocated state (deep copy).
132 The state is to be freed by calling `free_512`. Cloning the state this way is
133 useful, for instance, if your control-flow diverges and you need to feed
134 more (different) data into the hash in each branch.
135 */
136 Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_copy_512(Hacl_Streaming_MD_state_64 *state);
137 
138 void Hacl_Hash_SHA2_reset_512(Hacl_Streaming_MD_state_64 *state);
139 
140 /**
141 Feed an arbitrary amount of data into the hash. This function returns 0 for
142 success, or 1 if the combined length of all of the data passed to `update_512`
143 (since the last call to `reset_512`) exceeds 2^125-1 bytes.
144 
145 This function is identical to the update function for SHA2_384.
146 */
147 Hacl_Streaming_Types_error_code
148 Hacl_Hash_SHA2_update_512(
149   Hacl_Streaming_MD_state_64 *state,
150   uint8_t *input,
151   uint32_t input_len
152 );
153 
154 /**
155 Write the resulting hash into `output`, an array of 64 bytes. The state remains
156 valid after a call to `digest_512`, meaning the user may feed more data into
157 the hash via `update_512`. (The digest_512 function operates on an internal copy of
158 the state and therefore does not invalidate the client-held state `p`.)
159 */
160 void Hacl_Hash_SHA2_digest_512(Hacl_Streaming_MD_state_64 *state, uint8_t *output);
161 
162 /**
163 Free a state allocated with `malloc_512`.
164 
165 This function is identical to the free function for SHA2_384.
166 */
167 void Hacl_Hash_SHA2_free_512(Hacl_Streaming_MD_state_64 *state);
168 
169 /**
170 Hash `input`, of len `input_len`, into `output`, an array of 64 bytes.
171 */
172 void Hacl_Hash_SHA2_hash_512(uint8_t *output, uint8_t *input, uint32_t input_len);
173 
174 Hacl_Streaming_MD_state_64 *Hacl_Hash_SHA2_malloc_384(void);
175 
176 void Hacl_Hash_SHA2_reset_384(Hacl_Streaming_MD_state_64 *state);
177 
178 Hacl_Streaming_Types_error_code
179 Hacl_Hash_SHA2_update_384(
180   Hacl_Streaming_MD_state_64 *state,
181   uint8_t *input,
182   uint32_t input_len
183 );
184 
185 /**
186 Write the resulting hash into `output`, an array of 48 bytes. The state remains
187 valid after a call to `digest_384`, meaning the user may feed more data into
188 the hash via `update_384`.
189 */
190 void Hacl_Hash_SHA2_digest_384(Hacl_Streaming_MD_state_64 *state, uint8_t *output);
191 
192 void Hacl_Hash_SHA2_free_384(Hacl_Streaming_MD_state_64 *state);
193 
194 /**
195 Hash `input`, of len `input_len`, into `output`, an array of 48 bytes.
196 */
197 void Hacl_Hash_SHA2_hash_384(uint8_t *output, uint8_t *input, uint32_t input_len);
198 
199 #if defined(__cplusplus)
200 }
201 #endif
202 
203 #define __Hacl_Hash_SHA2_H_DEFINED
204 #endif
205