LibCrypto: Add libtommath vcpkg dependency and link it

An overlay port is required to add the `stdc-iec-559` and `install-pc`
patches.

The `stdc-iec-559` patch is required because Clang doesn't define
`__STDC_IEC_559__`. However, glibc and musl define it if `__GCC_IEC_559`
is not defined. The macro is taken from glibc source code.

The `install-pc` patch is required because libtommath doesn't install
the pkg-config files when building statically compromising our ability
to find it during build.

Clang: https://clang.llvm.org/c_status.html#:~:text=Yes-,
IEC%2060559%20support,-Unknown
glibc: https://sourceware.org/git/?p=glibc.git;a=blob;
f=include/stdc-predef.h
This commit is contained in:
devgianlu 2025-04-26 11:53:09 +02:00 committed by Jelle Raaijmakers
commit 915c6fdcf8
Notes: github-actions[bot] 2025-05-23 09:58:29 +00:00
11 changed files with 208 additions and 0 deletions

View file

@ -34,5 +34,9 @@ set(SOURCES
serenity_lib(LibCrypto crypto)
target_link_libraries(LibCrypto PRIVATE LibCore)
find_package(PkgConfig REQUIRED)
pkg_check_modules(libtommath REQUIRED IMPORTED_TARGET libtommath)
target_link_libraries(LibCrypto PRIVATE PkgConfig::libtommath)
find_package(OpenSSL REQUIRED)
target_link_libraries(LibCrypto PUBLIC OpenSSL::Crypto)

View file

@ -0,0 +1,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)

View file

@ -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;

View file

@ -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()

View file

@ -0,0 +1,12 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -168,7 +168,7 @@ install(TARGETS ${PROJECT_NAME}
)
# Install libtommath.pc for pkg-config if we build a shared library
-if(BUILD_SHARED_LIBS)
+if(BUILD_SHARED_LIBS OR TRUE)
# Let the user override the default directory of the pkg-config file (usually this shouldn't be required to be changed)
set(CMAKE_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Folder where to install .pc files")

View file

@ -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

View file

@ -0,0 +1,27 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO libtom/libtommath
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
stdc-iec-559.patch # Define __STDC_IEC_559__ like glibc and musl do.
install-pc.patch # Always install pkgconfig file, even when building statically.
)
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")

View file

@ -0,0 +1,20 @@
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
@@ -3,6 +3,16 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
+#ifdef __GCC_IEC_559
+# if __GCC_IEC_559 > 0
+# define __STDC_IEC_559__ 1
+# define __STDC_IEC_60559_BFP__ 201404L
+# endif
+#else
+# define __STDC_IEC_559__ 1
+# define __STDC_IEC_60559_BFP__ 201404L
+#endif
+
#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(_MSC_VER)
mp_err mp_set_double(mp_int *a, double b)
{

View file

@ -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

View file

@ -0,0 +1,17 @@
{
"name": "libtommath",
"version": "1.3.0",
"port-version": 2,
"description": "LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.",
"homepage": "https://www.libtom.net/LibTomMath/",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}

View file

@ -70,6 +70,7 @@
"dav1d"
]
},
"libtommath",
{
"name": "libwebp",
"features": [
@ -172,6 +173,10 @@
"name": "libproxy",
"version": "0.4.18#3"
},
{
"name": "libtommath",
"version": "1.3.0#2"
},
{
"name": "libavif",
"version": "1.0.4#1"