Merge pull request #4313 from SparkiDev/rsa_vfy_only

SP RSA verify only: fix to compile
This commit is contained in:
John Safranek
2021-08-23 14:42:56 -07:00
committed by GitHub
14 changed files with 10480 additions and 4813 deletions
+2 -1
View File
@@ -2932,7 +2932,8 @@ fi
if test "$ENABLED_ASN" = "no"
then
AM_CFLAGS="$AM_CFLAGS -DNO_ASN"
AM_CFLAGS="$AM_CFLAGS -DNO_ASN -DNO_ASN_CRYPT"
enable_pwdbased=no
if test "$ENABLED_DH" = "no" && test "$ENABLED_ECC" = "no"
then
# DH and ECC need bigint
+1 -1
View File
@@ -16867,7 +16867,7 @@ static int test_wc_RsaKeyToPublicDer (void)
word32 derLen = 162;
#else
int bits = 2048;
word32 derLen = 290;
word32 derLen = 294;
#endif
XMEMSET(&rng, 0, sizeof(rng));
+1554 -168
View File
File diff suppressed because it is too large Load Diff
+1649 -1637
View File
File diff suppressed because it is too large Load Diff
+2946 -162
View File
File diff suppressed because it is too large Load Diff
+20
View File
@@ -249,6 +249,7 @@ static void sp_2048_to_bin_72(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(WOLFSSL_SP_SMALL))) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 29 bits.
*
* a Array of sp_digit to normalize.
@@ -279,6 +280,7 @@ static void sp_2048_norm_36(sp_digit* a)
#endif /* WOLFSSL_SP_SMALL */
}
#endif /* (WOLFSSL_HAVE_SP_RSA && (!WOLFSSL_RSA_PUBLIC_ONLY || !WOLFSSL_SP_SMALL)) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 29 bits.
*
* a Array of sp_digit to normalize.
@@ -3221,6 +3223,7 @@ static int sp_2048_mod_72(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_2048_div_72(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -3535,6 +3538,7 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -5030,6 +5034,7 @@ static void sp_3072_to_bin_106(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 29 bits.
*
* a Array of sp_digit to normalize.
@@ -5043,6 +5048,7 @@ static void sp_3072_norm_53(sp_digit* a)
}
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 29 bits.
*
* a Array of sp_digit to normalize.
@@ -6728,6 +6734,7 @@ static int sp_3072_mod_106(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_3072_div_106(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -7038,6 +7045,7 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e
#endif
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -8380,6 +8388,7 @@ static void sp_3072_to_bin_112(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 28 bits.
*
* a Array of sp_digit to normalize.
@@ -8406,6 +8415,7 @@ static void sp_3072_norm_56(sp_digit* a)
a[55] += a[54] >> 28; a[54] &= 0xfffffff;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 28 bits.
*
* a Array of sp_digit to normalize.
@@ -10812,6 +10822,7 @@ static int sp_3072_mod_112(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_3072_div_112(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -11126,6 +11137,7 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -12693,6 +12705,7 @@ static void sp_4096_to_bin_142(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if defined(WOLFSSL_HAVE_SP_RSA) && !defined(SP_RSA_PRIVATE_EXP_D)
/* Normalize the values in each word to 29 bits.
*
@@ -12708,6 +12721,7 @@ static void sp_4096_norm_71(sp_digit* a)
}
#endif /* WOLFSSL_HAVE_SP_RSA & !SP_RSA_PRIVATE_EXP_D */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 29 bits.
*
* a Array of sp_digit to normalize.
@@ -14401,6 +14415,7 @@ static int sp_4096_mod_142(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_4096_div_142(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -14711,6 +14726,7 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e
#endif
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -15911,6 +15927,7 @@ static void sp_4096_to_bin_162(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if defined(WOLFSSL_HAVE_SP_RSA) && !defined(SP_RSA_PRIVATE_EXP_D)
/* Normalize the values in each word to 26 bits.
*
@@ -15932,6 +15949,7 @@ static void sp_4096_norm_81(sp_digit* a)
}
#endif /* WOLFSSL_HAVE_SP_RSA & !SP_RSA_PRIVATE_EXP_D */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 26 bits.
*
* a Array of sp_digit to normalize.
@@ -18293,6 +18311,7 @@ static int sp_4096_mod_162(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_4096_div_162(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -18607,6 +18626,7 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
+24
View File
@@ -250,6 +250,7 @@ static void sp_2048_to_bin_34(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 61 bits.
*
* a Array of sp_digit to normalize.
@@ -263,6 +264,7 @@ static void sp_2048_norm_17(sp_digit* a)
}
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 61 bits.
*
* a Array of sp_digit to normalize.
@@ -2177,6 +2179,7 @@ static int sp_2048_mod_34(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_2048_div_34(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -2487,6 +2490,7 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -3830,6 +3834,7 @@ static void sp_2048_to_bin_36(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 57 bits.
*
* a Array of sp_digit to normalize.
@@ -3850,6 +3855,7 @@ static void sp_2048_norm_18(sp_digit* a)
a[17] += a[16] >> 57; a[16] &= 0x1ffffffffffffffL;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 57 bits.
*
* a Array of sp_digit to normalize.
@@ -5730,6 +5736,7 @@ static int sp_2048_mod_36(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_2048_div_36(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -6044,6 +6051,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -7460,6 +7468,7 @@ static void sp_3072_to_bin_52(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 60 bits.
*
* a Array of sp_digit to normalize.
@@ -7473,6 +7482,7 @@ static void sp_3072_norm_26(sp_digit* a)
}
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 60 bits.
*
* a Array of sp_digit to normalize.
@@ -9142,6 +9152,7 @@ static int sp_3072_mod_52(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_3072_div_52(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -9452,6 +9463,7 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -10795,6 +10807,7 @@ static void sp_3072_to_bin_54(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 57 bits.
*
* a Array of sp_digit to normalize.
@@ -10816,6 +10829,7 @@ static void sp_3072_norm_27(sp_digit* a)
a[26] += a[25] >> 57; a[25] &= 0x1ffffffffffffffL;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 57 bits.
*
* a Array of sp_digit to normalize.
@@ -12844,6 +12858,7 @@ static int sp_3072_mod_54(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_3072_div_54(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -13158,6 +13173,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -14610,6 +14626,7 @@ static void sp_4096_to_bin_70(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if defined(WOLFSSL_HAVE_SP_RSA) && !defined(SP_RSA_PRIVATE_EXP_D)
/* Normalize the values in each word to 59 bits.
*
@@ -14625,6 +14642,7 @@ static void sp_4096_norm_35(sp_digit* a)
}
#endif /* WOLFSSL_HAVE_SP_RSA & !SP_RSA_PRIVATE_EXP_D */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 59 bits.
*
* a Array of sp_digit to normalize.
@@ -16213,6 +16231,7 @@ static int sp_4096_mod_70(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_4096_div_70(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -16523,6 +16542,7 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -17724,6 +17744,7 @@ static void sp_4096_to_bin_78(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if defined(WOLFSSL_HAVE_SP_RSA) && !defined(SP_RSA_PRIVATE_EXP_D)
/* Normalize the values in each word to 53 bits.
*
@@ -17751,6 +17772,7 @@ static void sp_4096_norm_39(sp_digit* a)
}
#endif /* WOLFSSL_HAVE_SP_RSA & !SP_RSA_PRIVATE_EXP_D */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 53 bits.
*
* a Array of sp_digit to normalize.
@@ -19907,6 +19929,7 @@ static int sp_4096_mod_78(sp_digit* r, const sp_digit* a, const sp_digit* m)
return sp_4096_div_78(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
@@ -20221,6 +20244,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e,
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) || */
/* WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
+1582 -162
View File
File diff suppressed because it is too large Load Diff
+25 -20
View File
@@ -2317,7 +2317,7 @@ int sp_grow(sp_int* a, int l)
}
#endif /* !WOLFSSL_RSA_VERIFY_ONLY || !NO_DH || HAVE_ECC */
#if !defined(WOLFSSL_RSA_VERIFY_ONLY)
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(HAVE_ECC)
/* Set the multi-precision number to zero.
*
* @param [out] a SP integer to set to zero.
@@ -2629,7 +2629,7 @@ static int _sp_cmp(sp_int* a, sp_int* b)
}
#endif
#ifndef WOLFSSL_RSA_VERIFY_ONLY
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(HAVE_ECC)
/* Compare two multi-precision numbers.
*
* Pointers are compared such that NULL is less than not NULL.
@@ -3318,8 +3318,23 @@ int sp_mul_d(sp_int* a, sp_int_digit d, sp_int* r)
#endif /* (WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY) ||
* (WOLFSSL_KEY_GEN && !NO_RSA) */
#if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY))
/* Predefine complicated rules of when to compile in sp_div_d and sp_mod_d. */
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
defined(WC_MP_TO_RADIX)
#define WOLFSSL_SP_DIV_D
#endif
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH) || \
(defined(HAVE_ECC) && (defined(FP_ECC) || defined(HAVE_COMP_KEY))) || \
(!defined(NO_RSA) && defined(WOLFSSL_KEY_GEN))
#define WOLFSSL_SP_MOD_D
#endif
#if (defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
!defined(WOLFSSL_RSA_PUBLIC_ONLY))) || \
defined(WOLFSSL_SP_DIV_D) || defined(WOLFSSL_SP_MOD_D)
#ifndef SP_ASM_DIV_WORD
/* Divide a two digit number by a digit number and return. (hi | lo) / d
*
@@ -3382,18 +3397,6 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
#endif /* WOLFSSL_SP_MATH_ALL || !NO_DH || HAVE_ECC ||
* (!NO_RSA && !WOLFSSL_RSA_VERIFY_ONLY) */
/* Predefine complicated rules of when to compile in sp_div_d and sp_mod_d. */
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
#define WOLFSSL_SP_DIV_D
#endif
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_HAVE_SP_DH) || \
(defined(HAVE_ECC) && (defined(FP_ECC) || defined(HAVE_COMP_KEY))) || \
(!defined(NO_RSA) && defined(WOLFSSL_KEY_GEN))
#define WOLFSSL_SP_MOD_D
#endif
#if (defined(WOLFSSL_SP_DIV_D) || defined(WOLFSSL_SP_MOD_D)) && \
!defined(WOLFSSL_SP_SMALL)
/* Divide by 3: r = a / 3 and rem = a % 3
@@ -3809,7 +3812,7 @@ int sp_div_2(sp_int* a, sp_int* r)
* Add/Subtract Functions
************************/
#if !defined(WOLFSSL_RSA_VERIFY_ONLY)
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_SP_INVMOD)
/* Add offset b to a into r: r = a + (b << (o * SP_WORD_SIZEOF))
*
* @param [in] a SP integer to add to.
@@ -3915,7 +3918,7 @@ static int _sp_sub_off(sp_int* a, sp_int* b, sp_int* r, int o)
#endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_SP_INT_NEGATIVE || !NO_DH ||
* HAVE_ECC || (!NO_RSA && !WOLFSSL_RSA_VERIFY_ONLY) */
#if !defined(WOLFSSL_RSA_VERIFY_ONLY)
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) || defined(WOLFSSL_SP_INVMOD)
/* Add b to a into r: r = a + b
*
* @param [in] a SP integer to add to.
@@ -12329,7 +12332,8 @@ int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz)
}
#if (!defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
defined(WOLFSSL_RSA_PUBLIC_ONLY)) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
defined(WOLFSSL_RSA_PUBLIC_ONLY)) && (!defined(WOLFSSL_RSA_VERIFY_ONLY) || \
defined(HAVE_ECC_KEY_EXPORT))
/* Convert the multi-precision number to an array of bytes in big-endian format.
*
* The array must be large enough for encoded number - use mp_unsigned_bin_size
@@ -12671,7 +12675,8 @@ int sp_tohex(sp_int* a, char* str)
#endif /* (WOLFSSL_SP_MATH_ALL && !WOLFSSL_RSA_VERIFY_ONLY) || WC_MP_TO_RADIX */
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
defined(WC_MP_TO_RADIX)
/* Put the big-endian, decimal string encoding of a into str.
*
* Assumes str is large enough for result.
+270 -258
View File
@@ -193,12 +193,14 @@ static void sp_2048_to_bin_32(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(WOLFSSL_SP_SMALL))) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
*/
#define sp_2048_norm_32(a)
#endif /* (WOLFSSL_HAVE_SP_RSA && (!WOLFSSL_RSA_PUBLIC_ONLY || !WOLFSSL_SP_SMALL)) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
@@ -830,7 +832,7 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) | WOLFSSL_HAVE_SP_DH */
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* r = 2^n mod m where n is the number of bits to reduce by.
* Given m must be 2048 bits, just need to subtract.
*
@@ -845,7 +847,7 @@ static void sp_2048_mont_norm_32(sp_digit* r, const sp_digit* m)
sp_2048_sub_in_place_32(r, m);
}
#endif /* WOLFSSL_HAVE_SP_RSA | WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) | WOLFSSL_HAVE_SP_DH */
extern sp_digit sp_2048_cond_sub_32(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern void sp_2048_mont_reduce_32(sp_digit* a, const sp_digit* m, sp_digit mp);
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -878,8 +880,7 @@ static void sp_2048_mont_sqr_32(sp_digit* r, const sp_digit* a,
sp_2048_mont_reduce_32(r, m, mp);
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
extern sp_digit sp_2048_cond_sub_avx2_32(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern sp_digit sp_2048_sub_32(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_2048_mul_d_avx2_32(sp_digit* r, const sp_digit* a, const sp_digit b);
#ifdef _WIN64
#if _MSC_VER < 1920
@@ -922,6 +923,86 @@ static WC_INLINE sp_digit div_2048_word_32(sp_digit d1, sp_digit d0,
return r;
}
#endif /* _WIN64 */
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_2048_div_32_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[64];
sp_digit t2[33];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[31];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 32);
for (i = 31; i > 0; i--) {
if (t1[i + 32] != d[i])
break;
}
if (t1[i + 32] >= d[i]) {
sp_2048_sub_in_place_32(&t1[32], d);
}
for (i=31; i>=0; i--) {
sp_digit hi = t1[32 + i] - (t1[32 + i] == div);
r1 = div_2048_word_32(hi, t1[32 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_2048_mul_d_avx2_32(t2, d, r1);
else
#endif
sp_2048_mul_d_32(t2, d, r1);
t1[32 + i] += sp_2048_sub_in_place_32(&t1[i], t2);
t1[32 + i] -= t2[32];
if (t1[32 + i] != 0) {
t1[32 + i] += sp_2048_add_32(&t1[i], &t1[i], d);
if (t1[32 + i] != 0)
t1[32 + i] += sp_2048_add_32(&t1[i], &t1[i], d);
}
}
for (i = 31; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_2048_sub_32(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 32);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_2048_mod_32_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_2048_div_32_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
extern sp_digit sp_2048_cond_sub_avx2_32(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
/* AND m into each word of a and store in r.
*
* r A single precision integer.
@@ -1014,6 +1095,7 @@ static WC_INLINE int sp_2048_div_32(const sp_digit* a, const sp_digit* d, sp_dig
return MP_OKAY;
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
@@ -1028,86 +1110,6 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a,
}
#endif /* WOLFSSL_HAVE_SP_DH || !WOLFSSL_RSA_PUBLIC_ONLY */
extern sp_digit sp_2048_sub_32(sp_digit* r, const sp_digit* a, const sp_digit* b);
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_2048_div_32_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[64];
sp_digit t2[33];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[31];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 32);
for (i = 31; i > 0; i--) {
if (t1[i + 32] != d[i])
break;
}
if (t1[i + 32] >= d[i]) {
sp_2048_sub_in_place_32(&t1[32], d);
}
for (i=31; i>=0; i--) {
sp_digit hi = t1[32 + i] - (t1[32 + i] == div);
r1 = div_2048_word_32(hi, t1[32 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_2048_mul_d_avx2_32(t2, d, r1);
else
#endif
sp_2048_mul_d_32(t2, d, r1);
t1[32 + i] += sp_2048_sub_in_place_32(&t1[i], t2);
t1[32 + i] -= t2[32];
if (t1[32 + i] != 0) {
t1[32 + i] += sp_2048_add_32(&t1[i], &t1[i], d);
if (t1[32 + i] != 0)
t1[32 + i] += sp_2048_add_32(&t1[i], &t1[i], d);
}
}
for (i = 31; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_2048_sub_32(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 32);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_2048_mod_32_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_2048_div_32_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -1263,8 +1265,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e,
return err;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
extern void sp_2048_mont_reduce_avx2_32(sp_digit* a, const sp_digit* m, sp_digit mp);
#ifdef HAVE_INTEL_AVX2
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -1458,8 +1460,8 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi
return err;
}
#endif /* HAVE_INTEL_AVX2 */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -1619,6 +1621,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em,
return err;
}
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
#if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)
/* RSA private key operation.
*
@@ -1868,6 +1871,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm,
return err;
}
#endif /* SP_RSA_PRIVATE_EXP_D | RSA_LOW_MEM */
#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
#endif /* WOLFSSL_HAVE_SP_RSA */
#if defined(WOLFSSL_HAVE_SP_DH) || (defined(WOLFSSL_HAVE_SP_RSA) && \
!defined(WOLFSSL_RSA_PUBLIC_ONLY))
@@ -2510,12 +2514,14 @@ static void sp_3072_to_bin_48(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(WOLFSSL_SP_SMALL))) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
*/
#define sp_3072_norm_48(a)
#endif /* (WOLFSSL_HAVE_SP_RSA && (!WOLFSSL_RSA_PUBLIC_ONLY || !WOLFSSL_SP_SMALL)) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
@@ -3157,7 +3163,7 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) | WOLFSSL_HAVE_SP_DH */
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* r = 2^n mod m where n is the number of bits to reduce by.
* Given m must be 3072 bits, just need to subtract.
*
@@ -3172,7 +3178,7 @@ static void sp_3072_mont_norm_48(sp_digit* r, const sp_digit* m)
sp_3072_sub_in_place_48(r, m);
}
#endif /* WOLFSSL_HAVE_SP_RSA | WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) | WOLFSSL_HAVE_SP_DH */
extern sp_digit sp_3072_cond_sub_48(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern void sp_3072_mont_reduce_48(sp_digit* a, const sp_digit* m, sp_digit mp);
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -3205,8 +3211,7 @@ static void sp_3072_mont_sqr_48(sp_digit* r, const sp_digit* a,
sp_3072_mont_reduce_48(r, m, mp);
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
extern sp_digit sp_3072_cond_sub_avx2_48(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern sp_digit sp_3072_sub_48(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_3072_mul_d_avx2_48(sp_digit* r, const sp_digit* a, const sp_digit b);
#ifdef _WIN64
#if _MSC_VER < 1920
@@ -3249,6 +3254,86 @@ static WC_INLINE sp_digit div_3072_word_48(sp_digit d1, sp_digit d0,
return r;
}
#endif /* _WIN64 */
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_3072_div_48_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[96];
sp_digit t2[49];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[47];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 48);
for (i = 47; i > 0; i--) {
if (t1[i + 48] != d[i])
break;
}
if (t1[i + 48] >= d[i]) {
sp_3072_sub_in_place_48(&t1[48], d);
}
for (i=47; i>=0; i--) {
sp_digit hi = t1[48 + i] - (t1[48 + i] == div);
r1 = div_3072_word_48(hi, t1[48 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_3072_mul_d_avx2_48(t2, d, r1);
else
#endif
sp_3072_mul_d_48(t2, d, r1);
t1[48 + i] += sp_3072_sub_in_place_48(&t1[i], t2);
t1[48 + i] -= t2[48];
if (t1[48 + i] != 0) {
t1[48 + i] += sp_3072_add_48(&t1[i], &t1[i], d);
if (t1[48 + i] != 0)
t1[48 + i] += sp_3072_add_48(&t1[i], &t1[i], d);
}
}
for (i = 47; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_3072_sub_48(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 48);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_3072_mod_48_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_3072_div_48_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
extern sp_digit sp_3072_cond_sub_avx2_48(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
/* AND m into each word of a and store in r.
*
* r A single precision integer.
@@ -3341,6 +3426,7 @@ static WC_INLINE int sp_3072_div_48(const sp_digit* a, const sp_digit* d, sp_dig
return MP_OKAY;
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
@@ -3355,86 +3441,6 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a,
}
#endif /* WOLFSSL_HAVE_SP_DH || !WOLFSSL_RSA_PUBLIC_ONLY */
extern sp_digit sp_3072_sub_48(sp_digit* r, const sp_digit* a, const sp_digit* b);
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_3072_div_48_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[96];
sp_digit t2[49];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[47];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 48);
for (i = 47; i > 0; i--) {
if (t1[i + 48] != d[i])
break;
}
if (t1[i + 48] >= d[i]) {
sp_3072_sub_in_place_48(&t1[48], d);
}
for (i=47; i>=0; i--) {
sp_digit hi = t1[48 + i] - (t1[48 + i] == div);
r1 = div_3072_word_48(hi, t1[48 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_3072_mul_d_avx2_48(t2, d, r1);
else
#endif
sp_3072_mul_d_48(t2, d, r1);
t1[48 + i] += sp_3072_sub_in_place_48(&t1[i], t2);
t1[48 + i] -= t2[48];
if (t1[48 + i] != 0) {
t1[48 + i] += sp_3072_add_48(&t1[i], &t1[i], d);
if (t1[48 + i] != 0)
t1[48 + i] += sp_3072_add_48(&t1[i], &t1[i], d);
}
}
for (i = 47; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_3072_sub_48(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 48);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_3072_mod_48_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_3072_div_48_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -3590,8 +3596,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e,
return err;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
extern void sp_3072_mont_reduce_avx2_48(sp_digit* a, const sp_digit* m, sp_digit mp);
#ifdef HAVE_INTEL_AVX2
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -3785,8 +3791,8 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi
return err;
}
#endif /* HAVE_INTEL_AVX2 */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -3946,6 +3952,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em,
return err;
}
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
#if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)
/* RSA private key operation.
*
@@ -4195,6 +4202,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm,
return err;
}
#endif /* SP_RSA_PRIVATE_EXP_D | RSA_LOW_MEM */
#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
#endif /* WOLFSSL_HAVE_SP_RSA */
#if defined(WOLFSSL_HAVE_SP_DH) || (defined(WOLFSSL_HAVE_SP_RSA) && \
!defined(WOLFSSL_RSA_PUBLIC_ONLY))
@@ -4837,12 +4845,14 @@ static void sp_4096_to_bin_64(sp_digit* r, byte* a)
}
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(WOLFSSL_SP_SMALL))) || defined(WOLFSSL_HAVE_SP_DH)
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
*/
#define sp_4096_norm_64(a)
#endif /* (WOLFSSL_HAVE_SP_RSA && (!WOLFSSL_RSA_PUBLIC_ONLY || !WOLFSSL_SP_SMALL)) || WOLFSSL_HAVE_SP_DH */
/* Normalize the values in each word to 64.
*
* a Array of sp_digit to normalize.
@@ -4882,7 +4892,7 @@ static void sp_4096_mont_setup(const sp_digit* a, sp_digit* rho)
}
extern void sp_4096_mul_d_64(sp_digit* r, const sp_digit* a, sp_digit b);
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* r = 2^n mod m where n is the number of bits to reduce by.
* Given m must be 4096 bits, just need to subtract.
*
@@ -4897,7 +4907,7 @@ static void sp_4096_mont_norm_64(sp_digit* r, const sp_digit* m)
sp_4096_sub_in_place_64(r, m);
}
#endif /* WOLFSSL_HAVE_SP_RSA | WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA & !WOLFSSL_RSA_PUBLIC_ONLY) | WOLFSSL_HAVE_SP_DH */
extern sp_digit sp_4096_cond_sub_64(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern void sp_4096_mont_reduce_64(sp_digit* a, const sp_digit* m, sp_digit mp);
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -4930,8 +4940,7 @@ static void sp_4096_mont_sqr_64(sp_digit* r, const sp_digit* a,
sp_4096_mont_reduce_64(r, m, mp);
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
extern sp_digit sp_4096_cond_sub_avx2_64(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
extern sp_digit sp_4096_sub_64(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_4096_mul_d_avx2_64(sp_digit* r, const sp_digit* a, const sp_digit b);
#ifdef _WIN64
#if _MSC_VER < 1920
@@ -4974,6 +4983,86 @@ static WC_INLINE sp_digit div_4096_word_64(sp_digit d1, sp_digit d0,
return r;
}
#endif /* _WIN64 */
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_4096_div_64_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[128];
sp_digit t2[65];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[63];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 64);
for (i = 63; i > 0; i--) {
if (t1[i + 64] != d[i])
break;
}
if (t1[i + 64] >= d[i]) {
sp_4096_sub_in_place_64(&t1[64], d);
}
for (i=63; i>=0; i--) {
sp_digit hi = t1[64 + i] - (t1[64 + i] == div);
r1 = div_4096_word_64(hi, t1[64 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_4096_mul_d_avx2_64(t2, d, r1);
else
#endif
sp_4096_mul_d_64(t2, d, r1);
t1[64 + i] += sp_4096_sub_in_place_64(&t1[i], t2);
t1[64 + i] -= t2[64];
if (t1[64 + i] != 0) {
t1[64 + i] += sp_4096_add_64(&t1[i], &t1[i], d);
if (t1[64 + i] != 0)
t1[64 + i] += sp_4096_add_64(&t1[i], &t1[i], d);
}
}
for (i = 63; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_4096_sub_64(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 64);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_4096_mod_64_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_4096_div_64_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
extern sp_digit sp_4096_cond_sub_avx2_64(sp_digit* r, const sp_digit* a, const sp_digit* b, sp_digit m);
/* AND m into each word of a and store in r.
*
* r A single precision integer.
@@ -5066,6 +5155,7 @@ static WC_INLINE int sp_4096_div_64(const sp_digit* a, const sp_digit* d, sp_dig
return MP_OKAY;
}
#if defined(WOLFSSL_HAVE_SP_DH) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
@@ -5080,86 +5170,6 @@ static WC_INLINE int sp_4096_mod_64(sp_digit* r, const sp_digit* a,
}
#endif /* WOLFSSL_HAVE_SP_DH || !WOLFSSL_RSA_PUBLIC_ONLY */
extern sp_digit sp_4096_sub_64(sp_digit* r, const sp_digit* a, const sp_digit* b);
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_4096_div_64_cond(const sp_digit* a, const sp_digit* d, sp_digit* m,
sp_digit* r)
{
sp_digit t1[128];
sp_digit t2[65];
sp_digit div;
sp_digit r1;
int i;
#ifdef HAVE_INTEL_AVX2
word32 cpuid_flags = cpuid_get_flags();
#endif
(void)m;
div = d[63];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 64);
for (i = 63; i > 0; i--) {
if (t1[i + 64] != d[i])
break;
}
if (t1[i + 64] >= d[i]) {
sp_4096_sub_in_place_64(&t1[64], d);
}
for (i=63; i>=0; i--) {
sp_digit hi = t1[64 + i] - (t1[64 + i] == div);
r1 = div_4096_word_64(hi, t1[64 + i - 1], div);
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_BMI2(cpuid_flags) && IS_INTEL_ADX(cpuid_flags))
sp_4096_mul_d_avx2_64(t2, d, r1);
else
#endif
sp_4096_mul_d_64(t2, d, r1);
t1[64 + i] += sp_4096_sub_in_place_64(&t1[i], t2);
t1[64 + i] -= t2[64];
if (t1[64 + i] != 0) {
t1[64 + i] += sp_4096_add_64(&t1[i], &t1[i], d);
if (t1[64 + i] != 0)
t1[64 + i] += sp_4096_add_64(&t1[i], &t1[i], d);
}
}
for (i = 63; i > 0; i--) {
if (t1[i] != d[i])
break;
}
if (t1[i] >= d[i]) {
sp_4096_sub_64(r, t1, d);
}
else {
XMEMCPY(r, t1, sizeof(*t1) * 64);
}
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_4096_mod_64_cond(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
return sp_4096_div_64_cond(a, m, NULL, r);
}
#if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_HAVE_SP_DH)
/* Modular exponentiate a to the e mod m. (r = a^e mod m)
*
* r A single precision number that is the result of the operation.
@@ -5315,8 +5325,8 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e,
return err;
}
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
extern void sp_4096_mont_reduce_avx2_64(sp_digit* a, const sp_digit* m, sp_digit mp);
#ifdef HAVE_INTEL_AVX2
/* Multiply two Montogmery form numbers mod the modulus (prime).
@@ -5510,8 +5520,8 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi
return err;
}
#endif /* HAVE_INTEL_AVX2 */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#endif /* (WOLFSSL_HAVE_SP_RSA && !WOLFSSL_RSA_PUBLIC_ONLY) || WOLFSSL_HAVE_SP_DH */
#ifdef WOLFSSL_HAVE_SP_RSA
/* RSA public key operation.
*
@@ -5671,6 +5681,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em,
return err;
}
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
#if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)
/* RSA private key operation.
*
@@ -5920,6 +5931,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm,
return err;
}
#endif /* SP_RSA_PRIVATE_EXP_D | RSA_LOW_MEM */
#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
#endif /* WOLFSSL_HAVE_SP_RSA */
#if defined(WOLFSSL_HAVE_SP_DH) || (defined(WOLFSSL_HAVE_SP_RSA) && \
!defined(WOLFSSL_RSA_PUBLIC_ONLY))
+1221 -1221
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+2
View File
@@ -14142,8 +14142,10 @@ exit_rsa_even_mod:
(void)out;
(void)outSz;
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
(void)plain;
(void)plainSz;
#endif
(void)inLen;
(void)rng;
+2 -1
View File
@@ -806,7 +806,8 @@ MP_API int sp_add_d(sp_int* a, sp_int_digit d, sp_int* r);
MP_API int sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r);
MP_API int sp_mul_d(sp_int* a, sp_int_digit d, sp_int* r);
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
defined(WC_MP_TO_RADIX)
MP_API int sp_div_d(sp_int* a, sp_int_digit d, sp_int* r, sp_int_digit* rem);
#endif
#if defined(WOLFSSL_SP_MATH_ALL) || (defined(HAVE_ECC) && \