0006-SAE-Avoid-branches-in-is_quadratic_residue_blind.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. From 362704dda04507e7ebb8035122e83d9f0ae7c320 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <jouni@codeaurora.org>
  3. Date: Tue, 26 Feb 2019 19:34:38 +0200
  4. Subject: [PATCH 06/14] SAE: Avoid branches in is_quadratic_residue_blind()
  5. Make the non-failure path in the function proceed without branches based
  6. on r_odd and in constant time to minimize risk of observable differences
  7. in timing or cache use. (CVE-2019-9494)
  8. Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
  9. ---
  10. src/common/sae.c | 64 ++++++++++++++++++++++++++++++++------------------------
  11. 1 file changed, 37 insertions(+), 27 deletions(-)
  12. diff --git a/src/common/sae.c b/src/common/sae.c
  13. index d55323b..5df9b95 100644
  14. --- a/src/common/sae.c
  15. +++ b/src/common/sae.c
  16. @@ -232,12 +232,14 @@ get_rand_1_to_p_1(const u8 *prime, size_t prime_len, size_t prime_bits,
  17. static int is_quadratic_residue_blind(struct sae_data *sae,
  18. const u8 *prime, size_t bits,
  19. - const struct crypto_bignum *qr,
  20. - const struct crypto_bignum *qnr,
  21. + const u8 *qr, const u8 *qnr,
  22. const struct crypto_bignum *y_sqr)
  23. {
  24. - struct crypto_bignum *r, *num;
  25. + struct crypto_bignum *r, *num, *qr_or_qnr = NULL;
  26. int r_odd, check, res = -1;
  27. + u8 qr_or_qnr_bin[SAE_MAX_ECC_PRIME_LEN];
  28. + size_t prime_len = sae->tmp->prime_len;
  29. + unsigned int mask;
  30. /*
  31. * Use the blinding technique to mask y_sqr while determining
  32. @@ -248,7 +250,7 @@ static int is_quadratic_residue_blind(struct sae_data *sae,
  33. * r = a random number between 1 and p-1, inclusive
  34. * num = (v * r * r) modulo p
  35. */
  36. - r = get_rand_1_to_p_1(prime, sae->tmp->prime_len, bits, &r_odd);
  37. + r = get_rand_1_to_p_1(prime, prime_len, bits, &r_odd);
  38. if (!r)
  39. return -1;
  40. @@ -258,41 +260,45 @@ static int is_quadratic_residue_blind(struct sae_data *sae,
  41. crypto_bignum_mulmod(num, r, sae->tmp->prime, num) < 0)
  42. goto fail;
  43. - if (r_odd) {
  44. - /*
  45. - * num = (num * qr) module p
  46. - * LGR(num, p) = 1 ==> quadratic residue
  47. - */
  48. - if (crypto_bignum_mulmod(num, qr, sae->tmp->prime, num) < 0)
  49. - goto fail;
  50. - check = 1;
  51. - } else {
  52. - /*
  53. - * num = (num * qnr) module p
  54. - * LGR(num, p) = -1 ==> quadratic residue
  55. - */
  56. - if (crypto_bignum_mulmod(num, qnr, sae->tmp->prime, num) < 0)
  57. - goto fail;
  58. - check = -1;
  59. - }
  60. + /*
  61. + * Need to minimize differences in handling different cases, so try to
  62. + * avoid branches and timing differences.
  63. + *
  64. + * If r_odd:
  65. + * num = (num * qr) module p
  66. + * LGR(num, p) = 1 ==> quadratic residue
  67. + * else:
  68. + * num = (num * qnr) module p
  69. + * LGR(num, p) = -1 ==> quadratic residue
  70. + */
  71. + mask = const_time_is_zero(r_odd);
  72. + const_time_select_bin(mask, qnr, qr, prime_len, qr_or_qnr_bin);
  73. + qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, prime_len);
  74. + if (!qr_or_qnr ||
  75. + crypto_bignum_mulmod(num, qr_or_qnr, sae->tmp->prime, num) < 0)
  76. + goto fail;
  77. + /* r_odd is 0 or 1; branchless version of check = r_odd ? 1 : -1, */
  78. + check = const_time_select_int(mask, -1, 1);
  79. res = crypto_bignum_legendre(num, sae->tmp->prime);
  80. if (res == -2) {
  81. res = -1;
  82. goto fail;
  83. }
  84. - res = res == check;
  85. + /* branchless version of res = res == check
  86. + * (res is -1, 0, or 1; check is -1 or 1) */
  87. + mask = const_time_eq(res, check);
  88. + res = const_time_select_int(mask, 1, 0);
  89. fail:
  90. crypto_bignum_deinit(num, 1);
  91. crypto_bignum_deinit(r, 1);
  92. + crypto_bignum_deinit(qr_or_qnr, 1);
  93. return res;
  94. }
  95. static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
  96. - const u8 *prime,
  97. - const struct crypto_bignum *qr,
  98. - const struct crypto_bignum *qnr,
  99. + const u8 *prime, const u8 *qr, const u8 *qnr,
  100. u8 *pwd_value)
  101. {
  102. struct crypto_bignum *y_sqr, *x_cand;
  103. @@ -452,6 +458,8 @@ static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
  104. struct crypto_bignum *x = NULL, *qr = NULL, *qnr = NULL;
  105. u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
  106. u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
  107. + u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
  108. + u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
  109. size_t bits;
  110. int res = -1;
  111. u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
  112. @@ -476,7 +484,9 @@ static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
  113. * (qnr) modulo p for blinding purposes during the loop.
  114. */
  115. if (get_random_qr_qnr(prime, prime_len, sae->tmp->prime, bits,
  116. - &qr, &qnr) < 0)
  117. + &qr, &qnr) < 0 ||
  118. + crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
  119. + crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
  120. goto fail;
  121. wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
  122. @@ -527,7 +537,7 @@ static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
  123. break;
  124. res = sae_test_pwd_seed_ecc(sae, pwd_seed,
  125. - prime, qr, qnr, x_cand_bin);
  126. + prime, qr_bin, qnr_bin, x_cand_bin);
  127. const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
  128. x_bin);
  129. pwd_seed_odd = const_time_select_u8(
  130. --
  131. 2.7.4