Skip to content

Commit

Permalink
ARIA cipher cmake (#6600)
Browse files Browse the repository at this point in the history
* ARIA Cipher CMake support
  • Loading branch information
gojimmypi committed Sep 19, 2023
1 parent de3c9f1 commit 5830f92
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ user_settings_asm.h
# MagicCrypto (ARIA Cipher)
MagicCrypto

# CMake build directory
/out
/out_temp

# debian packaging
debian/changelog
debian/control
Expand Down
81 changes: 81 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ endif()

project(wolfssl VERSION 5.6.3 LANGUAGES C ASM)

# Set WOLFSSL_ROOT if not already defined
if ("${WOLFSSL_ROOT}" STREQUAL "")
# we'll assume this CMakeLists.txt is in the root of wolfSSL
if (EXISTS "${CMAKE_SOURCE_DIR}/wolfcrypt/src/")
get_filename_component(WOLFSSL_ROOT "${CMAKE_SOURCE_DIR}" ABSOLUTE)
message(STATUS "Found WOLFSSL_ROOT = ${WOLFSSL_ROOT}")
endif()
else()
message(STATUS "Using predefined WOLFSSL_ROOT = ${WOLFSSL_ROOT}")
endif()

# shared library versioning
# increment if interfaces have been added, removed or changed
set(LIBTOOL_CURRENT 40)
Expand Down Expand Up @@ -531,6 +542,11 @@ if(WOLFSSL_AESCTR AND NOT WOLFSSL_FORTRESS)
"-DWOLFSSL_AES_DIRECT")
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)"
Expand Down Expand Up @@ -1877,6 +1893,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.
Expand Down Expand Up @@ -2001,13 +2020,69 @@ 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 (NOT "$ENV{ARIA_DIR}" STREQUAL "")
message(STATUS "Found Environment variable ARIA_DIR=$ENV{ARIA_DIR}")
if(WOLFSSL_ARIA)
message(STATUS "wolfSSL WOLFSSL_ARIA is enabled")
else()
message(STATUS "wolfSSL WOLFSSL_ARIA is not enabled. To enable, specify a user_settings.h file or run: cmake .. -DWOLFSSL_ARIA=yes")
message(STATUS "Clear the ARIA_DIR environment variable to otherwise suppress this message when not using ARIA ciphers.")
endif()
endif()

# ARIA Check
if(WOLFSSL_ARIA)
message(STATUS "WOLFSSL_ARIA is enabled")

find_package(ARIA)

if(ARIA_FOUND)
message(STATUS "ARIA find_package() success.")
else()
message(FATAL_ERROR "WOLFSSL_ARIA is enabled, but find_package() did not find ARIA MagicCrypto.\n"
"Check ARIA_DIR environment variable and/or copy MagicCrypto directory locally.")
endif()

list(APPEND WOLFSSL_LINK_LIBS "${ARIA_LIB_FILE}")

# The cmake target_include_directories() will complain about local directories,
# so we'll handle MagicCrypto differently when found in wolfssl.
# see below to use include_directories() instead.
if(ARIA_IS_LOCAL)
# there's also a wolfssl port API to include, plus local ARIA include
include_directories("wolfssl/wolfcrypt/port/aria" "MagicCrypto/include")
else()
# see below for target_include_directories() instead
include_directories("wolfssl/wolfcrypt/port/aria")
message(STATUS "ARIA_IS_LOCAL is false, appending ${ARIA_INCLUDE_DIR} to WOLFSSL_INCLUDE_DIRS")
list(APPEND WOLFSSL_INCLUDE_DIRS "${ARIA_INCLUDE_DIR}")
endif()

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")

# ARIA was enabled and we successfully found it.
set(HAVE_ARIA 1)
list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_ARIA")

message(STATUS "ARIA Check: WOLFSSL_LINK_LIBS = ${WOLFSSL_LINK_LIBS}")
endif()

