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 const 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 const 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 const 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 if (hmac_sha1(key, key_len, data, data_len, tmphash) < 0)
67 os_memcpy(hash, tmphash, 12);
69 case AUTH_HMAC_MD5_96:
72 if (hmac_md5(key, key_len, data, data_len, tmphash) < 0)
74 os_memcpy(hash, tmphash, 12);
84 const struct ikev2_prf_alg * ikev2_get_prf(int id)
88 for (i = 0; i < NUM_PRF_ALGS; i++) {
89 if (ikev2_prf_algs[i].id == id)
90 return &ikev2_prf_algs[i];
97 int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
98 size_t num_elem, const u8 *addr[], const size_t *len,
103 return hmac_sha1_vector(key, key_len, num_elem, addr, len,
106 return hmac_md5_vector(key, key_len, num_elem, addr, len, hash);
113 int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
114 const u8 *data, size_t data_len,
115 u8 *out, size_t out_len)
117 u8 hash[IKEV2_MAX_HASH_LEN];
122 const struct ikev2_prf_alg *prf;
125 prf = ikev2_get_prf(alg);
128 hash_len = prf->hash_len;
143 res = ikev2_prf_hash(alg, key, key_len, 2, &addr[1],
146 res = ikev2_prf_hash(alg, key, key_len, 3, addr, len,
151 if ((int) clen > end - pos)
153 os_memcpy(pos, hash, clen);
162 const struct ikev2_encr_alg * ikev2_get_encr(int id)
166 for (i = 0; i < NUM_ENCR_ALGS; i++) {
167 if (ikev2_encr_algs[i].id == id)
168 return &ikev2_encr_algs[i];
175 int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
176 const u8 *plain, u8 *crypt, size_t len)
178 struct crypto_cipher *cipher;
183 encr_alg = CRYPTO_CIPHER_ALG_3DES;
186 encr_alg = CRYPTO_CIPHER_ALG_AES;
189 wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
193 cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
194 if (cipher == NULL) {
195 wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
199 if (crypto_cipher_encrypt(cipher, plain, crypt, len) < 0) {
200 wpa_printf(MSG_INFO, "IKEV2: Encryption failed");
201 crypto_cipher_deinit(cipher);
204 crypto_cipher_deinit(cipher);
210 int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
211 const u8 *crypt, u8 *plain, size_t len)
213 struct crypto_cipher *cipher;
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_decrypt(cipher, crypt, plain, len) < 0) {
235 wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
236 crypto_cipher_deinit(cipher);
239 crypto_cipher_deinit(cipher);
245 int ikev2_parse_payloads(struct ikev2_payloads *payloads,
246 u8 next_payload, const u8 *pos, const u8 *end)
248 const struct ikev2_payload_hdr *phdr;
250 os_memset(payloads, 0, sizeof(*payloads));
252 while (next_payload != IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) {
253 unsigned int plen, pdatalen, left;
255 wpa_printf(MSG_DEBUG, "IKEV2: Processing payload %u",
260 if (left < sizeof(*phdr)) {
261 wpa_printf(MSG_INFO, "IKEV2: Too short message for "
262 "payload header (left=%ld)",
266 phdr = (const struct ikev2_payload_hdr *) pos;
267 plen = WPA_GET_BE16(phdr->payload_length);
268 if (plen < sizeof(*phdr) || plen > left) {
269 wpa_printf(MSG_INFO, "IKEV2: Invalid payload header "
274 wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Flags: 0x%x"
275 " Payload Length: %u",
276 phdr->next_payload, phdr->flags, plen);
278 pdata = (const u8 *) (phdr + 1);
279 pdatalen = plen - sizeof(*phdr);
281 switch (next_payload) {
282 case IKEV2_PAYLOAD_SA:
283 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Security "
285 payloads->sa = pdata;
286 payloads->sa_len = pdatalen;
288 case IKEV2_PAYLOAD_KEY_EXCHANGE:
289 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Key "
291 payloads->ke = pdata;
292 payloads->ke_len = pdatalen;
294 case IKEV2_PAYLOAD_IDi:
295 wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDi");
296 payloads->idi = pdata;
297 payloads->idi_len = pdatalen;
299 case IKEV2_PAYLOAD_IDr:
300 wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDr");
301 payloads->idr = pdata;
302 payloads->idr_len = pdatalen;
304 case IKEV2_PAYLOAD_CERTIFICATE:
305 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Certificate");
306 payloads->cert = pdata;
307 payloads->cert_len = pdatalen;
309 case IKEV2_PAYLOAD_AUTHENTICATION:
310 wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
312 payloads->auth = pdata;
313 payloads->auth_len = pdatalen;
315 case IKEV2_PAYLOAD_NONCE:
316 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Nonce");
317 payloads->nonce = pdata;
318 payloads->nonce_len = pdatalen;
320 case IKEV2_PAYLOAD_ENCRYPTED:
321 wpa_printf(MSG_DEBUG, "IKEV2: Payload: Encrypted");
322 payloads->encrypted = pdata;
323 payloads->encrypted_len = pdatalen;
325 case IKEV2_PAYLOAD_NOTIFICATION:
326 wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
328 payloads->notification = pdata;
329 payloads->notification_len = pdatalen;
332 if (phdr->flags & IKEV2_PAYLOAD_FLAGS_CRITICAL) {
333 wpa_printf(MSG_INFO, "IKEV2: Unsupported "
334 "critical payload %u - reject the "
335 "entire message", next_payload);
338 wpa_printf(MSG_DEBUG, "IKEV2: Skipped "
339 "unsupported payload %u",
344 if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&
347 * Next Payload in the case of Encrypted Payload is
348 * actually the payload type for the first embedded
351 payloads->encr_next_payload = phdr->next_payload;
352 next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;
354 next_payload = phdr->next_payload;
360 wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "
369 int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
370 const u8 *ID, size_t ID_len, u8 ID_type,
371 struct ikev2_keys *keys, int initiator,
372 const u8 *shared_secret, size_t shared_secret_len,
373 const u8 *nonce, size_t nonce_len,
374 const u8 *key_pad, size_t key_pad_len,
377 size_t sign_len, buf_len;
378 u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];
379 const struct ikev2_prf_alg *prf;
380 const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;
382 prf = ikev2_get_prf(prf_alg);
383 if (sign_msg == NULL || ID == NULL || SK_p == NULL ||
384 shared_secret == NULL || nonce == NULL || prf == NULL)
387 /* prf(SK_pi/r,IDi/r') */
388 buf_len = 4 + ID_len;
389 buf = os_zalloc(buf_len);
393 os_memcpy(buf + 4, ID, ID_len);
394 if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,
395 1, (const u8 **) &buf, &buf_len, hash) < 0) {
401 /* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */
402 sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;
403 sign_data = os_malloc(sign_len);
404 if (sign_data == NULL)
407 os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));
408 pos += wpabuf_len(sign_msg);
409 os_memcpy(pos, nonce, nonce_len);
411 os_memcpy(pos, hash, prf->hash_len);
413 /* AUTH = prf(prf(Shared Secret, key pad, sign_data) */
414 if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,
415 &key_pad, &key_pad_len, hash) < 0 ||
416 ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,
417 (const u8 **) &sign_data, &sign_len, auth_data) < 0)
428 u8 * ikev2_decrypt_payload(int encr_id, int integ_id,
429 struct ikev2_keys *keys, int initiator,
430 const struct ikev2_hdr *hdr,
431 const u8 *encrypted, size_t encrypted_len,
435 const u8 *pos, *end, *iv, *integ;
436 u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;
437 size_t decrypted_len, pad_len;
438 const struct ikev2_integ_alg *integ_alg;
439 const struct ikev2_encr_alg *encr_alg;
440 const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
441 const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
443 if (encrypted == NULL) {
444 wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");
448 encr_alg = ikev2_get_encr(encr_id);
449 if (encr_alg == NULL) {
450 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
453 iv_len = encr_alg->block_size;
455 integ_alg = ikev2_get_integ(integ_id);
456 if (integ_alg == NULL) {
457 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
461 if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {
462 wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "
469 end = encrypted + encrypted_len;
470 integ = end - integ_alg->hash_len;
473 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
476 if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
478 integ - (const u8 *) hdr, hash) < 0) {
479 wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "
483 if (os_memcmp_const(integ, hash, integ_alg->hash_len) != 0) {
484 wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "
490 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
494 decrypted_len = integ - pos;
495 decrypted = os_malloc(decrypted_len);
496 if (decrypted == NULL)
499 if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,
500 decrypted, decrypted_len) < 0) {
505 pad_len = decrypted[decrypted_len - 1];
506 if (decrypted_len < pad_len + 1) {
507 wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "
513 decrypted_len -= pad_len + 1;
515 *res_len = decrypted_len;
520 void ikev2_update_hdr(struct wpabuf *msg)
522 struct ikev2_hdr *hdr;
524 /* Update lenth field in HDR */
525 hdr = wpabuf_mhead(msg);
526 WPA_PUT_BE32(hdr->length, wpabuf_len(msg));
530 int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
531 int initiator, struct wpabuf *msg,
532 struct wpabuf *plain, u8 next_payload)
534 struct ikev2_payload_hdr *phdr;
536 size_t iv_len, pad_len;
538 const struct ikev2_integ_alg *integ_alg;
539 const struct ikev2_encr_alg *encr_alg;
540 const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
541 const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
543 wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");
545 /* Encr - RFC 4306, Sect. 3.14 */
547 encr_alg = ikev2_get_encr(encr_id);
548 if (encr_alg == NULL) {
549 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
552 iv_len = encr_alg->block_size;
554 integ_alg = ikev2_get_integ(integ_id);
555 if (integ_alg == NULL) {
556 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
561 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
566 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
570 phdr = wpabuf_put(msg, sizeof(*phdr));
571 phdr->next_payload = next_payload;
574 iv = wpabuf_put(msg, iv_len);
575 if (random_get_bytes(iv, iv_len)) {
576 wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");
580 pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;
581 if (pad_len == iv_len)
583 wpabuf_put(plain, pad_len);
584 wpabuf_put_u8(plain, pad_len);
586 if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,
587 wpabuf_head(plain), wpabuf_mhead(plain),
588 wpabuf_len(plain)) < 0)
591 wpabuf_put_buf(msg, plain);
593 /* Need to update all headers (Length fields) prior to hash func */
594 icv = wpabuf_put(msg, integ_alg->hash_len);
595 plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
596 WPA_PUT_BE16(phdr->payload_length, plen);
598 ikev2_update_hdr(msg);
600 return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
602 wpabuf_len(msg) - integ_alg->hash_len, icv);
608 int ikev2_keys_set(struct ikev2_keys *keys)
610 return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&
611 keys->SK_er && keys->SK_pi && keys->SK_pr;
615 void ikev2_free_keys(struct ikev2_keys *keys)
618 os_free(keys->SK_ai);
619 os_free(keys->SK_ar);
620 os_free(keys->SK_ei);
621 os_free(keys->SK_er);
622 os_free(keys->SK_pi);
623 os_free(keys->SK_pr);
624 keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =
625 keys->SK_pi = keys->SK_pr = NULL;
629 int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
630 const struct ikev2_integ_alg *integ,
631 const struct ikev2_encr_alg *encr,
632 const u8 *skeyseed, const u8 *data, size_t data_len,
633 struct ikev2_keys *keys)
639 * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =
640 * prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )
642 ikev2_free_keys(keys);
643 keys->SK_d_len = prf->key_len;
644 keys->SK_integ_len = integ->key_len;
645 keys->SK_encr_len = encr->key_len;
646 keys->SK_prf_len = prf->key_len;
648 keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +
649 2 * keys->SK_encr_len + 2 * keys->SK_prf_len;
650 keybuf = os_malloc(keybuf_len);
654 if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,
655 data, data_len, keybuf, keybuf_len)) {
662 keys->SK_d = os_malloc(keys->SK_d_len);
664 os_memcpy(keys->SK_d, pos, keys->SK_d_len);
665 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",
666 keys->SK_d, keys->SK_d_len);
668 pos += keys->SK_d_len;
670 keys->SK_ai = os_malloc(keys->SK_integ_len);
672 os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);
673 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",
674 keys->SK_ai, keys->SK_integ_len);
676 pos += keys->SK_integ_len;
678 keys->SK_ar = os_malloc(keys->SK_integ_len);
680 os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);
681 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",
682 keys->SK_ar, keys->SK_integ_len);
684 pos += keys->SK_integ_len;
686 keys->SK_ei = os_malloc(keys->SK_encr_len);
688 os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);
689 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",
690 keys->SK_ei, keys->SK_encr_len);
692 pos += keys->SK_encr_len;
694 keys->SK_er = os_malloc(keys->SK_encr_len);
696 os_memcpy(keys->SK_er, pos, keys->SK_encr_len);
697 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",
698 keys->SK_er, keys->SK_encr_len);
700 pos += keys->SK_encr_len;
702 keys->SK_pi = os_malloc(keys->SK_prf_len);
704 os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);
705 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",
706 keys->SK_pi, keys->SK_prf_len);
708 pos += keys->SK_prf_len;
710 keys->SK_pr = os_malloc(keys->SK_prf_len);
712 os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);
713 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",
714 keys->SK_pr, keys->SK_prf_len);
719 if (!ikev2_keys_set(keys)) {
720 ikev2_free_keys(keys);