Skip to content

Commit 9e5e8f2

Browse files
committed
port fixes from master
via the simple expedient of copying the entire function, with some minor changes to work in v3
1 parent 600abbb commit 9e5e8f2

File tree

1 file changed

+66
-24
lines changed
  • src/modules/rlm_eap/types/rlm_eap_pwd

1 file changed

+66
-24
lines changed

src/modules/rlm_eap/types/rlm_eap_pwd/eap_pwd.c

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,16 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
248248
char const *id_peer, int id_peer_len,
249249
uint32_t *token)
250250
{
251-
BIGNUM *x_candidate = NULL, *rnd = NULL, *y_sqrd = NULL, *qr = NULL, *qnr = NULL;
252-
HMAC_CTX *ctx = NULL;
253-
uint8_t pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, *xbuf = NULL, *pm1buf = NULL, ctr;
254-
int nid, is_odd, primebitlen, primebytelen, ret = 0, found = 0, mask;
255-
int save, i, rbits, qr_or_qnr, save_is_odd = 0, cmp;
256-
unsigned int skip;
257-
258-
ctx = HMAC_CTX_new();
259-
if (ctx == NULL) {
260-
DEBUG("failed allocating HMAC context");
261-
goto fail;
262-
}
251+
BIGNUM *x_candidate = NULL, *rnd = NULL, *y_sqrd = NULL, *qr = NULL, *qnr = NULL, *y1 = NULL, *y2 = NULL, *y = NULL, *exp = NULL;
252+
EVP_MD_CTX *hmac_ctx;
253+
EVP_PKEY *hmac_pkey;
254+
uint8_t pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, *xbuf = NULL, *pm1buf = NULL, *y1buf = NULL, *y2buf = NULL, *ybuf = NULL, ctr;
255+
int nid, is_odd, primebitlen, primebytelen, ret = 0, found = 0, mask;
256+
int save, i, rbits, qr_or_qnr, save_is_odd = 0, cmp;
257+
unsigned int skip;
258+
259+
MEM(hmac_ctx = EVP_MD_CTX_new());
260+
MEM(hmac_pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, allzero, sizeof(allzero)));
263261

264262
switch (grp_num) { /* from IANA registry for IKE D-H groups */
265263
case 19:
@@ -303,7 +301,11 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
303301
((qr = consttime_BN()) == NULL) ||
304302
((qnr = consttime_BN()) == NULL) ||
305303
((x_candidate = consttime_BN()) == NULL) ||
306-
((y_sqrd = consttime_BN()) == NULL)) {
304+
((y_sqrd = consttime_BN()) == NULL) ||
305+
((y1 = consttime_BN()) == NULL) ||
306+
((y2 = consttime_BN()) == NULL) ||
307+
((y = consttime_BN()) == NULL) ||
308+
((exp = consttime_BN()) == NULL)) {
307309
DEBUG("unable to create bignums");
308310
goto fail;
309311
}
@@ -332,6 +334,19 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
332334
DEBUG("unable to alloc space for pm1 buffer");
333335
goto fail;
334336
}
337+
if ((y1buf = talloc_zero_array(request, uint8_t, primebytelen)) == NULL) {
338+
DEBUG("unable to alloc space for y1 buffer");
339+
goto fail;
340+
}
341+
if ((y2buf = talloc_zero_array(request, uint8_t, primebytelen)) == NULL) {
342+
DEBUG("unable to alloc space for y2 buffer");
343+
goto fail;
344+
}
345+
if ((ybuf = talloc_zero_array(request, uint8_t, primebytelen)) == NULL) {
346+
DEBUG("unable to alloc space for y buffer");
347+
goto fail;
348+
}
349+
335350

