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