Skip to content

Commit

Permalink
Merge pull request #584 from paperchalice/bcrypt
Browse files Browse the repository at this point in the history
Support "Cryptography API: Next Generation" on Windows
  • Loading branch information
sjaeckel authored Jan 30, 2025
2 parents 5809141 + 2065e4c commit a1f1c5f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ set(PACKAGE_RELEASE_VERSION 1)
include(GNUInstallDirs)
include(CheckIPOSupported)
include(CMakePackageConfigHelpers)
include(CMakePushCheckState)
include(CheckSymbolExists)
# default is "No tests"
option(BUILD_TESTING "" OFF)
include(CTest)
Expand Down Expand Up @@ -104,6 +106,16 @@ target_compile_options(${PROJECT_NAME} BEFORE PRIVATE
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")
Expand Down
12 changes: 12 additions & 0 deletions s_mp_rand_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ static mp_err s_read_arc4random(void *p, size_t n)

#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)
Expand All @@ -45,6 +56,7 @@ static mp_err s_read_wincsp(void *p, size_t n)
}
return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
}
#endif
#endif /* WIN32 */

#if !defined(S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ)
Expand Down

0 comments on commit a1f1c5f

Please # to comment.