/src/botan/src/lib/rng/auto_rng/auto_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/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 | 0 | { |
23 | 0 | // for unique_ptr |
24 | 0 | } |
25 | | |
26 | | AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng, |
27 | | size_t reseed_interval) |
28 | 0 | { |
29 | 0 | m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC), |
30 | 0 | underlying_rng, |
31 | 0 | reseed_interval)); |
32 | 0 | force_reseed(); |
33 | 0 | } |
34 | | |
35 | | AutoSeeded_RNG::AutoSeeded_RNG(Entropy_Sources& entropy_sources, |
36 | | size_t reseed_interval) |
37 | 0 | { |
38 | 0 | m_rng.reset(new HMAC_DRBG(MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC), |
39 | 0 | entropy_sources, |
40 | 0 | reseed_interval)); |
41 | 0 | force_reseed(); |
42 | 0 | } |
43 | | |
44 | | AutoSeeded_RNG::AutoSeeded_RNG(RandomNumberGenerator& underlying_rng, |
45 | | Entropy_Sources& entropy_sources, |
46 | | size_t reseed_interval) |
47 | 0 | { |
48 | 0 | m_rng.reset(new HMAC_DRBG( |
49 | 0 | MessageAuthenticationCode::create_or_throw(BOTAN_AUTO_RNG_HMAC), |
50 | 0 | underlying_rng, entropy_sources, reseed_interval)); |
51 | 0 | force_reseed(); |
52 | 0 | } |
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 | 0 | { |
61 | 0 | } |
62 | | |
63 | | void AutoSeeded_RNG::force_reseed() |
64 | 0 | { |
65 | 0 | m_rng->force_reseed(); |
66 | 0 | m_rng->next_byte(); |
67 | 0 |
|
68 | 0 | if(!m_rng->is_seeded()) |
69 | 0 | { |
70 | 0 | throw Internal_Error("AutoSeeded_RNG reseeding failed"); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | bool AutoSeeded_RNG::is_seeded() const |
75 | 0 | { |
76 | 0 | return m_rng->is_seeded(); |
77 | 0 | } |
78 | | |
79 | | void AutoSeeded_RNG::clear() |
80 | 0 | { |
81 | 0 | m_rng->clear(); |
82 | 0 | } |
83 | | |
84 | | std::string AutoSeeded_RNG::name() const |
85 | 0 | { |
86 | 0 | return m_rng->name(); |
87 | 0 | } |
88 | | |
89 | | void AutoSeeded_RNG::add_entropy(const uint8_t in[], size_t len) |
90 | 0 | { |
91 | 0 | m_rng->add_entropy(in, len); |
92 | 0 | } |
93 | | |
94 | | size_t AutoSeeded_RNG::reseed(Entropy_Sources& srcs, |
95 | | size_t poll_bits, |
96 | | std::chrono::milliseconds poll_timeout) |
97 | 0 | { |
98 | 0 | return m_rng->reseed(srcs, poll_bits, poll_timeout); |
99 | 0 | } |
100 | | |
101 | | void AutoSeeded_RNG::randomize(uint8_t output[], size_t output_len) |
102 | 0 | { |
103 | 0 | m_rng->randomize_with_ts_input(output, output_len); |
104 | 0 | } |
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 | 0 | { |
109 | 0 | m_rng->randomize_with_input(output, output_len, ad, ad_len); |
110 | 0 | } |
111 | | |
112 | | } |