set_target_properties(wolfssl
PROPERTIES
SOVERSION ${LIBTOOL_SO_VERSION}
Expand All @@ -2024,6 +2099,12 @@ target_compile_definitions(wolfssl PUBLIC ${WOLFSSL_DEFINITIONS})
# Include Directories
####################################################

if("${WOLFSSL_INCLUDE_DIRS}" STREQUAL "")
message(STATUS "WOLFSSL_INCLUDE_DIRS is blank. No additional directories will be added.")
else()
message(STATUS "WOLFSSL_INCLUDE_DIRS = ${WOLFSSL_INCLUDE_DIRS}")
endif()

target_include_directories(wolfssl
PUBLIC
$<INSTALL_INTERFACE:include>
Expand Down
49 changes: 48 additions & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,53 @@

To build with debugging use: `cmake .. -DCMAKE_BUILD_TYPE=Debug`.

In the simplest form:

# create a root directory for wolfssl repo
git clone https://github.com/wolfSSL/wolfssl.git
cd wolfssl


# 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 Ciper Suite.

The ARIA cipher needs a 3rd party source binary, typically called
`MagicCrypto.tar.gz`.

The MagicCrypto files can be either copied to the local `wolfssl` directory,
or an environment variable `ARIA_DIR` can be set to point to the location.

Simply having the environment variable or local `MagicCrypto` directory
will not automatically enable the ARIA Ciphers.

To enable ARIA Ciphers in wolfSSL for `CMake`:

# From the root of the wolfSSL repo:

# set to your path
export ARIA_DIR=~/workspace/MagicCrypto

mkdir -p out
pushd out
cmake .. -DWOLFSSL_ARIA=yes
cmake --build .

# View the available ciphers with:
./examples/client/client -e
popd


Windows (Visual Studio)
---
1) Go to this page, download the appropriate Windows installer, and install
Expand All @@ -132,7 +179,7 @@
Windows (command line)
---
1) Open Command Prompt
2) Run the Visual Studio batch to setup command line variables, e.g. C:\Program Files (x86)\Microsoft Visual
2) Run the Visual Studio batch to setup command line variables, e.g. C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat
3) Follow steps in "Unix-based Platforms" above.

Expand Down
7 changes: 7 additions & 0 deletions cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# wolfSSL CMake

This directory contains some supplementary functions for the [CMakeLists.txt](../CMakeLists.txt) in the root.

See also cmake notes in the [INSTALL](../INSTALL) documentation file.


18 changes: 14 additions & 4 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -581,12 +586,17 @@ 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)
Expand Down
2 changes: 2 additions & 0 deletions cmake/include.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
EXTRA_DIST += cmake/README.md
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
108 changes: 108 additions & 0 deletions cmake/modules/FindARIA.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Filename: FindARIA.cmake
#
# Usage:
# find_package(ARIA [REQUIRED] [QUIET])
#
# Once complete this will define:
# ARIA_FOUND - system has ARIA MagicCrypto
# ARIA_INCLUDE_DIR - the include directory containing ARIA
# ARIA_LIBRARY - the path to the libARIA library
# ARIA_IS_LOCAL - optionally indicate the MagicCrypto is found locally in ./MagicCrypto

set(ARIA_INCLUDE_DIR)
set(ARIA_LIB_FILE)
set(ARIA_IS_LOCAL)

# when debugging cmake, ARIA_DIR environment variable can be manually set here:
# set(ENV{ARIA_DIR} "~/MagicCrypto")
# set(ENV{ARIA_DIR} "/mnt/c/workspace/MagicCrypto")
# set(ENV{ARIA_DIR} "c:\\workspace\\MagicCrypto")

# Make sure we have a ARIA_DIR environment variable with the path to MagicCrypto
if ("$ENV{ARIA_DIR}" STREQUAL "")
message(STATUS "The ARIA_DIR environment variable is not defined. Looking for headers in wolfssl/MagicCrypto")
if (EXISTS "${WOLFSSL_ROOT}/MagicCrypto/")
set(ARIA_INCLUDE_DIR "${WOLFSSL_ROOT}/MagicCrypto/include")
message(STATUS "Found ARIA in local MagicCrypto directory ${ARIA_INCLUDE_DIR}")
set(ARIA_IS_LOCAL 1)
else()
message(ERROR "ERROR: FindARIA.cmake missing ARIA_DIR value")
message(STATUS "Please set ARIA_DIR environment variable path to your MagicCrypto or copy to wolfssl/MagicCrypto")
endif()
else()
# If an environment variable is defined, the library CANNOT be in the local wolfssl directory.
# See CMake documentation for target_include_directories()
set(ARIA_IS_LOCAL)
set(ARIA_INCLUDE_DIR "$ENV{ARIA_DIR}/include")
message(STATUS "FindARIA.cmake found ARIA_INCLUDE_DIR = $ENV{ARIA_DIR}")

