0001-OpenSSL-Use-constant-time-operations-for-private-big.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. From d42c477cc794163a3757956bbffca5cea000923c Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <jouni@codeaurora.org>
  3. Date: Tue, 26 Feb 2019 11:43:03 +0200
  4. Subject: [PATCH 01/14] OpenSSL: Use constant time operations for private
  5. bignums
  6. This helps in reducing measurable timing differences in operations
  7. involving private information. BoringSSL has removed BN_FLG_CONSTTIME
  8. and expects specific constant time functions to be called instead, so a
  9. bit different approach is needed depending on which library is used.
  10. The main operation that needs protection against side channel attacks is
  11. BN_mod_exp() that depends on private keys (the public key validation
  12. step in crypto_dh_derive_secret() is an exception that can use the
  13. faster version since it does not depend on private keys).
  14. crypto_bignum_div() is currently used only in SAE FFC case with not
  15. safe-prime groups and only with values that do not depend on private
  16. keys, so it is not critical to protect it.
  17. crypto_bignum_inverse() is currently used only in SAE FFC PWE
  18. derivation. The additional protection here is targeting only OpenSSL.
  19. BoringSSL may need conversion to using BN_mod_inverse_blinded().
  20. This is related to CVE-2019-9494 and CVE-2019-9495.
  21. Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
  22. ---
  23. src/crypto/crypto_openssl.c | 20 +++++++++++++++-----
  24. 1 file changed, 15 insertions(+), 5 deletions(-)
  25. diff --git a/src/crypto/crypto_openssl.c b/src/crypto/crypto_openssl.c
  26. index 9c2ba58..ac53cc8 100644
  27. --- a/src/crypto/crypto_openssl.c
  28. +++ b/src/crypto/crypto_openssl.c
  29. @@ -607,7 +607,8 @@ int crypto_mod_exp(const u8 *base, size_t base_len,
  30. bn_result == NULL)
  31. goto error;
  32. - if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  33. + if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
  34. + ctx, NULL) != 1)
  35. goto error;
  36. *result_len = BN_bn2bin(bn_result, result);
  37. @@ -1360,8 +1361,9 @@ int crypto_bignum_exptmod(const struct crypto_bignum *a,
  38. bnctx = BN_CTX_new();
  39. if (bnctx == NULL)
  40. return -1;
  41. - res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  42. - (const BIGNUM *) c, bnctx);
  43. + res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
  44. + (const BIGNUM *) b, (const BIGNUM *) c,
  45. + bnctx, NULL);
  46. BN_CTX_free(bnctx);
  47. return res ? 0 : -1;
  48. @@ -1380,6 +1382,11 @@ int crypto_bignum_inverse(const struct crypto_bignum *a,
  49. bnctx = BN_CTX_new();
  50. if (bnctx == NULL)
  51. return -1;
  52. +#ifdef OPENSSL_IS_BORINGSSL
  53. + /* TODO: use BN_mod_inverse_blinded() ? */
  54. +#else /* OPENSSL_IS_BORINGSSL */
  55. + BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
  56. +#endif /* OPENSSL_IS_BORINGSSL */
  57. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  58. (const BIGNUM *) b, bnctx);
  59. BN_CTX_free(bnctx);
  60. @@ -1413,6 +1420,9 @@ int crypto_bignum_div(const struct crypto_bignum *a,
  61. bnctx = BN_CTX_new();
  62. if (bnctx == NULL)
  63. return -1;
  64. +#ifndef OPENSSL_IS_BORINGSSL
  65. + BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
  66. +#endif /* OPENSSL_IS_BORINGSSL */
  67. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  68. (const BIGNUM *) b, bnctx);
  69. BN_CTX_free(bnctx);
  70. @@ -1504,8 +1514,8 @@ int crypto_bignum_legendre(const struct crypto_bignum *a,
  71. /* exp = (p-1) / 2 */
  72. !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
  73. !BN_rshift1(exp, exp) ||
  74. - !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
  75. - bnctx))
  76. + !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
  77. + (const BIGNUM *) p, bnctx, NULL))
  78. goto fail;
  79. if (BN_is_word(tmp, 1))
  80. --
  81. 2.7.4