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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. From c93461c1d98f52681717a088776ab32fd97872b0 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <[email protected]>
  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 <[email protected]>
  11. ---
  12. src/crypto/crypto_openssl.c | 15 +++++++++------
  13. 1 file changed, 9 insertions(+), 6 deletions(-)
  14. --- a/src/crypto/crypto_openssl.c
  15. +++ b/src/crypto/crypto_openssl.c
  16. @@ -24,6 +24,7 @@
  17. #endif /* CONFIG_ECC */
  18. #include "common.h"
  19. +#include "utils/const_time.h"
  20. #include "wpabuf.h"
  21. #include "dh_group5.h"
  22. #include "sha1.h"
  23. @@ -1435,6 +1436,7 @@ int crypto_bignum_legendre(const struct
  24. BN_CTX *bnctx;
  25. BIGNUM *exp = NULL, *tmp = NULL;
  26. int res = -2;
  27. + unsigned int mask;
  28. if (TEST_FAIL())
  29. return -2;
  30. @@ -1453,12 +1455,13 @@ int crypto_bignum_legendre(const struct
  31. (const BIGNUM *) p, bnctx, NULL))
  32. goto fail;
  33. - if (BN_is_word(tmp, 1))
  34. - res = 1;
  35. - else if (BN_is_zero(tmp))
  36. - res = 0;
  37. - else
  38. - res = -1;
  39. + /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
  40. + * constant time selection to avoid branches here. */
  41. + res = -1;
  42. + mask = const_time_eq(BN_is_word(tmp, 1), 1);
  43. + res = const_time_select_int(mask, 1, res);
  44. + mask = const_time_eq(BN_is_zero(tmp), 1);
  45. + res = const_time_select_int(mask, 0, res);
  46. fail:
  47. BN_clear_free(tmp);