From 64a568d6100e5eab5b86e8bd31846212654d1fcf Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Wed, 20 Jan 2016 11:47:19 +0200 Subject: [PATCH] Fix MS-Windows build broken by a botched merge from emacs-25 * src/w32.c (w32_crypto_hprov): New static variable. (globals_of_w32): Initialize w32_crypto_hprov. (w32_init_crypt_random, w32_init_random): New functions. Include wincrypt.h. * src/w32.h (w32_init_random): Add prototype. --- src/w32.c | 32 ++++++++++++++++++++++++++++++++ src/w32.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/src/w32.c b/src/w32.c index 183a4e7e9d9..6f1d5fd1698 100644 --- a/src/w32.c +++ b/src/w32.c @@ -224,6 +224,8 @@ typedef struct _REPARSE_DATA_BUFFER { #include /* should be after winsock2.h */ +#include + #include #include "w32.h" @@ -2094,6 +2096,34 @@ init_user_info (void) CloseHandle (token); } +static HCRYPTPROV w32_crypto_hprov; +static int +w32_init_crypt_random (void) +{ + if (!CryptAcquireContext (&w32_crypto_hprov, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) + { + DebPrint (("CryptAcquireContext failed with error %x\n", + GetLastError ())); + w32_crypto_hprov = 0; + return -1; + } + return 0; +} + +int +w32_init_random (void *buf, ptrdiff_t buflen) +{ + if (!w32_crypto_hprov) + w32_init_crypt_random (); + if (w32_crypto_hprov) + { + if (CryptGenRandom (w32_crypto_hprov, buflen, (BYTE *)buf)) + return 0; + } + return -1; +} + int random (void) { @@ -9417,6 +9447,8 @@ globals_of_w32 (void) extern void dynlib_reset_last_error (void); dynlib_reset_last_error (); #endif + + w32_crypto_hprov = (HCRYPTPROV)0; } /* For make-serial-process */ diff --git a/src/w32.h b/src/w32.h index 097241b1b8f..fde3803c739 100644 --- a/src/w32.h +++ b/src/w32.h @@ -223,6 +223,9 @@ extern int w32_memory_info (unsigned long long *, unsigned long long *, /* Compare 2 UTF-8 strings in locale-dependent fashion. */ extern int w32_compare_strings (const char *, const char *, char *, int); +/* Return a cryptographically secure seed for PRNG. */ +extern int w32_init_random (void *, ptrdiff_t); + #ifdef HAVE_GNUTLS #include -- 2.39.2