message(STATUS "Checking environment location: ${ARIA_INCLUDE_DIR} and wolfSSL: ${WOLFSSL_ROOT}")
get_filename_component(dir1 "${ARIA_INCLUDE_DIR}" REALPATH)
get_filename_component(dir2 "${WOLFSSL_ROOT}/MagicCrypto/include" REALPATH)
message(STATUS "Found location dir: ${dir1} and ${dir2}")
if("${dir1}" STREQUAL "${dir2}")
message(STATUS "${ARIA_INCLUDE_DIR} exists within ${WOLFSSL_ROOT}.")
message(STATUS "Setting ARIA_IS_LOCAL flag and using wolfSSL path.")
set(ARIA_IS_LOCAL 1)
set(ARIA_INCLUDE_DIR "${WOLFSSL_ROOT}/MagicCrypto/include")
else()
if(EXISTS "${ARIA_INCLUDE_DIR}")
message(STATUS "Confirmed directory exists: ${ARIA_INCLUDE_DIR}")
else()
message(FATAL_ERROR "Directory not found: ${ARIA_INCLUDE_DIR}")
endif()

message(STATUS "Confirmed ${ARIA_INCLUDE_DIR} is not in local wolfSSL root.")
endif()
endif()

# Check that the appropriate files exist
find_path(ARIA_INCLUDE_DIR NAMES "mcapi.h" )

if (NOT EXISTS "${ARIA_INCLUDE_DIR}/mcapi.h")
message(FATAL_ERROR "File does not exist at ${ARIA_INCLUDE_DIR}/mcapi.h")
endif()

if(NOT EXISTS "${ARIA_INCLUDE_DIR}/mcapi_error.h")
message(FATAL_ERROR "File does not exist at ${ARIA_INCLUDE_DIR}/mcapi_error.h")
endif()

if(NOT EXISTS "${ARIA_INCLUDE_DIR}/mcapi_type.h")
message(FATAL_ERROR "File does not exist at $ARIA_INCLUDE_DIR/mcapi_type.h")
endif()

if(EXISTS "$ENV{ARIA_DIR}/lib/libMagicCrypto.so")
# Found ARIA binary via environment variable
set(ARIA_LIBRARY "MagicCrypto")
set(ARIA_LIB_FILE "$ENV{ARIA_DIR}/lib/libMagicCrypto.so")
message(STATUS "ARIA Check: found libMagicCrypto.so via environment variable.")
message(STATUS "Using ${ARIA_LIB_FILE}")
else()
# Did not find ARIA binary via environment variable, so let's look in the current wolfssl directory
if(EXISTS "${WOLFSSL_ROOT}/MagicCrypto/lib/libMagicCrypto.so")
# Found in the root of wolfssl, in ./MagicCrypto/lib
set(ARIA_LIBRARY "MagicCrypto")
set(ARIA_LIB_FILE "${WOLFSSL_ROOT}/MagicCrypto/lib/libMagicCrypto.so")
message(STATUS "ARIA Check: found libMagicCrypto.so via WOLFSSL_ROOT")
message(STATUS "Using ${ARIA_LIB_FILE}")
else()
# Could not find binary. Give up.
message(ERROR "ARIA Check: could not find libMagicCrypto.so via WOLFSSL_ROOT\n"
"Looked for ${WOLFSSL_ROOT}/MagicCrypto/lib/libMagicCrypto.so")
endif()
endif()

mark_as_advanced(ARIA_INCLUDE_DIR ARIA_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ARIA DEFAULT_MSG ARIA_INCLUDE_DIR ARIA_LIBRARY)

# Some additional optional debugging messages, set to (1) to enable
if(0)
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}")
endif()
Loading

0 comments on commit 5830f92

Please sign in to comment.