Botan 3.13.0
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/exceptn.h>
10#include <botan/internal/loadstor.h>
11
12#if defined(BOTAN_HAS_ENTROPY_SOURCE)
13 #include <botan/entropy_src.h>
14#endif
15
16#if defined(BOTAN_HAS_SYSTEM_RNG)
17 #include <botan/system_rng.h>
18#elif defined(BOTAN_HAS_OS_UTILS)
19 #include <botan/internal/os_utils.h>
20#endif
21
22#include <array>
23
24namespace Botan {
25
26void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) {
27 if(this->accepts_input()) {
28 std::array<uint8_t, 16> additional_input = {0};
29
30#if defined(BOTAN_HAS_SYSTEM_RNG)
31 // If we have a system RNG just read 128 bits from that
32 system_rng().randomize(additional_input);
33 constexpr size_t written = additional_input.size();
34#elif defined(BOTAN_HAS_OS_UTILS)
35 // Otherwise take clock + pid
36 const uint64_t clock = OS::get_high_resolution_clock();
37 const uint32_t pid = OS::get_process_id(); // 0 if no PIDs on this system
38
39 store_le(std::span{additional_input}.first<8>(), clock);
40 store_le(std::span{additional_input}.subspan<8, 4>(), pid);
41 const size_t written = 8 + (pid != 0) ? 4 : 0;
42#else
43 // Nothing to use in this case
44 constexpr size_t written = 0;
45#endif
46
47 this->fill_bytes_with_input(output, std::span{additional_input}.first(written));
48 } else {
49 this->fill_bytes_with_input(output, {});
50 }
51}
52
54 if(this->accepts_input()) {
55#if defined(BOTAN_HAS_ENTROPY_SOURCE)
56 return srcs.poll(*this, poll_bits);
57#else
58 BOTAN_UNUSED(srcs, poll_bits);
59#endif
60 }
61
62 return 0;
63}
64
66 if(this->accepts_input()) {
67 this->add_entropy(rng.random_vec(poll_bits / 8));
68 }
69}
70
71void Null_RNG::fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> /* ignored */) {
72 // throw if caller tries to obtain random bytes
73 if(!output.empty()) {
74 throw PRNG_Unseeded("Null_RNG called");
75 }
76}
77
78} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
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:114
virtual size_t reseed_from_sources(Entropy_Sources &srcs, size_t poll_bits=RandomNumberGenerator::DefaultPollBits)
Definition rng.cpp:53
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=RandomNumberGenerator::DefaultPollBits)
Definition rng.cpp:65
void randomize_with_ts_input(std::span< uint8_t > output)
Definition rng.cpp:26
void random_vec(std::span< uint8_t > v)
Definition rng.h:244
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:272
uint32_t BOTAN_TEST_API get_process_id()
Definition os_utils.cpp:77
RandomNumberGenerator & system_rng()
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736