Skip to content

Commit 56129bd

Browse files
authored
Merge pull request #7480 from gojimmypi/PR-cmake-liboqs-kyber
Introduce cmake SET_WOLFSSL_DEFINITIONS; Add Kyber and OQS
2 parents a916429 + 216925a commit 56129bd

2 files changed

Lines changed: 156 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# CMakeList.txt
1+
# CMakeLists.txt
22
#
33
# Copyright (C) 2006-2024 wolfSSL Inc.
44
#
@@ -545,16 +545,89 @@ add_option(WOLFSSL_OQS
545545
"Enable integration with the OQS (Open Quantum Safe) liboqs library (default: disabled)"
546546
"no" "yes;no")
547547

548-
if (WOLFSSL_OQS)
549-
find_package(OQS)
548+
# Kyber
549+
add_option(WOLFSSL_KYBER
550+
"Enable the wolfSSL PQ Kyber library (default: disabled)"
551+
"no" "yes;no")
550552

551-
if (OQS_FOUND)
552-
list(APPEND WOLFSSL_LINK_LIBS ${OQS_LIBRARY})
553-
list(APPEND WOLFSSL_INCLUDE_DIRS ${OQS_INCLUDE_DIR})
554-
set(HAVE_LIBOQS 1)
555-
list(APPEND WOLFSSL_DEFINITIONS
556-
"-DHAVE_TLS_EXTENSIONS"
557-
"-DHAVE_LIBOQS")
553+
# Experimental features
554+
add_option(WOLFSSL_EXPERIMENTAL
555+
"Enable experimental features (default: disabled)"
556+
"no" "yes;no")
557+
558+
message(STATUS "Looking for WOLFSSL_EXPERIMENTAL")
559+
if (WOLFSSL_EXPERIMENTAL)
560+
message(STATUS "Looking for WOLFSSL_EXPERIMENTAL - found")
561+
562+
# We've enabled the experimental environment, but let's
563+
# check if any experimental features are also enabled:
564+
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 0)
565+
566+
set_wolfssl_definitions("WOLFSSL_EXPERIMENTAL_SETTINGS" RESUlT)
567+
568+
# Checking for experimental feature: OQS
569+
message(STATUS "Looking for WOLFSSL_OQS")
570+
if (WOLFSSL_OQS)
571+
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 1)
572+
message(STATUS "Looking for WOLFSSL_OQS - found")
573+
574+
message(STATUS "Checking OQS")
575+
find_package(OQS)
576+
if (OQS_FOUND)
577+
message(STATUS "Checking OQS - found")
578+
list(APPEND WOLFSSL_LINK_LIBS ${OQS_LIBRARY})
579+
list(APPEND WOLFSSL_INCLUDE_DIRS ${OQS_INCLUDE_DIR})
580+
581+
set_wolfssl_definitions("HAVE_LIBOQS" RESUlT)
582+
set_wolfssl_definitions("HAVE_TLS_EXTENSIONS" RESUlT)
583+
set_wolfssl_definitions("OPENSSL_EXTRA" RESUlT)
584+
585+
else()
586+
message(STATUS "Checking OQS - not found")
587+
message(STATUS "WARNING: WOLFSSL_OQS enabled but not found: OQS_LIBRARY=${OQS_LIBRARY}, OQS_INCLUDE_DIR=${OQS_INCLUDE_DIR} ")
588+
endif()
589+
else()
590+
message(STATUS "Looking for WOLFSSL_OQS - not found")
591+
endif()
592+
593+
# Checking for experimental feature: Kyber
594+
message(STATUS "Looking for WOLFSSL_KYBER")
595+
if (WOLFSSL_KYBER)
596+
set(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE 1)
597+
598+
message(STATUS "Automatically set related requirements for Kyber:")
599+
set_wolfssl_definitions("WOLFSSL_HAVE_KYBER" RESUlT)
600+
set_wolfssl_definitions("WOLFSSL_WC_KYBER" RESUlT)
601+
set_wolfssl_definitions("WOLFSSL_SHA3" RESUlT)
602+
set_wolfssl_definitions("WOLFSSL_SHAKE128" RESUlT)
603+
set_wolfssl_definitions("WOLFSSL_SHAKE256" RESUlT)
604+
message(STATUS "Looking for WOLFSSL_KYBER - found")
605+
else()
606+
message(STATUS "Looking for WOLFSSL_KYBER - not found")
607+
endif()
608+
609+
# Other experimental feature detection can be added here...
610+
611+
# Were any experimental features found? Display a message.
612+
if(WOLFSSL_FOUND_EXPERIMENTAL_FEATURE)
613+
message(STATUS "WOLFSSL_EXPERIMENTAL enabled, experimental features enabled.")
614+
else()
615+
message(STATUS "Warning: WOLFSSL_EXPERIMENTAL enabled, but no experimental features enabled.")
616+
endif()
617+
618+
# Sanity checks
619+
if(WOLFSSL_OQS AND WOLFSSL_KYBER)
620+
message(FATAL_ERROR "Error: cannot enable both WOLFSSL_OQS and WOLFSSL_KYBER at the same time.")
621+
endif()
622+
623+
else()
624+
# Experimental mode not enabled, but were any experimental features enabled? Error out if so:
625+
message(STATUS "Looking for WOLFSSL_EXPERIMENTAL - not found")
626+
if (WOLFSSL_OQS)
627+
message(FATAL_ERROR "Error: WOLFSSL_OQS requires WOLFSSL_EXPERIMENTAL at this time.")
628+
endif()
629+
if(WOLFSSL_KYBER)
630+
message(FATAL_ERROR "Error: WOLFSSL_KYBER requires WOLFSSL_EXPERIMENTAL at this time.")
558631
endif()
559632
endif()
560633

