1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * Shared Dragonfly functionality
3*a1157835SDaniel Fojt  * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
4*a1157835SDaniel Fojt  * Copyright (c) 2019, The Linux Foundation
5*a1157835SDaniel Fojt  *
6*a1157835SDaniel Fojt  * This software may be distributed under the terms of the BSD license.
7*a1157835SDaniel Fojt  * See README for more details.
8*a1157835SDaniel Fojt  */
9*a1157835SDaniel Fojt 
10*a1157835SDaniel Fojt #include "utils/includes.h"
11*a1157835SDaniel Fojt 
12*a1157835SDaniel Fojt #include "utils/common.h"
13*a1157835SDaniel Fojt #include "utils/const_time.h"
14*a1157835SDaniel Fojt #include "crypto/crypto.h"
15*a1157835SDaniel Fojt #include "dragonfly.h"
16*a1157835SDaniel Fojt 
17*a1157835SDaniel Fojt 
dragonfly_suitable_group(int group,int ecc_only)18*a1157835SDaniel Fojt int dragonfly_suitable_group(int group, int ecc_only)
19*a1157835SDaniel Fojt {
20*a1157835SDaniel Fojt 	/* Enforce REVmd rules on which SAE groups are suitable for production
21*a1157835SDaniel Fojt 	 * purposes: FFC groups whose prime is >= 3072 bits and ECC groups
22*a1157835SDaniel Fojt 	 * defined over a prime field whose prime is >= 256 bits. Furthermore,
23*a1157835SDaniel Fojt 	 * ECC groups defined over a characteristic 2 finite field and ECC
24*a1157835SDaniel Fojt 	 * groups with a co-factor greater than 1 are not suitable. Disable
25*a1157835SDaniel Fojt 	 * groups that use Brainpool curves as well for now since they leak more
26*a1157835SDaniel Fojt 	 * timing information due to the prime not being close to a power of
27*a1157835SDaniel Fojt 	 * two. */
28*a1157835SDaniel Fojt 	return group == 19 || group == 20 || group == 21 ||
29*a1157835SDaniel Fojt 		(!ecc_only &&
30*a1157835SDaniel Fojt 		 (group == 15 || group == 16 || group == 17 || group == 18));
31*a1157835SDaniel Fojt }
32*a1157835SDaniel Fojt 
33*a1157835SDaniel Fojt 
dragonfly_min_pwe_loop_iter(int group)34*a1157835SDaniel Fojt unsigned int dragonfly_min_pwe_loop_iter(int group)
35*a1157835SDaniel Fojt {
36*a1157835SDaniel Fojt 	if (group == 22 || group == 23 || group == 24) {
37*a1157835SDaniel Fojt 		/* FFC groups for which pwd-value is likely to be >= p
38*a1157835SDaniel Fojt 		 * frequently */
39*a1157835SDaniel Fojt 		return 40;
40*a1157835SDaniel Fojt 	}
41*a1157835SDaniel Fojt 
42*a1157835SDaniel Fojt 	if (group == 1 || group == 2 || group == 5 || group == 14 ||
43*a1157835SDaniel Fojt 	    group == 15 || group == 16 || group == 17 || group == 18) {
44*a1157835SDaniel Fojt 		/* FFC groups that have prime that is close to a power of two */
45*a1157835SDaniel Fojt 		return 1;
46*a1157835SDaniel Fojt 	}
47*a1157835SDaniel Fojt 
48*a1157835SDaniel Fojt 	/* Default to 40 (this covers most ECC groups) */
49*a1157835SDaniel Fojt 	return 40;
50*a1157835SDaniel Fojt }
51*a1157835SDaniel Fojt 
52*a1157835SDaniel Fojt 
dragonfly_get_random_qr_qnr(const struct crypto_bignum * prime,struct crypto_bignum ** qr,struct crypto_bignum ** qnr)53*a1157835SDaniel Fojt int dragonfly_get_random_qr_qnr(const struct crypto_bignum *prime,
54*a1157835SDaniel Fojt 				struct crypto_bignum **qr,
55*a1157835SDaniel Fojt 				struct crypto_bignum **qnr)
56*a1157835SDaniel Fojt {
57*a1157835SDaniel Fojt 	*qr = *qnr = NULL;
58*a1157835SDaniel Fojt 
59*a1157835SDaniel Fojt 	while (!(*qr) || !(*qnr)) {
60*a1157835SDaniel Fojt 		struct crypto_bignum *tmp;
61*a1157835SDaniel Fojt 		int res;
62*a1157835SDaniel Fojt 
63*a1157835SDaniel Fojt 		tmp = crypto_bignum_init();
64*a1157835SDaniel Fojt 		if (!tmp || crypto_bignum_rand(tmp, prime) < 0) {
65*a1157835SDaniel Fojt 			crypto_bignum_deinit(tmp, 0);
66*a1157835SDaniel Fojt 			break;
67*a1157835SDaniel Fojt 		}
68*a1157835SDaniel Fojt 
69*a1157835SDaniel Fojt 		res = crypto_bignum_legendre(tmp, prime);
70*a1157835SDaniel Fojt 		if (res == 1 && !(*qr))
71*a1157835SDaniel Fojt 			*qr = tmp;
72*a1157835SDaniel Fojt 		else if (res == -1 && !(*qnr))
73*a1157835SDaniel Fojt 			*qnr = tmp;
74*a1157835SDaniel Fojt 		else
75*a1157835SDaniel Fojt 			crypto_bignum_deinit(tmp, 0);
76*a1157835SDaniel Fojt 	}
77*a1157835SDaniel Fojt 
78*a1157835SDaniel Fojt 	if (*qr && *qnr)
79*a1157835SDaniel Fojt 		return 0;
80*a1157835SDaniel Fojt 	crypto_bignum_deinit(*qr, 0);
81*a1157835SDaniel Fojt 	crypto_bignum_deinit(*qnr, 0);
82*a1157835SDaniel Fojt 	*qr = *qnr = NULL;
83*a1157835SDaniel Fojt 	return -1;
84*a1157835SDaniel Fojt }
85*a1157835SDaniel Fojt 
86*a1157835SDaniel Fojt 
87*a1157835SDaniel Fojt static struct crypto_bignum *
dragonfly_get_rand_1_to_p_1(const struct crypto_bignum * prime)88*a1157835SDaniel Fojt dragonfly_get_rand_1_to_p_1(const struct crypto_bignum *prime)
89*a1157835SDaniel Fojt {
90*a1157835SDaniel Fojt 	struct crypto_bignum *tmp, *pm1, *one;
91*a1157835SDaniel Fojt 
92*a1157835SDaniel Fojt 	tmp = crypto_bignum_init();
93*a1157835SDaniel Fojt 	pm1 = crypto_bignum_init();
94*a1157835SDaniel Fojt 	one = crypto_bignum_init_set((const u8 *) "\x01", 1);
95*a1157835SDaniel Fojt 	if (!tmp || !pm1 || !one ||
96*a1157835SDaniel Fojt 	    crypto_bignum_sub(prime, one, pm1) < 0 ||
97*a1157835SDaniel Fojt 	    crypto_bignum_rand(tmp, pm1) < 0 ||
98*a1157835SDaniel Fojt 	    crypto_bignum_add(tmp, one, tmp) < 0) {
99*a1157835SDaniel Fojt 		crypto_bignum_deinit(tmp, 0);
100*a1157835SDaniel Fojt 		tmp = NULL;
101*a1157835SDaniel Fojt 	}
102*a1157835SDaniel Fojt 
103*a1157835SDaniel Fojt 	crypto_bignum_deinit(pm1, 0);
104*a1157835SDaniel Fojt 	crypto_bignum_deinit(one, 0);
105*a1157835SDaniel Fojt 	return tmp;
106*a1157835SDaniel Fojt }
107*a1157835SDaniel Fojt 
108*a1157835SDaniel Fojt 
dragonfly_is_quadratic_residue_blind(struct crypto_ec * ec,const u8 * qr,const u8 * qnr,const struct crypto_bignum * val)109*a1157835SDaniel Fojt int dragonfly_is_quadratic_residue_blind(struct crypto_ec *ec,
110*a1157835SDaniel Fojt 					 const u8 *qr, const u8 *qnr,
111*a1157835SDaniel Fojt 					 const struct crypto_bignum *val)
112*a1157835SDaniel Fojt {
113*a1157835SDaniel Fojt 	struct crypto_bignum *r, *num, *qr_or_qnr = NULL;
114*a1157835SDaniel Fojt 	int check, res = -1;
115*a1157835SDaniel Fojt 	u8 qr_or_qnr_bin[DRAGONFLY_MAX_ECC_PRIME_LEN];
116*a1157835SDaniel Fojt 	const struct crypto_bignum *prime;
117*a1157835SDaniel Fojt 	size_t prime_len;
118*a1157835SDaniel Fojt 	unsigned int mask;
119*a1157835SDaniel Fojt 
120*a1157835SDaniel Fojt 	prime = crypto_ec_get_prime(ec);
121*a1157835SDaniel Fojt 	prime_len = crypto_ec_prime_len(ec);
122*a1157835SDaniel Fojt 
123*a1157835SDaniel Fojt 	/*
124*a1157835SDaniel Fojt 	 * Use a blinding technique to mask val while determining whether it is
125*a1157835SDaniel Fojt 	 * a quadratic residue modulo p to avoid leaking timing information
126*a1157835SDaniel Fojt 	 * while determining the Legendre symbol.
127*a1157835SDaniel Fojt 	 *
128*a1157835SDaniel Fojt 	 * v = val
129*a1157835SDaniel Fojt 	 * r = a random number between 1 and p-1, inclusive
130*a1157835SDaniel Fojt 	 * num = (v * r * r) modulo p
131*a1157835SDaniel Fojt 	 */
132*a1157835SDaniel Fojt 	r = dragonfly_get_rand_1_to_p_1(prime);
133*a1157835SDaniel Fojt 	if (!r)
134*a1157835SDaniel Fojt 		return -1;
135*a1157835SDaniel Fojt 
136*a1157835SDaniel Fojt 	num = crypto_bignum_init();
137*a1157835SDaniel Fojt 	if (!num ||
138*a1157835SDaniel Fojt 	    crypto_bignum_mulmod(val, r, prime, num) < 0 ||
139*a1157835SDaniel Fojt 	    crypto_bignum_mulmod(num, r, prime, num) < 0)
140*a1157835SDaniel Fojt 		goto fail;
141*a1157835SDaniel Fojt 
142*a1157835SDaniel Fojt 	/*
143*a1157835SDaniel Fojt 	 * Need to minimize differences in handling different cases, so try to
144*a1157835SDaniel Fojt 	 * avoid branches and timing differences.
145*a1157835SDaniel Fojt 	 *
146*a1157835SDaniel Fojt 	 * If r is odd:
147*a1157835SDaniel Fojt 	 * num = (num * qr) module p
148*a1157835SDaniel Fojt 	 * LGR(num, p) = 1 ==> quadratic residue
149*a1157835SDaniel Fojt 	 * else:
150*a1157835SDaniel Fojt 	 * num = (num * qnr) module p
151*a1157835SDaniel Fojt 	 * LGR(num, p) = -1 ==> quadratic residue
152*a1157835SDaniel Fojt 	 *
153*a1157835SDaniel Fojt 	 * mask is set to !odd(r)
154*a1157835SDaniel Fojt 	 */
155*a1157835SDaniel Fojt 	mask = const_time_is_zero(crypto_bignum_is_odd(r));
156*a1157835SDaniel Fojt 	const_time_select_bin(mask, qnr, qr, prime_len, qr_or_qnr_bin);
157*a1157835SDaniel Fojt 	qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, prime_len);
158*a1157835SDaniel Fojt 	if (!qr_or_qnr ||
159*a1157835SDaniel Fojt 	    crypto_bignum_mulmod(num, qr_or_qnr, prime, num) < 0)
160*a1157835SDaniel Fojt 		goto fail;
161*a1157835SDaniel Fojt 	/* branchless version of check = odd(r) ? 1 : -1, */
162*a1157835SDaniel Fojt 	check = const_time_select_int(mask, -1, 1);
163*a1157835SDaniel Fojt 
164*a1157835SDaniel Fojt 	/* Determine the Legendre symbol on the masked value */
165*a1157835SDaniel Fojt 	res = crypto_bignum_legendre(num, prime);
166*a1157835SDaniel Fojt 	if (res == -2) {
167*a1157835SDaniel Fojt 		res = -1;
168*a1157835SDaniel Fojt 		goto fail;
169*a1157835SDaniel Fojt 	}
170*a1157835SDaniel Fojt 	/* branchless version of res = res == check
171*a1157835SDaniel Fojt 	 * (res is -1, 0, or 1; check is -1 or 1) */
172*a1157835SDaniel Fojt 	mask = const_time_eq(res, check);
173*a1157835SDaniel Fojt 	res = const_time_select_int(mask, 1, 0);
174*a1157835SDaniel Fojt fail:
175*a1157835SDaniel Fojt 	crypto_bignum_deinit(num, 1);
176*a1157835SDaniel Fojt 	crypto_bignum_deinit(r, 1);
177*a1157835SDaniel Fojt 	crypto_bignum_deinit(qr_or_qnr, 1);
178*a1157835SDaniel Fojt 	return res;
179*a1157835SDaniel Fojt }
180*a1157835SDaniel Fojt 
181*a1157835SDaniel Fojt 
dragonfly_get_rand_2_to_r_1(struct crypto_bignum * val,const struct crypto_bignum * order)182*a1157835SDaniel Fojt static int dragonfly_get_rand_2_to_r_1(struct crypto_bignum *val,
183*a1157835SDaniel Fojt 				       const struct crypto_bignum *order)
184*a1157835SDaniel Fojt {
185*a1157835SDaniel Fojt 	return crypto_bignum_rand(val, order) == 0 &&
186*a1157835SDaniel Fojt 		!crypto_bignum_is_zero(val) &&
187*a1157835SDaniel Fojt 		!crypto_bignum_is_one(val);
188*a1157835SDaniel Fojt }
189*a1157835SDaniel Fojt 
190*a1157835SDaniel Fojt 
dragonfly_generate_scalar(const struct crypto_bignum * order,struct crypto_bignum * _rand,struct crypto_bignum * _mask,struct crypto_bignum * scalar)191*a1157835SDaniel Fojt int dragonfly_generate_scalar(const struct crypto_bignum *order,
192*a1157835SDaniel Fojt 			      struct crypto_bignum *_rand,
193*a1157835SDaniel Fojt 			      struct crypto_bignum *_mask,
194*a1157835SDaniel Fojt 			      struct crypto_bignum *scalar)
195*a1157835SDaniel Fojt {
196*a1157835SDaniel Fojt 	int count;
197*a1157835SDaniel Fojt 
198*a1157835SDaniel Fojt 	/* Select two random values rand,mask such that 1 < rand,mask < r and
199*a1157835SDaniel Fojt 	 * rand + mask mod r > 1. */
200*a1157835SDaniel Fojt 	for (count = 0; count < 100; count++) {
201*a1157835SDaniel Fojt 		if (dragonfly_get_rand_2_to_r_1(_rand, order) &&
202*a1157835SDaniel Fojt 		    dragonfly_get_rand_2_to_r_1(_mask, order) &&
203*a1157835SDaniel Fojt 		    crypto_bignum_add(_rand, _mask, scalar) == 0 &&
204*a1157835SDaniel Fojt 		    crypto_bignum_mod(scalar, order, scalar) == 0 &&
205*a1157835SDaniel Fojt 		    !crypto_bignum_is_zero(scalar) &&
206*a1157835SDaniel Fojt 		    !crypto_bignum_is_one(scalar))
207*a1157835SDaniel Fojt 			return 0;
208*a1157835SDaniel Fojt 	}
209*a1157835SDaniel Fojt 
210*a1157835SDaniel Fojt 	/* This should not be reachable in practice if the random number
211*a1157835SDaniel Fojt 	 * generation is working. */
212*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO,
213*a1157835SDaniel Fojt 		   "dragonfly: Unable to get randomness for own scalar");
214*a1157835SDaniel Fojt 	return -1;
215*a1157835SDaniel Fojt }
216