prepare CAMELLIA_CCM ESP encryption
[strongswan.git] / src / charon / sa / keymat.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * 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 "keymat.h"
17
18 #include <daemon.h>
19 #include <crypto/prf_plus.h>
20
21 typedef struct private_keymat_t private_keymat_t;
22
23 /**
24 * Private data of an keymat_t object.
25 */
26 struct private_keymat_t {
27
28 /**
29 * Public keymat_t interface.
30 */
31 keymat_t public;
32
33 /**
34 * IKE_SA Role, initiator or responder
35 */
36 bool initiator;
37
38 /**
39 * inbound signer (verify)
40 */
41 signer_t *signer_in;
42
43 /**
44 * outbound signer (sign)
45 */
46 signer_t *signer_out;
47
48 /**
49 * inbound crypter (decrypt)
50 */
51 crypter_t *crypter_in;
52
53 /**
54 * outbound crypter (encrypt)
55 */
56 crypter_t *crypter_out;
57
58 /**
59 * General purpose PRF
60 */
61 prf_t *prf;
62
63 /**
64 * Negotiated PRF algorithm
65 */
66 pseudo_random_function_t prf_alg;
67
68 /**
69 * Key to derive key material from for CHILD_SAs, rekeying
70 */
71 chunk_t skd;
72
73 /**
74 * Key to build outging authentication data (SKp)
75 */
76 chunk_t skp_build;
77
78 /**
79 * Key to verify incoming authentication data (SKp)
80 */
81 chunk_t skp_verify;
82 };
83
84 typedef struct keylen_entry_t keylen_entry_t;
85
86 /**
87 * Implicit key length for an algorithm
88 */
89 struct keylen_entry_t {
90 /** IKEv2 algorithm identifier */
91 int algo;
92 /** key length in bits */
93 int len;
94 };
95
96 #define END_OF_LIST -1
97
98 /**
99 * Keylen for encryption algos
100 */
101 keylen_entry_t keylen_enc[] = {
102 {ENCR_DES, 64},
103 {ENCR_3DES, 192},
104 {END_OF_LIST, 0}
105 };
106
107 /**
108 * Keylen for integrity algos
109 */
110 keylen_entry_t keylen_int[] = {
111 {AUTH_HMAC_MD5_96, 128},
112 {AUTH_HMAC_SHA1_96, 160},
113 {AUTH_HMAC_SHA2_256_128, 256},
114 {AUTH_HMAC_SHA2_384_192, 384},
115 {AUTH_HMAC_SHA2_512_256, 512},
116 {AUTH_AES_XCBC_96, 128},
117 {END_OF_LIST, 0}
118 };
119
120 /**
121 * Lookup key length of an algorithm
122 */
123 static int lookup_keylen(keylen_entry_t *list, int algo)
124 {
125 while (list->algo != END_OF_LIST)
126 {
127 if (algo == list->algo)
128 {
129 return list->len;
130 }
131 list++;
132 }
133 return 0;
134 }
135
136 /**
137 * Implementation of keymat_t.create_dh
138 */
139 static diffie_hellman_t* create_dh(private_keymat_t *this,
140 diffie_hellman_group_t group)
141 {
142 return lib->crypto->create_dh(lib->crypto, group);;
143 }
144
145 /**
146 * Implementation of keymat_t.derive_keys
147 */
148 static bool derive_ike_keys(private_keymat_t *this, proposal_t *proposal,
149 diffie_hellman_t *dh, chunk_t nonce_i,
150 chunk_t nonce_r, ike_sa_id_t *id,
151 pseudo_random_function_t rekey_function,
152 chunk_t rekey_skd)
153 {
154 chunk_t skeyseed, key, secret, full_nonce, fixed_nonce, prf_plus_seed;
155 chunk_t spi_i, spi_r;
156 crypter_t *crypter_i, *crypter_r;
157 signer_t *signer_i, *signer_r;
158 prf_plus_t *prf_plus;
159 u_int16_t alg, key_size;
160 prf_t *rekey_prf = NULL;
161
162 spi_i = chunk_alloca(sizeof(u_int64_t));
163 spi_r = chunk_alloca(sizeof(u_int64_t));
164
165 if (dh->get_shared_secret(dh, &secret) != SUCCESS)
166 {
167 return FALSE;
168 }
169
170 /* Create SAs general purpose PRF first, we may use it here */
171 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
172 {
173 DBG1(DBG_IKE, "no %N selected",
174 transform_type_names, PSEUDO_RANDOM_FUNCTION);
175 return FALSE;
176 }
177 this->prf_alg = alg;
178 this->prf = lib->crypto->create_prf(lib->crypto, alg);
179 if (this->prf == NULL)
180 {
181 DBG1(DBG_IKE, "%N %N not supported!",
182 transform_type_names, PSEUDO_RANDOM_FUNCTION,
183 pseudo_random_function_names, alg);
184 return FALSE;
185 }
186 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
187 /* full nonce is used as seed for PRF+ ... */
188 full_nonce = chunk_cat("cc", nonce_i, nonce_r);
189 /* but the PRF may need a fixed key which only uses the first bytes of
190 * the nonces. */
191 switch (alg)
192 {
193 case PRF_AES128_XCBC:
194 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
195 * not and therefore fixed key semantics apply to XCBC for key
196 * derivation. */
197 key_size = this->prf->get_key_size(this->prf)/2;
198 nonce_i.len = min(nonce_i.len, key_size);
199 nonce_r.len = min(nonce_r.len, key_size);
200 break;
201 default:
202 /* all other algorithms use variable key length, full nonce */
203 break;
204 }
205 fixed_nonce = chunk_cat("cc", nonce_i, nonce_r);
206 *((u_int64_t*)spi_i.ptr) = id->get_initiator_spi(id);
207 *((u_int64_t*)spi_r.ptr) = id->get_responder_spi(id);
208 prf_plus_seed = chunk_cat("ccc", full_nonce, spi_i, spi_r);
209
210 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
211 *
212 * if we are rekeying, SKEYSEED is built on another way
213 */
214 if (rekey_function == PRF_UNDEFINED) /* not rekeying */
215 {
216 /* SKEYSEED = prf(Ni | Nr, g^ir) */
217 this->prf->set_key(this->prf, fixed_nonce);
218 this->prf->allocate_bytes(this->prf, secret, &skeyseed);
219 this->prf->set_key(this->prf, skeyseed);
220 prf_plus = prf_plus_create(this->prf, prf_plus_seed);
221 }
222 else
223 {
224 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
225 * use OLD SAs PRF functions for both prf_plus and prf */
226 rekey_prf = lib->crypto->create_prf(lib->crypto, rekey_function);
227 if (!rekey_prf)
228 {
229 DBG1(DBG_IKE, "PRF of old SA %N not supported!",
230 pseudo_random_function_names, rekey_function);
231 chunk_free(&full_nonce);
232 chunk_free(&fixed_nonce);
233 chunk_clear(&prf_plus_seed);
234 return FALSE;
235 }
236 secret = chunk_cat("mc", secret, full_nonce);
237 rekey_prf->set_key(rekey_prf, rekey_skd);
238 rekey_prf->allocate_bytes(rekey_prf, secret, &skeyseed);
239 rekey_prf->set_key(rekey_prf, skeyseed);
240 prf_plus = prf_plus_create(rekey_prf, prf_plus_seed);
241 }
242 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
243
244 chunk_clear(&skeyseed);
245 chunk_clear(&secret);
246 chunk_free(&full_nonce);
247 chunk_free(&fixed_nonce);
248 chunk_clear(&prf_plus_seed);
249
250 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
251
252 /* SK_d is used for generating CHILD_SA key mat => store for later use */
253 key_size = this->prf->get_key_size(this->prf);
254 prf_plus->allocate_bytes(prf_plus, key_size, &this->skd);
255 DBG4(DBG_IKE, "Sk_d secret %B", &this->skd);
256
257 /* SK_ai/SK_ar used for integrity protection => signer_in/signer_out */
258 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL))
259 {
260 DBG1(DBG_IKE, "no %N selected",
261 transform_type_names, INTEGRITY_ALGORITHM);
262 prf_plus->destroy(prf_plus);
263 DESTROY_IF(rekey_prf);
264 return FALSE;
265 }
266 signer_i = lib->crypto->create_signer(lib->crypto, alg);
267 signer_r = lib->crypto->create_signer(lib->crypto, alg);
268 if (signer_i == NULL || signer_r == NULL)
269 {
270 DBG1(DBG_IKE, "%N %N not supported!",
271 transform_type_names, INTEGRITY_ALGORITHM,
272 integrity_algorithm_names ,alg);
273 prf_plus->destroy(prf_plus);
274 DESTROY_IF(rekey_prf);
275 return FALSE;
276 }
277 key_size = signer_i->get_key_size(signer_i);
278
279 prf_plus->allocate_bytes(prf_plus, key_size, &key);
280 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
281 signer_i->set_key(signer_i, key);
282 chunk_clear(&key);
283
284 prf_plus->allocate_bytes(prf_plus, key_size, &key);
285 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
286 signer_r->set_key(signer_r, key);
287 chunk_clear(&key);
288
289 if (this->initiator)
290 {
291 this->signer_in = signer_r;
292 this->signer_out = signer_i;
293 }
294 else
295 {
296 this->signer_in = signer_i;
297 this->signer_out = signer_r;
298 }
299
300 /* SK_ei/SK_er used for encryption => crypter_in/crypter_out */
301 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &key_size))
302 {
303 DBG1(DBG_IKE, "no %N selected",
304 transform_type_names, ENCRYPTION_ALGORITHM);
305 prf_plus->destroy(prf_plus);
306 DESTROY_IF(rekey_prf);
307 return FALSE;
308 }
309 crypter_i = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
310 crypter_r = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
311 if (crypter_i == NULL || crypter_r == NULL)
312 {
313 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
314 transform_type_names, ENCRYPTION_ALGORITHM,
315 encryption_algorithm_names, alg, key_size);
316 prf_plus->destroy(prf_plus);
317 DESTROY_IF(rekey_prf);
318 return FALSE;
319 }
320 key_size = crypter_i->get_key_size(crypter_i);
321
322 prf_plus->allocate_bytes(prf_plus, key_size, &key);
323 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
324 crypter_i->set_key(crypter_i, key);
325 chunk_clear(&key);
326
327 prf_plus->allocate_bytes(prf_plus, key_size, &key);
328 DBG4(DBG_IKE, "Sk_er secret %B", &key);
329 crypter_r->set_key(crypter_r, key);
330 chunk_clear(&key);
331
332 if (this->initiator)
333 {
334 this->crypter_in = crypter_r;
335 this->crypter_out = crypter_i;
336 }
337 else
338 {
339 this->crypter_in = crypter_i;
340 this->crypter_out = crypter_r;
341 }
342
343 /* SK_pi/SK_pr used for authentication => stored for later */
344 key_size = this->prf->get_key_size(this->prf);
345 prf_plus->allocate_bytes(prf_plus, key_size, &key);
346 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
347 if (this->initiator)
348 {
349 this->skp_build = key;
350 }
351 else
352 {
353 this->skp_verify = key;
354 }
355 prf_plus->allocate_bytes(prf_plus, key_size, &key);
356 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
357 if (this->initiator)
358 {
359 this->skp_verify = key;
360 }
361 else
362 {
363 this->skp_build = key;
364 }
365
366 /* all done, prf_plus not needed anymore */
367 prf_plus->destroy(prf_plus);
368 DESTROY_IF(rekey_prf);
369
370 return TRUE;
371 }
372
373 /**
374 * Implementation of keymat_t.derive_child_keys
375 */
376 static bool derive_child_keys(private_keymat_t *this,
377 proposal_t *proposal, diffie_hellman_t *dh,
378 chunk_t nonce_i, chunk_t nonce_r,
379 chunk_t *encr_i, chunk_t *integ_i,
380 chunk_t *encr_r, chunk_t *integ_r)
381 {
382 u_int16_t enc_alg, int_alg, enc_size = 0, int_size = 0;
383 chunk_t seed, secret = chunk_empty;
384 prf_plus_t *prf_plus;
385
386 if (dh)
387 {
388 if (dh->get_shared_secret(dh, &secret) != SUCCESS)
389 {
390 return FALSE;
391 }
392 DBG4(DBG_CHD, "DH secret %B", &secret);
393 }
394 seed = chunk_cata("mcc", secret, nonce_i, nonce_r);
395 DBG4(DBG_CHD, "seed %B", &seed);
396
397 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
398 &enc_alg, &enc_size))
399 {
400 DBG2(DBG_CHD, " using %N for encryption",
401 encryption_algorithm_names, enc_alg);
402
403 if (!enc_size)
404 {
405 enc_size = lookup_keylen(keylen_enc, enc_alg);
406 }
407 if (enc_alg != ENCR_NULL && !enc_size)
408 {
409 DBG1(DBG_CHD, "no keylength defined for %N",
410 encryption_algorithm_names, enc_alg);
411 return FALSE;
412 }
413 /* to bytes */
414 enc_size /= 8;
415
416 /* CCM/GCM/CTR needs additional bytes */
417 switch (enc_alg)
418 {
419 case ENCR_AES_CCM_ICV8:
420 case ENCR_AES_CCM_ICV12:
421 case ENCR_AES_CCM_ICV16:
422 case ENCR_CAMELLIA_CCM_ICV8:
423 case ENCR_CAMELLIA_CCM_ICV12:
424 case ENCR_CAMELLIA_CCM_ICV16:
425 enc_size += 3;
426 break;
427 case ENCR_AES_GCM_ICV8:
428 case ENCR_AES_GCM_ICV12:
429 case ENCR_AES_GCM_ICV16:
430 case ENCR_AES_CTR:
431 enc_size += 4;
432 break;
433 default:
434 break;
435 }
436 }
437
438 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
439 &int_alg, &int_size))
440 {
441 DBG2(DBG_CHD, " using %N for integrity",
442 integrity_algorithm_names, int_alg);
443
444 if (!int_size)
445 {
446 int_size = lookup_keylen(keylen_int, int_alg);
447 }
448 if (!int_size)
449 {
450 DBG1(DBG_CHD, "no keylength defined for %N",
451 integrity_algorithm_names, int_alg);
452 return FALSE;
453 }
454 /* to bytes */
455 int_size /= 8;
456 }
457
458 this->prf->set_key(this->prf, this->skd);
459 prf_plus = prf_plus_create(this->prf, seed);
460
461 prf_plus->allocate_bytes(prf_plus, enc_size, encr_i);
462 prf_plus->allocate_bytes(prf_plus, int_size, integ_i);
463 prf_plus->allocate_bytes(prf_plus, enc_size, encr_r);
464 prf_plus->allocate_bytes(prf_plus, int_size, integ_r);
465
466 prf_plus->destroy(prf_plus);
467
468 if (enc_size)
469 {
470 DBG4(DBG_CHD, "encryption initiator key %B", encr_i);
471 DBG4(DBG_CHD, "encryption responder key %B", encr_r);
472 }
473 if (int_size)
474 {
475 DBG4(DBG_CHD, "integrity initiator key %B", integ_i);
476 DBG4(DBG_CHD, "integrity responder key %B", integ_r);
477 }
478 return TRUE;
479 }
480
481 /**
482 * Implementation of keymat_t.get_skd
483 */
484 static pseudo_random_function_t get_skd(private_keymat_t *this, chunk_t *skd)
485 {
486 *skd = this->skd;
487 return this->prf_alg;
488 }
489
490 /**
491 * Implementation of keymat_t.get_signer
492 */
493 static signer_t* get_signer(private_keymat_t *this, bool in)
494 {
495 return in ? this->signer_in : this->signer_out;
496 }
497
498 /**
499 * Implementation of keymat_t.get_crypter
500 */
501 static crypter_t* get_crypter(private_keymat_t *this, bool in)
502 {
503 return in ? this->crypter_in : this->crypter_out;
504 }
505
506 /**
507 * Implementation of keymat_t.get_auth_octets
508 */
509 static chunk_t get_auth_octets(private_keymat_t *this, bool verify,
510 chunk_t ike_sa_init, chunk_t nonce,
511 identification_t *id)
512 {
513 chunk_t chunk, idx, octets;
514 chunk_t skp;
515
516 skp = verify ? this->skp_verify : this->skp_build;
517
518 chunk = chunk_alloca(4);
519 memset(chunk.ptr, 0, chunk.len);
520 chunk.ptr[0] = id->get_type(id);
521 idx = chunk_cata("cc", chunk, id->get_encoding(id));
522
523 DBG3(DBG_IKE, "IDx' %B", &idx);
524 DBG3(DBG_IKE, "SK_p %B", &skp);
525 this->prf->set_key(this->prf, skp);
526 this->prf->allocate_bytes(this->prf, idx, &chunk);
527
528 octets = chunk_cat("ccm", ike_sa_init, nonce, chunk);
529 DBG3(DBG_IKE, "octets = message + nonce + prf(Sk_px, IDx') %B", &octets);
530 return octets;
531 }
532
533 /**
534 * Key pad for the AUTH method SHARED_KEY_MESSAGE_INTEGRITY_CODE.
535 */
536 #define IKEV2_KEY_PAD "Key Pad for IKEv2"
537 #define IKEV2_KEY_PAD_LENGTH 17
538
539 /**
540 * Implementation of keymat_t.get_psk_sig
541 */
542 static chunk_t get_psk_sig(private_keymat_t *this, bool verify,
543 chunk_t ike_sa_init, chunk_t nonce, chunk_t secret,
544 identification_t *id)
545 {
546 chunk_t key_pad, key, sig, octets;
547
548 if (!secret.len)
549 { /* EAP uses SK_p if no MSK has been established */
550 secret = verify ? this->skp_verify : this->skp_build;
551 }
552 octets = get_auth_octets(this, verify, ike_sa_init, nonce, id);
553 /* AUTH = prf(prf(Shared Secret,"Key Pad for IKEv2"), <msg octets>) */
554 key_pad = chunk_create(IKEV2_KEY_PAD, IKEV2_KEY_PAD_LENGTH);
555 this->prf->set_key(this->prf, secret);
556 this->prf->allocate_bytes(this->prf, key_pad, &key);
557 this->prf->set_key(this->prf, key);
558 this->prf->allocate_bytes(this->prf, octets, &sig);
559 DBG4(DBG_IKE, "secret %B", &secret);
560 DBG4(DBG_IKE, "prf(secret, keypad) %B", &key);
561 DBG3(DBG_IKE, "AUTH = prf(prf(secret, keypad), octets) %B", &sig);
562 chunk_free(&octets);
563 chunk_free(&key);
564
565 return sig;
566 }
567
568 /**
569 * Implementation of keymat_t.destroy.
570 */
571 static void destroy(private_keymat_t *this)
572 {
573 DESTROY_IF(this->signer_in);
574 DESTROY_IF(this->signer_out);
575 DESTROY_IF(this->crypter_in);
576 DESTROY_IF(this->crypter_out);
577 DESTROY_IF(this->prf);
578 chunk_clear(&this->skd);
579 chunk_clear(&this->skp_verify);
580 chunk_clear(&this->skp_build);
581 free(this);
582 }
583
584 /**
585 * See header
586 */
587 keymat_t *keymat_create(bool initiator)
588 {
589 private_keymat_t *this = malloc_thing(private_keymat_t);
590
591 this->public.create_dh = (diffie_hellman_t*(*)(keymat_t*, diffie_hellman_group_t group))create_dh;
592 this->public.derive_ike_keys = (bool(*)(keymat_t*, proposal_t *proposal, diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r, ike_sa_id_t *id, pseudo_random_function_t,chunk_t))derive_ike_keys;
593 this->public.derive_child_keys = (bool(*)(keymat_t*, proposal_t *proposal, diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r, chunk_t *encr_i, chunk_t *integ_i, chunk_t *encr_r, chunk_t *integ_r))derive_child_keys;
594 this->public.get_skd = (pseudo_random_function_t(*)(keymat_t*, chunk_t *skd))get_skd;
595 this->public.get_signer = (signer_t*(*)(keymat_t*, bool in))get_signer;
596 this->public.get_crypter = (crypter_t*(*)(keymat_t*, bool in))get_crypter;
597 this->public.get_auth_octets = (chunk_t(*)(keymat_t *, bool verify, chunk_t ike_sa_init, chunk_t nonce, identification_t *id))get_auth_octets;
598 this->public.get_psk_sig = (chunk_t(*)(keymat_t*, bool verify, chunk_t ike_sa_init, chunk_t nonce, chunk_t secret, identification_t *id))get_psk_sig;
599 this->public.destroy = (void(*)(keymat_t*))destroy;
600
601 this->initiator = initiator;
602
603 this->signer_in = NULL;
604 this->signer_out = NULL;
605 this->crypter_in = NULL;
606 this->crypter_out = NULL;
607 this->prf = NULL;
608 this->prf_alg = PRF_UNDEFINED;
609 this->skd = chunk_empty;
610 this->skp_verify = chunk_empty;
611 this->skp_build = chunk_empty;
612
613 return &this->public;
614 }
615