2002-10-04 17:31:41 +00:00
|
|
|
#ifndef CRYPTOPP_OSRNG_H
|
|
|
|
#define CRYPTOPP_OSRNG_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef OS_RNG_AVAILABLE
|
|
|
|
|
|
|
|
#include "randpool.h"
|
|
|
|
#include "rng.h"
|
2003-07-04 00:17:37 +00:00
|
|
|
#include "des.h"
|
2004-04-08 01:32:07 +00:00
|
|
|
#include "fips140.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
//! Exception class for Operating-System Random Number Generator.
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL OS_RNG_Err : public Exception
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
OS_RNG_Err(const std::string &operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL MicrosoftCryptoProvider
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MicrosoftCryptoProvider();
|
|
|
|
~MicrosoftCryptoProvider();
|
|
|
|
#if defined(_WIN64)
|
|
|
|
typedef unsigned __int64 ProviderHandle; // type HCRYPTPROV, avoid #include <windows.h>
|
|
|
|
#else
|
|
|
|
typedef unsigned long ProviderHandle;
|
|
|
|
#endif
|
|
|
|
ProviderHandle GetProviderHandle() const {return m_hProvider;}
|
|
|
|
private:
|
|
|
|
ProviderHandle m_hProvider;
|
|
|
|
};
|
2004-04-08 01:32:07 +00:00
|
|
|
|
|
|
|
#pragma comment(lib, "advapi32.lib")
|
2002-10-04 17:31:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//! encapsulate CryptoAPI's CryptGenRandom or /dev/urandom
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL NonblockingRng : public RandomNumberGenerator
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NonblockingRng();
|
|
|
|
~NonblockingRng();
|
|
|
|
byte GenerateByte();
|
2005-07-12 04:23:32 +00:00
|
|
|
void GenerateBlock(byte *output, size_t size);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
|
|
|
# ifndef WORKAROUND_MS_BUG_Q258000
|
|
|
|
MicrosoftCryptoProvider m_Provider;
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
int m_fd;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BLOCKING_RNG_AVAILABLE
|
|
|
|
|
|
|
|
//! encapsulate /dev/random
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL BlockingRng : public RandomNumberGenerator
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockingRng();
|
|
|
|
~BlockingRng();
|
|
|
|
byte GenerateByte();
|
2005-07-12 04:23:32 +00:00
|
|
|
void GenerateBlock(byte *output, size_t size);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-07-12 04:23:32 +00:00
|
|
|
CRYPTOPP_DLL void CRYPTOPP_API OS_GenerateRandomBlock(bool blocking, byte *output, size_t size);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
//! Automaticly Seeded Randomness Pool
|
|
|
|
/*! This class seeds itself using an operating system provided RNG. */
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL AutoSeededRandomPool : public RandomPool
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! blocking will be ignored if the prefered RNG isn't available
|
|
|
|
explicit AutoSeededRandomPool(bool blocking = false, unsigned int seedSize = 32)
|
|
|
|
{Reseed(blocking, seedSize);}
|
|
|
|
void Reseed(bool blocking = false, unsigned int seedSize = 32);
|
|
|
|
};
|
|
|
|
|
|
|
|
//! RNG from ANSI X9.17 Appendix C, seeded using an OS provided RNG
|
|
|
|
template <class BLOCK_CIPHER>
|
2003-07-04 00:17:37 +00:00
|
|
|
class AutoSeededX917RNG : public RandomNumberGenerator, public NotCopyable
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! blocking will be ignored if the prefered RNG isn't available
|
|
|
|
explicit AutoSeededX917RNG(bool blocking = false)
|
|
|
|
{Reseed(blocking);}
|
|
|
|
void Reseed(bool blocking = false);
|
2002-10-17 16:32:28 +00:00
|
|
|
// exposed for testing
|
2005-07-12 04:23:32 +00:00
|
|
|
void Reseed(const byte *key, size_t keylength, const byte *seed, const byte *timeVector);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
byte GenerateByte();
|
|
|
|
|
|
|
|
private:
|
|
|
|
member_ptr<RandomNumberGenerator> m_rng;
|
|
|
|
SecByteBlock m_lastBlock;
|
|
|
|
bool m_isDifferent;
|
|
|
|
unsigned int m_counter;
|
|
|
|
};
|
|
|
|
|
2002-10-17 16:32:28 +00:00
|
|
|
template <class BLOCK_CIPHER>
|
2005-07-12 04:23:32 +00:00
|
|
|
void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(const byte *key, size_t keylength, const byte *seed, const byte *timeVector)
|
2002-10-17 16:32:28 +00:00
|
|
|
{
|
|
|
|
m_rng.reset(new X917RNG(new typename BLOCK_CIPHER::Encryption(key, keylength), seed, timeVector));
|
|
|
|
|
2003-03-20 01:24:12 +00:00
|
|
|
// for FIPS 140-2
|
|
|
|
m_lastBlock.resize(16);
|
|
|
|
m_rng->GenerateBlock(m_lastBlock, m_lastBlock.size());
|
|
|
|
m_counter = 0;
|
|
|
|
m_isDifferent = false;
|
2002-10-17 16:32:28 +00:00
|
|
|
}
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
template <class BLOCK_CIPHER>
|
|
|
|
void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(bool blocking)
|
|
|
|
{
|
|
|
|
SecByteBlock seed(BLOCK_CIPHER::BLOCKSIZE + BLOCK_CIPHER::DEFAULT_KEYLENGTH);
|
|
|
|
const byte *key;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
OS_GenerateRandomBlock(blocking, seed, seed.size());
|
|
|
|
key = seed + BLOCK_CIPHER::BLOCKSIZE;
|
|
|
|
} // check that seed and key don't have same value
|
|
|
|
while (memcmp(key, seed, STDMIN((unsigned int)BLOCK_CIPHER::BLOCKSIZE, (unsigned int)BLOCK_CIPHER::DEFAULT_KEYLENGTH)) == 0);
|
|
|
|
|
2005-01-20 04:19:35 +00:00
|
|
|
Reseed(key, BLOCK_CIPHER::DEFAULT_KEYLENGTH, seed, NULL);
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BLOCK_CIPHER>
|
|
|
|
byte AutoSeededX917RNG<BLOCK_CIPHER>::GenerateByte()
|
|
|
|
{
|
|
|
|
byte b = m_rng->GenerateByte();
|
|
|
|
|
2003-03-20 01:24:12 +00:00
|
|
|
// for FIPS 140-2
|
|
|
|
m_isDifferent = m_isDifferent || b != m_lastBlock[m_counter];
|
|
|
|
m_lastBlock[m_counter] = b;
|
|
|
|
++m_counter;
|
|
|
|
if (m_counter == m_lastBlock.size())
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2003-03-20 01:24:12 +00:00
|
|
|
if (!m_isDifferent)
|
|
|
|
throw SelfTestFailure("AutoSeededX917RNG: Continuous random number generator test failed.");
|
|
|
|
m_counter = 0;
|
|
|
|
m_isDifferent = false;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2005-09-05 21:43:43 +00:00
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS AutoSeededX917RNG<DES_EDE3>;
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|