Botan
3.14.1
Crypto and TLS for C&
src
lib
utils
hwrng
hwrng_power
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
11
namespace
Botan::HWRNG
{
12
13
CPUID::Feature
feature
() {
14
return
CPUID::Feature::DARN
;
15
}
16
17
BOTAN_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
Botan::CPUFeature::DARN
@ DARN
Definition
cpuid_features.h:24
Botan::CPUID::Feature
CPUFeature Feature
Definition
cpuid.h:44
Botan::HWRNG
Definition
hwrng.cpp:9
Botan::HWRNG::read_once
std::optional< uint64_t > read_once()
Definition
hwrng_aarch64.cpp:23
Botan::HWRNG::feature
CPUID::Feature feature()
Definition
hwrng_aarch64.cpp:19
Generated by
1.16.1