Botan 3.7.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/internal/loadstor.h>
10
11#if defined(BOTAN_HAS_ENTROPY_SOURCE)
12 #include <botan/entropy_src.h>
13#endif
14
15#if defined(BOTAN_HAS_SYSTEM_RNG)
16 #include <botan/system_rng.h>
17#endif
18
19#if defined(BOTAN_HAS_OS_UTILS)
20 #include <botan/internal/os_utils.h>
21#endif
22
23#include <array>
24
25namespace Botan {
26
27void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) {
28 if(this->accepts_input()) {
29 std::array<uint8_t, 32> additional_input = {0};
30
31#if defined(BOTAN_HAS_OS_UTILS)
32 store_le(std::span{additional_input}.subspan<0, 8>(), OS::get_high_resolution_clock());
33 store_le(std::span{additional_input}.subspan<8, 4>(), OS::get_process_id());
34 constexpr size_t offset = 12;
35#else
36 constexpr size_t offset = 0;
37#endif
38
39#if defined(BOTAN_HAS_SYSTEM_RNG)
40 system_rng().randomize(std::span{additional_input}.subspan<offset>());
41#else
42 BOTAN_UNUSED(offset);
43#endif
44
45 this->fill_bytes_with_input(output, additional_input);
46 } else {
47 this->fill_bytes_with_input(output, {});
48 }
49}
50
51size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) {
52 if(this->accepts_input()) {
53#if defined(BOTAN_HAS_ENTROPY_SOURCE)
54 return srcs.poll(*this, poll_bits, poll_timeout);
55#else
56 BOTAN_UNUSED(srcs, poll_bits, poll_timeout);
57#endif
58 }
59
60 return 0;
61}
62
64 if(this->accepts_input()) {
65 this->add_entropy(rng.random_vec(poll_bits / 8));
66 }
67}
68
69void Null_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) {
70 // throw if caller tries to obtain random bytes
71 if(!output.empty()) {
72 throw PRNG_Unseeded("Null_RNG called");
73 }
74}
75
76} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
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:63
void randomize_with_ts_input(std::span< uint8_t > output)
Definition rng.cpp:27
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:51
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition os_utils.cpp:272
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:82
RandomNumberGenerator & system_rng()
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:764