Skip to content

Commit eeb28d1

Browse files
committed
deps: float 949ff366 from openssl (ECDSA blinding)
Pending OpenSSL 1.0.2p release. Ref: https://www.nccgroup.trust/us/our-research/technical-advisory-return-of-the-hidden-number-problem/ Ref: nodejs#21345 Upstream: openssl/openssl@949ff366 Original commit message: Add blinding to an ECDSA signature Keegan Ryan (NCC Group) has demonstrated a side channel attack on an ECDSA signature operation. During signing the signer calculates: s:= k^-1 * (m + r * priv_key) mod order The addition operation above provides a sufficient signal for a flush+reload attack to derive the private key given sufficient signature operations. As a mitigation (based on a suggestion from Keegan) we add blinding to the operation so that: s := k^-1 * blind^-1 (blind * m + blind * r * priv_key) mod order Since this attack is a localhost side channel only no CVE is assigned. Reviewed-by: Rich Salz <rsalz@openssl.org>
1 parent 11b686f commit eeb28d1

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

deps/openssl/openssl/crypto/ecdsa/ecdsatest.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int restore_rand(void)
137137
return 1;
138138
}
139139

140-
static int fbytes_counter = 0;
140+
static int fbytes_counter = 0, use_fake = 0;
141141
static const char *numbers[8] = {
142142
"651056770906015076056810763456358567190100156695615665659",
143143
"6140507067065001063065065565667405560006161556565665656654",
@@ -158,6 +158,11 @@ int fbytes(unsigned char *buf, int num)
158158
int ret;
159159
BIGNUM *tmp = NULL;
160160

161+
if (use_fake == 0)
162+
return old_rand->bytes(buf, num);
163+
164+
use_fake = 0;
165+
161166
if (fbytes_counter >= 8)
162167
return 0;
163168
tmp = BN_new();
@@ -199,11 +204,13 @@ int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
199204
/* create the key */
200205
if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
201206
goto x962_int_err;
207+
use_fake = 1;
202208
if (!EC_KEY_generate_key(key))
203209
goto x962_int_err;
204210
BIO_printf(out, ".");
205211
(void)BIO_flush(out);
206212
/* create the signature */
213+
use_fake = 1;
207214
signature = ECDSA_do_sign(digest, 20, key);
208215
if (signature == NULL)
209216
goto x962_int_err;

deps/openssl/openssl/crypto/ecdsa/ecs_ossl.c

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
252252
{
253253
int ok = 0, i;
254254
BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
255+
BIGNUM *blind = NULL, *blindm = NULL;
255256
const BIGNUM *ckinv;
256257
BN_CTX *ctx = NULL;
257258
const EC_GROUP *group;
@@ -269,14 +270,25 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
269270
}
270271

271272
ret = ECDSA_SIG_new();
272-
if (!ret) {
273+
if (ret == NULL) {
273274
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
274275
return NULL;
275276
}
276277
s = ret->s;
277278

278-
if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
279-
(tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
279+
ctx = BN_CTX_new();
280+
if (ctx == NULL) {
281+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
282+
goto err;
283+
}
284+
285+
BN_CTX_start(ctx);
286+
order = BN_CTX_get(ctx);
287+
tmp = BN_CTX_get(ctx);
288+
m = BN_CTX_get(ctx);
289+
blind = BN_CTX_get(ctx);
290+
blindm = BN_CTX_get(ctx);
291+
if (blindm == NULL) {
280292
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
281293
goto err;
282294
}
@@ -315,26 +327,70 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
315327
}
316328
}
317329

318-
if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
330+
/*
331+
* The normal signature calculation is:
332+
*
333+
* s := k^-1 * (m + r * priv_key) mod order
334+
*
335+
* We will blind this to protect against side channel attacks
336+
*
337+
* s := k^-1 * blind^-1 * (blind * m + blind * r * priv_key) mod order
338+
*/
339+
340+
/* Generate a blinding value */
341+
do {
342+
if (!BN_rand(blind, BN_num_bits(order) - 1, -1, 0))
343+
goto err;
344+
} while (BN_is_zero(blind));
345+
BN_set_flags(blind, BN_FLG_CONSTTIME);
346+
BN_set_flags(blindm, BN_FLG_CONSTTIME);
347+
BN_set_flags(tmp, BN_FLG_CONSTTIME);
348+
349+
/* tmp := blind * priv_key * r mod order */
350+
if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
351+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
352+
goto err;
353+
}
354+
if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
355+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
356+
goto err;
357+
}
358+
359+
/* blindm := blind * m mod order */
360+
if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
361+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
362+
goto err;
363+
}
364+
365+
/* s : = (blind * priv_key * r) + (blind * m) mod order */
366+
if (!BN_mod_add_quick(s, tmp, blindm, order)) {
367+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
368+
goto err;
369+
}
370+
371+
/* s:= s * blind^-1 mod order */
372+
if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
319373
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
320374
goto err;
321375
}
322-
if (!BN_mod_add_quick(s, tmp, m, order)) {
376+
if (!BN_mod_mul(s, s, blind, order, ctx)) {
323377
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
324378
goto err;
325379
}
380+
381+
/* s := s * k^-1 mod order */
326382
if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
327383
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
328384
goto err;
329385
}
386+
330387
if (BN_is_zero(s)) {
331388
/*
332389
* if kinv and r have been supplied by the caller don't to
333390
* generate new kinv and r values
334391
*/
335392
if (in_kinv != NULL && in_r != NULL) {
336-
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
337-
ECDSA_R_NEED_NEW_SETUP_VALUES);
393+
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
338394
goto err;
339395
}
340396
} else
@@ -349,15 +405,11 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
349405
ECDSA_SIG_free(ret);
350406
ret = NULL;
351407
}
352-
if (ctx)
408+
if (ctx != NULL) {
409+
BN_CTX_end(ctx);
353410
BN_CTX_free(ctx);
354-
if (m)
355-
BN_clear_free(m);
356-
if (tmp)
357-
BN_clear_free(tmp);
358-
if (order)
359-
BN_free(order);
360-
if (kinv)
411+
}
412+
if (kinv != NULL)
361413
BN_clear_free(kinv);
362414
return ret;
363415
}

0 commit comments

Comments
 (0)