2464b63489c8b055fa7ae2dea2b4ef0b001d36e5
[openwrt/staging/luka.git] /
1 From eb595b3e3ab531645a5bde71cf6385335b7a4b95 Mon Sep 17 00:00:00 2001
2 From: Jouni Malinen <j@w1.fi>
3 Date: Sat, 16 May 2020 21:02:17 +0300
4 Subject: [PATCH 2/3] wolfssl: Fix crypto_bignum_rand() implementation
5
6 The previous implementation used mp_rand_prime() to generate a random
7 value in range 0..m. That is insanely slow way of generating a random
8 value since mp_rand_prime() is for generating a random _prime_ which is
9 not what is needed here. Replace that implementation with generationg of
10 a random value in the requested range without doing any kind of prime
11 number checks or loops to reject values that are not primes.
12
13 This speeds up SAE and EAP-pwd routines by couple of orders of
14 magnitude..
15
16 Signed-off-by: Jouni Malinen <j@w1.fi>
17 ---
18 src/crypto/crypto_wolfssl.c | 12 +++++++-----
19 1 file changed, 7 insertions(+), 5 deletions(-)
20
21 --- a/src/crypto/crypto_wolfssl.c
22 +++ b/src/crypto/crypto_wolfssl.c
23 @@ -1084,19 +1084,21 @@ int crypto_bignum_rand(struct crypto_big
24 {
25 int ret = 0;
26 WC_RNG rng;
27 + size_t len;
28 + u8 *buf;
29
30 if (TEST_FAIL())
31 return -1;
32 if (wc_InitRng(&rng) != 0)
33 return -1;
34 - if (mp_rand_prime((mp_int *) r,
35 - (mp_count_bits((mp_int *) m) + 7) / 8 * 2,
36 - &rng, NULL) != 0)
37 - ret = -1;
38 - if (ret == 0 &&
39 + len = (mp_count_bits((mp_int *) m) + 7) / 8;
40 + buf = os_malloc(len);
41 + if (!buf || wc_RNG_GenerateBlock(&rng, buf, len) != 0 ||
42 + mp_read_unsigned_bin((mp_int *) r, buf, len) != MP_OKAY ||
43 mp_mod((mp_int *) r, (mp_int *) m, (mp_int *) r) != 0)
44 ret = -1;
45 wc_FreeRng(&rng);
46 + bin_clear_free(buf, len);
47 return ret;
48 }
49