fb0cff7b491a4ed127a76905880ad657084053de
[strongswan.git] / src / pluto / crypto.c
1 /* crypto interfaces
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier
3 * Copyright (C) 2007-2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <freeswan.h>
17
18 #include "constants.h"
19 #include "defs.h"
20 #include "crypto.h"
21 #include "log.h"
22
23 static struct encrypt_desc encrypt_desc_3des =
24 {
25 algo_type: IKE_ALG_ENCRYPT,
26 algo_id: OAKLEY_3DES_CBC,
27 algo_next: NULL,
28
29 enc_blocksize: DES_BLOCK_SIZE,
30 keydeflen: DES_BLOCK_SIZE * 3 * BITS_PER_BYTE,
31 keyminlen: DES_BLOCK_SIZE * 3 * BITS_PER_BYTE,
32 keymaxlen: DES_BLOCK_SIZE * 3 * BITS_PER_BYTE,
33 };
34
35 #define AES_KEY_MIN_LEN 128
36 #define AES_KEY_DEF_LEN 128
37 #define AES_KEY_MAX_LEN 256
38
39 static struct encrypt_desc encrypt_desc_aes =
40 {
41 algo_type: IKE_ALG_ENCRYPT,
42 algo_id: OAKLEY_AES_CBC,
43 algo_next: NULL,
44
45 enc_blocksize: AES_BLOCK_SIZE,
46 keyminlen: AES_KEY_MIN_LEN,
47 keydeflen: AES_KEY_DEF_LEN,
48 keymaxlen: AES_KEY_MAX_LEN,
49 };
50
51 #define CAMELLIA_KEY_MIN_LEN 128
52 #define CAMELLIA_KEY_DEF_LEN 128
53 #define CAMELLIA_KEY_MAX_LEN 256
54
55 static struct encrypt_desc encrypt_desc_camellia =
56 {
57 algo_type: IKE_ALG_ENCRYPT,
58 algo_id: OAKLEY_CAMELLIA_CBC,
59 algo_next: NULL,
60
61 enc_blocksize: CAMELLIA_BLOCK_SIZE,
62 keyminlen: CAMELLIA_KEY_MIN_LEN,
63 keydeflen: CAMELLIA_KEY_DEF_LEN,
64 keymaxlen: CAMELLIA_KEY_MAX_LEN,
65 };
66
67 #define BLOWFISH_KEY_MIN_LEN 128
68 #define BLOWFISH_KEY_MAX_LEN 448
69
70 static struct encrypt_desc encrypt_desc_blowfish =
71 {
72 algo_type: IKE_ALG_ENCRYPT,
73 algo_id: OAKLEY_BLOWFISH_CBC,
74 algo_next: NULL,
75
76 enc_blocksize: BLOWFISH_BLOCK_SIZE,
77 keyminlen: BLOWFISH_KEY_MIN_LEN,
78 keydeflen: BLOWFISH_KEY_MIN_LEN,
79 keymaxlen: BLOWFISH_KEY_MAX_LEN,
80 };
81
82 #define SERPENT_KEY_MIN_LEN 128
83 #define SERPENT_KEY_DEF_LEN 128
84 #define SERPENT_KEY_MAX_LEN 256
85
86 static struct encrypt_desc encrypt_desc_serpent =
87 {
88 algo_type: IKE_ALG_ENCRYPT,
89 algo_id: OAKLEY_SERPENT_CBC,
90 algo_next: NULL,
91
92 enc_blocksize: SERPENT_BLOCK_SIZE,
93 keyminlen: SERPENT_KEY_MIN_LEN,
94 keydeflen: SERPENT_KEY_DEF_LEN,
95 keymaxlen: SERPENT_KEY_MAX_LEN,
96 };
97
98 #define TWOFISH_KEY_MIN_LEN 128
99 #define TWOFISH_KEY_DEF_LEN 128
100 #define TWOFISH_KEY_MAX_LEN 256
101
102 static struct encrypt_desc encrypt_desc_twofish =
103 {
104 algo_type: IKE_ALG_ENCRYPT,
105 algo_id: OAKLEY_TWOFISH_CBC,
106 algo_next: NULL,
107
108 enc_blocksize: TWOFISH_BLOCK_SIZE,
109 keydeflen: TWOFISH_KEY_MIN_LEN,
110 keyminlen: TWOFISH_KEY_DEF_LEN,
111 keymaxlen: TWOFISH_KEY_MAX_LEN,
112 };
113
114 static struct encrypt_desc encrypt_desc_twofish_ssh =
115 {
116 algo_type: IKE_ALG_ENCRYPT,
117 algo_id: OAKLEY_TWOFISH_CBC_SSH,
118 algo_next: NULL,
119
120 enc_blocksize: TWOFISH_BLOCK_SIZE,
121 keydeflen: TWOFISH_KEY_MIN_LEN,
122 keyminlen: TWOFISH_KEY_DEF_LEN,
123 keymaxlen: TWOFISH_KEY_MAX_LEN,
124 };
125
126 static struct hash_desc hash_desc_md5 =
127 {
128 algo_type: IKE_ALG_HASH,
129 algo_id: OAKLEY_MD5,
130 algo_next: NULL,
131 hash_digest_size: HASH_SIZE_MD5,
132 };
133
134 static struct hash_desc hash_desc_sha1 =
135 {
136 algo_type: IKE_ALG_HASH,
137 algo_id: OAKLEY_SHA,
138 algo_next: NULL,
139 hash_digest_size: HASH_SIZE_SHA1,
140 };
141
142 static struct hash_desc hash_desc_sha2_256 = {
143 algo_type: IKE_ALG_HASH,
144 algo_id: OAKLEY_SHA2_256,
145 algo_next: NULL,
146 hash_digest_size: HASH_SIZE_SHA256,
147 };
148
149 static struct hash_desc hash_desc_sha2_384 = {
150 algo_type: IKE_ALG_HASH,
151 algo_id: OAKLEY_SHA2_384,
152 algo_next: NULL,
153 hash_digest_size: HASH_SIZE_SHA384,
154 };
155
156 static struct hash_desc hash_desc_sha2_512 = {
157 algo_type: IKE_ALG_HASH,
158 algo_id: OAKLEY_SHA2_512,
159 algo_next: NULL,
160 hash_digest_size: HASH_SIZE_SHA512,
161 };
162
163 const struct dh_desc unset_group = {
164 algo_type: IKE_ALG_DH_GROUP,
165 algo_id: MODP_NONE,
166 algo_next: NULL,
167 ke_size: 0
168 };
169
170 static struct dh_desc dh_desc_modp_1024 = {
171 algo_type: IKE_ALG_DH_GROUP,
172 algo_id: MODP_1024_BIT,
173 algo_next: NULL,
174 ke_size: 1024 / BITS_PER_BYTE
175 };
176
177 static struct dh_desc dh_desc_modp_1536 = {
178 algo_type: IKE_ALG_DH_GROUP,
179 algo_id: MODP_1536_BIT,
180 algo_next: NULL,
181 ke_size: 1536 / BITS_PER_BYTE
182 };
183
184 static struct dh_desc dh_desc_modp_2048 = {
185 algo_type: IKE_ALG_DH_GROUP,
186 algo_id: MODP_2048_BIT,
187 algo_next: NULL,
188 ke_size: 2048 / BITS_PER_BYTE
189 };
190
191 static struct dh_desc dh_desc_modp_3072 = {
192 algo_type: IKE_ALG_DH_GROUP,
193 algo_id: MODP_3072_BIT,
194 algo_next: NULL,
195 ke_size: 3072 / BITS_PER_BYTE
196 };
197
198 static struct dh_desc dh_desc_modp_4096 = {
199 algo_type: IKE_ALG_DH_GROUP,
200 algo_id: MODP_4096_BIT,
201 algo_next: NULL,
202 ke_size: 4096 / BITS_PER_BYTE
203 };
204
205 static struct dh_desc dh_desc_modp_6144 = {
206 algo_type: IKE_ALG_DH_GROUP,
207 algo_id: MODP_6144_BIT,
208 algo_next: NULL,
209 ke_size: 6144 / BITS_PER_BYTE
210 };
211
212 static struct dh_desc dh_desc_modp_8192 = {
213 algo_type: IKE_ALG_DH_GROUP,
214 algo_id: MODP_8192_BIT,
215 algo_next: NULL,
216 ke_size: 8192 / BITS_PER_BYTE
217 };
218
219 static struct dh_desc dh_desc_ecp_256 = {
220 algo_type: IKE_ALG_DH_GROUP,
221 algo_id: ECP_256_BIT,
222 algo_next: NULL,
223 ke_size: 2*256 / BITS_PER_BYTE
224 };
225
226 static struct dh_desc dh_desc_ecp_384 = {
227 algo_type: IKE_ALG_DH_GROUP,
228 algo_id: ECP_384_BIT,
229 algo_next: NULL,
230 ke_size: 2*384 / BITS_PER_BYTE
231 };
232
233 static struct dh_desc dh_desc_ecp_521 = {
234 algo_type: IKE_ALG_DH_GROUP,
235 algo_id: ECP_521_BIT,
236 algo_next: NULL,
237 ke_size: 2*528 / BITS_PER_BYTE
238 };
239
240 static struct dh_desc dh_desc_ecp_192 = {
241 algo_type: IKE_ALG_DH_GROUP,
242 algo_id: ECP_192_BIT,
243 algo_next: NULL,
244 ke_size: 2*192 / BITS_PER_BYTE
245 };
246
247 static struct dh_desc dh_desc_ecp_224 = {
248 algo_type: IKE_ALG_DH_GROUP,
249 algo_id: ECP_224_BIT,
250 algo_next: NULL,
251 ke_size: 2*224 / BITS_PER_BYTE
252 };
253
254 bool init_crypto(void)
255 {
256 enumerator_t *enumerator;
257 encryption_algorithm_t encryption_alg;
258 hash_algorithm_t hash_alg;
259 diffie_hellman_group_t dh_group;
260 bool no_md5 = TRUE;
261 bool no_sha1 = TRUE;
262
263 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
264 while (enumerator->enumerate(enumerator, &hash_alg))
265 {
266 const struct hash_desc *desc;
267
268 switch (hash_alg)
269 {
270 case HASH_SHA1:
271 desc = &hash_desc_sha1;
272 no_sha1 = FALSE;
273 break;
274 case HASH_SHA256:
275 desc = &hash_desc_sha2_256;
276 break;
277 case HASH_SHA384:
278 desc = &hash_desc_sha2_384;
279 break;
280 case HASH_SHA512:
281 desc = &hash_desc_sha2_512;
282 break;
283 case HASH_MD5:
284 desc = &hash_desc_md5;
285 no_md5 = FALSE;
286 break;
287 default:
288 continue;
289 }
290 ike_alg_add((struct ike_alg *)desc);
291 }
292 enumerator->destroy(enumerator);
293
294 if (no_sha1 || no_md5)
295 {
296 plog("pluto cannot run without a %s%s%s hasher",
297 (no_sha1) ? "SHA-1" : "",
298 (no_sha1 && no_md5) ? " and " : "",
299 (no_md5) ? "MD5" : "");
300 return FALSE;
301 }
302
303 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
304 while (enumerator->enumerate(enumerator, &encryption_alg))
305 {
306 const struct encrypt_desc *desc;
307
308 switch (encryption_alg)
309 {
310 case ENCR_3DES:
311 desc = &encrypt_desc_3des;
312 break;
313 case ENCR_BLOWFISH:
314 desc = &encrypt_desc_blowfish;
315 break;
316 case ENCR_AES_CBC:
317 desc = &encrypt_desc_aes;
318 break;
319 case ENCR_CAMELLIA_CBC:
320 desc = &encrypt_desc_camellia;
321 break;
322 case ENCR_TWOFISH_CBC:
323 desc = &encrypt_desc_twofish;
324 ike_alg_add((struct ike_alg *)&encrypt_desc_twofish_ssh);
325 break;
326 case ENCR_SERPENT_CBC:
327 desc = &encrypt_desc_serpent;
328 break;
329 default:
330 continue;
331 }
332 ike_alg_add((struct ike_alg *)desc);
333 }
334 enumerator->destroy(enumerator);
335
336 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
337 while (enumerator->enumerate(enumerator, &dh_group))
338 {
339 const struct dh_desc *desc;
340
341 switch (dh_group)
342 {
343 case MODP_1024_BIT:
344 desc = &dh_desc_modp_1024;
345 break;
346 case MODP_1536_BIT:
347 desc = &dh_desc_modp_1536;
348 break;
349 case MODP_2048_BIT:
350 desc = &dh_desc_modp_2048;
351 break;
352 case MODP_3072_BIT:
353 desc = &dh_desc_modp_3072;
354 break;
355 case MODP_4096_BIT:
356 desc = &dh_desc_modp_4096;
357 break;
358 case MODP_6144_BIT:
359 desc = &dh_desc_modp_6144;
360 break;
361 case MODP_8192_BIT:
362 desc = &dh_desc_modp_8192;
363 break;
364 case ECP_256_BIT:
365 desc = &dh_desc_ecp_256;
366 break;
367 case ECP_384_BIT:
368 desc = &dh_desc_ecp_384;
369 break;
370 case ECP_521_BIT:
371 desc = &dh_desc_ecp_521;
372 break;
373 case ECP_192_BIT:
374 desc = &dh_desc_ecp_192;
375 break;
376 case ECP_224_BIT:
377 desc = &dh_desc_ecp_224;
378 break;
379 default:
380 continue;
381 }
382 ike_alg_add((struct ike_alg *)desc);
383 }
384 enumerator->destroy(enumerator);
385 return TRUE;
386 }
387
388 void free_crypto(void)
389 {
390 /* currently nothing to do */
391 }
392
393 /**
394 * Converts IKEv1 encryption algorithm name to crypter name
395 */
396 encryption_algorithm_t oakley_to_encryption_algorithm(int alg)
397 {
398 switch (alg)
399 {
400 case OAKLEY_DES_CBC:
401 return ENCR_DES;
402 case OAKLEY_IDEA_CBC:
403 return ENCR_IDEA;
404 case OAKLEY_BLOWFISH_CBC:
405 return ENCR_BLOWFISH;
406 case OAKLEY_RC5_R16_B64_CBC:
407 return ENCR_RC5;
408 case OAKLEY_3DES_CBC:
409 return ENCR_3DES;
410 case OAKLEY_CAST_CBC:
411 return ENCR_CAST;
412 case OAKLEY_AES_CBC:
413 return ENCR_AES_CBC;
414 case OAKLEY_CAMELLIA_CBC:
415 return ENCR_CAMELLIA_CBC;
416 case OAKLEY_SERPENT_CBC:
417 return ENCR_SERPENT_CBC;
418 case OAKLEY_TWOFISH_CBC:
419 case OAKLEY_TWOFISH_CBC_SSH:
420 return ENCR_TWOFISH_CBC;
421 default:
422 return ENCR_UNDEFINED;
423 }
424 }
425
426 /**
427 * Converts IKEv1 hash algorithm name to hasher name
428 */
429 hash_algorithm_t oakley_to_hash_algorithm(int alg)
430 {
431 switch (alg)
432 {
433 case OAKLEY_MD5:
434 return HASH_MD5;
435 case OAKLEY_SHA:
436 return HASH_SHA1;
437 case OAKLEY_SHA2_256:
438 return HASH_SHA256;
439 case OAKLEY_SHA2_384:
440 return HASH_SHA384;
441 case OAKLEY_SHA2_512:
442 return HASH_SHA512;
443 default:
444 return HASH_UNKNOWN;
445 }
446 }
447
448 /**
449 * Converts IKEv1 hash algorithm name to IKEv2 prf name
450 */
451 pseudo_random_function_t oakley_to_prf(int alg)
452 {
453 switch (alg)
454 {
455 case OAKLEY_MD5:
456 return PRF_HMAC_MD5;
457 case OAKLEY_SHA:
458 return PRF_HMAC_SHA1;
459 case OAKLEY_SHA2_256:
460 return PRF_HMAC_SHA2_256;
461 case OAKLEY_SHA2_384:
462 return PRF_HMAC_SHA2_384;
463 case OAKLEY_SHA2_512:
464 return PRF_HMAC_SHA2_512;
465 default:
466 return PRF_UNDEFINED;
467 }
468 }
469
470 /**
471 * Maps IKEv1 authentication method to IKEv2 signature scheme
472 */
473 signature_scheme_t oakley_to_signature_scheme(int method)
474 {
475 switch (method)
476 {
477 case OAKLEY_RSA_SIG:
478 case XAUTHInitRSA:
479 case XAUTHRespRSA:
480 return SIGN_RSA_EMSA_PKCS1_NULL;
481 case OAKLEY_ECDSA_256:
482 case OAKLEY_ECDSA_384:
483 case OAKLEY_ECDSA_521:
484 return SIGN_ECDSA_WITH_NULL;
485 default:
486 return SIGN_UNKNOWN;
487 }
488 }
489
490 /**
491 * Converts IKEv2 encryption to IKEv1 encryption algorithm
492 */
493 int oakley_from_encryption_algorithm(encryption_algorithm_t alg)
494 {
495 switch (alg)
496 {
497 case ENCR_DES:
498 return OAKLEY_DES_CBC;
499 case ENCR_3DES:
500 return OAKLEY_3DES_CBC;
501 case ENCR_RC5:
502 return OAKLEY_RC5_R16_B64_CBC;
503 case ENCR_IDEA:
504 return OAKLEY_IDEA_CBC;
505 case ENCR_CAST:
506 return OAKLEY_CAST_CBC;
507 case ENCR_BLOWFISH:
508 return OAKLEY_BLOWFISH_CBC;
509 case ENCR_AES_CBC:
510 return OAKLEY_AES_CBC;
511 case ENCR_CAMELLIA_CBC:
512 return OAKLEY_CAMELLIA_CBC;
513 case ENCR_SERPENT_CBC:
514 return OAKLEY_SERPENT_CBC;
515 case ENCR_TWOFISH_CBC:
516 return OAKLEY_TWOFISH_CBC;
517 default:
518 return 0;
519 }
520 }
521
522 /**
523 * Converts IKEv2 integrity to IKEv1 hash algorithm
524 */
525 int oakley_from_integrity_algorithm(integrity_algorithm_t alg)
526 {
527 switch (alg)
528 {
529 case AUTH_HMAC_MD5_96:
530 return OAKLEY_MD5;
531 case AUTH_HMAC_SHA1_96:
532 return OAKLEY_SHA;
533 case AUTH_HMAC_SHA2_256_128:
534 return OAKLEY_SHA2_256;
535 case AUTH_HMAC_SHA2_384_192:
536 return OAKLEY_SHA2_384;
537 case AUTH_HMAC_SHA2_512_256:
538 return OAKLEY_SHA2_512;
539 default:
540 return 0;
541 }
542 }
543
544 /**
545 * Converts IKEv2 encryption to IKEv1 ESP encryption algorithm
546 */
547 int esp_from_encryption_algorithm(encryption_algorithm_t alg)
548 {
549 switch (alg)
550 {
551 case ENCR_DES:
552 return ESP_DES;
553 case ENCR_3DES:
554 return ESP_3DES;
555 case ENCR_RC5:
556 return ESP_RC5;
557 case ENCR_IDEA:
558 return ESP_IDEA;
559 case ENCR_CAST:
560 return ESP_CAST;
561 case ENCR_BLOWFISH:
562 return ESP_BLOWFISH;
563 case ENCR_NULL:
564 return ESP_NULL;
565 case ENCR_AES_CBC:
566 return ESP_AES;
567 case ENCR_AES_CTR:
568 return ESP_AES_CTR;
569 case ENCR_AES_CCM_ICV8:
570 return ESP_AES_CCM_8;
571 case ENCR_AES_CCM_ICV12:
572 return ESP_AES_CCM_12;
573 case ENCR_AES_CCM_ICV16:
574 return ESP_AES_CCM_16;
575 case ENCR_AES_GCM_ICV8:
576 return ESP_AES_GCM_8;
577 case ENCR_AES_GCM_ICV12:
578 return ESP_AES_GCM_12;
579 case ENCR_AES_GCM_ICV16:
580 return ESP_AES_GCM_16;
581 case ENCR_CAMELLIA_CBC:
582 return ESP_CAMELLIA;
583 case ENCR_NULL_AUTH_AES_GMAC:
584 return ESP_AES_GMAC;
585 case ENCR_SERPENT_CBC:
586 return ESP_SERPENT;
587 case ENCR_TWOFISH_CBC:
588 return ESP_TWOFISH;
589 default:
590 return 0;
591 }
592 }
593
594 /**
595 * Converts IKEv2 integrity to IKEv1 ESP authentication algorithm
596 */
597 int esp_from_integrity_algorithm(integrity_algorithm_t alg)
598 {
599 switch (alg)
600 {
601 case AUTH_HMAC_MD5_96:
602 return AUTH_ALGORITHM_HMAC_MD5;
603 case AUTH_HMAC_SHA1_96:
604 return AUTH_ALGORITHM_HMAC_SHA1;
605 case AUTH_AES_XCBC_96:
606 return AUTH_ALGORITHM_AES_XCBC_MAC;
607 case AUTH_HMAC_SHA2_256_96:
608 return AUTH_ALGORITHM_HMAC_SHA2_256_96;
609 case AUTH_HMAC_SHA2_256_128:
610 return AUTH_ALGORITHM_HMAC_SHA2_256;
611 case AUTH_HMAC_SHA2_384_192:
612 return AUTH_ALGORITHM_HMAC_SHA2_384;
613 case AUTH_HMAC_SHA2_512_256:
614 return AUTH_ALGORITHM_HMAC_SHA2_512;
615 case AUTH_AES_128_GMAC:
616 return AUTH_ALGORITHM_AES_128_GMAC;
617 case AUTH_AES_192_GMAC:
618 return AUTH_ALGORITHM_AES_192_GMAC;
619 case AUTH_AES_256_GMAC:
620 return AUTH_ALGORITHM_AES_256_GMAC;
621 default:
622 return 0;
623 }
624 }