Skip to content

Commit 3fd873c

Browse files
authored
Merge pull request #12129 from dgarske/ecc_scalar_len
Check the ECC scalar length without timing that depends on its top bit, and clear the mp_int in mp_rand
2 parents 3f58629 + 243bb78 commit 3fd873c

5 files changed

Lines changed: 162 additions & 15 deletions

File tree

‎tests/api/test_ecc.c‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,6 +4471,116 @@ int test_wc_ecc_mulmod_unreduced(void)
44714471
return EXPECT_RESULT();
44724472
} /* END test_wc_ecc_mulmod_unreduced */
44734473

4474+
#if defined(HAVE_ECC) && defined(WOLFSSL_PUBLIC_MP) && \
4475+
!defined(NO_ECC_SECP) && \
4476+
!(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
4477+
defined(WOLFSSL_MICROCHIP_TA100)) && \
4478+
!defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_SELFTEST) && \
4479+
!defined(HAVE_FIPS) && !defined(WOLFSSL_NO_ECC_SW)
4480+
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
4481+
(ECC_MIN_KEY_SZ <= 256) && \
4482+
(!defined(WOLFSSL_SP_MATH) || !defined(WOLFSSL_SP_NO_256))
4483+
#define TEST_ECC_K_LEN_P256
4484+
#endif
4485+
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
4486+
(ECC_MIN_KEY_SZ <= 521) && \
4487+
(!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_521))
4488+
#define TEST_ECC_K_LEN_P521
4489+
#endif
4490+
#if defined(TEST_ECC_K_LEN_P256) || defined(TEST_ECC_K_LEN_P521)
4491+
#define TEST_ECC_MULMOD_K_LEN
4492+
#endif
4493+
#endif
4494+
4495+
#ifdef TEST_ECC_MULMOD_K_LEN
4496+
/* Set k to 2^bit. */
4497+
static int test_ecc_k_pow2(mp_int* k, int bit)
4498+
{
4499+
byte buf[80];
4500+
int len = bit / 8 + 1;
4501+
4502+
if ((bit < 0) || (len > (int)sizeof(buf))) {
4503+
return BAD_FUNC_ARG;
4504+
}
4505+
XMEMSET(buf, 0, sizeof(buf));
4506+
buf[0] = (byte)(1 << (bit % 8));
4507+
return mp_read_unsigned_bin(k, buf, (word32)len);
4508+
}
4509+
4510+
/* Check the scalar length limit of wc_ecc_mulmod() on one curve. */
4511+
static int test_ecc_mulmod_k_len_curve(int curveId)
4512+
{
4513+
EXPECT_DECLS;
4514+
const ecc_set_type* dp = NULL;
4515+
ecc_point* g = NULL;
4516+
ecc_point* r = NULL;
4517+
mp_int a;
4518+
mp_int prime;
4519+
mp_int k;
4520+
int bits = 0;
4521+
4522+
XMEMSET(&a, 0, sizeof(mp_int));
4523+
XMEMSET(&prime, 0, sizeof(mp_int));
4524+
XMEMSET(&k, 0, sizeof(mp_int));
4525+
4526+
ExpectNotNull(dp = wc_ecc_get_curve_params(
4527+
wc_ecc_get_curve_idx(curveId)));
4528+
ExpectIntEQ(mp_init_multi(&a, &prime, &k, NULL, NULL, NULL), 0);
4529+
ExpectNotNull(g = wc_ecc_new_point());
4530+
ExpectNotNull(r = wc_ecc_new_point());
4531+
if ((dp != NULL) && (g != NULL)) {
4532+
ExpectIntEQ(mp_read_radix(&a, dp->Af, MP_RADIX_HEX), 0);
4533+
ExpectIntEQ(mp_read_radix(&prime, dp->prime, MP_RADIX_HEX), 0);
4534+
ExpectIntEQ(mp_read_radix(g->x, dp->Gx, MP_RADIX_HEX), 0);
4535+
ExpectIntEQ(mp_read_radix(g->y, dp->Gy, MP_RADIX_HEX), 0);
4536+
ExpectIntEQ(mp_set(g->z, 1), 0);
4537+
bits = mp_count_bits(&prime);
4538+
}
4539+
4540+
/* A scalar one bit shorter than the prime is accepted. */
4541+
ExpectIntEQ(test_ecc_k_pow2(&k, bits - 1), 0);
4542+
ExpectIntEQ(wc_ecc_mulmod(&k, g, r, &a, &prime, 1), 0);
4543+
/* Two bits longer than the prime is rejected. One bit longer is not
4544+
* checked: some paths allow it. */
4545+
ExpectIntEQ(test_ecc_k_pow2(&k, bits + 1), 0);
4546+
ExpectIntEQ(wc_ecc_mulmod(&k, g, r, &a, &prime, 1),
4547+
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
4548+
/* A bit in a digit above the prime's top digit is rejected. */
4549+
ExpectIntEQ(test_ecc_k_pow2(&k, bits + 64), 0);
4550+
ExpectIntEQ(wc_ecc_mulmod(&k, g, r, &a, &prime, 1),
4551+
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
4552+
4553+
wc_ecc_del_point(r);
4554+
wc_ecc_del_point(g);
4555+
mp_free(&k);
4556+
mp_free(&prime);
4557+
mp_free(&a);
4558+
4559+
return EXPECT_RESULT();
4560+
}
4561+
#endif
4562+
4563+
/*
4564+
* Testing the scalar length limit of wc_ecc_mulmod() around the bit length of
4565+
* the prime.
4566+
*/
4567+
int test_wc_ecc_mulmod_k_len(void)
4568+
{
4569+
EXPECT_DECLS;
4570+
#ifdef TEST_ECC_MULMOD_K_LEN
4571+
#ifdef TEST_ECC_K_LEN_P256
4572+
ExpectIntEQ(test_ecc_mulmod_k_len_curve(ECC_SECP256R1), TEST_SUCCESS);
4573+
#endif
4574+
#ifdef TEST_ECC_K_LEN_P521
4575+
ExpectIntEQ(test_ecc_mulmod_k_len_curve(ECC_SECP521R1), TEST_SUCCESS);
4576+
#endif
4577+
#ifdef FP_ECC
4578+
wc_ecc_fp_free();
4579+
#endif
4580+
#endif
4581+
return EXPECT_RESULT();
4582+
} /* END test_wc_ecc_mulmod_k_len */
4583+
44744584
#if defined(HAVE_ECC) && defined(WOLFSSL_PUBLIC_MP) && \
44754585
defined(WOLFSSL_PUBLIC_ECC_ADD_DBL) && !defined(WOLFSSL_SP_MATH) && \
44764586
!defined(NO_ECC256) && !defined(NO_ECC_SECP) && \

‎tests/api/test_ecc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int test_wc_ecc_shared_secret_ssh(void);
7878
int test_wc_ecc_verify_hash_ex(void);
7979
int test_wc_ecc_mulmod(void);
8080
int test_wc_ecc_mulmod_unreduced(void);
81+
int test_wc_ecc_mulmod_k_len(void);
8182
int test_wc_ecc_mulmod_zero(void);
8283
int test_wc_ecc_mulmod_negative(void);
8384
int test_wc_ecc_mulmod_curves(void);
@@ -151,6 +152,7 @@ int test_wc_ecc_fp_cache_curve_binding(void);
151152
TEST_DECL_GROUP("ecc", test_wc_ecc_verify_hash_ex), \
152153
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod), \
153154
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod_unreduced), \
155+
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod_k_len), \
154156
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod_zero), \
155157
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod_negative), \
156158
TEST_DECL_GROUP("ecc", test_wc_ecc_mulmod_curves), \

