Debian Codegraph @

Modules/_hacl/Hacl_Hash_Blake2s.h

raw
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_Blake2s_H
27 #define Hacl_Hash_Blake2s_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/internal/types.h"
36 #include "krml/lowstar_endianness.h"
37 #include "krml/internal/target.h"
38
39 #include "Hacl_Streaming_Types.h"
40 #include "Hacl_Hash_Blake2b.h"
41
42 #define HACL_HASH_BLAKE2S_BLOCK_BYTES (64U)
43
44 #define HACL_HASH_BLAKE2S_OUT_BYTES (32U)
45
46 #define HACL_HASH_BLAKE2S_KEY_BYTES (32U)
47
48 #define HACL_HASH_BLAKE2S_SALT_BYTES (8U)
49
50 #define HACL_HASH_BLAKE2S_PERSONAL_BYTES (8U)
51
52 typedef struct Hacl_Hash_Blake2s_block_state_t_s Hacl_Hash_Blake2s_block_state_t;
53
54 typedef struct Hacl_Hash_Blake2s_state_t_s Hacl_Hash_Blake2s_state_t;
55
56 /**
57 General-purpose allocation function that gives control over all
58 Blake2 parameters, including the key. Further resettings of the state SHALL be
59 done with `reset_with_params_and_key`, and SHALL feature the exact same values
60 for the `key_length` and `digest_length` fields as passed here. In other words,
61 once you commit to a digest and key length, the only way to change these
62 parameters is to allocate a new object.
63
64 The caller must satisfy the following requirements.
65 - The length of the key k MUST match the value of the field key_length in the
66 parameters.
67 - The key_length must not exceed 32 for S, 64 for B.
68 - The digest_length must not exceed 32 for S, 64 for B.
69
70 */
71 Hacl_Hash_Blake2s_state_t
72 *Hacl_Hash_Blake2s_malloc_with_params_and_key(
73 Hacl_Hash_Blake2b_blake2_params *p,
74 bool last_node,
75 uint8_t *k
76 );
77
78 /**
79 Specialized allocation function that picks default values for all
80 parameters, except for the key_length. Further resettings of the state SHALL be
81 done with `reset_with_key`, and SHALL feature the exact same key length `kk` as
82 passed here. In other words, once you commit to a key length, the only way to
83 change this parameter is to allocate a new object.
84
85 The caller must satisfy the following requirements.
86 - The key_length must not exceed 32 for S, 64 for B.
87
88 */
89 Hacl_Hash_Blake2s_state_t *Hacl_Hash_Blake2s_malloc_with_key(uint8_t *k, uint8_t kk);
90
91 /**
92 Specialized allocation function that picks default values for all
93 parameters, and has no key. Effectively, this is what you want if you intend to
94 use Blake2 as a hash function. Further resettings of the state SHALL be done with `reset`.
95 */
96 Hacl_Hash_Blake2s_state_t *Hacl_Hash_Blake2s_malloc(void);
97
98 /**
99 General-purpose re-initialization function with parameters and
100 key. You cannot change digest_length, key_length, or last_node, meaning those values in
101 the parameters object must be the same as originally decided via one of the
102 malloc functions. All other values of the parameter can be changed. The behavior
103 is unspecified if you violate this precondition.
104 */
105 void
106 Hacl_Hash_Blake2s_reset_with_key_and_params(
107 Hacl_Hash_Blake2s_state_t *s,
108 Hacl_Hash_Blake2b_blake2_params *p,
109 uint8_t *k
110 );
111
112 /**
113 Specialized-purpose re-initialization function with no parameters,
114 and a key. The key length must be the same as originally decided via your choice
115 of malloc function. All other parameters are reset to their default values. The
116 original call to malloc MUST have set digest_length to the default value. The
117 behavior is unspecified if you violate this precondition.
118 */
119 void Hacl_Hash_Blake2s_reset_with_key(Hacl_Hash_Blake2s_state_t *s, uint8_t *k);
120
121 /**
122 Specialized-purpose re-initialization function with no parameters
123 and no key. This is what you want if you intend to use Blake2 as a hash
124 function. The key length and digest length must have been set to their
125 respective default values via your choice of malloc function (always true if you
126 used `malloc`). All other parameters are reset to their default values. The
127 behavior is unspecified if you violate this precondition.
128 */
129 void Hacl_Hash_Blake2s_reset(Hacl_Hash_Blake2s_state_t *s);
130
131 /**
132 Update function; 0 = success, 1 = max length exceeded
133 */
134 Hacl_Streaming_Types_error_code
135 Hacl_Hash_Blake2s_update(Hacl_Hash_Blake2s_state_t *state, uint8_t *chunk, uint32_t chunk_len);
136
137 /**
138 Digest function. This function expects the `output` array to hold
139 at least `digest_length` bytes, where `digest_length` was determined by your
140 choice of `malloc` function. Concretely, if you used `malloc` or
141 `malloc_with_key`, then the expected length is 32 for S, or 64 for B (default
142 digest length). If you used `malloc_with_params_and_key`, then the expected
143 length is whatever you chose for the `digest_length` field of your parameters.
144 For convenience, this function returns `digest_length`. When in doubt, callers
145 can pass an array of size HACL_BLAKE2S_32_OUT_BYTES, then use the return value
146 to see how many bytes were actually written.
147 */
148 uint8_t Hacl_Hash_Blake2s_digest(Hacl_Hash_Blake2s_state_t *s, uint8_t *dst);
149
150 Hacl_Hash_Blake2b_index Hacl_Hash_Blake2s_info(Hacl_Hash_Blake2s_state_t *s);
151
152 /**
153 Free state function when there is no key
154 */
155 void Hacl_Hash_Blake2s_free(Hacl_Hash_Blake2s_state_t *state);
156
157 /**
158 Copying. This preserves all parameters.
159 */
160 Hacl_Hash_Blake2s_state_t *Hacl_Hash_Blake2s_copy(Hacl_Hash_Blake2s_state_t *state);
161
162 /**
163 Write the BLAKE2s digest of message `input` using key `key` into `output`.
164
165 @param output Pointer to `output_len` bytes of memory where the digest is written to.
166 @param output_len Length of the to-be-generated digest with 1 <= `output_len` <= 64.
167 @param input Pointer to `input_len` bytes of memory where the input message is read from.
168 @param input_len Length of the input message.
169 @param key Pointer to `key_len` bytes of memory where the key is read from.
170 @param key_len Length of the key. Can be 0.
171 */
172 void
173 Hacl_Hash_Blake2s_hash_with_key(
174 uint8_t *output,
175 uint32_t output_len,
176 uint8_t *input,
177 uint32_t input_len,
178 uint8_t *key,
179 uint32_t key_len
180 );
181
182 /**
183 Write the BLAKE2s digest of message `input` using key `key` and
184 parameters `params` into `output`. The `key` array must be of length
185 `params.key_length`. The `output` array must be of length
186 `params.digest_length`.
187 */
188 void
189 Hacl_Hash_Blake2s_hash_with_key_and_params(
190 uint8_t *output,
191 uint8_t *input,
192 uint32_t input_len,
193 Hacl_Hash_Blake2b_blake2_params params,
194 uint8_t *key
195 );
196
197 #if defined(__cplusplus)
198 }
199 #endif
200
201 #define Hacl_Hash_Blake2s_H_DEFINED
202 #endif /* Hacl_Hash_Blake2s_H */