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