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(NO_OS_DEPENDENCE) && defined(OS_RNG_AVAILABLE) 22 #ifdef CRYPTOPP_WIN32_AVAILABLE 23 #define WIN32_LEAN_AND_MEAN 25 #if defined(USE_MS_CRYPTOAPI) 27 #ifndef CRYPT_NEWKEYSET 28 # define CRYPT_NEWKEYSET 0x00000008 30 #ifndef CRYPT_MACHINE_KEYSET 31 # define CRYPT_MACHINE_KEYSET 0x00000020 33 #elif defined(USE_MS_CNGAPI) 35 #ifndef BCRYPT_SUCCESS 36 # define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) 38 #ifndef STATUS_INVALID_PARAMETER 39 # define STATUS_INVALID_PARAMETER 0xC000000D 41 #ifndef STATUS_INVALID_HANDLE 42 # define STATUS_INVALID_HANDLE 0xC0000008 47 #ifdef CRYPTOPP_UNIX_AVAILABLE 55 #if defined(NONBLOCKING_RNG_AVAILABLE) || defined(BLOCKING_RNG_AVAILABLE) 57 :
Exception(OTHER_ERROR,
"OS_Rng: " + operation +
" operation failed with error " +
58 #ifdef CRYPTOPP_WIN32_AVAILABLE
68 #ifdef NONBLOCKING_RNG_AVAILABLE 70 #ifdef CRYPTOPP_WIN32_AVAILABLE 72 #if defined(USE_MS_CNGAPI) 73 inline DWORD NtStatusToErrorCode(NTSTATUS status)
75 if (status == STATUS_INVALID_PARAMETER)
76 return ERROR_INVALID_PARAMETER;
77 else if (status == STATUS_INVALID_HANDLE)
78 return ERROR_INVALID_HANDLE;
84 #if defined(UNICODE) || defined(_UNICODE) 85 # define CRYPTOPP_CONTAINER L"Crypto++ RNG" 87 # define CRYPTOPP_CONTAINER "Crypto++ RNG" 92 #if defined(USE_MS_CRYPTOAPI) 94 if (!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
96 const DWORD firstErr = GetLastError();
97 if (!CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET ) &&
98 !CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
101 SetLastError(firstErr);
105 #elif defined(USE_MS_CNGAPI) 106 NTSTATUS ret = BCryptOpenAlgorithmProvider(&m_hProvider, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
107 if (!(BCRYPT_SUCCESS(ret)))
110 SetLastError(NtStatusToErrorCode(ret));
111 throw OS_RNG_Err(
"BCryptOpenAlgorithmProvider");
116 MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
118 #if defined(USE_MS_CRYPTOAPI) 120 CryptReleaseContext(m_hProvider, 0);
121 #elif defined(USE_MS_CNGAPI) 123 BCryptCloseAlgorithmProvider(m_hProvider, 0);
127 #endif // CRYPTOPP_WIN32_AVAILABLE 131 #ifndef CRYPTOPP_WIN32_AVAILABLE 132 m_fd = open(
"/dev/urandom",O_RDONLY);
138 NonblockingRng::~NonblockingRng()
140 #ifndef CRYPTOPP_WIN32_AVAILABLE 147 #ifdef CRYPTOPP_WIN32_AVAILABLE 150 # if defined(USE_MS_CRYPTOAPI) 153 # elif defined(USE_MS_CNGAPI) 154 NTSTATUS ret = BCryptGenRandom(hProvider.
GetProviderHandle(), output, (ULONG)size, 0);
155 if (!(BCRYPT_SUCCESS(ret)))
158 SetLastError(NtStatusToErrorCode(ret));
165 ssize_t len = read(m_fd, output, size);
169 if (errno != EINTR && errno != EAGAIN)
178 #endif // CRYPTOPP_WIN32_AVAILABLE 181 #endif // NONBLOCKING_RNG_AVAILABLE 185 #ifdef BLOCKING_RNG_AVAILABLE 187 #ifndef CRYPTOPP_BLOCKING_RNG_FILENAME 189 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/srandom" 191 #define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/random" 197 m_fd = open(CRYPTOPP_BLOCKING_RNG_FILENAME,O_RDONLY);
199 throw OS_RNG_Err(
"open " CRYPTOPP_BLOCKING_RNG_FILENAME);
202 BlockingRng::~BlockingRng()
213 ssize_t len = read(m_fd, output, size);
217 if (errno != EINTR && errno != EAGAIN)
218 throw OS_RNG_Err(
"read " CRYPTOPP_BLOCKING_RNG_FILENAME);
230 #endif // BLOCKING_RNG_AVAILABLE 236 #ifdef NONBLOCKING_RNG_AVAILABLE 240 #ifdef BLOCKING_RNG_AVAILABLE 246 #ifdef BLOCKING_RNG_AVAILABLE 250 #ifdef NONBLOCKING_RNG_AVAILABLE 266 #endif // OS_RNG_AVAILABLE 268 #endif // CRYPTOPP_IMPORTS void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Base class for all exceptions thrown by the library.
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.