Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
14 size_t Intel_Rdrand::poll(RandomNumberGenerator& rng)
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
34 if(RDRAND_RNG::available())
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 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RDRAND_RNG::available
static bool available()
Definition: rdrand_rng.cpp:85
Botan::RDRAND_RNG::randomize
void randomize(uint8_t out[], size_t out_len) override
Definition: rdrand_rng.cpp:60
Botan::RDRAND_RNG
Definition: rdrand_rng.h:15
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan
Definition: alg_id.cpp:13
Botan::Intel_Rdrand::poll
size_t poll(RandomNumberGenerator &rng) override
Definition: rdrand.cpp:14