summaryrefslogtreecommitdiff
path: root/src/libstrongswan/crypto/hashers/hasher.c
blob: 2bd55ad2e0979b634fc52dccb3a392260440512b (plain)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Copyright (C) 2012-2015 Tobias Brunner
 * Copyright (C) 2015-2017 Andreas Steffen
 * Copyright (C) 2005-2006 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "hasher.h"

#include <asn1/oid.h>
#include <credentials/keys/signature_params.h>

ENUM_BEGIN(hash_algorithm_names, HASH_SHA1, HASH_IDENTITY,
	"HASH_SHA1",
	"HASH_SHA2_256",
	"HASH_SHA2_384",
	"HASH_SHA2_512",
	"HASH_IDENTITY");
ENUM_NEXT(hash_algorithm_names, HASH_UNKNOWN, HASH_SHA3_512, HASH_IDENTITY,
	"HASH_UNKNOWN",
	"HASH_MD2",
	"HASH_MD4",
	"HASH_MD5",
	"HASH_SHA2_224",
	"HASH_SHA3_224",
	"HASH_SHA3_256",
	"HASH_SHA3_384",
	"HASH_SHA3_512");
ENUM_END(hash_algorithm_names, HASH_SHA3_512);

ENUM_BEGIN(hash_algorithm_short_names, HASH_SHA1, HASH_IDENTITY,
	"sha1",
	"sha256",
	"sha384",
	"sha512",
	"identity");
ENUM_NEXT(hash_algorithm_short_names, HASH_UNKNOWN, HASH_SHA3_512, HASH_IDENTITY,
	"unknown",
	"md2",
	"md4",
	"md5",
	"sha224",
	"sha3_224",
	"sha3_256",
	"sha3_384",
	"sha3_512");
ENUM_END(hash_algorithm_short_names, HASH_SHA3_512);

ENUM_BEGIN(hash_algorithm_short_names_upper, HASH_SHA1, HASH_IDENTITY,
	"SHA1",
	"SHA2_256",
	"SHA2_384",
	"SHA2_512",
	"IDENTITY");
ENUM_NEXT(hash_algorithm_short_names_upper, HASH_UNKNOWN, HASH_SHA3_512, HASH_IDENTITY,
	"UNKNOWN",
	"MD2",
	"MD4",
	"MD5",
	"SHA2_224",
	"SHA3_224",
	"SHA3_256",
	"SHA3_384",
	"SHA3_512");
ENUM_END(hash_algorithm_short_names_upper, HASH_SHA3_512);

/*
 * Described in header
 */
size_t hasher_hash_size(hash_algorithm_t alg)
{
	switch (alg)
	{
		case HASH_SHA1:
			return HASH_SIZE_SHA1;
		case HASH_SHA256:
			return HASH_SIZE_SHA256;
		case HASH_SHA384:
			return HASH_SIZE_SHA384;
		case HASH_SHA512:
			return HASH_SIZE_SHA512;
		case HASH_MD2:
			return HASH_SIZE_MD2;
		case HASH_MD4:
			return HASH_SIZE_MD4;
		case HASH_MD5:
			return HASH_SIZE_MD5;
		case HASH_SHA224:
			return HASH_SIZE_SHA224;
		case HASH_SHA3_224:
			return HASH_SIZE_SHA224;
		case HASH_SHA3_256:
			return HASH_SIZE_SHA256;
		case HASH_SHA3_384:
			return HASH_SIZE_SHA384;
		case HASH_SHA3_512:
			return HASH_SIZE_SHA512;
		case HASH_IDENTITY:
		case HASH_UNKNOWN:
			break;
	}
	return 0;
}

/*
 * Described in header.
 */
