Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

auto_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/auto_rng.h>
8 #include <botan/entropy_src.h>
9 #include <botan/hmac_drbg.h>
10
11 #if defined(BOTAN_HAS_SYSTEM_RNG)
12 #include <botan/system_rng.h>
13 #endif
14
15 #if !defined(BOTAN_AUTO_RNG_HMAC)
16 #error "No hash function defined for AutoSeeded_RNG in build.h (try enabling sha2_32)"
17 #endif
18
19 namespace Botan {
20
21 AutoSeeded_RNG::~AutoSeeded_RNG()
22 {
23 // for unique_ptr
24 }
25
26 AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng,
27 size_t reseed_interval)
28 {
29 m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
30 underlying_rng,
31 reseed_interval));
32 force_reseed();
33 }
34
35 AutoSeeded_RNG::AutoSeeded_RNG(Entropy_Sources& entropy_sources,
36 size_t reseed_interval)
37 {
38 m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
39 entropy_sources,
40 reseed_interval));
41 force_reseed();
42 }
43
44 AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng,
45 Entropy_Sources& entropy_sources,
46 size_t reseed_interval)
47 {
48 m_rng.reset(new HMAC_DRBG(
49 MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC),
50 underlying_rng, entropy_sources, reseed_interval));
51 force_reseed();
52 }
53
54 AutoSeeded_RNG::AutoSeeded_RNG(size_t reseed_interval) :
55 #if defined(BOTAN_HAS_SYSTEM_RNG)
56 AutoSeeded_RNG(system_rng(), reseed_interval)
57 #else
58 AutoSeeded_RNG(Entropy_Sources::global_sources(), reseed_interval)
59 #endif
60 {
61 }
62
63 void AutoSeeded_RNG::force_reseed()
64 {
65 m_rng->force_reseed();
66 m_rng->next_byte();
67
68 if(!m_rng->is_seeded())
69 {
70 throw Exception("AutoSeeded_RNG reseeding failed");
71 }
72 }
73
74 bool AutoSeeded_RNG::is_seeded() const
75 {
76 return m_rng->is_seeded();
77 }
78
79 void AutoSeeded_RNG::clear()
80 {
81 m_rng->clear();
82 }
83
84 std::string AutoSeeded_RNG::name() const
85 {
86 return m_rng->name();
87 }
88
89 void AutoSeeded_RNG::add_entropy(const uint8_t in[], size_t len)
90 {
91 m_rng->add_entropy(in, len);
92 }
93
94 size_t AutoSeeded_RNG::reseed(Entropy_Sources& srcs,
95 size_t poll_bits,
96 std::chrono::milliseconds poll_timeout)
97 {
98 return m_rng->reseed(srcs, poll_bits, poll_timeout);
99 }
100
101 void AutoSeeded_RNG::randomize(uint8_t output[], size_t output_len)
102 {
103 randomize_with_ts_input(output, output_len);
104 }
105
106 void AutoSeeded_RNG::randomize_with_input(uint8_t output[], size_t output_len,
107 const uint8_t ad[], size_t ad_len)
108 {
109 m_rng->randomize_with_input(output, output_len, ad, ad_len);
110 }
111
112 }
Botan::AutoSeeded_RNG::randomize
void randomize(uint8_t out[], size_t len) override
Definition: auto_rng.cpp:101
Botan::Exception
Definition: exceptn.h:21
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::AutoSeeded_RNG::clear
void clear() override
Definition: auto_rng.cpp:79
Botan::AutoSeeded_RNG::AutoSeeded_RNG
AutoSeeded_RNG(size_t reseed_interval=BOTAN_RNG_DEFAULT_RESEED_INTERVAL)
Definition: auto_rng.cpp:54
Botan::RandomNumberGenerator::randomize_with_ts_input
virtual void randomize_with_ts_input(uint8_t output[], size_t output_len)
Definition: rng.cpp:17
Botan::AutoSeeded_RNG::~AutoSeeded_RNG
~AutoSeeded_RNG()
Definition: auto_rng.cpp:21
Botan::AutoSeeded_RNG::force_reseed
void force_reseed()
Definition: auto_rng.cpp:63
Botan::AutoSeeded_RNG::name
std::string name() const override
Definition: auto_rng.cpp:84
Botan::AutoSeeded_RNG
Definition: auto_rng.h:20
Botan::AutoSeeded_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: auto_rng.cpp:94
Botan
Definition: alg_id.cpp:13
Botan::AutoSeeded_RNG::is_seeded
bool is_seeded() const override
Definition: auto_rng.cpp:74
Botan::AutoSeeded_RNG::randomize_with_input
void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len) override
Definition: auto_rng.cpp:106
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:196
Botan::Entropy_Sources
Definition: entropy_src.h:51
Botan::AutoSeeded_RNG::add_entropy
void add_entropy(const uint8_t in[], size_t len) override
Definition: auto_rng.cpp:89
Botan::MessageAuthenticationCode::create_or_throw
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:138
Botan::HMAC_DRBG
Definition: hmac_drbg.h:21