Fabcoin Core  0.16.2
P2P Digital Currency
osrng.h
Go to the documentation of this file.
1 // osrng.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_OSRNG_H
7 #define CRYPTOPP_OSRNG_H
8 
9 #include "config.h"
10 
11 #if !defined(OS_NO_DEPENDENCE) && defined(OS_RNG_AVAILABLE)
12 
13 #include "cryptlib.h"
14 #include "randpool.h"
15 #include "smartptr.h"
16 #include "fips140.h"
17 #include "rng.h"
18 #include "aes.h"
19 #include "sha.h"
20 
22 
23 class CRYPTOPP_DLL OS_RNG_Err : public Exception
26 {
27 public:
30  OS_RNG_Err(const std::string &operation);
31 };
32 
33 #ifdef NONBLOCKING_RNG_AVAILABLE
34 
35 #ifdef CRYPTOPP_WIN32_AVAILABLE
36 class CRYPTOPP_DLL MicrosoftCryptoProvider
40 {
41 public:
43  MicrosoftCryptoProvider();
44  ~MicrosoftCryptoProvider();
45 
46 // type HCRYPTPROV and BCRYPT_ALG_HANDLE, avoid #include <windows.h>
47 #if defined(USE_MS_CRYPTOAPI)
48 # if defined(__CYGWIN__) && defined(__x86_64__)
49  typedef unsigned long long ProviderHandle;
50 # elif defined(WIN64) || defined(_WIN64)
51  typedef unsigned __int64 ProviderHandle;
52 # else
53  typedef unsigned long ProviderHandle;
54 # endif
55 #elif defined(USE_MS_CNGAPI)
56  typedef void *PVOID;
57  typedef PVOID ProviderHandle;
58 #endif // USE_MS_CRYPTOAPI or USE_MS_CNGAPI
59 
67  ProviderHandle GetProviderHandle() const {return m_hProvider;}
68 
69 private:
70  ProviderHandle m_hProvider;
71 };
72 
73 #if defined(_MSC_VER) && defined(USE_MS_CRYPTOAPI)
74 # pragma comment(lib, "advapi32.lib")
75 #endif
76 
77 #if defined(_MSC_VER) && defined(USE_MS_CNGAPI)
78 # pragma comment(lib, "bcrypt.lib")
79 #endif
80 
81 #endif //CRYPTOPP_WIN32_AVAILABLE
82 
87 class CRYPTOPP_DLL NonblockingRng : public RandomNumberGenerator
88 {
89 public:
91  NonblockingRng();
92  ~NonblockingRng();
93 
98  void GenerateBlock(byte *output, size_t size);
99 
100 protected:
101 #ifdef CRYPTOPP_WIN32_AVAILABLE
102  MicrosoftCryptoProvider m_Provider;
103 #else
104  int m_fd;
105 #endif
106 };
107 
108 #endif
109 
110 #if defined(BLOCKING_RNG_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
111 
115 class CRYPTOPP_DLL BlockingRng : public RandomNumberGenerator
116 {
117 public:
119  BlockingRng();
120  ~BlockingRng();
121 
126  void GenerateBlock(byte *output, size_t size);
127 
128 protected:
129  int m_fd;
130 };
131 
132 #endif
133 
144 CRYPTOPP_DLL void CRYPTOPP_API OS_GenerateRandomBlock(bool blocking, byte *output, size_t size);
145 
146 
151 class CRYPTOPP_DLL AutoSeededRandomPool : public RandomPool
152 {
153 public:
159  explicit AutoSeededRandomPool(bool blocking = false, unsigned int seedSize = 32)
160  {Reseed(blocking, seedSize);}
161 
165  void Reseed(bool blocking = false, unsigned int seedSize = 32);
166 };
167 
177 template <class BLOCK_CIPHER>
178 class AutoSeededX917RNG : public RandomNumberGenerator, public NotCopyable
179 {
180 public:
187  explicit AutoSeededX917RNG(bool blocking = false, bool autoSeed = true)
188  {if (autoSeed) Reseed(blocking);}
189 
197  void Reseed(bool blocking = false, const byte *additionalEntropy = NULL, size_t length = 0);
198 
206  void Reseed(const byte *key, size_t keylength, const byte *seed, const byte *timeVector);
207 
208  bool CanIncorporateEntropy() const {return true;}
209  void IncorporateEntropy(const byte *input, size_t length) {Reseed(false, input, length);}
210  void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
211  {m_rng->GenerateIntoBufferedTransformation(target, channel, length);}
212 
213 private:
215 };
216 
217 template <class BLOCK_CIPHER>
218 void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(const byte *key, size_t keylength, const byte *seed, const byte *timeVector)
219 {
220  m_rng.reset(new X917RNG(new typename BLOCK_CIPHER::Encryption(key, keylength), seed, timeVector));
221 }
222 
223 template <class BLOCK_CIPHER>
224 void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(bool blocking, const byte *input, size_t length)
225 {
226  SecByteBlock seed(BLOCK_CIPHER::BLOCKSIZE + BLOCK_CIPHER::DEFAULT_KEYLENGTH);
227  const byte *key;
228  do
229  {
230  OS_GenerateRandomBlock(blocking, seed, seed.size());
231  if (length > 0)
232  {
233  SHA256 hash;
234  hash.Update(seed, seed.size());
235  hash.Update(input, length);
236  hash.TruncatedFinal(seed, UnsignedMin(hash.DigestSize(), seed.size()));
237  }
238  key = seed + BLOCK_CIPHER::BLOCKSIZE;
239  } // check that seed and key don't have same value
240  while (memcmp(key, seed, STDMIN((unsigned int)BLOCK_CIPHER::BLOCKSIZE, (unsigned int)BLOCK_CIPHER::DEFAULT_KEYLENGTH)) == 0);
241 
242  Reseed(key, BLOCK_CIPHER::DEFAULT_KEYLENGTH, seed, NULL);
243 }
244 
245 CRYPTOPP_DLL_TEMPLATE_CLASS AutoSeededX917RNG<AES>;
246 
247 #if defined(CRYPTOPP_DOXYGEN_PROCESSING)
248 class DefaultAutoSeededRNG {}
254 #else
255 // AutoSeededX917RNG<AES> in FIPS mode, otherwise it's AutoSeededRandomPool
256 #if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
257 typedef AutoSeededX917RNG<AES> DefaultAutoSeededRNG;
258 #else
259 typedef AutoSeededRandomPool DefaultAutoSeededRNG;
260 #endif
261 #endif // CRYPTOPP_DOXYGEN_PROCESSING
262 
264 
265 #endif
266 
267 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
Randomness Pool based on AES-256.
Definition: randpool.h:49
uint8_t byte
Definition: Common.h:57
SHA-256 message digest.
Definition: sha.h:39
Class file for Randomness Pool.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:326
ANSI X9.17 RNG.
Definition: rng.h:48
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
Abstract base classes that provide a uniform interface to this library.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: iterhash.h:161
Classes for automatic resource management.
Library configuration file.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: iterhash.cpp:12
Interface for random number generators.
Definition: cryptlib.h:1188
Interface for buffered transformations.
Definition: cryptlib.h:1352
Pointer that overloads operator ->
Definition: smartptr.h:39
#define CRYPTOPP_API
Definition: config.h:705
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:512
Miscellaneous classes for RNGs.
void TruncatedFinal(byte *digest, size_t digestSize)
Computes the hash of the current message.
Definition: iterhash.cpp:131
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:477
Classes for SHA-1 and SHA-2 family of message digests.
Classes and functions for the FIPS 140-2 validated library.
uint8_t const size_t const size
Definition: sha3.h:20
#define NAMESPACE_END
Definition: config.h:201
word64 lword
Definition: config.h:245
#define CRYPTOPP_DLL
Definition: config.h:704
Ensures an object is not copyable.
Definition: misc.h:217
void reset(T *p=0)
Definition: smartptr.h:72