Botan 3.14.1
Crypto and TLS for C&
rng.cpp
Go to the documentation of this file.
1/*
2* (C) 2016 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/rng.h>
8
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/buffer_stuffer.h>
12#include <botan/internal/loadstor.h>
13
14#if defined(BOTAN_HAS_ENTROPY_SOURCE)
15 #include <botan/entropy_src.h>
16#endif
17
18#if defined(BOTAN_HAS_SYSTEM_RNG)
19 #include <botan/system_rng.h>
20#endif
21
22#if defined(BOTAN_HAS_OS_UTILS)
23 #include <botan/internal/os_utils.h>
24#endif
25
26#include <array>
27
28namespace Botan {
29
30void RandomNumberGenerator::accept_seed_material(std::span<const uint8_t> input) {
31 this->fill_bytes_with_input({}, input);
32}
33
34namespace {
35
36// 22 bytes is the most HMAC_DRBG(SHA-256) can accept without needing an
37// additional compression for each HMAC in update
38constexpr size_t RollbackInputBytes = 22;
39
40/*
41* Fill out with values likely to be unique to this call even if the system RNG
42* repeats its output after a VM rollback the guest was not informed of.
43* Returns the number of bytes written.
44*
45* Ideally, we would use the processor RNG here (RDRAND/DARN), if available.
46* Unfortunately there is a huge variance in the performance of this instruction
47* depending on uarch and microcode; uops.info reports RDRAND instruction costs
48* ranging between 65 to 13800 cycles. On the higher end, a single RDRAND
49* invocation costs more than everything else (clock/pid read, system RNG draw,
50* and the HMAC_DRBG itself)
51*/
52size_t rollback_unique_input(std::span<uint8_t, RollbackInputBytes> out) {
53 BufferStuffer stuffer(out);
54
55#if defined(BOTAN_HAS_OS_UTILS)
56 // Uses the CPU cycle counter if available
57 stuffer.append(store_le(OS::get_high_resolution_clock()));
58#endif
59
60#if defined(BOTAN_HAS_SYSTEM_RNG)
61 system_rng().randomize(stuffer.next(stuffer.remaining_capacity()));
62#elif defined(BOTAN_HAS_OS_UTILS)
63 if(const uint32_t pid = OS::get_process_id(); pid != 0) {
64 stuffer.append(store_le(pid));
65 }
66#endif
67
68 return out.size() - stuffer.remaining_capacity();
69}
70
71} // namespace
72
73void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) {
74 if(!this->accepts_input()) {
75 this->fill_bytes_with_input(output, {});
76 return;
77 }
78
79 // TODO(C++26): let rollback_unique_input return std::inplace_vector
80 std::array<uint8_t, RollbackInputBytes> additional_input = {0};
81 const size_t written = rollback_unique_input(additional_input);
82 this->fill_bytes_with_input(output, std::span{additional_input}.first(written));
83}
84
86 if(this->accepts_input()) {
87#if defined(BOTAN_HAS_ENTROPY_SOURCE)
88 Entropy_Accumulator acc(poll_bits, [this](std::span<const uint8_t> in) { this->fill_bytes_with_input({}, in); });
89 srcs._gather(acc);
90 return acc.bits_collected();
91#else
92 BOTAN_UNUSED(srcs, poll_bits);
93#endif
94 }
95
96 return 0;
97}
98
100 if(this->accepts_input()) {
101 this->add_entropy(rng.random_vec(poll_bits / 8));
102 }
103}
104
105void Null_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) {
106 // throw if caller tries to obtain random bytes
107 if(!output.empty()) {
108 throw PRNG_Unseeded("Null_RNG called");
109 }
110}
111
112} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
Helper class to ease in-place marshalling of concatenated fixed-length values.
size_t _gather(Entropy_Accumulator &acc)
void randomize(std::span< uint8_t > output)
Definition rng.h:86
virtual bool accepts_input() const =0
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:121
virtual void accept_seed_material(std::span< const uint8_t > input)
Definition rng.cpp:30
virtual size_t reseed_from_sources(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits)
Definition rng.cpp:85
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits)
Definition rng.cpp:99
void randomize_with_ts_input(std::span< uint8_t > output)
Definition rng.cpp:73
void random_vec(std::span< uint8_t > v)
Definition rng.h:254
virtual void fill_bytes_with_input(std::span< uint8_t > output, std::span< const uint8_t > input)=0
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:273
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:78
RandomNumberGenerator & system_rng()
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736