0003-OpenSSL-Use-constant-time-selection-for-crypto_bignu.patch 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. From c93461c1d98f52681717a088776ab32fd97872b0 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <jouni@codeaurora.org>
  3. Date: Fri, 8 Mar 2019 00:24:12 +0200
  4. Subject: [PATCH 03/14] OpenSSL: Use constant time selection for
  5. crypto_bignum_legendre()
  6. Get rid of the branches that depend on the result of the Legendre
  7. operation. This is needed to avoid leaking information about different
  8. temporary results in blinding mechanisms.
  9. This is related to CVE-2019-9494 and CVE-2019-9495.
  10. Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
  11. ---
  12. src/crypto/crypto_openssl.c | 15 +++++++++------
  13. 1 file changed, 9 insertions(+), 6 deletions(-)
  14. diff --git a/src/crypto/crypto_openssl.c b/src/crypto/crypto_openssl.c
  15. index ac53cc8..0f52101 100644
  16. --- a/src/crypto/crypto_openssl.c
  17. +++ b/src/crypto/crypto_openssl.c
  18. @@ -24,6 +24,7 @@
  19. #endif /* CONFIG_ECC */
  20. #include "common.h"
  21. +#include "utils/const_time.h"
  22. #include "wpabuf.h"
  23. #include "dh_group5.h"
  24. #include "sha1.h"
  25. @@ -1500,6 +1501,7 @@ int crypto_bignum_legendre(const struct crypto_bignum *a,
  26. BN_CTX *bnctx;
  27. BIGNUM *exp = NULL, *tmp = NULL;
  28. int res = -2;
  29. + unsigned int mask;
  30. if (TEST_FAIL())
  31. return -2;
  32. @@ -1518,12 +1520,13 @@ int crypto_bignum_legendre(const struct crypto_bignum *a,
  33. (const BIGNUM *) p, bnctx, NULL))
  34. goto fail;
  35. - if (BN_is_word(tmp, 1))
  36. - res = 1;
  37. - else if (BN_is_zero(tmp))
  38. - res = 0;
  39. - else
  40. - res = -1;
  41. + /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
  42. + * constant time selection to avoid branches here. */
  43. + res = -1;
  44. + mask = const_time_eq(BN_is_word(tmp, 1), 1);
  45. + res = const_time_select_int(mask, 1, res);
  46. + mask = const_time_eq(BN_is_zero(tmp), 1);
  47. + res = const_time_select_int(mask, 0, res);
  48. fail:
  49. BN_clear_free(tmp);
  50. --
  51. 2.7.4