Skip to content

Commit a1f1c5f

Browse files
authored
Merge pull request #584 from paperchalice/bcrypt
Support "Cryptography API: Next Generation" on Windows
2 parents 5809141 + 2065e4c commit a1f1c5f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ set(PACKAGE_RELEASE_VERSION 1)
2323
include(GNUInstallDirs)
2424
include(CheckIPOSupported)
2525
include(CMakePackageConfigHelpers)
26+
include(CMakePushCheckState)
27+
include(CheckSymbolExists)
2628
# default is "No tests"
2729
option(BUILD_TESTING "" OFF)
2830
include(CTest)
@@ -104,6 +106,16 @@ target_compile_options(${PROJECT_NAME} BEFORE PRIVATE
104106
target_link_options(${PROJECT_NAME} BEFORE PRIVATE
105107
${LTM_LD_FLAGS}
106108
)
109+
if(MSVC)
110+
cmake_push_check_state()
111+
set(CMAKE_REQUIRED_LIBRARIES bcrypt)
112+
check_symbol_exists(BCryptGenRandom "Windows.h;bcrypt.h" BCRYPT_AVAILABLE)
113+
cmake_pop_check_state()
114+
if(BCRYPT_AVAILABLE)
115+
target_compile_definitions(${PROJECT_NAME} PRIVATE LTM_WIN32_BCRYPT)
116+
target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt)
117+
endif()
118+
endif()
107119

108120
set(PUBLIC_HEADERS tommath.h)
109121
set(C89 False CACHE BOOL "(Usually maintained automatically) Enable when the library is in c89 mode to package the correct header files on install")

s_mp_rand_platform.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ static mp_err s_read_arc4random(void *p, size_t n)
2828

2929
#define WIN32_LEAN_AND_MEAN
3030
#include <windows.h>
31+
32+
#ifdef LTM_WIN32_BCRYPT
33+
#include <bcrypt.h>
34+
#pragma comment(lib, "bcrypt")
35+
36+
static mp_err s_read_wincsp(void *p, size_t n)
37+
{
38+
return BCRYPT_SUCCESS(BCryptGenRandom(NULL, (PUCHAR)p, (ULONG)n,
39+
BCRYPT_USE_SYSTEM_PREFERRED_RNG)) ? MP_OKAY : MP_ERR;
40+
}
41+
#else
3142
#include <wincrypt.h>
3243

3344
static mp_err s_read_wincsp(void *p, size_t n)
@@ -45,6 +56,7 @@ static mp_err s_read_wincsp(void *p, size_t n)
4556
}
4657
return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
4758
}
59+
#endif
4860
#endif /* WIN32 */
4961

5062
#if !defined(S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ)

0 commit comments

Comments
 (0)