Botan 2.19.5 Crypto and TLS for C&

rdrand_rng.cpp
Go to the documentation of this file.
1/*
2* RDRAND RNG
3* (C) 2016,2019 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/rdrand_rng.h>
9#include <botan/processor_rng.h>
10#include <botan/loadstor.h>
11
12 namespace Botan {
13
14 void RDRAND_RNG::randomize(uint8_t out[], size_t out_len)
15 {
16 Processor_RNG rng;
17 rng.randomize(out, out_len);
18 }
19
20 RDRAND_RNG::RDRAND_RNG()
21 {
22 // Will throw if instruction is not available
23 Processor_RNG rng;
24 }
25
26//static
27 bool RDRAND_RNG::available()
28 {
29 return Processor_RNG::available();
30 }
31
32//static
33uint32_t RDRAND_RNG::rdrand()
34 {
35 Processor_RNG rng;
36
37 for(;;)
38 {
39 try
40 {
41 uint8_t out[4];
42 rng.randomize(out, 4);
43 return load_le<uint32_t>(out, 0);
44 }
45 catch(PRNG_Unseeded&) {}
46 }
47 }
48
49//static
50uint32_t RDRAND_RNG::rdrand_status(bool& ok)
51 {
52 ok = false;
53 Processor_RNG rng;
54
55 try
56 {
57 uint8_t out[4];
58 rng.randomize(out, 4);
59 ok = true;
60 return load_le<uint32_t>(out, 0);
61 }
62 catch(PRNG_Unseeded&) {}
63
64 return 0;
65 }
66
67}
Botan::PRNG_Unseeded
Definition: exceptn.h:227
Botan::Processor_RNG
Definition: processor_rng.h:20
Botan::Processor_RNG::available
static bool available()
Definition: processor_rng.cpp:100
Botan::Processor_RNG::randomize
void randomize(uint8_t out[], size_t out_len) override
Definition: processor_rng.cpp:122
Botan::RDRAND_RNG::rdrand_status
static uint32_t rdrand_status(bool &ok)
Definition: rdrand_rng.cpp:50
Botan::RDRAND_RNG::RDRAND_RNG
RDRAND_RNG()
Definition: rdrand_rng.cpp:20
Botan::RDRAND_RNG::rdrand
static uint32_t rdrand()
Definition: rdrand_rng.cpp:33
Botan::RDRAND_RNG::randomize
void randomize(uint8_t out[], size_t out_len) override
Definition: rdrand_rng.cpp:14
Botan::RDRAND_RNG::available
static bool available()
Definition: rdrand_rng.cpp:27
Botan
Definition: alg_id.cpp:13
Botan::load_le< uint32_t >
uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:198