Skip to content

Commit

Permalink
Update CMake scripts to support Botan 3 backend selection/search.
Browse files Browse the repository at this point in the history
  • Loading branch information
falko-strenzke authored and ni4 committed Feb 23, 2024
1 parent b9c157a commit 6ad46bf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ if (NOT CRYPTO_BACKEND)
endif()
string(TOLOWER ${CRYPTO_BACKEND} CRYPTO_BACKEND_LOWERCASE)
if(CRYPTO_BACKEND_LOWERCASE STREQUAL "botan")
# Default value;
# Default value; version 2 or 3 of Botan
set(CRYPTO_BACKEND_BOTAN 1)
elseif(CRYPTO_BACKEND_LOWERCASE STREQUAL "botan3")
set(CRYPTO_BACKEND "botan")
set(CRYPTO_BACKEND_LOWERCASE "botan")
# Require version 3 of Botan
set(CRYPTO_BACKEND_BOTAN 1)
set(CRYPTO_BACKEND_BOTAN3 1)
elseif(CRYPTO_BACKEND_LOWERCASE STREQUAL "openssl")
set(CRYPTO_BACKEND_OPENSSL 1)
else()
Expand Down Expand Up @@ -135,7 +141,11 @@ endif(WIN32)

# set a few other things at the top level to prevent incompatibilities
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
if(NOT CRYPTO_BACKEND_BOTAN3)
set(CMAKE_CXX_STANDARD 11)
else()
set(CMAKE_CXX_STANDARD 20)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_definitions(-D_GNU_SOURCE)
Expand Down
100 changes: 57 additions & 43 deletions cmake/Modules/FindBotan2.cmake → cmake/Modules/FindBotan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
# POSSIBILITY OF SUCH DAMAGE.

