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