From 6ad46bf988ff220ef5d9817e8856d4e44a974cd4 Mon Sep 17 00:00:00 2001 From: Falko Strenzke Date: Thu, 4 May 2023 12:57:45 +0200 Subject: [PATCH] Update CMake scripts to support Botan 3 backend selection/search. --- CMakeLists.txt | 14 ++- .../{FindBotan2.cmake => FindBotan.cmake} | 100 ++++++++++-------- src/lib/CMakeLists.txt | 20 +++- src/lib/config.h.in | 1 + src/tests/CMakeLists.txt | 8 +- 5 files changed, 90 insertions(+), 53 deletions(-) rename cmake/Modules/{FindBotan2.cmake => FindBotan.cmake} (53%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb6d40cb41..22f1022a54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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) diff --git a/cmake/Modules/FindBotan2.cmake b/cmake/Modules/FindBotan.cmake similarity index 53% rename from cmake/Modules/FindBotan2.cmake rename to cmake/Modules/FindBotan.cmake index 2708491947..6511355758 100644 --- a/cmake/Modules/FindBotan2.cmake +++ b/cmake/Modules/FindBotan.cmake @@ -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 # ^^^^^^^^^^^^^^^^ @@ -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) diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 086ac57d8e..c8fbdcd1b8 100755 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -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) @@ -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 @@ -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}) @@ -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() diff --git a/src/lib/config.h.in b/src/lib/config.h.in index f8880d55d7..3b31ec99ea 100644 --- a/src/lib/config.h.in +++ b/src/lib/config.h.in @@ -51,6 +51,7 @@ #cmakedefine HAVE__TEMPNAM #cmakedefine CRYPTO_BACKEND_BOTAN +#cmakedefine CRYPTO_BACKEND_BOTAN3 #cmakedefine CRYPTO_BACKEND_OPENSSL #cmakedefine CRYPTO_BACKEND_OPENSSL3 diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 7d2a6b0cfe..026dc09750 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -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) @@ -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