#.rst:
# FindBotan2
# FindBotan
# -----------
#
# Find the botan-2 library.
# Find the botan-2 or botan-3 library.
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` targets:
#
# ``Botan2::Botan2``
# The botan-2 library, if found.
# ``Botan::Botan``
# The botan-2 or botan-3 library, if found.
#
# Result variables
# ^^^^^^^^^^^^^^^^
Expand All @@ -43,89 +43,103 @@
#
# ::
#
# BOTAN2_FOUND - true if the headers and library were found
# BOTAN2_INCLUDE_DIRS - where to find headers
# BOTAN2_LIBRARIES - list of libraries to link
# BOTAN2_VERSION - library version that was found, if any
# BOTAN_FOUND - true if the headers and library were found
# BOTAN_INCLUDE_DIRS - where to find headers
# BOTAN_LIBRARIES - list of libraries to link
# BOTAN_VERSION - library version that was found, if any

# use pkg-config to get the directories and then use these values
# in the find_path() and find_library() calls

find_package(PkgConfig QUIET)
pkg_check_modules(PC_BOTAN2 QUIET botan-2)

# Search for the version 2 first unless version 3 requested
if(NOT "${Botan_FIND_VERSION_MAJOR}" EQUAL "3")
pkg_check_modules(PC_BOTAN QUIET botan-2)
set(_suffixes "botan-2" "botan-3")
set(_names "botan-2" "libbotan-2" "botan-3" "libbotan-3")
else()
set(_suffixes "botan-3")
set(_names "botan-3" "libbotan-3")
endif()
if(NOT PC_BOTAN_FOUND)
pkg_check_modules(PC_BOTAN QUIET botan-3)
endif()

# find the headers
find_path(BOTAN2_INCLUDE_DIR
find_path(BOTAN_INCLUDE_DIR
NAMES botan/version.h
HINTS
${PC_BOTAN2_INCLUDEDIR}
${PC_BOTAN2_INCLUDE_DIRS}
PATH_SUFFIXES botan-2
${PC_BOTAN_INCLUDEDIR}
${PC_BOTAN_INCLUDE_DIRS}
PATH_SUFFIXES ${_suffixes}
)

# find the library
if(MSVC)
find_library(BOTAN2_LIBRARY
find_library(BOTAN_LIBRARY
NAMES botan
HINTS
${PC_BOTAN2_LIBDIR}
${PC_BOTAN2_LIBRARY_DIRS}
${PC_BOTAN_LIBDIR}
${PC_BOTAN_LIBRARY_DIRS}
)
else()
find_library(BOTAN2_LIBRARY
NAMES botan-2 libbotan-2
find_library(BOTAN_LIBRARY
NAMES
${_names}
HINTS
${PC_BOTAN2_LIBDIR}
${PC_BOTAN2_LIBRARY_DIRS}
${PC_BOTAN_LIBDIR}
${PC_BOTAN_LIBRARY_DIRS}
)
endif()

# determine the version
if(PC_BOTAN2_VERSION)
set(BOTAN2_VERSION ${PC_BOTAN2_VERSION})
elseif(BOTAN2_INCLUDE_DIR AND EXISTS "${BOTAN2_INCLUDE_DIR}/botan/build.h")
file(STRINGS "${BOTAN2_INCLUDE_DIR}/botan/build.h" botan2_version_str
if(PC_BOTAN_VERSION)
set(BOTAN_VERSION ${PC_BOTAN_VERSION})
elseif(BOTAN_INCLUDE_DIR AND EXISTS "${BOTAN_INCLUDE_DIR}/botan/build.h")
file(STRINGS "${BOTAN_INCLUDE_DIR}/botan/build.h" botan_version_str
REGEX "^#define[\t ]+(BOTAN_VERSION_[A-Z]+)[\t ]+[0-9]+")

string(REGEX REPLACE ".*#define[\t ]+BOTAN_VERSION_MAJOR[\t ]+([0-9]+).*"
"\\1" _botan2_version_major "${botan2_version_str}")
"\\1" _botan_version_major "${botan_version_str}")
string(REGEX REPLACE ".*#define[\t ]+BOTAN_VERSION_MINOR[\t ]+([0-9]+).*"
"\\1" _botan2_version_minor "${botan2_version_str}")
"\\1" _botan_version_minor "${botan_version_str}")
string(REGEX REPLACE ".*#define[\t ]+BOTAN_VERSION_PATCH[\t ]+([0-9]+).*"
"\\1" _botan2_version_patch "${botan2_version_str}")
set(BOTAN2_VERSION "${_botan2_version_major}.${_botan2_version_minor}.${_botan2_version_patch}"
"\\1" _botan_version_patch "${botan_version_str}")
set(BOTAN_VERSION "${_botan_version_major}.${_botan_version_minor}.${_botan_version_patch}"
CACHE INTERNAL "The version of Botan which was detected")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Botan2
REQUIRED_VARS BOTAN2_LIBRARY BOTAN2_INCLUDE_DIR
VERSION_VAR BOTAN2_VERSION
find_package_handle_standard_args(Botan
REQUIRED_VARS BOTAN_LIBRARY BOTAN_INCLUDE_DIR
VERSION_VAR BOTAN_VERSION
)

if (BOTAN2_FOUND)
set(BOTAN2_INCLUDE_DIRS ${BOTAN2_INCLUDE_DIR} ${PC_BOTAN2_INCLUDE_DIRS})
set(BOTAN2_LIBRARIES ${BOTAN2_LIBRARY})
if (BOTAN_FOUND)
set(BOTAN_INCLUDE_DIRS ${BOTAN_INCLUDE_DIR} ${PC_BOTAN_INCLUDE_DIRS})
set(BOTAN_LIBRARIES ${BOTAN_LIBRARY})
endif()

if (BOTAN2_FOUND AND NOT TARGET Botan2::Botan2)
if (BOTAN_FOUND AND NOT TARGET Botan::Botan)
# create the new library target
add_library(Botan2::Botan2 UNKNOWN IMPORTED)
add_library(Botan::Botan UNKNOWN IMPORTED)
# set the required include dirs for the target
if (BOTAN2_INCLUDE_DIRS)
set_target_properties(Botan2::Botan2
if (BOTAN_INCLUDE_DIRS)
set_target_properties(Botan::Botan
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${BOTAN2_INCLUDE_DIRS}"
INTERFACE_INCLUDE_DIRECTORIES "${BOTAN_INCLUDE_DIRS}"
)
endif()
# set the required libraries for the target
if (EXISTS "${BOTAN2_LIBRARY}")
set_target_properties(Botan2::Botan2
if (EXISTS "${BOTAN_LIBRARY}")
set_target_properties(Botan::Botan
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${BOTAN2_LIBRARY}"
IMPORTED_LOCATION "${BOTAN_LIBRARY}"
)
endif()
endif()

mark_as_advanced(BOTAN2_INCLUDE_DIR BOTAN2_LIBRARY)
mark_as_advanced(BOTAN_INCLUDE_DIR BOTAN_LIBRARY)

20 changes: 15 additions & 5 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ find_package(ZLIB REQUIRED)

# required packages
find_package(JSON-C 0.11 REQUIRED)
if (CRYPTO_BACKEND_BOTAN)
find_package(Botan2 2.14.0 REQUIRED)
if (CRYPTO_BACKEND_BOTAN3)
find_package(Botan 3.0.0 REQUIRED)
elseif (CRYPTO_BACKEND_BOTAN)
find_package(Botan 2.14.0 REQUIRED)
if(BOTAN_VERSION VERSION_GREATER_EQUAL 3.0.0)
set(CRYPTO_BACKEND_BOTAN3 1)
endif()
endif()
if (CRYPTO_BACKEND_OPENSSL)
include(FindOpenSSL)
Expand Down Expand Up @@ -129,7 +134,7 @@ endfunction()

if(CRYPTO_BACKEND_BOTAN)
# check botan's enabled features
set(CMAKE_REQUIRED_INCLUDES "${BOTAN2_INCLUDE_DIRS}")
set(CMAKE_REQUIRED_INCLUDES "${BOTAN_INCLUDE_DIRS}")
set(_botan_required_features
# base
BIGINT FFI HEX_CODEC PGP_S2K
Expand All @@ -142,12 +147,17 @@ if(CRYPTO_BACKEND_BOTAN)
# hash
CRC24 HASH MD5 SHA1 SHA2_32 SHA2_64 SHA3
# public-key core
DL_GROUP DL_PUBLIC_KEY_FAMILY ECC_GROUP ECC_PUBLIC_KEY_CRYPTO PUBLIC_KEY_CRYPTO
DL_GROUP ECC_GROUP ECC_PUBLIC_KEY_CRYPTO PUBLIC_KEY_CRYPTO # Botan-2: DL_PUBLIC_KEY_FAMILY Botan-3: DL_SCHEME, see switch below
# public-key algs
CURVE_25519 DSA ECDH ECDSA ED25519 ELGAMAL RSA
# public-key operations etc
EME_PKCS1v15 EMSA_PKCS1 EMSA_RAW KDF_BASE RFC3394_KEYWRAP SP800_56A
)
if(BOTAN_VERSION VERSION_LESS 3.0.0)
set(_botan_required_features ${_botan_required_features} DL_PUBLIC_KEY_FAMILY)
else()
set(_botan_required_features ${_botan_required_features} DL_SCHEME RAW_HASH_FN)
endif()
foreach(feature ${_botan_required_features})
check_cxx_symbol_exists("BOTAN_HAS_${feature}" botan/build.h _botan_has_${feature})
if (NOT _botan_has_${feature})
Expand Down Expand Up @@ -323,7 +333,7 @@ target_include_directories(librnp-obj
)
target_link_libraries(librnp-obj PRIVATE JSON-C::JSON-C)
if (CRYPTO_BACKEND_BOTAN)
target_link_libraries(librnp-obj PRIVATE Botan2::Botan2)
target_link_libraries(librnp-obj PRIVATE Botan::Botan)
elseif (CRYPTO_BACKEND_OPENSSL)
target_link_libraries(librnp-obj PRIVATE OpenSSL::Crypto)
endif()
Expand Down
1 change: 1 addition & 0 deletions src/lib/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#cmakedefine HAVE__TEMPNAM

#cmakedefine CRYPTO_BACKEND_BOTAN
#cmakedefine CRYPTO_BACKEND_BOTAN3
#cmakedefine CRYPTO_BACKEND_OPENSSL
#cmakedefine CRYPTO_BACKEND_OPENSSL3

Expand Down
8 changes: 5 additions & 3 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ else()
endif()

find_package(JSON-C 0.11 REQUIRED)
if (CRYPTO_BACKEND_LOWERCASE STREQUAL "botan")
find_package(Botan2 2.14.0 REQUIRED)
if (CRYPTO_BACKEND_BOTAN3)
find_package(Botan 3.0.0 REQUIRED)
elseif (CRYPTO_BACKEND_BOTAN)
find_package(Botan 2.14.0 REQUIRED)
endif()
if (CRYPTO_BACKEND_LOWERCASE STREQUAL "openssl")
find_package(OpenSSL 1.1.1 REQUIRED)
Expand Down Expand Up @@ -170,7 +172,7 @@ target_include_directories(rnp_tests
PRIVATE
"${PROJECT_SOURCE_DIR}/src"
"${PROJECT_SOURCE_DIR}/src/lib"
"${BOTAN2_INCLUDE_DIRS}"
"${BOTAN_INCLUDE_DIRS}"
)
target_link_libraries(rnp_tests
PRIVATE
Expand Down

0 comments on commit 6ad46bf

Please sign in to comment.