/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/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 | | |
19 | | namespace Botan { |
20 | | |
21 | 0 | void RandomNumberGenerator::randomize_with_ts_input(std::span<uint8_t> output) { |
22 | 0 | if(this->accepts_input()) { |
23 | 0 | constexpr auto s_hd_clk = sizeof(decltype(OS::get_high_resolution_clock())); |
24 | 0 | constexpr auto s_sys_ts = sizeof(decltype(OS::get_system_timestamp_ns())); |
25 | 0 | constexpr auto s_pid = sizeof(decltype(OS::get_process_id())); |
26 | |
|
27 | 0 | std::array<uint8_t, s_hd_clk + s_sys_ts + s_pid> additional_input = {0}; |
28 | 0 | auto s_additional_input = std::span(additional_input.begin(), additional_input.end()); |
29 | |
|
30 | 0 | store_le(OS::get_high_resolution_clock(), s_additional_input.data()); |
31 | 0 | s_additional_input = s_additional_input.subspan(s_hd_clk); |
32 | |
|
33 | 0 | #if defined(BOTAN_HAS_SYSTEM_RNG) |
34 | 0 | System_RNG system_rng; |
35 | 0 | 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 | 0 | this->fill_bytes_with_input(output, additional_input); |
44 | 0 | } else { |
45 | 0 | this->fill_bytes_with_input(output, {}); |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | 0 | size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs, size_t poll_bits, std::chrono::milliseconds poll_timeout) { |
50 | 0 | if(this->accepts_input()) { |
51 | 0 | return srcs.poll(*this, poll_bits, poll_timeout); |
52 | 0 | } else { |
53 | 0 | return 0; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | 0 | void RandomNumberGenerator::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits) { |
58 | 0 | if(this->accepts_input()) { |
59 | 0 | this->add_entropy(rng.random_vec(poll_bits / 8)); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | } // namespace Botan |