-
Notifications
You must be signed in to change notification settings - Fork 7
/
hash.h
336 lines (325 loc) · 7.45 KB
/
hash.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (C) 2022 - This file is part of libdrbg project
*
* Author: Ryad BENADJILA <ryad.benadjila@ssi.gouv.fr>
* Contributor: Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#ifndef __HASH_H__
#define __HASH_H__
/* The configuration file */
#include "libhash_config.h"
#include "utils.h"
#define MAX_DIGEST_SIZE 0
#define MAX_BLOCK_SIZE 0
/* Hash algorithms */
#ifdef WITH_HASH_SHA224
#include "sha224.h"
#endif
#ifdef WITH_HASH_SHA256
#include "sha256.h"
#endif
#ifdef WITH_HASH_SHA384
#include "sha384.h"
#endif
#ifdef WITH_HASH_SHA512
#include "sha512.h"
#endif
#ifdef WITH_HASH_SHA512_224
#include "sha512-224.h"
#endif
#ifdef WITH_HASH_SHA512_256
#include "sha512-256.h"
#endif
#ifdef WITH_HASH_SHA3_224
#include "sha3-224.h"
#endif
#ifdef WITH_HASH_SHA3_256
#include "sha3-256.h"
#endif
#ifdef WITH_HASH_SHA3_384
#include "sha3-384.h"
#endif
#ifdef WITH_HASH_SHA3_512
#include "sha3-512.h"
#endif
#ifdef WITH_HASH_SM3
#include "sm3.h"
#endif
#ifdef WITH_HASH_SHAKE256
#include "shake256.h"
#endif
#ifdef WITH_HASH_STREEBOG256
#include "streebog256.h"
#endif
#ifdef WITH_HASH_STREEBOG512
#include "streebog512.h"
#endif
#ifdef WITH_HASH_RIPEMD160
#include "ripemd160.h"
#endif
#ifdef WITH_HASH_BELT_HASH
#include "belt-hash.h"
#endif
#ifdef WITH_HASH_BASH224
#include "bash224.h"
#endif
#ifdef WITH_HASH_BASH256
#include "bash256.h"
#endif
#ifdef WITH_HASH_BASH384
#include "bash384.h"
#endif
#ifdef WITH_HASH_BASH512
#include "bash512.h"
#endif
/* Deprecated hash algorithms */
#ifdef WITH_HASH_MD2
/* MD-2 */
#include "md2.h"
#endif
#ifdef WITH_HASH_MD4
/* MD-4 */
#include "md4.h"
#endif
#ifdef WITH_HASH_MD5
/* MD-5 */
#include "md5.h"
#endif
#ifdef WITH_HASH_SHA0
/* SHA-0 */
#include "sha0.h"
#endif
#ifdef WITH_HASH_SHA1
/* SHA-1 */
#include "sha1.h"
#endif
#ifdef WITH_HASH_MDC2
/* MDC-2 */
#include "mdc2.h"
#endif
#ifdef WITH_HASH_GOSTR34_11_94
/* GOSTR34-11-94 source code */
#include "gostr34_11_94.h"
#endif
#if (MAX_BLOCK_SIZE == 0) || (MAX_DIGEST_SIZE == 0)
#error "No hash function is defined!!! Please define at least one ..."
#endif
/****************************************************/
/****************************************************/
/****************************************************/
typedef enum {
HASH_UNKNOWN_HASH_ALG = 0,
#ifdef WITH_HASH_SHA224
HASH_SHA224 = 1,
#endif
#ifdef WITH_HASH_SHA256
HASH_SHA256 = 2,
#endif
#ifdef WITH_HASH_SHA384
HASH_SHA384 = 3,
#endif
#ifdef WITH_HASH_SHA512
HASH_SHA512 = 4,
#endif
#ifdef WITH_HASH_SHA512_224
HASH_SHA512_224 = 5,
#endif
#ifdef WITH_HASH_SHA512_256
HASH_SHA512_256 = 6,
#endif
#ifdef WITH_HASH_SHA3_224
HASH_SHA3_224 = 7,
#endif
#ifdef WITH_HASH_SHA3_256
HASH_SHA3_256 = 8,
#endif
#ifdef WITH_HASH_SHA3_384
HASH_SHA3_384 = 9,
#endif
#ifdef WITH_HASH_SHA3_512
HASH_SHA3_512 = 10,
#endif
#ifdef WITH_HASH_SM3
HASH_SM3 = 11,
#endif
#ifdef WITH_HASH_STREEBOG256
HASH_STREEBOG256 = 12,
#endif
#ifdef WITH_HASH_STREEBOG512
HASH_STREEBOG512 = 13,
#endif
#ifdef WITH_HASH_SHAKE256
HASH_SHAKE256 = 14,
#endif
#ifdef WITH_HASH_RIPEMD160
HASH_RIPEMD160 = 15,
#endif
#ifdef WITH_HASH_BELT_HASH
HASH_BELT_HASH = 16,
#endif
#ifdef WITH_HASH_BASH224
HASH_BASH224 = 17,
#endif
#ifdef WITH_HASH_BASH256
HASH_BASH256 = 18,
#endif
#ifdef WITH_HASH_BASH384
HASH_BASH384 = 19,
#endif
#ifdef WITH_HASH_BASH512
HASH_BASH512 = 20,
#endif
/* Deprecated hash algorithms (for security reasons).
* XXX: NOTE: These algorithms are here as a playground e.g.
* to test some backward compatibility of cryptographic cipher suites,
* please DO NOT use them in production code!
*/
#ifdef WITH_HASH_MD2
HASH_MD2 = 21,
#endif
#ifdef WITH_HASH_MD4
HASH_MD4 = 22,
#endif
#ifdef WITH_HASH_MD5
HASH_MD5 = 23,
#endif
#ifdef WITH_HASH_SHA0
HASH_SHA0 = 24,
#endif
#ifdef WITH_HASH_SHA1
HASH_SHA1 = 25,
#endif
#ifdef WITH_HASH_MDC2
HASH_MDC2_PADDING1 = 26,
HASH_MDC2_PADDING2 = 27,
#endif
#ifdef WITH_HASH_GOSTR34_11_94
HASH_GOST34_11_94_NORM = 28,
HASH_GOST34_11_94_RFC4357 = 29,
#endif
} hash_alg_type;
/* Our generic hash context */
typedef union {
#ifdef WITH_HASH_SHA224
sha224_context sha224ctx;
#endif
#ifdef WITH_HASH_SHA256
sha256_context sha256ctx;
#endif
#ifdef WITH_HASH_SHA384
sha384_context sha384ctx;
#endif
#ifdef WITH_HASH_SHA512
sha512_context sha512ctx;
#endif
#ifdef WITH_HASH_SHA512_224
sha512_224_context sha512_224ctx;
#endif
#ifdef WITH_HASH_SHA512_256
sha512_256_context sha512_256ctx;
#endif
#ifdef WITH_HASH_SHA3_224
sha3_224_context sha3_224ctx;
#endif
#ifdef WITH_HASH_SHA3_256
sha3_256_context sha3_256ctx;
#endif
#ifdef WITH_HASH_SHA3_384
sha3_384_context sha3_384ctx;
#endif
#ifdef WITH_HASH_SHA3_512
sha3_512_context sha3_512ctx;
#endif
#ifdef WITH_HASH_SM3
sm3_context sm3ctx;
#endif
#ifdef WITH_HASH_STREEBOG256
streebog256_context streebog256ctx;
#endif
#ifdef WITH_HASH_STREEBOG512
streebog512_context streebog512ctx;
#endif
#ifdef WITH_HASH_SHAKE256
shake256_context shake256ctx;
#endif
#ifdef WITH_HASH_RIPEMD160
ripemd160_context ripemd160ctx;
#endif
#ifdef WITH_HASH_BELT_HASH
belt_hash_context belt_hashctx;
#endif
#ifdef WITH_HASH_BASH224
bash224_context bash224ctx;
#endif
#ifdef WITH_HASH_BASH256
bash256_context bash256ctx;
#endif
#ifdef WITH_HASH_BASH384
bash384_context bash384ctx;
#endif
#ifdef WITH_HASH_BASH512
bash512_context bash512ctx;
#endif
/*** Deprecated hash functions ***/
#ifdef WITH_HASH_MD2
/* MD2 */
md2_context md2ctx;
#endif
#ifdef WITH_HASH_MD4
/* MD4 */
md4_context md4ctx;
#endif
#ifdef WITH_HASH_MD5
/* MD5 */
md5_context md5ctx;
#endif
#ifdef WITH_HASH_SHA0
/* SHA-0 */
sha0_context sha0ctx;
#endif
#ifdef WITH_HASH_SHA1
/* SHA-1 */
sha1_context sha1ctx;
#endif
#ifdef WITH_HASH_MDC2
/* MDC2 */
mdc2_context mdc2ctx;
#endif
#ifdef WITH_HASH_GOSTR34_11_94
/* GOSTR34-11-94 */
gostr34_11_94_context gostr34_11_94ctx;
#endif
} hash_context;
int hash_get_hash_sizes(hash_alg_type hash_type, uint8_t *hlen, uint8_t *block_size);
int hash_init(hash_context *ctx, hash_alg_type hash_type);
int hash_update(hash_context *ctx, const uint8_t *chunk, uint32_t chunklen, hash_alg_type hash_type);
int hash_final(hash_context *ctx, uint8_t *output, hash_alg_type hash_type);
int hash_hfunc(const uint8_t *input, uint32_t ilen, uint8_t *digest, hash_alg_type hash_type);
int hash_hfunc_scattered(const uint8_t **input, const uint32_t *ilen, uint8_t *digest, hash_alg_type hash_type);
/* Safeguard to handle MAX_DIGEST_SIZE consistency */
#ifdef __GNUC__
/* gcc and clang */
#define ATTRIBUTE_USED __attribute__((used))
#else
#define ATTRIBUTE_USED
#endif
#define _LIBHASH_CONCATENATE(a, b) a##_##b
#define LIBHASH_CONCATENATE(a, b) _LIBHASH_CONCATENATE(a, b)
void LIBHASH_CONCATENATE(libhash_consistency_check, MAX_DIGEST_SIZE) (void);
#ifdef LIBHASH_CONSISTENCY_CHECK
ATTRIBUTE_USED void LIBHASH_CONCATENATE(libhash_consistency_check,
MAX_DIGEST_SIZE) (void) {
return;
}
#else
ATTRIBUTE_USED static inline void libhash_check_libconsistency(void)
{
LIBHASH_CONCATENATE(libhash_consistency_check,
MAX_DIGEST_SIZE) ();
return;
}
#endif
#endif /* __HASH_H__ */