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
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.
13 This speeds up SAE and EAP-pwd routines by couple of orders of
16 Signed-off-by: Jouni Malinen <j@w1.fi>
18 src/crypto/crypto_wolfssl.c | 12 +++++++-----
19 1 file changed, 7 insertions(+), 5 deletions(-)
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
32 if (wc_InitRng(&rng) != 0)
34 - if (mp_rand_prime((mp_int *) r,
35 - (mp_count_bits((mp_int *) m) + 7) / 8 * 2,
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)
46 + bin_clear_free(buf, len);