summaryrefslogtreecommitdiff
path: root/lib/liboqs/include/oqs/sha2.h
blob: 8d8973e4cfaf14ee39f1d1f94f15ac7bd85fa9c9 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
 * \file sha2.h
 * \brief SHA2 functions; not part of the OQS public API
 *
 * Contains the API and documentation for SHA2 digest implementation
 *
 * <b>Note this is not part of the OQS public API: implementations within liboqs can use these
 * functions, but external consumers of liboqs should not use these functions.</b>
 *
 * \author Douglas Stebila
 *
 * SPDX-License-Identifier: MIT
 */

#ifndef OQS_SHA2_H
#define OQS_SHA2_H

#include <stddef.h>
#include <stdint.h>

#if defined(__cplusplus)
extern "C" {
#endif

/** Data structure for the state of the SHA-224 incremental hashing API. */
typedef struct {
	/** Internal state */
	void *ctx;
} OQS_SHA2_sha224_ctx;

/**
 * \brief Process a message with SHA-256 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 32 bytes in length.
 *
 * \param output The output byte array
 * \param input The message input byte array
 * \param inplen The number of message bytes to process
 */
void OQS_SHA2_sha256(uint8_t *output, const uint8_t *input, size_t inplen);

/** Data structure for the state of the SHA-256 incremental hashing API. */
typedef struct {
	/** Internal state */
	void *ctx;
} OQS_SHA2_sha256_ctx;

/**
 * \brief Allocate and initialize the state for the SHA-256 incremental hashing API.
 *
 * \warning The state must be released by OQS_SHA2_sha256_inc_finalize
 * or OQS_SHA2_sha256_inc_ctx_release.
 *
 * \param state Pointer to the state
 */
void OQS_SHA2_sha256_inc_init(OQS_SHA2_sha256_ctx *state);

/**
 * \brief Duplicate state for the SHA-256 incremental hashing API.
 *
 * \warning dest must be allocated by the caller. Caller is responsible
 * for releasing dest by calling either OQS_SHA3_sha3_256_inc_finalize or
 * OQS_SHA3_sha3_256_inc_ctx_release.
 *
 * \param dest The function state to copy into; must be initialized
 * \param src The function state to copy; must be initialized
 */
void OQS_SHA2_sha256_inc_ctx_clone(OQS_SHA2_sha256_ctx *dest, const OQS_SHA2_sha256_ctx *src);

/**
 * \brief Process blocks with SHA-256 and update the state.
 *
 * \warning The state must be initialized by OQS_SHA2_sha256_inc_init or OQS_SHA2_sha256_inc_ctx_clone.
 *
 * \param state The state to update
 * \param in Message input byte array
 * \param inblocks The number of blocks of message bytes to process
 */
void OQS_SHA2_sha256_inc_blocks(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inblocks);

/**
 * \brief Process more message bytes with SHA-256 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 32 bytes in length. The state is
 * deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha256_inc_init again.
 *
 * \param out The output byte array
 * \param state The state
 * \param in Additional message input byte array
 * \param inlen The number of additional message bytes to process
 */
void OQS_SHA2_sha256_inc_finalize(uint8_t *out, OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inlen);

/**
 * \brief Destroy state.
 *
 * \warning The state is deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha256_inc_init again.
 *
 * \param state The state
 */
void OQS_SHA2_sha256_inc_ctx_release(OQS_SHA2_sha256_ctx *state);

/**
 * \brief Process a message with SHA-384 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 48 bytes in length.
 *
 * \param output The output byte array
 * \param input The message input byte array
 * \param inplen The number of message bytes to process
 */
void OQS_SHA2_sha384(uint8_t *output, const uint8_t *input, size_t inplen);

/** Data structure for the state of the SHA-384 incremental hashing API. */
typedef struct {
	/** Internal state. */
	void *ctx;
} OQS_SHA2_sha384_ctx;

/**
 * \brief Allocate and initialize the state for the SHA-384 incremental hashing API.
 *
 * \warning The state must be released by OQS_SHA2_sha384_inc_finalize
 * or OQS_SHA2_sha384_inc_ctx_release.
 *
 * \param state Pointer to the state
 */
void OQS_SHA2_sha384_inc_init(OQS_SHA2_sha384_ctx *state);

/**
 * \brief Duplicate state for the SHA-384 incremental hashing API.
 *
 * \warning dest must be allocated by the caller. Caller is responsible
 * for releasing dest by calling either OQS_SHA3_sha3_384_inc_finalize or
 * OQS_SHA3_sha3_384_inc_ctx_release.
 *
 * \param dest The function state to copy into; must be initialized
 * \param src The function state to copy; must be initialized
 */
void OQS_SHA2_sha384_inc_ctx_clone(OQS_SHA2_sha384_ctx *dest, const OQS_SHA2_sha384_ctx *src);

/**
 * \brief Process blocks with SHA-384 and update the state.
 *
 * \warning The state must be initialized by OQS_SHA2_sha384_inc_init or OQS_SHA2_sha384_inc_ctx_clone.
 *
 * \param state The state to update
 * \param in Message input byte array
 * \param inblocks The number of blocks of message bytes to process
 */
void OQS_SHA2_sha384_inc_blocks(OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inblocks);

/**
 * \brief Process more message bytes with SHA-384 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 48 bytes in length. The state is
 * deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha384_inc_init again.
 *
 * \param out The output byte array
 * \param state The state
 * \param in Additional message input byte array
 * \param inlen The number of additional message bytes to process
 */
void OQS_SHA2_sha384_inc_finalize(uint8_t *out, OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inlen);

/**
 * \brief Destroy state.
 *
 * \warning The state is deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha384_inc_init again.
 *
 * \param state The state
 */
void OQS_SHA2_sha384_inc_ctx_release(OQS_SHA2_sha384_ctx *state);

/**
 * \brief Process a message with SHA-512 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 64 bytes in length.
 *
 * \param output The output byte array
 * \param input The message input byte array
 * \param inplen The number of message bytes to process
 */
void OQS_SHA2_sha512(uint8_t *output, const uint8_t *input, size_t inplen);

/** Data structure for the state of the SHA-512 incremental hashing API. */
typedef struct {
	/** Internal state. */
	void *ctx;
} OQS_SHA2_sha512_ctx;

/**
 * \brief Allocate and initialize the state for the SHA-512 incremental hashing API.
 *
 * \warning The state must be released by OQS_SHA2_sha512_inc_finalize
 * or OQS_SHA2_sha512_inc_ctx_release.
 *
 * \param state Pointer to the state
 */
void OQS_SHA2_sha512_inc_init(OQS_SHA2_sha512_ctx *state);

/**
 * \brief Duplicate state for the SHA-512 incremental hashing API.
 *
 * \warning dest must be allocated by the caller. Caller is responsible
 * for releasing dest by calling either OQS_SHA3_sha3_512_inc_finalize or
 * OQS_SHA3_sha3_512_inc_ctx_release.
 *
 * \param dest The function state to copy into; must be initialized
 * \param src The function state to copy; must be initialized
 */
void OQS_SHA2_sha512_inc_ctx_clone(OQS_SHA2_sha512_ctx *dest, const OQS_SHA2_sha512_ctx *src);

/**
 * \brief Process blocks with SHA-512 and update the state.
 *
 * \warning The state must be initialized by OQS_SHA2_sha512_inc_init or OQS_SHA2_sha512_inc_ctx_clone.
 *
 * \param state The state to update
 * \param in Message input byte array
 * \param inblocks The number of blocks of message bytes to process
 */
void OQS_SHA2_sha512_inc_blocks(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks);

/**
 * \brief Process more message bytes with SHA-512 and return the hash code in the output byte array.
 *
 * \warning The output array must be at least 64 bytes in length. The state is
 * deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha512_inc_init again.
 *
 * \param out The output byte array
 * \param state The state
 * \param in Additional message input byte array
 * \param inlen The number of additional message bytes to process
 */
void OQS_SHA2_sha512_inc_finalize(uint8_t *out, OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inlen);

/**
 * \brief Destroy state.
 *
 * \warning The state is deallocated by this function and can not be used again after calling
 * this function without calling OQS_SHA2_sha512_inc_init again.
 *
 * \param state The state
 */
void OQS_SHA2_sha512_inc_ctx_release(OQS_SHA2_sha512_ctx *state);

#if defined(__cplusplus)
} // extern "C"
#endif

#endif // OQS_SHA2_H