Fabcoin Core  0.16.2
P2P Digital Currency
rdrand.h
Go to the documentation of this file.
1 // rdrand.h - written and placed in public domain by Jeffrey Walton and Uri Blumenthal.
2 // Copyright assigned to Crypto++ project.
3 
7 
8 #ifndef CRYPTOPP_RDRAND_H
9 #define CRYPTOPP_RDRAND_H
10 
11 #include "cryptlib.h"
12 
13 // This file (and friends) provides both RDRAND and RDSEED, but its somewhat
14 // experimental. They were added at Crypto++ 5.6.3. At compile time, it
15 // indirectly uses CRYPTOPP_BOOL_{X86|X32|X64} (via CRYPTOPP_CPUID_AVAILABLE)
16 // to select an implementation or "throw NotImplemented". At runtime, the
17 // class uses the result of CPUID to determine if RDRAND or RDSEED are
18 // available. If not available, a lazy throw strategy is used. I.e., the
19 // throw is deferred until GenerateBlock() is called.
20 
21 // Microsoft added RDRAND in August 2012, VS2012. GCC added RDRAND in December 2010, GCC 4.6.
22 // Clang added RDRAND in July 2012, Clang 3.2. Intel added RDRAND in September 2011, ICC 12.1.
23 
25 
26 class RDRAND_Err : public Exception
30 {
31 public:
32  RDRAND_Err(const std::string &operation)
33  : Exception(OTHER_ERROR, "RDRAND: " + operation + " operation failed") {}
34 };
35 
40 {
41 public:
42  std::string AlgorithmName() const {return "RDRAND";}
43 
54  RDRAND(unsigned int retries = 4) : m_retries(retries) {}
55 
56  virtual ~RDRAND() {}
57 
60  unsigned int GetRetries() const
61  {
62  return m_retries;
63  }
64 
67  void SetRetries(unsigned int retries)
68  {
69  m_retries = retries;
70  }
71 
75 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
76  virtual void GenerateBlock(byte *output, size_t size);
77 #else
78  virtual void GenerateBlock(byte *output, size_t size) {
79  CRYPTOPP_UNUSED(output), CRYPTOPP_UNUSED(size);
80  throw NotImplemented("RDRAND: rdrand is not available on this platform");
81  }
82 #endif
83 
89 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
90  virtual void DiscardBytes(size_t n);
91 #else
92  virtual void DiscardBytes(size_t n) {
93  CRYPTOPP_UNUSED(n);
94  throw NotImplemented("RDRAND: rdrand is not available on this platform");
95  }
96 #endif
97 
102  virtual void IncorporateEntropy(const byte *input, size_t length)
103  {
104  // Override to avoid the base class' throw.
105  CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
106  // CRYPTOPP_ASSERT(0); // warn in debug builds
107  }
108 
109 private:
110  unsigned int m_retries;
111 };
112 
116 class RDSEED_Err : public Exception
117 {
118 public:
119  RDSEED_Err(const std::string &operation)
120  : Exception(OTHER_ERROR, "RDSEED: " + operation + " operation failed") {}
121 };
122 
127 {
128 public:
129  std::string AlgorithmName() const {return "RDSEED";}
130 
138  RDSEED(unsigned int retries = 64) : m_retries(retries) {}
139 
140  virtual ~RDSEED() {}
141 
144  unsigned int GetRetries() const
145  {
146  return m_retries;
147  }
148 
151  void SetRetries(unsigned int retries)
152  {
153  m_retries = retries;
154  }
155 
159 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
160  virtual void GenerateBlock(byte *output, size_t size);
161 #else
162  virtual void GenerateBlock(byte *output, size_t size) {
163  CRYPTOPP_UNUSED(output), CRYPTOPP_UNUSED(size);
164  throw NotImplemented("RDSEED: rdseed is not available on this platform");
165  }
166 #endif
167 
173 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
174  virtual void DiscardBytes(size_t n);
175 #else
176  virtual void DiscardBytes(size_t n) {
177  CRYPTOPP_UNUSED(n);
178  throw NotImplemented("RDSEED: rdseed is not available on this platform");
179  }
180 #endif
181 
186  virtual void IncorporateEntropy(const byte *input, size_t length)
187  {
188  // Override to avoid the base class' throw.
189  CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
190  // CRYPTOPP_ASSERT(0); // warn in debug builds
191  }
192 
193 private:
194  unsigned int m_retries;
195 };
196 
198 
199 #endif // CRYPTOPP_RDRAND_H
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: rdrand.h:162
uint8_t byte
Definition: Common.h:57
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: rdrand.h:78
Hardware generated random numbers using RDRAND instruction.
Definition: rdrand.h:39
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: rdrand.h:186
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Abstract base classes that provide a uniform interface to this library.
virtual ~RDSEED()
Definition: rdrand.h:140
RDSEED(unsigned int retries=64)
Construct a RDSEED generator.
Definition: rdrand.h:138
Interface for random number generators.
Definition: cryptlib.h:1188
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: rdrand.h:102
void SetRetries(unsigned int retries)
Set the number of retries used by the generator.
Definition: rdrand.h:151
Exception thrown when a RDRAND generator encounters a generator related error.
Definition: rdrand.h:29
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: rdrand.h:176
A method was called which was not implemented.
Definition: cryptlib.h:205
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: rdrand.h:92
virtual ~RDRAND()
Definition: rdrand.h:56
unsigned int GetRetries() const
Retrieve the number of retries used by the generator.
Definition: rdrand.h:144
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: rdrand.h:42
Hardware generated random numbers using RDSEED instruction.
Definition: rdrand.h:126
unsigned int m_retries
Definition: rdrand.h:194
uint8_t const size_t const size
Definition: sha3.h:20
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
unsigned int m_retries
Definition: rdrand.h:110
#define NAMESPACE_END
Definition: config.h:201
RDRAND(unsigned int retries=4)
Construct a RDRAND generator.
Definition: rdrand.h:54
void SetRetries(unsigned int retries)
Set the number of retries used by the generator.
Definition: rdrand.h:67
RDRAND_Err(const std::string &operation)
Definition: rdrand.h:32
unsigned int GetRetries() const
Retrieve the number of retries used by the generator.
Definition: rdrand.h:60
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: rdrand.h:129
RDSEED_Err(const std::string &operation)
Definition: rdrand.h:119
Exception thrown when a RDSEED generator encounters a generator related error.
Definition: rdrand.h:116