From a9fd5eb5315d39bae0520b4ae71e17f523116071 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Wed, 12 Jul 2023 18:00:00 -0700 Subject: [PATCH] ARIA CMake support --- CMakeLists.txt | 60 ++++++++++++++++ README_cmake.md | 92 +++++++++++++++++++++++++ cmake/README.md | 6 ++ cmake/functions.cmake | 20 ++++-- cmake/include.am | 2 + cmake/modules/FindARIA.cmake | 70 +++++++++++++++++++ examples/client/client.c | 4 ++ examples/echoclient/echoclient.c | 3 + examples/echoserver/echoserver.c | 4 ++ examples/server/server.c | 4 ++ wolfcrypt/src/port/aria/aria-crypt.c | 2 + wolfcrypt/src/port/aria/aria-cryptocb.c | 1 + 12 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 README_cmake.md create mode 100644 cmake/README.md create mode 100644 cmake/modules/FindARIA.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 51889ebecc..28887ca46c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -536,6 +536,22 @@ if(WOLFSSL_AESCTR AND NOT WOLFSSL_FORTRESS) "-DWOLFSSL_AES_DIRECT") endif() +# if we find an ARIA_DIR turn on ARIA support +if ("$ENV{ARIA_DIR}" STREQUAL "") + set(HAVE_ARIA 0) + set(WOLFSSL_ARIA 0) +else() + message(STATUS "Found ARIA_DIR = $ENV{ARIA_DIR}") + message(STATUS "Setting HAVE_ARIA, WOLFSSL_ARIA") + set(HAVE_ARIA 1) + set(WOLFSSL_ARIA 1) +endif() + +# ARIA +add_option("WOLFSSL_ARIA" + "Enable wolfSSL ARIA support (default: disabled)" + "no" "yes;no") + # AES-CCM add_option("WOLFSSL_AESCCM" "Enable wolfSSL AES-CCM support (default: disabled)" @@ -1882,6 +1898,9 @@ if (WOLFSSL_CAAM) list(APPEND WOLFSSL_DEFINITIONS "-DWOLFSSL_CAAM") endif() +if (WOLFSSL_ARIA) + list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_ARIA") +endif() # Generates the BUILD_* flags. These control what source files are included in # the library. A series of AM_CONDITIONALs handle this in configure.ac. @@ -2015,13 +2034,45 @@ set(LIB_SOURCES "") # in the *.am files. generate_lib_src_list("${LIB_SOURCES}") if(BUILD_SHARED_LIBS) + message(STATUS "BUILD_SHARED_LIBS enabled: ${LIB_SOURCES}") add_library(wolfssl SHARED ${LIB_SOURCES}) else() + message(STATUS "Static Libs: ${LIB_SOURCES}") add_library(wolfssl STATIC ${LIB_SOURCES}) endif() add_library(wolfssl::wolfssl ALIAS wolfssl) +if(WOLFSSL_ARIA) + find_package(ARIA) + + if (ARIA_FOUND) + # message(STATUS "find_package FOUND ARIA") + else() + message(WARNING "WOLFSSL_ARIA is enabled, but find_package did not find ARIA MagicCrypto") + endif() + + list(APPEND WOLFSSL_LINK_LIBS "${ARIA_LIB_FILE}") + list(APPEND WOLFSSL_INCLUDE_DIRS "${ARIA_INCLUDE_DIR}") + + # there's also a wolfssl port API to include + include_directories("wolfssl/wolfcrypt/port/aria") + add_library(MagicCrypto_lib + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/src/port/aria/aria-crypt.c + ${CMAKE_CURRENT_SOURCE_DIR}/wolfcrypt/src/port/aria/aria-cryptocb.c + ) + + set_target_properties(MagicCrypto_lib PROPERTIES OUTPUT_NAME "MagicCrypto") + target_link_libraries(MagicCrypto_lib wolfssl) + target_compile_options(MagicCrypto_lib PRIVATE "-DHAVE_ARIA") + + set(HAVE_ARIA 1) + list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_ARIA") + + message("ARIA Check: WOLFSSL_LINK_LIBS = ${WOLFSSL_LINK_LIBS}") + message("ARIA Check: WOLFSSL_INCLUDE_DIRS = ${WOLFSSL_INCLUDE_DIRS}") +endif() + set_target_properties(wolfssl PROPERTIES SOVERSION ${LIBTOOL_SO_VERSION} @@ -2297,6 +2348,15 @@ else() endif() endif() +if(NOT BUILD_ARIA) + list(APPEND HEADER_EXCLUDE + "MagicCrypto/include/mcapi.h") + list(APPEND HEADER_EXCLUDE + "MagicCrypto/include/mcapi_error.h") + list(APPEND HEADER_EXCLUDE + "MagicCrypto/include/mcapi_type.h") +endif() + if(NOT BUILD_SP) list(APPEND HEADER_EXCLUDE "wolfssl/wolfcrypt/sp.h") diff --git a/README_cmake.md b/README_cmake.md new file mode 100644 index 0000000000..d983a2f5b2 --- /dev/null +++ b/README_cmake.md @@ -0,0 +1,92 @@ +# wolfSSL CMake notes + +The root of the wolfSSL repo contains a [CMakeLists.txt](./CMakeLists.txt) that can be used to compile the +[examples](examples/README.md). + +In the simplest form: + +```bash +# create a root directory for wolfssl repo +git clone https://github.com/wolfSSL/wolfssl.git +cd wolfssl +./autogen.sh +./configure --enable-all +``` + +See the [documentation](https://www.wolfssl.com/documentation/manuals/wolfssl/chapter02.html) for more details +on the various `./configure` options. + +## Build CMake Examples for Linux + + +```bash +# From the root of the wolfSSL repo: + +mkdir -p out +pushd out +cmake .. +cmake --build . + +# View the available ciphers with: +./examples/client/client -e +popd +``` + +## Build CMake Examples for Windows + +```bash +# From the root of the wolfSSL repo: + + +mkdir -p out +pushd out +cmake .. +cmake --build . + +# View the available ciphers with: +./examples/client/client -e +popd +``` + +## ARIA cipher suite + +The ARIA cipher needs a 3rd party source binary, typically called `MagicCrypto.tar.gz`. + +When debugging, these environment variables may be helpful: + +#### Enable ARIA Cipher for Linux Examples + +```bash +# From the root of the wolfSSL repo: + +# set to your path +export ARIA_DIR=/mnt/c/workspace/MagicCrypto + +mkdir -p out +pushd out +cmake .. +cmake --build . + +# View the available ciphers with: +./examples/client/client -e +popd +``` + +#### Enable ARIA Cipher for Windows Examples + +Unzip your `MagicCrypto.tar.gz`, shown here for `C:\workspace\MagicCrypto` + +```bash +# set to your path +export ARIA_DIR=c:\\workspace\\MagicCrypto + +mkdir -p out +pushd out +cmake .. +cmake --build . + +# View the available ciphers with: +./examples/client/client -e +popd +``` + diff --git a/cmake/README.md b/cmake/README.md new file mode 100644 index 0000000000..b6167ae8e7 --- /dev/null +++ b/cmake/README.md @@ -0,0 +1,6 @@ +# wolfSSL CMake + +This directory contains some supplementary functions for the [CMakeLists.txt](../CMakeLists.txt) in the root. + +See also the [README_cmake.md](../README_cmake.md) file. + diff --git a/cmake/functions.cmake b/cmake/functions.cmake index f0fc48c2e9..e67bf23a72 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -199,6 +199,11 @@ function(generate_build_flags) set(BUILD_DILITHIUM "yes" PARENT_SCOPE) set(BUILD_EXT_KYBER "yes" PARENT_SCOPE) endif() + if(WOLFSSL_ARIA OR WOLFSSL_USER_SETTINGS) + message(STATUS "ARIA functions.cmake found WOLFSSL_ARIA") + # we cannot actually build, as we only have pre-compiled bin + set(BUILD_ARIA "yes" PARENT_SCOPE) + endif() set(BUILD_INLINE ${WOLFSSL_INLINE} PARENT_SCOPE) if(WOLFSSL_OCSP OR WOLFSSL_USER_SETTINGS) set(BUILD_OCSP "yes" PARENT_SCOPE) @@ -618,12 +623,19 @@ function(generate_lib_src_list LIB_SOURCES) wolfcrypt/src/wc_port.c wolfcrypt/src/error.c) + if(BUILD_ARIA) + list(APPEND LIB_SOURCES + wolfcrypt/src/port/aria/aria-crypt.c + wolfcrypt/src/port/aria/aria-cryptocb.c) + endif() + + if(NOT BUILD_FIPS_RAND) - list(APPEND LIB_SOURCES - wolfcrypt/src/wc_encrypt.c - wolfcrypt/src/signature.c - wolfcrypt/src/wolfmath.c) + list(APPEND LIB_SOURCES + wolfcrypt/src/wc_encrypt.c + wolfcrypt/src/signature.c + wolfcrypt/src/wolfmath.c) endif() if(BUILD_MEMORY) diff --git a/cmake/include.am b/cmake/include.am index 52ecdd0e23..0db4da9569 100644 --- a/cmake/include.am +++ b/cmake/include.am @@ -1,4 +1,6 @@ EXTRA_DIST += cmake/Config.cmake.in EXTRA_DIST += cmake/config.in EXTRA_DIST += cmake/functions.cmake +EXTRA_DIST += cmake/modules/FindARIA.cmake EXTRA_DIST += cmake/modules/FindOQS.cmake + diff --git a/cmake/modules/FindARIA.cmake b/cmake/modules/FindARIA.cmake new file mode 100644 index 0000000000..6538281b87 --- /dev/null +++ b/cmake/modules/FindARIA.cmake @@ -0,0 +1,70 @@ +# Filename: FindARIA.cmake +# +# Usage: +# find_package(ARIA [REQUIRED] [QUIET]) +# +# Once done this will define: +# ARIA_FOUND - system has ARIA MAgicCrypt0 +# ARIA_INCLUDE_DIR - the include directory containing ARIA +# ARIA_LIBRARY - the path to the libARIA library +# + +set(ARIA_INCLUDE_DIR) +set(ARIA_LIB_FILE) + +# when debugging cmake, ARIA_DIR environment variable can be manually set here: +# set(ENV{ARIA_DIR} "/mnt/c/workspace/MagicCrypto") +# set(ENV{ARIA_DIR} "c:\\workspace\\MagicCrypto") + +# Make sure we have a ARIA_DIR encironment variable with the path to MagicCrypto +if ("$ENV{ARIA_DIR}" STREQUAL "") + message(ERROR "ERROR: FindARIA.cmake missing ARIA_DIR value") + message(STATUS "Please set ARIA_DIR environment variable path to your MagicCrypto") +else() + set(ARIA_INCLUDE_DIR "$ENV{ARIA_DIR}/include") + message(STATUS "FindARIA.cmake found ARIA_INCLUDE_DIR = $ENV{ARIA_DIR}") + # set(ARIA_LIBRARY "$ENV{ARIA_INCLUDE_DIR}/lib") +endif() + +# Check that the appropriate files exist +find_path(ARIA_INCLUDE_DIR NAMES "mcapi.h" ) + +if (EXISTS "${ARIA_INCLUDE_DIR}/mcapi.h") + # message("Found ${ARIA_INCLUDE_DIR}/mcapi.h") +else() + message(ERROR "File does not exist at ${ARIA_INCLUDE_DIR}/mcapi.h") +endif() + +if(EXISTS "${ARIA_INCLUDE_DIR}/mcapi_error.h") + # message("Found ${ARIA_INCLUDE_DIR}/mcapi_error.h") +else() + message(ERROR "File does not exist at ${ARIA_INCLUDE_DIR}/mcapi_error.h") +endif() + +if(EXISTS "${ARIA_INCLUDE_DIR}/mcapi_type.h") + # message("Found ${ARIA_INCLUDE_DIR}/mcapi_type.h") +else() + message(ERROR "File does not exist at $ARIA_INCLUDE_DIR/mcapi_type.h") +endif() + +# find_library(ARIA_LIBRARY +# NAMES "libMagicCrypto.so" # this is not the library name, nor is it "MagicCrypto" +# HINTS "$ENV{ARIA_DIR}/lib/libMagicCrypto.so") + +if(EXISTS "$ENV{ARIA_DIR}/lib/libMagicCrypto.so") + set(ARIA_LIBRARY "MagicCrypto") + set(ARIA_LIB_FILE "$ENV{ARIA_DIR}/lib/libMagicCrypto.so") + # message(STATUS "ARIA Check: found libMagicCrypto.so via file exists") +endif() + +mark_as_advanced(ARIA_INCLUDE_DIR ARIA_LIBRARY) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(ARIA DEFAULT_MSG ARIA_INCLUDE_DIR ARIA_LIBRARY) + +# message(STATUS "") +# message(STATUS "ARIA Check: FindARIA.cmake") +# message(STATUS "ARIA Check: ARIA_INCLUDE_DIR: ${ARIA_INCLUDE_DIR}") +# message(STATUS "ARIA Check: ARIA_LIBRARY: ${ARIA_LIBRARY}") +# message(STATUS "ARIA Check: ARIA_FOUND: ${ARIA_FOUND}") +# message(STATUS "ARIA Check: CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/examples/client/client.c b/examples/client/client.c index 80fb64a8c4..6eb8b25015 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -76,6 +76,10 @@ static const char *wolfsentry_config_path = NULL; static int devId = INVALID_DEVID; #endif +#ifdef HAVE_ARIA + #include "aria-crypt.h" +#endif + #define DEFAULT_TIMEOUT_SEC 2 #ifndef MAX_NON_BLOCK_SEC #define MAX_NON_BLOCK_SEC 10 diff --git a/examples/echoclient/echoclient.c b/examples/echoclient/echoclient.c index 740af4ed0f..2dba4be022 100644 --- a/examples/echoclient/echoclient.c +++ b/examples/echoclient/echoclient.c @@ -68,6 +68,9 @@ static int devId = INVALID_DEVID; #endif +#ifdef HAVE_ARIA + #include "aria-crypt.h" +#endif void echoclient_test(void* args) { diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index c0e875f6d3..95780ac9da 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -63,6 +63,10 @@ static int devId = INVALID_DEVID; #endif +#ifdef HAVE_ARIA + #include "aria-crypt.h" +#endif + #define SVR_COMMAND_SIZE 256 static void SignalReady(void* args, word16 port) diff --git a/examples/server/server.c b/examples/server/server.c index 6d05e47799..ef7c2f2e79 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -109,6 +109,10 @@ static struct group_info group_id_to_text[] = { static int devId = INVALID_DEVID; #endif +#ifdef HAVE_ARIA + #include "aria-crypt.h" +#endif + #define DEFAULT_TIMEOUT_SEC 2 /* Note on using port 0: if the server uses port 0 to bind an ephemeral port diff --git a/wolfcrypt/src/port/aria/aria-crypt.c b/wolfcrypt/src/port/aria/aria-crypt.c index 502f51af44..cc11d9be70 100644 --- a/wolfcrypt/src/port/aria/aria-crypt.c +++ b/wolfcrypt/src/port/aria/aria-crypt.c @@ -38,6 +38,8 @@ size and a key size of 128, 192, or 256 bits. #include #include #include +#include +#include /* return 0 on success or WC_INIT_E on failure */ int wc_AriaInitCrypt(wc_Aria* aria, MC_ALGID algo) diff --git a/wolfcrypt/src/port/aria/aria-cryptocb.c b/wolfcrypt/src/port/aria/aria-cryptocb.c index b1cf45c41a..9ca63cbf72 100644 --- a/wolfcrypt/src/port/aria/aria-cryptocb.c +++ b/wolfcrypt/src/port/aria/aria-cryptocb.c @@ -39,6 +39,7 @@ size and a key size of 128, 192, or 256 bits. #include #include #include +#include int wc_AriaInit(void) {