2 * Shared Dragonfly functionality
3 * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2019, The Linux Foundation
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
13 #include "utils/const_time.h"
14 #include "crypto/crypto.h"
15 #include "dragonfly.h"
18 int dragonfly_suitable_group(int group, int ecc_only)
20 /* Enforce REVmd rules on which SAE groups are suitable for production
21 * purposes: FFC groups whose prime is >= 3072 bits and ECC groups
22 * defined over a prime field whose prime is >= 256 bits. Furthermore,
23 * ECC groups defined over a characteristic 2 finite field and ECC
24 * groups with a co-factor greater than 1 are not suitable. Disable
25 * groups that use Brainpool curves as well for now since they leak more
26 * timing information due to the prime not being close to a power of
28 return group == 19 || group == 20 || group == 21 ||
30 (group == 15 || group == 16 || group == 17 || group == 18));
34 unsigned int dragonfly_min_pwe_loop_iter(int group)
36 if (group == 22 || group == 23 || group == 24) {
37 /* FFC groups for which pwd-value is likely to be >= p
42 if (group == 1 || group == 2 || group == 5 || group == 14 ||
43 group == 15 || group == 16 || group == 17 || group == 18) {
44 /* FFC groups that have prime that is close to a power of two */
48 /* Default to 40 (this covers most ECC groups) */
53 int dragonfly_get_random_qr_qnr(const struct crypto_bignum *prime,
54 struct crypto_bignum **qr,
55 struct crypto_bignum **qnr)
59 while (!(*qr) || !(*qnr)) {
60 struct crypto_bignum *tmp;
63 tmp = crypto_bignum_init();
64 if (!tmp || crypto_bignum_rand(tmp, prime) < 0) {
65 crypto_bignum_deinit(tmp, 0);
69 res = crypto_bignum_legendre(tmp, prime);
70 if (res == 1 && !(*qr))
72 else if (res == -1 && !(*qnr))
75 crypto_bignum_deinit(tmp, 0);
80 crypto_bignum_deinit(*qr, 0);
81 crypto_bignum_deinit(*qnr, 0);
87 static struct crypto_bignum *
88 dragonfly_get_rand_1_to_p_1(const struct crypto_bignum *prime)
90 struct crypto_bignum *tmp, *pm1, *one;
92 tmp = crypto_bignum_init();
93 pm1 = crypto_bignum_init();
94 one = crypto_bignum_init_set((const u8 *) "\x01", 1);
95 if (!tmp || !pm1 || !one ||
96 crypto_bignum_sub(prime, one, pm1) < 0 ||
97 crypto_bignum_rand(tmp, pm1) < 0 ||
98 crypto_bignum_add(tmp, one, tmp) < 0) {
99 crypto_bignum_deinit(tmp, 0);
103 crypto_bignum_deinit(pm1, 0);
104 crypto_bignum_deinit(one, 0);
109 int dragonfly_is_quadratic_residue_blind(struct crypto_ec *ec,
110 const u8 *qr, const u8 *qnr,
111 const struct crypto_bignum *val)
113 struct crypto_bignum *r, *num, *qr_or_qnr = NULL;
115 u8 qr_or_qnr_bin[DRAGONFLY_MAX_ECC_PRIME_LEN];
116 const struct crypto_bignum *prime;
120 prime = crypto_ec_get_prime(ec);
121 prime_len = crypto_ec_prime_len(ec);
124 * Use a blinding technique to mask val while determining whether it is
125 * a quadratic residue modulo p to avoid leaking timing information
126 * while determining the Legendre symbol.
129 * r = a random number between 1 and p-1, inclusive
130 * num = (v * r * r) modulo p
132 r = dragonfly_get_rand_1_to_p_1(prime);
136 num = crypto_bignum_init();
138 crypto_bignum_mulmod(val, r, prime, num) < 0 ||
139 crypto_bignum_mulmod(num, r, prime, num) < 0)
143 * Need to minimize differences in handling different cases, so try to
144 * avoid branches and timing differences.
147 * num = (num * qr) module p
148 * LGR(num, p) = 1 ==> quadratic residue
150 * num = (num * qnr) module p
151 * LGR(num, p) = -1 ==> quadratic residue
153 * mask is set to !odd(r)
155 mask = const_time_is_zero(crypto_bignum_is_odd(r));
156 const_time_select_bin(mask, qnr, qr, prime_len, qr_or_qnr_bin);
157 qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, prime_len);
159 crypto_bignum_mulmod(num, qr_or_qnr, prime, num) < 0)
161 /* branchless version of check = odd(r) ? 1 : -1, */
162 check = const_time_select_int(mask, -1, 1);
164 /* Determine the Legendre symbol on the masked value */
165 res = crypto_bignum_legendre(num, prime);
170 /* branchless version of res = res == check
171 * (res is -1, 0, or 1; check is -1 or 1) */
172 mask = const_time_eq(res, check);
173 res = const_time_select_int(mask, 1, 0);
175 crypto_bignum_deinit(num, 1);
176 crypto_bignum_deinit(r, 1);
177 crypto_bignum_deinit(qr_or_qnr, 1);
182 static int dragonfly_get_rand_2_to_r_1(struct crypto_bignum *val,
183 const struct crypto_bignum *order)
185 return crypto_bignum_rand(val, order) == 0 &&
186 !crypto_bignum_is_zero(val) &&
187 !crypto_bignum_is_one(val);
191 int dragonfly_generate_scalar(const struct crypto_bignum *order,
192 struct crypto_bignum *_rand,
193 struct crypto_bignum *_mask,
194 struct crypto_bignum *scalar)
198 /* Select two random values rand,mask such that 1 < rand,mask < r and
199 * rand + mask mod r > 1. */
200 for (count = 0; count < 100; count++) {
201 if (dragonfly_get_rand_2_to_r_1(_rand, order) &&
202 dragonfly_get_rand_2_to_r_1(_mask, order) &&
203 crypto_bignum_add(_rand, _mask, scalar) == 0 &&
204 crypto_bignum_mod(scalar, order, scalar) == 0 &&
205 !crypto_bignum_is_zero(scalar) &&
206 !crypto_bignum_is_one(scalar))
210 /* This should not be reachable in practice if the random number
211 * generation is working. */
213 "dragonfly: Unable to get randomness for own scalar");