Botan 3.14.1
Crypto and TLS for C&
Botan::HWRNG Namespace Reference

Functions

bool available ()
CPUID::Feature feature ()
std::optional< uint64_t > read ()
std::optional< uint64_t > read_once ()

Function Documentation

◆ available()

BOTAN_TEST_API bool Botan::HWRNG::available ( )

Return true if the current CPU supports the hardware RNG instruction

Definition at line 11 of file hwrng.cpp.

11 {
12 return CPUID::has(feature());
13}
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
CPUID::Feature feature()

References feature(), and Botan::CPUID::has().

Referenced by Botan::Processor_RNG::available().

◆ feature()

CPUID::Feature Botan::HWRNG::feature ( )

Return the CPUID feature which indicates the hardware RNG is available

Definition at line 19 of file hwrng_aarch64.cpp.

References Botan::CPUFeature::RNG.

Referenced by available(), and Botan::Processor_RNG::name().

◆ read()

BOTAN_TEST_API std::optional< uint64_t > Botan::HWRNG::read ( )

Read 64 bits from the hardware RNG

Returns nullopt if the instruction repeatedly failed to produce an output, which indicates a hardware fault.

Must only be called if available() returns true

Definition at line 15 of file hwrng.cpp.

15 {
16 /*
17 * Intel DRNG Software Implementation Guide, Section 5.2.1
18 * It is recommended that applications attempt 10 retries in a tight loop
19 * in the unlikely event that the RDRAND instruction does not return a
20 * random number. This number is based on a binomial probability argument:
21 * given the design margins of the DRNG, the odds of ten failures in a row
22 * are astronomically small and would in fact be an indication of a larger
23 * CPU issue.
24 *
25 * PowerISA 3.0 p.78:
26 * When the error value is obtained, software is expected to repeat the
27 * operation. [...] The recommended number of attempts may be
28 * implementation specific. In the absence of other guidance, ten attempts
29 * should be adequate.
30 */
31 constexpr size_t HWRNG_RETRIES = 10;
32
33 for(size_t i = 0; i != HWRNG_RETRIES; ++i) {
34 if(const auto output = read_once()) {
35 return output;
36 }
37 }
38
39 return std::nullopt;
40}
std::optional< uint64_t > read_once()

References read_once().

◆ read_once()

std::optional< uint64_t > Botan::HWRNG::read_once ( )

Make a single attempt to read 64 bits from the hardware RNG

Returns nullopt if the instruction indicated failure

Must only be called if available() returns true

Definition at line 23 of file hwrng_aarch64.cpp.

23 {
24 uint64_t output = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
25
26#if defined(BOTAN_USE_GCC_INLINE_ASM)
27 uint64_t ok = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
28 // RNDR sets the Z flag if it could not produce a random number
29 // NOLINTNEXTLINE(*-no-assembler)
30 asm volatile("mrs %0, rndr; cset %1, ne" : "=r"(output), "=r"(ok) : : "cc");
31#else
32 const bool ok = (__rndr(&output) == 0);
33#endif
34
35 if(ok) {
36 return output;
37 } else {
38 return std::nullopt;
39 }
40}

Referenced by read().