From 83d42800351f50885d60d2b131f57af7a3ddce5e Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 29 Apr 2012 13:26:29 -0700 Subject: [PATCH] Get entropy from the windows system provider and give to SSL --- src/PlatRand.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/PlatRand.cpp diff --git a/src/PlatRand.cpp b/src/PlatRand.cpp new file mode 100644 index 0000000000..8b0d4dfc62 --- /dev/null +++ b/src/PlatRand.cpp @@ -0,0 +1,38 @@ + +#ifdef WIN32 + +#include +#include + +#include + +bool AddSystemEntropy() +{ // Get entropy from the Windows crypto provider + char name[512], rand[128]; + DWORD count = 500; + HCRYPTOPROV cryptoHandle; + + if (!CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count)) + return false; + if (!CryptAcquireContext(&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) + return false; + + if(!CryptGenRandom(cryptoHandle, 128, reinterpret_cast rand)) + { + CryptReleaseContext(cryptoHandle, 0); + return false; + } + + CryptReleaseContext(cryptoHandle, 0); + RAND_seed(rand, 128); + return true; +} + +#else + +bool AddSystemEntropy() +{ // Stub for implementing on other platforms + return false; +} + +#endif