Move platform specific random number stuff

This commit is contained in:
Vinnie Falco
2013-05-26 10:49:19 -07:00
parent b40f11288b
commit a7eb5c7ca6
8 changed files with 86 additions and 93 deletions

View File

@@ -24,6 +24,7 @@
#include "ripple_basics.h"
#include <iostream>
#include <fstream>
#include <boost/algorithm/string.hpp>
@@ -54,12 +55,15 @@
#include "events/ripple_UptimeTimer.cpp"
#include "memory/ripple_StringUtilities.cpp"
#ifdef WIN32
#include <windows.h> // for ripple_RandomNumbers.cpp
#include <wincrypt.h> // for ripple_RandomNumbers.cpp
// Winsock #defines 'max' and does other stupid things so put it last
#include "Winsock2.h" // for ripple_ByteOrder.cpp
#include <Winsock2.h> // for ripple_ByteOrder.cpp
#endif
#include "memory/ripple_ByteOrder.cpp"
#include "memory/ripple_StringUtilities.cpp"
#include "system/ripple_RandomNumbers.cpp"

View File

@@ -28,3 +28,81 @@ void getRand(unsigned char *buf, int num)
}
}
//------------------------------------------------------------------------------
// VFALCO: TODO replace WIN32 macro with VFLIB_WIN32
#ifdef WIN32
bool AddSystemEntropy()
{ // Get entropy from the Windows crypto provider
char name[512], rand[128];
DWORD count = 500;
HCRYPTPROV cryptoHandle;
if (!CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count))
{
#ifdef DEBUG
std::cerr << "Unable to get default crypto provider" << std::endl;
#endif
return false;
}
if (!CryptAcquireContext(&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
#ifdef DEBUG
std::cerr << "Unable to acquire crypto provider" << std::endl;
#endif
return false;
}
if(!CryptGenRandom(cryptoHandle, 128, reinterpret_cast<BYTE*>(rand)))
{
#ifdef DEBUG
std::cerr << "Unable to get entropy from crypto provider" << std::endl;
#endif
CryptReleaseContext(cryptoHandle, 0);
return false;
}
CryptReleaseContext(cryptoHandle, 0);
RAND_seed(rand, 128);
return true;
}
#else
#include <iostream>
#include <fstream>
#include <openssl/rand.h>
bool AddSystemEntropy()
{
char rand[128];
std::ifstream reader;
reader.open("/dev/urandom", std::ios::in | std::ios::binary);
if (!reader.is_open())
{
#ifdef DEBUG
std::cerr << "Unable to open random source" << std::endl;
#endif
return false;
}
reader.read(rand, 128);
int bytesRead = reader.gcount();
if (bytesRead == 0)
{
#ifdef DEBUG
std::cerr << "Unable to read from random source" << std::endl;
#endif
return false;
}
RAND_seed(rand, bytesRead);
return bytesRead >= 64;
}
#endif

View File

@@ -19,6 +19,8 @@
#ifndef RIPPLE_RANDOMNUMBERS_H
#define RIPPLE_RANDOMNUMBERS_H
extern bool AddSystemEntropy ();
// Cryptographically secure random number source
// VFALCO: TODO Clean this up, rename stuff

View File

@@ -58,7 +58,6 @@ static const uint64 tenTo17m1 = tenTo17 - 1;
#include "src/cpp/ripple/PackedMessage.cpp" // no log
#include "src/cpp/ripple/ParameterTable.cpp" // no log
#include "src/cpp/ripple/ParseSection.cpp"
#include "src/cpp/ripple/PlatRand.cpp" // no log
#include "src/cpp/ripple/ProofOfWork.cpp"
#include "src/cpp/ripple/RippleAddress.cpp"
#include "src/cpp/ripple/rfc1751.cpp" // no log

View File

@@ -887,12 +887,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="src\cpp\ripple\PlatRand.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="src\cpp\ripple\ProofOfWork.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -516,9 +516,6 @@
<ClCompile Include="src\cpp\ripple\ParseSection.cpp">
<Filter>1. Modules\ripple_mess\types</Filter>
</ClCompile>
<ClCompile Include="src\cpp\ripple\PlatRand.cpp">
<Filter>1. Modules\ripple_mess\types</Filter>
</ClCompile>
<ClCompile Include="src\cpp\ripple\ProofOfWork.cpp">
<Filter>1. Modules\ripple_mess\types</Filter>
</ClCompile>

View File

@@ -1,80 +0,0 @@
#ifdef WIN32
#include <windows.h>
#include <wincrypt.h>
#include <openssl/rand.h>
bool AddSystemEntropy()
{ // Get entropy from the Windows crypto provider
char name[512], rand[128];
DWORD count = 500;
HCRYPTPROV cryptoHandle;
if (!CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count))
{
#ifdef DEBUG
std::cerr << "Unable to get default crypto provider" << std::endl;
#endif
return false;
}
if (!CryptAcquireContext(&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
#ifdef DEBUG
std::cerr << "Unable to acquire crypto provider" << std::endl;
#endif
return false;
}
if(!CryptGenRandom(cryptoHandle, 128, reinterpret_cast<BYTE*>(rand)))
{
#ifdef DEBUG
std::cerr << "Unable to get entropy from crypto provider" << std::endl;
#endif
CryptReleaseContext(cryptoHandle, 0);
return false;
}
CryptReleaseContext(cryptoHandle, 0);
RAND_seed(rand, 128);
return true;
}
#else
#include <iostream>
#include <fstream>
#include <openssl/rand.h>
bool AddSystemEntropy()
{
char rand[128];
std::ifstream reader;
reader.open("/dev/urandom", std::ios::in | std::ios::binary);
if (!reader.is_open())
{
#ifdef DEBUG
std::cerr << "Unable to open random source" << std::endl;
#endif
return false;
}
reader.read(rand, 128);
int bytesRead = reader.gcount();
if (bytesRead == 0)
{
#ifdef DEBUG
std::cerr << "Unable to read from random source" << std::endl;
#endif
return false;
}
RAND_seed(rand, bytesRead);
return bytesRead >= 64;
}
#endif

View File

@@ -14,7 +14,6 @@
namespace po = boost::program_options;
extern bool AddSystemEntropy();
extern void TFInit();
extern void LEFInit();
extern void SVFInit();