From 1b1c32d7cf883078e0da02aaa38981f81c639cf3 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 17 May 2017 16:21:20 -0400 Subject: [PATCH] Build RDRAND for all platofrms (Issue 419, PR 424) We have not been able to determine a reliable way to detect cpu's and platforms with Cmake. We are side stepping the Cmake problem by building rdrand.cpp all the time. If its not avilable for a cpu or platform, then RDRAND or RDSEED throw an exception. --- GNUmakefile | 10 ++-------- rdrand.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- validat1.cpp | 6 ------ validate.h | 2 -- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 7947557b..8ec0f7f8 100755 --- a/GNUmakefile +++ b/GNUmakefile @@ -522,10 +522,6 @@ SRCS := cryptlib.cpp cpu.cpp integer.cpp $(filter-out cryptlib.cpp cpu.cpp integ ifeq ($(IS_X86)$(IS_X32)$(IS_X64)$(IS_ARM32)$(IS_ARM64),00000) SRCS := $(filter-out cpu.cpp, $(SRCS)) endif -# Need RDRAND for X86/X64/X32 -ifeq ($(IS_X86)$(IS_X32)$(IS_X64),000) - SRCS := $(filter-out rdrand.cpp, $(SRCS)) -endif ifneq ($(IS_MINGW),0) SRCS += winpipes.cpp @@ -766,13 +762,11 @@ endif trim: ifneq ($(IS_DARWIN),0) sed -i '' -e's/[[:space:]]*$$//' *.sh *.h *.cpp *.asm *.s *.sln *.vcxproj *.filters GNUmakefile GNUmakefile-cross - sed -i '' -e's/[[:space:]]*$$//' *.sh TestData/*.dat - sed -i '' -e's/[[:space:]]*$$//' *.sh TestVectors/*.txt + sed -i '' -e's/[[:space:]]*$$//' TestData/*.dat TestVectors/*.txt TestScripts/*.sh make convert else sed -i -e's/[[:space:]]*$$//' *.sh *.h *.cpp *.asm *.s *.sln *.vcxproj *.filters GNUmakefile GNUmakefile-cross - sed -i -e's/[[:space:]]*$$//' *.sh TestData/*.dat - sed -i -e's/[[:space:]]*$$//' *.sh TestVectors/*.txt + sed -i -e's/[[:space:]]*$$//' TestData/*.dat TestVectors/*.txt TestScripts/*.sh make convert endif diff --git a/rdrand.cpp b/rdrand.cpp index f9dab893..b591407d 100644 --- a/rdrand.cpp +++ b/rdrand.cpp @@ -69,12 +69,12 @@ # define GCC_RDSEED_ASM_AVAILABLE 1 # endif # elif defined(CRYPTOPP_GCC_VERSION) -# if defined(__RDRND__) && (CRYPTOPP_GCC_VERSION >= 40600) && !defined(__OPTIMIZE_SIZE__) +# if defined(__RDRND__) && (CRYPTOPP_GCC_VERSION >= 40600) && !defined(__OPTIMIZE__) # define ALL_RDRAND_INTRIN_AVAILABLE 1 # else # define GCC_RDRAND_ASM_AVAILABLE 1 # endif -# if defined(__RDSEED__) && (CRYPTOPP_GCC_VERSION >= 40600) && !defined(__OPTIMIZE_SIZE__) +# if defined(__RDSEED__) && (CRYPTOPP_GCC_VERSION >= 40600) && !defined(__OPTIMIZE__) # define ALL_RDSEED_INTRIN_AVAILABLE 1 # else # define GCC_RDSEED_ASM_AVAILABLE 1 @@ -118,6 +118,8 @@ extern "C" void NASM_RDSEED_GenerateBlock(byte*, size_t); NAMESPACE_BEGIN(CryptoPP) +#if defined(CRYPTOPP_CPUID_AVAILABLE) + // Fills 4 bytes inline void RDRAND32(void* output) { @@ -425,4 +427,38 @@ void RDSEED::DiscardBytes(size_t n) } } +#else // CRYPTOPP_CPUID_AVAILABLE + +RDRAND::RDRAND() +{ + throw RDRAND_Err("HasRDRAND"); +} + +void RDRAND::GenerateBlock(byte *output, size_t size) +{ + CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size); +} + +void RDRAND::DiscardBytes(size_t n) +{ + CRYPTOPP_UNUSED(n); +} + +RDSEED::RDSEED() +{ + throw RDSEED_Err("HasRDSEED"); +} + +void RDSEED::GenerateBlock(byte *output, size_t size) +{ + CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size); +} + +void RDSEED::DiscardBytes(size_t n) +{ + CRYPTOPP_UNUSED(n); +} + +#endif + NAMESPACE_END diff --git a/validat1.cpp b/validat1.cpp index 1157d60e..fa43cc92 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -79,10 +79,8 @@ bool ValidateAll(bool thorough) #if defined(CRYPTOPP_EXTENDED_VALIDATION) pass=TestMersenne() && pass; #endif -#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) pass=TestRDRAND() && pass; pass=TestRDSEED() && pass; -#endif #if defined(CRYPTOPP_EXTENDED_VALIDATION) // http://github.com/weidai11/cryptopp/issues/92 @@ -927,7 +925,6 @@ bool TestMersenne() } #endif -#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) bool TestRDRAND() { std::cout << "\nTesting RDRAND generator...\n\n"; @@ -1030,9 +1027,7 @@ bool TestRDRAND() std::cout.flush(); return pass; } -#endif -#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) bool TestRDSEED() { std::cout << "\nTesting RDSEED generator...\n\n"; @@ -1135,7 +1130,6 @@ bool TestRDSEED() std::cout.flush(); return pass; } -#endif bool ValidateHashDRBG() { diff --git a/validate.h b/validate.h index 9e25e1bb..8d2d4f46 100644 --- a/validate.h +++ b/validate.h @@ -22,10 +22,8 @@ bool TestRandomPool(); #if !defined(NO_OS_DEPENDENCE) bool TestAutoSeededX917(); #endif -#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) bool TestRDRAND(); bool TestRDSEED(); -#endif bool ValidateBaseCode(); bool ValidateCRC32();