061-0006-SAE-Avoid-branches-in-is_quadratic_residue_blind.patch 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. From 362704dda04507e7ebb8035122e83d9f0ae7c320 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <[email protected]>
  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 <[email protected]>
  9. ---
  10. src/common/sae.c | 64 ++++++++++++++++++++++++++++++++------------------------
  11. 1 file changed, 37 insertions(+), 27 deletions(-)
  12. --- a/src/common/sae.c
  13. +++ b/src/common/sae.c
  14. @@ -209,12 +209,14 @@ get_rand_1_to_p_1(const u8 *prime, size_
  15. static int is_quadratic_residue_blind(struct sae_data *sae,
  16. const u8 *prime, size_t bits,
  17. - const struct crypto_bignum *qr,
  18. - const struct crypto_bignum *qnr,
  19. + const u8 *qr, const u8 *qnr,
  20. const struct crypto_bignum *y_sqr)
  21. {
  22. - struct crypto_bignum *r, *num;
  23. + struct crypto_bignum *r, *num, *qr_or_qnr = NULL;
  24. int r_odd, check, res = -1;
  25. + u8 qr_or_qnr_bin[SAE_MAX_ECC_PRIME_LEN];
  26. + size_t prime_len = sae->tmp->prime_len;
  27. + unsigned int mask;
  28. /*
  29. * Use the blinding technique to mask y_sqr while determining
  30. @@ -225,7 +227,7 @@ static int is_quadratic_residue_blind(st
  31. * r = a random number between 1 and p-1, inclusive
  32. * num = (v * r * r) modulo p
  33. */
  34. - r = get_rand_1_to_p_1(prime, sae->tmp->prime_len, bits, &r_odd);
  35. + r = get_rand_1_to_p_1(prime, prime_len, bits, &r_odd);
  36. if (!r)
  37. return -1;
  38. @@ -235,41 +237,45 @@ static int is_quadratic_residue_blind(st
  39. crypto_bignum_mulmod(num, r, sae->tmp->prime, num) < 0)
  40. goto fail;
  41. - if (r_odd) {
  42. - /*
  43. - * num = (num * qr) module p
  44. - * LGR(num, p) = 1 ==> quadratic residue
  45. - */
  46. - if (crypto_bignum_mulmod(num, qr, sae->tmp->prime, num) < 0)
  47. - goto fail;
  48. - check = 1;
  49. - } else {
  50. - /*
  51. - * num = (num * qnr) module p
  52. - * LGR(num, p) = -1 ==> quadratic residue
  53. - */
  54. - if (crypto_bignum_mulmod(num, qnr, sae->tmp->prime, num) < 0)
  55. - goto fail;
  56. - check = -1;
  57. - }
  58. + /*
  59. + * Need to minimize differences in handling different cases, so try to
  60. + * avoid branches and timing differences.
  61. + *
  62. + * If r_odd:
  63. + * num = (num * qr) module p
  64. + * LGR(num, p) = 1 ==> quadratic residue
  65. + * else:
  66. + * num = (num * qnr) module p
  67. + * LGR(num, p) = -1 ==> quadratic residue
  68. + */
  69. + mask = const_time_is_zero(r_odd);
  70. + const_time_select_bin(mask, qnr, qr, prime_len, qr_or_qnr_bin);
  71. + qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, prime_len);
  72. + if (!qr_or_qnr ||
  73. + crypto_bignum_mulmod(num, qr_or_qnr, sae->tmp->prime, num) < 0)
  74. + goto fail;
  75. + /* r_odd is 0 or 1; branchless version of check = r_odd ? 1 : -1, */
  76. + check = const_time_select_int(mask, -1, 1);
  77. res = crypto_bignum_legendre(num, sae->tmp->prime);
  78. if (res == -2) {
  79. res = -1;
  80. goto fail;
  81. }
  82. - res = res == check;
  83. + /* branchless version of res = res == check
  84. + * (res is -1, 0, or 1; check is -1 or 1) */
  85. + mask = const_time_eq(res, check);
  86. + res = const_time_select_int(mask, 1, 0);
  87. fail:
  88. crypto_bignum_deinit(num, 1);
  89. crypto_bignum_deinit(r, 1);
  90. + crypto_bignum_deinit(qr_or_qnr, 1);
  91. return res;
  92. }
  93. static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
  94. - const u8 *prime,
  95. - const struct crypto_bignum *qr,
  96. - const struct crypto_bignum *qnr,
  97. + const u8 *prime, const u8 *qr, const u8 *qnr,
  98. u8 *pwd_value)
  99. {
  100. struct crypto_bignum *y_sqr, *x_cand;
  101. @@ -429,6 +435,8 @@ static int sae_derive_pwe_ecc(struct sae
  102. struct crypto_bignum *x = NULL, *qr = NULL, *qnr = NULL;
  103. u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
  104. u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
  105. + u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
  106. + u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
  107. size_t bits;
  108. int res = -1;
  109. u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
  110. @@ -453,7 +461,9 @@ static int sae_derive_pwe_ecc(struct sae
  111. * (qnr) modulo p for blinding purposes during the loop.
  112. */
  113. if (get_random_qr_qnr(prime, prime_len, sae->tmp->prime, bits,
  114. - &qr, &qnr) < 0)
  115. + &qr, &qnr) < 0 ||
  116. + crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
  117. + crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
  118. goto fail;
  119. wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
  120. @@ -504,7 +514,7 @@ static int sae_derive_pwe_ecc(struct sae
  121. break;
  122. res = sae_test_pwd_seed_ecc(sae, pwd_seed,
  123. - prime, qr, qnr, x_cand_bin);
  124. + prime, qr_bin, qnr_bin, x_cand_bin);
  125. const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
  126. x_bin);
  127. pwd_seed_odd = const_time_select_u8(