hash_algorithm_t hasher_algorithm_from_oid(int oid)
{
	switch (oid)
	{
		case OID_MD2:
		case OID_MD2_WITH_RSA:
			return HASH_MD2;
		case OID_MD5:
		case OID_MD5_WITH_RSA:
			return HASH_MD5;
		case OID_SHA1:
		case OID_SHA1_WITH_RSA:
			return HASH_SHA1;
		case OID_SHA224:
		case OID_SHA224_WITH_RSA:
			return HASH_SHA224;
		case OID_SHA256:
		case OID_SHA256_WITH_RSA:
			return HASH_SHA256;
		case OID_SHA384:
		case OID_SHA384_WITH_RSA:
			return HASH_SHA384;
		case OID_SHA512:
		case OID_SHA512_WITH_RSA:
			return HASH_SHA512;
		case OID_SHA3_224:
		case OID_RSASSA_PKCS1V15_WITH_SHA3_224:
			return HASH_SHA3_224;
		case OID_SHA3_256:
		case OID_RSASSA_PKCS1V15_WITH_SHA3_256:
			return HASH_SHA3_256;
		case OID_SHA3_384:
		case OID_RSASSA_PKCS1V15_WITH_SHA3_384:
			return HASH_SHA3_384;
		case OID_SHA3_512:
		case OID_RSASSA_PKCS1V15_WITH_SHA3_512:
			return HASH_SHA3_512;
		case OID_ED25519:
		case OID_ED448:
			return HASH_IDENTITY;
		default:
			return HASH_UNKNOWN;
	}
}

/*
 * Described in header.
 */
hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg)
{
	switch (alg)
	{
		case PRF_HMAC_MD5:
			return HASH_MD5;
		case PRF_HMAC_SHA1:
		case PRF_FIPS_SHA1_160:
		case PRF_KEYED_SHA1:
			return HASH_SHA1;
		case PRF_HMAC_SHA2_256:
			return HASH_SHA256;
		case PRF_HMAC_SHA2_384:
			return HASH_SHA384;
		case PRF_HMAC_SHA2_512:
			return HASH_SHA512;
		case PRF_HMAC_TIGER:
		case PRF_AES128_XCBC:
		case PRF_AES128_CMAC:
		case PRF_FIPS_DES:
		case PRF_CAMELLIA128_XCBC:
		case PRF_UNDEFINED:
			break;
	}
	return HASH_UNKNOWN;
}

/*
 * Described in header.
 */
hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
												 size_t *length)
{
	if (length)
	{
		switch (alg)
		{
			case AUTH_HMAC_MD5_96:
			case AUTH_HMAC_SHA1_96:
			case AUTH_HMAC_SHA2_256_96:
				*length = 12;
				break;
			case AUTH_HMAC_MD5_128:
			case AUTH_HMAC_SHA1_128:
			case AUTH_HMAC_SHA2_256_128:
				*length = 16;
				break;
			case AUTH_HMAC_SHA1_160:
				*length = 20;
				break;
			case AUTH_HMAC_SHA2_384_192:
				*length = 24;
				break;
			case AUTH_HMAC_SHA2_256_256:
			case AUTH_HMAC_SHA2_512_256:
				*length = 32;
				break;
			case AUTH_HMAC_SHA2_384_384:
				*length = 48;
				break;
			case AUTH_HMAC_SHA2_512_512:
				*length = 64;
				break;
			default:
				break;
		}
	}
	switch (alg)
	{
		case AUTH_HMAC_MD5_96:
		case AUTH_HMAC_MD5_128:
		case AUTH_KPDK_MD5:
			return HASH_MD5;
		case AUTH_HMAC_SHA1_96:
		case AUTH_HMAC_SHA1_128:
		case AUTH_HMAC_SHA1_160:
			return HASH_SHA1;
		case AUTH_HMAC_SHA2_256_96:
		case AUTH_HMAC_SHA2_256_128:
		case AUTH_HMAC_SHA2_256_256:
			return HASH_SHA256;
		case AUTH_HMAC_SHA2_384_192:
		case AUTH_HMAC_SHA2_384_384:
			return HASH_SHA384;
		case AUTH_HMAC_SHA2_512_256:
		case AUTH_HMAC_SHA2_512_512:
			return HASH_SHA512;
		case AUTH_AES_CMAC_96:
		case AUTH_AES_128_GMAC:
		case AUTH_AES_192_GMAC:
		case AUTH_AES_256_GMAC:
		case AUTH_AES_XCBC_96:
		case AUTH_DES_MAC:
		case AUTH_CAMELLIA_XCBC_96:
		case AUTH_UNDEFINED:
			break;
	}
	return HASH_UNKNOWN;
}

/*
 * Described in header.
 */
