062-0004-EAP-pwd-Use-constant-time-and-memory-access-for-find.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. From aaf65feac67c3993935634eefe5bc76b9fce03aa Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <[email protected]>
  3. Date: Tue, 26 Feb 2019 11:59:45 +0200
  4. Subject: [PATCH 04/14] EAP-pwd: Use constant time and memory access for
  5. finding the PWE
  6. This algorithm could leak information to external observers in form of
  7. timing differences or memory access patterns (cache use). While the
  8. previous implementation had protection against the most visible timing
  9. differences (looping 40 rounds and masking the legendre operation), it
  10. did not protect against memory access patterns between the two possible
  11. code paths in the masking operations. That might be sufficient to allow
  12. an unprivileged process running on the same device to be able to
  13. determine which path is being executed through a cache attack and based
  14. on that, determine information about the used password.
  15. Convert the PWE finding loop to use constant time functions and
  16. identical memory access path without different branches for the QR/QNR
  17. cases to minimize possible side-channel information similarly to the
  18. changes done for SAE authentication. (CVE-2019-9495)
  19. Signed-off-by: Jouni Malinen <[email protected]>
  20. ---
  21. src/eap_common/eap_pwd_common.c | 187 +++++++++++++++++++++-------------------
  22. 1 file changed, 99 insertions(+), 88 deletions(-)
  23. --- a/src/eap_common/eap_pwd_common.c
  24. +++ b/src/eap_common/eap_pwd_common.c
  25. @@ -8,11 +8,15 @@
  26. #include "includes.h"
  27. #include "common.h"
  28. +#include "utils/const_time.h"
  29. #include "crypto/sha256.h"
  30. #include "crypto/crypto.h"
  31. #include "eap_defs.h"
  32. #include "eap_pwd_common.h"
  33. +#define MAX_ECC_PRIME_LEN 66
  34. +
  35. +
  36. /* The random function H(x) = HMAC-SHA256(0^32, x) */
  37. struct crypto_hash * eap_pwd_h_init(void)
  38. {
  39. @@ -102,6 +106,15 @@ EAP_PWD_group * get_eap_pwd_group(u16 nu
  40. }
  41. +static void buf_shift_right(u8 *buf, size_t len, size_t bits)
  42. +{
  43. + size_t i;
  44. + for (i = len - 1; i > 0; i--)
  45. + buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
  46. + buf[0] >>= bits;
  47. +}
  48. +
  49. +
  50. /*
  51. * compute a "random" secret point on an elliptic curve based
  52. * on the password and identities.
  53. @@ -113,17 +126,27 @@ int compute_password_element(EAP_PWD_gro
  54. const u8 *token)
  55. {
  56. struct crypto_bignum *qr = NULL, *qnr = NULL, *one = NULL;
  57. + struct crypto_bignum *qr_or_qnr = NULL;
  58. + u8 qr_bin[MAX_ECC_PRIME_LEN];
  59. + u8 qnr_bin[MAX_ECC_PRIME_LEN];
  60. + u8 qr_or_qnr_bin[MAX_ECC_PRIME_LEN];
  61. + u8 x_bin[MAX_ECC_PRIME_LEN];
  62. struct crypto_bignum *tmp1 = NULL, *tmp2 = NULL, *pm1 = NULL;
  63. struct crypto_hash *hash;
  64. unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr;
  65. - int is_odd, ret = 0, check, found = 0;
  66. - size_t primebytelen, primebitlen;
  67. - struct crypto_bignum *x_candidate = NULL, *rnd = NULL, *cofactor = NULL;
  68. + int ret = 0, check, res;
  69. + u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
  70. + * mask */
  71. + size_t primebytelen = 0, primebitlen;
  72. + struct crypto_bignum *x_candidate = NULL, *cofactor = NULL;
  73. const struct crypto_bignum *prime;
  74. + u8 mask, found_ctr = 0, is_odd = 0;
  75. if (grp->pwe)
  76. return -1;
  77. + os_memset(x_bin, 0, sizeof(x_bin));
  78. +
  79. prime = crypto_ec_get_prime(grp->group);
  80. cofactor = crypto_bignum_init();
  81. grp->pwe = crypto_ec_point_init(grp->group);
  82. @@ -152,8 +175,6 @@ int compute_password_element(EAP_PWD_gro
  83. /* get a random quadratic residue and nonresidue */
  84. while (!qr || !qnr) {
  85. - int res;
  86. -
  87. if (crypto_bignum_rand(tmp1, prime) < 0)
  88. goto fail;
  89. res = crypto_bignum_legendre(tmp1, prime);
  90. @@ -167,6 +188,11 @@ int compute_password_element(EAP_PWD_gro
  91. if (!tmp1)
  92. goto fail;
  93. }
  94. + if (crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin),
  95. + primebytelen) < 0 ||
  96. + crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin),
  97. + primebytelen) < 0)
  98. + goto fail;
  99. os_memset(prfbuf, 0, primebytelen);
  100. ctr = 0;
  101. @@ -194,17 +220,16 @@ int compute_password_element(EAP_PWD_gro
  102. eap_pwd_h_update(hash, &ctr, sizeof(ctr));
  103. eap_pwd_h_final(hash, pwe_digest);
  104. - crypto_bignum_deinit(rnd, 1);
  105. - rnd = crypto_bignum_init_set(pwe_digest, SHA256_MAC_LEN);
  106. - if (!rnd) {
  107. - wpa_printf(MSG_INFO, "EAP-pwd: unable to create rnd");
  108. - goto fail;
  109. - }
  110. + is_odd = const_time_select_u8(
  111. + found, is_odd, pwe_digest[SHA256_MAC_LEN - 1] & 0x01);
  112. if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN,
  113. (u8 *) "EAP-pwd Hunting And Pecking",
  114. os_strlen("EAP-pwd Hunting And Pecking"),
  115. prfbuf, primebitlen) < 0)
  116. goto fail;
  117. + if (primebitlen % 8)
  118. + buf_shift_right(prfbuf, primebytelen,
  119. + 8 - primebitlen % 8);
  120. crypto_bignum_deinit(x_candidate, 1);
  121. x_candidate = crypto_bignum_init_set(prfbuf, primebytelen);
  122. @@ -214,24 +239,13 @@ int compute_password_element(EAP_PWD_gro
  123. goto fail;
  124. }
  125. - /*
  126. - * eap_pwd_kdf() returns a string of bits 0..primebitlen but
  127. - * BN_bin2bn will treat that string of bits as a big endian
  128. - * number. If the primebitlen is not an even multiple of 8
  129. - * then excessive bits-- those _after_ primebitlen-- so now
  130. - * we have to shift right the amount we masked off.
  131. - */
  132. - if ((primebitlen % 8) &&
  133. - crypto_bignum_rshift(x_candidate,
  134. - (8 - (primebitlen % 8)),
  135. - x_candidate) < 0)
  136. - goto fail;
  137. -
  138. if (crypto_bignum_cmp(x_candidate, prime) >= 0)
  139. continue;
  140. - wpa_hexdump(MSG_DEBUG, "EAP-pwd: x_candidate",
  141. - prfbuf, primebytelen);
  142. + wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate",
  143. + prfbuf, primebytelen);
  144. + const_time_select_bin(found, x_bin, prfbuf, primebytelen,
  145. + x_bin);
  146. /*
  147. * compute y^2 using the equation of the curve
  148. @@ -260,13 +274,15 @@ int compute_password_element(EAP_PWD_gro
  149. * Flip a coin, multiply by the random quadratic residue or the
  150. * random quadratic nonresidue and record heads or tails.
  151. */
  152. - if (crypto_bignum_is_odd(tmp1)) {
  153. - crypto_bignum_mulmod(tmp2, qr, prime, tmp2);
  154. - check = 1;
  155. - } else {
  156. - crypto_bignum_mulmod(tmp2, qnr, prime, tmp2);
  157. - check = -1;
  158. - }
  159. + mask = const_time_eq_u8(crypto_bignum_is_odd(tmp1), 1);
  160. + check = const_time_select_s8(mask, 1, -1);
  161. + const_time_select_bin(mask, qr_bin, qnr_bin, primebytelen,
  162. + qr_or_qnr_bin);
  163. + crypto_bignum_deinit(qr_or_qnr, 1);
  164. + qr_or_qnr = crypto_bignum_init_set(qr_or_qnr_bin, primebytelen);
  165. + if (!qr_or_qnr ||
  166. + crypto_bignum_mulmod(tmp2, qr_or_qnr, prime, tmp2) < 0)
  167. + goto fail;
  168. /*
  169. * Now it's safe to do legendre, if check is 1 then it's
  170. @@ -274,59 +290,12 @@ int compute_password_element(EAP_PWD_gro
  171. * change result), if check is -1 then it's the opposite test
  172. * (multiplying a qr by qnr would make a qnr).
  173. */
  174. - if (crypto_bignum_legendre(tmp2, prime) == check) {
  175. - if (found == 1)
  176. - continue;
  177. -
  178. - /* need to unambiguously identify the solution */
  179. - is_odd = crypto_bignum_is_odd(rnd);
  180. -
  181. - /*
  182. - * We know x_candidate is a quadratic residue so set
  183. - * it here.
  184. - */
  185. - if (crypto_ec_point_solve_y_coord(grp->group, grp->pwe,
  186. - x_candidate,
  187. - is_odd) != 0) {
  188. - wpa_printf(MSG_INFO,
  189. - "EAP-pwd: Could not solve for y");
  190. - continue;
  191. - }
  192. -
  193. - /*
  194. - * If there's a solution to the equation then the point
  195. - * must be on the curve so why check again explicitly?
  196. - * OpenSSL code says this is required by X9.62. We're
  197. - * not X9.62 but it can't hurt just to be sure.
  198. - */
  199. - if (!crypto_ec_point_is_on_curve(grp->group,
  200. - grp->pwe)) {
  201. - wpa_printf(MSG_INFO,
  202. - "EAP-pwd: point is not on curve");
  203. - continue;
  204. - }
  205. -
  206. - if (!crypto_bignum_is_one(cofactor)) {
  207. - /* make sure the point is not in a small
  208. - * sub-group */
  209. - if (crypto_ec_point_mul(grp->group, grp->pwe,
  210. - cofactor,
  211. - grp->pwe) != 0) {
  212. - wpa_printf(MSG_INFO,
  213. - "EAP-pwd: cannot multiply generator by order");
  214. - continue;
  215. - }
  216. - if (crypto_ec_point_is_at_infinity(grp->group,
  217. - grp->pwe)) {
  218. - wpa_printf(MSG_INFO,
  219. - "EAP-pwd: point is at infinity");
  220. - continue;
  221. - }
  222. - }
  223. - wpa_printf(MSG_DEBUG,
  224. - "EAP-pwd: found a PWE in %d tries", ctr);
  225. - found = 1;
  226. - }
  227. + res = crypto_bignum_legendre(tmp2, prime);
  228. + if (res == -2)
  229. + goto fail;
  230. + mask = const_time_eq(res, check);
  231. + found_ctr = const_time_select_u8(found, found_ctr, ctr);
  232. + found |= mask;
  233. }
  234. if (found == 0) {
  235. wpa_printf(MSG_INFO,
  236. @@ -334,6 +303,44 @@ int compute_password_element(EAP_PWD_gro
  237. num);
  238. goto fail;
  239. }
  240. +
  241. + /*
  242. + * We know x_candidate is a quadratic residue so set it here.
  243. + */
  244. + crypto_bignum_deinit(x_candidate, 1);
  245. + x_candidate = crypto_bignum_init_set(x_bin, primebytelen);
  246. + if (!x_candidate ||
  247. + crypto_ec_point_solve_y_coord(grp->group, grp->pwe, x_candidate,
  248. + is_odd) != 0) {
  249. + wpa_printf(MSG_INFO, "EAP-pwd: Could not solve for y");
  250. + goto fail;
  251. + }
  252. +
  253. + /*
  254. + * If there's a solution to the equation then the point must be on the
  255. + * curve so why check again explicitly? OpenSSL code says this is
  256. + * required by X9.62. We're not X9.62 but it can't hurt just to be sure.
  257. + */
  258. + if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) {
  259. + wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve");
  260. + goto fail;
  261. + }
  262. +
  263. + if (!crypto_bignum_is_one(cofactor)) {
  264. + /* make sure the point is not in a small sub-group */
  265. + if (crypto_ec_point_mul(grp->group, grp->pwe, cofactor,
  266. + grp->pwe) != 0) {
  267. + wpa_printf(MSG_INFO,
  268. + "EAP-pwd: cannot multiply generator by order");
  269. + goto fail;
  270. + }
  271. + if (crypto_ec_point_is_at_infinity(grp->group, grp->pwe)) {
  272. + wpa_printf(MSG_INFO, "EAP-pwd: point is at infinity");
  273. + goto fail;
  274. + }
  275. + }
  276. + wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr);
  277. +
  278. if (0) {
  279. fail:
  280. crypto_ec_point_deinit(grp->pwe, 1);
  281. @@ -343,14 +350,18 @@ int compute_password_element(EAP_PWD_gro
  282. /* cleanliness and order.... */
  283. crypto_bignum_deinit(cofactor, 1);
  284. crypto_bignum_deinit(x_candidate, 1);
  285. - crypto_bignum_deinit(rnd, 1);
  286. crypto_bignum_deinit(pm1, 0);
  287. crypto_bignum_deinit(tmp1, 1);
  288. crypto_bignum_deinit(tmp2, 1);
  289. crypto_bignum_deinit(qr, 1);
  290. crypto_bignum_deinit(qnr, 1);
  291. + crypto_bignum_deinit(qr_or_qnr, 1);
  292. crypto_bignum_deinit(one, 0);
  293. - os_free(prfbuf);
  294. + bin_clear_free(prfbuf, primebytelen);
  295. + os_memset(qr_bin, 0, sizeof(qr_bin));
  296. + os_memset(qnr_bin, 0, sizeof(qnr_bin));
  297. + os_memset(qr_or_qnr_bin, 0, sizeof(qr_or_qnr_bin));
  298. + os_memset(pwe_digest, 0, sizeof(pwe_digest));
  299. return ret;
  300. }