1 /* 2 * EAP server/peer: EAP-pwd shared routines 3 * Copyright (c) 2010, Dan Harkins <dharkins (at) 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 u8 prime_bin[MAX_ECC_PRIME_LEN]; 148 struct crypto_bignum *tmp1 = NULL, *tmp2 = NULL, *pm1 = NULL; 149 struct crypto_hash *hash; 150 unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr; 151 int ret = 0, check, res; 152 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_* 153 * mask */ 154 size_t primebytelen = 0, primebitlen; 155 struct crypto_bignum *x_candidate = NULL; 156 const struct crypto_bignum *prime; 157 u8 mask, found_ctr = 0, is_odd = 0; 158 159 if (grp->pwe) 160 return -1; 161 162 os_memset(x_bin, 0, sizeof(x_bin)); 163 164 prime = crypto_ec_get_prime(grp->group); 165 primebitlen = crypto_ec_prime_len_bits(grp->group); 166 primebytelen = crypto_ec_prime_len(grp->group); 167 if (crypto_bignum_to_bin(prime, prime_bin, sizeof(prime_bin), 168 primebytelen) < 0) 169 return -1; 170 grp->pwe = crypto_ec_point_init(grp->group); 171 tmp1 = crypto_bignum_init(); 172 pm1 = crypto_bignum_init(); 173 one = crypto_bignum_init_set((const u8 *) "\x01", 1); 174 if (!grp->pwe || !tmp1 || !pm1 || !one) { 175 wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums"); 176 goto fail; 177 } 178 179 if ((prfbuf = os_malloc(primebytelen)) == NULL) { 180 wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf " 181 "buffer"); 182 goto fail; 183 } 184 if (crypto_bignum_sub(prime, one, pm1) < 0) 185 goto fail; 186 187 /* get a random quadratic residue and nonresidue */ 188 while (!qr || !qnr) { 189 if (crypto_bignum_rand(tmp1, prime) < 0) 190 goto fail; 191 res = crypto_bignum_legendre(tmp1, prime); 192 if (!qr && res == 1) { 193 qr = tmp1; 194 tmp1 = crypto_bignum_init(); 195 } else if (!qnr && res == -1) { 196 qnr = tmp1; 197 tmp1 = crypto_bignum_init(); 198 } 199 if (!tmp1) 200 goto fail; 201 } 202 if (crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), 203 primebytelen) < 0 || 204 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), 205 primebytelen) < 0) 206 goto fail; 207 208 os_memset(prfbuf, 0, primebytelen); 209 ctr = 0; 210 211 /* 212 * Run through the hunting-and-pecking loop 40 times to mask the time 213 * necessary to find PWE. The odds of PWE not being found in 40 loops is 214 * roughly 1 in 1 trillion. 215 */ 216 while (ctr < 40) { 217 ctr++; 218 219 /* 220 * compute counter-mode password value and stretch to prime 221 * pwd-seed = H(token | peer-id | server-id | password | 222 * counter) 223 */ 224 hash = eap_pwd_h_init(); 225 if (hash == NULL) 226 goto fail; 227 eap_pwd_h_update(hash, token, sizeof(u32)); 228 eap_pwd_h_update(hash, id_peer, id_peer_len); 229 eap_pwd_h_update(hash, id_server, id_server_len); 230 eap_pwd_h_update(hash, password, password_len); 231 eap_pwd_h_update(hash, &ctr, sizeof(ctr)); 232 eap_pwd_h_final(hash, pwe_digest); 233 234 is_odd = const_time_select_u8( 235 found, is_odd, pwe_digest[SHA256_MAC_LEN - 1] & 0x01); 236 if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN, 237 (u8 *) "EAP-pwd Hunting And Pecking", 238 os_strlen("EAP-pwd Hunting And Pecking"), 239 prfbuf, primebitlen) < 0) 240 goto fail; 241 if (primebitlen % 8) 242 buf_shift_right(prfbuf, primebytelen, 243 8 - primebitlen % 8); 244 if (const_time_memcmp(prfbuf, prime_bin, primebytelen) >= 0) 245 continue; 246 247 crypto_bignum_deinit(x_candidate, 1); 248 x_candidate = crypto_bignum_init_set(prfbuf, primebytelen); 249 if (!x_candidate) { 250 wpa_printf(MSG_INFO, 251 "EAP-pwd: unable to create x_candidate"); 252 goto fail; 253 } 254 255 wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate", 256 prfbuf, primebytelen); 257 const_time_select_bin(found, x_bin, prfbuf, primebytelen, 258 x_bin); 259 260 /* 261 * compute y^2 using the equation of the curve 262 * 263 * y^2 = x^3 + ax + b 264 */ 265 crypto_bignum_deinit(tmp2, 1); 266 tmp2 = crypto_ec_point_compute_y_sqr(grp->group, x_candidate); 267 if (!tmp2) 268 goto fail; 269 270 /* 271 * mask tmp2 so doing legendre won't leak timing info 272 * 273 * tmp1 is a random number between 1 and p-1 274 */ 275 if (crypto_bignum_rand(tmp1, pm1) < 0 || 276 crypto_bignum_mulmod(tmp2, tmp1, prime, tmp2) < 0 || 277 crypto_bignum_mulmod(tmp2, tmp1, prime, tmp2) < 0) 278 goto fail; 279 280 /* 281 * Now tmp2 (y^2) is masked, all values between 1 and p-1 282 * are equally probable. Multiplying by r^2 does not change 283 * whether or not tmp2 is a quadratic residue, just masks it. 284 * 285 * Flip a coin, multiply by the random quadratic residue or the 286 * random quadratic nonresidue and record heads or tails. 287 */ 288 mask = const_time_eq_u8(crypto_bignum_is_odd(tmp1), 1); 289 check = const_time_select_s8(mask, 1, -1); 290 const_time_select_bin(mask, qr_bin, qnr_bin, primebytelen, 291 qr_or_qnr_bin); 292 crypto_bignum_deinit(qr_or_qnr, 1); 293 qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, primebytelen); 294 if (!qr_or_qnr || 295 crypto_bignum_mulmod(tmp2, qr_or_qnr, prime, tmp2) < 0) 296 goto fail; 297 298 /* 299 * Now it's safe to do legendre, if check is 1 then it's 300 * a straightforward test (multiplying by qr does not 301 * change result), if check is -1 then it's the opposite test 302 * (multiplying a qr by qnr would make a qnr). 303 */ 304 res = crypto_bignum_legendre(tmp2, prime); 305 if (res == -2) 306 goto fail; 307 mask = const_time_eq(res, check); 308 found_ctr = const_time_select_u8(found, found_ctr, ctr); 309 found |= mask; 310 } 311 if (found == 0) { 312 wpa_printf(MSG_INFO, 313 "EAP-pwd: unable to find random point on curve for group %d, something's fishy", 314 num); 315 goto fail; 316 } 317 318 /* 319 * We know x_candidate is a quadratic residue so set it here. 320 */ 321 crypto_bignum_deinit(x_candidate, 1); 322 x_candidate = crypto_bignum_init_set(x_bin, primebytelen); 323 if (!x_candidate || 324 crypto_ec_point_solve_y_coord(grp->group, grp->pwe, x_candidate, 325 is_odd) != 0) { 326 wpa_printf(MSG_INFO, "EAP-pwd: Could not solve for y"); 327 goto fail; 328 } 329 330 /* 331 * If there's a solution to the equation then the point must be on the 332 * curve so why check again explicitly? OpenSSL code says this is 333 * required by X9.62. We're not X9.62 but it can't hurt just to be sure. 334 */ 335 if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) { 336 wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve"); 337 goto fail; 338 } 339 340 wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr); 341 342 if (0) { 343 fail: 344 crypto_ec_point_deinit(grp->pwe, 1); 345 grp->pwe = NULL; 346 ret = 1; 347 } 348 /* cleanliness and order.... */ 349 crypto_bignum_deinit(x_candidate, 1); 350 crypto_bignum_deinit(pm1, 0); 351 crypto_bignum_deinit(tmp1, 1); 352 crypto_bignum_deinit(tmp2, 1); 353 crypto_bignum_deinit(qr, 1); 354 crypto_bignum_deinit(qnr, 1); 355 crypto_bignum_deinit(qr_or_qnr, 1); 356 crypto_bignum_deinit(one, 0); 357 bin_clear_free(prfbuf, primebytelen); 358 os_memset(qr_bin, 0, sizeof(qr_bin)); 359 os_memset(qnr_bin, 0, sizeof(qnr_bin)); 360 os_memset(qr_or_qnr_bin, 0, sizeof(qr_or_qnr_bin)); 361 os_memset(pwe_digest, 0, sizeof(pwe_digest)); 362 363 return ret; 364 } 365 366 367 int compute_keys(EAP_PWD_group *grp, const struct crypto_bignum *k, 368 const struct crypto_bignum *peer_scalar, 369 const struct crypto_bignum *server_scalar, 370 const u8 *confirm_peer, const u8 *confirm_server, 371 const u32 *ciphersuite, u8 *msk, u8 *emsk, u8 *session_id) 372 { 373 struct crypto_hash *hash; 374 u8 mk[SHA256_MAC_LEN], *cruft; 375 u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN]; 376 size_t prime_len, order_len; 377 378 prime_len = crypto_ec_prime_len(grp->group); 379 order_len = crypto_ec_order_len(grp->group); 380 381 cruft = os_malloc(prime_len); 382 if (!cruft) 383 return -1; 384 385 /* 386 * first compute the session-id = TypeCode | H(ciphersuite | scal_p | 387 * scal_s) 388 */ 389 session_id[0] = EAP_TYPE_PWD; 390 hash = eap_pwd_h_init(); 391 if (hash == NULL) { 392 os_free(cruft); 393 return -1; 394 } 395 eap_pwd_h_update(hash, (const u8 *) ciphersuite, sizeof(u32)); 396 crypto_bignum_to_bin(peer_scalar, cruft, order_len, order_len); 397 eap_pwd_h_update(hash, cruft, order_len); 398 crypto_bignum_to_bin(server_scalar, cruft, order_len, order_len); 399 eap_pwd_h_update(hash, cruft, order_len); 400 eap_pwd_h_final(hash, &session_id[1]); 401 402 /* then compute MK = H(k | confirm-peer | confirm-server) */ 403 hash = eap_pwd_h_init(); 404 if (hash == NULL) { 405 os_free(cruft); 406 return -1; 407 } 408 crypto_bignum_to_bin(k, cruft, prime_len, prime_len); 409 eap_pwd_h_update(hash, cruft, prime_len); 410 os_free(cruft); 411 eap_pwd_h_update(hash, confirm_peer, SHA256_MAC_LEN); 412 eap_pwd_h_update(hash, confirm_server, SHA256_MAC_LEN); 413 eap_pwd_h_final(hash, mk); 414 415 /* stretch the mk with the session-id to get MSK | EMSK */ 416 if (eap_pwd_kdf(mk, SHA256_MAC_LEN, 417 session_id, SHA256_MAC_LEN + 1, 418 msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8) < 0) { 419 return -1; 420 } 421 422 os_memcpy(msk, msk_emsk, EAP_MSK_LEN); 423 os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN); 424 425 return 1; 426 } 427 428 429 static int eap_pwd_element_coord_ok(const struct crypto_bignum *prime, 430 const u8 *buf, size_t len) 431 { 432 struct crypto_bignum *val; 433 int ok = 1; 434 435 val = crypto_bignum_init_set(buf, len); 436 if (!val || crypto_bignum_is_zero(val) || 437 crypto_bignum_cmp(val, prime) >= 0) 438 ok = 0; 439 crypto_bignum_deinit(val, 0); 440 return ok; 441 } 442 443 444 struct crypto_ec_point * eap_pwd_get_element(EAP_PWD_group *group, 445 const u8 *buf) 446 { 447 struct crypto_ec_point *element; 448 const struct crypto_bignum *prime; 449 size_t prime_len; 450 451 prime = crypto_ec_get_prime(group->group); 452 prime_len = crypto_ec_prime_len(group->group); 453 454 /* RFC 5931, 2.8.5.2.2: 0 < x,y < p */ 455 if (!eap_pwd_element_coord_ok(prime, buf, prime_len) || 456 !eap_pwd_element_coord_ok(prime, buf + prime_len, prime_len)) { 457 wpa_printf(MSG_INFO, "EAP-pwd: Invalid coordinate in element"); 458 return NULL; 459 } 460 461 element = crypto_ec_point_from_bin(group->group, buf); 462 if (!element) { 463 wpa_printf(MSG_INFO, "EAP-pwd: EC point from element failed"); 464 return NULL; 465 } 466 467 /* RFC 5931, 2.8.5.2.2: on curve and not the point at infinity */ 468 if (!crypto_ec_point_is_on_curve(group->group, element) || 469 crypto_ec_point_is_at_infinity(group->group, element)) { 470 wpa_printf(MSG_INFO, "EAP-pwd: Invalid element"); 471 goto fail; 472 } 473 474 out: 475 return element; 476 fail: 477 crypto_ec_point_deinit(element, 0); 478 element = NULL; 479 goto out; 480 } 481 482 483 struct crypto_bignum * eap_pwd_get_scalar(EAP_PWD_group *group, const u8 *buf) 484 { 485 struct crypto_bignum *scalar; 486 const struct crypto_bignum *order; 487 size_t order_len; 488 489 order = crypto_ec_get_order(group->group); 490 order_len = crypto_ec_order_len(group->group); 491 492 /* RFC 5931, 2.8.5.2: 1 < scalar < r */ 493 scalar = crypto_bignum_init_set(buf, order_len); 494 if (!scalar || crypto_bignum_is_zero(scalar) || 495 crypto_bignum_is_one(scalar) || 496 crypto_bignum_cmp(scalar, order) >= 0) { 497 wpa_printf(MSG_INFO, "EAP-pwd: received scalar is invalid"); 498 crypto_bignum_deinit(scalar, 0); 499 scalar = NULL; 500 } 501 502 return scalar; 503 } 504 505 506 int eap_pwd_get_rand_mask(EAP_PWD_group *group, struct crypto_bignum *_rand, 507 struct crypto_bignum *_mask, 508 struct crypto_bignum *scalar) 509 { 510 const struct crypto_bignum *order; 511 int count; 512 513 order = crypto_ec_get_order(group->group); 514 515 /* Select two random values rand,mask such that 1 < rand,mask < r and 516 * rand + mask mod r > 1. */ 517 for (count = 0; count < 100; count++) { 518 if (crypto_bignum_rand(_rand, order) == 0 && 519 !crypto_bignum_is_zero(_rand) && 520 crypto_bignum_rand(_mask, order) == 0 && 521 !crypto_bignum_is_zero(_mask) && 522 crypto_bignum_add(_rand, _mask, scalar) == 0 && 523 crypto_bignum_mod(scalar, order, scalar) == 0 && 524 !crypto_bignum_is_zero(scalar) && 525 !crypto_bignum_is_one(scalar)) 526 return 0; 527 } 528 529 wpa_printf(MSG_INFO, "EAP-pwd: unable to get randomness"); 530 return -1; 531 } 532