1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// osrng.cpp - written and placed in the public domain by Wei Dai
// Thanks to Leonard Janke for the suggestion for AutoSeededRandomPool.
#include "pch.h"
#ifndef CRYPTOPP_IMPORTS
#include "osrng.h"
#ifdef OS_RNG_AVAILABLE
#include "rng.h"
#ifdef CRYPTOPP_WIN32_AVAILABLE
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include <windows.h>
#include <wincrypt.h>
#endif
#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)
: Exception(OTHER_ERROR, "OS_Rng: " + operation + " operation failed with error " +
#ifdef CRYPTOPP_WIN32_AVAILABLE
"0x" + IntToString(GetLastError(), 16)
#else
IntToString(errno)
#endif
)
{
}
#endif
#ifdef NONBLOCKING_RNG_AVAILABLE
#ifdef CRYPTOPP_WIN32_AVAILABLE
MicrosoftCryptoProvider::MicrosoftCryptoProvider()
{
if(!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
throw OS_RNG_Err("CryptAcquireContext");
}
MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
{
CryptReleaseContext(m_hProvider, 0);
}
#endif
NonblockingRng::NonblockingRng()
{
#ifndef CRYPTOPP_WIN32_AVAILABLE
m_fd = open("/dev/urandom",O_RDONLY);
if (m_fd == -1)
throw OS_RNG_Err("open /dev/urandom");
#endif
}
NonblockingRng::~NonblockingRng()
{
#ifndef CRYPTOPP_WIN32_AVAILABLE
close(m_fd);
#endif
}
byte NonblockingRng::GenerateByte()
{
byte b;
GenerateBlock(&b, 1);
return b;
}
void NonblockingRng::GenerateBlock(byte *output, unsigned int size)
{
#ifdef CRYPTOPP_WIN32_AVAILABLE
# ifdef WORKAROUND_MS_BUG_Q258000
static MicrosoftCryptoProvider m_Provider;
# endif
if (!CryptGenRandom(m_Provider.GetProviderHandle(), size, output))
throw OS_RNG_Err("CryptGenRandom");
#else
if (read(m_fd, output, size) != size)
throw OS_RNG_Err("read /dev/urandom");
#endif
}
#endif
// *************************************************************
#ifdef BLOCKING_RNG_AVAILABLE
BlockingRng::BlockingRng()
{
m_fd = open("/dev/random",O_RDONLY);
if (m_fd == -1)
throw OS_RNG_Err("open /dev/random");
}
BlockingRng::~BlockingRng()
{
close(m_fd);
}
byte BlockingRng::GenerateByte()
{
byte b;
GenerateBlock(&b, 1);
return b;
}
void BlockingRng::GenerateBlock(byte *output, unsigned int size)
{
while (size)
{
// on some systems /dev/random will block until all bytes
// are available, on others it will returns immediately
int len = read(m_fd, output, STDMIN(size, (unsigned int)INT_MAX));
if (len == -1)
throw OS_RNG_Err("read /dev/random");
size -= len;
output += len;
if (size)
sleep(1);
}
}
#endif
// *************************************************************
void OS_GenerateRandomBlock(bool blocking, byte *output, unsigned int 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);
Put(seed, seedSize);
}
NAMESPACE_END
#endif
#endif
|