From 3f528fef1ff15da1bfe3b4807b514299da0454e1 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 8 Mar 2017 14:09:58 -0500 Subject: [PATCH] Add guard for RDSEED64 on 32-bit platforms (Issue 387) Cleanup comments in rdrand.cpp and rdrand.asm --- rdrand.asm | 11 +++-------- rdrand.cpp | 35 +++++------------------------------ 2 files changed, 8 insertions(+), 38 deletions(-) diff --git a/rdrand.asm b/rdrand.asm index 5919307f..37f7b52b 100644 --- a/rdrand.asm +++ b/rdrand.asm @@ -22,12 +22,6 @@ PUBLIC MASM_RDSEED_GenerateBlock ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Caller/Callee Saved Registers -;; https://msdn.microsoft.com/en-us/library/6t169e9c.aspx - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; C/C++ Function prototypes (both are fastcall) ;; X86: ;; extern "C" void __fastcall MASM_RDRAND_GenerateBlock(byte* ptr, size_t size); @@ -42,6 +36,7 @@ IFDEF _M_X86 ;; Set via the command line .486 .MODEL FLAT +;; Fastcall calling conventions exports ALIAS <@MASM_RDRAND_GenerateBlock@8> = ALIAS <@MASM_RDSEED_GenerateBlock@8> = @@ -149,7 +144,7 @@ OPTION EPILOGUE:NONE ;; RCX (in): arg1, byte* buffer ;; RDX (in): arg2, size_t bsize -MASM_RDRAND_GenerateBlock PROC +MASM_RDRAND_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD MWSIZE EQU 08h ;; machine word size buffer EQU rcx @@ -335,7 +330,7 @@ OPTION EPILOGUE:NONE ;; RCX (in): arg1, byte* buffer ;; RDX (in): arg2, size_t bsize -MASM_RDSEED_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD +MASM_RDSEED_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD MWSIZE EQU 08h ;; machine word size buffer EQU rcx diff --git a/rdrand.cpp b/rdrand.cpp index 11d38d91..31ef84d0 100644 --- a/rdrand.cpp +++ b/rdrand.cpp @@ -7,8 +7,6 @@ #include "rdrand.h" #include "cpu.h" -#include - #if CRYPTOPP_MSC_VERSION # pragma warning(disable: 4100) #endif @@ -16,9 +14,9 @@ // This file (and friends) provides both RDRAND and RDSEED. They were added at // Crypto++ 5.6.3. At compile time, it uses CRYPTOPP_BOOL_{X86|X32|X64} // to select an implementation or "throw NotImplemented". The class does not -// use CPUID to determine if RDRAND or RDSEED are available. If not available, -// then a SIGILL will result. Users of the classes should call HasRDRAND() or -// HasRDSEED() to determine if a generator is available. +// determine if RDRAND or RDSEED are available at runtime. If not available, +// then a SIGILL will result. Users of the classes should call HasRDRAND() +// or HasRDSEED() to determine if a generator is available. // The original classes accepted a retry count. Retries were superflous for // RDRAND, and RDSEED encountered a failure about 1 in 256 bytes depending // on the processor. Retries were removed at Crypto++ 6.0 because @@ -104,23 +102,11 @@ #endif #if MASM_RDRAND_ASM_AVAILABLE -# ifdef _M_X64 extern "C" void CRYPTOPP_FASTCALL MASM_RDRAND_GenerateBlock(byte*, size_t); -// # pragma comment(lib, "rdrand-x64.lib") -# else -extern "C" void CRYPTOPP_FASTCALL MASM_RDRAND_GenerateBlock(byte*, size_t); -// # pragma comment(lib, "rdrand-x86.lib") -# endif #endif #if MASM_RDSEED_ASM_AVAILABLE -# ifdef _M_X64 extern "C" void CRYPTOPP_FASTCALL MASM_RDSEED_GenerateBlock(byte*, size_t); -// # pragma comment(lib, "rdrand-x64.lib") -# else -extern "C" void CRYPTOPP_FASTCALL MASM_RDSEED_GenerateBlock(byte*, size_t); -// # pragma comment(lib, "rdrand-x86.lib") -# endif #endif #if NASM_RDRAND_ASM_AVAILABLE @@ -134,13 +120,6 @@ extern "C" void NASM_RDSEED_GenerateBlock(byte*, size_t); ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// -ANONYMOUS_NAMESPACE_BEGIN -// GCC, MSVC and SunCC have optimized calls to RDRAND away. We experieced -// it under GCC and MSVC. Other have reported it for SunCC. This attempts -// to tame the optimizer even though it abuses the volatile keyword. -static volatile int s_unused; -ANONYMOUS_NAMESPACE_END - NAMESPACE_BEGIN(CryptoPP) // Fills 4 bytes @@ -266,9 +245,6 @@ void RDRAND::GenerateBlock(byte *output, size_t size) // RDRAND not detected at compile time, or no suitable compiler found throw NotImplemented("RDRAND: failed to find a suitable implementation"); #endif - - // Size is not 0 - s_unused ^= output[0]; } void RDRAND::DiscardBytes(size_t n) @@ -328,6 +304,7 @@ inline void RDSEED32(void* output) #endif } +#if CRYPTOPP_BOOL_X64 // Fills 8 bytes inline void RDSEED64(void* output) { @@ -365,6 +342,7 @@ inline void RDSEED64(void* output) throw NotImplemented("RDSEED: failed to find an implementation"); #endif } +#endif // CRYPTOPP_BOOL_X64 and RDSEED64 void RDSEED::GenerateBlock(byte *output, size_t size) { @@ -408,9 +386,6 @@ void RDSEED::GenerateBlock(byte *output, size_t size) std::memcpy(output, &val, size); } #endif - - // Size is not 0 - s_unused ^= output[0]; } void RDSEED::DiscardBytes(size_t n)