integrity_algorithm_t hasher_algorithm_to_integrity(hash_algorithm_t alg,
													size_t length)
{
	switch (alg)
	{
		case HASH_MD5:
			switch (length)
			{
				case 12:
					return AUTH_HMAC_MD5_96;
				case 16:
					return AUTH_HMAC_MD5_128;
			}
			break;
		case HASH_SHA1:
			switch (length)
			{
				case 12:
					return AUTH_HMAC_SHA1_96;
				case 16:
					return AUTH_HMAC_SHA1_128;
				case 20:
					return AUTH_HMAC_SHA1_160;
			}
			break;
		case HASH_SHA256:
			switch (length)
			{
				case 12:
					return AUTH_HMAC_SHA2_256_96;
				case 16:
					return AUTH_HMAC_SHA2_256_128;
				case 32:
					return AUTH_HMAC_SHA2_256_256;
			}
			break;
		case HASH_SHA384:
			switch (length)
			{
				case 24:
					return AUTH_HMAC_SHA2_384_192;
				case 48:
					return AUTH_HMAC_SHA2_384_384;

			}
			break;
		case HASH_SHA512:
			switch (length)
			{
				case 32:
					return AUTH_HMAC_SHA2_512_256;
				case 64:
					return AUTH_HMAC_SHA2_512_512;
			}
			break;
		case HASH_MD2:
		case HASH_MD4:
		case HASH_SHA224:
		case HASH_SHA3_224:
		case HASH_SHA3_256:
		case HASH_SHA3_384:
		case HASH_SHA3_512:
		case HASH_IDENTITY:
		case HASH_UNKNOWN:
			break;
	}
	return AUTH_UNDEFINED;
}

/*
 * Described in header.
 */
bool hasher_algorithm_for_ikev2(hash_algorithm_t alg)
{
	switch (alg)
	{
		case HASH_IDENTITY:
		case HASH_SHA256:
		case HASH_SHA384:
		case HASH_SHA512:
			return TRUE;
		case HASH_UNKNOWN:
		case HASH_MD2:
		case HASH_MD4:
		case HASH_MD5:
		case HASH_SHA1:
		case HASH_SHA224:
		case HASH_SHA3_224:
		case HASH_SHA3_256:
		case HASH_SHA3_384:
		case HASH_SHA3_512:
			break;
	}
	return FALSE;
}

/*
 * Described in header.
 */
int hasher_algorithm_to_oid(hash_algorithm_t alg)
{
	int oid;

	switch (alg)
	{
		case HASH_MD2:
			oid = OID_MD2;
			break;
		case HASH_MD5:
			oid = OID_MD5;
			break;
		case HASH_SHA1:
			oid = OID_SHA1;
			break;
		case HASH_SHA224:
			oid = OID_SHA224;
			break;
		case HASH_SHA256:
			oid = OID_SHA256;
			break;
		case HASH_SHA384:
			oid = OID_SHA384;
			break;
		case HASH_SHA512:
			oid = OID_SHA512;
			break;
		case HASH_SHA3_224:
			oid = OID_SHA3_224;
			break;
		case HASH_SHA3_256:
			oid = OID_SHA3_256;
			break;
		case HASH_SHA3_384:
			oid = OID_SHA3_384;
			break;
		case HASH_SHA3_512:
			oid = OID_SHA3_512;
			break;
		default:
			oid = OID_UNKNOWN;
	}
	return oid;
}

/*
 * Described in header.
 */
