proper portable fallback
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CRYPTOPP_UNIX_AVAILABLE
|
#if defined(UNIX)
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -28,16 +28,16 @@ OS_RNG_Err::OS_RNG_Err(const std::string &operation)
|
|||||||
: Exception(OTHER_ERROR, "OS_Rng: " + operation + " operation failed with error " +
|
: Exception(OTHER_ERROR, "OS_Rng: " + operation + " operation failed with error " +
|
||||||
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
||||||
"0x" + IntToString(GetLastError(), 16)
|
"0x" + IntToString(GetLastError(), 16)
|
||||||
#else
|
#elif defined(UNIX)
|
||||||
IntToString(errno)
|
IntToString(errno)
|
||||||
|
#else
|
||||||
|
"(unknown)"
|
||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
||||||
|
|
||||||
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
||||||
|
|
||||||
MicrosoftCryptoProvider::MicrosoftCryptoProvider()
|
MicrosoftCryptoProvider::MicrosoftCryptoProvider()
|
||||||
@@ -61,7 +61,7 @@ MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
|
|||||||
|
|
||||||
NonblockingRng::NonblockingRng()
|
NonblockingRng::NonblockingRng()
|
||||||
{
|
{
|
||||||
#ifndef CRYPTOPP_WIN32_AVAILABLE
|
#if defined(UNIX)
|
||||||
m_fd = open("/dev/urandom",O_RDONLY);
|
m_fd = open("/dev/urandom",O_RDONLY);
|
||||||
if (m_fd == -1)
|
if (m_fd == -1)
|
||||||
throw OS_RNG_Err("open /dev/urandom");
|
throw OS_RNG_Err("open /dev/urandom");
|
||||||
@@ -70,7 +70,7 @@ NonblockingRng::NonblockingRng()
|
|||||||
|
|
||||||
NonblockingRng::~NonblockingRng()
|
NonblockingRng::~NonblockingRng()
|
||||||
{
|
{
|
||||||
#ifndef CRYPTOPP_WIN32_AVAILABLE
|
#if defined(UNIX)
|
||||||
close(m_fd);
|
close(m_fd);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -90,55 +90,15 @@ void NonblockingRng::GenerateBlock(byte *output, unsigned int size)
|
|||||||
# endif
|
# endif
|
||||||
if (!CryptGenRandom(m_Provider.GetProviderHandle(), size, output))
|
if (!CryptGenRandom(m_Provider.GetProviderHandle(), size, output))
|
||||||
throw OS_RNG_Err("CryptGenRandom");
|
throw OS_RNG_Err("CryptGenRandom");
|
||||||
#else
|
#elif defined(UNIX)
|
||||||
if (read(m_fd, output, size) != int(size))
|
if (read(m_fd, output, size) != int(size))
|
||||||
throw OS_RNG_Err("read /dev/urandom");
|
throw OS_RNG_Err("read /dev/urandom");
|
||||||
|
#else
|
||||||
|
#warning No crypto source
|
||||||
|
memset( output, 0, size );
|
||||||
#endif
|
#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
|
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#ifdef OS_RNG_AVAILABLE
|
|
||||||
|
|
||||||
//removed
|
//removed
|
||||||
//#include "randpool.h"
|
//#include "randpool.h"
|
||||||
|
|
||||||
@@ -21,8 +19,6 @@ public:
|
|||||||
OS_RNG_Err(const std::string &operation);
|
OS_RNG_Err(const std::string &operation);
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef NONBLOCKING_RNG_AVAILABLE
|
|
||||||
|
|
||||||
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
||||||
class MicrosoftCryptoProvider
|
class MicrosoftCryptoProvider
|
||||||
{
|
{
|
||||||
@@ -54,32 +50,11 @@ protected:
|
|||||||
# ifndef WORKAROUND_MS_BUG_Q258000
|
# ifndef WORKAROUND_MS_BUG_Q258000
|
||||||
MicrosoftCryptoProvider m_Provider;
|
MicrosoftCryptoProvider m_Provider;
|
||||||
# endif
|
# endif
|
||||||
#else
|
#elif defined(UNIX)
|
||||||
int m_fd;
|
int m_fd;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef BLOCKING_RNG_AVAILABLE
|
|
||||||
|
|
||||||
//! encapsulate /dev/random
|
|
||||||
class BlockingRng : public RandomNumberGenerator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
BlockingRng();
|
|
||||||
~BlockingRng();
|
|
||||||
byte GenerateByte();
|
|
||||||
void GenerateBlock(byte *output, unsigned int size);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
int m_fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user