066-0004-SAE-Run-through-prf-result-processing-even-if-it-pri.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. From a25b48118d75f3c2d7cb1b2c3b4cffb13091a34c Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <[email protected]>
  3. Date: Mon, 24 Jun 2019 23:01:06 +0300
  4. Subject: [PATCH 4/6] SAE: Run through prf result processing even if it >=
  5. prime
  6. This reduces differences in timing and memory access within the
  7. hunting-and-pecking loop for ECC groups that have a prime that is not
  8. close to a power of two (e.g., Brainpool curves).
  9. Signed-off-by: Jouni Malinen <[email protected]>
  10. (cherry picked from commit 147bf7b88a9c231322b5b574263071ca6dbb0503)
  11. ---
  12. src/common/sae.c | 15 ++++++++++++---
  13. 1 file changed, 12 insertions(+), 3 deletions(-)
  14. --- a/src/common/sae.c
  15. +++ b/src/common/sae.c
  16. @@ -304,6 +304,8 @@ static int sae_test_pwd_seed_ecc(struct
  17. struct crypto_bignum *y_sqr, *x_cand;
  18. int res;
  19. size_t bits;
  20. + int cmp_prime;
  21. + unsigned int in_range;
  22. wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
  23. @@ -317,8 +319,13 @@ static int sae_test_pwd_seed_ecc(struct
  24. wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
  25. pwd_value, sae->tmp->prime_len);
  26. - if (const_time_memcmp(pwd_value, prime, sae->tmp->prime_len) >= 0)
  27. - return 0;
  28. + cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
  29. + /* Create a const_time mask for selection based on prf result
  30. + * being smaller than prime. */
  31. + in_range = const_time_fill_msb((unsigned int) cmp_prime);
  32. + /* The algorithm description would skip the next steps if
  33. + * cmp_prime >= 0 (reutnr 0 here), but go through them regardless to
  34. + * minimize externally observable differences in behavior. */
  35. x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
  36. if (!x_cand)
  37. @@ -330,7 +337,9 @@ static int sae_test_pwd_seed_ecc(struct
  38. res = is_quadratic_residue_blind(sae, prime, bits, qr, qnr, y_sqr);
  39. crypto_bignum_deinit(y_sqr, 1);
  40. - return res;
  41. + if (res < 0)
  42. + return res;
  43. + return const_time_select_int(in_range, res, 0);
  44. }