aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/rng/rng.cpp
blob: 923b417dcf52f43351ae38928cac4fbffb4e07a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Random Number Generator
* (C) 1999-2008,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/entropy_src.h>
#include <botan/loadstor.h>
#include <botan/internal/os_utils.h>

#if defined(BOTAN_HAS_HMAC_DRBG)
  #include <botan/hmac_drbg.h>
#endif

#if defined(BOTAN_HAS_HMAC_RNG)
  #include <botan/hmac_rng.h>
#endif

namespace Botan {

size_t RandomNumberGenerator::reseed(size_t bits_to_collect)
   {
   return this->reseed_with_timeout(bits_to_collect,
                                    BOTAN_RNG_RESEED_DEFAULT_TIMEOUT);
   }

size_t RandomNumberGenerator::reseed_with_timeout(size_t bits_to_collect,
                                                  std::chrono::milliseconds timeout)
   {
   return this->reseed_with_sources(Entropy_Sources::global_sources(),
                                    bits_to_collect,
                                    timeout);
   }

size_t RandomNumberGenerator::reseed_with_sources(Entropy_Sources& srcs,
                                                  size_t poll_bits,
                                                  std::chrono::milliseconds poll_timeout)
   {
   return srcs.poll(*this, poll_bits, poll_timeout);
   }

Stateful_RNG::Stateful_RNG(size_t bytes_before_reseed) : m_bytes_before_reseed(bytes_before_reseed)
   {
   }

void Stateful_RNG::clear()
   {
   m_successful_initialization = false;
   m_bytes_since_reseed = 0;
   m_last_pid = 0;
   }

size_t Stateful_RNG::reseed_with_sources(Entropy_Sources& srcs,
                                         size_t poll_bits,
                                         std::chrono::milliseconds poll_timeout)
   {
   size_t bits_collected = RandomNumberGenerator::reseed_with_sources(srcs, poll_bits, poll_timeout);

   if(bits_collected >= poll_bits)
      {
      m_successful_initialization = true;
      m_bytes_since_reseed = 0;
      }

   return bits_collected;
   }

void Stateful_RNG::reseed_check(size_t bytes_requested)
   {
   const bool fork_detected = (m_last_pid > 0) && (OS::get_process_id() != m_last_pid);

   m_bytes_since_reseed += bytes_requested;
   m_last_pid = OS::get_process_id();

   if(!is_seeded() || fork_detected)
      {
      this->reseed(BOTAN_RNG_RESEED_POLL_BITS);
      }
   else if(m_bytes_before_reseed > 0 && m_bytes_since_reseed >= m_bytes_before_reseed)
      {
      this->reseed_with_timeout(BOTAN_RNG_RESEED_POLL_BITS,
                                BOTAN_RNG_AUTO_RESEED_TIMEOUT);
      }

   if(!is_seeded())
      {
      throw PRNG_Unseeded(name());
      }
   }

void Stateful_RNG::initialize_with(const byte input[], size_t len)
   {
   add_entropy(input, len);
   m_successful_initialization = true;
   }

bool Stateful_RNG::is_seeded() const
   {
   return m_successful_initialization;
   }

RandomNumberGenerator* RandomNumberGenerator::make_rng()
   {
   return new AutoSeeded_RNG;
   }

AutoSeeded_RNG::AutoSeeded_RNG(size_t max_bytes_before_reseed)
   {
   m_rng.reset(new BOTAN_AUTO_RNG_DRBG(BOTAN_AUTO_RNG_HASH, max_bytes_before_reseed));

   size_t bits = m_rng->reseed(BOTAN_AUTO_RNG_ENTROPY_TARGET);

   if(!m_rng->is_seeded())
      {
      throw Exception("AutoSeeded_RNG failed to gather enough entropy only got " +
                      std::to_string(bits) + " bits");
      }
   }

void AutoSeeded_RNG::randomize(byte output[], size_t output_len)
   {
   /*
   Form additional input which is provided to the PRNG implementation
   to paramaterize the KDF output.
   */
   byte additional_input[24] = { 0 };
   store_le(OS::get_system_timestamp_ns(), additional_input);
   store_le(OS::get_processor_timestamp(), additional_input + 8);
   store_le(OS::get_process_id(), additional_input + 16);
   store_le(m_counter++, additional_input + 20);

   randomize_with_input(output, output_len, additional_input, sizeof(additional_input));
   }

void AutoSeeded_RNG::randomize_with_input(byte output[], size_t output_len,
                                          const byte ad[], size_t ad_len)
   {
   m_rng->randomize_with_input(output, output_len, ad, ad_len);
   }

}