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