Skip to content

Commit 7d87c78

Browse files
committed
Avoid indexing by secret-data in ECDH table lookups
1 parent 389a9c5 commit 7d87c78

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/ecdh_impl.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
#endif
2020
#define WNAF_SIZE(w) ((WNAF_BITS + (w) - 1) / (w))
2121

22+
/* This is like `ECMULT_TABLE_GET_GE` but is constant time */
23+
#define ECDH_TABLE_GET_GE(r,pre,n,w) do { \
24+
int m; \
25+
int abs_n = (n) * (((n) > 0) * 2 - 1); \
26+
secp256k1_fe_t neg_y; \
27+
VERIFY_CHECK(((n) & 1) == 1); \
28+
VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
29+
VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
30+
for (m = 1; m < (1 << ((w) - 1)); m += 2) { \
31+
/* This loop is used to avoid secret data in array indices. See
32+
* the comment in ecmult_gen_impl.h for rationale. */ \
33+
secp256k1_fe_cmov(&(r)->x, &(pre)[(m - 1) / 2].x, m == abs_n); \
34+
secp256k1_fe_cmov(&(r)->y, &(pre)[(m - 1) / 2].y, m == abs_n); \
35+
} \
36+
(r)->infinity = 0; \
37+
secp256k1_fe_normalize_weak(&(r)->x); \
38+
secp256k1_fe_normalize_weak(&(r)->y); \
39+
secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
40+
secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
41+
} while(0)
42+
2243
/** Convert a number to WNAF notation. The number becomes represented by sum(2^{wi} * wnaf[i], i=0..return_val)
2344
* with the following guarantees:
2445
* - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
@@ -161,18 +182,18 @@ static void secp256k1_point_multiply(secp256k1_gej_t *r, const secp256k1_ge_t *a
161182
}
162183
#ifdef USE_ENDOMORPHISM
163184
n = wnaf_1[i];
164-
ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
185+
ECDH_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
165186
VERIFY_CHECK(n != 0);
166187
secp256k1_gej_add_ge(r, r, &tmpa);
167188

168189
n = wnaf_lam[i];
169-
ECMULT_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
190+
ECDH_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
170191
VERIFY_CHECK(n != 0);
171192
secp256k1_gej_add_ge(r, r, &tmpa);
172193
#else
173194
n = wnaf[i];
174195
VERIFY_CHECK(n != 0);
175-
ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
196+
ECDH_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
176197
secp256k1_gej_add_ge(r, r, &tmpa);
177198
#endif
178199
}

0 commit comments

Comments
 (0)