Skip to content
This repository was archived by the owner on Aug 24, 2025. It is now read-only.

Commit ab91952

Browse files
paulidalemattcaswell
authored andcommitted
Address a timing side channel whereby it is possible to determine some
information about the length of the scalar used in ECDSA operations from a large number (2^32) of signatures. This doesn't rate as a CVE because: * For the non-constant time code, there are easier ways to extract more information. * For the constant time code, it requires a significant number of signatures to leak a small amount of information. Thanks to Neals Fournaise, Eliane Jaulmes and Jean-Rene Reinhard for reporting this issue. Reviewed-by: Andy Polyakov <appro@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from openssl#4576) (cherry picked from commit 4a089bb)
1 parent 7184480 commit ab91952

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

crypto/ec/ecdsa_ossl.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
2+
* Copyright 2002-2017 The OpenSSL Project Authors. All Rights Reserved.
33
*
44
* Licensed under the OpenSSL license (the "License"). You may not use
55
* this file except in compliance with the License. You can obtain a copy
@@ -41,6 +41,7 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
4141
EC_POINT *tmp_point = NULL;
4242
const EC_GROUP *group;
4343
int ret = 0;
44+
int order_bits;
4445

4546
if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
4647
ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
@@ -77,6 +78,13 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
7778
goto err;
7879
}
7980

81+
/* Preallocate space */
82+
order_bits = BN_num_bits(order);
83+
if (!BN_set_bit(k, order_bits)
84+
|| !BN_set_bit(r, order_bits)
85+
|| !BN_set_bit(X, order_bits))
86+
goto err;
87+
8088
do {
8189
/* get random k */
8290
do
@@ -100,13 +108,19 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
100108
/*
101109
* We do not want timing information to leak the length of k, so we
102110
* compute G*k using an equivalent scalar of fixed bit-length.
111+
*
112+
* We unconditionally perform both of these additions to prevent a
113+
* small timing information leakage. We then choose the sum that is
114+
* one bit longer than the order. This guarantees the code
115+
* path used in the constant time implementations elsewhere.
116+
*
117+
* TODO: revisit the BN_copy aiming for a memory access agnostic
118+
* conditional copy.
103119
*/
104-
105-
if (!BN_add(k, k, order))
120+
if (!BN_add(r, k, order)
121+
|| !BN_add(X, r, order)
122+
|| !BN_copy(k, BN_num_bits(r) > order_bits ? r : X))
106123
goto err;
107-
if (BN_num_bits(k) <= BN_num_bits(order))
108-
if (!BN_add(k, k, order))
109-
goto err;
110124

111125
/* compute r the x-coordinate of generator * k */
112126
if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {

0 commit comments

Comments
 (0)