336351
/*
337352
* derive random quadradic residue and quadratic non-residue
@@ -361,13 +376,19 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
361376
* pwd-seed = H(token | peer-id | server-id | password |
362377
* counter)
363378
*/
364-
HMAC_Init_ex(ctx, allzero, SHA256_DIGEST_LENGTH, EVP_sha256(),NULL);
365-
HMAC_Update(ctx, (uint8_t *)token, sizeof(*token));
366-
HMAC_Update(ctx, (uint8_t const *)id_peer, id_peer_len);
367-
HMAC_Update(ctx, (uint8_t const *)id_server, id_server_len);
368-
HMAC_Update(ctx, (uint8_t const *)password, password_len);
369-
HMAC_Update(ctx, (uint8_t *)&ctr, sizeof(ctr));
370-
pwd_hmac_final(ctx, pwe_digest);
379+
EVP_DigestSignInit(hmac_ctx, NULL, EVP_sha256(), NULL, hmac_pkey);
380+
EVP_DigestSignUpdate(hmac_ctx, (uint8_t *)token, sizeof(*token));
381+
EVP_DigestSignUpdate(hmac_ctx, (uint8_t const *)id_peer, id_peer_len);
382+
EVP_DigestSignUpdate(hmac_ctx, (uint8_t const *)id_server, id_server_len);
383+
EVP_DigestSignUpdate(hmac_ctx, (uint8_t const *)password, password_len);
384+
EVP_DigestSignUpdate(hmac_ctx, (uint8_t *)&ctr, sizeof(ctr));
385+
386+
{
387+
size_t mdlen = SHA256_DIGEST_LENGTH;
388+
389+
EVP_DigestSignFinal(hmac_ctx, pwe_digest, &mdlen);
390+
EVP_MD_CTX_reset(hmac_ctx);
391+
}
371392

372393
BN_bin2bn(pwe_digest, SHA256_DIGEST_LENGTH, rnd);
373394
eap_pwd_kdf(pwe_digest, SHA256_DIGEST_LENGTH, "EAP-pwd Hunting And Pecking",
@@ -401,7 +422,7 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
401422
* need to unambiguously identify the solution, if there is
402423
* one..
403424
*/
404-
is_odd = BN_is_odd(rnd) ? 1 : 0;
425+
is_odd = BN_is_odd(rnd);
405426

406427
/*
407428
* check whether x^3 + a*x + b is a quadratic residue
@@ -444,8 +465,21 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
444465
* now we can savely construct PWE
445466
*/
446467
BN_bin2bn(xbuf, primebytelen, x_candidate);
447-
if (!EC_POINT_set_compressed_coordinates(session->group, session->pwe,
448-
x_candidate, save_is_odd, NULL)) {
468+
do_equation(session->group, y_sqrd, x_candidate, session->bnctx);
469+
if ( !BN_add(exp, session->prime, BN_value_one()) ||
470+
!BN_rshift(exp, exp, 2) ||
471+
!BN_mod_exp_mont_consttime(y1, y_sqrd, exp, session->prime, session->bnctx, NULL) ||
472+
!BN_sub(y2, session->prime, y1) ||
473+
!BN_bn2bin(y1, y1buf) ||
474+
!BN_bn2bin(y2, y2buf)) {
475+
DEBUG("unable to compute y");
476+
goto fail;
477+
}
478+
mask = const_time_eq(save_is_odd, BN_is_odd(y1));
479+
const_time_select_bin(mask, y1buf, y2buf, primebytelen, ybuf);
480+
if (BN_bin2bn(ybuf, primebytelen, y) == NULL ||
481+
!EC_POINT_set_affine_coordinates(session->group, session->pwe, x_candidate, y, session->bnctx)) {
482+
DEBUG("unable to set point coordinate");
449483
goto fail;
450484
}
451485

@@ -461,12 +495,20 @@ int compute_password_element (REQUEST *request, pwd_session_t *session, uint16_t
461495
BN_clear_free(qr);
462496
BN_clear_free(qnr);
463497
BN_clear_free(rnd);
498+
BN_clear_free(y1);
499+
BN_clear_free(y2);
500+
BN_clear_free(y);
501+
BN_clear_free(exp);
464502

465503
if (prfbuf) talloc_free(prfbuf);
466504
if (xbuf) talloc_free(xbuf);
467505
if (pm1buf) talloc_free(pm1buf);
506+
if (y1buf) talloc_free(y1buf);
507+
if (y2buf) talloc_free(y2buf);
508+
if (ybuf) talloc_free(ybuf);
468509

469-
HMAC_CTX_free(ctx);
510+
EVP_MD_CTX_free(hmac_ctx);
511+
EVP_PKEY_free(hmac_pkey);
470512

471513
return ret;
472514
}

0 commit comments

Comments
 (0)