Merge pull request #2912 from SparkiDev/sp_movbe

Only use Intel instruction movbe when available
pull/2925/head
toddouska 2020-04-23 09:25:02 -07:00 committed by GitHub
commit 54aa50e628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1385 additions and 150 deletions

View File

@ -97,6 +97,7 @@
if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; }
if (cpuid_flag(1, 0, ECX, 25)) { cpuid_flags |= CPUID_AESNI ; }
if (cpuid_flag(7, 0, EBX, 19)) { cpuid_flags |= CPUID_ADX ; }
if (cpuid_flag(1, 0, ECX, 22)) { cpuid_flags |= CPUID_MOVBE ; }
cpuid_check = 1;
}
}

View File

@ -49,7 +49,27 @@
#ifdef WOLFSSL_SP_X86_64_ASM
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#ifndef WOLFSSL_SP_NO_2048
extern void sp_2048_from_bin(sp_digit* r, int size, const byte* a, int n);
extern void sp_2048_from_bin_bswap(sp_digit* r, int size, const byte* a, int n);
extern void sp_2048_from_bin_movbe(sp_digit* r, int size, const byte* a, int n);
/* Read big endian unsigned byte array into r.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a Byte array.
* n Number of bytes in array to read.
*/
static void sp_2048_from_bin(sp_digit* r, int size, const byte* a, int n)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_2048_from_bin_movbe(r, size, a, n);
}
else {
sp_2048_from_bin_bswap(r, size, a, n);
}
}
/* Convert an mp_int to an array of sp_digit.
*
* r A single precision integer.
@ -132,7 +152,26 @@ static void sp_2048_from_mp(sp_digit* r, int size, const mp_int* a)
#endif
}
extern void sp_2048_to_bin(sp_digit* r, byte* a);
extern void sp_2048_to_bin_bswap(sp_digit* r, byte* a);
extern void sp_2048_to_bin_movbe(sp_digit* r, byte* a);
/* Write r as big endian to byte array.
* Fixed length number of bytes written: 256
*
* r A single precision integer.
* a Byte array.
*/
static void sp_2048_to_bin(sp_digit* r, byte* a)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_2048_to_bin_movbe(r, a);
}
else {
sp_2048_to_bin_bswap(r, a);
}
}
extern void sp_2048_mul_16(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_2048_sqr_16(sp_digit* r, const sp_digit* a);
extern void sp_2048_mul_avx2_16(sp_digit* r, const sp_digit* a, const sp_digit* b);
@ -2184,7 +2223,27 @@ int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res)
#endif /* !WOLFSSL_SP_NO_2048 */
#ifndef WOLFSSL_SP_NO_3072
extern void sp_3072_from_bin(sp_digit* r, int size, const byte* a, int n);
extern void sp_3072_from_bin_bswap(sp_digit* r, int size, const byte* a, int n);
extern void sp_3072_from_bin_movbe(sp_digit* r, int size, const byte* a, int n);
/* Read big endian unsigned byte array into r.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a Byte array.
* n Number of bytes in array to read.
*/
static void sp_3072_from_bin(sp_digit* r, int size, const byte* a, int n)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_3072_from_bin_movbe(r, size, a, n);
}
else {
sp_3072_from_bin_bswap(r, size, a, n);
}
}
/* Convert an mp_int to an array of sp_digit.
*
* r A single precision integer.
@ -2267,7 +2326,26 @@ static void sp_3072_from_mp(sp_digit* r, int size, const mp_int* a)
#endif
}
extern void sp_3072_to_bin(sp_digit* r, byte* a);
extern void sp_3072_to_bin_bswap(sp_digit* r, byte* a);
extern void sp_3072_to_bin_movbe(sp_digit* r, byte* a);
/* Write r as big endian to byte array.
* Fixed length number of bytes written: 384
*
* r A single precision integer.
* a Byte array.
*/
static void sp_3072_to_bin(sp_digit* r, byte* a)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_3072_to_bin_movbe(r, a);
}
else {
sp_3072_to_bin_bswap(r, a);
}
}
extern void sp_3072_mul_12(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_3072_sqr_12(sp_digit* r, const sp_digit* a);
extern void sp_3072_mul_avx2_12(sp_digit* r, const sp_digit* a, const sp_digit* b);
@ -4333,7 +4411,27 @@ int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res)
#endif /* !WOLFSSL_SP_NO_3072 */
#ifdef WOLFSSL_SP_4096
extern void sp_4096_from_bin(sp_digit* r, int size, const byte* a, int n);
extern void sp_4096_from_bin_bswap(sp_digit* r, int size, const byte* a, int n);
extern void sp_4096_from_bin_movbe(sp_digit* r, int size, const byte* a, int n);
/* Read big endian unsigned byte array into r.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a Byte array.
* n Number of bytes in array to read.
*/
static void sp_4096_from_bin(sp_digit* r, int size, const byte* a, int n)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_4096_from_bin_movbe(r, size, a, n);
}
else {
sp_4096_from_bin_bswap(r, size, a, n);
}
}
/* Convert an mp_int to an array of sp_digit.
*
* r A single precision integer.
@ -4416,7 +4514,26 @@ static void sp_4096_from_mp(sp_digit* r, int size, const mp_int* a)
#endif
}
extern void sp_4096_to_bin(sp_digit* r, byte* a);
extern void sp_4096_to_bin_bswap(sp_digit* r, byte* a);
extern void sp_4096_to_bin_movbe(sp_digit* r, byte* a);
/* Write r as big endian to byte array.
* Fixed length number of bytes written: 512
*
* r A single precision integer.
* a Byte array.
*/
static void sp_4096_to_bin(sp_digit* r, byte* a)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_4096_to_bin_movbe(r, a);
}
else {
sp_4096_to_bin_bswap(r, a);
}
}
extern sp_digit sp_4096_sub_in_place_64(sp_digit* a, const sp_digit* b);
extern sp_digit sp_4096_add_64(sp_digit* r, const sp_digit* a, const sp_digit* b);
extern void sp_4096_mul_64(sp_digit* r, const sp_digit* a, const sp_digit* b);
@ -22065,7 +22182,27 @@ static int sp_256_iszero_4(const sp_digit* a)
#endif /* WOLFSSL_VALIDATE_ECC_KEYGEN || HAVE_ECC_SIGN || HAVE_ECC_VERIFY */
extern void sp_256_add_one_4(sp_digit* a);
extern void sp_256_from_bin(sp_digit* r, int size, const byte* a, int n);
extern void sp_256_from_bin_bswap(sp_digit* r, int size, const byte* a, int n);
extern void sp_256_from_bin_movbe(sp_digit* r, int size, const byte* a, int n);
/* Read big endian unsigned byte array into r.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a Byte array.
* n Number of bytes in array to read.
*/
static void sp_256_from_bin(sp_digit* r, int size, const byte* a, int n)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_256_from_bin_movbe(r, size, a, n);
}
else {
sp_256_from_bin_bswap(r, size, a, n);
}
}
/* Generates a scalar that is in the range 1..order-1.
*
* rng Random number generator.
@ -22192,7 +22329,26 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap)
}
#ifdef HAVE_ECC_DHE
extern void sp_256_to_bin(sp_digit* r, byte* a);
extern void sp_256_to_bin_bswap(sp_digit* r, byte* a);
extern void sp_256_to_bin_movbe(sp_digit* r, byte* a);
/* Write r as big endian to byte array.
* Fixed length number of bytes written: 32
*
* r A single precision integer.
* a Byte array.
*/
static void sp_256_to_bin(sp_digit* r, byte* a)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_256_to_bin_movbe(r, a);
}
else {
sp_256_to_bin_bswap(r, a);
}
}
/* Multiply the point by the scalar and serialize the X ordinate.
* The number is 0 padded to maximum size on output.
*
@ -27886,7 +28042,27 @@ static int sp_384_iszero_6(const sp_digit* a)
#endif /* WOLFSSL_VALIDATE_ECC_KEYGEN || HAVE_ECC_SIGN || HAVE_ECC_VERIFY */
extern void sp_384_add_one_6(sp_digit* a);
extern void sp_384_from_bin(sp_digit* r, int size, const byte* a, int n);
extern void sp_384_from_bin_bswap(sp_digit* r, int size, const byte* a, int n);
extern void sp_384_from_bin_movbe(sp_digit* r, int size, const byte* a, int n);
/* Read big endian unsigned byte array into r.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a Byte array.
* n Number of bytes in array to read.
*/
static void sp_384_from_bin(sp_digit* r, int size, const byte* a, int n)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_384_from_bin_movbe(r, size, a, n);
}
else {
sp_384_from_bin_bswap(r, size, a, n);
}
}
/* Generates a scalar that is in the range 1..order-1.
*
* rng Random number generator.
@ -28013,7 +28189,26 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap)
}
#ifdef HAVE_ECC_DHE
extern void sp_384_to_bin(sp_digit* r, byte* a);
extern void sp_384_to_bin_bswap(sp_digit* r, byte* a);
extern void sp_384_to_bin_movbe(sp_digit* r, byte* a);
/* Write r as big endian to byte array.
* Fixed length number of bytes written: 48
*
* r A single precision integer.
* a Byte array.
*/
static void sp_384_to_bin(sp_digit* r, byte* a)
{
word32 cpuid_flags = cpuid_get_flags();
if (IS_INTEL_MOVBE(cpuid_flags)) {
sp_384_to_bin_movbe(r, a);
}
else {
sp_384_to_bin_bswap(r, a);
}
}
/* Multiply the point by the scalar and serialize the X ordinate.
* The number is 0 padded to maximum size on output.
*

File diff suppressed because it is too large Load Diff

View File

@ -41,6 +41,7 @@
#define CPUID_BMI2 0x0010 /* MULX, RORX */
#define CPUID_AESNI 0x0020
#define CPUID_ADX 0x0040 /* ADCX, ADOX */
#define CPUID_MOVBE 0x0080 /* Move and byte swap */
#define IS_INTEL_AVX1(f) ((f) & CPUID_AVX1)
#define IS_INTEL_AVX2(f) ((f) & CPUID_AVX2)
@ -49,6 +50,7 @@
#define IS_INTEL_BMI2(f) ((f) & CPUID_BMI2)
#define IS_INTEL_AESNI(f) ((f) & CPUID_AESNI)
#define IS_INTEL_ADX(f) ((f) & CPUID_ADX)
#define IS_INTEL_MOVBE(f) ((f) & CPUID_MOVBE)
void cpuid_set_flags(void);
word32 cpuid_get_flags(void);