@@ -571,6 +644,9 @@ endif()
571644
# - Atomic user record layer
572645
# - Public key callbacks
573646
# - Microchip/Atmel CryptoAuthLib
647+
# - XMSS
648+
# - LMS
649+
# - dual-certs
574650

575651
# AES-CBC
576652
add_option("WOLFSSL_AESCBC"

cmake/functions.cmake

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,3 +941,73 @@ function(add_to_options_file DEFINITIONS OPTION_FILE)
941941
endif()
942942
endforeach()
943943
endfunction()
944+
945+
# Function: set_wolfssl_definitions
946+
# Parameter: SEARCH_VALUE The string to search for. (e.g. "WOLFSSL_SHA3")
947+
# Returns: RESULT
948+
#
949+
# Searches WOLFSSL_DEFINITIONS for SEARCH_VALUE
950+
# Returns RESULT = 1 (true) if the search value is found
951+
#
952+
# Ensures setting is only added once and prints status messages.
953+
#
954+
# Also sets a parent (global in cmake file) variable by the same name to 1.
955+
#
956+
# See also get_wolfssl_definitions() for query-only.
957+
#
958+
function(set_wolfssl_definitions SEARCH_VALUE RESULT)
959+
if (${SEARCH_VALUE} STREQUAL "")
960+
message(FATAL_ERROR "Function set_wolfssl_definitions cannot have blank SEARCH_VALUE")
961+
endif()
962+
963+
list(FIND WOLFSSL_DEFINITIONS "${SEARCH_VALUE}" pos)
964+
string(SUBSTRING "${SEARCH_VALUE}" 0 2 PREFIX_VALUE)
965+
966+
if ("${PREFIX_VALUE}" STREQUAL "-D")
967+
message(FATAL_ERROR "Do not specify the -D prefix in set_wolfssl_definitions")
968+
endif()
969+
970+
if(${pos} EQUAL -1)
971+
message(STATUS "${SEARCH_VALUE} not found in WOLFSSL_DEFINITIONS.")
972+
973+
message(STATUS "Enabling ${SEARCH_VALUE}")
974+
list(APPEND WOLFSSL_DEFINITIONS "-D${SEARCH_VALUE}")
975+
set(${SEARCH_VALUE} 1 PARENT_SCOPE)
976+
# override_cache("${SEARCH_VALUE}" "yes") # Need to check that value is settable
977+
set(${RESULT} 1 PARENT_SCOPE)
978+
message(STATUS "Enabling ${SEARCH_VALUE} - success")
979+
980+
else()
981+
message(STATUS "${SEARCH_VALUE} found in WOLFSSL_DEFINITIONS.")
982+
set(${RESULT} 0 PARENT_SCOPE)
983+
endif()
984+
endfunction()
985+
986+
# Function: get_wolfssl_definitions
987+
# Parameter: SEARCH_VALUE The string to search for. (e.g. "WOLFSSL_SHA3")
988+
# Returns: RESULT
989+
#
990+
# Searches WOLFSSL_DEFINITIONS for SEARCH_VALUE
991+
# Returns RESULT = 1 (true) if the search value is found
992+
#
993+
# Unlike set_wolfssl_definitions(), this function only queries the WOLFSSL_DEFINITIONS.
994+
#
995+
function(get_wolfssl_definitions SEARCH_VALUE RESULT)
996+
if (${SEARCH_VALUE} STREQUAL "")
997+
message(FATAL_ERROR "Function get_wolfssl_definitions cannot have blank SEARCH_VALUE")
998+
endif()
999+
1000+
list(FIND WOLFSSL_DEFINITIONS "${SEARCH_VALUE}" pos)
1001+
string(SUBSTRING "${SEARCH_VALUE}" 0 2 PREFIX_VALUE)
1002+
1003+
if ("${PREFIX_VALUE}" STREQUAL "-D")
1004+
message(FATAL_ERROR "Do not specify the -D prefix in get_wolfssl_definitions")
1005+
endif()
1006+
1007+
1008+
if(${pos} EQUAL -1)
1009+
message(STATUS "${SEARCH_VALUE} not found in WOLFSSL_DEFINITIONS.")
1010+
else()
1011+
message(STATUS "${SEARCH_VALUE} found in WOLFSSL_DEFINITIONS.")
1012+
endif()
1013+
endfunction()

0 commit comments

Comments
 (0)