Skip to content

Commit

Permalink
Zephyr RTOS support
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Frauschi committed Dec 4, 2023
1 parent 78e65bf commit 65f0130
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .CMake/compiler_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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})
Expand Down
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/common/rand/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <openssl/rand.h>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/oqsconfig.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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_PROPERTY:zephyr_interface,INTERFACE_INCLUDE_DIRECTORIES>
)

target_include_directories(${target} SYSTEM PRIVATE
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
)

target_compile_definitions(${target} PRIVATE
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_DEFINITIONS>
)

target_compile_options(${target} PRIVATE
$<TARGET_PROPERTY:zephyr_interface,INTERFACE_COMPILE_OPTIONS>
$<TARGET_PROPERTY:compiler,no_builtin>
)

# 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()
6 changes: 6 additions & 0 deletions zephyr/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: MIT

config LIBOQS
bool "Enable liboqs"
help
This option enables the liboqs as a Zephyr module.
3 changes: 3 additions & 0 deletions zephyr/module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
cmake: zephyr
kconfig: zephyr/Kconfig
20 changes: 20 additions & 0 deletions zephyr/zephyr_rng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT

#include <stdint.h>
#include <stddef.h>

// Autogenerated header file from Zephyr containing the version number
#include <version.h>

#if KERNEL_VERSION_NUMBER >= 0x30500
#include <zephyr/random/random.h>
#else
#include <zephyr/random/rand32.h>
#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);
}

0 comments on commit 65f0130

Please sign in to comment.