1f05cddf9SRui Paulo /* 2f05cddf9SRui Paulo * EAP server/peer: EAP-pwd shared routines 3f05cddf9SRui Paulo * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org> 4f05cddf9SRui Paulo * 5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license. 6f05cddf9SRui Paulo * See README for more details. 7f05cddf9SRui Paulo */ 8f05cddf9SRui Paulo 9f05cddf9SRui Paulo #include "includes.h" 10f05cddf9SRui Paulo #include "common.h" 114bc52338SCy Schubert #include "utils/const_time.h" 12206b73d0SCy Schubert #include "common/dragonfly.h" 13f05cddf9SRui Paulo #include "crypto/sha256.h" 14f05cddf9SRui Paulo #include "crypto/crypto.h" 15f05cddf9SRui Paulo #include "eap_defs.h" 16f05cddf9SRui Paulo #include "eap_pwd_common.h" 17f05cddf9SRui Paulo 184bc52338SCy Schubert #define MAX_ECC_PRIME_LEN 66 194bc52338SCy Schubert 204bc52338SCy Schubert 21f05cddf9SRui Paulo /* The random function H(x) = HMAC-SHA256(0^32, x) */ 22f05cddf9SRui Paulo struct crypto_hash * eap_pwd_h_init(void) 23f05cddf9SRui Paulo { 24f05cddf9SRui Paulo u8 allzero[SHA256_MAC_LEN]; 25f05cddf9SRui Paulo os_memset(allzero, 0, SHA256_MAC_LEN); 26f05cddf9SRui Paulo return crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, allzero, 27f05cddf9SRui Paulo SHA256_MAC_LEN); 28f05cddf9SRui Paulo } 29f05cddf9SRui Paulo 30f05cddf9SRui Paulo 31f05cddf9SRui Paulo void eap_pwd_h_update(struct crypto_hash *hash, const u8 *data, size_t len) 32f05cddf9SRui Paulo { 33f05cddf9SRui Paulo crypto_hash_update(hash, data, len); 34f05cddf9SRui Paulo } 35f05cddf9SRui Paulo 36f05cddf9SRui Paulo 37f05cddf9SRui Paulo void eap_pwd_h_final(struct crypto_hash *hash, u8 *digest) 38f05cddf9SRui Paulo { 39f05cddf9SRui Paulo size_t len = SHA256_MAC_LEN; 40f05cddf9SRui Paulo crypto_hash_finish(hash, digest, &len); 41f05cddf9SRui Paulo } 42f05cddf9SRui Paulo 43f05cddf9SRui Paulo 44f05cddf9SRui Paulo /* a counter-based KDF based on NIST SP800-108 */ 45f05cddf9SRui Paulo static int eap_pwd_kdf(const u8 *key, size_t keylen, const u8 *label, 46f05cddf9SRui Paulo size_t labellen, u8 *result, size_t resultbitlen) 47f05cddf9SRui Paulo { 48f05cddf9SRui Paulo struct crypto_hash *hash; 49f05cddf9SRui Paulo u8 digest[SHA256_MAC_LEN]; 50f05cddf9SRui Paulo u16 i, ctr, L; 51f05cddf9SRui Paulo size_t resultbytelen, len = 0, mdlen; 52f05cddf9SRui Paulo 53f05cddf9SRui Paulo resultbytelen = (resultbitlen + 7) / 8; 54f05cddf9SRui Paulo ctr = 0; 55f05cddf9SRui Paulo L = htons(resultbitlen); 56f05cddf9SRui Paulo while (len < resultbytelen) { 57f05cddf9SRui Paulo ctr++; 58f05cddf9SRui Paulo i = htons(ctr); 59f05cddf9SRui Paulo hash = crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, 60f05cddf9SRui Paulo key, keylen); 61f05cddf9SRui Paulo if (hash == NULL) 62f05cddf9SRui Paulo return -1; 63f05cddf9SRui Paulo if (ctr > 1) 64f05cddf9SRui Paulo crypto_hash_update(hash, digest, SHA256_MAC_LEN); 65f05cddf9SRui Paulo crypto_hash_update(hash, (u8 *) &i, sizeof(u16)); 66f05cddf9SRui Paulo crypto_hash_update(hash, label, labellen); 67f05cddf9SRui Paulo crypto_hash_update(hash, (u8 *) &L, sizeof(u16)); 68f05cddf9SRui Paulo mdlen = SHA256_MAC_LEN; 69f05cddf9SRui Paulo if (crypto_hash_finish(hash, digest, &mdlen) < 0) 70f05cddf9SRui Paulo return -1; 71f05cddf9SRui Paulo if ((len + mdlen) > resultbytelen) 72f05cddf9SRui Paulo os_memcpy(result + len, digest, resultbytelen - len); 73f05cddf9SRui Paulo else 74f05cddf9SRui Paulo os_memcpy(result + len, digest, mdlen); 75f05cddf9SRui Paulo len += mdlen; 76f05cddf9SRui Paulo } 77f05cddf9SRui Paulo 78f05cddf9SRui Paulo /* since we're expanding to a bit length, mask off the excess */ 79f05cddf9SRui Paulo if (resultbitlen % 8) { 80f05cddf9SRui Paulo u8 mask = 0xff; 81f05cddf9SRui Paulo mask <<= (8 - (resultbitlen % 8)); 82f05cddf9SRui Paulo result[resultbytelen - 1] &= mask; 83f05cddf9SRui Paulo } 84f05cddf9SRui Paulo 85f05cddf9SRui Paulo return 0; 86f05cddf9SRui Paulo } 87f05cddf9SRui Paulo 88f05cddf9SRui Paulo 8985732ac8SCy Schubert EAP_PWD_group * get_eap_pwd_group(u16 num) 9085732ac8SCy Schubert { 9185732ac8SCy Schubert EAP_PWD_group *grp; 9285732ac8SCy Schubert 93206b73d0SCy Schubert if (!dragonfly_suitable_group(num, 1)) { 944bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: unsuitable group %u", num); 954bc52338SCy Schubert return NULL; 964bc52338SCy Schubert } 9785732ac8SCy Schubert grp = os_zalloc(sizeof(EAP_PWD_group)); 9885732ac8SCy Schubert if (!grp) 9985732ac8SCy Schubert return NULL; 10085732ac8SCy Schubert grp->group = crypto_ec_init(num); 10185732ac8SCy Schubert if (!grp->group) { 10285732ac8SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC group"); 10385732ac8SCy Schubert os_free(grp); 10485732ac8SCy Schubert return NULL; 10585732ac8SCy Schubert } 10685732ac8SCy Schubert 10785732ac8SCy Schubert grp->group_num = num; 10885732ac8SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: provisioned group %d", num); 10985732ac8SCy Schubert 11085732ac8SCy Schubert return grp; 11185732ac8SCy Schubert } 11285732ac8SCy Schubert 11385732ac8SCy Schubert 114f05cddf9SRui Paulo /* 115f05cddf9SRui Paulo * compute a "random" secret point on an elliptic curve based 116f05cddf9SRui Paulo * on the password and identities. 117f05cddf9SRui Paulo */ 118f05cddf9SRui Paulo int compute_password_element(EAP_PWD_group *grp, u16 num, 119325151a3SRui Paulo const u8 *password, size_t password_len, 120325151a3SRui Paulo const u8 *id_server, size_t id_server_len, 121325151a3SRui Paulo const u8 *id_peer, size_t id_peer_len, 122325151a3SRui Paulo const u8 *token) 123f05cddf9SRui Paulo { 124206b73d0SCy Schubert struct crypto_bignum *qr = NULL, *qnr = NULL; 1254bc52338SCy Schubert u8 qr_bin[MAX_ECC_PRIME_LEN]; 1264bc52338SCy Schubert u8 qnr_bin[MAX_ECC_PRIME_LEN]; 1274bc52338SCy Schubert u8 qr_or_qnr_bin[MAX_ECC_PRIME_LEN]; 1284bc52338SCy Schubert u8 x_bin[MAX_ECC_PRIME_LEN]; 129206b73d0SCy Schubert u8 prime_bin[MAX_ECC_PRIME_LEN]; 130ec080394SCy Schubert u8 x_y[2 * MAX_ECC_PRIME_LEN]; 131ec080394SCy Schubert struct crypto_bignum *tmp2 = NULL, *y = NULL; 132f05cddf9SRui Paulo struct crypto_hash *hash; 133f05cddf9SRui Paulo unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr; 134206b73d0SCy Schubert int ret = 0, res; 1354bc52338SCy Schubert u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_* 1364bc52338SCy Schubert * mask */ 1374bc52338SCy Schubert size_t primebytelen = 0, primebitlen; 1384bc52338SCy Schubert struct crypto_bignum *x_candidate = NULL; 13985732ac8SCy Schubert const struct crypto_bignum *prime; 140206b73d0SCy Schubert u8 found_ctr = 0, is_odd = 0; 141206b73d0SCy Schubert int cmp_prime; 142206b73d0SCy Schubert unsigned int in_range; 143ec080394SCy Schubert unsigned int is_eq; 144f05cddf9SRui Paulo 14585732ac8SCy Schubert if (grp->pwe) 146f05cddf9SRui Paulo return -1; 147f05cddf9SRui Paulo 1484bc52338SCy Schubert os_memset(x_bin, 0, sizeof(x_bin)); 1494bc52338SCy Schubert 15085732ac8SCy Schubert prime = crypto_ec_get_prime(grp->group); 151206b73d0SCy Schubert primebitlen = crypto_ec_prime_len_bits(grp->group); 152206b73d0SCy Schubert primebytelen = crypto_ec_prime_len(grp->group); 153206b73d0SCy Schubert if (crypto_bignum_to_bin(prime, prime_bin, sizeof(prime_bin), 154206b73d0SCy Schubert primebytelen) < 0) 155206b73d0SCy Schubert return -1; 156f05cddf9SRui Paulo 157f05cddf9SRui Paulo if ((prfbuf = os_malloc(primebytelen)) == NULL) { 158f05cddf9SRui Paulo wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf " 159f05cddf9SRui Paulo "buffer"); 160f05cddf9SRui Paulo goto fail; 161f05cddf9SRui Paulo } 16285732ac8SCy Schubert 16385732ac8SCy Schubert /* get a random quadratic residue and nonresidue */ 164206b73d0SCy Schubert if (dragonfly_get_random_qr_qnr(prime, &qr, &qnr) < 0 || 165206b73d0SCy Schubert crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), 1664bc52338SCy Schubert primebytelen) < 0 || 1674bc52338SCy Schubert crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), 1684bc52338SCy Schubert primebytelen) < 0) 1694bc52338SCy Schubert goto fail; 17085732ac8SCy Schubert 17185732ac8SCy Schubert os_memset(prfbuf, 0, primebytelen); 17285732ac8SCy Schubert ctr = 0; 17385732ac8SCy Schubert 17485732ac8SCy Schubert /* 17585732ac8SCy Schubert * Run through the hunting-and-pecking loop 40 times to mask the time 17685732ac8SCy Schubert * necessary to find PWE. The odds of PWE not being found in 40 loops is 17785732ac8SCy Schubert * roughly 1 in 1 trillion. 17885732ac8SCy Schubert */ 17985732ac8SCy Schubert while (ctr < 40) { 180f05cddf9SRui Paulo ctr++; 181f05cddf9SRui Paulo 182f05cddf9SRui Paulo /* 183f05cddf9SRui Paulo * compute counter-mode password value and stretch to prime 184f05cddf9SRui Paulo * pwd-seed = H(token | peer-id | server-id | password | 185f05cddf9SRui Paulo * counter) 186f05cddf9SRui Paulo */ 187f05cddf9SRui Paulo hash = eap_pwd_h_init(); 188f05cddf9SRui Paulo if (hash == NULL) 189f05cddf9SRui Paulo goto fail; 190f05cddf9SRui Paulo eap_pwd_h_update(hash, token, sizeof(u32)); 191f05cddf9SRui Paulo eap_pwd_h_update(hash, id_peer, id_peer_len); 192f05cddf9SRui Paulo eap_pwd_h_update(hash, id_server, id_server_len); 193f05cddf9SRui Paulo eap_pwd_h_update(hash, password, password_len); 194f05cddf9SRui Paulo eap_pwd_h_update(hash, &ctr, sizeof(ctr)); 195f05cddf9SRui Paulo eap_pwd_h_final(hash, pwe_digest); 196f05cddf9SRui Paulo 1974bc52338SCy Schubert is_odd = const_time_select_u8( 1984bc52338SCy Schubert found, is_odd, pwe_digest[SHA256_MAC_LEN - 1] & 0x01); 199f05cddf9SRui Paulo if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN, 200f05cddf9SRui Paulo (u8 *) "EAP-pwd Hunting And Pecking", 201f05cddf9SRui Paulo os_strlen("EAP-pwd Hunting And Pecking"), 202f05cddf9SRui Paulo prfbuf, primebitlen) < 0) 203f05cddf9SRui Paulo goto fail; 2044bc52338SCy Schubert if (primebitlen % 8) 2054bc52338SCy Schubert buf_shift_right(prfbuf, primebytelen, 2064bc52338SCy Schubert 8 - primebitlen % 8); 207206b73d0SCy Schubert cmp_prime = const_time_memcmp(prfbuf, prime_bin, primebytelen); 208206b73d0SCy Schubert /* Create a const_time mask for selection based on prf result 209206b73d0SCy Schubert * being smaller than prime. */ 210206b73d0SCy Schubert in_range = const_time_fill_msb((unsigned int) cmp_prime); 211206b73d0SCy Schubert /* The algorithm description would skip the next steps if 212206b73d0SCy Schubert * cmp_prime >= 0, but go through them regardless to minimize 213206b73d0SCy Schubert * externally observable differences in behavior. */ 214f05cddf9SRui Paulo 21585732ac8SCy Schubert crypto_bignum_deinit(x_candidate, 1); 21685732ac8SCy Schubert x_candidate = crypto_bignum_init_set(prfbuf, primebytelen); 21785732ac8SCy Schubert if (!x_candidate) { 21885732ac8SCy Schubert wpa_printf(MSG_INFO, 21985732ac8SCy Schubert "EAP-pwd: unable to create x_candidate"); 22085732ac8SCy Schubert goto fail; 22185732ac8SCy Schubert } 222f05cddf9SRui Paulo 2234bc52338SCy Schubert wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate", 224f05cddf9SRui Paulo prfbuf, primebytelen); 2254bc52338SCy Schubert const_time_select_bin(found, x_bin, prfbuf, primebytelen, 2264bc52338SCy Schubert x_bin); 227f05cddf9SRui Paulo 228f05cddf9SRui Paulo /* 22985732ac8SCy Schubert * compute y^2 using the equation of the curve 23085732ac8SCy Schubert * 23185732ac8SCy Schubert * y^2 = x^3 + ax + b 232f05cddf9SRui Paulo */ 2334bc52338SCy Schubert crypto_bignum_deinit(tmp2, 1); 23485732ac8SCy Schubert tmp2 = crypto_ec_point_compute_y_sqr(grp->group, x_candidate); 23585732ac8SCy Schubert if (!tmp2) 23685732ac8SCy Schubert goto fail; 237f05cddf9SRui Paulo 238206b73d0SCy Schubert res = dragonfly_is_quadratic_residue_blind(grp->group, qr_bin, 239206b73d0SCy Schubert qnr_bin, tmp2); 240206b73d0SCy Schubert if (res < 0) 24185732ac8SCy Schubert goto fail; 2424bc52338SCy Schubert found_ctr = const_time_select_u8(found, found_ctr, ctr); 243206b73d0SCy Schubert /* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them 244206b73d0SCy Schubert * (with res converted to 0/0xff and masked with prf being below 245206b73d0SCy Schubert * prime) handles this in constant time. 246206b73d0SCy Schubert */ 247206b73d0SCy Schubert found |= (res & in_range) * 0xff; 24885732ac8SCy Schubert } 24985732ac8SCy Schubert if (found == 0) { 25085732ac8SCy Schubert wpa_printf(MSG_INFO, 25185732ac8SCy Schubert "EAP-pwd: unable to find random point on curve for group %d, something's fishy", 25285732ac8SCy Schubert num); 25385732ac8SCy Schubert goto fail; 25485732ac8SCy Schubert } 2554bc52338SCy Schubert 2564bc52338SCy Schubert /* 2574bc52338SCy Schubert * We know x_candidate is a quadratic residue so set it here. 2584bc52338SCy Schubert */ 2594bc52338SCy Schubert crypto_bignum_deinit(x_candidate, 1); 2604bc52338SCy Schubert x_candidate = crypto_bignum_init_set(x_bin, primebytelen); 261ec080394SCy Schubert if (!x_candidate) 262ec080394SCy Schubert goto fail; 263ec080394SCy Schubert 264ec080394SCy Schubert /* y = sqrt(x^3 + ax + b) mod p 265ec080394SCy Schubert * if LSB(y) == LSB(pwd-seed): PWE = (x, y) 266ec080394SCy Schubert * else: PWE = (x, p - y) 267ec080394SCy Schubert * 268ec080394SCy Schubert * Calculate y and the two possible values for PWE and after that, 269ec080394SCy Schubert * use constant time selection to copy the correct alternative. 270ec080394SCy Schubert */ 271ec080394SCy Schubert y = crypto_ec_point_compute_y_sqr(grp->group, x_candidate); 272ec080394SCy Schubert if (!y || 273ec080394SCy Schubert dragonfly_sqrt(grp->group, y, y) < 0 || 274ec080394SCy Schubert crypto_bignum_to_bin(y, x_y, MAX_ECC_PRIME_LEN, primebytelen) < 0 || 275ec080394SCy Schubert crypto_bignum_sub(prime, y, y) < 0 || 276ec080394SCy Schubert crypto_bignum_to_bin(y, x_y + MAX_ECC_PRIME_LEN, 277ec080394SCy Schubert MAX_ECC_PRIME_LEN, primebytelen) < 0) { 278*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "EAP-pwd: Could not solve y"); 279ec080394SCy Schubert goto fail; 280ec080394SCy Schubert } 281ec080394SCy Schubert 282ec080394SCy Schubert /* Constant time selection of the y coordinate from the two 283ec080394SCy Schubert * options */ 284ec080394SCy Schubert is_eq = const_time_eq(is_odd, x_y[primebytelen - 1] & 0x01); 285ec080394SCy Schubert const_time_select_bin(is_eq, x_y, x_y + MAX_ECC_PRIME_LEN, 286ec080394SCy Schubert primebytelen, x_y + primebytelen); 287ec080394SCy Schubert os_memcpy(x_y, x_bin, primebytelen); 288ec080394SCy Schubert wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: PWE", x_y, 2 * primebytelen); 289ec080394SCy Schubert grp->pwe = crypto_ec_point_from_bin(grp->group, x_y); 290ec080394SCy Schubert if (!grp->pwe) { 291ec080394SCy Schubert wpa_printf(MSG_DEBUG, "EAP-pwd: Could not generate PWE"); 2924bc52338SCy Schubert goto fail; 2934bc52338SCy Schubert } 2944bc52338SCy Schubert 2954bc52338SCy Schubert /* 2964bc52338SCy Schubert * If there's a solution to the equation then the point must be on the 2974bc52338SCy Schubert * curve so why check again explicitly? OpenSSL code says this is 2984bc52338SCy Schubert * required by X9.62. We're not X9.62 but it can't hurt just to be sure. 2994bc52338SCy Schubert */ 3004bc52338SCy Schubert if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) { 3014bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve"); 3024bc52338SCy Schubert goto fail; 3034bc52338SCy Schubert } 3044bc52338SCy Schubert 3054bc52338SCy Schubert wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr); 3064bc52338SCy Schubert 307f05cddf9SRui Paulo if (0) { 308f05cddf9SRui Paulo fail: 30985732ac8SCy Schubert crypto_ec_point_deinit(grp->pwe, 1); 310f05cddf9SRui Paulo grp->pwe = NULL; 311f05cddf9SRui Paulo ret = 1; 312f05cddf9SRui Paulo } 313f05cddf9SRui Paulo /* cleanliness and order.... */ 31485732ac8SCy Schubert crypto_bignum_deinit(x_candidate, 1); 31585732ac8SCy Schubert crypto_bignum_deinit(tmp2, 1); 316ec080394SCy Schubert crypto_bignum_deinit(y, 1); 31785732ac8SCy Schubert crypto_bignum_deinit(qr, 1); 31885732ac8SCy Schubert crypto_bignum_deinit(qnr, 1); 3194bc52338SCy Schubert bin_clear_free(prfbuf, primebytelen); 3204bc52338SCy Schubert os_memset(qr_bin, 0, sizeof(qr_bin)); 3214bc52338SCy Schubert os_memset(qnr_bin, 0, sizeof(qnr_bin)); 3224bc52338SCy Schubert os_memset(qr_or_qnr_bin, 0, sizeof(qr_or_qnr_bin)); 3234bc52338SCy Schubert os_memset(pwe_digest, 0, sizeof(pwe_digest)); 324ec080394SCy Schubert forced_memzero(x_y, sizeof(x_y)); 325f05cddf9SRui Paulo 326f05cddf9SRui Paulo return ret; 327f05cddf9SRui Paulo } 328f05cddf9SRui Paulo 329f05cddf9SRui Paulo 33085732ac8SCy Schubert int compute_keys(EAP_PWD_group *grp, const struct crypto_bignum *k, 33185732ac8SCy Schubert const struct crypto_bignum *peer_scalar, 33285732ac8SCy Schubert const struct crypto_bignum *server_scalar, 333325151a3SRui Paulo const u8 *confirm_peer, const u8 *confirm_server, 334325151a3SRui Paulo const u32 *ciphersuite, u8 *msk, u8 *emsk, u8 *session_id) 335f05cddf9SRui Paulo { 336f05cddf9SRui Paulo struct crypto_hash *hash; 337f05cddf9SRui Paulo u8 mk[SHA256_MAC_LEN], *cruft; 338f05cddf9SRui Paulo u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN]; 33985732ac8SCy Schubert size_t prime_len, order_len; 340f05cddf9SRui Paulo 34185732ac8SCy Schubert prime_len = crypto_ec_prime_len(grp->group); 34285732ac8SCy Schubert order_len = crypto_ec_order_len(grp->group); 34385732ac8SCy Schubert 34485732ac8SCy Schubert cruft = os_malloc(prime_len); 34585732ac8SCy Schubert if (!cruft) 346f05cddf9SRui Paulo return -1; 347f05cddf9SRui Paulo 348f05cddf9SRui Paulo /* 349f05cddf9SRui Paulo * first compute the session-id = TypeCode | H(ciphersuite | scal_p | 350f05cddf9SRui Paulo * scal_s) 351f05cddf9SRui Paulo */ 352f05cddf9SRui Paulo session_id[0] = EAP_TYPE_PWD; 353f05cddf9SRui Paulo hash = eap_pwd_h_init(); 354f05cddf9SRui Paulo if (hash == NULL) { 355f05cddf9SRui Paulo os_free(cruft); 356f05cddf9SRui Paulo return -1; 357f05cddf9SRui Paulo } 358325151a3SRui Paulo eap_pwd_h_update(hash, (const u8 *) ciphersuite, sizeof(u32)); 359*a90b9d01SCy Schubert if (crypto_bignum_to_bin(peer_scalar, cruft, order_len, 360*a90b9d01SCy Schubert order_len) < 0) { 361*a90b9d01SCy Schubert os_free(cruft); 362*a90b9d01SCy Schubert return -1; 363*a90b9d01SCy Schubert } 364*a90b9d01SCy Schubert 36585732ac8SCy Schubert eap_pwd_h_update(hash, cruft, order_len); 366*a90b9d01SCy Schubert if (crypto_bignum_to_bin(server_scalar, cruft, order_len, 367*a90b9d01SCy Schubert order_len) < 0) { 368*a90b9d01SCy Schubert os_free(cruft); 369*a90b9d01SCy Schubert return -1; 370*a90b9d01SCy Schubert } 371*a90b9d01SCy Schubert 37285732ac8SCy Schubert eap_pwd_h_update(hash, cruft, order_len); 373f05cddf9SRui Paulo eap_pwd_h_final(hash, &session_id[1]); 374f05cddf9SRui Paulo 375f05cddf9SRui Paulo /* then compute MK = H(k | confirm-peer | confirm-server) */ 376f05cddf9SRui Paulo hash = eap_pwd_h_init(); 377f05cddf9SRui Paulo if (hash == NULL) { 378f05cddf9SRui Paulo os_free(cruft); 379f05cddf9SRui Paulo return -1; 380f05cddf9SRui Paulo } 381*a90b9d01SCy Schubert 382*a90b9d01SCy Schubert if (crypto_bignum_to_bin(k, cruft, prime_len, prime_len) < 0) { 383*a90b9d01SCy Schubert os_free(cruft); 384*a90b9d01SCy Schubert return -1; 385*a90b9d01SCy Schubert } 386*a90b9d01SCy Schubert 38785732ac8SCy Schubert eap_pwd_h_update(hash, cruft, prime_len); 388f05cddf9SRui Paulo os_free(cruft); 389f05cddf9SRui Paulo eap_pwd_h_update(hash, confirm_peer, SHA256_MAC_LEN); 390f05cddf9SRui Paulo eap_pwd_h_update(hash, confirm_server, SHA256_MAC_LEN); 391f05cddf9SRui Paulo eap_pwd_h_final(hash, mk); 392f05cddf9SRui Paulo 393f05cddf9SRui Paulo /* stretch the mk with the session-id to get MSK | EMSK */ 394f05cddf9SRui Paulo if (eap_pwd_kdf(mk, SHA256_MAC_LEN, 395f05cddf9SRui Paulo session_id, SHA256_MAC_LEN + 1, 396f05cddf9SRui Paulo msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8) < 0) { 397f05cddf9SRui Paulo return -1; 398f05cddf9SRui Paulo } 399f05cddf9SRui Paulo 400f05cddf9SRui Paulo os_memcpy(msk, msk_emsk, EAP_MSK_LEN); 401f05cddf9SRui Paulo os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN); 402f05cddf9SRui Paulo 403f05cddf9SRui Paulo return 1; 404f05cddf9SRui Paulo } 4054bc52338SCy Schubert 4064bc52338SCy Schubert 4074bc52338SCy Schubert static int eap_pwd_element_coord_ok(const struct crypto_bignum *prime, 4084bc52338SCy Schubert const u8 *buf, size_t len) 4094bc52338SCy Schubert { 4104bc52338SCy Schubert struct crypto_bignum *val; 4114bc52338SCy Schubert int ok = 1; 4124bc52338SCy Schubert 4134bc52338SCy Schubert val = crypto_bignum_init_set(buf, len); 4144bc52338SCy Schubert if (!val || crypto_bignum_is_zero(val) || 4154bc52338SCy Schubert crypto_bignum_cmp(val, prime) >= 0) 4164bc52338SCy Schubert ok = 0; 4174bc52338SCy Schubert crypto_bignum_deinit(val, 0); 4184bc52338SCy Schubert return ok; 4194bc52338SCy Schubert } 4204bc52338SCy Schubert 4214bc52338SCy Schubert 4224bc52338SCy Schubert struct crypto_ec_point * eap_pwd_get_element(EAP_PWD_group *group, 4234bc52338SCy Schubert const u8 *buf) 4244bc52338SCy Schubert { 4254bc52338SCy Schubert struct crypto_ec_point *element; 4264bc52338SCy Schubert const struct crypto_bignum *prime; 4274bc52338SCy Schubert size_t prime_len; 4284bc52338SCy Schubert 4294bc52338SCy Schubert prime = crypto_ec_get_prime(group->group); 4304bc52338SCy Schubert prime_len = crypto_ec_prime_len(group->group); 4314bc52338SCy Schubert 4324bc52338SCy Schubert /* RFC 5931, 2.8.5.2.2: 0 < x,y < p */ 4334bc52338SCy Schubert if (!eap_pwd_element_coord_ok(prime, buf, prime_len) || 4344bc52338SCy Schubert !eap_pwd_element_coord_ok(prime, buf + prime_len, prime_len)) { 4354bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: Invalid coordinate in element"); 4364bc52338SCy Schubert return NULL; 4374bc52338SCy Schubert } 4384bc52338SCy Schubert 4394bc52338SCy Schubert element = crypto_ec_point_from_bin(group->group, buf); 4404bc52338SCy Schubert if (!element) { 4414bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: EC point from element failed"); 4424bc52338SCy Schubert return NULL; 4434bc52338SCy Schubert } 4444bc52338SCy Schubert 4454bc52338SCy Schubert /* RFC 5931, 2.8.5.2.2: on curve and not the point at infinity */ 4464bc52338SCy Schubert if (!crypto_ec_point_is_on_curve(group->group, element) || 4474bc52338SCy Schubert crypto_ec_point_is_at_infinity(group->group, element)) { 4484bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: Invalid element"); 4494bc52338SCy Schubert goto fail; 4504bc52338SCy Schubert } 4514bc52338SCy Schubert 4524bc52338SCy Schubert out: 4534bc52338SCy Schubert return element; 4544bc52338SCy Schubert fail: 4554bc52338SCy Schubert crypto_ec_point_deinit(element, 0); 4564bc52338SCy Schubert element = NULL; 4574bc52338SCy Schubert goto out; 4584bc52338SCy Schubert } 4594bc52338SCy Schubert 4604bc52338SCy Schubert 4614bc52338SCy Schubert struct crypto_bignum * eap_pwd_get_scalar(EAP_PWD_group *group, const u8 *buf) 4624bc52338SCy Schubert { 4634bc52338SCy Schubert struct crypto_bignum *scalar; 4644bc52338SCy Schubert const struct crypto_bignum *order; 4654bc52338SCy Schubert size_t order_len; 4664bc52338SCy Schubert 4674bc52338SCy Schubert order = crypto_ec_get_order(group->group); 4684bc52338SCy Schubert order_len = crypto_ec_order_len(group->group); 4694bc52338SCy Schubert 4704bc52338SCy Schubert /* RFC 5931, 2.8.5.2: 1 < scalar < r */ 4714bc52338SCy Schubert scalar = crypto_bignum_init_set(buf, order_len); 4724bc52338SCy Schubert if (!scalar || crypto_bignum_is_zero(scalar) || 4734bc52338SCy Schubert crypto_bignum_is_one(scalar) || 4744bc52338SCy Schubert crypto_bignum_cmp(scalar, order) >= 0) { 4754bc52338SCy Schubert wpa_printf(MSG_INFO, "EAP-pwd: received scalar is invalid"); 4764bc52338SCy Schubert crypto_bignum_deinit(scalar, 0); 4774bc52338SCy Schubert scalar = NULL; 4784bc52338SCy Schubert } 4794bc52338SCy Schubert 4804bc52338SCy Schubert return scalar; 4814bc52338SCy Schubert } 4824bc52338SCy Schubert 4834bc52338SCy Schubert 4844bc52338SCy Schubert int eap_pwd_get_rand_mask(EAP_PWD_group *group, struct crypto_bignum *_rand, 4854bc52338SCy Schubert struct crypto_bignum *_mask, 4864bc52338SCy Schubert struct crypto_bignum *scalar) 4874bc52338SCy Schubert { 488206b73d0SCy Schubert return dragonfly_generate_scalar(crypto_ec_get_order(group->group), 489206b73d0SCy Schubert _rand, _mask, scalar); 4904bc52338SCy Schubert } 491