Skip to content

Commit

Permalink
Update build for OpenEMV common crypto submodule
Browse files Browse the repository at this point in the history
* Allow parent project to provide crypto submodule
* Allow crypto submodule to provide CMake config file dependencies
* Allow crypto submodule to provide pkgconfig file dependencies
* Remove CMake modules used to download and find MbedTLS. These are
  provided by the crypto submodule.
* Remove MbedTLS and OpenSSL checks, configs and linking. The crypto
  submodule will download/build/use an underlying crypto implementation.
* Remove crypto implementation definitions from generated config header
  • Loading branch information
leonlynch committed Apr 15, 2022
1 parent 77c3dcc commit 25beab4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 180 deletions.
30 changes: 27 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ project(tr31
LANGUAGES C
)

# Determine whether this project is the top-level project
if(${CMAKE_VERSION} VERSION_LESS "3.21")
get_directory_property(TR31_HAS_PARENT PARENT_DIRECTORY)
if(NOT TR31_HAS_PARENT)
set(TR31_IS_TOP_LEVEL True)
endif()
else()
# CMake >=3.21 provides <PROJECT-NAME>_IS_TOP_LEVEL
set(TR31_IS_TOP_LEVEL ${tr31_IS_TOP_LEVEL})
endif()

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
Expand All @@ -26,7 +37,17 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Mo

include(CTest)

add_subdirectory(crypto)
# Allow parent scope to provide crypto targets when not building shared libs
if(TR31_IS_TOP_LEVEL OR BUILD_SHARED_LIBS)
add_subdirectory(crypto EXCLUDE_FROM_ALL)
add_subdirectory(crypto/test)
elseif (NOT TARGET crypto_tdes OR
NOT TARGET crypto_aes OR
NOT TARGET crypto_mem OR
NOT TARGET crypto_rand)
message(FATAL_ERROR "Parent project must provide crypto libraries for static builds")
endif()

add_subdirectory(src)
add_subdirectory(test)

Expand All @@ -42,7 +63,8 @@ install(FILES

# generate and install basic CMake config files
include(CMakePackageConfigHelpers) # provides CMake config generator macros
foreach(pkg IN LISTS TR31_PACKAGE_DEPENDENCIES)
# NOTE: crypto subdirectory provides CRYPTO_PACKAGE_DEPENDENCIES
foreach(pkg IN LISTS CRYPTO_PACKAGE_DEPENDENCIES)
# build dependency string for use in CMake config file
string(APPEND TR31_CONFIG_PACKAGE_DEPENDENCIES "find_dependency(${pkg})\n")
endforeach()
Expand Down Expand Up @@ -77,7 +99,9 @@ export(EXPORT tr31Targets
set(TR31_INSTALL_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE STRING "Installation location for tr31 pkgconfig files")
message(STATUS "Using pkgconfig install location \"${TR31_INSTALL_PKGCONFIG_DIR}\"")
set(TR31_PKGCONFIG_LIB_NAME ${CMAKE_PROJECT_NAME})
# NOTE: subdirectories will provide TR31_PKGCONFIG_REQ_PRIV and TR31_PKGCONFIG_LIBS_PRIV
# NOTE: crypto subdirectory provides CRYPTO_PKGCONFIG_REQ_PRIV and CRYPTO_PKGCONFIG_LIBS_PRIV
set(TR31_PKGCONFIG_REQ_PRIV ${CRYPTO_PKGCONFIG_REQ_PRIV})
set(TR31_PKGCONFIG_LIBS_PRIV ${CRYPTO_PKGCONFIG_LIBS_PRIV})
configure_file(pkgconfig/libtr31.pc.in
"${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libtr31.pc"
@ONLY
Expand Down
47 changes: 0 additions & 47 deletions cmake/Modules/FetchMbedTLS.cmake

This file was deleted.

93 changes: 0 additions & 93 deletions cmake/Modules/FindMbedTLS.cmake

This file was deleted.

34 changes: 0 additions & 34 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,6 @@

cmake_minimum_required(VERSION 3.16)

find_package(MbedTLS 2.16) # Latest version available on Ubuntu 20.04 and Fedora 34
option(FETCH_MBEDTLS "Download and build MbedTLS")
if(NOT MbedTLS_FOUND AND FETCH_MBEDTLS)
include(FetchMbedTLS)
endif()

find_package(OpenSSL 1.1 COMPONENTS Crypto)

# choose crypto implementation and inform parent scope
if(NOT MbedTLS_FOUND AND NOT OpenSSL_FOUND)
message(FATAL_ERROR "Either MbedTLS or OpenSSL is required")
endif()
if(MbedTLS_FOUND)
# prefer MbedTLS
message(STATUS "Using MbedTLS")
set(USE_MBEDTLS TRUE)
list(APPEND TR31_PACKAGE_DEPENDENCIES "MbedTLS 2.16")
set(TR31_PACKAGE_DEPENDENCIES ${TR31_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
# NOTE: MbedTLS has no pkgconfig file so TR31_PKGCONFIG_REQ_PRIV cannot be set
set(TR31_PKGCONFIG_LIBS_PRIV "-lmbedcrypto" PARENT_SCOPE)
else()
message(STATUS "Using OpenSSL")
set(USE_OPENSSL TRUE)
list(APPEND TR31_PACKAGE_DEPENDENCIES "OpenSSL 1.1 COMPONENTS Crypto")
set(TR31_PACKAGE_DEPENDENCIES ${TR31_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
set(TR31_PKGCONFIG_REQ_PRIV "libcrypto" PARENT_SCOPE)
set(TR31_PKGCONFIG_LIBS_PRIV "-lcrypto" PARENT_SCOPE)
endif()

include(CheckFunctionExists)
check_function_exists("argp_parse" argp_FOUND)
option(FETCH_ARGP "Download and build argp-standalone")
Expand Down Expand Up @@ -71,11 +42,6 @@ target_include_directories(tr31 INTERFACE
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)
target_link_libraries(tr31 PRIVATE crypto_tdes crypto_aes crypto_mem crypto_rand)
if(MbedTLS_FOUND)
target_link_libraries(tr31 PRIVATE MbedTLS::mbedcrypto)
elseif(OpenSSL_FOUND)
target_link_libraries(tr31 PRIVATE OpenSSL::Crypto)
endif()

# TR-31 command line tool
add_executable(tr31-tool tr31-tool.c)
Expand Down
2 changes: 0 additions & 2 deletions src/tr31_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
#define TR31_CONFIG_H

#define TR31_LIB_VERSION_STRING "@CMAKE_PROJECT_VERSION@"
#cmakedefine USE_MBEDTLS
#cmakedefine USE_OPENSSL

#endif
2 changes: 1 addition & 1 deletion src/tr31_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
*/

#include "tr31_crypto.h"
#include "tr31_config.h"
#include "tr31.h"

#include "crypto_tdes.h"
#include "crypto_aes.h"
#include "crypto_mem.h"

#include <stdint.h>
#include <string.h>

#define TR31_KBEK_VARIANT_XOR (0x45)
Expand Down

0 comments on commit 25beab4

Please sign in to comment.