Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 #include <botan/entropy_src.h>
9 #include <botan/loadstor.h>
10 #include <botan/internal/os_utils.h>
11
12 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
13 #include <botan/auto_rng.h>
14 #endif
15
16 namespace Botan {
17
18 void RandomNumberGenerator::randomize_with_ts_input(uint8_t output[], size_t output_len)
19 {
20 if(this->accepts_input())
21 {
22 /*
23 Form additional input which is provided to the PRNG implementation
24 to paramaterize the KDF output.
25 */
26 uint8_t additional_input[16] = { 0 };
27 store_le(OS::get_system_timestamp_ns(), additional_input);
28 store_le(OS::get_high_resolution_clock(), additional_input + 8);
29
30 this->randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
31 }
32 else
33 {
34 this->randomize(output, output_len);
35 }
36 }
37
38 void RandomNumberGenerator::randomize_with_input(uint8_t output[], size_t output_len,
39 const uint8_t input[], size_t input_len)
40 {
41 this->add_entropy(input, input_len);
42 this->randomize(output, output_len);
43 }
44
45 size_t RandomNumberGenerator::reseed(Entropy_Sources& srcs,
46 size_t poll_bits,
47 std::chrono::milliseconds poll_timeout)
48 {
49 if(this->accepts_input())
50 {
51 return srcs.poll(*this, poll_bits, poll_timeout);
52 }
53 else
54 {
55 return 0;
56 }
57 }
58
59 void RandomNumberGenerator::reseed_from_rng(RandomNumberGenerator& rng, size_t poll_bits)
60 {
61 if(this->accepts_input())
62 {
63 secure_vector<uint8_t> buf(poll_bits / 8);
64 rng.randomize(buf.data(), buf.size());
65 this->add_entropy(buf.data(), buf.size());
66 }
67 }
68
69 RandomNumberGenerator* RandomNumberGenerator::make_rng()
70 {
71 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
72 return new AutoSeeded_RNG;
73 #else
74 throw Not_Implemented("make_rng failed, no AutoSeeded_RNG in this build");
75 #endif
76 }
77
78 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
79
80 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
81 Serialized_RNG::Serialized_RNG() : m_rng(new AutoSeeded_RNG) {}
82 #else
83 Serialized_RNG::Serialized_RNG()
84 {
85 throw Not_Implemented("Serialized_RNG default constructor failed: AutoSeeded_RNG disabled in build");
86 }
87 #endif
88
89 #endif
90
91 }
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:38
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0
Botan::RandomNumberGenerator::randomize_with_ts_input
virtual void randomize_with_ts_input(uint8_t output[], size_t output_len)
Definition: rng.cpp:18
Botan::Not_Implemented
Definition: exceptn.h:355
Botan::RandomNumberGenerator::make_rng
static RandomNumberGenerator * make_rng()
Definition: rng.cpp:69
Botan::AutoSeeded_RNG
Definition: auto_rng.h:20
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:45
Botan::RandomNumberGenerator::accepts_input
virtual bool accepts_input() const =0
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::OS::get_system_timestamp_ns
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:293
Botan
Definition: alg_id.cpp:13
m_rng
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:54
Botan::RandomNumberGenerator::reseed_from_rng
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
Botan::Entropy_Sources
Definition: entropy_src.h:56
Botan::Entropy_Sources::poll
size_t poll(RandomNumberGenerator &rng, size_t bits, std::chrono::milliseconds timeout)
Definition: entropy_srcs.cpp:149
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:454
Botan::OS::get_high_resolution_clock
uint64_t BOTAN_TEST_API get_high_resolution_clock()
Definition: os_utils.cpp:241