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