testing: Updated hasher tests
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.c
1 /*
2 * Copyright (C) 2012-2015 Tobias Brunner
3 * Copyright (C) 2015 Andreas Steffen
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "hasher.h"
20
21 #include <asn1/oid.h>
22
23 ENUM_BEGIN(hash_algorithm_names, HASH_SHA1, HASH_SHA512,
24 "HASH_SHA1",
25 "HASH_SHA256",
26 "HASH_SHA384",
27 "HASH_SHA512");
28 ENUM_NEXT(hash_algorithm_names, HASH_UNKNOWN, HASH_SHA3_512, HASH_SHA512,
29 "HASH_UNKNOWN",
30 "HASH_MD2",
31 "HASH_MD4",
32 "HASH_MD5",
33 "HASH_SHA224",
34 "HASH_SHA3_224",
35 "HASH_SHA3_256",
36 "HASH_SHA3_384",
37 "HASH_SHA3_512");
38 ENUM_END(hash_algorithm_names, HASH_SHA3_512);
39
40 ENUM_BEGIN(hash_algorithm_short_names, HASH_SHA1, HASH_SHA512,
41 "sha1",
42 "sha256",
43 "sha384",
44 "sha512");
45 ENUM_NEXT(hash_algorithm_short_names, HASH_UNKNOWN, HASH_SHA3_512, HASH_SHA512,
46 "unknown",
47 "md2",
48 "md4",
49 "md5",
50 "sha224",
51 "sha3_224",
52 "sha3_256",
53 "sha3_384",
54 "sha3_512");
55 ENUM_END(hash_algorithm_short_names, HASH_SHA3_512);
56
57 /*
58 * Described in header.
59 */
60 hash_algorithm_t hasher_algorithm_from_oid(int oid)
61 {
62 switch (oid)
63 {
64 case OID_MD2:
65 case OID_MD2_WITH_RSA:
66 return HASH_MD2;
67 case OID_MD5:
68 case OID_MD5_WITH_RSA:
69 return HASH_MD5;
70 case OID_SHA1:
71 case OID_SHA1_WITH_RSA:
72 return HASH_SHA1;
73 case OID_SHA224:
74 case OID_SHA224_WITH_RSA:
75 return HASH_SHA224;
76 case OID_SHA256:
77 case OID_SHA256_WITH_RSA:
78 return HASH_SHA256;
79 case OID_SHA384:
80 case OID_SHA384_WITH_RSA:
81 return HASH_SHA384;
82 case OID_SHA512:
83 case OID_SHA512_WITH_RSA:
84 return HASH_SHA512;
85 case OID_SHA3_224:
86 return HASH_SHA3_224;
87 case OID_SHA3_256:
88 return HASH_SHA3_256;
89 case OID_SHA3_384:
90 return HASH_SHA3_384;
91 case OID_SHA3_512:
92 return HASH_SHA3_512;
93 default:
94 return HASH_UNKNOWN;
95 }
96 }
97
98 /*
99 * Described in header.
100 */
101 hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg)
102 {
103 switch (alg)
104 {
105 case PRF_HMAC_MD5:
106 return HASH_MD5;
107 case PRF_HMAC_SHA1:
108 case PRF_FIPS_SHA1_160:
109 case PRF_KEYED_SHA1:
110 return HASH_SHA1;
111 case PRF_HMAC_SHA2_256:
112 return HASH_SHA256;
113 case PRF_HMAC_SHA2_384:
114 return HASH_SHA384;
115 case PRF_HMAC_SHA2_512:
116 return HASH_SHA512;
117 case PRF_HMAC_TIGER:
118 case PRF_AES128_XCBC:
119 case PRF_AES128_CMAC:
120 case PRF_FIPS_DES:
121 case PRF_CAMELLIA128_XCBC:
122 case PRF_UNDEFINED:
123 break;
124 }
125 return HASH_UNKNOWN;
126 }
127
128 /*
129 * Described in header.
130 */
131 hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
132 size_t *length)
133 {
134 if (length)
135 {
136 switch (alg)
137 {
138 case AUTH_HMAC_MD5_96:
139 case AUTH_HMAC_SHA1_96:
140 case AUTH_HMAC_SHA2_256_96:
141 *length = 12;
142 break;
143 case AUTH_HMAC_MD5_128:
144 case AUTH_HMAC_SHA1_128:
145 case AUTH_HMAC_SHA2_256_128:
146 *length = 16;
147 break;
148 case AUTH_HMAC_SHA1_160:
149 *length = 20;
150 break;
151 case AUTH_HMAC_SHA2_384_192:
152 *length = 24;
153 break;
154 case AUTH_HMAC_SHA2_256_256:
155 case AUTH_HMAC_SHA2_512_256:
156 *length = 32;
157 break;
158 case AUTH_HMAC_SHA2_384_384:
159 *length = 48;
160 break;
161 case AUTH_HMAC_SHA2_512_512:
162 *length = 64;
163 break;
164 default:
165 break;
166 }
167 }
168 switch (alg)
169 {
170 case AUTH_HMAC_MD5_96:
171 case AUTH_HMAC_MD5_128:
172 case AUTH_KPDK_MD5:
173 return HASH_MD5;
174 case AUTH_HMAC_SHA1_96:
175 case AUTH_HMAC_SHA1_128:
176 case AUTH_HMAC_SHA1_160:
177 return HASH_SHA1;
178 case AUTH_HMAC_SHA2_256_96:
179 case AUTH_HMAC_SHA2_256_128:
180 case AUTH_HMAC_SHA2_256_256:
181 return HASH_SHA256;
182 case AUTH_HMAC_SHA2_384_192:
183 case AUTH_HMAC_SHA2_384_384:
184 return HASH_SHA384;
185 case AUTH_HMAC_SHA2_512_256:
186 case AUTH_HMAC_SHA2_512_512:
187 return HASH_SHA512;
188 case AUTH_AES_CMAC_96:
189 case AUTH_AES_128_GMAC:
190 case AUTH_AES_192_GMAC:
191 case AUTH_AES_256_GMAC:
192 case AUTH_AES_XCBC_96:
193 case AUTH_DES_MAC:
194 case AUTH_CAMELLIA_XCBC_96:
195 case AUTH_UNDEFINED:
196 break;
197 }
198 return HASH_UNKNOWN;
199 }
200
201 /*
202 * Described in header.
203 */
204 integrity_algorithm_t hasher_algorithm_to_integrity(hash_algorithm_t alg,
205 size_t length)
206 {
207 switch (alg)
208 {
209 case HASH_MD5:
210 switch (length)
211 {
212 case 12:
213 return AUTH_HMAC_MD5_96;
214 case 16:
215 return AUTH_HMAC_MD5_128;
216 }
217 break;
218 case HASH_SHA1:
219 switch (length)
220 {
221 case 12:
222 return AUTH_HMAC_SHA1_96;
223 case 16:
224 return AUTH_HMAC_SHA1_128;
225 case 20:
226 return AUTH_HMAC_SHA1_160;
227 }
228 break;
229 case HASH_SHA256:
230 switch (length)
231 {
232 case 12:
233 return AUTH_HMAC_SHA2_256_96;
234 case 16:
235 return AUTH_HMAC_SHA2_256_128;
236 case 32:
237 return AUTH_HMAC_SHA2_256_256;
238 }
239 break;
240 case HASH_SHA384:
241 switch (length)
242 {
243 case 24:
244 return AUTH_HMAC_SHA2_384_192;
245 case 48:
246 return AUTH_HMAC_SHA2_384_384;
247
248 }
249 break;
250 case HASH_SHA512:
251 switch (length)
252 {
253 case 32:
254 return AUTH_HMAC_SHA2_512_256;
255 case 64:
256 return AUTH_HMAC_SHA2_512_512;
257 }
258 break;
259 case HASH_MD2:
260 case HASH_MD4:
261 case HASH_SHA224:
262 case HASH_SHA3_224:
263 case HASH_SHA3_256:
264 case HASH_SHA3_384:
265 case HASH_SHA3_512:
266 case HASH_UNKNOWN:
267 break;
268 }
269 return AUTH_UNDEFINED;
270 }
271
272 /*
273 * Described in header.
274 */
275 bool hasher_algorithm_for_ikev2(hash_algorithm_t alg)
276 {
277 switch (alg)
278 {
279 case HASH_SHA1:
280 case HASH_SHA256:
281 case HASH_SHA384:
282 case HASH_SHA512:
283 return TRUE;
284 case HASH_UNKNOWN:
285 case HASH_MD2:
286 case HASH_MD4:
287 case HASH_MD5:
288 case HASH_SHA224:
289 case HASH_SHA3_224:
290 case HASH_SHA3_256:
291 case HASH_SHA3_384:
292 case HASH_SHA3_512:
293 break;
294 }
295 return FALSE;
296 }
297
298 /*
299 * Described in header.
300 */
301 int hasher_algorithm_to_oid(hash_algorithm_t alg)
302 {
303 int oid;
304
305 switch (alg)
306 {
307 case HASH_MD2:
308 oid = OID_MD2;
309 break;
310 case HASH_MD5:
311 oid = OID_MD5;
312 break;
313 case HASH_SHA1:
314 oid = OID_SHA1;
315 break;
316 case HASH_SHA224:
317 oid = OID_SHA224;
318 break;
319 case HASH_SHA256:
320 oid = OID_SHA256;
321 break;
322 case HASH_SHA384:
323 oid = OID_SHA384;
324 break;
325 case HASH_SHA512:
326 oid = OID_SHA512;
327 break;
328 case HASH_SHA3_224:
329 oid = OID_SHA3_224;
330 break;
331 case HASH_SHA3_256:
332 oid = OID_SHA3_256;
333 break;
334 case HASH_SHA3_384:
335 oid = OID_SHA3_384;
336 break;
337 case HASH_SHA3_512:
338 oid = OID_SHA3_512;
339 break;
340 default:
341 oid = OID_UNKNOWN;
342 }
343 return oid;
344 }
345
346 /*
347 * Described in header.
348 */
349 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key)
350 {
351 switch (key)
352 {
353 case KEY_RSA:
354 switch (alg)
355 {
356 case HASH_MD2:
357 return OID_MD2_WITH_RSA;
358 case HASH_MD5:
359 return OID_MD5_WITH_RSA;
360 case HASH_SHA1:
361 return OID_SHA1_WITH_RSA;
362 case HASH_SHA224:
363 return OID_SHA224_WITH_RSA;
364 case HASH_SHA256:
365 return OID_SHA256_WITH_RSA;
366 case HASH_SHA384:
367 return OID_SHA384_WITH_RSA;
368 case HASH_SHA512:
369 return OID_SHA512_WITH_RSA;
370 default:
371 return OID_UNKNOWN;
372 }
373 case KEY_ECDSA:
374 switch (alg)
375 {
376 case HASH_SHA1:
377 return OID_ECDSA_WITH_SHA1;
378 case HASH_SHA256:
379 return OID_ECDSA_WITH_SHA256;
380 case HASH_SHA384:
381 return OID_ECDSA_WITH_SHA384;
382 case HASH_SHA512:
383 return OID_ECDSA_WITH_SHA512;
384 default:
385 return OID_UNKNOWN;
386 }
387 case KEY_BLISS:
388 switch (alg)
389 {
390 case HASH_SHA256:
391 return OID_BLISS_WITH_SHA2_256;
392 case HASH_SHA384:
393 return OID_BLISS_WITH_SHA2_384;
394 case HASH_SHA512:
395 return OID_BLISS_WITH_SHA2_512;
396 case HASH_SHA3_256:
397 return OID_BLISS_WITH_SHA3_256;
398 case HASH_SHA3_384:
399 return OID_BLISS_WITH_SHA3_384;
400 case HASH_SHA3_512:
401 return OID_BLISS_WITH_SHA3_512;
402 default:
403 return OID_UNKNOWN;
404 }
405 default:
406 return OID_UNKNOWN;
407 }
408 }
409
410 /*
411 * Defined in header.
412 */
413 hash_algorithm_t hasher_from_signature_scheme(signature_scheme_t scheme)
414 {
415 switch (scheme)
416 {
417 case SIGN_UNKNOWN:
418 case SIGN_RSA_EMSA_PKCS1_NULL:
419 case SIGN_ECDSA_WITH_NULL:
420 break;
421 case SIGN_RSA_EMSA_PKCS1_MD5:
422 return HASH_MD5;
423 case SIGN_RSA_EMSA_PKCS1_SHA1:
424 case SIGN_ECDSA_WITH_SHA1_DER:
425 return HASH_SHA1;
426 case SIGN_RSA_EMSA_PKCS1_SHA224:
427 return HASH_SHA224;
428 case SIGN_RSA_EMSA_PKCS1_SHA256:
429 case SIGN_ECDSA_WITH_SHA256_DER:
430 case SIGN_ECDSA_256:
431 case SIGN_BLISS_WITH_SHA2_256:
432 return HASH_SHA256;
433 case SIGN_RSA_EMSA_PKCS1_SHA384:
434 case SIGN_ECDSA_WITH_SHA384_DER:
435 case SIGN_ECDSA_384:
436 case SIGN_BLISS_WITH_SHA2_384:
437 return HASH_SHA384;
438 case SIGN_RSA_EMSA_PKCS1_SHA512:
439 case SIGN_ECDSA_WITH_SHA512_DER:
440 case SIGN_ECDSA_521:
441 case SIGN_BLISS_WITH_SHA2_512:
442 return HASH_SHA512;
443 case SIGN_BLISS_WITH_SHA3_256:
444 return HASH_SHA3_256;
445 case SIGN_BLISS_WITH_SHA3_384:
446 return HASH_SHA3_384;
447 case SIGN_BLISS_WITH_SHA3_512:
448 return HASH_SHA3_512;
449 }
450 return HASH_UNKNOWN;
451 }