summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/rng/rdrand_rng/rdrand_rng.cpp
blob: fade5a19927604a37b04455267f909e398dcf0b8 (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
/*
* RDRAND RNG
* (C) 2016,2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/rdrand_rng.h>
#include <botan/processor_rng.h>
#include <botan/loadstor.h>

namespace Botan {

void RDRAND_RNG::randomize(uint8_t out[], size_t out_len)
   {
   Processor_RNG rng;
   rng.randomize(out, out_len);
   }

RDRAND_RNG::RDRAND_RNG()
   {
   // Will throw if instruction is not available
   Processor_RNG rng;
   }

//static
bool RDRAND_RNG::available()
   {
   return Processor_RNG::available();
   }

//static
uint32_t RDRAND_RNG::rdrand()
   {
   Processor_RNG rng;

   for(;;)
      {
      try
         {
         uint8_t out[4];
         rng.randomize(out, 4);
         return load_le<uint32_t>(out, 0);
         }
      catch(PRNG_Unseeded&) {}
      }
   }

//static
uint32_t RDRAND_RNG::rdrand_status(bool& ok)
   {
   ok = false;
   Processor_RNG rng;

   try
      {
      uint8_t out[4];
      rng.randomize(out, 4);
      ok = true;
      return load_le<uint32_t>(out, 0);
      }
   catch(PRNG_Unseeded&) {}

   return 0;
   }

}