‎tests/api/test_wolfmath.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ int test_mp_rand(void)
352352
mp_int a;
353353
WC_RNG rng;
354354
int digits = 1;
355+
byte wide[5 * sizeof(mp_digit)];
355356

356357
XMEMSET(&a, 0, sizeof(mp_int));
357358
XMEMSET(&rng, 0, sizeof(WC_RNG));
@@ -366,12 +367,19 @@ int test_mp_rand(void)
366367
* rejected: drives the "digits > size" operand of that guard true (the
367368
* valid call below drives it false). This rejection only exists on the
368369
* fixed-size backends; USE_INTEGER_HEAP_MATH grows the mp_int instead
369-
* (mp_set_bit) and would legally succeed (and force a large allocation),
370+
* (mp_2expt) and would legally succeed (and force a large allocation),
370371
* so the vector is limited to the fixed-size backends. */
371372
#ifndef USE_INTEGER_HEAP_MATH
372373
ExpectIntNE(mp_rand(&a, 100000, &rng), 0);
373374
#endif
374375
ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
376+
/* A wider value already in a must not survive into the result. */
377+
XMEMSET(wide, 0xff, sizeof(wide));
378+
ExpectIntEQ(mp_read_unsigned_bin(&a, wide, (word32)sizeof(wide)),
379+
MP_OKAY);
380+
ExpectIntEQ(mp_rand(&a, 2, &rng), 0);
381+
ExpectIntLE(mp_count_bits(&a), 2 * DIGIT_BIT);
382+
ExpectIntGT(mp_count_bits(&a), DIGIT_BIT);
375383

