Skip to content

Commit

Permalink
Merge pull request #7480 from gojimmypi/PR-cmake-liboqs-kyber
Browse files Browse the repository at this point in the history
Introduce cmake SET_WOLFSSL_DEFINITIONS; Add Kyber and OQS
  • Loading branch information
dgarske authored May 13, 2024
2 parents a916429 + 216925a commit 56129bd
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 10 deletions.
96 changes: 86 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CMakeList.txt
# CMakeLists.txt
#
# Copyright (C) 2006-2024 wolfSSL Inc.
#
Expand Down Expand Up @@ -545,16 +545,89 @@ add_option(WOLFSSL_OQS
"Enable integration with the OQS (Open Quantum Safe) liboqs library (default: disabled)"
"no" "yes;no")

if (WOLFSSL_OQS)
find_package(OQS)
# Kyber
add_option(WOLFSSL_KYBER
"Enable the wolfSSL PQ Kyber library (default: disabled)"
"no" "yes;no")

if (OQS_FOUND)
list(APPEND WOLFSSL_LINK_LIBS ${OQS_LIBRARY})
list(APPEND WOLFSSL_INCLUDE_DIRS ${OQS_INCLUDE_DIR})
set(HAVE_LIBOQS 1)
list(APPEND WOLFSSL_DEFINITIONS
"-DHAVE_TLS_EXTENSIONS"
"-DHAVE_LIBOQS")
# Experimental features
add_option(WOLFSSL_EXPERIMENTAL
"Enable experimental features (default: disabled)"
"no" "yes;no")

message(STATUS "Looking for WOLFSSL_EXPERIMENTAL")
if (WOLFSSL_EXPERIMENTAL)
message(STATUS "Looking for WOLFSSL_EXPERIMENTAL - found")

# We've enabled the experimental environment, but let's
# check if any experimental features are also enabled:
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 0)

set_wolfssl_definitions("WOLFSSL_EXPERIMENTAL_SETTINGS" RESUlT)

# Checking for experimental feature: OQS
message(STATUS "Looking for WOLFSSL_OQS")
if (WOLFSSL_OQS)
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 1)
message(STATUS "Looking for WOLFSSL_OQS - found")

message(STATUS "Checking OQS")
find_package(OQS)
if (OQS_FOUND)
message(STATUS "Checking OQS - found")
list(APPEND WOLFSSL_LINK_LIBS ${OQS_LIBRARY})
list(APPEND WOLFSSL_INCLUDE_DIRS ${OQS_INCLUDE_DIR})

set_wolfssl_definitions("HAVE_LIBOQS" RESUlT)
set_wolfssl_definitions("HAVE_TLS_EXTENSIONS" RESUlT)
set_wolfssl_definitions("OPENSSL_EXTRA" RESUlT)

else()
message(STATUS "Checking OQS - not found")
message(STATUS "WARNING: WOLFSSL_OQS enabled but not found: OQS_LIBRARY=${OQS_LIBRARY}, OQS_INCLUDE_DIR=${OQS_INCLUDE_DIR} ")
endif()
else()
message(STATUS "Looking for WOLFSSL_OQS - not found")
endif()

# Checking for experimental feature: Kyber
message(STATUS "Looking for WOLFSSL_KYBER")
if (WOLFSSL_KYBER)
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 1)

message(STATUS "Automatically set related requirements for Kyber:")
set_wolfssl_definitions("WOLFSSL_HAVE_KYBER" RESUlT)
set_wolfssl_definitions("WOLFSSL_WC_KYBER" RESUlT)
set_wolfssl_definitions("WOLFSSL_SHA3" RESUlT)
set_wolfssl_definitions("WOLFSSL_SHAKE128" RESUlT)
set_wolfssl_definitions("WOLFSSL_SHAKE256" RESUlT)
message(STATUS "Looking for WOLFSSL_KYBER - found")
else()
message(STATUS "Looking for WOLFSSL_KYBER - not found")
endif()

# Other experimental feature detection can be added here...

# Were any experimental features found? Display a message.
if(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE)
message(STATUS "WOLFSSL_EXPERIMENTAL enabled, experimental features enabled.")
else()
message(STATUS "Warning: WOLFSSL_EXPERIMENTAL enabled, but no experimental features enabled.")
endif()

# Sanity checks
if(WOLFSSL_OQS AND WOLFSSL_KYBER)
message(FATAL_ERROR "Error: cannot enable both WOLFSSL_OQS and WOLFSSL_KYBER at the same time.")
endif()

