forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash_algs.c
359 lines (342 loc) · 10.7 KB
/
hash_algs.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* 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.
*/
#include "hash_algs.h"
/*
* Return the hash mapping entry 'hm' associated with given hash name
* 'hash_name'. The function returns 0 on success, -1 on error. 'hm'
* is only meaningful on success.
*/
ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_name(const char *hash_name, const hash_mapping **hm)
{
const hash_mapping *_hm = NULL;
int ret, check;
u8 i;
MUST_HAVE(((hash_name != NULL) && (hm != NULL)), ret, err);
ret = -1;
for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG;
_hm = &hash_maps[++i]) {
const char *exp_name = (const char *)_hm->name;
if ((!are_str_equal(hash_name, exp_name, &check)) && check) {
(*hm) = _hm;
ret = 0;
break;
}
}
err:
return ret;
}
/*
* Return the hash mapping entry 'hm' associated with given hash type value.
* The function returns 0 on success, -1 on error. 'hm' is not meaningfull
* on error.
*/
ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_type(hash_alg_type hash_type, const hash_mapping **hm)
{
const hash_mapping *_hm = NULL;
int ret;
u8 i;
MUST_HAVE((hm != NULL), ret, err);
ret = -1;
for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG;
_hm = &hash_maps[++i]) {
if (_hm->type == hash_type) {
(*hm) = _hm;
ret = 0;
break;
}
}
err:
return ret;
}
/*
* Returns respectively in digest_size and block_size param the digest size
* and block size for given hash function, if return value of the function is 0.
* If return value is -1, then the hash algorithm is not known and output
* parameters are not modified.
*/
ATTRIBUTE_WARN_UNUSED_RET int get_hash_sizes(hash_alg_type hash_type, u8 *digest_size, u8 *block_size)
{
const hash_mapping *m;
int ret;
u8 i;
ret = -1;
for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG;
m = &hash_maps[++i]) {
if (m->type == hash_type) {
if (digest_size != NULL) {
(*digest_size) = m->digest_size;
}
if (block_size != NULL) {
(*block_size) = m->block_size;
}
ret = 0;
break;
}
}
return ret;
}
/*
* Helper that sanity checks the provided hash mapping against our
* constant ones. Returns 0 on success, -1 on error.
*/
ATTRIBUTE_WARN_UNUSED_RET int hash_mapping_callbacks_sanity_check(const hash_mapping *h)
{
const hash_mapping *m;
int ret = -1, check;
u8 i;
MUST_HAVE((h != NULL), ret, err);
/* We just check is our mapping is indeed
* one of the registered mappings.
*/
for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG;
m = &hash_maps[++i]) {
if (m->type == h->type) {
if ((!are_str_equal_nlen(m->name, h->name, MAX_HASH_ALG_NAME_LEN, &check)) && (!check)){
goto err;
} else if (m->digest_size != h->digest_size) {
goto err;
} else if(m->block_size != h->block_size) {
goto err;
} else if(m->hfunc_init != h->hfunc_init) {
goto err;
} else if(m->hfunc_update != h->hfunc_update) {
goto err;
} else if(m->hfunc_finalize != h->hfunc_finalize) {
goto err;
} else if(m->hfunc_scattered != h->hfunc_scattered) {
goto err;
} else{
ret = 0;
}
}
}
err:
return ret;
}
/*****************************************/
/* Trampolines to each specific function to
* handle typing of our generic union structure.
*/
#ifdef WITH_HASH_SHA224
ATTRIBUTE_WARN_UNUSED_RET int _sha224_init(hash_context * hctx)
{
return sha224_init((sha224_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha224_update((sha224_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha224_final(hash_context * hctx, unsigned char *output)
{
return sha224_final((sha224_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA256
ATTRIBUTE_WARN_UNUSED_RET int _sha256_init(hash_context * hctx)
{
return sha256_init((sha256_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha256_update((sha256_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha256_final(hash_context * hctx, unsigned char *output)
{
return sha256_final((sha256_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA384
ATTRIBUTE_WARN_UNUSED_RET int _sha384_init(hash_context * hctx)
{
return sha384_init((sha384_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha384_update((sha384_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha384_final(hash_context * hctx, unsigned char *output)
{
return sha384_final((sha384_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA512
ATTRIBUTE_WARN_UNUSED_RET int _sha512_init(hash_context * hctx)
{
return sha512_init((sha512_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha512_update((sha512_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_final(hash_context * hctx, unsigned char *output)
{
return sha512_final((sha512_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA512_224
ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_init(hash_context * hctx)
{
return sha512_224_init((sha512_224_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha512_224_update((sha512_224_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_final(hash_context * hctx, unsigned char *output)
{
return sha512_224_final((sha512_224_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA512_256
ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_init(hash_context * hctx)
{
return sha512_256_init((sha512_256_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha512_256_update((sha512_256_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_final(hash_context * hctx, unsigned char *output)
{
return sha512_256_final((sha512_256_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA3_224
ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_init(hash_context * hctx)
{
return sha3_224_init((sha3_224_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha3_224_update((sha3_224_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_final(hash_context * hctx, unsigned char *output)
{
return sha3_224_final((sha3_224_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA3_256
ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_init(hash_context * hctx)
{
return sha3_256_init((sha3_256_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha3_256_update((sha3_256_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_final(hash_context * hctx, unsigned char *output)
{
return sha3_256_final((sha3_256_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA3_384
ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_init(hash_context * hctx)
{
return sha3_384_init((sha3_384_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha3_384_update((sha3_384_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_final(hash_context * hctx, unsigned char *output)
{
return sha3_384_final((sha3_384_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHA3_512
ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_init(hash_context * hctx)
{
return sha3_512_init((sha3_512_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sha3_512_update((sha3_512_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_final(hash_context * hctx, unsigned char *output)
{
return sha3_512_final((sha3_512_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SM3
ATTRIBUTE_WARN_UNUSED_RET int _sm3_init(hash_context * hctx)
{
return sm3_init((sm3_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _sm3_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return sm3_update((sm3_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _sm3_final(hash_context * hctx, unsigned char *output)
{
return sm3_final((sm3_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_SHAKE256
ATTRIBUTE_WARN_UNUSED_RET int _shake256_init(hash_context * hctx)
{
return shake256_init((shake256_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _shake256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return shake256_update((shake256_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _shake256_final(hash_context * hctx, unsigned char *output)
{
return shake256_final((shake256_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_STREEBOG256
ATTRIBUTE_WARN_UNUSED_RET int _streebog256_init(hash_context * hctx)
{
return streebog256_init((streebog256_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _streebog256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return streebog256_update((streebog256_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _streebog256_final(hash_context * hctx, unsigned char *output)
{
return streebog256_final((streebog256_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_STREEBOG512
ATTRIBUTE_WARN_UNUSED_RET int _streebog512_init(hash_context * hctx)
{
return streebog512_init((streebog512_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _streebog512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return streebog512_update((streebog512_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _streebog512_final(hash_context * hctx, unsigned char *output)
{
return streebog512_final((streebog512_context*)hctx, output);
}
#endif
#ifdef WITH_HASH_RIPEMD160
ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_init(hash_context * hctx)
{
return ripemd160_init((ripemd160_context*)hctx);
}
ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)
{
return ripemd160_update((ripemd160_context*)hctx, chunk, chunklen);
}
ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_final(hash_context * hctx, unsigned char *output)
{
return ripemd160_final((ripemd160_context*)hctx, output);
}
#endif