6#ifndef CRYPTOPP_OSRNG_H
7#define CRYPTOPP_OSRNG_H
11#if !defined(NO_OS_DEPENDENCE) && defined(OS_RNG_AVAILABLE)
22NAMESPACE_BEGIN(CryptoPP)
25class CRYPTOPP_DLL OS_RNG_Err :
public Exception
30 OS_RNG_Err(
const std::string &operation);
33#ifdef NONBLOCKING_RNG_AVAILABLE
35#ifdef CRYPTOPP_WIN32_AVAILABLE
38class CRYPTOPP_DLL MicrosoftCryptoProvider
42 MicrosoftCryptoProvider();
43 ~MicrosoftCryptoProvider();
46#if defined(USE_MS_CRYPTOAPI)
47# if defined(__CYGWIN__) && defined(__x86_64__)
48 typedef unsigned long long ProviderHandle;
49# elif defined(WIN64) || defined(_WIN64)
50 typedef unsigned __int64 ProviderHandle;
52 typedef unsigned long ProviderHandle;
54#elif defined(USE_MS_CNGAPI)
56 typedef PVOID ProviderHandle;
66 ProviderHandle GetProviderHandle()
const {
return m_hProvider;}
69 ProviderHandle m_hProvider;
72#if defined(_MSC_VER) && defined(USE_MS_CRYPTOAPI)
73# pragma comment(lib, "advapi32.lib")
76#if defined(_MSC_VER) && defined(USE_MS_CNGAPI)
77# pragma comment(lib, "bcrypt.lib")
88 CRYPTOPP_STATIC_CONSTEXPR
const char* StaticAlgorithmName() {
return "NonblockingRng"; }
99 void GenerateBlock(
byte *output,
size_t size);
102#ifdef CRYPTOPP_WIN32_AVAILABLE
103 MicrosoftCryptoProvider m_Provider;
111#if defined(BLOCKING_RNG_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
122 CRYPTOPP_STATIC_CONSTEXPR
const char* StaticAlgorithmName() {
return "BlockingRng"; }
133 void GenerateBlock(
byte *output,
size_t size);
151CRYPTOPP_DLL
void CRYPTOPP_API OS_GenerateRandomBlock(
bool blocking,
byte *output,
size_t size);
158class CRYPTOPP_DLL AutoSeededRandomPool :
public RandomPool
161 CRYPTOPP_STATIC_CONSTEXPR
const char* StaticAlgorithmName() {
return "AutoSeededRandomPool"; }
163 ~AutoSeededRandomPool() {}
170 explicit AutoSeededRandomPool(
bool blocking =
false,
unsigned int seedSize = 32)
171 {Reseed(blocking, seedSize);}
176 void Reseed(
bool blocking =
false,
unsigned int seedSize = 32);
189template <
class BLOCK_CIPHER>
193 static std::string StaticAlgorithmName() {
194 return std::string(
"AutoSeededX917RNG(") + BLOCK_CIPHER::StaticAlgorithmName() + std::string(
")");
197 ~AutoSeededX917RNG() {}
205 explicit AutoSeededX917RNG(
bool blocking =
false,
bool autoSeed =
true)
206 {
if (autoSeed) Reseed(blocking);}
215 void Reseed(
bool blocking =
false,
const byte *input = NULLPTR,
size_t length = 0);
224 void Reseed(
const byte *key,
size_t keylength,
const byte *seed,
const byte *timeVector);
227 void IncorporateEntropy(
const byte *input,
size_t length) {Reseed(
false, input, length);}
229 {m_rng->GenerateIntoBufferedTransformation(target, channel, length);}
237template <
class BLOCK_CIPHER>
238void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(
const byte *key,
size_t keylength,
const byte *seed,
const byte *timeVector)
240 m_rng.reset(
new X917RNG(
new typename BLOCK_CIPHER::Encryption(key, keylength), seed, timeVector));
243template <
class BLOCK_CIPHER>
244void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(
bool blocking,
const byte *input,
size_t length)
246 enum {BlockSize=BLOCK_CIPHER::BLOCKSIZE};
247 enum {KeyLength=BLOCK_CIPHER::DEFAULT_KEYLENGTH};
251 const byte label[] =
"X9.17 key generation";
252 const byte *key=NULLPTR;
256 OS_GenerateRandomBlock(blocking, temp, temp.size());
266 key = seed + BlockSize;
268 while (memcmp(key, seed,
STDMIN((
size_t)BlockSize, (
size_t)KeyLength)) == 0);
270 Reseed(key, KeyLength, seed, NULLPTR);
273template <
class BLOCK_CIPHER>
274std::string AutoSeededX917RNG<BLOCK_CIPHER>::AlgorithmProvider()
const
277 typename BLOCK_CIPHER::Encryption bc;
278 return bc.AlgorithmProvider();
281CRYPTOPP_DLL_TEMPLATE_CLASS AutoSeededX917RNG<AES>;
283#if defined(CRYPTOPP_DOXYGEN_PROCESSING)
290class DefaultAutoSeededRNG {}
293#if CRYPTOPP_ENABLE_COMPLIANCE_WITH_FIPS_140_2
294typedef AutoSeededX917RNG<AES> DefaultAutoSeededRNG;
296typedef AutoSeededRandomPool DefaultAutoSeededRNG;
Class file for the AES cipher (Rijndael)
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Base class for all exceptions thrown by the library.
Extract-and-Expand Key Derivation Function (HKDF)
size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen, const NameValuePairs ¶ms) const
Derive a key from a seed.
Ensures an object is not copyable.
Interface for random number generators.
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
virtual bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Randomness Pool based on AES-256.
Pointer that overloads operator ->
Library configuration file.
word64 lword
Large word type.
Abstract base classes that provide a uniform interface to this library.
Classes and functions for the FIPS 140-2 validated library.
Classes for HKDF from RFC 5869.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define EnumToInt(v)
Integer value.
Class file for Randomness Pool.
Miscellaneous classes for RNGs.
Classes for SHA-1 and SHA-2 family of message digests.
Classes for automatic resource management.