Botan 3.14.1
Crypto and TLS for C&
hwrng_power.cpp
Go to the documentation of this file.
1/*
2* (C) 2016,2019,2020,2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/hwrng.h>
8
9#include <botan/internal/isa_extn.h>
10
11namespace Botan::HWRNG {
12
15}
16
17BOTAN_FN_ISA_RNG std::optional<uint64_t> read_once() {
18 /*
19 DARN indicates error by returning 0xFF..FF, ie is biased. Which is crazy.
20 Avoid the bias by invoking it twice and, assuming both succeed, returning the
21 XOR of the two results, which should unbias the output.
22 */
23 uint64_t output = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
24 uint64_t output2 = 0; // NOLINT(*-const-correctness) clang-tidy doesn't understand inline asm
25 // DARN codes are 0: 32-bit conditioned, 1: 64-bit conditioned, 2: 64-bit raw (ala RDSEED)
26 asm volatile("darn %0, 1" : "=r"(output)); // NOLINT(*-no-assembler)
27 asm volatile("darn %0, 1" : "=r"(output2)); // NOLINT(*-no-assembler)
28
29 const bool success = (~output) != 0 && (~output2) != 0;
30
31 if(success) {
32 return output ^ output2;
33 } else {
34 return std::nullopt;
35 }
36}
37
38} // namespace Botan::HWRNG
CPUFeature Feature
Definition cpuid.h:44
std::optional< uint64_t > read_once()
CPUID::Feature feature()