2017-01-27 12:05:45 +00:00
|
|
|
// osrng.cpp - originally written and placed in the public domain by Wei Dai
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
// Thanks to Leonard Janke for the suggestion for AutoSeededRandomPool.
|
|
|
|
|
|
|
|
#include "pch.h"
|
2016-05-03 04:23:05 +00:00
|
|
|
#include "config.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
// Win32 has CryptoAPI and <wincrypt.h>. Windows 10 and Windows Store 10 have CNG and <bcrypt.h>.
|
2019-01-05 17:48:39 +00:00
|
|
|
// There's a hole for Windows Phone 8 and Windows Store 8. There is no userland RNG available.
|
|
|
|
// Also see http://www.drdobbs.com/windows/using-c-and-com-with-winrt/240168150 and
|
|
|
|
// http://stackoverflow.com/questions/36974545/random-numbers-for-windows-phone-8-and-windows-store-8 and
|
|
|
|
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/25b83e13-c85f-4aa1-a057-88a279ea3fd6/what-crypto-random-generator-c-code-could-use-on-wp81
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(OS_RNG_AVAILABLE)
|
|
|
|
# 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.")
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-02-18 22:38:40 +00:00
|
|
|
#if !defined(NO_OS_DEPENDENCE) && defined(OS_RNG_AVAILABLE)
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#include "osrng.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "rng.h"
|
|
|
|
|
2021-12-29 09:45:53 +00:00
|
|
|
// FreeBSD links /dev/urandom -> /dev/random. It showed up when we added
|
|
|
|
// O_NOFOLLOW to harden the non-blocking generator. Use Arc4Random instead
|
|
|
|
// for a non-blocking generator. Arc4Random is cryptograhic quality prng
|
|
|
|
// based on ChaCha20. The ChaCha20 generator is seeded from /dev/random,
|
|
|
|
// so we can't completely avoid the blocking.
|
|
|
|
// https://www.freebsd.org/cgi/man.cgi?query=arc4random_buf.
|
|
|
|
#ifdef __FreeBSD__
|
2022-02-26 19:47:11 +00:00
|
|
|
# define DONT_USE_O_NOFOLLOW 1
|
2021-12-29 09:45:53 +00:00
|
|
|
# define USE_FREEBSD_ARC4RANDOM 1
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2022-02-27 21:28:32 +00:00
|
|
|
// Solaris links /dev/urandom -> ../devices/pseudo/random@0:urandom
|
|
|
|
// We can't access the device. Avoid O_NOFOLLOW for the platform.
|
2022-02-26 19:47:11 +00:00
|
|
|
#ifdef __sun
|
|
|
|
# define DONT_USE_O_NOFOLLOW 1
|
|
|
|
#endif
|
|
|
|
|
2022-02-27 21:28:32 +00:00
|
|
|
// And other OSes that don't define it
|
|
|
|
#ifndef O_NOFOLLOW
|
|
|
|
# define DONT_USE_O_NOFOLLOW 1
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
2016-05-03 04:23:05 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-11-05 06:59:46 +00:00
|
|
|
#include <windows.h>
|
2019-08-27 18:44:27 +00:00
|
|
|
#ifndef ERROR_INCORRECT_SIZE
|
|
|
|
# define ERROR_INCORRECT_SIZE 0x000005B6
|
|
|
|
#endif
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(USE_MS_CRYPTOAPI)
|
2015-11-05 06:59:46 +00:00
|
|
|
#include <wincrypt.h>
|
2016-05-03 04:23:05 +00:00
|
|
|
#ifndef CRYPT_NEWKEYSET
|
|
|
|
# define CRYPT_NEWKEYSET 0x00000008
|
|
|
|
#endif
|
|
|
|
#ifndef CRYPT_MACHINE_KEYSET
|
|
|
|
# define CRYPT_MACHINE_KEYSET 0x00000020
|
|
|
|
#endif
|
|
|
|
#elif defined(USE_MS_CNGAPI)
|
|
|
|
#include <bcrypt.h>
|
|
|
|
#ifndef BCRYPT_SUCCESS
|
|
|
|
# define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
|
|
|
|
#endif
|
|
|
|
#ifndef STATUS_INVALID_PARAMETER
|
|
|
|
# define STATUS_INVALID_PARAMETER 0xC000000D
|
|
|
|
#endif
|
|
|
|
#ifndef STATUS_INVALID_HANDLE
|
|
|
|
# define STATUS_INVALID_HANDLE 0xC0000008
|
|
|
|
#endif
|
|
|
|
#endif
|
2019-08-27 18:44:27 +00:00
|
|
|
#endif // Win32
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
#if defined(NONBLOCKING_RNG_AVAILABLE) || defined(BLOCKING_RNG_AVAILABLE)
|
|
|
|
OS_RNG_Err::OS_RNG_Err(const std::string &operation)
|
2016-09-10 08:57:48 +00:00
|
|
|
: Exception(OTHER_ERROR, "OS_Rng: " + operation + " operation failed with error " +
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
|
|
|
"0x" + IntToString(GetLastError(), 16)
|
|
|
|
#else
|
|
|
|
IntToString(errno)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(USE_MS_CNGAPI)
|
|
|
|
inline DWORD NtStatusToErrorCode(NTSTATUS status)
|
|
|
|
{
|
2023-06-25 02:51:26 +00:00
|
|
|
if (status == static_cast<NTSTATUS>(STATUS_INVALID_PARAMETER))
|
2016-05-03 04:23:05 +00:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
2023-06-25 02:51:26 +00:00
|
|
|
else if (status == static_cast<NTSTATUS>(STATUS_INVALID_HANDLE))
|
2016-05-03 04:23:05 +00:00
|
|
|
return ERROR_INVALID_HANDLE;
|
|
|
|
else
|
2023-06-25 02:51:26 +00:00
|
|
|
return static_cast<DWORD>(status);
|
2016-05-03 04:23:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
# define CRYPTOPP_CONTAINER L"Crypto++ RNG"
|
|
|
|
#else
|
|
|
|
# define CRYPTOPP_CONTAINER "Crypto++ RNG"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MicrosoftCryptoProvider::MicrosoftCryptoProvider() : m_hProvider(0)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(USE_MS_CRYPTOAPI)
|
|
|
|
// See http://support.microsoft.com/en-us/kb/238187 for CRYPT_NEWKEYSET fallback strategy
|
2015-11-05 06:59:46 +00:00
|
|
|
if (!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
2016-05-03 04:23:05 +00:00
|
|
|
{
|
|
|
|
const DWORD firstErr = GetLastError();
|
|
|
|
if (!CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET /*user*/) &&
|
|
|
|
!CryptAcquireContext(&m_hProvider, CRYPTOPP_CONTAINER, 0, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
|
|
|
|
{
|
|
|
|
// Set original error with original code
|
|
|
|
SetLastError(firstErr);
|
|
|
|
throw OS_RNG_Err("CryptAcquireContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(USE_MS_CNGAPI)
|
|
|
|
NTSTATUS ret = BCryptOpenAlgorithmProvider(&m_hProvider, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
|
|
|
if (!(BCRYPT_SUCCESS(ret)))
|
|
|
|
{
|
|
|
|
// Hack... OS_RNG_Err calls GetLastError()
|
|
|
|
SetLastError(NtStatusToErrorCode(ret));
|
|
|
|
throw OS_RNG_Err("BCryptOpenAlgorithmProvider");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
|
|
|
|
{
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(USE_MS_CRYPTOAPI)
|
|
|
|
if (m_hProvider)
|
|
|
|
CryptReleaseContext(m_hProvider, 0);
|
|
|
|
#elif defined(USE_MS_CNGAPI)
|
|
|
|
if (m_hProvider)
|
|
|
|
BCryptCloseAlgorithmProvider(m_hProvider, 0);
|
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // CRYPTOPP_WIN32_AVAILABLE
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
NonblockingRng::NonblockingRng()
|
|
|
|
{
|
2022-02-26 19:47:11 +00:00
|
|
|
#if !defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(USE_FREEBSD_ARC4RANDOM)
|
|
|
|
# ifndef DONT_USE_O_NOFOLLOW
|
2021-09-25 19:32:27 +00:00
|
|
|
const int flags = O_RDONLY|O_NOFOLLOW;
|
2022-02-26 19:47:11 +00:00
|
|
|
# else
|
2021-09-25 19:32:27 +00:00
|
|
|
const int flags = O_RDONLY;
|
2022-02-26 19:47:11 +00:00
|
|
|
# endif
|
2021-09-25 19:32:27 +00:00
|
|
|
|
|
|
|
m_fd = open("/dev/urandom", flags);
|
2015-11-05 06:59:46 +00:00
|
|
|
if (m_fd == -1)
|
|
|
|
throw OS_RNG_Err("open /dev/urandom");
|
2019-08-12 09:40:22 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NonblockingRng::~NonblockingRng()
|
|
|
|
{
|
2022-02-26 19:47:11 +00:00
|
|
|
#if !defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(USE_FREEBSD_ARC4RANDOM)
|
2015-11-05 06:59:46 +00:00
|
|
|
close(m_fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void NonblockingRng::GenerateBlock(byte *output, size_t size)
|
|
|
|
{
|
|
|
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
2016-05-03 04:23:05 +00:00
|
|
|
// Acquiring a provider is expensive. Do it once and retain the reference.
|
2020-08-07 23:11:47 +00:00
|
|
|
# if defined(CRYPTOPP_CXX11_STATIC_INIT)
|
2019-08-12 04:11:41 +00:00
|
|
|
static const MicrosoftCryptoProvider hProvider = MicrosoftCryptoProvider();
|
|
|
|
# else
|
2017-03-15 11:19:31 +00:00
|
|
|
const MicrosoftCryptoProvider &hProvider = Singleton<MicrosoftCryptoProvider>().Ref();
|
2019-08-12 04:11:41 +00:00
|
|
|
# endif
|
2016-05-03 04:23:05 +00:00
|
|
|
# if defined(USE_MS_CRYPTOAPI)
|
2019-08-18 01:19:04 +00:00
|
|
|
DWORD dwSize;
|
|
|
|
CRYPTOPP_ASSERT(SafeConvert(size, dwSize));
|
|
|
|
if (!SafeConvert(size, dwSize))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INCORRECT_SIZE);
|
|
|
|
throw OS_RNG_Err("GenerateBlock size");
|
|
|
|
}
|
|
|
|
BOOL ret = CryptGenRandom(hProvider.GetProviderHandle(), dwSize, output);
|
|
|
|
CRYPTOPP_ASSERT(ret != FALSE);
|
|
|
|
if (ret == FALSE)
|
2015-11-05 06:59:46 +00:00
|
|
|
throw OS_RNG_Err("CryptGenRandom");
|
2016-05-03 04:23:05 +00:00
|
|
|
# elif defined(USE_MS_CNGAPI)
|
2019-08-18 01:19:04 +00:00
|
|
|
ULONG ulSize;
|
|
|
|
CRYPTOPP_ASSERT(SafeConvert(size, ulSize));
|
|
|
|
if (!SafeConvert(size, ulSize))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INCORRECT_SIZE);
|
|
|
|
throw OS_RNG_Err("GenerateBlock size");
|
|
|
|
}
|
|
|
|
NTSTATUS ret = BCryptGenRandom(hProvider.GetProviderHandle(), output, ulSize, 0);
|
|
|
|
CRYPTOPP_ASSERT(BCRYPT_SUCCESS(ret));
|
2016-05-03 04:23:05 +00:00
|
|
|
if (!(BCRYPT_SUCCESS(ret)))
|
|
|
|
{
|
|
|
|
// Hack... OS_RNG_Err calls GetLastError()
|
|
|
|
SetLastError(NtStatusToErrorCode(ret));
|
|
|
|
throw OS_RNG_Err("BCryptGenRandom");
|
|
|
|
}
|
|
|
|
# endif
|
2015-11-05 06:59:46 +00:00
|
|
|
#else
|
2021-12-29 09:45:53 +00:00
|
|
|
|
|
|
|
# if defined(USE_FREEBSD_ARC4RANDOM)
|
|
|
|
// Cryptographic quality prng based on ChaCha20,
|
|
|
|
// https://www.freebsd.org/cgi/man.cgi?query=arc4random_buf
|
|
|
|
arc4random_buf(output, size);
|
|
|
|
# else
|
2015-11-05 06:59:46 +00:00
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
ssize_t len = read(m_fd, output, size);
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
// /dev/urandom reads CAN give EAGAIN errors! (maybe EINTR as well)
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
throw OS_RNG_Err("read /dev/urandom");
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output += len;
|
|
|
|
size -= len;
|
|
|
|
}
|
2021-12-29 09:45:53 +00:00
|
|
|
# endif // USE_FREEBSD_ARC4RANDOM
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // CRYPTOPP_WIN32_AVAILABLE
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // NONBLOCKING_RNG_AVAILABLE
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
|
|
|
|
#ifdef BLOCKING_RNG_AVAILABLE
|
|
|
|
|
|
|
|
#ifndef CRYPTOPP_BLOCKING_RNG_FILENAME
|
2021-09-25 19:34:39 +00:00
|
|
|
# ifdef __OpenBSD__
|
|
|
|
# define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/srandom"
|
|
|
|
# else
|
|
|
|
# define CRYPTOPP_BLOCKING_RNG_FILENAME "/dev/random"
|
|
|
|
# endif
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
BlockingRng::BlockingRng()
|
|
|
|
{
|
2022-02-26 19:47:11 +00:00
|
|
|
#ifndef DONT_USE_O_NOFOLLOW
|
2021-09-25 19:32:27 +00:00
|
|
|
const int flags = O_RDONLY|O_NOFOLLOW;
|
2021-09-25 19:34:39 +00:00
|
|
|
#else
|
2021-09-25 19:32:27 +00:00
|
|
|
const int flags = O_RDONLY;
|
2021-09-25 19:34:39 +00:00
|
|
|
#endif
|
2021-09-25 19:32:27 +00:00
|
|
|
|
|
|
|
m_fd = open(CRYPTOPP_BLOCKING_RNG_FILENAME, flags);
|
2015-11-05 06:59:46 +00:00
|
|
|
if (m_fd == -1)
|
|
|
|
throw OS_RNG_Err("open " CRYPTOPP_BLOCKING_RNG_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingRng::~BlockingRng()
|
|
|
|
{
|
|
|
|
close(m_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingRng::GenerateBlock(byte *output, size_t size)
|
|
|
|
{
|
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
// on some systems /dev/random will block until all bytes
|
|
|
|
// are available, on others it returns immediately
|
|
|
|
ssize_t len = read(m_fd, output, size);
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
// /dev/random reads CAN give EAGAIN errors! (maybe EINTR as well)
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
throw OS_RNG_Err("read " CRYPTOPP_BLOCKING_RNG_FILENAME);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= len;
|
|
|
|
output += len;
|
|
|
|
if (size)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // BLOCKING_RNG_AVAILABLE
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
|
|
|
|
void OS_GenerateRandomBlock(bool blocking, byte *output, size_t size)
|
|
|
|
{
|
|
|
|
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
|
|
if (blocking)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef BLOCKING_RNG_AVAILABLE
|
|
|
|
BlockingRng rng;
|
|
|
|
rng.GenerateBlock(output, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BLOCKING_RNG_AVAILABLE
|
|
|
|
if (!blocking)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
|
|
NonblockingRng rng;
|
|
|
|
rng.GenerateBlock(output, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoSeededRandomPool::Reseed(bool blocking, unsigned int seedSize)
|
|
|
|
{
|
|
|
|
SecByteBlock seed(seedSize);
|
|
|
|
OS_GenerateRandomBlock(blocking, seed, seedSize);
|
|
|
|
IncorporateEntropy(seed, seedSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // OS_RNG_AVAILABLE
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // CRYPTOPP_IMPORTS
|