13c260e60Schristos /*
23c260e60Schristos * EAP server/peer: EAP-EKE shared routines
33c260e60Schristos * Copyright (c) 2011-2013, Jouni Malinen <j@w1.fi>
43c260e60Schristos *
53c260e60Schristos * This software may be distributed under the terms of the BSD license.
63c260e60Schristos * See README for more details.
73c260e60Schristos */
83c260e60Schristos
93c260e60Schristos #include "includes.h"
103c260e60Schristos
113c260e60Schristos #include "common.h"
123c260e60Schristos #include "crypto/aes.h"
133c260e60Schristos #include "crypto/aes_wrap.h"
143c260e60Schristos #include "crypto/crypto.h"
153c260e60Schristos #include "crypto/dh_groups.h"
163c260e60Schristos #include "crypto/random.h"
173c260e60Schristos #include "crypto/sha1.h"
183c260e60Schristos #include "crypto/sha256.h"
193c260e60Schristos #include "eap_common/eap_defs.h"
203c260e60Schristos #include "eap_eke_common.h"
213c260e60Schristos
223c260e60Schristos
eap_eke_dh_len(u8 group)233c260e60Schristos static int eap_eke_dh_len(u8 group)
243c260e60Schristos {
253c260e60Schristos switch (group) {
263c260e60Schristos case EAP_EKE_DHGROUP_EKE_2:
273c260e60Schristos return 128;
283c260e60Schristos case EAP_EKE_DHGROUP_EKE_5:
293c260e60Schristos return 192;
303c260e60Schristos case EAP_EKE_DHGROUP_EKE_14:
313c260e60Schristos return 256;
323c260e60Schristos case EAP_EKE_DHGROUP_EKE_15:
333c260e60Schristos return 384;
343c260e60Schristos case EAP_EKE_DHGROUP_EKE_16:
353c260e60Schristos return 512;
363c260e60Schristos }
373c260e60Schristos
383c260e60Schristos return -1;
393c260e60Schristos }
403c260e60Schristos
413c260e60Schristos
eap_eke_dhcomp_len(u8 dhgroup,u8 encr)423c260e60Schristos static int eap_eke_dhcomp_len(u8 dhgroup, u8 encr)
433c260e60Schristos {
443c260e60Schristos int dhlen;
453c260e60Schristos
463c260e60Schristos dhlen = eap_eke_dh_len(dhgroup);
4736ebd06eSchristos if (dhlen < 0 || encr != EAP_EKE_ENCR_AES128_CBC)
483c260e60Schristos return -1;
493c260e60Schristos return AES_BLOCK_SIZE + dhlen;
503c260e60Schristos }
513c260e60Schristos
523c260e60Schristos
eap_eke_dh_group(u8 group)533c260e60Schristos static const struct dh_group * eap_eke_dh_group(u8 group)
543c260e60Schristos {
553c260e60Schristos switch (group) {
563c260e60Schristos case EAP_EKE_DHGROUP_EKE_2:
573c260e60Schristos return dh_groups_get(2);
583c260e60Schristos case EAP_EKE_DHGROUP_EKE_5:
593c260e60Schristos return dh_groups_get(5);
603c260e60Schristos case EAP_EKE_DHGROUP_EKE_14:
613c260e60Schristos return dh_groups_get(14);
623c260e60Schristos case EAP_EKE_DHGROUP_EKE_15:
633c260e60Schristos return dh_groups_get(15);
643c260e60Schristos case EAP_EKE_DHGROUP_EKE_16:
653c260e60Schristos return dh_groups_get(16);
663c260e60Schristos }
673c260e60Schristos
683c260e60Schristos return NULL;
693c260e60Schristos }
703c260e60Schristos
713c260e60Schristos
eap_eke_dh_generator(u8 group)723c260e60Schristos static int eap_eke_dh_generator(u8 group)
733c260e60Schristos {
743c260e60Schristos switch (group) {
753c260e60Schristos case EAP_EKE_DHGROUP_EKE_2:
763c260e60Schristos return 5;
773c260e60Schristos case EAP_EKE_DHGROUP_EKE_5:
783c260e60Schristos return 31;
793c260e60Schristos case EAP_EKE_DHGROUP_EKE_14:
803c260e60Schristos return 11;
813c260e60Schristos case EAP_EKE_DHGROUP_EKE_15:
823c260e60Schristos return 5;
833c260e60Schristos case EAP_EKE_DHGROUP_EKE_16:
843c260e60Schristos return 5;
853c260e60Schristos }
863c260e60Schristos
873c260e60Schristos return -1;
883c260e60Schristos }
893c260e60Schristos
903c260e60Schristos
eap_eke_pnonce_len(u8 mac)913c260e60Schristos static int eap_eke_pnonce_len(u8 mac)
923c260e60Schristos {
933c260e60Schristos int mac_len;
943c260e60Schristos
953c260e60Schristos if (mac == EAP_EKE_MAC_HMAC_SHA1)
963c260e60Schristos mac_len = SHA1_MAC_LEN;
973c260e60Schristos else if (mac == EAP_EKE_MAC_HMAC_SHA2_256)
983c260e60Schristos mac_len = SHA256_MAC_LEN;
993c260e60Schristos else
1003c260e60Schristos return -1;
1013c260e60Schristos
1023c260e60Schristos return AES_BLOCK_SIZE + 16 + mac_len;
1033c260e60Schristos }
1043c260e60Schristos
1053c260e60Schristos
eap_eke_pnonce_ps_len(u8 mac)1063c260e60Schristos static int eap_eke_pnonce_ps_len(u8 mac)
1073c260e60Schristos {
1083c260e60Schristos int mac_len;
1093c260e60Schristos
1103c260e60Schristos if (mac == EAP_EKE_MAC_HMAC_SHA1)
1113c260e60Schristos mac_len = SHA1_MAC_LEN;
1123c260e60Schristos else if (mac == EAP_EKE_MAC_HMAC_SHA2_256)
1133c260e60Schristos mac_len = SHA256_MAC_LEN;
1143c260e60Schristos else
1153c260e60Schristos return -1;
1163c260e60Schristos
1173c260e60Schristos return AES_BLOCK_SIZE + 2 * 16 + mac_len;
1183c260e60Schristos }
1193c260e60Schristos
1203c260e60Schristos
eap_eke_prf_len(u8 prf)1213c260e60Schristos static int eap_eke_prf_len(u8 prf)
1223c260e60Schristos {
1233c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA1)
1243c260e60Schristos return 20;
1253c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA2_256)
1263c260e60Schristos return 32;
1273c260e60Schristos return -1;
1283c260e60Schristos }
1293c260e60Schristos
1303c260e60Schristos
eap_eke_nonce_len(u8 prf)1313c260e60Schristos static int eap_eke_nonce_len(u8 prf)
1323c260e60Schristos {
1333c260e60Schristos int prf_len;
1343c260e60Schristos
1353c260e60Schristos prf_len = eap_eke_prf_len(prf);
1363c260e60Schristos if (prf_len < 0)
1373c260e60Schristos return -1;
1383c260e60Schristos
1393c260e60Schristos if (prf_len > 2 * 16)
1403c260e60Schristos return (prf_len + 1) / 2;
1413c260e60Schristos
1423c260e60Schristos return 16;
1433c260e60Schristos }
1443c260e60Schristos
1453c260e60Schristos
eap_eke_auth_len(u8 prf)1463c260e60Schristos static int eap_eke_auth_len(u8 prf)
1473c260e60Schristos {
1483c260e60Schristos switch (prf) {
1493c260e60Schristos case EAP_EKE_PRF_HMAC_SHA1:
1503c260e60Schristos return SHA1_MAC_LEN;
1513c260e60Schristos case EAP_EKE_PRF_HMAC_SHA2_256:
1523c260e60Schristos return SHA256_MAC_LEN;
1533c260e60Schristos }
1543c260e60Schristos
1553c260e60Schristos return -1;
1563c260e60Schristos }
1573c260e60Schristos
1583c260e60Schristos
eap_eke_dh_init(u8 group,u8 * ret_priv,u8 * ret_pub)1593c260e60Schristos int eap_eke_dh_init(u8 group, u8 *ret_priv, u8 *ret_pub)
1603c260e60Schristos {
1613c260e60Schristos int generator;
1623c260e60Schristos u8 gen;
1633c260e60Schristos const struct dh_group *dh;
1643c260e60Schristos
1653c260e60Schristos generator = eap_eke_dh_generator(group);
16636ebd06eSchristos dh = eap_eke_dh_group(group);
16736ebd06eSchristos if (generator < 0 || generator > 255 || !dh)
1683c260e60Schristos return -1;
1693c260e60Schristos gen = generator;
1703c260e60Schristos
1710a73ee0aSchristos if (crypto_dh_init(gen, dh->prime, dh->prime_len, ret_priv,
1720a73ee0aSchristos ret_pub) < 0)
1733c260e60Schristos return -1;
1743c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: DH private value",
1753c260e60Schristos ret_priv, dh->prime_len);
1763c260e60Schristos wpa_hexdump(MSG_DEBUG, "EAP-EKE: DH public value",
1773c260e60Schristos ret_pub, dh->prime_len);
1783c260e60Schristos
1793c260e60Schristos return 0;
1803c260e60Schristos }
1813c260e60Schristos
1823c260e60Schristos
eap_eke_prf(u8 prf,const u8 * key,size_t key_len,const u8 * data,size_t data_len,const u8 * data2,size_t data2_len,u8 * res)1833c260e60Schristos static int eap_eke_prf(u8 prf, const u8 *key, size_t key_len, const u8 *data,
1843c260e60Schristos size_t data_len, const u8 *data2, size_t data2_len,
1853c260e60Schristos u8 *res)
1863c260e60Schristos {
1873c260e60Schristos const u8 *addr[2];
1883c260e60Schristos size_t len[2];
1893c260e60Schristos size_t num_elem = 1;
1903c260e60Schristos
1913c260e60Schristos addr[0] = data;
1923c260e60Schristos len[0] = data_len;
1933c260e60Schristos if (data2) {
1943c260e60Schristos num_elem++;
1953c260e60Schristos addr[1] = data2;
1963c260e60Schristos len[1] = data2_len;
1973c260e60Schristos }
1983c260e60Schristos
1993c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA1)
2003c260e60Schristos return hmac_sha1_vector(key, key_len, num_elem, addr, len, res);
2013c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA2_256)
2023c260e60Schristos return hmac_sha256_vector(key, key_len, num_elem, addr, len,
2033c260e60Schristos res);
2043c260e60Schristos return -1;
2053c260e60Schristos }
2063c260e60Schristos
2073c260e60Schristos
eap_eke_prf_hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * res,size_t len)2083c260e60Schristos static int eap_eke_prf_hmac_sha1(const u8 *key, size_t key_len, const u8 *data,
2093c260e60Schristos size_t data_len, u8 *res, size_t len)
2103c260e60Schristos {
2113c260e60Schristos u8 hash[SHA1_MAC_LEN];
2123c260e60Schristos u8 idx;
2133c260e60Schristos const u8 *addr[3];
2143c260e60Schristos size_t vlen[3];
2153c260e60Schristos int ret;
2163c260e60Schristos
2173c260e60Schristos idx = 0;
2183c260e60Schristos addr[0] = hash;
2193c260e60Schristos vlen[0] = SHA1_MAC_LEN;
2203c260e60Schristos addr[1] = data;
2213c260e60Schristos vlen[1] = data_len;
2223c260e60Schristos addr[2] = &idx;
2233c260e60Schristos vlen[2] = 1;
2243c260e60Schristos
2253c260e60Schristos while (len > 0) {
2263c260e60Schristos idx++;
2273c260e60Schristos if (idx == 1)
2283c260e60Schristos ret = hmac_sha1_vector(key, key_len, 2, &addr[1],
2293c260e60Schristos &vlen[1], hash);
2303c260e60Schristos else
2313c260e60Schristos ret = hmac_sha1_vector(key, key_len, 3, addr, vlen,
2323c260e60Schristos hash);
2333c260e60Schristos if (ret < 0)
2343c260e60Schristos return -1;
2353c260e60Schristos if (len > SHA1_MAC_LEN) {
2363c260e60Schristos os_memcpy(res, hash, SHA1_MAC_LEN);
2373c260e60Schristos res += SHA1_MAC_LEN;
2383c260e60Schristos len -= SHA1_MAC_LEN;
2393c260e60Schristos } else {
2403c260e60Schristos os_memcpy(res, hash, len);
2413c260e60Schristos len = 0;
2423c260e60Schristos }
2433c260e60Schristos }
2443c260e60Schristos
2453c260e60Schristos return 0;
2463c260e60Schristos }
2473c260e60Schristos
2483c260e60Schristos
eap_eke_prf_hmac_sha256(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * res,size_t len)2493c260e60Schristos static int eap_eke_prf_hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
2503c260e60Schristos size_t data_len, u8 *res, size_t len)
2513c260e60Schristos {
2523c260e60Schristos u8 hash[SHA256_MAC_LEN];
2533c260e60Schristos u8 idx;
2543c260e60Schristos const u8 *addr[3];
2553c260e60Schristos size_t vlen[3];
2563c260e60Schristos int ret;
2573c260e60Schristos
2583c260e60Schristos idx = 0;
2593c260e60Schristos addr[0] = hash;
2603c260e60Schristos vlen[0] = SHA256_MAC_LEN;
2613c260e60Schristos addr[1] = data;
2623c260e60Schristos vlen[1] = data_len;
2633c260e60Schristos addr[2] = &idx;
2643c260e60Schristos vlen[2] = 1;
2653c260e60Schristos
2663c260e60Schristos while (len > 0) {
2673c260e60Schristos idx++;
2683c260e60Schristos if (idx == 1)
2693c260e60Schristos ret = hmac_sha256_vector(key, key_len, 2, &addr[1],
2703c260e60Schristos &vlen[1], hash);
2713c260e60Schristos else
2723c260e60Schristos ret = hmac_sha256_vector(key, key_len, 3, addr, vlen,
2733c260e60Schristos hash);
2743c260e60Schristos if (ret < 0)
2753c260e60Schristos return -1;
2763c260e60Schristos if (len > SHA256_MAC_LEN) {
2773c260e60Schristos os_memcpy(res, hash, SHA256_MAC_LEN);
2783c260e60Schristos res += SHA256_MAC_LEN;
2793c260e60Schristos len -= SHA256_MAC_LEN;
2803c260e60Schristos } else {
2813c260e60Schristos os_memcpy(res, hash, len);
2823c260e60Schristos len = 0;
2833c260e60Schristos }
2843c260e60Schristos }
2853c260e60Schristos
2863c260e60Schristos return 0;
2873c260e60Schristos }
2883c260e60Schristos
2893c260e60Schristos
eap_eke_prfplus(u8 prf,const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * res,size_t len)2903c260e60Schristos static int eap_eke_prfplus(u8 prf, const u8 *key, size_t key_len,
2913c260e60Schristos const u8 *data, size_t data_len, u8 *res, size_t len)
2923c260e60Schristos {
2933c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA1)
2943c260e60Schristos return eap_eke_prf_hmac_sha1(key, key_len, data, data_len, res,
2953c260e60Schristos len);
2963c260e60Schristos if (prf == EAP_EKE_PRF_HMAC_SHA2_256)
2973c260e60Schristos return eap_eke_prf_hmac_sha256(key, key_len, data, data_len,
2983c260e60Schristos res, len);
2993c260e60Schristos return -1;
3003c260e60Schristos }
3013c260e60Schristos
3023c260e60Schristos
eap_eke_derive_key(struct eap_eke_session * sess,const u8 * password,size_t password_len,const u8 * id_s,size_t id_s_len,const u8 * id_p,size_t id_p_len,u8 * key)3033c260e60Schristos int eap_eke_derive_key(struct eap_eke_session *sess,
3043c260e60Schristos const u8 *password, size_t password_len,
3053c260e60Schristos const u8 *id_s, size_t id_s_len, const u8 *id_p,
3063c260e60Schristos size_t id_p_len, u8 *key)
3073c260e60Schristos {
3083c260e60Schristos u8 zeros[EAP_EKE_MAX_HASH_LEN];
3093c260e60Schristos u8 temp[EAP_EKE_MAX_HASH_LEN];
3103c260e60Schristos size_t key_len = 16; /* Only AES-128-CBC is used here */
3113c260e60Schristos u8 *id;
3123c260e60Schristos
3133c260e60Schristos /* temp = prf(0+, password) */
3143c260e60Schristos os_memset(zeros, 0, sess->prf_len);
3153c260e60Schristos if (eap_eke_prf(sess->prf, zeros, sess->prf_len,
3163c260e60Schristos password, password_len, NULL, 0, temp) < 0)
3173c260e60Schristos return -1;
3183c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: temp = prf(0+, password)",
3193c260e60Schristos temp, sess->prf_len);
3203c260e60Schristos
3213c260e60Schristos /* key = prf+(temp, ID_S | ID_P) */
3223c260e60Schristos id = os_malloc(id_s_len + id_p_len);
3233c260e60Schristos if (id == NULL)
3243c260e60Schristos return -1;
3253c260e60Schristos os_memcpy(id, id_s, id_s_len);
3263c260e60Schristos os_memcpy(id + id_s_len, id_p, id_p_len);
3273c260e60Schristos wpa_hexdump_ascii(MSG_DEBUG, "EAP-EKE: ID_S | ID_P",
3283c260e60Schristos id, id_s_len + id_p_len);
3293c260e60Schristos if (eap_eke_prfplus(sess->prf, temp, sess->prf_len,
3303c260e60Schristos id, id_s_len + id_p_len, key, key_len) < 0) {
3313c260e60Schristos os_free(id);
3323c260e60Schristos return -1;
3333c260e60Schristos }
3343c260e60Schristos os_free(id);
3353c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: key = prf+(temp, ID_S | ID_P)",
3363c260e60Schristos key, key_len);
3373c260e60Schristos
3383c260e60Schristos return 0;
3393c260e60Schristos }
3403c260e60Schristos
3413c260e60Schristos
eap_eke_dhcomp(struct eap_eke_session * sess,const u8 * key,const u8 * dhpub,u8 * ret_dhcomp)3423c260e60Schristos int eap_eke_dhcomp(struct eap_eke_session *sess, const u8 *key, const u8 *dhpub,
3433c260e60Schristos u8 *ret_dhcomp)
3443c260e60Schristos {
3453c260e60Schristos u8 pub[EAP_EKE_MAX_DH_LEN];
3463c260e60Schristos int dh_len;
3473c260e60Schristos u8 iv[AES_BLOCK_SIZE];
3483c260e60Schristos
3493c260e60Schristos dh_len = eap_eke_dh_len(sess->dhgroup);
3503c260e60Schristos if (dh_len < 0)
3513c260e60Schristos return -1;
3523c260e60Schristos
3533c260e60Schristos /*
3543c260e60Schristos * DHComponent = Encr(key, y)
3553c260e60Schristos *
3563c260e60Schristos * All defined DH groups use primes that have length devisible by 16, so
3573c260e60Schristos * no need to do extra padding for y (= pub).
3583c260e60Schristos */
3593c260e60Schristos if (sess->encr != EAP_EKE_ENCR_AES128_CBC)
3603c260e60Schristos return -1;
3613c260e60Schristos if (random_get_bytes(iv, AES_BLOCK_SIZE))
3623c260e60Schristos return -1;
3633c260e60Schristos wpa_hexdump(MSG_DEBUG, "EAP-EKE: IV for Encr(key, y)",
3643c260e60Schristos iv, AES_BLOCK_SIZE);
3653c260e60Schristos os_memcpy(pub, dhpub, dh_len);
3663c260e60Schristos if (aes_128_cbc_encrypt(key, iv, pub, dh_len) < 0)
3673c260e60Schristos return -1;
3683c260e60Schristos os_memcpy(ret_dhcomp, iv, AES_BLOCK_SIZE);
3693c260e60Schristos os_memcpy(ret_dhcomp + AES_BLOCK_SIZE, pub, dh_len);
3703c260e60Schristos wpa_hexdump(MSG_DEBUG, "EAP-EKE: DHComponent = Encr(key, y)",
3713c260e60Schristos ret_dhcomp, AES_BLOCK_SIZE + dh_len);
3723c260e60Schristos
3733c260e60Schristos return 0;
3743c260e60Schristos }
3753c260e60Schristos
3763c260e60Schristos
eap_eke_shared_secret(struct eap_eke_session * sess,const u8 * key,const u8 * dhpriv,const u8 * peer_dhcomp)3773c260e60Schristos int eap_eke_shared_secret(struct eap_eke_session *sess, const u8 *key,
3783c260e60Schristos const u8 *dhpriv, const u8 *peer_dhcomp)
3793c260e60Schristos {
3803c260e60Schristos u8 zeros[EAP_EKE_MAX_HASH_LEN];
3813c260e60Schristos u8 peer_pub[EAP_EKE_MAX_DH_LEN];
3823c260e60Schristos u8 modexp[EAP_EKE_MAX_DH_LEN];
3833c260e60Schristos size_t len;
3843c260e60Schristos const struct dh_group *dh;
3853c260e60Schristos
3863c260e60Schristos dh = eap_eke_dh_group(sess->dhgroup);
38736ebd06eSchristos if (sess->encr != EAP_EKE_ENCR_AES128_CBC || !dh)
3883c260e60Schristos return -1;
3893c260e60Schristos
3903c260e60Schristos /* Decrypt peer DHComponent */
3913c260e60Schristos os_memcpy(peer_pub, peer_dhcomp + AES_BLOCK_SIZE, dh->prime_len);
3923c260e60Schristos if (aes_128_cbc_decrypt(key, peer_dhcomp, peer_pub, dh->prime_len) < 0) {
3933c260e60Schristos wpa_printf(MSG_INFO, "EAP-EKE: Failed to decrypt DHComponent");
3943c260e60Schristos return -1;
3953c260e60Schristos }
3963c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Decrypted peer DH pubkey",
3973c260e60Schristos peer_pub, dh->prime_len);
3983c260e60Schristos
3993c260e60Schristos /* SharedSecret = prf(0+, g ^ (x_s * x_p) (mod p)) */
4003c260e60Schristos len = dh->prime_len;
4010a73ee0aSchristos if (crypto_dh_derive_secret(*dh->generator, dh->prime, dh->prime_len,
402*3d6c0713Schristos NULL, 0, dhpriv, dh->prime_len, peer_pub,
4030a73ee0aSchristos dh->prime_len, modexp, &len) < 0)
4043c260e60Schristos return -1;
4053c260e60Schristos if (len < dh->prime_len) {
4063c260e60Schristos size_t pad = dh->prime_len - len;
4073c260e60Schristos os_memmove(modexp + pad, modexp, len);
4083c260e60Schristos os_memset(modexp, 0, pad);
4093c260e60Schristos }
4103c260e60Schristos
4113c260e60Schristos os_memset(zeros, 0, sess->auth_len);
4123c260e60Schristos if (eap_eke_prf(sess->prf, zeros, sess->auth_len, modexp, dh->prime_len,
4133c260e60Schristos NULL, 0, sess->shared_secret) < 0)
4143c260e60Schristos return -1;
4153c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: SharedSecret",
4163c260e60Schristos sess->shared_secret, sess->auth_len);
4173c260e60Schristos
4183c260e60Schristos return 0;
4193c260e60Schristos }
4203c260e60Schristos
4213c260e60Schristos
eap_eke_derive_ke_ki(struct eap_eke_session * sess,const u8 * id_s,size_t id_s_len,const u8 * id_p,size_t id_p_len)4223c260e60Schristos int eap_eke_derive_ke_ki(struct eap_eke_session *sess,
4233c260e60Schristos const u8 *id_s, size_t id_s_len,
4243c260e60Schristos const u8 *id_p, size_t id_p_len)
4253c260e60Schristos {
4263c260e60Schristos u8 buf[EAP_EKE_MAX_KE_LEN + EAP_EKE_MAX_KI_LEN];
4273c260e60Schristos size_t ke_len, ki_len;
4283c260e60Schristos u8 *data;
4293c260e60Schristos size_t data_len;
4303c260e60Schristos const char *label = "EAP-EKE Keys";
4313c260e60Schristos size_t label_len;
4323c260e60Schristos
4333c260e60Schristos /*
4343c260e60Schristos * Ke | Ki = prf+(SharedSecret, "EAP-EKE Keys" | ID_S | ID_P)
4353c260e60Schristos * Ke = encryption key
4363c260e60Schristos * Ki = integrity protection key
4373c260e60Schristos * Length of each key depends on the selected algorithms.
4383c260e60Schristos */
4393c260e60Schristos
4403c260e60Schristos if (sess->encr == EAP_EKE_ENCR_AES128_CBC)
4413c260e60Schristos ke_len = 16;
4423c260e60Schristos else
4433c260e60Schristos return -1;
4443c260e60Schristos
4453c260e60Schristos if (sess->mac == EAP_EKE_PRF_HMAC_SHA1)
4463c260e60Schristos ki_len = 20;
4473c260e60Schristos else if (sess->mac == EAP_EKE_PRF_HMAC_SHA2_256)
4483c260e60Schristos ki_len = 32;
4493c260e60Schristos else
4503c260e60Schristos return -1;
4513c260e60Schristos
4523c260e60Schristos label_len = os_strlen(label);
4533c260e60Schristos data_len = label_len + id_s_len + id_p_len;
4543c260e60Schristos data = os_malloc(data_len);
4553c260e60Schristos if (data == NULL)
4563c260e60Schristos return -1;
4573c260e60Schristos os_memcpy(data, label, label_len);
4583c260e60Schristos os_memcpy(data + label_len, id_s, id_s_len);
4593c260e60Schristos os_memcpy(data + label_len + id_s_len, id_p, id_p_len);
4603c260e60Schristos if (eap_eke_prfplus(sess->prf, sess->shared_secret, sess->prf_len,
4613c260e60Schristos data, data_len, buf, ke_len + ki_len) < 0) {
4623c260e60Schristos os_free(data);
4633c260e60Schristos return -1;
4643c260e60Schristos }
4653c260e60Schristos
4663c260e60Schristos os_memcpy(sess->ke, buf, ke_len);
4673c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Ke", sess->ke, ke_len);
4683c260e60Schristos os_memcpy(sess->ki, buf + ke_len, ki_len);
4693c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Ki", sess->ki, ki_len);
4703c260e60Schristos
4713c260e60Schristos os_free(data);
4723c260e60Schristos return 0;
4733c260e60Schristos }
4743c260e60Schristos
4753c260e60Schristos
eap_eke_derive_ka(struct eap_eke_session * sess,const u8 * id_s,size_t id_s_len,const u8 * id_p,size_t id_p_len,const u8 * nonce_p,const u8 * nonce_s)4763c260e60Schristos int eap_eke_derive_ka(struct eap_eke_session *sess,
4773c260e60Schristos const u8 *id_s, size_t id_s_len,
4783c260e60Schristos const u8 *id_p, size_t id_p_len,
4793c260e60Schristos const u8 *nonce_p, const u8 *nonce_s)
4803c260e60Schristos {
4813c260e60Schristos u8 *data, *pos;
4823c260e60Schristos size_t data_len;
4833c260e60Schristos const char *label = "EAP-EKE Ka";
4843c260e60Schristos size_t label_len;
4853c260e60Schristos
4863c260e60Schristos /*
4873c260e60Schristos * Ka = prf+(SharedSecret, "EAP-EKE Ka" | ID_S | ID_P | Nonce_P |
4883c260e60Schristos * Nonce_S)
4893c260e60Schristos * Ka = authentication key
4903c260e60Schristos * Length of the key depends on the selected algorithms.
4913c260e60Schristos */
4923c260e60Schristos
4933c260e60Schristos label_len = os_strlen(label);
4943c260e60Schristos data_len = label_len + id_s_len + id_p_len + 2 * sess->nonce_len;
4953c260e60Schristos data = os_malloc(data_len);
4963c260e60Schristos if (data == NULL)
4973c260e60Schristos return -1;
4983c260e60Schristos pos = data;
4993c260e60Schristos os_memcpy(pos, label, label_len);
5003c260e60Schristos pos += label_len;
5013c260e60Schristos os_memcpy(pos, id_s, id_s_len);
5023c260e60Schristos pos += id_s_len;
5033c260e60Schristos os_memcpy(pos, id_p, id_p_len);
5043c260e60Schristos pos += id_p_len;
5053c260e60Schristos os_memcpy(pos, nonce_p, sess->nonce_len);
5063c260e60Schristos pos += sess->nonce_len;
5073c260e60Schristos os_memcpy(pos, nonce_s, sess->nonce_len);
5083c260e60Schristos if (eap_eke_prfplus(sess->prf, sess->shared_secret, sess->prf_len,
5093c260e60Schristos data, data_len, sess->ka, sess->prf_len) < 0) {
5103c260e60Schristos os_free(data);
5113c260e60Schristos return -1;
5123c260e60Schristos }
5133c260e60Schristos os_free(data);
5143c260e60Schristos
5153c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Ka", sess->ka, sess->prf_len);
5163c260e60Schristos
5173c260e60Schristos return 0;
5183c260e60Schristos }
5193c260e60Schristos
5203c260e60Schristos
eap_eke_derive_msk(struct eap_eke_session * sess,const u8 * id_s,size_t id_s_len,const u8 * id_p,size_t id_p_len,const u8 * nonce_p,const u8 * nonce_s,u8 * msk,u8 * emsk)5213c260e60Schristos int eap_eke_derive_msk(struct eap_eke_session *sess,
5223c260e60Schristos const u8 *id_s, size_t id_s_len,
5233c260e60Schristos const u8 *id_p, size_t id_p_len,
5243c260e60Schristos const u8 *nonce_p, const u8 *nonce_s,
5253c260e60Schristos u8 *msk, u8 *emsk)
5263c260e60Schristos {
5273c260e60Schristos u8 *data, *pos;
5283c260e60Schristos size_t data_len;
5293c260e60Schristos const char *label = "EAP-EKE Exported Keys";
5303c260e60Schristos size_t label_len;
5313c260e60Schristos u8 buf[EAP_MSK_LEN + EAP_EMSK_LEN];
5323c260e60Schristos
5333c260e60Schristos /*
5343c260e60Schristos * MSK | EMSK = prf+(SharedSecret, "EAP-EKE Exported Keys" | ID_S |
5353c260e60Schristos * ID_P | Nonce_P | Nonce_S)
5363c260e60Schristos */
5373c260e60Schristos
5383c260e60Schristos label_len = os_strlen(label);
5393c260e60Schristos data_len = label_len + id_s_len + id_p_len + 2 * sess->nonce_len;
5403c260e60Schristos data = os_malloc(data_len);
5413c260e60Schristos if (data == NULL)
5423c260e60Schristos return -1;
5433c260e60Schristos pos = data;
5443c260e60Schristos os_memcpy(pos, label, label_len);
5453c260e60Schristos pos += label_len;
5463c260e60Schristos os_memcpy(pos, id_s, id_s_len);
5473c260e60Schristos pos += id_s_len;
5483c260e60Schristos os_memcpy(pos, id_p, id_p_len);
5493c260e60Schristos pos += id_p_len;
5503c260e60Schristos os_memcpy(pos, nonce_p, sess->nonce_len);
5513c260e60Schristos pos += sess->nonce_len;
5523c260e60Schristos os_memcpy(pos, nonce_s, sess->nonce_len);
5533c260e60Schristos if (eap_eke_prfplus(sess->prf, sess->shared_secret, sess->prf_len,
5543c260e60Schristos data, data_len, buf, EAP_MSK_LEN + EAP_EMSK_LEN) <
5553c260e60Schristos 0) {
5563c260e60Schristos os_free(data);
5573c260e60Schristos return -1;
5583c260e60Schristos }
5593c260e60Schristos os_free(data);
5603c260e60Schristos
5613c260e60Schristos os_memcpy(msk, buf, EAP_MSK_LEN);
5623c260e60Schristos os_memcpy(emsk, buf + EAP_MSK_LEN, EAP_EMSK_LEN);
5633c260e60Schristos os_memset(buf, 0, sizeof(buf));
5643c260e60Schristos
5653c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: MSK", msk, EAP_MSK_LEN);
5663c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: EMSK", msk, EAP_EMSK_LEN);
5673c260e60Schristos
5683c260e60Schristos return 0;
5693c260e60Schristos }
5703c260e60Schristos
5713c260e60Schristos
eap_eke_mac(u8 mac,const u8 * key,const u8 * data,size_t data_len,u8 * res)5723c260e60Schristos static int eap_eke_mac(u8 mac, const u8 *key, const u8 *data, size_t data_len,
5733c260e60Schristos u8 *res)
5743c260e60Schristos {
5753c260e60Schristos if (mac == EAP_EKE_MAC_HMAC_SHA1)
5763c260e60Schristos return hmac_sha1(key, SHA1_MAC_LEN, data, data_len, res);
5773c260e60Schristos if (mac == EAP_EKE_MAC_HMAC_SHA2_256)
5783c260e60Schristos return hmac_sha256(key, SHA256_MAC_LEN, data, data_len, res);
5793c260e60Schristos return -1;
5803c260e60Schristos }
5813c260e60Schristos
5823c260e60Schristos
eap_eke_prot(struct eap_eke_session * sess,const u8 * data,size_t data_len,u8 * prot,size_t * prot_len)5833c260e60Schristos int eap_eke_prot(struct eap_eke_session *sess,
5843c260e60Schristos const u8 *data, size_t data_len,
5853c260e60Schristos u8 *prot, size_t *prot_len)
5863c260e60Schristos {
5873c260e60Schristos size_t block_size, icv_len, pad;
5883c260e60Schristos u8 *pos, *iv, *e;
5893c260e60Schristos
5903c260e60Schristos if (sess->encr == EAP_EKE_ENCR_AES128_CBC)
5913c260e60Schristos block_size = AES_BLOCK_SIZE;
5923c260e60Schristos else
5933c260e60Schristos return -1;
5943c260e60Schristos
5953c260e60Schristos if (sess->mac == EAP_EKE_PRF_HMAC_SHA1)
5963c260e60Schristos icv_len = SHA1_MAC_LEN;
5973c260e60Schristos else if (sess->mac == EAP_EKE_PRF_HMAC_SHA2_256)
5983c260e60Schristos icv_len = SHA256_MAC_LEN;
5993c260e60Schristos else
6003c260e60Schristos return -1;
6013c260e60Schristos
6023c260e60Schristos pad = data_len % block_size;
6033c260e60Schristos if (pad)
6043c260e60Schristos pad = block_size - pad;
6053c260e60Schristos
6063c260e60Schristos if (*prot_len < block_size + data_len + pad + icv_len) {
6073c260e60Schristos wpa_printf(MSG_INFO, "EAP-EKE: Not enough room for Prot() data");
60836ebd06eSchristos return -1;
6093c260e60Schristos }
6103c260e60Schristos pos = prot;
6113c260e60Schristos
6123c260e60Schristos if (random_get_bytes(pos, block_size))
6133c260e60Schristos return -1;
6143c260e60Schristos iv = pos;
6153c260e60Schristos wpa_hexdump(MSG_DEBUG, "EAP-EKE: IV for Prot()", iv, block_size);
6163c260e60Schristos pos += block_size;
6173c260e60Schristos
6183c260e60Schristos e = pos;
6193c260e60Schristos os_memcpy(pos, data, data_len);
6203c260e60Schristos pos += data_len;
6213c260e60Schristos if (pad) {
6223c260e60Schristos if (random_get_bytes(pos, pad))
6233c260e60Schristos return -1;
6243c260e60Schristos pos += pad;
6253c260e60Schristos }
6263c260e60Schristos
62736ebd06eSchristos if (aes_128_cbc_encrypt(sess->ke, iv, e, data_len + pad) < 0 ||
62836ebd06eSchristos eap_eke_mac(sess->mac, sess->ki, e, data_len + pad, pos) < 0)
6293c260e60Schristos return -1;
6303c260e60Schristos pos += icv_len;
6313c260e60Schristos
6323c260e60Schristos *prot_len = pos - prot;
6333c260e60Schristos return 0;
6343c260e60Schristos }
6353c260e60Schristos
6363c260e60Schristos
eap_eke_decrypt_prot(struct eap_eke_session * sess,const u8 * prot,size_t prot_len,u8 * data,size_t * data_len)6373c260e60Schristos int eap_eke_decrypt_prot(struct eap_eke_session *sess,
6383c260e60Schristos const u8 *prot, size_t prot_len,
6393c260e60Schristos u8 *data, size_t *data_len)
6403c260e60Schristos {
6413c260e60Schristos size_t block_size, icv_len;
6423c260e60Schristos u8 icv[EAP_EKE_MAX_HASH_LEN];
6433c260e60Schristos
6443c260e60Schristos if (sess->encr == EAP_EKE_ENCR_AES128_CBC)
6453c260e60Schristos block_size = AES_BLOCK_SIZE;
6463c260e60Schristos else
6473c260e60Schristos return -1;
6483c260e60Schristos
6493c260e60Schristos if (sess->mac == EAP_EKE_PRF_HMAC_SHA1)
6503c260e60Schristos icv_len = SHA1_MAC_LEN;
6513c260e60Schristos else if (sess->mac == EAP_EKE_PRF_HMAC_SHA2_256)
6523c260e60Schristos icv_len = SHA256_MAC_LEN;
6533c260e60Schristos else
6543c260e60Schristos return -1;
6553c260e60Schristos
65636ebd06eSchristos if (prot_len < 2 * block_size + icv_len ||
65736ebd06eSchristos (prot_len - icv_len) % block_size)
6583c260e60Schristos return -1;
6593c260e60Schristos
6603c260e60Schristos if (eap_eke_mac(sess->mac, sess->ki, prot + block_size,
6613c260e60Schristos prot_len - block_size - icv_len, icv) < 0)
6623c260e60Schristos return -1;
6633c260e60Schristos if (os_memcmp_const(icv, prot + prot_len - icv_len, icv_len) != 0) {
6643c260e60Schristos wpa_printf(MSG_INFO, "EAP-EKE: ICV mismatch in Prot() data");
6653c260e60Schristos return -1;
6663c260e60Schristos }
6673c260e60Schristos
6683c260e60Schristos if (*data_len < prot_len - block_size - icv_len) {
6693c260e60Schristos wpa_printf(MSG_INFO, "EAP-EKE: Not enough room for decrypted Prot() data");
6703c260e60Schristos return -1;
6713c260e60Schristos }
6723c260e60Schristos
6733c260e60Schristos *data_len = prot_len - block_size - icv_len;
6743c260e60Schristos os_memcpy(data, prot + block_size, *data_len);
6753c260e60Schristos if (aes_128_cbc_decrypt(sess->ke, prot, data, *data_len) < 0) {
6763c260e60Schristos wpa_printf(MSG_INFO, "EAP-EKE: Failed to decrypt Prot() data");
6773c260e60Schristos return -1;
6783c260e60Schristos }
6793c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Decrypted Prot() data",
6803c260e60Schristos data, *data_len);
6813c260e60Schristos
6823c260e60Schristos return 0;
6833c260e60Schristos }
6843c260e60Schristos
6853c260e60Schristos
eap_eke_auth(struct eap_eke_session * sess,const char * label,const struct wpabuf * msgs,u8 * auth)6863c260e60Schristos int eap_eke_auth(struct eap_eke_session *sess, const char *label,
6873c260e60Schristos const struct wpabuf *msgs, u8 *auth)
6883c260e60Schristos {
6893c260e60Schristos wpa_printf(MSG_DEBUG, "EAP-EKE: Auth(%s)", label);
6903c260e60Schristos wpa_hexdump_key(MSG_DEBUG, "EAP-EKE: Ka for Auth",
6913c260e60Schristos sess->ka, sess->auth_len);
6923c260e60Schristos wpa_hexdump_buf(MSG_MSGDUMP, "EAP-EKE: Messages for Auth", msgs);
6933c260e60Schristos return eap_eke_prf(sess->prf, sess->ka, sess->auth_len,
6943c260e60Schristos (const u8 *) label, os_strlen(label),
6953c260e60Schristos wpabuf_head(msgs), wpabuf_len(msgs), auth);
6963c260e60Schristos }
6973c260e60Schristos
6983c260e60Schristos
eap_eke_session_init(struct eap_eke_session * sess,u8 dhgroup,u8 encr,u8 prf,u8 mac)6993c260e60Schristos int eap_eke_session_init(struct eap_eke_session *sess, u8 dhgroup, u8 encr,
7003c260e60Schristos u8 prf, u8 mac)
7013c260e60Schristos {
7023c260e60Schristos sess->dhgroup = dhgroup;
7033c260e60Schristos sess->encr = encr;
7043c260e60Schristos sess->prf = prf;
7053c260e60Schristos sess->mac = mac;
7063c260e60Schristos
7073c260e60Schristos sess->prf_len = eap_eke_prf_len(prf);
7083c260e60Schristos sess->nonce_len = eap_eke_nonce_len(prf);
7093c260e60Schristos sess->auth_len = eap_eke_auth_len(prf);
7103c260e60Schristos sess->dhcomp_len = eap_eke_dhcomp_len(sess->dhgroup, sess->encr);
7113c260e60Schristos sess->pnonce_len = eap_eke_pnonce_len(sess->mac);
7123c260e60Schristos sess->pnonce_ps_len = eap_eke_pnonce_ps_len(sess->mac);
71336ebd06eSchristos if (sess->prf_len < 0 || sess->nonce_len < 0 || sess->auth_len < 0 ||
71436ebd06eSchristos sess->dhcomp_len < 0 || sess->pnonce_len < 0 ||
71536ebd06eSchristos sess->pnonce_ps_len < 0)
7163c260e60Schristos return -1;
7173c260e60Schristos
7183c260e60Schristos return 0;
7193c260e60Schristos }
7203c260e60Schristos
7213c260e60Schristos
eap_eke_session_clean(struct eap_eke_session * sess)7223c260e60Schristos void eap_eke_session_clean(struct eap_eke_session *sess)
7233c260e60Schristos {
7243c260e60Schristos os_memset(sess->shared_secret, 0, EAP_EKE_MAX_HASH_LEN);
7253c260e60Schristos os_memset(sess->ke, 0, EAP_EKE_MAX_KE_LEN);
7263c260e60Schristos os_memset(sess->ki, 0, EAP_EKE_MAX_KI_LEN);
7273c260e60Schristos os_memset(sess->ka, 0, EAP_EKE_MAX_KA_LEN);
7283c260e60Schristos }
729