@@ -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) ||
0 commit comments