8 #include <botan/rdrand_rng.h>
9 #include <botan/loadstor.h>
10 #include <botan/cpuid.h>
12 #if !defined(BOTAN_USE_GCC_INLINE_ASM)
13 #include <immintrin.h>
20 #if defined(BOTAN_TARGET_ARCH_IS_X86_64)
21 typedef uint64_t rdrand_output;
23 typedef uint32_t rdrand_output;
27 rdrand_output read_rdrand()
33 const size_t RDRAND_RETRIES = 10;
35 for(
size_t i = 0; i < RDRAND_RETRIES; ++i)
40 #if defined(BOTAN_USE_GCC_INLINE_ASM)
42 asm(
"rdrand %0; adcl $0,%1" :
43 "=r" (r),
"=r" (cf) :
"0" (r),
"1" (cf) :
"cc");
44 #elif defined(BOTAN_TARGET_ARCH_IS_X86_64)
45 cf = _rdrand64_step(&r);
47 cf = _rdrand32_step(&r);
55 throw PRNG_Unseeded(
"RDRAND read failed");
62 while(out_len >=
sizeof(rdrand_output))
64 const rdrand_output r = read_rdrand();
66 out +=
sizeof(rdrand_output);
67 out_len -=
sizeof(rdrand_output);
72 const rdrand_output r = read_rdrand();
73 for(
size_t i = 0; i != out_len; ++i)
81 throw Invalid_State(
"Current CPU does not support RDRAND instruction");
87 return CPUID::has_rdrand();
93 return static_cast<uint32_t
>(read_rdrand());
104 const uint32_t r =
static_cast<uint32_t
>(read_rdrand());
void randomize(uint8_t out[], size_t out_len) override
constexpr uint8_t get_byte(size_t byte_num, T input)
#define BOTAN_FUNC_ISA(isa)
void store_le(uint16_t in, uint8_t out[2])