Botan 3.6.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/entropy_src.h>
10#include <botan/internal/loadstor.h>
11#include <botan/internal/os_utils.h>
12
13#if defined(BOTAN_HAS_SYSTEM_RNG)
14 #include <botan/system_rng.h>
15#endif
16
17#include <array>
18
19namespace Botan {
20
21void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) {
22 if(this->accepts_input()) {
23 constexpr auto s_hd_clk = sizeof(decltype(OS::get_high_resolution_clock()));
24 constexpr auto s_sys_ts = sizeof(decltype(OS::get_system_timestamp_ns()));
25 constexpr auto s_pid = sizeof(decltype(OS::get_process_id()));
26
27 std::array<uint8_t, s_hd_clk + s_sys_ts + s_pid> additional_input = {0};
28 auto s_additional_input = std::span(additional_input.begin(), additional_input.end());
29
30 store_le(OS::get_high_resolution_clock(), s_additional_input.data());
31 s_additional_input = s_additional_input.subspan(s_hd_clk);
32
33#if defined(BOTAN_HAS_SYSTEM_RNG)
35 system_rng.randomize(s_additional_input);
36#else
37 store_le(OS::get_system_timestamp_ns(), s_additional_input.data());
38 s_additional_input = s_additional_input.subspan(s_sys_ts);
39
40 store_le(OS::get_process_id(), s_additional_input.data());
41#endif
42
43 this->fill_bytes_with_input(output, additional_input);
44 } else {
45 this->fill_bytes_with_input(output, {});
46 }
47}
48
49size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) {
50 if(this->accepts_input()) {
51 return srcs.poll(*this, poll_bits, poll_timeout);
52 } else {
53 return 0;
54 }
55}
56
58 if(this->accepts_input()) {
59 this->add_entropy(rng.random_vec(poll_bits / 8));
60 }
61}
62
63void Null_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) {
64 // throw if caller tries to obtain random bytes
65 if(!output.empty()) {
66 throw PRNG_Unseeded("Null_RNG called");
67 }
68}
69
70} // namespace Botan
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
void randomize(std::span< uint8_t > output)
Definition rng.h:53
virtual bool accepts_input() const =0
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:76
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition rng.cpp:57
void randomize_with_ts_input(std::span< uint8_t > output)
Definition rng.cpp:21
void random_vec(std::span< uint8_t > v)
Definition rng.h:180
virtual void fill_bytes_with_input(std::span< uint8_t > output, std::span< const uint8_t > input)=0
virtual size_t reseed(Entropy_Sources &srcs, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS, std::chrono::milliseconds poll_timeout=BOTAN_RNG_RESEED_DEFAULT_TIMEOUT)
Definition rng.cpp:49
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:279
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:329
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:112
RandomNumberGenerator & system_rng()
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:764