8 #ifndef CRYPTOPP_IMPORTS 13 #if defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(OS_RNG_AVAILABLE) 14 # pragma message("WARNING: Compiling for Windows but an OS RNG is not available. This is likely a Windows Phone 8 or Windows Store 8 app.") 17 #if !defined(OS_NO_DEPENDENCE) && defined(OS_RNG_AVAILABLE) 22 #ifdef CRYPTOPP_WIN32_AVAILABLE 26 #define WIN32_LEAN_AND_MEAN 28 #if defined(USE_MS_CRYPTOAPI) 30 #ifndef CRYPT_NEWKEYSET 31 # define CRYPT_NEWKEYSET 0x00000008 33 #ifndef CRYPT_MACHINE_KEYSET 34 # define CRYPT_MACHINE_KEYSET 0x00000020 36 #elif defined(USE_MS_CNGAPI) 39 #ifndef BCRYPT_SUCCESS 40 # define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) 42 #ifndef STATUS_INVALID_PARAMETER 43 # define STATUS_INVALID_PARAMETER 0xC000000D 45 #ifndef STATUS_INVALID_HANDLE 46 # define STATUS_INVALID_HANDLE 0xC0000008 51 #ifdef CRYPTOPP_UNIX_AVAILABLE 59 #if defined(NONBLOCKING_RNG_AVAILABLE) || defined(BLOCKING_RNG_AVAILABLE) 60 OS_RNG_Err::OS_RNG_Err(
const std::string &operation)
61 :
Exception(OTHER_ERROR,
"OS_Rng: " + operation +
" operation failed with error " +
62 #ifdef CRYPTOPP_WIN32_AVAILABLE
72 #ifdef NONBLOCKING_RNG_AVAILABLE 74 #ifdef CRYPTOPP_WIN32_AVAILABLE 76 #if defined(USE_MS_CNGAPI) 77 inline DWORD NtStatusToErrorCode(NTSTATUS status)
79 if (status == STATUS_INVALID_PARAMETER)
80 return ERROR_INVALID_PARAMETER;
81 else if (status == STATUS_INVALID_HANDLE)
82 return ERROR_INVALID_HANDLE;
88 #if defined(UNICODE) || defined(_UNICODE) 89 # define CRYPTOPP_CONTAINER L"Crypto++ RNG" 91 # define CRYPTOPP_CONTAINER "Crypto++ RNG" 94 MicrosoftCryptoProvider::MicrosoftCryptoProvider() : m_hProvider(0)
96 #if defined(USE_MS_CRYPTOAPI) 98 if (!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
100 const DWORD firstErr = GetLastError();
101 if (!CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET ) &&
102 !CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
105 SetLastError(firstErr);
106 throw OS_RNG_Err(
"CryptAcquireContext");
109 #elif defined(USE_MS_CNGAPI) 110 NTSTATUS ret = BCryptOpenAlgorithmProvider(&m_hProvider, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
111 if (!(BCRYPT_SUCCESS(ret)))
114 SetLastError(NtStatusToErrorCode(ret));
115 throw OS_RNG_Err(
"BCryptOpenAlgorithmProvider");
120 MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
122 #if defined(USE_MS_CRYPTOAPI) 124 CryptReleaseContext(m_hProvider, 0);
125 #elif defined(USE_MS_CNGAPI) 127 BCryptCloseAlgorithmProvider(m_hProvider, 0);
131 #endif // CRYPTOPP_WIN32_AVAILABLE 133 NonblockingRng::NonblockingRng()
135 #ifndef CRYPTOPP_WIN32_AVAILABLE 136 m_fd = open(
"/dev/urandom",O_RDONLY);
138 throw OS_RNG_Err(
"open /dev/urandom");
142 NonblockingRng::~NonblockingRng()
144 #ifndef CRYPTOPP_WIN32_AVAILABLE 149 void NonblockingRng::GenerateBlock(
byte *output,
size_t size)
151 #ifdef CRYPTOPP_WIN32_AVAILABLE 154 # if defined(USE_MS_CRYPTOAPI) 155 if (!CryptGenRandom(hProvider.GetProviderHandle(), (DWORD)size, output))
156 throw OS_RNG_Err(
"CryptGenRandom");
157 # elif defined(USE_MS_CNGAPI) 158 NTSTATUS ret = BCryptGenRandom(hProvider.GetProviderHandle(), output, (ULONG)size, 0);
159 if (!(BCRYPT_SUCCESS(ret)))
162 SetLastError(NtStatusToErrorCode(ret));
163 throw OS_RNG_Err(
"BCryptGenRandom");
169 ssize_t len =
read(m_fd, output, size);
173 if (errno != EINTR && errno != EAGAIN)
174 throw OS_RNG_Err(
"read /dev/urandom");
182 #endif // CRYPTOPP_WIN32_AVAILABLE 185 #endif // NONBLOCKING_RNG_AVAILABLE 189 #ifdef BLOCKING_RNG_AVAILABLE 191 #ifndef CRYPTOPP_BLOCKING_RNG_FILENAME 193 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/srandom" 195 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/random" 199 BlockingRng::BlockingRng()
201 m_fd = open(CRYPTOPP_BLOCKING_RNG_FILENAME,O_RDONLY);
203 throw OS_RNG_Err(
"open " CRYPTOPP_BLOCKING_RNG_FILENAME);
206 BlockingRng::~BlockingRng()
211 void BlockingRng::GenerateBlock(
byte *output,
size_t size)
217 ssize_t len =
read(m_fd, output, size);
221 if (errno != EINTR && errno != EAGAIN)
222 throw OS_RNG_Err(
"read " CRYPTOPP_BLOCKING_RNG_FILENAME);
234 #endif // BLOCKING_RNG_AVAILABLE 238 void OS_GenerateRandomBlock(
bool blocking,
byte *output,
size_t size)
240 #ifdef NONBLOCKING_RNG_AVAILABLE 244 #ifdef BLOCKING_RNG_AVAILABLE 246 rng.GenerateBlock(output, size);
250 #ifdef BLOCKING_RNG_AVAILABLE 254 #ifdef NONBLOCKING_RNG_AVAILABLE 256 rng.GenerateBlock(output, size);
261 void AutoSeededRandomPool::Reseed(
bool blocking,
unsigned int seedSize)
264 OS_GenerateRandomBlock(blocking, seed, seedSize);
265 IncorporateEntropy(seed, seedSize);
270 #endif // OS_RNG_AVAILABLE 272 #endif // CRYPTOPP_IMPORTS Base class for all exceptions thrown by the library.
Restricts the instantiation of a class to one static object without locks.
#define NAMESPACE_BEGIN(x)
Library configuration file.
bool read(const std::string &s, Value &value)
Miscellaneous classes for RNGs.
uint8_t const size_t const size
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Classes for access to the operating system's random number generators.