/src/botan/src/lib/rng/rng.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
25 | | namespace Botan { |
26 | | |
27 | 0 | void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) { |
28 | 0 | if(this->accepts_input()) { |
29 | 0 | std::array<uint8_t, 32> additional_input = {0}; |
30 | |
|
31 | 0 | #if defined(BOTAN_HAS_OS_UTILS) |
32 | 0 | store_le(std::span{additional_input}.subspan<0, 8>(), OS::get_high_resolution_clock()); |
33 | 0 | store_le(std::span{additional_input}.subspan<8, 4>(), OS::get_process_id()); |
34 | 0 | 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 | 0 | BOTAN_UNUSED(offset); |
43 | 0 | #endif |
44 | |
|
45 | 0 | this->fill_bytes_with_input(output, additional_input); |
46 | 0 | } else { |
47 | 0 | this->fill_bytes_with_input(output, {}); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) { |
52 | 0 | if(this->accepts_input()) { |
53 | 0 | #if defined(BOTAN_HAS_ENTROPY_SOURCE) |
54 | 0 | return srcs.poll(*this, poll_bits, poll_timeout); |
55 | | #else |
56 | | BOTAN_UNUSED(srcs, poll_bits, poll_timeout); |
57 | | #endif |
58 | 0 | } |
59 | | |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | 0 | void RandomNumberGenerator::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits) { |
64 | 0 | if(this->accepts_input()) { |
65 | 0 | this->add_entropy(rng.random_vec(poll_bits / 8)); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 0 | void 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 | 0 | if(!output.empty()) { |
72 | 0 | throw PRNG_Unseeded("Null_RNG called"); |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | } // namespace Botan |