Add guard for RDSEED64 on 32-bit platforms (Issue 387)

Cleanup comments in rdrand.cpp and rdrand.asm
pull/354/merge
Jeffrey Walton 2017-03-08 14:09:58 -05:00
parent 5b98a255f7
commit 3f528fef1f
2 changed files with 8 additions and 38 deletions

View File

@ -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) ;; C/C++ Function prototypes (both are fastcall)
;; X86: ;; X86:
;; extern "C" void __fastcall MASM_RDRAND_GenerateBlock(byte* ptr, size_t size); ;; 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 .486
.MODEL FLAT .MODEL FLAT
;; Fastcall calling conventions exports
ALIAS <@MASM_RDRAND_GenerateBlock@8> = <MASM_RDRAND_GenerateBlock> ALIAS <@MASM_RDRAND_GenerateBlock@8> = <MASM_RDRAND_GenerateBlock>
ALIAS <@MASM_RDSEED_GenerateBlock@8> = <MASM_RDSEED_GenerateBlock> ALIAS <@MASM_RDSEED_GenerateBlock@8> = <MASM_RDSEED_GenerateBlock>
@ -149,7 +144,7 @@ OPTION EPILOGUE:NONE
;; RCX (in): arg1, byte* buffer ;; RCX (in): arg1, byte* buffer
;; RDX (in): arg2, size_t bsize ;; RDX (in): arg2, size_t bsize
MASM_RDRAND_GenerateBlock PROC MASM_RDRAND_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD
MWSIZE EQU 08h ;; machine word size MWSIZE EQU 08h ;; machine word size
buffer EQU rcx buffer EQU rcx
@ -335,7 +330,7 @@ OPTION EPILOGUE:NONE
;; RCX (in): arg1, byte* buffer ;; RCX (in): arg1, byte* buffer
;; RDX (in): arg2, size_t bsize ;; 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 MWSIZE EQU 08h ;; machine word size
buffer EQU rcx buffer EQU rcx

View File

@ -7,8 +7,6 @@
#include "rdrand.h" #include "rdrand.h"
#include "cpu.h" #include "cpu.h"
#include <iostream>
#if CRYPTOPP_MSC_VERSION #if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4100) # pragma warning(disable: 4100)
#endif #endif
@ -16,9 +14,9 @@
// This file (and friends) provides both RDRAND and RDSEED. They were added at // 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} // 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 // to select an implementation or "throw NotImplemented". The class does not
// use CPUID to determine if RDRAND or RDSEED are available. If not 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 // then a SIGILL will result. Users of the classes should call HasRDRAND()
// HasRDSEED() to determine if a generator is available. // or HasRDSEED() to determine if a generator is available.
// The original classes accepted a retry count. Retries were superflous for // The original classes accepted a retry count. Retries were superflous for
// RDRAND, and RDSEED encountered a failure about 1 in 256 bytes depending // RDRAND, and RDSEED encountered a failure about 1 in 256 bytes depending
// on the processor. Retries were removed at Crypto++ 6.0 because // on the processor. Retries were removed at Crypto++ 6.0 because
@ -104,23 +102,11 @@
#endif #endif
#if MASM_RDRAND_ASM_AVAILABLE #if MASM_RDRAND_ASM_AVAILABLE
# ifdef _M_X64
extern "C" void CRYPTOPP_FASTCALL MASM_RDRAND_GenerateBlock(byte*, size_t); 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 #endif
#if MASM_RDSEED_ASM_AVAILABLE #if MASM_RDSEED_ASM_AVAILABLE
# ifdef _M_X64
extern "C" void CRYPTOPP_FASTCALL MASM_RDSEED_GenerateBlock(byte*, size_t); 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 #endif
#if NASM_RDRAND_ASM_AVAILABLE #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) NAMESPACE_BEGIN(CryptoPP)
// Fills 4 bytes // 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 // RDRAND not detected at compile time, or no suitable compiler found
throw NotImplemented("RDRAND: failed to find a suitable implementation"); throw NotImplemented("RDRAND: failed to find a suitable implementation");
#endif #endif
// Size is not 0
s_unused ^= output[0];
} }
void RDRAND::DiscardBytes(size_t n) void RDRAND::DiscardBytes(size_t n)
@ -328,6 +304,7 @@ inline void RDSEED32(void* output)
#endif #endif
} }
#if CRYPTOPP_BOOL_X64
// Fills 8 bytes // Fills 8 bytes
inline void RDSEED64(void* output) inline void RDSEED64(void* output)
{ {
@ -365,6 +342,7 @@ inline void RDSEED64(void* output)
throw NotImplemented("RDSEED: failed to find an implementation"); throw NotImplemented("RDSEED: failed to find an implementation");
#endif #endif
} }
#endif // CRYPTOPP_BOOL_X64 and RDSEED64
void RDSEED::GenerateBlock(byte *output, size_t size) 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); std::memcpy(output, &val, size);
} }
#endif #endif
// Size is not 0
s_unused ^= output[0];
} }
void RDSEED::DiscardBytes(size_t n) void RDSEED::DiscardBytes(size_t n)