]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
wolfssl: Fix crypto_bignum_rand() implementation
authorJouni Malinen <j@w1.fi>
Sat, 16 May 2020 18:02:17 +0000 (21:02 +0300)
committerJouni Malinen <j@w1.fi>
Sat, 16 May 2020 18:02:17 +0000 (21:02 +0300)
The previous implementation used mp_rand_prime() to generate a random
value in range 0..m. That is insanely slow way of generating a random
value since mp_rand_prime() is for generating a random _prime_ which is
not what is needed here. Replace that implementation with generationg of
a random value in the requested range without doing any kind of prime
number checks or loops to reject values that are not primes.

This speeds up SAE and EAP-pwd routines by couple of orders of
magnitude..

Signed-off-by: Jouni Malinen <j@w1.fi>
src/crypto/crypto_wolfssl.c

index dc68bd651f5695c267aae11c9e2196d7708f3895..2e4bf8962b550b9bbdb9843024803d2de9bc5bc1 100644 (file)
@@ -1104,19 +1104,21 @@ int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
 {
        int ret = 0;
        WC_RNG rng;
+       size_t len;
+       u8 *buf;
 
        if (TEST_FAIL())
                return -1;
        if (wc_InitRng(&rng) != 0)
                return -1;
-       if (mp_rand_prime((mp_int *) r,
-                         (mp_count_bits((mp_int *) m) + 7) / 8 * 2,
-                         &rng, NULL) != 0)
-               ret = -1;
-       if (ret == 0 &&
+       len = (mp_count_bits((mp_int *) m) + 7) / 8;
+       buf = os_malloc(len);
+       if (!buf || wc_RNG_GenerateBlock(&rng, buf, len) != 0 ||
+           mp_read_unsigned_bin((mp_int *) r, buf, len) != MP_OKAY ||
            mp_mod((mp_int *) r, (mp_int *) m, (mp_int *) r) != 0)
                ret = -1;
        wc_FreeRng(&rng);
+       bin_clear_free(buf, len);
        return ret;
 }