7 #ifndef CRYPTOPP_IMPORTS
11 #ifdef OS_RNG_AVAILABLE
15 #ifdef CRYPTOPP_WIN32_AVAILABLE
17 #define _WIN32_WINNT 0x0400
23 #ifdef CRYPTOPP_UNIX_AVAILABLE
29 NAMESPACE_BEGIN(CryptoPP)
31 #if defined(NONBLOCKING_RNG_AVAILABLE) || defined(BLOCKING_RNG_AVAILABLE)
32 OS_RNG_Err::OS_RNG_Err(
const std::string &operation)
33 :
Exception(OTHER_ERROR,
"OS_Rng: " + operation +
" operation failed with error " +
34 #ifdef CRYPTOPP_WIN32_AVAILABLE
35 "0x" + IntToString(GetLastError(), 16)
44 #ifdef NONBLOCKING_RNG_AVAILABLE
46 #ifdef CRYPTOPP_WIN32_AVAILABLE
48 MicrosoftCryptoProvider::MicrosoftCryptoProvider()
50 if(!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
54 MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
56 CryptReleaseContext(m_hProvider, 0);
61 NonblockingRng::NonblockingRng()
63 #ifndef CRYPTOPP_WIN32_AVAILABLE
64 m_fd = open(
"/dev/urandom",O_RDONLY);
70 NonblockingRng::~NonblockingRng()
72 #ifndef CRYPTOPP_WIN32_AVAILABLE
79 #ifdef CRYPTOPP_WIN32_AVAILABLE
80 # ifdef WORKAROUND_MS_BUG_Q258000
83 if (!CryptGenRandom(m_Provider.GetProviderHandle(), (DWORD)size, output))
88 ssize_t len = read(m_fd, output, size);
93 if (errno != EINTR && errno != EAGAIN)
109 #ifdef BLOCKING_RNG_AVAILABLE
111 #ifndef CRYPTOPP_BLOCKING_RNG_FILENAME
113 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/srandom"
115 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/random"
119 BlockingRng::BlockingRng()
121 m_fd = open(CRYPTOPP_BLOCKING_RNG_FILENAME,O_RDONLY);
123 throw OS_RNG_Err(
"open " CRYPTOPP_BLOCKING_RNG_FILENAME);
126 BlockingRng::~BlockingRng()
137 ssize_t len = read(m_fd, output, size);
141 if (errno != EINTR && errno != EAGAIN)
142 throw OS_RNG_Err(
"read " CRYPTOPP_BLOCKING_RNG_FILENAME);
158 void OS_GenerateRandomBlock(
bool blocking, byte *output,
size_t size)
160 #ifdef NONBLOCKING_RNG_AVAILABLE
164 #ifdef BLOCKING_RNG_AVAILABLE
170 #ifdef BLOCKING_RNG_AVAILABLE
174 #ifdef NONBLOCKING_RNG_AVAILABLE
181 void AutoSeededRandomPool::Reseed(
bool blocking,
unsigned int seedSize)
184 OS_GenerateRandomBlock(blocking, seed, seedSize);
void IncorporateEntropy(const byte *input, size_t length)
update RNG state with additional unpredictable values
base class for all exceptions thrown by Crypto++
encapsulate CryptoAPI's CryptGenRandom or /dev/urandom
a block of memory allocated using A
void GenerateBlock(byte *output, size_t size)
generate random array of bytes
void GenerateBlock(byte *output, size_t size)
generate random array of bytes
Exception class for Operating-System Random Number Generator.
encapsulate /dev/random, or /dev/srandom on OpenBSD