7#include <botan/internal/hwrng.h>
9#include <botan/internal/isa_extn.h>
10#include <botan/internal/target_info.h>
12#if !defined(BOTAN_USE_GCC_INLINE_ASM)
13 #include <immintrin.h>
20#if defined(BOTAN_TARGET_ARCH_IS_X86_32)
21using rdrand_output = uint32_t;
23using rdrand_output = uint64_t;
26BOTAN_FN_ISA_RNG std::optional<rdrand_output> rdrand() {
27 rdrand_output output = 0;
30#if defined(BOTAN_USE_GCC_INLINE_ASM)
33 asm volatile(
"rdrand %0; adcl $0,%1" :
"=r"(output),
"=r"(cf) :
"0"(output),
"1"(cf) :
"cc");
34#elif defined(BOTAN_TARGET_ARCH_IS_X86_32)
35 cf = _rdrand32_step(&output);
37 unsigned long long outputll = 0;
38 cf = _rdrand64_step(&outputll);
39 output =
static_cast<uint64_t
>(outputll);
56#if defined(BOTAN_TARGET_ARCH_IS_X86_32)
58 const auto lo = rdrand();
59 const auto hi = rdrand();
62 return (
static_cast<uint64_t
>(*hi) << 32) | *lo;
std::optional< uint64_t > read_once()