]> granicus.if.org Git - libevent/commitdiff
Convert from WinCrypt to Windows BCrypt
authorGerry Garvey <ggarvey@gmx.com>
Sat, 1 Aug 2020 15:10:48 +0000 (16:10 +0100)
committerAzat Khuzhin <azat@libevent.org>
Sun, 2 Aug 2020 15:27:10 +0000 (18:27 +0300)
Fixes: #1069
CMakeLists.txt
Makefile.am
arc4random.c
include/event2/util.h

index 89f0ca324c047fef4e4d9468b13bcea5d9adbd54..5a29621687906752ccf8a6ad8ce22769f7546baf 100644 (file)
@@ -340,7 +340,7 @@ endif()
 
 # Winsock.
 if(WIN32)
-    set(CMAKE_REQUIRED_LIBRARIES  ws2_32 shell32 advapi32)
+    set(CMAKE_REQUIRED_LIBRARIES  ws2_32 shell32 advapi32 bcrypt)
     set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h -D_WIN32_WINNT=0x0600)
 endif()
 if (SOLARIS)
@@ -935,7 +935,7 @@ if(WIN32)
     list(APPEND HDR_PRIVATE WIN32-Code/getopt.h)
 
     set(EVENT__DNS_USE_FTIME_FOR_ID 1)
-    set(LIB_PLATFORM ws2_32 shell32 advapi32)
+    set(LIB_PLATFORM ws2_32 shell32 advapi32 bcrypt)
     add_definitions(
             -D_CRT_SECURE_NO_WARNINGS
             -D_CRT_NONSTDC_NO_DEPRECATE)
index 14a1ea68e4f78566a96b5c5ee2dd2f3e00ca12c5..5c97c375ac8c6123e5f02c0c58ce4728f0593e0e 100644 (file)
@@ -183,7 +183,7 @@ include test/include.am
 if BUILD_WIN32
 
 SYS_CORE_LIBS = -liphlpapi
-SYS_LIBS = -lws2_32 -lshell32 -ladvapi32
+SYS_LIBS = -lws2_32 -lshell32 -ladvapi32 -lbcrypt
 SYS_SRC = win32select.c buffer_iocp.c event_iocp.c \
        bufferevent_async.c
 SYS_INCLUDES = -IWIN32-Code
index 9a00ea2c1074149ef1d62549c3fe32990cea5b51..cde22d0afeaae7cafd4267de64bca908cb8864da 100644 (file)
@@ -52,7 +52,7 @@
 #ifndef ARC4RANDOM_NO_INCLUDES
 #include "evconfig-private.h"
 #ifdef _WIN32
-#include <wincrypt.h>
+#include <bcrypt.h>
 #include <process.h>
 #include <winerror.h>
 #else
@@ -149,20 +149,10 @@ read_all(int fd, unsigned char *buf, size_t count)
 static int
 arc4_seed_win32(void)
 {
-       /* This is adapted from Tor's crypto_seed_rng() */
-       static int provider_set = 0;
-       static HCRYPTPROV provider;
        unsigned char buf[ADD_ENTROPY];
 
-       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))
+       if (BCryptGenRandom(NULL, buf, sizeof(buf),
+               BCRYPT_USE_SYSTEM_PREFERRED_RNG))
                return -1;
        arc4_addrandom(buf, sizeof(buf));
        evutil_memclear_(buf, sizeof(buf));
index 4cedfc3f9b444a8f8703908197c332ef43458c7a..43955bf6f7e286ffea003e1990dd38f7292f1b32 100644 (file)
@@ -819,7 +819,7 @@ const char *evutil_gai_strerror(int err);
  *
  * Current versions of Libevent use an ARC4-based random number generator,
  * seeded using the platform's entropy source (/dev/urandom on Unix-like
- * systems; CryptGenRandom on Windows).  This is not actually as secure as it
+ * systems; BCryptGenRandom on Windows). This is not actually as secure as it
  * should be: ARC4 is a pretty lousy cipher, and the current implementation
  * provides only rudimentary prediction- and backtracking-resistance.  Don't
  * use this for serious cryptographic applications.