091-0002-wolfssl-Fix-crypto_bignum_rand-implementation.patch 1.5 KB

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