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) 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" 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);
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 135 #ifndef CRYPTOPP_WIN32_AVAILABLE 136 m_fd = open(
"/dev/urandom",O_RDONLY);
142 NonblockingRng::~NonblockingRng()
144 #ifndef CRYPTOPP_WIN32_AVAILABLE 151 #ifdef CRYPTOPP_WIN32_AVAILABLE 154 # if defined(USE_MS_CRYPTOAPI) 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));
169 ssize_t len = read(m_fd, output, size);
173 if (errno != EINTR && errno != EAGAIN)
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" 201 m_fd = open(CRYPTOPP_BLOCKING_RNG_FILENAME,O_RDONLY);
203 throw OS_RNG_Err(
"open " CRYPTOPP_BLOCKING_RNG_FILENAME);
206 BlockingRng::~BlockingRng()
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 240 #ifdef NONBLOCKING_RNG_AVAILABLE 244 #ifdef BLOCKING_RNG_AVAILABLE 250 #ifdef BLOCKING_RNG_AVAILABLE 254 #ifdef NONBLOCKING_RNG_AVAILABLE 265 IncorporateEntropy(seed, seedSize);
270 #endif // OS_RNG_AVAILABLE 272 #endif // CRYPTOPP_IMPORTS Base class for all exceptions thrown by the library.
Wrapper for Microsoft crypto service provider.
BlockingRng()
Construct a BlockingRng.
OS_RNG_Err(const std::string &operation)
Constructs an OS_RNG_Err.
Restricts the instantiation of a class to one static object without locks.
Wrapper class for /dev/random and /dev/srandom.
void Reseed(bool blocking=false, unsigned int seedSize=32)
Reseed an AutoSeededRandomPool.
Library configuration file.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
NonblockingRng()
Construct a NonblockingRng.
Exception thrown when an operating system error is encountered.
Miscellaneous classes for RNGs.
MicrosoftCryptoProvider()
Construct a MicrosoftCryptoProvider.
Wrapper class for /dev/random and /dev/srandom.
void OS_GenerateRandomBlock(bool blocking, byte *output, size_t size)
OS_GenerateRandomBlock.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Crypto++ library namespace.
ProviderHandle GetProviderHandle() const
Retrieves the provider handle.
Classes for access to the operating system's random number generators.