Botan  2.13.0
Crypto and TLS for C++11
rdrand.cpp
Go to the documentation of this file.
1 /*
2 * Entropy Source Using Intel's rdrand instruction
3 * (C) 2012,2015,2019 Jack Lloyd
4 * (C) 2015 Daniel Neus
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/internal/rdrand.h>
10 #include <botan/rdrand_rng.h>
11 
12 namespace Botan {
13 
15  {
16  /*
17  * Intel's documentation for RDRAND at
18  * https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
19  * claims that software can guarantee a reseed event by polling enough data:
20  * "There is an upper bound of 511 samples per seed in the implementation
21  * where samples are 128 bits in size and can provide two 64-bit random
22  * numbers each."
23  *
24  * By requesting 8192 bytes we are asking for 512 samples and thus are assured
25  * that at some point in producing the output, at least one reseed of the
26  * internal state will occur.
27  *
28  * The alternative approach is to "Iteratively execute 32 RDRAND invocations
29  * with a 10 us wait period per iteration." however in practice this proves to
30  * be about 20x slower, despite producing much less seed material.
31  */
32  const size_t RDRAND_POLL_BYTES = 8*1024;
33 
35  {
36  RDRAND_RNG rdrand_rng;
37  secure_vector<uint8_t> buf(RDRAND_POLL_BYTES);
38  rdrand_rng.randomize(&buf[0], buf.size());
39  rng.add_entropy(buf.data(), buf.size());
40  }
41 
42  // RDRAND is used but not trusted
43  return 0;
44  }
45 
46 }
virtual void add_entropy(const uint8_t input[], size_t length)=0
static bool available()
Definition: rdrand_rng.cpp:85
void randomize(uint8_t out[], size_t out_len) override
Definition: rdrand_rng.cpp:60
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Definition: alg_id.cpp:13
size_t poll(RandomNumberGenerator &rng) override
Definition: rdrand.cpp:14