376384
mp_clear(&a);
377385
DoExpectIntEQ(wc_FreeRng(&rng), 0);

‎wolfcrypt/src/ecc.c‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,32 @@ static void alt_fp_init(mp_int* a)
20962096
}
20972097
#endif /* ALT_ECC_SIZE */
20982098

2099+
/* Returns 1 when the scalar k has more than bits bits, otherwise 0.
2100+
*
2101+
* mp_count_bits() takes time that depends on the position of the top set bit.
2102+
* This ORs together every bit of k at or above bits instead, and reduces the
2103+
* result to 0 or 1 without a comparison, so only the number of digits in k
2104+
* decides the time taken.
2105+
*/
2106+
static WC_MAYBE_UNUSED int ecc_k_too_long(const mp_int* k, int bits)
2107+
{
2108+
int i;
2109+
int top = bits / DIGIT_BIT;
2110+
int n = mp_get_digit_count(k);
2111+
mp_digit d = 0;
2112+
2113+
if (top < n) {
2114+
d = (mp_digit)(mp_get_digit(k, top) >> (bits % DIGIT_BIT));
2115+
for (i = top + 1; i < n; i++) {
2116+
d |= mp_get_digit(k, i);
2117+
}
2118+
}
2119+
2120+
/* Top bit of d | -d is set only when d is not zero. */
2121+
d |= (mp_digit)(0 - d);
2122+
return (int)(d >> (sizeof(mp_digit) * CHAR_BIT - 1));
2123+
}
2124+
20992125

21002126
#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
21012127
!defined(WOLFSSL_NO_ECC_SW) && \
@@ -3900,7 +3926,7 @@ static int ecc_sp_mulmod(const mp_int* k, ecc_point* G, ecc_point* R,
39003926
* and y only. It writes the result's digits but not their signs, so the
39013927
* result point's ordinates must not be negative either. Leave anything
39023928
* else to the generic code, which handles them. */
3903-
if (mp_isneg(k) || (mp_count_bits(k) > bits)) {
3929+
if (mp_isneg(k) || ecc_k_too_long(k, bits)) {
39043930
return NOT_COMPILED_IN;
39053931
}
39063932
if (mp_isneg(G->x) || mp_isneg(G->y) ||
@@ -4003,7 +4029,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
40034029
}
40044030

40054031
/* k can't be negative or have more bits than modulus count plus 1 */
4006-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus) + 1)) {
4032+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus) + 1)) {
40074033
err = ECC_OUT_OF_RANGE_E;
40084034
goto exit;
40094035
}
@@ -4107,7 +4133,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
41074133
/* For supported curves the order is the same length in bits as the modulus.
41084134
* Can't have more than order bits for the scalar.
41094135
*/
4110-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus))) {
4136+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus))) {
41114137
return ECC_OUT_OF_RANGE_E;
41124138
}
41134139
if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
@@ -4235,14 +4261,14 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
42354261

