diff --git a/src/port/pg_strong_random.c b/src/port/pg_strong_random.c index 6be5874cbf..6d075eaf1a 100644 --- a/src/port/pg_strong_random.c +++ b/src/port/pg_strong_random.c @@ -28,17 +28,13 @@ #include #endif #ifdef USE_WIN32_RANDOM -#include +#include +#ifndef STATUS_SUCCESS +#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) #endif - -#ifdef USE_WIN32_RANDOM -/* - * Cache a global crypto provider that only gets freed when the process - * exits, in case we need random numbers more than once. - */ -static HCRYPTPROV hProvider = 0; #endif + #if defined(USE_DEV_URANDOM) /* * Read (random) bytes from a file. @@ -85,7 +81,7 @@ random_from_file(const char *filename, void *buf, size_t len) * We support a number of sources: * * 1. OpenSSL's RAND_bytes() - * 2. Windows' CryptGenRandom() function + * 2. Windows' BCryptGenRandom() function * 3. /dev/urandom * * The configure script will choose which one to use, and set @@ -140,28 +136,8 @@ pg_strong_random(void *buf, size_t len) * Windows has CryptoAPI for strong cryptographic numbers. */ #elif defined(USE_WIN32_RANDOM) - if (hProvider == 0) - { - if (!CryptAcquireContext(&hProvider, - NULL, - MS_DEF_PROV, - PROV_RSA_FULL, - CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) - { - /* - * On failure, set back to 0 in case the value was for some reason - * modified. - */ - hProvider = 0; - } - } - /* Re-check in case we just retrieved the provider */ - if (hProvider != 0) - { - if (CryptGenRandom(hProvider, len, buf)) - return true; - } - return false; + return (BCryptGenRandom(NULL, buf, len, + BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS); /* * Read /dev/urandom ourselves.