Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

stateful_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/stateful_rng.h>
8 #include <botan/internal/os_utils.h>
9 #include <botan/loadstor.h>
10
11 namespace Botan {
12
13 void Stateful_RNG::clear()
14 {
15 m_reseed_counter = 0;
16 m_last_pid = 0;
17 }
18
19 void Stateful_RNG::force_reseed()
20 {
21 m_reseed_counter = 0;
22 }
23
24 bool Stateful_RNG::is_seeded() const
25 {
26 return m_reseed_counter > 0;
27 }
28
29 void Stateful_RNG::initialize_with(const uint8_t input[], size_t len)
30 {
31 add_entropy(input, len);
32
33 if(8*len >= security_level())
34 {
35 m_reseed_counter = 1;
36 }
37 }
38
39 void Stateful_RNG::randomize_with_ts_input(uint8_t output[], size_t output_len)
40 {
41 uint8_t additional_input[24] = { 0 };
42 store_le(OS::get_system_timestamp_ns(), additional_input);
43 store_le(OS::get_high_resolution_clock(), additional_input + 8);
44 store_le(m_last_pid, additional_input + 16);
45 store_le(static_cast<uint32_t>(m_reseed_counter), additional_input + 20);
46
47 randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
48 }
49
50 size_t Stateful_RNG::reseed(Entropy_Sources& srcs,
51 size_t poll_bits,
52 std::chrono::milliseconds poll_timeout)
53 {
54 size_t bits_collected = RandomNumberGenerator::reseed(srcs, poll_bits, poll_timeout);
55
56 if(bits_collected >= security_level())
57 {
58 m_reseed_counter = 1;
59 }
60
61 return bits_collected;
62 }
63
64 void Stateful_RNG::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits)
65 {
66 RandomNumberGenerator::reseed_from_rng(rng, poll_bits);
67
68 if(poll_bits >= security_level())
69 {
70 m_reseed_counter = 1;
71 }
72 }
73
74 void Stateful_RNG::reseed_check()
75 {
76 const uint32_t cur_pid = OS::get_process_id();
77
78 const bool fork_detected = (m_last_pid > 0) && (cur_pid != m_last_pid);
79
80 if(is_seeded() == false ||
81 fork_detected ||
82 (m_reseed_interval > 0 && m_reseed_counter >= m_reseed_interval))
83 {
84 m_reseed_counter = 0;
85 m_last_pid = cur_pid;
86
87 if(m_underlying_rng)
88 {
89 reseed_from_rng(*m_underlying_rng, security_level());
90 }
91
92 if(m_entropy_sources)
93 {
94 reseed(*m_entropy_sources, security_level());
95 }
96
97 if(!is_seeded())
98 {
99 if(fork_detected)
100 throw Exception("Detected use of fork but cannot reseed DRBG");
101 else
102 throw PRNG_Unseeded(name());
103 }
104 }
105 else
106 {
107 BOTAN_ASSERT(m_reseed_counter != 0, "RNG is seeded");
108 m_reseed_counter += 1;
109 }
110 }
111
112 }
Botan::Stateful_RNG::randomize_with_ts_input
void randomize_with_ts_input(uint8_t output[], size_t output_len) overridefinal
Definition: stateful_rng.cpp:39
Botan::Exception
Definition: exceptn.h:21
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator::randomize_with_input
virtual void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len)
Definition: rng.cpp:30
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::OS::get_process_id
uint32_t BOTAN_DLL get_process_id()
Definition: os_utils.cpp:31
Botan::Stateful_RNG::security_level
virtual size_t security_level() const =0
Botan::Stateful_RNG::clear
void clear() override
Definition: stateful_rng.cpp:13
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::Stateful_RNG::reseed_from_rng
void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS) overridefinal
Definition: stateful_rng.cpp:64
Botan::RandomNumberGenerator::reseed
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:37
Botan::RandomNumberGenerator::name
virtual std::string name() const =0
Botan::OS::get_system_timestamp_ns
uint64_t BOTAN_DLL get_system_timestamp_ns()
Definition: os_utils.cpp:157
Botan::Stateful_RNG::is_seeded
bool is_seeded() const overridefinal
Definition: stateful_rng.cpp:24
Botan::Stateful_RNG::force_reseed
void force_reseed()
Definition: stateful_rng.cpp:19
Botan
Definition: alg_id.cpp:13
Botan::Stateful_RNG::reseed_check
void reseed_check()
Definition: stateful_rng.cpp:74
Botan::RandomNumberGenerator::reseed_from_rng
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:44
Botan::Stateful_RNG::initialize_with
void initialize_with(const uint8_t input[], size_t length)
Definition: stateful_rng.cpp:29
Botan::Stateful_RNG::reseed
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) override
Definition: stateful_rng.cpp:50
Botan::Entropy_Sources
Definition: entropy_src.h:51
Botan::Stateful_RNG::m_reseed_counter
size_t m_reseed_counter
Definition: stateful_rng.h:132
Botan::PRNG_Unseeded
Definition: exceptn.h:120
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:457
Botan::OS::get_high_resolution_clock
uint64_t BOTAN_DLL get_high_resolution_clock()
Definition: os_utils.cpp:109