File: rng.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (300 lines) | stat: -rw-r--r-- 8,688 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*************************************************
* Global RNG Source File                         *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/rng.h>
#include <botan/mutex.h>
#include <botan/lookup.h>
#include <botan/init.h>
#include <botan/util.h>
#include <memory>
#include <vector>

namespace Botan {

namespace {

/*************************************************
* Global RNG/EntropySource state                 *
*************************************************/
class RNG_State
   {
   public:
      void set_rngs(RandomNumberGenerator*, RandomNumberGenerator*);
      void add_es(EntropySource*, bool);
      void add_entropy(const byte[], u32bit);
      u32bit poll_es(EntropySource*, bool);

      u32bit seed(bool, u32bit);

      void randomize(byte[], u32bit, RNG_Quality);

      RNG_State();
      ~RNG_State();
   private:
      void seed_nonce_rng();
      RandomNumberGenerator* global_rng;
      RandomNumberGenerator* nonce_rng;
      Mutex* rng_mutex;
      Mutex* sources_mutex;
      std::vector<EntropySource*> sources;
   };

/*************************************************
* Create the RNG state                           *
*************************************************/
RNG_State::RNG_State()
   {
   global_rng = nonce_rng = 0;
   rng_mutex = get_mutex();
   sources_mutex = get_mutex();
   }

/*************************************************
* Destroy the RNG state                          *
*************************************************/
RNG_State::~RNG_State()
   {
   delete global_rng;
   delete nonce_rng;
   for(u32bit j = 0; j != sources.size(); j++)
      delete sources[j];

   delete rng_mutex;
   delete sources_mutex;
   }

/*************************************************
* Set the RNG algorithms                         *
*************************************************/
void RNG_State::set_rngs(RandomNumberGenerator* rng1,
                         RandomNumberGenerator* rng2)
   {
   if(rng1)
      {
      if(global_rng)
         delete global_rng;
      global_rng = rng1;
      }

   if(rng2)
      {
      if(nonce_rng)
         delete nonce_rng;
      nonce_rng = rng2;
      }
   }

/*************************************************
* Get entropy from the global RNG                *
*************************************************/
void RNG_State::randomize(byte output[], u32bit size, RNG_Quality level)
   {
   const std::string LTERM_CIPHER = "WiderWake4+1";

   Mutex_Holder lock(rng_mutex);

   if(!global_rng || !nonce_rng)
      throw Invalid_State("Global_RNG::randomize: The global RNG is unset");

   if(level == Nonce)
      nonce_rng->randomize(output, size);
   else if(level == SessionKey)
      global_rng->randomize(output, size);
   else if(level == LongTermKey)
      {
      global_rng->randomize(output, size);
      if(have_stream_cipher(LTERM_CIPHER))
         {
         std::auto_ptr<StreamCipher> cipher(get_stream_cipher(LTERM_CIPHER));
         SecureVector<byte> key(cipher->MAXIMUM_KEYLENGTH);
         global_rng->randomize(key.begin(), key.size());
         cipher->set_key(key.begin(), key.size());
         cipher->encrypt(output, size);
         }
      }
   else
      throw Invalid_Argument("Global_RNG::randomize: Invalid RNG_Quality");
   }

/*************************************************
* Add entropy to the RNG                         *
*************************************************/
void RNG_State::add_entropy(const byte buf[], u32bit length)
   {
   Mutex_Holder lock(rng_mutex);

   if(!global_rng || !nonce_rng)
      throw Invalid_State("Global_RNG::add_entropy: The global RNG is unset");

   global_rng->add_entropy(buf, length);
   seed_nonce_rng();
   }

/*************************************************
* Add an EntropySource to the list               *
*************************************************/
void RNG_State::add_es(EntropySource* src, bool last)
   {
   Mutex_Holder lock(sources_mutex);
   if(last)
      sources.push_back(src);
   else
      sources.insert(sources.begin(), src);
   }

/*************************************************
* Seed the nonce RNG                             *
*************************************************/
void RNG_State::seed_nonce_rng()
   {
   if(!global_rng->is_seeded())
      return;

   for(u32bit j = 0; j != 3; j++)
      {
      if(nonce_rng->is_seeded())
         break;

      SecureVector<byte> entropy(64);
      global_rng->randomize(entropy.begin(), entropy.size());
      nonce_rng->add_entropy(entropy.begin(), entropy.size());
      }
   }

/*************************************************
* Try to do a poll on an EntropySource           *
*************************************************/
u32bit RNG_State::poll_es(EntropySource* source, bool slow_poll)
   {
   SecureVector<byte> buffer(256);
   u32bit got = 0;

   if(slow_poll) got = source->slow_poll(buffer.begin(), buffer.size());
   else          got = source->fast_poll(buffer.begin(), buffer.size());

   add_entropy(buffer.begin(), got);
   return entropy_estimate(buffer.begin(), got);
   }

/*************************************************
* Attempt to seed the RNGs                       *
*************************************************/
u32bit RNG_State::seed(bool slow_poll, u32bit bits_to_get)
   {
   Mutex_Holder lock(sources_mutex);

   u32bit bits = 0;
   for(u32bit j = 0; j != sources.size(); j++)
      {
      bits += poll_es(sources[j], slow_poll);
      if(bits_to_get && bits >= bits_to_get)
         return bits;
      }
   return bits;
   }

/*************************************************
* The global RNG state                           *
*************************************************/
RNG_State* rng_state = 0;

}

namespace Global_RNG {

/*************************************************
* Get entropy from the global RNG                *
*************************************************/
void randomize(byte output[], u32bit size, RNG_Quality level)
   {
   if(!rng_state)
      throw Internal_Error("Global_RNG::randomize: RNG state never created");
   rng_state->randomize(output, size, level);
   }

/*************************************************
* Get entropy from the global RNG                *
*************************************************/
byte random(RNG_Quality level)
   {
   byte ret = 0;
   randomize(&ret, 1, level);
   return ret;
   }

/*************************************************
* Add entropy to the global RNG                  *
*************************************************/
void add_entropy(const byte entropy[], u32bit size)
   {
   if(!rng_state)
      throw Internal_Error("Global_RNG::add_entropy: RNG state never created");
   rng_state->add_entropy(entropy, size);
   }

/*************************************************
* Add entropy to the global RNG                  *
*************************************************/
void add_entropy(EntropySource& src, bool slow_poll)
   {
   if(!rng_state)
      throw Internal_Error("Global_RNG::poll_es: RNG state never created");
   rng_state->poll_es(&src, slow_poll);
   }

/*************************************************
* Add an EntropySource to the list               *
*************************************************/
void add_es(EntropySource* src, bool last)
   {
   if(!rng_state)
      throw Internal_Error("Global_RNG::add_es: RNG state never created");
   rng_state->add_es(src, last);
   }

/*************************************************
* Seed the global RNG                            *
*************************************************/
u32bit seed(bool slow_poll, u32bit bits_to_get)
   {
   if(!rng_state)
      throw Internal_Error("Global_RNG::seed: RNG state never created");
   return rng_state->seed(slow_poll, bits_to_get);
   }

}

namespace Init {

/*************************************************
* Initialize the RNG system                      *
*************************************************/
void init_rng_subsystem()
   {
   rng_state = new RNG_State;
   }

/*************************************************
* Deinitialize the RNG system                    *
*************************************************/
void shutdown_rng_subsystem()
   {
   delete rng_state;
   rng_state = 0;
   }

/*************************************************
* Setup the global RNG                           *
*************************************************/
void set_global_rngs(RandomNumberGenerator* rng1, RandomNumberGenerator* rng2)
   {
   if(!rng_state)
      throw Internal_Error("set_global_rngs: RNG state never created");
   rng_state->set_rngs(rng1, rng2);
   }

}

}