2 * IKEv2 common routines for initiator and responder
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/crypto.h"
13 #include "crypto/md5.h"
14 #include "crypto/sha1.h"
15 #include "crypto/random.h"
16 #include "ikev2_common.h"
19 static struct ikev2_integ_alg ikev2_integ_algs[] = {
20 { AUTH_HMAC_SHA1_96, 20, 12 },
21 { AUTH_HMAC_MD5_96, 16, 12 }
24 #define NUM_INTEG_ALGS ARRAY_SIZE(ikev2_integ_algs)
27 static struct ikev2_prf_alg ikev2_prf_algs[] = {
28 { PRF_HMAC_SHA1, 20, 20 },
29 { PRF_HMAC_MD5, 16, 16 }
32 #define NUM_PRF_ALGS ARRAY_SIZE(ikev2_prf_algs)
35 static struct ikev2_encr_alg ikev2_encr_algs[] = {
36 { ENCR_AES_CBC, 16, 16 }, /* only 128-bit keys supported for now */
40 #define NUM_ENCR_ALGS ARRAY_SIZE(ikev2_encr_algs)
43 const struct ikev2_integ_alg * ikev2_get_integ(int id)
47 for (i = 0; i < NUM_INTEG_ALGS; i++) {
48 if (ikev2_integ_algs[i].id == id)
49 return &ikev2_integ_algs[i];
56 int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
57 size_t data_len, u8 *hash)
59 u8 tmphash[IKEV2_MAX_HASH_LEN];
62 case AUTH_HMAC_SHA1_96:
65 hmac_sha1(key, key_len, data, data_len, tmphash);
66 os_memcpy(hash, tmphash, 12);
68 case AUTH_HMAC_MD5_96:
71 hmac_md5(key, key_len, data, data_len, tmphash);
72 os_memcpy(hash, tmphash, 12);
82 const struct ikev2_prf_alg * ikev2_get_prf(int id)
86 for (i = 0; i < NUM_PRF_ALGS; i++) {
87 if (ikev2_prf_algs[i].id == id)
88 return &ikev2_prf_algs[i];
95 int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
96 size_t num_elem, const u8 *addr[], const size_t *len,
101 hmac_sha1_vector(key, key_len, num_elem, addr, len, hash);
104 hmac_md5_vector(key, key_len, num_elem, addr, len, hash);
114 int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
115 const u8 *data, size_t data_len,
116 u8 *out, size_t out_len)
118 u8 hash[IKEV2_MAX_HASH_LEN];
123 const struct ikev2_prf_alg *prf;
126 prf = ikev2_get_prf(alg);
129 hash_len = prf->hash_len;
144 res = ikev2_prf_hash(alg, key, key_len, 2, &addr[1],
147 res = ikev2_prf_hash(alg, key, key_len, 3, addr, len,
152 if ((int) clen > end - pos)
154 os_memcpy(pos, hash, clen);
163 const struct ikev2_encr_alg * ikev2_get_encr(int id)
167 for (i = 0; i < NUM_ENCR_ALGS; i++) {
168 if (ikev2_encr_algs[i].id == id)
169 return &ikev2_encr_algs[i];
183 void des3_key_setup(const u8 *key, struct des3_key_s *dkey);
184 void des3_encrypt(const u8 *plain, const struct des3_key_s *key, u8 *crypt);
185 void des3_decrypt(const u8 *crypt, const struct des3_key_s *key, u8 *plain);
189 int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
190 const u8 *plain, u8 *crypt, size_t len)
192 struct crypto_cipher *cipher;
196 if (alg == ENCR_3DES) {
197 struct des3_key_s des3key;
201 /* ECB mode is used incorrectly for 3DES!? */
203 wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
206 des3_key_setup(key, &des3key);
210 for (i = 0; i < blocks; i++) {
211 des3_encrypt(pos, &des3key, pos);
218 encr_alg = CRYPTO_CIPHER_ALG_3DES;
221 encr_alg = CRYPTO_CIPHER_ALG_AES;
224 wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
228 cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
229 if (cipher == NULL) {
230 wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
234 if (crypto_cipher_encrypt(cipher, plain, crypt, len) < 0) {
235 wpa_printf(MSG_INFO, "IKEV2: Encryption failed");
236 crypto_cipher_deinit(cipher);
239 crypto_cipher_deinit(cipher);
248 int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
249 const u8 *crypt, u8 *plain, size_t len)
251 struct crypto_cipher *cipher;
255 if (alg == ENCR_3DES) {
256 struct des3_key_s des3key;
259 /* ECB mode is used incorrectly for 3DES!? */
261 wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
264 des3_key_setup(key, &des3key);
267 wpa_printf(MSG_INFO, "IKEV2: Invalid encrypted "
272 for (i = 0; i < blocks; i++) {
273 des3_decrypt(crypt, &des3key, plain);
281 encr_alg = CRYPTO_CIPHER_ALG_3DES;
284 encr_alg = CRYPTO_CIPHER_ALG_AES;
287 wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
291 cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
292 if (cipher == NULL) {
293 wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
297 if (crypto_cipher_decrypt(cipher, crypt, plain, len) < 0) {
298 wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
299 crypto_cipher_deinit(cipher);
302 crypto_cipher_deinit(cipher);
311 int ikev2_parse_payloads(struct ikev2_payloads *payloads,
312 u8 next_payload, const u8 *pos, const u8 *end)
314 const struct ikev2_payload_hdr *phdr;
316 os_memset(payloads, 0, sizeof(*payloads));
318 while (next_payload != IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) {
321 wpa_printf(MSG_DEBUG, "IKEV2: Processing payload %u",
323 if (end - pos < (int) sizeof(*phdr)) {
324 wpa_printf(MSG_INFO, "IKEV2: Too short message for "
325 "payload header (left=%ld)",
328 phdr = (const struct ikev2_payload_hdr *) pos;
329 plen = WPA_GET_BE16(phdr->payload_length);
330 if (plen < (int) sizeof(*phdr) || pos + plen > end) {
331 wpa_printf(MSG_INFO, "IKEV2: Invalid payload header "
336 wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Flags: 0x%x"
337 " Payload Length: %d",
338 phdr->next_payload, phdr->flags, plen);
340 pdata = (const u8 *) (phdr + 1);
341 pdatalen = plen - sizeof(*phdr);
343 switch (next_payload) {
344 case IKEV2_PAYLOAD_SA:
345 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Security "
347 payloads->sa = pdata;
348 payloads->sa_len = pdatalen;
350 case IKEV2_PAYLOAD_KEY_EXCHANGE:
351 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Key "
353 payloads->ke = pdata;
354 payloads->ke_len = pdatalen;
356 case IKEV2_PAYLOAD_IDi:
357 wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDi");
358 payloads->idi = pdata;
359 payloads->idi_len = pdatalen;
361 case IKEV2_PAYLOAD_IDr:
362 wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDr");
363 payloads->idr = pdata;
364 payloads->idr_len = pdatalen;
366 case IKEV2_PAYLOAD_CERTIFICATE:
367 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Certificate");
368 payloads->cert = pdata;
369 payloads->cert_len = pdatalen;
371 case IKEV2_PAYLOAD_AUTHENTICATION:
372 wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
374 payloads->auth = pdata;
375 payloads->auth_len = pdatalen;
377 case IKEV2_PAYLOAD_NONCE:
378 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Nonce");
379 payloads->nonce = pdata;
380 payloads->nonce_len = pdatalen;
382 case IKEV2_PAYLOAD_ENCRYPTED:
383 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Encrypted");
384 payloads->encrypted = pdata;
385 payloads->encrypted_len = pdatalen;
387 case IKEV2_PAYLOAD_NOTIFICATION:
388 wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
390 payloads->notification = pdata;
391 payloads->notification_len = pdatalen;
394 if (phdr->flags & IKEV2_PAYLOAD_FLAGS_CRITICAL) {
395 wpa_printf(MSG_INFO, "IKEV2: Unsupported "
396 "critical payload %u - reject the "
397 "entire message", next_payload);
400 wpa_printf(MSG_DEBUG, "IKEV2: Skipped "
401 "unsupported payload %u",
406 if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&
409 * Next Payload in the case of Encrypted Payload is
410 * actually the payload type for the first embedded
413 payloads->encr_next_payload = phdr->next_payload;
414 next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;
416 next_payload = phdr->next_payload;
422 wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "
431 int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
432 const u8 *ID, size_t ID_len, u8 ID_type,
433 struct ikev2_keys *keys, int initiator,
434 const u8 *shared_secret, size_t shared_secret_len,
435 const u8 *nonce, size_t nonce_len,
436 const u8 *key_pad, size_t key_pad_len,
439 size_t sign_len, buf_len;
440 u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];
441 const struct ikev2_prf_alg *prf;
442 const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;
444 prf = ikev2_get_prf(prf_alg);
445 if (sign_msg == NULL || ID == NULL || SK_p == NULL ||
446 shared_secret == NULL || nonce == NULL || prf == NULL)
449 /* prf(SK_pi/r,IDi/r') */
450 buf_len = 4 + ID_len;
451 buf = os_zalloc(buf_len);
455 os_memcpy(buf + 4, ID, ID_len);
456 if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,
457 1, (const u8 **) &buf, &buf_len, hash) < 0) {
463 /* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */
464 sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;
465 sign_data = os_malloc(sign_len);
466 if (sign_data == NULL)
469 os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));
470 pos += wpabuf_len(sign_msg);
471 os_memcpy(pos, nonce, nonce_len);
473 os_memcpy(pos, hash, prf->hash_len);
475 /* AUTH = prf(prf(Shared Secret, key pad, sign_data) */
476 if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,
477 &key_pad, &key_pad_len, hash) < 0 ||
478 ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,
479 (const u8 **) &sign_data, &sign_len, auth_data) < 0)
490 u8 * ikev2_decrypt_payload(int encr_id, int integ_id,
491 struct ikev2_keys *keys, int initiator,
492 const struct ikev2_hdr *hdr,
493 const u8 *encrypted, size_t encrypted_len,
497 const u8 *pos, *end, *iv, *integ;
498 u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;
499 size_t decrypted_len, pad_len;
500 const struct ikev2_integ_alg *integ_alg;
501 const struct ikev2_encr_alg *encr_alg;
502 const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
503 const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
505 if (encrypted == NULL) {
506 wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");
510 encr_alg = ikev2_get_encr(encr_id);
511 if (encr_alg == NULL) {
512 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
515 iv_len = encr_alg->block_size;
517 integ_alg = ikev2_get_integ(integ_id);
518 if (integ_alg == NULL) {
519 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
523 if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {
524 wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "
531 end = encrypted + encrypted_len;
532 integ = end - integ_alg->hash_len;
535 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
538 if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
540 integ - (const u8 *) hdr, hash) < 0) {
541 wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "
545 if (os_memcmp(integ, hash, integ_alg->hash_len) != 0) {
546 wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "
552 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
556 decrypted_len = integ - pos;
557 decrypted = os_malloc(decrypted_len);
558 if (decrypted == NULL)
561 if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,
562 decrypted, decrypted_len) < 0) {
567 pad_len = decrypted[decrypted_len - 1];
568 if (decrypted_len < pad_len + 1) {
569 wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "
575 decrypted_len -= pad_len + 1;
577 *res_len = decrypted_len;
582 void ikev2_update_hdr(struct wpabuf *msg)
584 struct ikev2_hdr *hdr;
586 /* Update lenth field in HDR */
587 hdr = wpabuf_mhead(msg);
588 WPA_PUT_BE32(hdr->length, wpabuf_len(msg));
592 int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
593 int initiator, struct wpabuf *msg,
594 struct wpabuf *plain, u8 next_payload)
596 struct ikev2_payload_hdr *phdr;
598 size_t iv_len, pad_len;
600 const struct ikev2_integ_alg *integ_alg;
601 const struct ikev2_encr_alg *encr_alg;
602 const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
603 const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
605 wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");
607 /* Encr - RFC 4306, Sect. 3.14 */
609 encr_alg = ikev2_get_encr(encr_id);
610 if (encr_alg == NULL) {
611 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
614 iv_len = encr_alg->block_size;
616 integ_alg = ikev2_get_integ(integ_id);
617 if (integ_alg == NULL) {
618 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
623 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
628 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
632 phdr = wpabuf_put(msg, sizeof(*phdr));
633 phdr->next_payload = next_payload;
636 iv = wpabuf_put(msg, iv_len);
637 if (random_get_bytes(iv, iv_len)) {
638 wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");
642 pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;
643 if (pad_len == iv_len)
645 wpabuf_put(plain, pad_len);
646 wpabuf_put_u8(plain, pad_len);
648 if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,
649 wpabuf_head(plain), wpabuf_mhead(plain),
650 wpabuf_len(plain)) < 0)
653 wpabuf_put_buf(msg, plain);
655 /* Need to update all headers (Length fields) prior to hash func */
656 icv = wpabuf_put(msg, integ_alg->hash_len);
657 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
658 WPA_PUT_BE16(phdr->payload_length, plen);
660 ikev2_update_hdr(msg);
662 return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
664 wpabuf_len(msg) - integ_alg->hash_len, icv);
670 int ikev2_keys_set(struct ikev2_keys *keys)
672 return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&
673 keys->SK_er && keys->SK_pi && keys->SK_pr;
677 void ikev2_free_keys(struct ikev2_keys *keys)
680 os_free(keys->SK_ai);
681 os_free(keys->SK_ar);
682 os_free(keys->SK_ei);
683 os_free(keys->SK_er);
684 os_free(keys->SK_pi);
685 os_free(keys->SK_pr);
686 keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =
687 keys->SK_pi = keys->SK_pr = NULL;
691 int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
692 const struct ikev2_integ_alg *integ,
693 const struct ikev2_encr_alg *encr,
694 const u8 *skeyseed, const u8 *data, size_t data_len,
695 struct ikev2_keys *keys)
701 * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =
702 * prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )
704 ikev2_free_keys(keys);
705 keys->SK_d_len = prf->key_len;
706 keys->SK_integ_len = integ->key_len;
707 keys->SK_encr_len = encr->key_len;
708 keys->SK_prf_len = prf->key_len;
710 /* Uses encryption key length for SK_d; should be PRF length */
711 keys->SK_d_len = keys->SK_encr_len;
714 keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +
715 2 * keys->SK_encr_len + 2 * keys->SK_prf_len;
716 keybuf = os_malloc(keybuf_len);
720 if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,
721 data, data_len, keybuf, keybuf_len)) {
728 keys->SK_d = os_malloc(keys->SK_d_len);
730 os_memcpy(keys->SK_d, pos, keys->SK_d_len);
731 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",
732 keys->SK_d, keys->SK_d_len);
734 pos += keys->SK_d_len;
736 keys->SK_ai = os_malloc(keys->SK_integ_len);
738 os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);
739 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",
740 keys->SK_ai, keys->SK_integ_len);
742 pos += keys->SK_integ_len;
744 keys->SK_ar = os_malloc(keys->SK_integ_len);
746 os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);
747 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",
748 keys->SK_ar, keys->SK_integ_len);
750 pos += keys->SK_integ_len;
752 keys->SK_ei = os_malloc(keys->SK_encr_len);
754 os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);
755 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",
756 keys->SK_ei, keys->SK_encr_len);
758 pos += keys->SK_encr_len;
760 keys->SK_er = os_malloc(keys->SK_encr_len);
762 os_memcpy(keys->SK_er, pos, keys->SK_encr_len);
763 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",
764 keys->SK_er, keys->SK_encr_len);
766 pos += keys->SK_encr_len;
768 keys->SK_pi = os_malloc(keys->SK_prf_len);
770 os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);
771 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",
772 keys->SK_pi, keys->SK_prf_len);
774 pos += keys->SK_prf_len;
776 keys->SK_pr = os_malloc(keys->SK_prf_len);
778 os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);
779 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",
780 keys->SK_pr, keys->SK_prf_len);
785 if (!ikev2_keys_set(keys)) {
786 ikev2_free_keys(keys);