Skip to content

Commit

Permalink
[libtommath] Use cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
paperchalice committed Jan 30, 2025
1 parent ab42fb3 commit daa5010
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 84 deletions.
50 changes: 50 additions & 0 deletions ports/libtommath/bcrypt.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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
@@ -32,20 +32,20 @@
#include <wincrypt.h>

static mp_err s_read_wincsp(void *p, size_t n)
{
- static HCRYPTPROV hProv = 0;
- if (hProv == 0) {
- HCRYPTPROV h = 0;
- if (!CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
- (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
- !CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
- CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
+ static BCRYPT_ALG_HANDLE hAlg = 0;
+ if (hAlg == 0) {
+ BCRYPT_ALG_HANDLE h = 0;
+ NTSTATUS status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RSA_ALGORITHM, NULL,
+ BCRYPT_HASH_REUSABLE_FLAG);
+ if(!BCRYPT_SUCCESS(status)) {
return MP_ERR;
}
- hProv = h;
+ hAlg = h;
}
- return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
+ NTSTATUS status = BCryptGenRandom(hAlg, (PUCHAR)p, (ULONG)n, 0);
+ return BCRYPT_SUCCESS(status) ? MP_OKAY : MP_ERR;
}
#endif /* WIN32 */

#if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ)
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -117,8 +117,13 @@
)
target_link_options(${PROJECT_NAME} BEFORE PRIVATE
${LTM_LD_FLAGS}
)
+if(WIN32)
+ target_link_options(${PROJECT_NAME} PRIVATE
+ bcrypt.lib
+ )
+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)
14 changes: 14 additions & 0 deletions ports/libtommath/has-set-double.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c
--- a/bn_mp_set_double.c
+++ b/bn_mp_set_double.c
@@ -2,9 +2,9 @@
#ifdef BN_MP_SET_DOUBLE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

-#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
+#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(_MSC_VER)
mp_err mp_set_double(mp_int *a, double b)
{
uint64_t frac;
int exp;
12 changes: 12 additions & 0 deletions ports/libtommath/import-lib.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/sources.cmake b/sources.cmake
--- a/sources.cmake
+++ b/sources.cmake
@@ -171,4 +171,8 @@
tommath_cutoffs.h
tommath_private.h
tommath_superclass.h
)
+
+if(WIN32)
+ list(APPEND SOURCES tommath.def)
+endif()
21 changes: 21 additions & 0 deletions ports/libtommath/msvc-dce.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
@@ -136,13 +136,17 @@

mp_err s_mp_rand_platform(void *p, size_t n)
{
mp_err err = MP_ERR;
+ #ifndef _MSC_VER
if ((err != MP_OKAY) && MP_HAS(S_READ_ARC4RANDOM)) err = s_read_arc4random(p, n);
+ #endif
if ((err != MP_OKAY) && MP_HAS(S_READ_WINCSP)) err = s_read_wincsp(p, n);
+ #ifndef _MSC_VER
if ((err != MP_OKAY) && MP_HAS(S_READ_GETRANDOM)) err = s_read_getrandom(p, n);
if ((err != MP_OKAY) && MP_HAS(S_READ_URANDOM)) err = s_read_urandom(p, n);
if ((err != MP_OKAY) && MP_HAS(S_READ_LTM_RNG)) err = s_read_ltm_rng(p, n);
+ #endif
return err;
}

#endif
100 changes: 17 additions & 83 deletions ports/libtommath/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,22 @@ vcpkg_from_github(
REF "v${VERSION}"
SHA512 3dbd7053a670afa563a069a9785f1aa4cab14a210bcd05d8fc7db25bd3dcce36b10a3f4f54ca92d75a694f891226f01bdf6ac15bacafeb93a8be6b04c579beb3
HEAD_REF develop
PATCHES
bcrypt.patch
import-lib.patch
has-set-double.patch # Remove in next release.
msvc-dce.patch # This is a won't fix, see https://github.com/libtom/libtommath/blob/develop/s_mp_rand_platform.c#L120-L138
)

if(VCPKG_TARGET_IS_WINDOWS)
vcpkg_check_linkage(ONLY_STATIC_LIBRARY)

if(VCPKG_CRT_LINKAGE STREQUAL "dynamic")
set(CRTFLAG "/MD")
else()
set(CRTFLAG "/MT")
endif()

# Make sure we start from a clean slate
vcpkg_execute_build_process(
COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc clean
WORKING_DIRECTORY ${SOURCE_PATH}
LOGNAME clean-${TARGET_TRIPLET}-dbg
)

#Debug Build
vcpkg_execute_build_process(
COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc CFLAGS="${CRTFLAG}d"
WORKING_DIRECTORY ${SOURCE_PATH}
LOGNAME build-${TARGET_TRIPLET}-dbg
)

file(INSTALL
${SOURCE_PATH}/tommath.lib
DESTINATION ${CURRENT_PACKAGES_DIR}/debug/lib
)

# Clean up necessary to rebuild without debug symbols
vcpkg_execute_build_process(
COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc clean
WORKING_DIRECTORY ${SOURCE_PATH}
LOGNAME clean-${TARGET_TRIPLET}-rel
)

vcpkg_execute_build_process(
COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc CFLAGS="${CRTFLAG}"
WORKING_DIRECTORY ${SOURCE_PATH}
LOGNAME build-${TARGET_TRIPLET}-rel
)

file(INSTALL
${SOURCE_PATH}/tommath.lib
DESTINATION ${CURRENT_PACKAGES_DIR}/lib
)

file(INSTALL
${SOURCE_PATH}/tommath.h
DESTINATION ${CURRENT_PACKAGES_DIR}/include
)
else()
if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic")
set(MAKE_FILE "makefile.shared")
else()
set(MAKE_FILE "makefile")
endif()

vcpkg_execute_build_process(
COMMAND make -f ${MAKE_FILE} clean
WORKING_DIRECTORY ${SOURCE_PATH}
)

vcpkg_execute_build_process(
COMMAND make -j${VCPKG_CONCURRENCY} -f ${MAKE_FILE} PREFIX=${CURRENT_PACKAGES_DIR}/debug COMPILE_DEBUG=1 install
WORKING_DIRECTORY ${SOURCE_PATH}
)
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")

vcpkg_execute_build_process(
COMMAND make -f ${MAKE_FILE} clean
WORKING_DIRECTORY ${SOURCE_PATH}
)

vcpkg_execute_build_process(
COMMAND make -j${VCPKG_CONCURRENCY} -f ${MAKE_FILE} PREFIX=${CURRENT_PACKAGES_DIR} install
WORKING_DIRECTORY ${SOURCE_PATH}
)
endif()

file(INSTALL
${SOURCE_PATH}/LICENSE
DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT} RENAME copyright
)
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
)
vcpkg_cmake_install()
vcpkg_copy_pdbs()
vcpkg_cmake_config_fixup(CONFIG_PATH "lib/cmake/${PORT}")
vcpkg_fixup_pkgconfig()
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
9 changes: 9 additions & 0 deletions ports/libtommath/usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
libtommath provides CMake targets:

find_package(libtommath CONFIG REQUIRED)
target_link_libraries(main PRIVATE libtommath)

libtommath provides pkg-config modules:

# public domain library for manipulating large integer numbers
libtommath
13 changes: 12 additions & 1 deletion ports/libtommath/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"name": "libtommath",
"version": "1.3.0",
"port-version": 1,
"description": "LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.",
"homepage": "https://www.libtom.net/LibTomMath/"
"homepage": "https://www.libtom.net/LibTomMath/",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}

0 comments on commit daa5010

Please sign in to comment.