Get entropy from platform's native source.

This commit is contained in:
JoelKatz
2012-04-29 13:45:31 -07:00
parent f56dd7c108
commit ce9592eceb
2 changed files with 56 additions and 4 deletions

View File

@@ -13,12 +13,26 @@ bool AddSystemEntropy()
HCRYPTOPROV 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;
}
@@ -30,9 +44,36 @@ bool AddSystemEntropy()
#else
#include <iostream>
#include <fstream>
#include <openssl/rand.h>
bool AddSystemEntropy()
{ // Stub for implementing on other platforms
return false;
{
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

@@ -1,9 +1,13 @@
#include "Application.h"
#include <iostream>
#include "CallRPC.h"
#include "Config.h"
extern void runTests();
extern bool AddSystemEntropy();
using namespace std;
using namespace boost;
@@ -50,9 +54,16 @@ int parseCommandline(int argc, char* argv[])
theConfig.load();
if (!AddSystemEntropy())
{
#ifdef DEBUG
std::cerr << "Unable to add system entropy" << std::endl;
#endif
}
if(argc>1)
{
ret=commandLineRPC(argc, argv);
ret = commandLineRPC(argc, argv);
if(ret)
printHelp();
}
@@ -66,6 +77,6 @@ int main(int argc, char* argv[])
{
// runTests();
return(parseCommandline(argc,argv));
return(parseCommandline(argc, argv));
}
// vim:ts=4