From 8e38dcc3473023e3ba50976ba353a20b94938b12 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Thu, 20 Jul 2017 12:24:38 +1000 Subject: [PATCH] Tidy code up - use local static for cpuid flags --- wolfcrypt/src/aes.c | 17 ++++++------ wolfcrypt/src/cpuid.c | 8 +++--- wolfcrypt/src/random.c | 20 +++++++------- wolfcrypt/src/sha256.c | 44 +++++++++++++++---------------- wolfcrypt/src/sha512.c | 59 +++++++++++++++++------------------------- 5 files changed, 67 insertions(+), 81 deletions(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 04d5d0318..fcce35f35 100755 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -620,13 +620,16 @@ #define XASM_LINK(f) #endif /* _MSC_VER */ - static int Check_CPU_support_AES(void) - { - return IS_INTEL_AESNI(cpuid_get_flags()) != 0; - } - static int checkAESNI = 0; static int haveAESNI = 0; + static word32 intel_flags = 0; + + static int Check_CPU_support_AES(void) + { + intel_flags = cpuid_get_flags(); + + return IS_INTEL_AESNI(intel_flags) != 0; + } /* tell C compiler these are asm functions in case any mix up of ABI underscore @@ -7162,8 +7165,6 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, #ifdef WOLFSSL_AESNI if (haveAESNI) { #ifdef HAVE_INTEL_AVX2 - word32 intel_flags = cpuid_get_flags(); - if (IS_INTEL_AVX2(intel_flags)) { AES_GCM_encrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz, (const byte*)aes->key, aes->rounds); @@ -7425,8 +7426,6 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, #ifdef WOLFSSL_AESNI if (haveAESNI) { #ifdef HAVE_INTEL_AVX2 - word32 intel_flags = cpuid_get_flags(); - if (IS_INTEL_AVX2(intel_flags)) { if (AES_GCM_decrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz, (byte*)aes->key, aes->rounds) == 0) diff --git a/wolfcrypt/src/cpuid.c b/wolfcrypt/src/cpuid.c index 087217864..c144422d3 100644 --- a/wolfcrypt/src/cpuid.c +++ b/wolfcrypt/src/cpuid.c @@ -41,8 +41,8 @@ #define XASM_LINK(f) asm(f) #else - #include + #define cpuid(a,b) __cpuid((int*)a,b) #define XASM_LINK(f) @@ -58,8 +58,8 @@ static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) { - static int got_intel_cpu = 0; - static unsigned int reg[5]; + int got_intel_cpu = 0; + unsigned int reg[5]; reg[4] = '\0'; cpuid(reg, 0, 0); @@ -79,13 +79,13 @@ void cpuid_set_flags(void) { if (!cpuid_check) { - cpuid_check = 1; if (cpuid_flag(1, 0, ECX, 28)) { cpuid_flags |= CPUID_AVX1 ; } if (cpuid_flag(7, 0, EBX, 5)) { cpuid_flags |= CPUID_AVX2 ; } if (cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; } if (cpuid_flag(1, 0, ECX, 30)) { cpuid_flags |= CPUID_RDRAND; } if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; } if (cpuid_flag(1, 0, ECX, 26)) { cpuid_flags |= CPUID_AESNI ; } + cpuid_check = 1; } } diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 705578f1e..08f039231 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -135,7 +135,11 @@ int wc_RNG_GenerateByte(WC_RNG* rng, byte* b) #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED) - static void wc_InitRng_IntelRD(void); + static word32 intel_flags = 0; + static void wc_InitRng_IntelRD(void) + { + intel_flags = cpuid_get_flags(); + } #ifdef HAVE_INTEL_RDSEED static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz); #endif @@ -535,7 +539,7 @@ int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId) #ifdef HAVE_INTEL_RDRAND /* if CPU supports RDRAND, use it directly and by-pass DRBG init */ - if (IS_INTEL_RDRAND(cpuid_get_flags())) + if (IS_INTEL_RDRAND(intel_flags)) return 0; #endif @@ -605,7 +609,7 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz) return BAD_FUNC_ARG; #ifdef HAVE_INTEL_RDRAND - if (IS_INTEL_RDRAND(cpuid_get_flags())) + if (IS_INTEL_RDRAND(intel_flags)) return wc_GenerateRand_IntelRD(NULL, output, sz); #endif @@ -977,10 +981,6 @@ int wc_FreeNetRandom(void) #if defined(HAVE_INTEL_RDRAND) || defined(HAVE_INTEL_RDSEED) -static void wc_InitRng_IntelRD(void) { - cpuid_set_flags(); -} - #ifdef WOLFSSL_ASYNC_CRYPT /* need more retries if multiple cores */ #define INTELRD_RETRY (32 * 8) @@ -1018,7 +1018,7 @@ static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz) (void)os; - if (!IS_INTEL_RDSEED(cpuid_get_flags())) + if (!IS_INTEL_RDSEED(intel_flags)) return -1; for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64), @@ -1073,7 +1073,7 @@ static int wc_GenerateRand_IntelRD(OS_Seed* os, byte* output, word32 sz) (void)os; - if (!IS_INTEL_RDRAND(cpuid_get_flags())) + if (!IS_INTEL_RDRAND(intel_flags)) return -1; for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64), @@ -1653,7 +1653,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) int ret = 0; #ifdef HAVE_INTEL_RDSEED - if (IS_INTEL_RDSEED(cpuid_get_flags())) { + if (IS_INTEL_RDSEED(intel_flags)) { ret = wc_GenerateSeed_IntelRD(NULL, output, sz); if (ret == 0) { /* success, we're done */ diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 3c137a6c4..40ee9afe6 100755 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -197,30 +197,36 @@ static int InitSha256(Sha256* sha256) #endif static int (*Transform_p)(Sha256* sha256) /* = _Transform */; static int transform_check = 0; + static word32 intel_flags; #define XTRANSFORM(sha256, B) (*Transform_p)(sha256) - static void set_Transform(void) + static void Sha256_SetTransform(void) { - word32 intel_flags; - cpuid_set_flags(); if (transform_check) return; - transform_check = 1; + intel_flags = cpuid_get_flags(); #if defined(HAVE_INTEL_AVX2) if (IS_INTEL_AVX2(intel_flags) && IS_INTEL_BMI2(intel_flags)) { - Transform_p = Transform_AVX1_RORX; return; - Transform_p = Transform_AVX2; - /* for avoiding warning,"not used" */ + if (1) + Transform_p = Transform_AVX1_RORX; + else + Transform_p = Transform_AVX2; } + else #endif #if defined(HAVE_INTEL_AVX1) - Transform_p = ((IS_INTEL_AVX1(intel_flags)) ? Transform_AVX1 : - Transform); return; + if (1) { + Transform_p = ((IS_INTEL_AVX1(intel_flags)) ? Transform_AVX1 : + Transform); + } + else #endif - Transform_p = Transform; return; + Transform_p = Transform; + + transform_check = 1; } /* Dummy for saving MM_REGs on behalf of Transform */ @@ -246,7 +252,7 @@ static int InitSha256(Sha256* sha256) return ret; /* choose best Transform function under this runtime environment */ - set_Transform(); + Sha256_SetTransform(); #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256) ret = wolfAsync_DevCtxInit(&sha256->asyncDev, @@ -466,11 +472,6 @@ static int InitSha256(Sha256* sha256) { int ret = 0; byte* local; -#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) -#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - word32 intel_flags = cpuid_get_flags(); -#endif -#endif if (sha256 == NULL || (data == NULL && len > 0)) { return BAD_FUNC_ARG; @@ -534,11 +535,6 @@ static int InitSha256(Sha256* sha256) int ret; byte* local = (byte*)sha256->buffer; -#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) -#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - word32 intel_flags = cpuid_get_flags(); -#endif -#endif if (sha256 == NULL) { return BAD_FUNC_ARG; @@ -560,8 +556,10 @@ static int InitSha256(Sha256* sha256) #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags)) #endif + { ByteReverseWords(sha256->buffer, sha256->buffer, - SHA256_BLOCK_SIZE); + SHA256_BLOCK_SIZE); + } #endif } @@ -1811,7 +1809,7 @@ static int Transform_AVX2(Sha256* sha256) #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2) /* choose best Transform function under this runtime environment */ - set_Transform(); + Sha256_SetTransform(); #endif return ret; diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index 0b19795cb..60c9a1102 100755 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -272,6 +272,7 @@ static int InitSha512(Sha512* sha512) static int _Transform(Sha512 *sha512); static int (*Transform_p)(Sha512* sha512) = _Transform; static int transform_check = 0; + static int intel_flags; #define Transform(sha512) (*Transform_p)(sha512) /* Dummy for saving MM_REGs on behalf of Transform */ @@ -287,25 +288,30 @@ static int InitSha512(Sha512* sha512) static void Sha512_SetTransform() { - word32 intel_flags; - if (transform_check) return; - transform_check = 1; + intel_flags = cpuid_get_flags(); #if defined(HAVE_INTEL_AVX2) if (IS_INTEL_AVX2(intel_flags) && IS_INTEL_BMI2(intel_flags)) { - Transform_p = Transform_AVX1_RORX; return; - Transform_p = Transform_AVX2; - /* for avoiding warning,"not used" */ + if (1) + Transform_p = Transform_AVX1_RORX; + else + Transform_p = Transform_AVX2; } + else #endif #if defined(HAVE_INTEL_AVX1) - Transform_p = ((IS_INTEL_AVX1(intel_flags)) ? Transform_AVX1 : - _Transform); return; + if (1) { + Transform_p = ((IS_INTEL_AVX1(intel_flags)) ? Transform_AVX1 : + _Transform); + } + else #endif - Transform_p = _Transform; + Transform_p = _Transform; + + transform_check = 1; } int wc_InitSha512_ex(Sha512* sha512, void* heap, int devId) @@ -495,15 +501,6 @@ static INLINE int Sha512Update(Sha512* sha512, const byte* data, word32 len) int ret = 0; /* do block size increments */ byte* local = (byte*)sha512->buffer; -#if defined(LITTLE_ENDIAN_ORDER) -#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - word32 intel_flags = cpuid_get_flags(); -#endif -#endif - - if (sha512 == NULL || (data == NULL && len > 0)) { - return BAD_FUNC_ARG; - } /* check that internal buffLen is valid */ if (sha512->buffLen >= SHA512_BLOCK_SIZE) @@ -543,7 +540,7 @@ static INLINE int Sha512Update(Sha512* sha512, const byte* data, word32 len) int wc_Sha512Update(Sha512* sha512, const byte* data, word32 len) { - if (sha512 == NULL ||(data == NULL && len > 0)) { + if (sha512 == NULL || (data == NULL && len > 0)) { return BAD_FUNC_ARG; } @@ -563,11 +560,6 @@ static INLINE int Sha512Final(Sha512* sha512) { byte* local = (byte*)sha512->buffer; int ret; -#if defined(LITTLE_ENDIAN_ORDER) -#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - word32 intel_flags = cpuid_get_flags(); -#endif -#endif if (sha512 == NULL) { return BAD_FUNC_ARG; @@ -582,15 +574,15 @@ static INLINE int Sha512Final(Sha512* sha512) if (sha512->buffLen > SHA512_PAD_SIZE) { XMEMSET(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE - sha512->buffLen); sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen; - #if defined(LITTLE_ENDIAN_ORDER) - #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags)) - #endif - { - ByteReverseWords64(sha512->buffer,sha512->buffer, +#if defined(LITTLE_ENDIAN_ORDER) + #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) + if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags)) + #endif + { + ByteReverseWords64(sha512->buffer,sha512->buffer, SHA512_BLOCK_SIZE); - } - #endif /* LITTLE_ENDIAN_ORDER */ + } +#endif /* LITTLE_ENDIAN_ORDER */ ret = Transform(sha512); if (ret != 0) return ret; @@ -609,9 +601,7 @@ static INLINE int Sha512Final(Sha512* sha512) #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags)) #endif - { ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE); - } #endif /* ! length ordering dependent on digest endian type ! */ @@ -1429,7 +1419,6 @@ int wc_Sha384Final(Sha384* sha384, byte* hash) /* Hardware Acceleration */ #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2) - int wc_InitSha384_ex(Sha384* sha384, void* heap, int devId) { int ret = InitSha384(sha384);