From ac01277d93636cd7cb9163555e2d929c39849371 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 26 Oct 2016 22:35:24 -0400 Subject: [PATCH] Add X86 SHA cpu feature detection --- cpu.cpp | 8 ++++++-- cpu.h | 11 +++++++++++ validat1.cpp | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 73c8096de..5d0664ad5 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -185,8 +185,8 @@ static bool TrySSE2() bool CRYPTOPP_SECTION_INIT g_x86DetectionDone = false; bool CRYPTOPP_SECTION_INIT g_hasMMX = false, CRYPTOPP_SECTION_INIT g_hasISSE = false, CRYPTOPP_SECTION_INIT g_hasSSE2 = false, CRYPTOPP_SECTION_INIT g_hasSSSE3 = false; -bool CRYPTOPP_SECTION_INIT g_hasSSE4 = false, CRYPTOPP_SECTION_INIT g_hasAESNI = false, CRYPTOPP_SECTION_INIT g_hasCLMUL = false, CRYPTOPP_SECTION_INIT g_isP4 = false; -bool CRYPTOPP_SECTION_INIT g_hasRDRAND = false, CRYPTOPP_SECTION_INIT g_hasRDSEED = false; +bool CRYPTOPP_SECTION_INIT g_hasSSE4 = false, CRYPTOPP_SECTION_INIT g_hasAESNI = false, CRYPTOPP_SECTION_INIT g_hasCLMUL = false, CRYPTOPP_SECTION_INIT g_hasSHA = false; +bool CRYPTOPP_SECTION_INIT g_hasRDRAND = false, CRYPTOPP_SECTION_INIT g_hasRDSEED = false, CRYPTOPP_SECTION_INIT g_isP4 = false; bool CRYPTOPP_SECTION_INIT g_hasPadlockRNG = false, CRYPTOPP_SECTION_INIT g_hasPadlockACE = false, CRYPTOPP_SECTION_INIT g_hasPadlockACE2 = false; bool CRYPTOPP_SECTION_INIT g_hasPadlockPHE = false, CRYPTOPP_SECTION_INIT g_hasPadlockPMM = false; word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE; @@ -254,6 +254,7 @@ void DetectX86Features() { static const unsigned int RDRAND_FLAG = (1 << 30); static const unsigned int RDSEED_FLAG = (1 << 18); + static const unsigned int SHA_FLAG = (1 << 29); g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf; g_cacheLineSize = 8 * GETBYTE(cpuid1[1], 1); @@ -263,7 +264,10 @@ void DetectX86Features() { word32 cpuid3[4]; if (CpuId(7, cpuid3)) + { g_hasRDSEED = !!(cpuid3[1] /*EBX*/ & RDSEED_FLAG); + g_hasSHA = !!(cpuid3[1] /*EBX*/ & SHA_FLAG); + } } } else if (IsAMD(cpuid)) diff --git a/cpu.h b/cpu.h index b2e55b30b..6a7e61734 100644 --- a/cpu.h +++ b/cpu.h @@ -109,6 +109,7 @@ extern CRYPTOPP_DLL bool g_hasSSSE3; extern CRYPTOPP_DLL bool g_hasSSE4; extern CRYPTOPP_DLL bool g_hasAESNI; extern CRYPTOPP_DLL bool g_hasCLMUL; +extern CRYPTOPP_DLL bool g_hasSHA; extern CRYPTOPP_DLL bool g_isP4; extern CRYPTOPP_DLL bool g_hasRDRAND; extern CRYPTOPP_DLL bool g_hasRDSEED; @@ -209,6 +210,16 @@ inline bool HasCLMUL() return g_hasCLMUL; } +//! \brief Determines SHA availability +//! \returns true if SHA is determined to be available, false otherwise +//! \details HasSHA() is a runtime check performed using CPUID +inline bool HasSHA() +{ + if (!g_x86DetectionDone) + DetectX86Features(); + return g_hasSHA; +} + //! \brief Determines if the CPU is an Intel P4 //! \returns true if the CPU is a P4, false otherwise //! \details IsP4() is a runtime check performed using CPUID diff --git a/validat1.cpp b/validat1.cpp index ea75089fa..6eb1d3f80 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -310,7 +310,7 @@ bool TestSettings() cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4; cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED(); - cout << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << endl; + cout << ", hasSHA == " << HasSHA() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << endl; #elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64) bool hasNEON = HasNEON();