11 #if CRYPTOPP_MSC_VERSION 12 # pragma warning(disable: 4100) 41 #define NASM_RDRAND_ASM_AVAILABLE 1 42 #define NASM_RDSEED_ASM_AVAILABLE 1 51 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) 52 # ifndef CRYPTOPP_CPUID_AVAILABLE 53 # define CRYPTOPP_CPUID_AVAILABLE 57 #if defined(CRYPTOPP_CPUID_AVAILABLE) 58 # if defined(CRYPTOPP_MSC_VERSION) 59 # define MASM_RDRAND_ASM_AVAILABLE 1 60 # define MASM_RDSEED_ASM_AVAILABLE 1 61 # elif defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION) 62 # define GCC_RDRAND_ASM_AVAILABLE 1 63 # define GCC_RDSEED_ASM_AVAILABLE 1 64 # elif defined(__SUNPRO_CC) 65 # if defined(__RDRND__) && (__SUNPRO_CC >= 0x5130) 66 # define ALL_RDRAND_INTRIN_AVAILABLE 1 67 # elif (__SUNPRO_CC >= 0x5100) 68 # define GCC_RDRAND_ASM_AVAILABLE 1 70 # if defined(__RDSEED__) && (__SUNPRO_CC >= 0x5140) 71 # define ALL_RDSEED_INTRIN_AVAILABLE 1 72 # elif (__SUNPRO_CC >= 0x5100) 73 # define GCC_RDSEED_ASM_AVAILABLE 1 75 # elif defined(CRYPTOPP_GCC_VERSION) 76 # if defined(__RDRND__) && (CRYPTOPP_GCC_VERSION >= 30200) 77 # define ALL_RDRAND_INTRIN_AVAILABLE 1 79 # define GCC_RDRAND_ASM_AVAILABLE 1 81 # if defined(__RDSEED__) && (CRYPTOPP_GCC_VERSION >= 30200) 82 # define ALL_RDSEED_INTRIN_AVAILABLE 1 84 # define GCC_RDSEED_ASM_AVAILABLE 1 91 # if MASM_RDRAND_ASM_AVAILABLE 92 # pragma message ("MASM_RDRAND_ASM_AVAILABLE is 1") 93 # elif NASM_RDRAND_ASM_AVAILABLE 94 # pragma message ("NASM_RDRAND_ASM_AVAILABLE is 1") 95 # elif GCC_RDRAND_ASM_AVAILABLE 96 # pragma message ("GCC_RDRAND_ASM_AVAILABLE is 1") 97 # elif ALL_RDRAND_INTRIN_AVAILABLE 98 # pragma message ("ALL_RDRAND_INTRIN_AVAILABLE is 1") 100 # pragma message ("RDRAND is not available") 102 # if MASM_RDSEED_ASM_AVAILABLE 103 # pragma message ("MASM_RDSEED_ASM_AVAILABLE is 1") 104 # elif NASM_RDSEED_ASM_AVAILABLE 105 # pragma message ("NASM_RDSEED_ASM_AVAILABLE is 1") 106 # elif GCC_RDSEED_ASM_AVAILABLE 107 # pragma message ("GCC_RDSEED_ASM_AVAILABLE is 1") 108 # elif ALL_RDSEED_INTRIN_AVAILABLE 109 # pragma message ("ALL_RDSEED_INTRIN_AVAILABLE is 1") 111 # pragma message ("RDSEED is not available") 118 #if (ALL_RDRAND_INTRIN_AVAILABLE || ALL_RDSEED_INTRIN_AVAILABLE) 119 # include <immintrin.h> 120 # if defined(__GNUC__) && (CRYPTOPP_GCC_VERSION >= 40600) 121 # include <x86intrin.h> 123 # if defined(__has_include) 124 # if __has_include(<x86intrin.h>) 125 # include <x86intrin.h> 130 #if MASM_RDRAND_ASM_AVAILABLE 135 extern "C" int MASM_RRA_GenerateBlock(
byte*,
size_t,
unsigned int);
140 #if MASM_RDSEED_ASM_AVAILABLE 145 extern "C" int MASM_RSA_GenerateBlock(
byte*,
size_t,
unsigned int);
150 #if NASM_RDRAND_ASM_AVAILABLE 151 extern "C" int NASM_RRA_GenerateBlock(
byte*,
size_t,
unsigned int);
154 #if NASM_RDSEED_ASM_AVAILABLE 155 extern "C" int NASM_RSA_GenerateBlock(
byte*,
size_t,
unsigned int);
163 #if ALL_RDRAND_INTRIN_AVAILABLE 164 static int ALL_RRI_GenerateBlock(
byte *output,
size_t size,
unsigned int safety)
167 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 173 while (size >=
sizeof(val))
175 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 176 if (_rdrand32_step((
word32*)output))
179 if (_rdrand64_step(reinterpret_cast<unsigned long long*>(output)))
182 output +=
sizeof(val);
197 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 198 if (_rdrand32_step(&val))
201 if (_rdrand64_step(reinterpret_cast<unsigned long long*>(&val)))
204 memcpy(output, &val, size);
219 return int(size == 0);
221 #endif // ALL_RDRAND_INTRINSIC_AVAILABLE 223 #if GCC_RDRAND_ASM_AVAILABLE 224 static int GCC_RRA_GenerateBlock(
byte *output,
size_t size,
unsigned int safety)
227 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 236 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 237 ".byte 0x48, 0x0f, 0xc7, 0xf0;\n" 239 ".byte 0x0f, 0xc7, 0xf0;\n" 242 :
"=a" (val),
"=qm" (rc)
249 if (size >=
sizeof(val))
252 output +=
sizeof(val);
257 memcpy(output, &val, size);
273 return int(size == 0);
276 #endif // GCC_RDRAND_ASM_AVAILABLE 278 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) 285 throw NotImplemented(
"RDRAND: rdrand is not available on this platform");
288 #if MASM_RDRAND_ASM_AVAILABLE 289 rc = MASM_RRA_GenerateBlock(output, size,
m_retries);
290 if (!rc) {
throw RDRAND_Err(
"MASM_RRA_GenerateBlock"); }
291 #elif NASM_RDRAND_ASM_AVAILABLE 292 rc = NASM_RRA_GenerateBlock(output, size,
m_retries);
293 if (!rc) {
throw RDRAND_Err(
"NASM_RRA_GenerateBlock"); }
294 #elif ALL_RDRAND_INTRIN_AVAILABLE 295 rc = ALL_RRI_GenerateBlock(output, size,
m_retries);
296 if (!rc) {
throw RDRAND_Err(
"ALL_RRI_GenerateBlock"); }
297 #elif GCC_RDRAND_ASM_AVAILABLE 298 rc = GCC_RRA_GenerateBlock(output, size,
m_retries);
299 if (!rc) {
throw RDRAND_Err(
"GCC_RRA_GenerateBlock"); }
302 throw NotImplemented(
"RDRAND: failed to find a suitable implementation???");
303 #endif // CRYPTOPP_CPUID_AVAILABLE 311 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 327 #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 332 #if ALL_RDSEED_INTRIN_AVAILABLE 333 static int ALL_RSI_GenerateBlock(
byte *output,
size_t size,
unsigned int safety)
336 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 342 while (size >=
sizeof(val))
344 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 345 if (_rdseed32_step((
word32*)output))
348 if (_rdseed64_step(reinterpret_cast<unsigned long long*>(output)))
351 output +=
sizeof(val);
366 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 367 if (_rdseed32_step(&val))
370 if (_rdseed64_step(reinterpret_cast<unsigned long long*>(&val)))
373 memcpy(output, &val, size);
388 return int(size == 0);
390 #endif // ALL_RDSEED_INTRIN_AVAILABLE 392 #if GCC_RDSEED_ASM_AVAILABLE 393 static int GCC_RSA_GenerateBlock(
byte *output,
size_t size,
unsigned int safety)
396 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 405 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 406 ".byte 0x48, 0x0f, 0xc7, 0xf8;\n" 408 ".byte 0x0f, 0xc7, 0xf8;\n" 411 :
"=a" (val),
"=qm" (rc)
418 if (size >=
sizeof(val))
421 output +=
sizeof(val);
426 memcpy(output, &val, size);
442 return int(size == 0);
444 #endif // GCC_RDSEED_ASM_AVAILABLE 446 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) 453 throw NotImplemented(
"RDSEED: rdseed is not available on this platform");
456 #if MASM_RDSEED_ASM_AVAILABLE 457 rc = MASM_RSA_GenerateBlock(output, size,
m_retries);
458 if (!rc) {
throw RDSEED_Err(
"MASM_RSA_GenerateBlock"); }
459 #elif NASM_RDSEED_ASM_AVAILABLE 460 rc = NASM_RSA_GenerateBlock(output, size,
m_retries);
461 if (!rc) {
throw RDRAND_Err(
"NASM_RSA_GenerateBlock"); }
462 #elif ALL_RDSEED_INTRIN_AVAILABLE 463 rc = ALL_RSI_GenerateBlock(output, size,
m_retries);
464 if (!rc) {
throw RDSEED_Err(
"ALL_RSI_GenerateBlock"); }
465 #elif GCC_RDSEED_ASM_AVAILABLE 466 rc = GCC_RSA_GenerateBlock(output, size,
m_retries);
467 if (!rc) {
throw RDSEED_Err(
"GCC_RSA_GenerateBlock"); }
470 throw NotImplemented(
"RDSEED: failed to find a suitable implementation???");
479 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 495 #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 void SecureWipeBuffer(T *buf, size_t n)
Sets each element of an array to 0.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
#define NAMESPACE_BEGIN(x)
Abstract base classes that provide a uniform interface to this library.
Library configuration file.
Classes for RDRAND and RDSEED.
byte order is little-endian
Classes and functions for secure memory allocations.
Exception thrown when a RDRAND generator encounters a generator related error.
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
A method was called which was not implemented.
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
unsigned long long word64
Fixed size stack-based SecBlock.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Functions for CPU features and intrinsics.
uint8_t const size_t const size
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
#define CRYPTOPP_FASTCALL
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Exception thrown when a RDSEED generator encounters a generator related error.
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.