Skip to content

Commit

Permalink
[OIS] Add PSA crypto backend support (#26994)
Browse files Browse the repository at this point in the history
This commit allows the selection of PSA as the cryptographic
algorithm used when building Matter CryptoPAL with the Open
IoT SDK.
The GitHub CI workflow for the SDK examples/unit tests has been
updated to add a matrix test setup which builds and runs the
examples with both mbedtls and psa cryptographic algorithms.

Add call to psa_crypto_init()

The Matter PSA implementation still uses some underlying MbedTLS
functions (including random number generation). To use these
functions however a call to psa_crypto_init() is required.
Extend Matter Python builder with crypto backend options.

Enable ECP optimization.

Signed-off-by: Anna Bridge <[email protected]>
  • Loading branch information
ATmobica authored Jun 21, 2023
1 parent f915236 commit 834663d
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 57 deletions.
1 change: 1 addition & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ ProxyDiscovery
ProxyValid
ProxyView
PRs
PSA
PSCAN
PSECT
PSK
Expand Down
11 changes: 8 additions & 3 deletions .github/workflows/examples-openiotsdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ env:

jobs:
openiotsdk:
strategy:
fail-fast: false
matrix:
cryptoBackend: ["psa", "mbedtls"]

name: Open IoT SDK examples building
timeout-minutes: 90

Expand Down Expand Up @@ -89,7 +94,7 @@ jobs:
id: build_shell
timeout-minutes: 10
run: |
scripts/examples/openiotsdk_example.sh shell
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} shell
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
openiotsdk release shell \
examples/shell/openiotsdk/build/chip-openiotsdk-shell-example.elf \
Expand All @@ -99,7 +104,7 @@ jobs:
id: build_lock_app
timeout-minutes: 10
run: |
scripts/examples/openiotsdk_example.sh lock-app
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} lock-app
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
openiotsdk release lock-app \
examples/lock-app/openiotsdk/build/chip-openiotsdk-lock-app-example.elf \
Expand All @@ -109,7 +114,7 @@ jobs:
id: build_unit_tests
timeout-minutes: 10
run: |
scripts/examples/openiotsdk_example.sh unit-tests
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} unit-tests
- name: "Test: shell example"
if: steps.build_shell.outcome == 'success'
Expand Down
9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
"-Cbuild",
"-d${input:openiotsdkDebugMode}",
"-l${input:openiotsdkLwipDebug}",
"-b${input:openiotsdkCryptoBackend}",
"${input:openiotsdkExample}"
],
"group": "build",
Expand All @@ -271,6 +272,7 @@
"-Cbuild",
"-d${input:openiotsdkDebugMode}",
"-l${input:openiotsdkLwipDebug}",
"-b${input:openiotsdkCryptoBackend}",
"unit-tests"
],
"group": "build",
Expand Down Expand Up @@ -480,6 +482,13 @@
"options": ["false", "true"],
"default": "false"
},
{
"type": "pickString",
"id": "openiotsdkCryptoBackend",
"description": "Which Crypto algorithm do you wish to use?",
"options": ["mbedtls", "psa"],
"default": "mbedtls"
},
{
"type": "command",
"id": "openiotsdkExample",
Expand Down
1 change: 1 addition & 0 deletions config/openiotsdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ matter_add_gn_arg_bool ("chip_automation_logging" CONFIG_CHIP_AUTO
matter_add_gn_arg_bool ("chip_error_logging" CONFIG_CHIP_ERROR_LOGGING)
matter_add_gn_arg_bool ("chip_openiotsdk_use_tfm" TFM_SUPPORT)
matter_add_gn_arg_bool ("chip_openiotsdk_use_psa_ps" CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS)
matter_add_gn_arg_string("chip_crypto" "${CONFIG_CHIP_CRYPTO}")
if (TARGET cmsis-rtos-api)
matter_add_gn_arg_string("target_os" "cmsis-rtos")
endif()
Expand Down
1 change: 0 additions & 1 deletion config/openiotsdk/chip-gn/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ chip_system_config_use_lwip = true
lwip_platform = "external"
chip_system_config_use_sockets = false

chip_crypto = "mbedtls"
chip_external_mbedtls = true

custom_toolchain = "${chip_root}/config/openiotsdk/chip-gn/toolchain:openiotsdk"
Expand Down
11 changes: 9 additions & 2 deletions config/openiotsdk/cmake/chip.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH)

# Default CHIP build configuration
# Default CHIP build configuration
set(CONFIG_CHIP_PROJECT_CONFIG "main/include/CHIPProjectConfig.h" CACHE STRING "")
set(CONFIG_CHIP_LIB_TESTS NO CACHE BOOL "")
set(CONFIG_CHIP_LIB_SHELL NO CACHE BOOL "")
Expand All @@ -32,6 +32,7 @@ set(CONFIG_CHIP_AUTOMATION_LOGGING YES CACHE BOOL "Enable logging at automation
set(CONFIG_CHIP_ERROR_LOGGING YES CACHE BOOL "Enable logging at error level")

set(CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS NO CACHE BOOL "Enable using PSA Protected Storage")
set(CONFIG_CHIP_CRYPTO "mbedtls" CACHE STRING "Matter crypto backend. Mbedtls as default")

if(CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS AND NOT TFM_SUPPORT)
message( FATAL_ERROR "You can not use PSA Protected Storage without TF-M support" )
Expand All @@ -53,8 +54,14 @@ if(TFM_SUPPORT)
add_dependencies(chip-gn tfm-ns-interface)
endif()

if ("${CONFIG_CHIP_CRYPTO}" STREQUAL "psa")
target_compile_definitions(chip
INTERFACE
CONFIG_CHIP_CRYPTO_PSA)
endif()

function(chip_add_data_model target scope model_name)
target_include_directories(${target}
target_include_directories(${target}
PUBLIC
${GEN_DIR}/app-common
${GEN_DIR}/${model_name}-app
Expand Down
35 changes: 18 additions & 17 deletions config/openiotsdk/cmake/sdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ get_filename_component(OPEN_IOT_SDK_STORAGE_SOURCE ${CHIP_ROOT}/third_party/open
# Open IoT SDK targets passed to CHIP build
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS)

# Additional Open IoT SDK build configuration
# Additional Open IoT SDK build configuration
set(TFM_SUPPORT NO CACHE BOOL "Add Trusted Firmware-M (TF-M) support to application")
set(TFM_NS_APP_VERSION "0.0.0" CACHE STRING "TF-M non-secure application version (in the x.x.x format)")
set(CONFIG_CHIP_OPEN_IOT_SDK_LWIP_DEBUG NO CACHE BOOL "Enable LwIP debug logs")
Expand Down Expand Up @@ -82,7 +82,7 @@ if(TFM_SUPPORT)
set(TFM_PLATFORM ${OPEN_IOT_SDK_EXAMPLE_COMMON}/tf-m/targets/an552)
set(TFM_PSA_FIRMWARE_UPDATE ON)
set(MCUBOOT_IMAGE_VERSION_NS ${TFM_NS_APP_VERSION})
set(TFM_CMAKE_ARGS "-DCONFIG_TFM_ENABLE_FP=ON;-DTFM_PROFILE=profile_medium;-DTFM_EXCEPTION_INFO_DUMP=ON;-DCONFIG_TFM_HALT_ON_CORE_PANIC=ON;-DTFM_ISOLATION_LEVEL=1")
set(TFM_CMAKE_ARGS "-DCONFIG_TFM_ENABLE_FP=ON;-DTFM_PROFILE=profile_medium;-DTFM_EXCEPTION_INFO_DUMP=ON;-DCONFIG_TFM_HALT_ON_CORE_PANIC=ON;-DTFM_ISOLATION_LEVEL=1;-DTFM_MBEDCRYPTO_PLATFORM_EXTRA_CONFIG_PATH=${OPEN_IOT_SDK_CONFIG}/mbedtls/mbedtls_config_psa.h;-DMBEDCRYPTO_BUILD_TYPE=${CMAKE_BUILD_TYPE};-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(TFM_CMAKE_ARGS "${TFM_CMAKE_ARGS};-DMCUBOOT_LOG_LEVEL=INFO;-DTFM_SPM_LOG_LEVEL=TFM_SPM_LOG_LEVEL_DEBUG;-DTFM_PARTITION_LOG_LEVEL=TFM_PARTITION_LOG_LEVEL_INFO")
else()
Expand Down Expand Up @@ -117,24 +117,24 @@ endif()
# Add RTOS configuration headers
# Link cmsis-rtos-api against a concrete implementation
if(TARGET cmsis-rtos-api)
target_include_directories(cmsis-core
INTERFACE
target_include_directories(cmsis-core
INTERFACE
cmsis-config
)

target_compile_definitions(cmsis-rtos-api
PUBLIC
DOMAIN_NS=$<IF:$<BOOL:${TFM_SUPPORT}>,1,0>
)

if(TARGET freertos-kernel)
target_include_directories(freertos-kernel
PUBLIC
target_include_directories(freertos-kernel
PUBLIC
freertos-config
)

target_link_libraries(freertos-kernel
PUBLIC
target_link_libraries(freertos-kernel
PUBLIC
cmsis-core
)

Expand Down Expand Up @@ -250,14 +250,6 @@ if("cmsis-freertos" IN_LIST IOTSDK_FETCH_LIST)
)
endif()

if("mbedtls" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
mbedtls
mbedtls-config
mbedtls-threading-cmsis-rtos
)
endif()

if("lwip" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
lwipcore
Expand All @@ -282,6 +274,15 @@ if("trusted-firmware-m" IN_LIST IOTSDK_FETCH_LIST)
)
endif()

# Note: Mbed TLS must appear after TF-M otherwise psa from mbed TLS is used
if("mbedtls" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
mbedtls
mbedtls-config
mbedtls-threading-cmsis-rtos
)
endif()

# Additional Open IoT SDK port components

# Add Open IoT SDK storage source
Expand Down
5 changes: 5 additions & 0 deletions config/openiotsdk/lwip/user_lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
*/
#define LWIP_RAW (1)

/**
* Disable DHCP as the IP6 link local address can be used.
*/
#define LWIP_DHCP 0

#ifdef LWIP_DEBUG

// Debug Options
Expand Down
6 changes: 3 additions & 3 deletions config/openiotsdk/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@
* or MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG.
*
*/
#define MBEDTLS_PSA_CRYPTO_C
//#define MBEDTLS_PSA_CRYPTO_C

/**
* \def MBEDTLS_PSA_CRYPTO_SE_C
Expand Down Expand Up @@ -3313,8 +3313,8 @@
//#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */

/* ECP options */
//#define MBEDTLS_ECP_WINDOW_SIZE 4 /**< Maximum window size used */
//#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */

/* Entropy options */
//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Expand Down
6 changes: 6 additions & 0 deletions config/openiotsdk/mbedtls/mbedtls_config_psa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#define MBEDTLS_SHA1_C
#define PSA_WANT_ALG_SHA_1
#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
#undef MBEDTLS_ECP_FIXED_POINT_OPTIM
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
52 changes: 46 additions & 6 deletions docs/guides/openiotsdk_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,42 @@ For `TF-M` protected storage use:
[Open IoT SDK build script](../../scripts/examples/openiotsdk_example.sh)
provides the `-K,--kvsfile` option to use the persistence options listed above.

### Crypto backend

Open IoT SDK port supports two crypto backend implementations:

- [Mbed TLS](../guides/openiotsdk_platform_overview.md#mbed-tls) - it's the
default option
- [PSA crypto service](https://tf-m-user-guide.trustedfirmware.org/integration_guide/services/tfm_crypto_integration_guide.html)
from the
[TrustedFirmware-M (TF-M)](../guides/openiotsdk_platform_overview.md#trusted-firmware-m)
component

The CMake variable `CONFIG_CHIP_CRYPTO` controls how cryptographic operations
are implemented in Matter. It accepts two values:

- `mbedtls`: use Mbed TLS for crypto operations.
- `psa`: use
[PSA Cryptography API](https://armmbed.github.io/mbed-crypto/html/) for
crypto operations.

This variable can be set in the main application `CMakeLists.txt`:

```
set(CONFIG_CHIP_CRYPTO <mbedtls | psa>)
```

The variable can also be defined with CMake CLI:

```
cmake -G <...> -DCONFIG_CHIP_CRYPTO=<mbedtls | psa> <...>
```

> 💡 **Notes**:
>
> The `TF-M PSA crypto` option requires enabling [TF-M](#trusted-firmware-m)
> support.
## Building

You can build examples using the dedicated VSCode task or by calling directly
Expand All @@ -386,6 +422,7 @@ the build script from the command line.
- Select `Build Open IoT SDK example`
- Decide on debug mode support
- Decide on LwIP debug logs support
- Choose crypto algorithm
- Choose example name

This will call the script with the selected parameters.
Expand Down Expand Up @@ -568,12 +605,12 @@ telnet> close
## Specific examples

### Build lock-app example and run it in the network namespace
### Build lock-app example with PSA crypto backend support and run it in the network namespace

**Using CLI**

```
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh lock-app
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh -b psa lock-app
export TEST_NETWORK_NAME=OIStest
Expand All @@ -593,6 +630,7 @@ Build example:
- Select `Build Open IoT SDK example`
- Deny debug mode support `false`
- Deny LwIP debug logs support `false`
- Choose crypto algorithm `psa`
- Choose example name `lock-app`

Setup network environment:
Expand All @@ -614,12 +652,12 @@ Run example:

The example output should be seen in the terminal window.

### Build lock-app example and execute its test in the network namespace
### Build lock-app example with mbedtls crypto backend support and execute its test in the network namespace

**Using CLI**

```
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh lock-app
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh -b mbedtls lock-app
export TEST_NETWORK_NAME=OIStest
Expand All @@ -639,6 +677,7 @@ Build example:
- Select `Build Open IoT SDK example`
- Deny debug mode support `false`
- Deny LwIP debug logs support `false`
- Choose crypto algorithm `mbedtls`
- Choose example name `lock-app`

Setup network environment:
Expand All @@ -658,7 +697,7 @@ Test example:
- Enter network interface `OIStesttap`
- Choose example name `lock-app`

### Build lock-app example in debug mode and debug it in the network namespace using the VSCode task
### Build lock-app example with mbedtls crypto backend support in debug mode and debug it in the network namespace using the VSCode task

Build example:

Expand All @@ -667,6 +706,7 @@ Build example:
- Select `Build Open IoT SDK example`
- Confirm debug mode support `true`
- Deny LwIP debug logs support `false`
- Choose crypto algorithm `mbedtls`
- Choose example name `lock-app`

Setup network environment:
Expand Down Expand Up @@ -757,7 +797,7 @@ Example:
id: build_new_example
timeout-minutes: 10
run: |
scripts/examples/openiotsdk_example.sh new-example
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} new-example
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
openiotsdk release new-example \
examples/new-example/openiotsdk/build/chip-openiotsdk-new-example-example.elf \
Expand Down
13 changes: 13 additions & 0 deletions examples/platform/openiotsdk/app/openiotsdk_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "iotsdk/ip_network_api.h"
#include "mbedtls/platform.h"

#ifdef CONFIG_CHIP_CRYPTO_PSA
#include "psa/crypto.h"
#endif

#include <DeviceInfoProviderImpl.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -179,6 +183,15 @@ int openiotsdk_platform_init(void)
return EXIT_FAILURE;
}

#ifdef CONFIG_CHIP_CRYPTO_PSA
ret = psa_crypto_init();
if (ret)
{
ChipLogError(NotSpecified, "PSA crypto initialization failed: %d", ret);
return EXIT_FAILURE;
}
#endif

#ifdef TFM_SUPPORT
ret = get_psa_images_details();
if (ret != 0)
Expand Down
Loading

0 comments on commit 834663d

Please sign in to comment.