int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key)
{
	switch (key)
	{
		case KEY_RSA:
			switch (alg)
			{
				case HASH_MD2:
					return OID_MD2_WITH_RSA;
				case HASH_MD5:
					return OID_MD5_WITH_RSA;
				case HASH_SHA1:
					return OID_SHA1_WITH_RSA;
				case HASH_SHA224:
					return OID_SHA224_WITH_RSA;
				case HASH_SHA256:
					return OID_SHA256_WITH_RSA;
				case HASH_SHA384:
					return OID_SHA384_WITH_RSA;
				case HASH_SHA512:
					return OID_SHA512_WITH_RSA;
				case HASH_SHA3_224:
					return OID_RSASSA_PKCS1V15_WITH_SHA3_224;
				case HASH_SHA3_256:
					return OID_RSASSA_PKCS1V15_WITH_SHA3_256;
				case HASH_SHA3_384:
					return OID_RSASSA_PKCS1V15_WITH_SHA3_384;
				case HASH_SHA3_512:
					return OID_RSASSA_PKCS1V15_WITH_SHA3_512;
				default:
					return OID_UNKNOWN;
			}
		case KEY_ECDSA:
			switch (alg)
			{
				case HASH_SHA1:
					return OID_ECDSA_WITH_SHA1;
				case HASH_SHA256:
					return OID_ECDSA_WITH_SHA256;
				case HASH_SHA384:
					return OID_ECDSA_WITH_SHA384;
				case HASH_SHA512:
					return OID_ECDSA_WITH_SHA512;
				default:
					return OID_UNKNOWN;
			}
		case KEY_ED25519:
			switch (alg)
			{
				case HASH_IDENTITY:
					return OID_ED25519;
				default:
					return OID_UNKNOWN;
			}
		case KEY_ED448:
			switch (alg)
			{
				case HASH_IDENTITY:
					return OID_ED448;
				default:
					return OID_UNKNOWN;
			}
		case KEY_BLISS:
			switch (alg)
			{
				case HASH_SHA256:
					return OID_BLISS_WITH_SHA2_256;
				case HASH_SHA384:
					return OID_BLISS_WITH_SHA2_384;
				case HASH_SHA512:
					return OID_BLISS_WITH_SHA2_512;
				case HASH_SHA3_256:
					return OID_BLISS_WITH_SHA3_256;
				case HASH_SHA3_384:
					return OID_BLISS_WITH_SHA3_384;
				case HASH_SHA3_512:
					return OID_BLISS_WITH_SHA3_512;
				default:
					return OID_UNKNOWN;
			}
		default:
			return OID_UNKNOWN;
	}
}

/*
 * Defined in header.
 */
hash_algorithm_t hasher_from_signature_scheme(signature_scheme_t scheme,
											  void *params)
{
	switch (scheme)
	{
		case SIGN_UNKNOWN:
		case SIGN_RSA_EMSA_PKCS1_NULL:
		case SIGN_ECDSA_WITH_NULL:
			break;
		case SIGN_RSA_EMSA_PSS:
			if (params)
			{
				rsa_pss_params_t *pss = params;
				return pss->hash;
			}
			break;
		case SIGN_ED25519:
		case SIGN_ED448:
			return HASH_IDENTITY;
		case SIGN_RSA_EMSA_PKCS1_MD5:
			return HASH_MD5;
		case SIGN_RSA_EMSA_PKCS1_SHA1:
		case SIGN_ECDSA_WITH_SHA1_DER:
			return HASH_SHA1;
		case SIGN_RSA_EMSA_PKCS1_SHA2_224:
			return HASH_SHA224;
		case SIGN_RSA_EMSA_PKCS1_SHA2_256:
		case SIGN_ECDSA_WITH_SHA256_DER:
		case SIGN_ECDSA_256:
		case SIGN_BLISS_WITH_SHA2_256:
			return HASH_SHA256;
		case SIGN_RSA_EMSA_PKCS1_SHA2_384:
		case SIGN_ECDSA_WITH_SHA384_DER:
		case SIGN_ECDSA_384:
		case SIGN_BLISS_WITH_SHA2_384:
			return HASH_SHA384;
		case SIGN_RSA_EMSA_PKCS1_SHA2_512:
		case SIGN_ECDSA_WITH_SHA512_DER:
		case SIGN_ECDSA_521:
		case SIGN_BLISS_WITH_SHA2_512:
			return HASH_SHA512;
		case SIGN_RSA_EMSA_PKCS1_SHA3_224:
			return HASH_SHA3_224;
		case SIGN_RSA_EMSA_PKCS1_SHA3_256:
		case SIGN_BLISS_WITH_SHA3_256:
			return HASH_SHA3_256;
		case SIGN_RSA_EMSA_PKCS1_SHA3_384:
		case SIGN_BLISS_WITH_SHA3_384:
			return HASH_SHA3_384;
		case SIGN_RSA_EMSA_PKCS1_SHA3_512:
		case SIGN_BLISS_WITH_SHA3_512:
			return HASH_SHA3_512;
	}
	return HASH_UNKNOWN;
}