Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ if (WIN32)
list(APPEND SYMBOLS_TO_CHECK
_gmtime64
_gmtime64_s
BCryptGenRandom
)
else()
list(APPEND SYMBOLS_TO_CHECK
Expand Down
20 changes: 20 additions & 0 deletions arc4random.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
#ifndef ARC4RANDOM_NO_INCLUDES
#include "evconfig-private.h"
#ifdef _WIN32
#ifndef EVENT__HAVE_BCRYPTGENRANDOM
#include <wincrypt.h>
#else
#include <bcrypt.h>
#endif
#include <process.h>
#include <winerror.h>
#else
Expand Down Expand Up @@ -153,9 +157,25 @@ static int
arc4_seed_win32(void)
{
unsigned char buf[ADD_ENTROPY];
#ifndef EVENT__HAVE_BCRYPTGENRANDOM
/* This is adapted from Tor's crypto_seed_rng() */
static int provider_set = 0;
static HCRYPTPROV provider;

if (!provider_set) {
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
if (GetLastError() != (DWORD)NTE_BAD_KEYSET)
return -1;
}
provider_set = 1;
}
if (!CryptGenRandom(provider, sizeof(buf), buf))
#else

if (BCryptGenRandom(NULL, buf, sizeof(buf),
BCRYPT_USE_SYSTEM_PREFERRED_RNG))
#endif
return -1;
arc4_addrandom(buf, sizeof(buf));
evutil_memclear_(buf, sizeof(buf));
Expand Down
3 changes: 3 additions & 0 deletions event-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
/* Define to 1 if you have the `_gmtime64' function. */
#cmakedefine EVENT__HAVE__GMTIME64 1

/* Define to 1 if you have the `BCryptGenRandom' function. */
#cmakedefine EVENT__HAVE_BCRYPTGENRANDOM 1

/* Define to 1 if the system has the type `struct addrinfo'. */
#cmakedefine EVENT__HAVE_STRUCT_ADDRINFO 1

Expand Down