aboutsummaryrefslogtreecommitdiff
path: root/vcpkg/ports/libtommath/bcrypt.patch
blob: dc59d79a14a9a42a833a75197947d34525be976e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,8 +22,10 @@
 #-----------------------------------------------------------------------------
 include(GNUInstallDirs)
 include(CheckIPOSupported)
 include(CMakePackageConfigHelpers)
+include(CMakePushCheckState)
+include(CheckSymbolExists)
 # default is "No tests"
 option(BUILD_TESTING "" OFF)
 include(CTest)
 include(sources.cmake)
@@ -118,8 +120,19 @@
 target_link_options(${PROJECT_NAME} BEFORE PRIVATE
     ${LTM_LD_FLAGS}
 )
 
+if(MSVC)
+    cmake_push_check_state()
+    set(CMAKE_REQUIRED_LIBRARIES bcrypt)
+    check_symbol_exists(BCryptGenRandom "Windows.h;bcrypt.h" BCRYPT_AVAILABLE)
+    cmake_pop_check_state()
+    if(BCRYPT_AVAILABLE)
+        target_compile_definitions(${PROJECT_NAME} PRIVATE LTM_WIN32_BCRYPT)
+        target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt)
+    endif()
+endif()
+
 set(PUBLIC_HEADERS tommath.h)
 set(C89 False CACHE BOOL "(Usually maintained automatically) Enable when the library is in c89 mode to package the correct header files on install")
 if(C89)
     list(APPEND PUBLIC_HEADERS tommath_c89.h)
diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c
--- a/bn_s_mp_rand_platform.c
+++ b/bn_s_mp_rand_platform.c
@@ -28,8 +28,19 @@
 #endif
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+
+#ifdef LTM_WIN32_BCRYPT
+#include <bcrypt.h>
+#pragma comment(lib, "bcrypt")
+
+static mp_err s_read_wincsp(void *p, size_t n)
+{
+   return BCRYPT_SUCCESS(BCryptGenRandom(NULL, (PUCHAR)p, (ULONG)n,
+                                         BCRYPT_USE_SYSTEM_PREFERRED_RNG)) ? MP_OKAY : MP_ERR;
+}
+#else
 #include <wincrypt.h>
 
 static mp_err s_read_wincsp(void *p, size_t n)
 {
@@ -45,8 +56,9 @@
       hProv = h;
    }
    return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
 }
+#endif
 #endif /* WIN32 */
 
 #if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 25)