2 * EAP server/peer: EAP-pwd shared routines
3 * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #include "utils/const_time.h"
12 #include "crypto/sha256.h"
13 #include "crypto/crypto.h"
15 #include "eap_pwd_common.h"
17 #define MAX_ECC_PRIME_LEN 66
20 /* The random function H(x) = HMAC-SHA256(0^32, x) */
21 struct crypto_hash * eap_pwd_h_init(void)
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,
30 void eap_pwd_h_update(struct crypto_hash *hash, const u8 *data, size_t len)
32 crypto_hash_update(hash, data, len);
36 void eap_pwd_h_final(struct crypto_hash *hash, u8 *digest)
38 size_t len = SHA256_MAC_LEN;
39 crypto_hash_finish(hash, digest, &len);
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)
47 struct crypto_hash *hash;
48 u8 digest[SHA256_MAC_LEN];
50 size_t resultbytelen, len = 0, mdlen;
52 resultbytelen = (resultbitlen + 7) / 8;
54 L = htons(resultbitlen);
55 while (len < resultbytelen) {
58 hash = crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256,
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)
70 if ((len + mdlen) > resultbytelen)
71 os_memcpy(result + len, digest, resultbytelen - len);
73 os_memcpy(result + len, digest, mdlen);
77 /* since we're expanding to a bit length, mask off the excess */
78 if (resultbitlen % 8) {
80 mask <<= (8 - (resultbitlen % 8));
81 result[resultbytelen - 1] &= mask;
88 static int eap_pwd_suitable_group(u16 num)
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;
97 EAP_PWD_group * get_eap_pwd_group(u16 num)
101 if (!eap_pwd_suitable_group(num)) {
102 wpa_printf(MSG_INFO, "EAP-pwd: unsuitable group %u", num);
105 grp = os_zalloc(sizeof(EAP_PWD_group));
108 grp->group = crypto_ec_init(num);
110 wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC group");
115 grp->group_num = num;
116 wpa_printf(MSG_INFO, "EAP-pwd: provisioned group %d", num);
122 static void buf_shift_right(u8 *buf, size_t len, size_t bits)
125 for (i = len - 1; i > 0; i--)
126 buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
132 * compute a "random" secret point on an elliptic curve based
133 * on the password and identities.
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,
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_*
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;
161 os_memset(x_bin, 0, sizeof(x_bin));
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");
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 "
180 if (crypto_bignum_sub(prime, one, pm1) < 0)
183 /* get a random quadratic residue and nonresidue */
184 while (!qr || !qnr) {
185 if (crypto_bignum_rand(tmp1, prime) < 0)
187 res = crypto_bignum_legendre(tmp1, prime);
188 if (!qr && res == 1) {
190 tmp1 = crypto_bignum_init();
191 } else if (!qnr && res == -1) {
193 tmp1 = crypto_bignum_init();
198 if (crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin),
200 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin),
204 os_memset(prfbuf, 0, primebytelen);
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.
216 * compute counter-mode password value and stretch to prime
217 * pwd-seed = H(token | peer-id | server-id | password |
220 hash = eap_pwd_h_init();
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);
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)
238 buf_shift_right(prfbuf, primebytelen,
239 8 - primebitlen % 8);
241 crypto_bignum_deinit(x_candidate, 1);
242 x_candidate = crypto_bignum_init_set(prfbuf, primebytelen);
245 "EAP-pwd: unable to create x_candidate");
249 if (crypto_bignum_cmp(x_candidate, prime) >= 0)
252 wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate",
253 prfbuf, primebytelen);
254 const_time_select_bin(found, x_bin, prfbuf, primebytelen,
258 * compute y^2 using the equation of the curve
262 crypto_bignum_deinit(tmp2, 1);
263 tmp2 = crypto_ec_point_compute_y_sqr(grp->group, x_candidate);
268 * mask tmp2 so doing legendre won't leak timing info
270 * tmp1 is a random number between 1 and p-1
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)
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.
282 * Flip a coin, multiply by the random quadratic residue or the
283 * random quadratic nonresidue and record heads or tails.
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,
289 crypto_bignum_deinit(qr_or_qnr, 1);
290 qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, primebytelen);
292 crypto_bignum_mulmod(tmp2, qr_or_qnr, prime, tmp2) < 0)
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).
301 res = crypto_bignum_legendre(tmp2, prime);
304 mask = const_time_eq(res, check);
305 found_ctr = const_time_select_u8(found, found_ctr, ctr);
310 "EAP-pwd: unable to find random point on curve for group %d, something's fishy",
316 * We know x_candidate is a quadratic residue so set it here.
318 crypto_bignum_deinit(x_candidate, 1);
319 x_candidate = crypto_bignum_init_set(x_bin, primebytelen);
321 crypto_ec_point_solve_y_coord(grp->group, grp->pwe, x_candidate,
323 wpa_printf(MSG_INFO, "EAP-pwd: Could not solve for y");
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.
332 if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) {
333 wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve");
337 wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr);
341 crypto_ec_point_deinit(grp->pwe, 1);
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));
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)
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;
375 prime_len = crypto_ec_prime_len(grp->group);
376 order_len = crypto_ec_order_len(grp->group);
378 cruft = os_malloc(prime_len);
383 * first compute the session-id = TypeCode | H(ciphersuite | scal_p |
386 session_id[0] = EAP_TYPE_PWD;
387 hash = eap_pwd_h_init();
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]);
399 /* then compute MK = H(k | confirm-peer | confirm-server) */
400 hash = eap_pwd_h_init();
405 crypto_bignum_to_bin(k, cruft, prime_len, prime_len);
406 eap_pwd_h_update(hash, cruft, prime_len);
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);
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) {
419 os_memcpy(msk, msk_emsk, EAP_MSK_LEN);
420 os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN);
426 static int eap_pwd_element_coord_ok(const struct crypto_bignum *prime,
427 const u8 *buf, size_t len)
429 struct crypto_bignum *val;
432 val = crypto_bignum_init_set(buf, len);
433 if (!val || crypto_bignum_is_zero(val) ||
434 crypto_bignum_cmp(val, prime) >= 0)
436 crypto_bignum_deinit(val, 0);
441 struct crypto_ec_point * eap_pwd_get_element(EAP_PWD_group *group,
444 struct crypto_ec_point *element;
445 const struct crypto_bignum *prime;
448 prime = crypto_ec_get_prime(group->group);
449 prime_len = crypto_ec_prime_len(group->group);
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");
458 element = crypto_ec_point_from_bin(group->group, buf);
460 wpa_printf(MSG_INFO, "EAP-pwd: EC point from element failed");
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");
474 crypto_ec_point_deinit(element, 0);
480 struct crypto_bignum * eap_pwd_get_scalar(EAP_PWD_group *group, const u8 *buf)
482 struct crypto_bignum *scalar;
483 const struct crypto_bignum *order;
486 order = crypto_ec_get_order(group->group);
487 order_len = crypto_ec_order_len(group->group);
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);
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)
507 const struct crypto_bignum *order;
510 order = crypto_ec_get_order(group->group);
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))
526 wpa_printf(MSG_INFO, "EAP-pwd: unable to get randomness");