Botan 3.14.1
Crypto and TLS for C&
processor_rng.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/processor_rng.h>
8
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/hwrng.h>
12#include <botan/internal/loadstor.h>
13
14namespace Botan {
15
16namespace {
17
18uint64_t read_hwrng() {
19 if(const auto output = HWRNG::read()) {
20 return *output;
21 }
22
23 throw PRNG_Unseeded("Processor RNG instruction failed to produce output within expected iterations");
24}
25
26} // namespace
27
28//static
32
33std::string Processor_RNG::name() const {
34 return HWRNG::feature().to_string();
35}
36
37void Processor_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
38 // No way to provide entropy to processor-specific generator, ignore...
39 BOTAN_UNUSED(in);
40
41 while(out.size() >= 8) {
42 store_le(read_hwrng(), out.data());
43 out = out.subspan(8);
44 }
45
46 if(!out.empty()) {
47 // at most 7 bytes left
48 const auto hwrng_bytes = store_le(read_hwrng());
49 copy_mem(out, std::span{hwrng_bytes}.first(out.size()));
50 }
51}
52
55 throw Invalid_State("Current CPU does not support RNG instruction");
56 }
57}
58
59} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
std::string to_string() const
Definition cpuid.cpp:28
static bool available()
std::string name() const override
CPUID::Feature feature()
bool available()
Definition hwrng.cpp:11
std::optional< uint64_t > read()
Definition hwrng.cpp:15
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736