42364262
#ifdef HAVE_ECC_CDH
42374263
if (mp_count_bits(modulus) > mp_count_bits(order)) {
4238-
if (mp_count_bits(k) > mp_count_bits(modulus)) {
4264+
if (ecc_k_too_long(k, mp_count_bits(modulus))) {
42394265
return ECC_OUT_OF_RANGE_E;
42404266
}
42414267
}
42424268
else
42434269
#endif
42444270
/* k can't have more bits than order */
4245-
if (mp_count_bits(k) > mp_count_bits(order)) {
4271+
if (ecc_k_too_long(k, mp_count_bits(order))) {
42464272
WOLFSSL_MSG("Private key length is greater than order in bits.");
42474273
return ECC_OUT_OF_RANGE_E;
42484274
}
@@ -4336,7 +4362,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
43364362
if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
43374363
return ECC_BAD_ARG_E;
43384364
}
4339-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus))) {
4365+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus))) {
43404366
return ECC_OUT_OF_RANGE_E;
43414367
}
43424368
if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
@@ -13691,7 +13717,7 @@ int wc_ecc_sig_size(const ecc_key* key)
1369113717
#define FP_COMB_MAX_MOD_SZ 126
1369213718
#endif
1369313719
/* The comb covers the bits of the modulus. An order can be a bit longer. */
13694-
#define FP_K_FITS_COMB(k, m) ((mp_count_bits(k) <= mp_count_bits(m)) && \
13720+
#define FP_K_FITS_COMB(k, m) (!ecc_k_too_long(k, mp_count_bits(m)) && \
1369513721
(mp_unsigned_bin_size(m) <= FP_COMB_MAX_MOD_SZ))
1369613722
#else
1369713723
#define FP_MULMOD_USES_LUT 1
@@ -14760,7 +14786,7 @@ static int accel_fp_mul(int idx, const mp_int* k, ecc_point *R, mp_int* a,
1476014786
#endif
1476114787

1476214788
/* if it's smaller than modulus we fine */
14763-
if (mp_unsigned_bin_size(k) > mp_unsigned_bin_size(modulus)) {
14789+
if (ecc_k_too_long(k, mp_unsigned_bin_size(modulus) * 8)) {
1476414790
/* find order */
1476514791
y = (unsigned)mp_unsigned_bin_size(modulus);
1476614792
for (x = 0; ecc_sets[x].size; x++) {
@@ -14792,7 +14818,7 @@ static int accel_fp_mul(int idx, const mp_int* k, ecc_point *R, mp_int* a,
1479214818
lut_gap = bitlen / FP_LUT;
1479314819

1479414820
/* get the k value */
14795-
if (mp_unsigned_bin_size(tk) > (int)(KB_SIZE - 2)) {
14821+
if (ecc_k_too_long(tk, (int)(KB_SIZE - 2) * 8)) {
1479614822
err = BUFFER_E; goto done;
1479714823
}
1479814824
#ifdef ECC_TIMING_RESISTANT
@@ -15401,7 +15427,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
1540115427
}
1540215428

1540315429
/* k can't be negative or have more bits than modulus count plus 1 */
15404-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus) + 1)) {
15430+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus) + 1)) {
1540515431
return ECC_OUT_OF_RANGE_E;
1540615432
}
1540715433

@@ -15500,7 +15526,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
1550015526
if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL) {
1550115527
return ECC_BAD_ARG_E;
1550215528
}
15503-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus))) {
15529+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus))) {
1550415530
return ECC_OUT_OF_RANGE_E;
1550515531
}
1550615532
if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
@@ -15574,7 +15600,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
1557415600
}
1557515601

1557615602
/* k can't be negative or have more bits than order */
15577-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(order))) {
15603+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(order))) {
1557815604
return ECC_OUT_OF_RANGE_E;
1557915605
}
1558015606

@@ -15676,7 +15702,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
1567615702
order == NULL) {
1567715703
return ECC_BAD_ARG_E;
1567815704
}
15679-
if (mp_isneg(k) || (mp_count_bits(k) > mp_count_bits(modulus))) {
15705+
if (mp_isneg(k) || ecc_k_too_long(k, mp_count_bits(modulus))) {
1568015706
return ECC_OUT_OF_RANGE_E;
1568115707
}
1568215708
if (mp_count_bits(G->x) > mp_count_bits(modulus) ||

‎wolfcrypt/src/wolfmath.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ int mp_rand(mp_int* a, int digits, WC_RNG* rng)
180180
#ifdef USE_INTEGER_HEAP_MATH
181181
/* allocate space for digits */
182182
if (ret == MP_OKAY) {
183-
ret = mp_set_bit(a, digits * DIGIT_BIT - 1);
183+
/* Zeroed first, so no wider value already in a survives. */
184+
ret = mp_2expt(a, digits * DIGIT_BIT - 1);
184185
}
185186
#else
186187
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)

0 commit comments

Comments
 (0)