else()
# Experimental mode not enabled, but were any experimental features enabled? Error out if so:
message(STATUS "Looking for WOLFSSL_EXPERIMENTAL - not found")
if (WOLFSSL_OQS)
message(FATAL_ERROR "Error: WOLFSSL_OQS requires WOLFSSL_EXPERIMENTAL at this time.")
endif()
if(WOLFSSL_KYBER)
message(FATAL_ERROR "Error: WOLFSSL_KYBER requires WOLFSSL_EXPERIMENTAL at this time.")
endif()
endif()

Expand All @@ -571,6 +644,9 @@ endif()
# - Atomic user record layer
# - Public key callbacks
# - Microchip/Atmel CryptoAuthLib
# - XMSS
# - LMS
# - dual-certs

# AES-CBC
add_option("WOLFSSL_AESCBC"
Expand Down
70 changes: 70 additions & 0 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -941,3 +941,73 @@ function(add_to_options_file DEFINITIONS OPTION_FILE)
endif()
endforeach()
endfunction()

# Function: set_wolfssl_definitions
# Parameter: SEARCH_VALUE The string to search for. (e.g. "WOLFSSL_SHA3")
# Returns: RESULT
#
# Searches WOLFSSL_DEFINITIONS for SEARCH_VALUE
# Returns RESULT = 1 (true) if the search value is found
#
# Ensures setting is only added once and prints status messages.
#
# Also sets a parent (global in cmake file) variable by the same name to 1.
#
# See also get_wolfssl_definitions() for query-only.
#
function(set_wolfssl_definitions SEARCH_VALUE RESULT)
if (${SEARCH_VALUE} STREQUAL "")
message(FATAL_ERROR "Function set_wolfssl_definitions cannot have blank SEARCH_VALUE")
endif()

list(FIND WOLFSSL_DEFINITIONS "${SEARCH_VALUE}" pos)
string(SUBSTRING "${SEARCH_VALUE}" 0 2 PREFIX_VALUE)

if ("${PREFIX_VALUE}" STREQUAL "-D")
message(FATAL_ERROR "Do not specify the -D prefix in set_wolfssl_definitions")
endif()

if(${pos} EQUAL -1)
message(STATUS "${SEARCH_VALUE} not found in WOLFSSL_DEFINITIONS.")

message(STATUS "Enabling ${SEARCH_VALUE}")
list(APPEND WOLFSSL_DEFINITIONS "-D${SEARCH_VALUE}")
set(${SEARCH_VALUE} 1 PARENT_SCOPE)
# override_cache("${SEARCH_VALUE}" "yes") # Need to check that value is settable
set(${RESULT} 1 PARENT_SCOPE)
message(STATUS "Enabling ${SEARCH_VALUE} - success")

else()
message(STATUS "${SEARCH_VALUE} found in WOLFSSL_DEFINITIONS.")
set(${RESULT} 0 PARENT_SCOPE)
endif()
endfunction()

# Function: get_wolfssl_definitions
# Parameter: SEARCH_VALUE The string to search for. (e.g. "WOLFSSL_SHA3")
# Returns: RESULT
#
# Searches WOLFSSL_DEFINITIONS for SEARCH_VALUE
# Returns RESULT = 1 (true) if the search value is found
#
# Unlike set_wolfssl_definitions(), this function only queries the WOLFSSL_DEFINITIONS.
#
function(get_wolfssl_definitions SEARCH_VALUE RESULT)
if (${SEARCH_VALUE} STREQUAL "")
message(FATAL_ERROR "Function get_wolfssl_definitions cannot have blank SEARCH_VALUE")
endif()

list(FIND WOLFSSL_DEFINITIONS "${SEARCH_VALUE}" pos)
string(SUBSTRING "${SEARCH_VALUE}" 0 2 PREFIX_VALUE)

if ("${PREFIX_VALUE}" STREQUAL "-D")
message(FATAL_ERROR "Do not specify the -D prefix in get_wolfssl_definitions")
endif()


if(${pos} EQUAL -1)
message(STATUS "${SEARCH_VALUE} not found in WOLFSSL_DEFINITIONS.")
else()
message(STATUS "${SEARCH_VALUE} found in WOLFSSL_DEFINITIONS.")
endif()
endfunction()

0 comments on commit 56129bd

Please sign in to comment.