From 65f01309816932ea49ad3f98874519f0bb615f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Frauenschl=C3=A4ger?= Date: Thu, 30 Nov 2023 10:01:32 +0100 Subject: [PATCH] Zephyr RTOS support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds initial support for the zephyr operating system. Some minor changes to the library build system have been made for it to be compilable with zephyr. Furthermore, we added support for an externally defined RNG method. Signed-off-by: Tobias Frauenschläger --- .CMake/compiler_opts.cmake | 2 +- CMakeLists.txt | 11 +++++-- src/CMakeLists.txt | 6 ++-- src/common/rand/rand.c | 7 +++++ src/oqsconfig.h.cmake | 2 ++ zephyr/CMakeLists.txt | 60 ++++++++++++++++++++++++++++++++++++++ zephyr/Kconfig | 6 ++++ zephyr/module.yml | 3 ++ zephyr/zephyr_rng.c | 20 +++++++++++++ 9 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 zephyr/CMakeLists.txt create mode 100644 zephyr/Kconfig create mode 100644 zephyr/module.yml create mode 100644 zephyr/zephyr_rng.c diff --git a/.CMake/compiler_opts.cmake b/.CMake/compiler_opts.cmake index 98fe0b72b7..083529b32c 100644 --- a/.CMake/compiler_opts.cmake +++ b/.CMake/compiler_opts.cmake @@ -85,7 +85,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU") add_compile_options(${OQS_OPT_FLAG}) # If this is not a dist build we also need to set the OQS_USE_[EXTENSION] flags - if(NOT ${OQS_DIST_BUILD}) + if(NOT ${OQS_DIST_BUILD} AND NOT CMAKE_CROSSCOMPILING) include(${CMAKE_CURRENT_LIST_DIR}/gcc_clang_intrinsics.cmake) endif() endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 03912599d1..4593ddfc11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,18 +26,25 @@ option(OQS_BUILD_ONLY_LIB "Build only liboqs and do not expose build targets for set(OQS_MINIMAL_BUILD "" CACHE STRING "Only build specifically listed algorithms.") option(OQS_PERMIT_UNSUPPORTED_ARCHITECTURE "Permit compilation on an an unsupported architecture." OFF) option(OQS_STRICT_WARNINGS "Enable all compiler warnings." OFF) +option(OQS_POSITION_INDEPENDENT_CODE "Generate Position Independent Code (-fPIC)" ON) +option(OQS_USE_EXTERNAL_RNG "Use an external RNG instead of the default system RNG." OFF) set(OQS_OPT_TARGET auto CACHE STRING "The target microarchitecture for optimization.") set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_C_VISIBILITY_PRESET hidden) set(OQS_VERSION_TEXT "0.10.0-dev") set(OQS_COMPILE_BUILD_TARGET "${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_HOST_SYSTEM}") set(OQS_MINIMAL_GCC_VERSION "7.1.0") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +if(OQS_POSITION_INDEPENDENT_CODE) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +else() + set(CMAKE_POSITION_INDEPENDENT_CODE OFF) +endif() + # heuristic check to see whether we're running on a RaspberryPi if(EXISTS "/opt/vc/include/bcm_host.h") add_definitions( -DOQS_USE_RASPBERRY_PI ) @@ -61,7 +68,7 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8") if(${OQS_DIST_BUILD}) set(OQS_DIST_ARM64_V8_BUILD ON) endif() -elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armel|armhf|armv7|arm32v7") +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armel|armhf|armv7|arm32v7|arm") set(ARCH "arm32v7") set(ARCH_ARM32v7 ON) if(${OQS_DIST_BUILD}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e31a27f61c..6d867097fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,12 +70,12 @@ target_include_directories(oqs ) set_target_properties(oqs PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib" + LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib" VERSION ${OQS_VERSION_TEXT} SOVERSION 5 # For Windows DLLs - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin") configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/liboqsConfig.cmake" diff --git a/src/common/rand/rand.c b/src/common/rand/rand.c index 92ea271d19..3043c62cdb 100644 --- a/src/common/rand/rand.c +++ b/src/common/rand/rand.c @@ -22,6 +22,9 @@ void OQS_randombytes_nist_kat(uint8_t *random_array, size_t bytes_to_read); #ifdef OQS_USE_OPENSSL void OQS_randombytes_openssl(uint8_t *random_array, size_t bytes_to_read); #endif +#ifdef OQS_USE_EXTERNAL_RNG +extern void OQS_randombytes_external(uint8_t *random_array, size_t bytes_to_read); +#endif #ifdef OQS_USE_OPENSSL #include @@ -76,6 +79,10 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { exit(EXIT_FAILURE); } } +#elif defined(OQS_USE_EXTERNAL_RNG) +void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { + OQS_randombytes_external(random_array, bytes_to_read); +} #else void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { FILE *handle; diff --git a/src/oqsconfig.h.cmake b/src/oqsconfig.h.cmake index c68c068c28..606873ed95 100644 --- a/src/oqsconfig.h.cmake +++ b/src/oqsconfig.h.cmake @@ -25,6 +25,8 @@ #cmakedefine OQS_USE_SHA2_OPENSSL 1 #cmakedefine OQS_USE_SHA3_OPENSSL 1 +#cmakedefine OQS_USE_EXTERNAL_RNG 1 + #cmakedefine OQS_USE_ADX_INSTRUCTIONS 1 #cmakedefine OQS_USE_AES_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX_INSTRUCTIONS 1 diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt new file mode 100644 index 0000000000..a172d7c6a4 --- /dev/null +++ b/zephyr/CMakeLists.txt @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: MIT + +if(CONFIG_LIBOQS) + # We add our own module library to build our RNG implementation + zephyr_library() + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/zephyr/zephyr_rng.c) + + # Configuration for liboqs + set(OQS_DIST_BUILD OFF) + set(OQS_BUILD_ONLY_LIB ON) + set(OQS_USE_OPENSSL OFF) + set(OQS_POSITION_INDEPENDENT_CODE OFF) + set(OQS_USE_EXTERNAL_RNG ON) + + set(CMAKE_CROSSCOMPILING ON) + set(CMAKE_SIZEOF_VOID_P 4) # Not really needed, only for surpressing unrelated warnings + + # Disable features by hand, as CMake won't find them properly with Zephyr + set(CMAKE_HAVE_GETENTROPY OFF) + set(CMAKE_HAVE_ALIGNED_ALLOC OFF) + set(CMAKE_HAVE_POSIX_MEMALIGN OFF) + set(CMAKE_HAVE_MEMALIGN OFF) + set(CMAKE_HAVE_EXPLICIT_BZERO OFF) + set(CMAKE_HAVE_MEMSET_S OFF) + set(CC_SUPPORTS_WA_NOEXECSTACK OFF) + set(LD_SUPPORTS_WL_Z_NOEXECSTACK OFF) + + add_subdirectory(.. build) + + # Add compiler options from Zephyr to all liboqs targets + zephyr_get_targets(.. "STATIC_LIBRARY;OBJECT_LIBRARY" ALL_TARGETS) + foreach(target ${ALL_TARGETS}) + target_include_directories(${target} PRIVATE + $ + ) + + target_include_directories(${target} SYSTEM PRIVATE + $ + ) + + target_compile_definitions(${target} PRIVATE + $ + ) + + target_compile_options(${target} PRIVATE + $ + $ + ) + + # liboqs depends on unistd.h, which ultimately needs the generated syscall_list.h file, + # which is generated as part of ${SYSCALL_LIST_H_TARGET} target. + add_dependencies(${target} ${SYSCALL_LIST_H_TARGET}) + endforeach() + + # Link the liboqs library to our module library + zephyr_library_link_libraries(oqs) + + # Include the liboqs headers + zephyr_include_directories(${CMAKE_CURRENT_BINARY_DIR}/build/include) +endif() \ No newline at end of file diff --git a/zephyr/Kconfig b/zephyr/Kconfig new file mode 100644 index 0000000000..75ab350679 --- /dev/null +++ b/zephyr/Kconfig @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: MIT + +config LIBOQS + bool "Enable liboqs" + help + This option enables the liboqs as a Zephyr module. diff --git a/zephyr/module.yml b/zephyr/module.yml new file mode 100644 index 0000000000..cbff6a1aef --- /dev/null +++ b/zephyr/module.yml @@ -0,0 +1,3 @@ +build: + cmake: zephyr + kconfig: zephyr/Kconfig diff --git a/zephyr/zephyr_rng.c b/zephyr/zephyr_rng.c new file mode 100644 index 0000000000..6ca768f8d1 --- /dev/null +++ b/zephyr/zephyr_rng.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT + +#include +#include + +// Autogenerated header file from Zephyr containing the version number +#include + +#if KERNEL_VERSION_NUMBER >= 0x30500 +#include +#else +#include +#endif + + +void OQS_randombytes_external(uint8_t *random_array, size_t bytes_to_read) +{ + // Obtain random bytes from the zephyr RNG + sys_rand_get(random_array, bytes_to_read); +}