forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_algs.h
263 lines (253 loc) · 8.24 KB
/
hash_algs.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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#ifndef __HASH_ALGS_H__
#define __HASH_ALGS_H__
#include "../lib_ecc_config.h"
#include "../lib_ecc_types.h"
#include "../words/words.h"
#include "sha224.h"
#include "sha256.h"
#include "sha384.h"
#include "sha512.h"
#include "sha512-224.h"
#include "sha512-256.h"
#include "sha3-224.h"
#include "sha3-256.h"
#include "sha3-384.h"
#include "sha3-512.h"
#include "../utils/utils.h"
#if (MAX_DIGEST_SIZE == 0)
#error "It seems you disabled all hash algorithmsin lib_ecc_config.h"
#endif
#if (MAX_BLOCK_SIZE == 0)
#error "It seems you disabled all hash algorithmsin lib_ecc_config.h"
#endif
typedef union {
#ifdef SHA224_BLOCK_SIZE
sha224_context sha224;
#endif
#ifdef SHA256_BLOCK_SIZE
sha256_context sha256;
#endif
#ifdef SHA384_BLOCK_SIZE
sha384_context sha384;
#endif
#ifdef SHA512_BLOCK_SIZE
sha512_context sha512;
#endif
#ifdef SHA512_224_BLOCK_SIZE
sha512_224_context sha512_224;
#endif
#ifdef SHA512_256_BLOCK_SIZE
sha512_256_context sha512_256;
#endif
#ifdef SHA3_224_BLOCK_SIZE
sha3_224_context sha3_224;
#endif
#ifdef SHA3_256_BLOCK_SIZE
sha3_256_context sha3_256;
#endif
#ifdef SHA3_384_BLOCK_SIZE
sha3_384_context sha3_384;
#endif
#ifdef SHA3_512_BLOCK_SIZE
sha3_512_context sha3_512;
#endif
} hash_context;
typedef void (*_hfunc_init) (hash_context * hctx);
typedef void (*_hfunc_update) (hash_context * hctx,
const unsigned char *chunk, u32 chunklen);
typedef void (*_hfunc_finalize) (hash_context * hctx, unsigned char *output);
typedef void (*_hfunc_scattered) (const unsigned char **inputs,
const u32 *ilens, unsigned char *output);
#define HASH_MAPPING_SANITY_CHECK(A) \
MUST_HAVE(((A) != NULL) && \
((A)->name != NULL) && \
((A)->hfunc_init != NULL) && \
((A)->hfunc_update != NULL) && \
((A)->hfunc_finalize != NULL) && \
((A)->hfunc_scattered != NULL))
/*
* All the hash algorithms we support are abstracted using the following
* structure (and following map) which provides for each hash alg its
* digest size, its block size and the associated scattered function.
*/
typedef struct {
hash_alg_type type;
const char *name;
u8 digest_size;
u8 block_size;
_hfunc_init hfunc_init;
_hfunc_update hfunc_update;
_hfunc_finalize hfunc_finalize;
_hfunc_scattered hfunc_scattered;
} hash_mapping;
#define MAX_HASH_ALG_NAME_LEN 0
static const hash_mapping hash_maps[] = {
#ifdef WITH_HASH_SHA224
{.type = SHA224, /* SHA224 */
.name = "SHA224",
.digest_size = SHA224_DIGEST_SIZE,
.block_size = SHA224_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha224_init,
.hfunc_update = (_hfunc_update) sha224_update,
.hfunc_finalize = (_hfunc_finalize) sha224_final,
.hfunc_scattered = sha224_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA224 */
#ifdef WITH_HASH_SHA256
{.type = SHA256, /* SHA256 */
.name = "SHA256",
.digest_size = SHA256_DIGEST_SIZE,
.block_size = SHA256_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha256_init,
.hfunc_update = (_hfunc_update) sha256_update,
.hfunc_finalize = (_hfunc_finalize) sha256_final,
.hfunc_scattered = sha256_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA256 */
#ifdef WITH_HASH_SHA384
{.type = SHA384, /* SHA384 */
.name = "SHA384",
.digest_size = SHA384_DIGEST_SIZE,
.block_size = SHA384_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha384_init,
.hfunc_update = (_hfunc_update) sha384_update,
.hfunc_finalize = (_hfunc_finalize) sha384_final,
.hfunc_scattered = sha384_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA384 */
#ifdef WITH_HASH_SHA512
{.type = SHA512, /* SHA512 */
.name = "SHA512",
.digest_size = SHA512_DIGEST_SIZE,
.block_size = SHA512_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha512_init,
.hfunc_update = (_hfunc_update) sha512_update,
.hfunc_finalize = (_hfunc_finalize) sha512_final,
.hfunc_scattered = sha512_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA512 */
#ifdef WITH_HASH_SHA512_224
{.type = SHA512_224, /* SHA512_224 */
.name = "SHA512_224",
.digest_size = SHA512_224_DIGEST_SIZE,
.block_size = SHA512_224_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha512_224_init,
.hfunc_update = (_hfunc_update) sha512_224_update,
.hfunc_finalize = (_hfunc_finalize) sha512_224_final,
.hfunc_scattered = sha512_224_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA512_224 */
#ifdef WITH_HASH_SHA512_256
{.type = SHA512_256, /* SHA512_256 */
.name = "SHA512_256",
.digest_size = SHA512_256_DIGEST_SIZE,
.block_size = SHA512_256_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha512_256_init,
.hfunc_update = (_hfunc_update) sha512_256_update,
.hfunc_finalize = (_hfunc_finalize) sha512_256_final,
.hfunc_scattered = sha512_256_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 7)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 7
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA512_256 */
#ifdef WITH_HASH_SHA3_224
{.type = SHA3_224, /* SHA3_224 */
.name = "SHA3_224",
.digest_size = SHA3_224_DIGEST_SIZE,
.block_size = SHA3_224_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha3_224_init,
.hfunc_update = (_hfunc_update) sha3_224_update,
.hfunc_finalize = (_hfunc_finalize) sha3_224_final,
.hfunc_scattered = sha3_224_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 9)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 9
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA3_224 */
#ifdef WITH_HASH_SHA3_256
{.type = SHA3_256, /* SHA3_256 */
.name = "SHA3_256",
.digest_size = SHA3_256_DIGEST_SIZE,
.block_size = SHA3_256_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha3_256_init,
.hfunc_update = (_hfunc_update) sha3_256_update,
.hfunc_finalize = (_hfunc_finalize) sha3_256_final,
.hfunc_scattered = sha3_256_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 9)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 9
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA3_256 */
#ifdef WITH_HASH_SHA3_384
{.type = SHA3_384, /* SHA3_384 */
.name = "SHA3_384",
.digest_size = SHA3_384_DIGEST_SIZE,
.block_size = SHA3_384_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha3_384_init,
.hfunc_update = (_hfunc_update) sha3_384_update,
.hfunc_finalize = (_hfunc_finalize) sha3_384_final,
.hfunc_scattered = sha3_384_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 9)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 9
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA3_384 */
#ifdef WITH_HASH_SHA3_512
{.type = SHA3_512, /* SHA3_512 */
.name = "SHA3_512",
.digest_size = SHA3_512_DIGEST_SIZE,
.block_size = SHA3_512_BLOCK_SIZE,
.hfunc_init = (_hfunc_init) sha3_512_init,
.hfunc_update = (_hfunc_update) sha3_512_update,
.hfunc_finalize = (_hfunc_finalize) sha3_512_final,
.hfunc_scattered = sha3_512_scattered},
#if (MAX_HASH_ALG_NAME_LEN < 9)
#undef MAX_HASH_ALG_NAME_LEN
#define MAX_HASH_ALG_NAME_LEN 9
#endif /* MAX_HASH_ALG_NAME_LEN */
#endif /* WITH_HASH_SHA3_512 */
{.type = UNKNOWN_HASH_ALG, /* Needs to be kept last */
.name = "UNKNOWN",
.digest_size = 0,
.block_size = 0,
.hfunc_init = NULL,
.hfunc_update = NULL,
.hfunc_finalize = NULL,
.hfunc_scattered = NULL},
};
const hash_mapping *get_hash_by_name(const char *hash_name);
const hash_mapping *get_hash_by_type(hash_alg_type hash_type);
int get_hash_sizes(hash_alg_type hash_type, u8 *digest_size, u8 *block_size);
int hash_mapping_callbacks_sanity_check(const hash_mapping *h);
#endif /* __HASH_ALGS_H__ */