@@ -4780,7 +4780,7 @@ static void _sp_mont_setup(const sp_int* m, sp_int_digit* rho);
47804780
47814781/* Determine when mp_add_d is required. */
47824782#if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
4783- !defined(NO_DSA) || ( defined(HAVE_ECC) && defined(HAVE_COMP_KEY) ) || \
4783+ !defined(NO_DSA) || defined(HAVE_ECC) || \
47844784 (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
47854785 defined(OPENSSL_EXTRA)
47864786#define WOLFSSL_SP_ADD_D
@@ -5327,8 +5327,8 @@ int sp_abs(const sp_int* a, sp_int* r)
53275327 (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
53285328/* Compare absolute value of two multi-precision numbers.
53295329 *
5330- * @param [in] a SP integer.
5331- * @param [in] b SP integer.
5330+ * @param [in] a SP integer.
5331+ * @param [in] b SP integer.
53325332 *
53335333 * @return MP_GT when a is greater than b.
53345334 * @return MP_LT when a is less than b.
@@ -5373,8 +5373,8 @@ static int _sp_cmp_abs(const sp_int* a, const sp_int* b)
53735373 *
53745374 * Pointers are compared such that NULL is less than not NULL.
53755375 *
5376- * @param [in] a SP integer.
5377- * @param [in] b SP integer.
5376+ * @param [in] a SP integer.
5377+ * @param [in] b SP integer.
53785378 *
53795379 * @return MP_GT when a is greater than b.
53805380 * @return MP_LT when a is less than b.
@@ -5413,8 +5413,8 @@ int sp_cmp_mag(const sp_int* a, const sp_int* b)
54135413 *
54145414 * Assumes a and b are not NULL.
54155415 *
5416- * @param [in] a SP integer.
5417- * @param [in] a SP integer.
5416+ * @param [in] a SP integer.
5417+ * @param [in] b SP integer.
54185418 *
54195419 * @return MP_GT when a is greater than b.
54205420 * @return MP_LT when a is less than b.
@@ -5457,8 +5457,8 @@ static int _sp_cmp(const sp_int* a, const sp_int* b)
54575457 *
54585458 * Pointers are compared such that NULL is less than not NULL.
54595459 *
5460- * @param [in] a SP integer.
5461- * @param [in] a SP integer.
5460+ * @param [in] a SP integer.
5461+ * @param [in] b SP integer.
54625462 *
54635463 * @return MP_GT when a is greater than b.
54645464 * @return MP_LT when a is less than b.
@@ -5490,6 +5490,80 @@ int sp_cmp(const sp_int* a, const sp_int* b)
54905490}
54915491#endif
54925492
5493+ #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
5494+ defined(WOLFSSL_ECC_GEN_REJECT_SAMPLING)
5495+ /* Compare two multi-precision numbers in constant time.
5496+ *
5497+ * Assumes a and b are not NULL.
5498+ * Assumes a and b are positive.
5499+ *
5500+ * @param [in] a SP integer.
5501+ * @param [in] b SP integer.
5502+ * @param [in] n Number of digits to compare.
5503+ *
5504+ * @return MP_GT when a is greater than b.
5505+ * @return MP_LT when a is less than b.
5506+ * @return MP_EQ when a is equals b.
5507+ */
5508+ static int _sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n)
5509+ {
5510+ int ret = MP_EQ;
5511+ int i;
5512+ int mask = -1;
5513+
5514+ for (i = n - 1; i >= 0; i--) {
5515+ sp_int_digit ad = a->dp[i] & ((sp_int_digit)0 - (i < (int)a->used));
5516+ sp_int_digit bd = b->dp[i] & ((sp_int_digit)0 - (i < (int)b->used));
5517+
5518+ ret |= mask & ((0 - (ad < bd)) & MP_LT);
5519+ mask &= 0 - (ret == MP_EQ);
5520+ ret |= mask & ((0 - (ad > bd)) & MP_GT);
5521+ mask &= 0 - (ret == MP_EQ);
5522+ }
5523+
5524+ return ret;
5525+ }
5526+
5527+ /* Compare two multi-precision numbers in constant time.
5528+ *
5529+ * Pointers are compared such that NULL is less than not NULL.
5530+ * Assumes a and b are positive.
5531+ * Assumes a and b have n digits set at sometime.
5532+ *
5533+ * @param [in] a SP integer.
5534+ * @param [in] b SP integer.
5535+ * @param [in] n Number of digits to compare.
5536+ *
5537+ * @return MP_GT when a is greater than b.
5538+ * @return MP_LT when a is less than b.
5539+ * @return MP_EQ when a is equals b.
5540+ */
5541+ int sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n)
5542+ {
5543+ int ret;
5544+
5545+ /* Check pointers first. Both NULL returns equal. */
5546+ if (a == b) {
5547+ ret = MP_EQ;
5548+ }
5549+ /* Nothing is smaller than something. */
5550+ else if (a == NULL) {
5551+ ret = MP_LT;
5552+ }
5553+ /* Something is larger than nothing. */
5554+ else if (b == NULL) {
5555+ ret = MP_GT;
5556+ }
5557+ else
5558+ {
5559+ /* Compare values - a and b are not NULL. */
5560+ ret = _sp_cmp_ct(a, b, n);
5561+ }
5562+
5563+ return ret;
5564+ }
5565+ #endif /* HAVE_ECC && !WC_NO_RNG && WOLFSSL_ECC_GEN_REJECT_SAMPLING */
5566+
54935567/*************************
54945568 * Bit check/set functions
54955569 *************************/
@@ -7673,10 +7747,6 @@ int sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, sp_int* r)
76737747}
76747748#endif /* WOLFSSL_SP_MATH_ALL */
76757749
7676- #if (defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)) || \
7677- (defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
7678- defined(WOLFCRYPT_HAVE_ECCSI) || defined(WOLFCRYPT_HAVE_SAKKE) || \
7679- defined(OPENSSL_ALL))
76807750/* Constant time clamping/
76817751 *
76827752 * @param [in, out] a SP integer to clamp.
@@ -7693,7 +7763,6 @@ static void sp_clamp_ct(sp_int* a)
76937763 }
76947764 a->used = used;
76957765}
7696- #endif
76977766
76987767#if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
76997768/* Add two value and reduce: r = (a + b) % m
@@ -14362,7 +14431,8 @@ int sp_div_2d(const sp_int* a, int e, sp_int* r, sp_int* rem)
1436214431}
1436314432#endif /* WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY */
1436414433
14365- #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
14434+ #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
14435+ defined(HAVE_ECC)
1436614436/* The bottom e bits: r = a & ((1 << e) - 1)
1436714437 *
1436814438 * @param [in] a SP integer to reduce.
@@ -14432,7 +14502,7 @@ int sp_mod_2d(const sp_int* a, int e, sp_int* r)
1443214502
1443314503 return err;
1443414504}
14435- #endif /* WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY */
14505+ #endif /* ( WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY)) || HAVE_ECC */
1443614506
1443714507#if (defined(WOLFSSL_SP_MATH_ALL) && (!defined(WOLFSSL_RSA_VERIFY_ONLY) || \
1443814508 !defined(NO_DH))) || defined(OPENSSL_ALL)
@@ -17780,7 +17850,7 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz)
1778017850 #endif /* LITTLE_ENDIAN_ORDER */
1778117851 }
1778217852#endif
17783- sp_clamp (a);
17853+ sp_clamp_ct (a);
1778417854 }
1778517855
1778617856 return err;
0 commit comments