1 /* 2 * EAP server/peer: EAP-pwd shared routines 3 * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 #include "common.h" 11 #include "utils/const_time.h" 12 #include "crypto/sha256.h" 13 #include "crypto/crypto.h" 14 #include "eap_defs.h" 15 #include "eap_pwd_common.h" 16 17 #define MAX_ECC_PRIME_LEN 66 18 19 20 /* The random function H(x) = HMAC-SHA256(0^32, x) */ 21 struct crypto_hash * eap_pwd_h_init(void) 22 { 23 u8 allzero[SHA256_MAC_LEN]; 24 os_memset(allzero, 0, SHA256_MAC_LEN); 25 return crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, allzero, 26 SHA256_MAC_LEN); 27 } 28 29 30 void eap_pwd_h_update(struct crypto_hash *hash, const u8 *data, size_t len) 31 { 32 crypto_hash_update(hash, data, len); 33 } 34 35 36 void eap_pwd_h_final(struct crypto_hash *hash, u8 *digest) 37 { 38 size_t len = SHA256_MAC_LEN; 39 crypto_hash_finish(hash, digest, &len); 40 } 41 42 43 /* a counter-based KDF based on NIST SP800-108 */ 44 static int eap_pwd_kdf(const u8 *key, size_t keylen, const u8 *label, 45 size_t labellen, u8 *result, size_t resultbitlen) 46 { 47 struct crypto_hash *hash; 48 u8 digest[SHA256_MAC_LEN]; 49 u16 i, ctr, L; 50 size_t resultbytelen, len = 0, mdlen; 51 52 resultbytelen = (resultbitlen + 7) / 8; 53 ctr = 0; 54 L = htons(resultbitlen); 55 while (len < resultbytelen) { 56 ctr++; 57 i = htons(ctr); 58 hash = crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, 59 key, keylen); 60 if (hash == NULL) 61 return -1; 62 if (ctr > 1) 63 crypto_hash_update(hash, digest, SHA256_MAC_LEN); 64 crypto_hash_update(hash, (u8 *) &i, sizeof(u16)); 65 crypto_hash_update(hash, label, labellen); 66 crypto_hash_update(hash, (u8 *) &L, sizeof(u16)); 67 mdlen = SHA256_MAC_LEN; 68 if (crypto_hash_finish(hash, digest, &mdlen) < 0) 69 return -1; 70 if ((len + mdlen) > resultbytelen) 71 os_memcpy(result + len, digest, resultbytelen - len); 72 else 73 os_memcpy(result + len, digest, mdlen); 74 len += mdlen; 75 } 76 77 /* since we're expanding to a bit length, mask off the excess */ 78 if (resultbitlen % 8) { 79 u8 mask = 0xff; 80 mask <<= (8 - (resultbitlen % 8)); 81 result[resultbytelen - 1] &= mask; 82 } 83 84 return 0; 85 } 86 87 88 static int eap_pwd_suitable_group(u16 num) 89 { 90 /* Do not allow ECC groups with prime under 256 bits based on guidance 91 * for the similar design in SAE. */ 92 return num == 19 || num == 20 || num == 21 || 93 num == 28 || num == 29 || num == 30; 94 } 95 96 97 EAP_PWD_group * get_eap_pwd_group(u16 num) 98 { 99 EAP_PWD_group *grp; 100 101 if (!eap_pwd_suitable_group(num)) { 102 wpa_printf(MSG_INFO, "EAP-pwd: unsuitable group %u", num); 103 return NULL; 104 } 105 grp = os_zalloc(sizeof(EAP_PWD_group)); 106 if (!grp) 107 return NULL; 108 grp->group = crypto_ec_init(num); 109 if (!grp->group) { 110 wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC group"); 111 os_free(grp); 112 return NULL; 113 } 114 115 grp->group_num = num; 116 wpa_printf(MSG_INFO, "EAP-pwd: provisioned group %d", num); 117 118 return grp; 119 } 120 121 122 static void buf_shift_right(u8 *buf, size_t len, size_t bits) 123 { 124 size_t i; 125 for (i = len - 1; i > 0; i--) 126 buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits); 127 buf[0] >>= bits; 128 } 129 130 131 /* 132 * compute a "random" secret point on an elliptic curve based 133 * on the password and identities. 134 */ 135 int compute_password_element(EAP_PWD_group *grp, u16 num, 136 const u8 *password, size_t password_len, 137 const u8 *id_server, size_t id_server_len, 138 const u8 *id_peer, size_t id_peer_len, 139 const u8 *token) 140 { 141 struct crypto_bignum *qr = NULL, *qnr = NULL, *one = NULL; 142 struct crypto_bignum *qr_or_qnr = NULL; 143 u8 qr_bin[MAX_ECC_PRIME_LEN]; 144 u8 qnr_bin[MAX_ECC_PRIME_LEN]; 145 u8 qr_or_qnr_bin[MAX_ECC_PRIME_LEN]; 146 u8 x_bin[MAX_ECC_PRIME_LEN]; 147 struct crypto_bignum *tmp1 = NULL, *tmp2 = NULL, *pm1 = NULL; 148 struct crypto_hash *hash; 149 unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr; 150 int ret = 0, check, res; 151 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_* 152 * mask */ 153 size_t primebytelen = 0, primebitlen; 154 struct crypto_bignum *x_candidate = NULL; 155 const struct crypto_bignum *prime; 156 u8 mask, found_ctr = 0, is_odd = 0; 157 158 if (grp->pwe) 159 return -1; 160 161 os_memset(x_bin, 0, sizeof(x_bin)); 162 163 prime = crypto_ec_get_prime(grp->group); 164 grp->pwe = crypto_ec_point_init(grp->group); 165 tmp1 = crypto_bignum_init(); 166 pm1 = crypto_bignum_init(); 167 one = crypto_bignum_init_set((const u8 *) "\x01", 1); 168 if (!grp->pwe || !tmp1 || !pm1 || !one) { 169 wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums"); 170 goto fail; 171 } 172 173 primebitlen = crypto_ec_prime_len_bits(grp->group); 174 primebytelen = crypto_ec_prime_len(grp->group); 175 if ((prfbuf = os_malloc(primebytelen)) == NULL) { 176 wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf " 177 "buffer"); 178 goto fail; 179 } 180 if (crypto_bignum_sub(prime, one, pm1) < 0) 181 goto fail; 182 183 /* get a random quadratic residue and nonresidue */ 184 while (!qr || !qnr) { 185 if (crypto_bignum_rand(tmp1, prime) < 0) 186 goto fail; 187 res = crypto_bignum_legendre(tmp1, prime); 188 if (!qr && res == 1) { 189 qr = tmp1; 190 tmp1 = crypto_bignum_init(); 191 } else if (!qnr && res == -1) { 192 qnr = tmp1; 193 tmp1 = crypto_bignum_init(); 194 } 195 if (!tmp1) 196 goto fail; 197 } 198 if (crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), 199 primebytelen) < 0 || 200 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), 201 primebytelen) < 0) 202 goto fail; 203 204 os_memset(prfbuf, 0, primebytelen); 205 ctr = 0; 206 207 /* 208 * Run through the hunting-and-pecking loop 40 times to mask the time 209 * necessary to find PWE. The odds of PWE not being found in 40 loops is 210 * roughly 1 in 1 trillion. 211 */ 212 while (ctr < 40) { 213 ctr++; 214 215 /* 216 * compute counter-mode password value and stretch to prime 217 * pwd-seed = H(token | peer-id | server-id | password | 218 * counter) 219 */ 220 hash = eap_pwd_h_init(); 221 if (hash == NULL) 222 goto fail; 223 eap_pwd_h_update(hash, token, sizeof(u32)); 224 eap_pwd_h_update(hash, id_peer, id_peer_len); 225 eap_pwd_h_update(hash, id_server, id_server_len); 226 eap_pwd_h_update(hash, password, password_len); 227 eap_pwd_h_update(hash, &ctr, sizeof(ctr)); 228 eap_pwd_h_final(hash, pwe_digest); 229 230 is_odd = const_time_select_u8( 231 found, is_odd, pwe_digest[SHA256_MAC_LEN - 1] & 0x01); 232 if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN, 233 (u8 *) "EAP-pwd Hunting And Pecking", 234 os_strlen("EAP-pwd Hunting And Pecking"), 235 prfbuf, primebitlen) < 0) 236 goto fail; 237 if (primebitlen % 8) 238 buf_shift_right(prfbuf, primebytelen, 239 8 - primebitlen % 8); 240 241 crypto_bignum_deinit(x_candidate, 1); 242 x_candidate = crypto_bignum_init_set(prfbuf, primebytelen); 243 if (!x_candidate) { 244 wpa_printf(MSG_INFO, 245 "EAP-pwd: unable to create x_candidate"); 246 goto fail; 247 } 248 249 if (crypto_bignum_cmp(x_candidate, prime) >= 0) 250 continue; 251 252 wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate", 253 prfbuf, primebytelen); 254 const_time_select_bin(found, x_bin, prfbuf, primebytelen, 255 x_bin); 256 257 /* 258 * compute y^2 using the equation of the curve 259 * 260 * y^2 = x^3 + ax + b 261 */ 262 crypto_bignum_deinit(tmp2, 1); 263 tmp2 = crypto_ec_point_compute_y_sqr(grp->group, x_candidate); 264 if (!tmp2) 265 goto fail; 266 267 /* 268 * mask tmp2 so doing legendre won't leak timing info 269 * 270 * tmp1 is a random number between 1 and p-1 271 */ 272 if (crypto_bignum_rand(tmp1, pm1) < 0 || 273 crypto_bignum_mulmod(tmp2, tmp1, prime, tmp2) < 0 || 274 crypto_bignum_mulmod(tmp2, tmp1, prime, tmp2) < 0) 275 goto fail; 276 277 /* 278 * Now tmp2 (y^2) is masked, all values between 1 and p-1 279 * are equally probable. Multiplying by r^2 does not change 280 * whether or not tmp2 is a quadratic residue, just masks it. 281 * 282 * Flip a coin, multiply by the random quadratic residue or the 283 * random quadratic nonresidue and record heads or tails. 284 */ 285 mask = const_time_eq_u8(crypto_bignum_is_odd(tmp1), 1); 286 check = const_time_select_s8(mask, 1, -1); 287 const_time_select_bin(mask, qr_bin, qnr_bin, primebytelen, 288 qr_or_qnr_bin); 289 crypto_bignum_deinit(qr_or_qnr, 1); 290 qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, primebytelen); 291 if (!qr_or_qnr || 292 crypto_bignum_mulmod(tmp2, qr_or_qnr, prime, tmp2) < 0) 293 goto fail; 294 295 /* 296 * Now it's safe to do legendre, if check is 1 then it's 297 * a straightforward test (multiplying by qr does not 298 * change result), if check is -1 then it's the opposite test 299 * (multiplying a qr by qnr would make a qnr). 300 */ 301 res = crypto_bignum_legendre(tmp2, prime); 302 if (res == -2) 303 goto fail; 304 mask = const_time_eq(res, check); 305 found_ctr = const_time_select_u8(found, found_ctr, ctr); 306 found |= mask; 307 } 308 if (found == 0) { 309 wpa_printf(MSG_INFO, 310 "EAP-pwd: unable to find random point on curve for group %d, something's fishy", 311 num); 312 goto fail; 313 } 314 315 /* 316 * We know x_candidate is a quadratic residue so set it here. 317 */ 318 crypto_bignum_deinit(x_candidate, 1); 319 x_candidate = crypto_bignum_init_set(x_bin, primebytelen); 320 if (!x_candidate || 321 crypto_ec_point_solve_y_coord(grp->group, grp->pwe, x_candidate, 322 is_odd) != 0) { 323 wpa_printf(MSG_INFO, "EAP-pwd: Could not solve for y"); 324 goto fail; 325 } 326 327 /* 328 * If there's a solution to the equation then the point must be on the 329 * curve so why check again explicitly? OpenSSL code says this is 330 * required by X9.62. We're not X9.62 but it can't hurt just to be sure. 331 */ 332 if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) { 333 wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve"); 334 goto fail; 335 } 336 337 wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr); 338 339 if (0) { 340 fail: 341 crypto_ec_point_deinit(grp->pwe, 1); 342 grp->pwe = NULL; 343 ret = 1; 344 } 345 /* cleanliness and order.... */ 346 crypto_bignum_deinit(x_candidate, 1); 347 crypto_bignum_deinit(pm1, 0); 348 crypto_bignum_deinit(tmp1, 1); 349 crypto_bignum_deinit(tmp2, 1); 350 crypto_bignum_deinit(qr, 1); 351 crypto_bignum_deinit(qnr, 1); 352 crypto_bignum_deinit(qr_or_qnr, 1); 353 crypto_bignum_deinit(one, 0); 354 bin_clear_free(prfbuf, primebytelen); 355 os_memset(qr_bin, 0, sizeof(qr_bin)); 356 os_memset(qnr_bin, 0, sizeof(qnr_bin)); 357 os_memset(qr_or_qnr_bin, 0, sizeof(qr_or_qnr_bin)); 358 os_memset(pwe_digest, 0, sizeof(pwe_digest)); 359 360 return ret; 361 } 362 363 364 int compute_keys(EAP_PWD_group *grp, const struct crypto_bignum *k, 365 const struct crypto_bignum *peer_scalar, 366 const struct crypto_bignum *server_scalar, 367 const u8 *confirm_peer, const u8 *confirm_server, 368 const u32 *ciphersuite, u8 *msk, u8 *emsk, u8 *session_id) 369 { 370 struct crypto_hash *hash; 371 u8 mk[SHA256_MAC_LEN], *cruft; 372 u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN]; 373 size_t prime_len, order_len; 374 375 prime_len = crypto_ec_prime_len(grp->group); 376 order_len = crypto_ec_order_len(grp->group); 377 378 cruft = os_malloc(prime_len); 379 if (!cruft) 380 return -1; 381 382 /* 383 * first compute the session-id = TypeCode | H(ciphersuite | scal_p | 384 * scal_s) 385 */ 386 session_id[0] = EAP_TYPE_PWD; 387 hash = eap_pwd_h_init(); 388 if (hash == NULL) { 389 os_free(cruft); 390 return -1; 391 } 392 eap_pwd_h_update(hash, (const u8 *) ciphersuite, sizeof(u32)); 393 crypto_bignum_to_bin(peer_scalar, cruft, order_len, order_len); 394 eap_pwd_h_update(hash, cruft, order_len); 395 crypto_bignum_to_bin(server_scalar, cruft, order_len, order_len); 396 eap_pwd_h_update(hash, cruft, order_len); 397 eap_pwd_h_final(hash, &session_id[1]); 398 399 /* then compute MK = H(k | confirm-peer | confirm-server) */ 400 hash = eap_pwd_h_init(); 401 if (hash == NULL) { 402 os_free(cruft); 403 return -1; 404 } 405 crypto_bignum_to_bin(k, cruft, prime_len, prime_len); 406 eap_pwd_h_update(hash, cruft, prime_len); 407 os_free(cruft); 408 eap_pwd_h_update(hash, confirm_peer, SHA256_MAC_LEN); 409 eap_pwd_h_update(hash, confirm_server, SHA256_MAC_LEN); 410 eap_pwd_h_final(hash, mk); 411 412 /* stretch the mk with the session-id to get MSK | EMSK */ 413 if (eap_pwd_kdf(mk, SHA256_MAC_LEN, 414 session_id, SHA256_MAC_LEN + 1, 415 msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8) < 0) { 416 return -1; 417 } 418 419 os_memcpy(msk, msk_emsk, EAP_MSK_LEN); 420 os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN); 421 422 return 1; 423 } 424 425 426 static int eap_pwd_element_coord_ok(const struct crypto_bignum *prime, 427 const u8 *buf, size_t len) 428 { 429 struct crypto_bignum *val; 430 int ok = 1; 431 432 val = crypto_bignum_init_set(buf, len); 433 if (!val || crypto_bignum_is_zero(val) || 434 crypto_bignum_cmp(val, prime) >= 0) 435 ok = 0; 436 crypto_bignum_deinit(val, 0); 437 return ok; 438 } 439 440 441 struct crypto_ec_point * eap_pwd_get_element(EAP_PWD_group *group, 442 const u8 *buf) 443 { 444 struct crypto_ec_point *element; 445 const struct crypto_bignum *prime; 446 size_t prime_len; 447 448 prime = crypto_ec_get_prime(group->group); 449 prime_len = crypto_ec_prime_len(group->group); 450 451 /* RFC 5931, 2.8.5.2.2: 0 < x,y < p */ 452 if (!eap_pwd_element_coord_ok(prime, buf, prime_len) || 453 !eap_pwd_element_coord_ok(prime, buf + prime_len, prime_len)) { 454 wpa_printf(MSG_INFO, "EAP-pwd: Invalid coordinate in element"); 455 return NULL; 456 } 457 458 element = crypto_ec_point_from_bin(group->group, buf); 459 if (!element) { 460 wpa_printf(MSG_INFO, "EAP-pwd: EC point from element failed"); 461 return NULL; 462 } 463 464 /* RFC 5931, 2.8.5.2.2: on curve and not the point at infinity */ 465 if (!crypto_ec_point_is_on_curve(group->group, element) || 466 crypto_ec_point_is_at_infinity(group->group, element)) { 467 wpa_printf(MSG_INFO, "EAP-pwd: Invalid element"); 468 goto fail; 469 } 470 471 out: 472 return element; 473 fail: 474 crypto_ec_point_deinit(element, 0); 475 element = NULL; 476 goto out; 477 } 478 479 480 struct crypto_bignum * eap_pwd_get_scalar(EAP_PWD_group *group, const u8 *buf) 481 { 482 struct crypto_bignum *scalar; 483 const struct crypto_bignum *order; 484 size_t order_len; 485 486 order = crypto_ec_get_order(group->group); 487 order_len = crypto_ec_order_len(group->group); 488 489 /* RFC 5931, 2.8.5.2: 1 < scalar < r */ 490 scalar = crypto_bignum_init_set(buf, order_len); 491 if (!scalar || crypto_bignum_is_zero(scalar) || 492 crypto_bignum_is_one(scalar) || 493 crypto_bignum_cmp(scalar, order) >= 0) { 494 wpa_printf(MSG_INFO, "EAP-pwd: received scalar is invalid"); 495 crypto_bignum_deinit(scalar, 0); 496 scalar = NULL; 497 } 498 499 return scalar; 500 } 501 502 503 int eap_pwd_get_rand_mask(EAP_PWD_group *group, struct crypto_bignum *_rand, 504 struct crypto_bignum *_mask, 505 struct crypto_bignum *scalar) 506 { 507 const struct crypto_bignum *order; 508 int count; 509 510 order = crypto_ec_get_order(group->group); 511 512 /* Select two random values rand,mask such that 1 < rand,mask < r and 513 * rand + mask mod r > 1. */ 514 for (count = 0; count < 100; count++) { 515 if (crypto_bignum_rand(_rand, order) == 0 && 516 !crypto_bignum_is_zero(_rand) && 517 crypto_bignum_rand(_mask, order) == 0 && 518 !crypto_bignum_is_zero(_mask) && 519 crypto_bignum_add(_rand, _mask, scalar) == 0 && 520 crypto_bignum_mod(scalar, order, scalar) == 0 && 521 !crypto_bignum_is_zero(scalar) && 522 !crypto_bignum_is_one(scalar)) 523 return 0; 524 } 525 526 wpa_printf(MSG_INFO, "EAP-pwd: unable to get randomness"); 527 return -1; 528 } 529