Skip to content

Add proper Zephyr module support with updated HAL for v3/v4 compatibility #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
cmake_minimum_required(VERSION 3.20.0)

# Option to enable Zephyr support
option(ATCA_ZEPHYR_SUPPORT "Enable Zephyr HAL support and build as a Zephyr library" OFF)

project (cryptoauthlib C)

# Set the current release version
Expand All @@ -11,9 +15,9 @@ set(VERSION_PATCH 8)
option(BUILD_TESTS "Create Test Application with library" OFF)

if(UNIX)
option(SETUP_INSTALLER "Setup installation and packaging as well" ON)
option(SETUP_INSTALLER "Setup installation and packaging as well" ON)
else()
set(SETUP_INSTALLER OFF CACHE INTERNAL "Disabling installation on this platform")
set(SETUP_INSTALLER OFF CACHE INTERNAL "Disabling installation on this platform")
endif()

# Default install root which is normally /usr/local/
Expand All @@ -24,21 +28,35 @@ set(CMAKE_INSTALL_PREFIX "/" CACHE INTERNAL "")

include(cmake/check_environment.cmake)

# Make sure when testing that everything goes where it should
if(BUILD_TESTS)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
if (NOT Zephyr_FOUND AND ATCA_ZEPHYR_SUPPORT)
find_package(Zephyr QUIET HINTS $ENV{ZEPHYR_BASE})
endif()

if (Zephyr_FOUND)
set(ATCA_ZEPHYR_SUPPORT ON CACHE BOOL "Auto-enable Zephyr support when Zephyr toolchain detected" FORCE)
include_directories(${ZEPHYR_INCLUDE_DIRS})
include(cmake/zephyr_options.cmake)
else()
# If Zephyr support is requested but not found, we can still proceed with the build
message(STATUS "Zephyr support requested but not found, proceeding without Zephyr")
set(ATCA_ZEPHYR_SUPPORT OFF CACHE BOOL "Zephyr support not found" FORCE)
endif()

add_subdirectory(lib)

# Make sure when testing that everything goes where it should
if(BUILD_TESTS)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# Tests
if(BUILD_TESTS)
add_subdirectory(test)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT cryptoauth_test)
add_subdirectory(test)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT cryptoauth_test)
endif(BUILD_TESTS)

# If we're installing the library then we'll add the global configuration files
if(SETUP_INSTALLER)
include(cmake/config_install.cmake)
include(cmake/config_install.cmake)
endif()
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,73 @@ Edit the mchp-cryptoauth.rules file and add the following line to the file:
```text
SUBSYSTEM=="hidraw", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2312", MODE="0666"
```


## Incorporating CryptoAuthLib in a Zephyr Project

CryptoAuthLib now builds as a proper Zephyr module and exposes its Kconfig options directly.

### Out-of-tree Zephyr application

Add this **before** your `find_package(Zephyr …)` in your top-level `CMakeLists.txt`:

```cmake
# Tell Zephyr where to find the CryptoAuthLib module
set(EXTRA_ZEPHYR_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/../modules/lib/cryptoauthlib)

# Now discover Zephyr (and CryptoAuthLib)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
````

### In-tree Zephyr workspace

If you’ve placed CryptoAuthLib under `zephyr/modules/lib/cryptoauthlib`, simply:

```cmake
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
```

and Zephyr will auto-scan its `modules/` folder.

### Available Kconfig Options

All of CryptoAuthLib’s Zephyr build flags are controlled via Kconfig. In your `prj.conf`, you can enable or disable:

```conf
# HAL drivers
CONFIG_ATCA_HAL_I2C=y
CONFIG_ATCA_HAL_SPI=n
CONFIG_ATCA_HAL_KIT_HID=y
# …

# Library behavior
CONFIG_ATCA_PRINTF=y
CONFIG_ATCA_NO_HEAP=n
CONFIG_ATCA_STRICT_C99=y
# …

# Crypto backends
CONFIG_ATCA_MBEDTLS=y
# …

# Certificate & JWT
CONFIG_ATCA_JWT_EN=y
CONFIG_ATCACERT_COMPCERT_EN=y
CONFIG_ATCACERT_FULLSTOREDCERT_EN=y
# …

# Device support
CONFIG_ATCA_ATECC608_SUPPORT=y
CONFIG_ATCA_ECC204_SUPPORT=y
# …
```

When you build, these `CONFIG_ATCA_*` settings are automatically mapped into the corresponding `ATCA_*` CMake options, and into `atca_config.h`, so your source sees:

```c
#ifdef ATCA_HAL_I2C
// I2C driver code is compiled in
#endif
```

This gives you seamless, Zephyr-style integration of CryptoAuthLib alongside your application.
34 changes: 34 additions & 0 deletions cmake/zephyr_options.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# zephyr_options.cmake
# Map Zephyr Kconfig symbols (CONFIG_* variables) to our ATCA_* CMake options

# Only run under Zephyr support
if (ATCA_ZEPHYR_SUPPORT)
message(STATUS "Mapping Kconfig symbols to CryptoAuthLib CMake options...")

# List of all CryptoAuthLib options to map
set(_opts
HAL_I2C HAL_SPI HAL_CUSTOM HAL_KIT_HID HAL_KIT_BRIDGE HAL_KIT_UART HAL_SWI_UART
PRINTF NO_HEAP CHECK_PARAMS_EN ENABLE_DEPRECATED STRICT_C99 MULTIPART_BUF_EN
MBEDTLS WOLFSSL OPENSSL JWT_EN
ATCACERT_COMPCERT_EN ATCACERT_FULLSTOREDCERT_EN
TNGTLS_SUPPORT TNGLORA_SUPPORT TFLEX_SUPPORT TNG_LEGACY_SUPPORT
WPC_SUPPORT
ATCAC_SHA384_EN ATCAC_SHA512_EN
PKCS11
ATSHA204A_SUPPORT ATSHA206A_SUPPORT ATECC108A_SUPPORT ATECC508A_SUPPORT
ATECC608_SUPPORT ECC204_SUPPORT TA010_SUPPORT SHA104_SUPPORT SHA105_SUPPORT
)

foreach(opt IN LISTS _opts)
# Construct Kconfig var name, e.g. CONFIG_ATCA_HAL_I2C
set(_kconfig_var CONFIG_ATCA_${opt})
if(DEFINED ${_kconfig_var})
# Read its boolean value (y/n) into a CMake boolean
if(${_kconfig_var})
set(ATCA_${opt} ON CACHE BOOL "Auto-set from Kconfig (${_kconfig_var})" FORCE)
else()
set(ATCA_${opt} OFF CACHE BOOL "Auto-clear from Kconfig (${_kconfig_var})" FORCE)
endif()
endif()
endforeach()
endif()
Loading