Skip to content

Commit 3876cfd

Browse files
ATmobicapull[bot]
authored andcommitted
[OIS] Add PSA crypto backend support (#26994)
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]>
1 parent 19a2672 commit 3876cfd

22 files changed

+183
-57
lines changed

.github/.wordlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ ProxyDiscovery
10711071
ProxyValid
10721072
ProxyView
10731073
PRs
1074+
PSA
10741075
PSCAN
10751076
PSECT
10761077
PSK

.github/workflows/examples-openiotsdk.yaml

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ env:
3131

3232
jobs:
3333
openiotsdk:
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
cryptoBackend: ["psa", "mbedtls"]
38+
3439
name: Open IoT SDK examples building
3540
timeout-minutes: 90
3641

@@ -89,7 +94,7 @@ jobs:
8994
id: build_shell
9095
timeout-minutes: 10
9196
run: |
92-
scripts/examples/openiotsdk_example.sh shell
97+
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} shell
9398
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
9499
openiotsdk release shell \
95100
examples/shell/openiotsdk/build/chip-openiotsdk-shell-example.elf \
@@ -99,7 +104,7 @@ jobs:
99104
id: build_lock_app
100105
timeout-minutes: 10
101106
run: |
102-
scripts/examples/openiotsdk_example.sh lock-app
107+
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} lock-app
103108
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
104109
openiotsdk release lock-app \
105110
examples/lock-app/openiotsdk/build/chip-openiotsdk-lock-app-example.elf \
@@ -109,7 +114,7 @@ jobs:
109114
id: build_unit_tests
110115
timeout-minutes: 10
111116
run: |
112-
scripts/examples/openiotsdk_example.sh unit-tests
117+
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} unit-tests
113118
114119
- name: "Test: shell example"
115120
if: steps.build_shell.outcome == 'success'

.vscode/tasks.json

+9
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
"-Cbuild",
252252
"-d${input:openiotsdkDebugMode}",
253253
"-l${input:openiotsdkLwipDebug}",
254+
"-b${input:openiotsdkCryptoBackend}",
254255
"${input:openiotsdkExample}"
255256
],
256257
"group": "build",
@@ -271,6 +272,7 @@
271272
"-Cbuild",
272273
"-d${input:openiotsdkDebugMode}",
273274
"-l${input:openiotsdkLwipDebug}",
275+
"-b${input:openiotsdkCryptoBackend}",
274276
"unit-tests"
275277
],
276278
"group": "build",
@@ -480,6 +482,13 @@
480482
"options": ["false", "true"],
481483
"default": "false"
482484
},
485+
{
486+
"type": "pickString",
487+
"id": "openiotsdkCryptoBackend",
488+
"description": "Which Crypto algorithm do you wish to use?",
489+
"options": ["mbedtls", "psa"],
490+
"default": "mbedtls"
491+
},
483492
{
484493
"type": "command",
485494
"id": "openiotsdkExample",

config/openiotsdk/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ matter_add_gn_arg_bool ("chip_automation_logging" CONFIG_CHIP_AUTO
6262
matter_add_gn_arg_bool ("chip_error_logging" CONFIG_CHIP_ERROR_LOGGING)
6363
matter_add_gn_arg_bool ("chip_openiotsdk_use_tfm" TFM_SUPPORT)
6464
matter_add_gn_arg_bool ("chip_openiotsdk_use_psa_ps" CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS)
65+
matter_add_gn_arg_string("chip_crypto" "${CONFIG_CHIP_CRYPTO}")
6566
if (TARGET cmsis-rtos-api)
6667
matter_add_gn_arg_string("target_os" "cmsis-rtos")
6768
endif()

config/openiotsdk/chip-gn/args.gni

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ chip_system_config_use_lwip = true
3131
lwip_platform = "external"
3232
chip_system_config_use_sockets = false
3333

34-
chip_crypto = "mbedtls"
3534
chip_external_mbedtls = true
3635

3736
custom_toolchain = "${chip_root}/config/openiotsdk/chip-gn/toolchain:openiotsdk"

config/openiotsdk/cmake/chip.cmake

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

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

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

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

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

57+
if ("${CONFIG_CHIP_CRYPTO}" STREQUAL "psa")
58+
target_compile_definitions(chip
59+
INTERFACE
60+
CONFIG_CHIP_CRYPTO_PSA)
61+
endif()
62+
5663
function(chip_add_data_model target scope model_name)
57-
target_include_directories(${target}
64+
target_include_directories(${target}
5865
PUBLIC
5966
${GEN_DIR}/app-common
6067
${GEN_DIR}/${model_name}-app

config/openiotsdk/cmake/sdk.cmake

+18-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ get_filename_component(OPEN_IOT_SDK_STORAGE_SOURCE ${CHIP_ROOT}/third_party/open
2727
# Open IoT SDK targets passed to CHIP build
2828
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS)
2929

30-
# Additional Open IoT SDK build configuration
30+
# Additional Open IoT SDK build configuration
3131
set(TFM_SUPPORT NO CACHE BOOL "Add Trusted Firmware-M (TF-M) support to application")
3232
set(TFM_NS_APP_VERSION "0.0.0" CACHE STRING "TF-M non-secure application version (in the x.x.x format)")
3333
set(CONFIG_CHIP_OPEN_IOT_SDK_LWIP_DEBUG NO CACHE BOOL "Enable LwIP debug logs")
@@ -82,7 +82,7 @@ if(TFM_SUPPORT)
8282
set(TFM_PLATFORM ${OPEN_IOT_SDK_EXAMPLE_COMMON}/tf-m/targets/an552)
8383
set(TFM_PSA_FIRMWARE_UPDATE ON)
8484
set(MCUBOOT_IMAGE_VERSION_NS ${TFM_NS_APP_VERSION})
85-
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")
85+
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}")
8686
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
8787
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")
8888
else()
@@ -117,24 +117,24 @@ endif()
117117
# Add RTOS configuration headers
118118
# Link cmsis-rtos-api against a concrete implementation
119119
if(TARGET cmsis-rtos-api)
120-
target_include_directories(cmsis-core
121-
INTERFACE
120+
target_include_directories(cmsis-core
121+
INTERFACE
122122
cmsis-config
123123
)
124-
124+
125125
target_compile_definitions(cmsis-rtos-api
126126
PUBLIC
127127
DOMAIN_NS=$<IF:$<BOOL:${TFM_SUPPORT}>,1,0>
128128
)
129129

130130
if(TARGET freertos-kernel)
131-
target_include_directories(freertos-kernel
132-
PUBLIC
131+
target_include_directories(freertos-kernel
132+
PUBLIC
133133
freertos-config
134134
)
135135

136-
target_link_libraries(freertos-kernel
137-
PUBLIC
136+
target_link_libraries(freertos-kernel
137+
PUBLIC
138138
cmsis-core
139139
)
140140

@@ -250,14 +250,6 @@ if("cmsis-freertos" IN_LIST IOTSDK_FETCH_LIST)
250250
)
251251
endif()
252252

253-
if("mbedtls" IN_LIST IOTSDK_FETCH_LIST)
254-
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
255-
mbedtls
256-
mbedtls-config
257-
mbedtls-threading-cmsis-rtos
258-
)
259-
endif()
260-
261253
if("lwip" IN_LIST IOTSDK_FETCH_LIST)
262254
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
263255
lwipcore
@@ -282,6 +274,15 @@ if("trusted-firmware-m" IN_LIST IOTSDK_FETCH_LIST)
282274
)
283275
endif()
284276

277+
# Note: Mbed TLS must appear after TF-M otherwise psa from mbed TLS is used
278+
if("mbedtls" IN_LIST IOTSDK_FETCH_LIST)
279+
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
280+
mbedtls
281+
mbedtls-config
282+
mbedtls-threading-cmsis-rtos
283+
)
284+
endif()
285+
285286
# Additional Open IoT SDK port components
286287

287288
# Add Open IoT SDK storage source

config/openiotsdk/lwip/user_lwipopts.h

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
*/
5050
#define LWIP_RAW (1)
5151

52+
/**
53+
* Disable DHCP as the IP6 link local address can be used.
54+
*/
55+
#define LWIP_DHCP 0
56+
5257
#ifdef LWIP_DEBUG
5358

5459
// Debug Options

config/openiotsdk/mbedtls/mbedtls_config.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -2692,7 +2692,7 @@
26922692
* or MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG.
26932693
*
26942694
*/
2695-
#define MBEDTLS_PSA_CRYPTO_C
2695+
//#define MBEDTLS_PSA_CRYPTO_C
26962696

26972697
/**
26982698
* \def MBEDTLS_PSA_CRYPTO_SE_C
@@ -3313,8 +3313,8 @@
33133313
//#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
33143314

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

33193319
/* Entropy options */
33203320
//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#define MBEDTLS_SHA1_C
3+
#define PSA_WANT_ALG_SHA_1
4+
#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
5+
#undef MBEDTLS_ECP_FIXED_POINT_OPTIM
6+
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */

docs/guides/openiotsdk_examples.md

+46-6
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,42 @@ For `TF-M` protected storage use:
374374
[Open IoT SDK build script](../../scripts/examples/openiotsdk_example.sh)
375375
provides the `-K,--kvsfile` option to use the persistence options listed above.
376376
377+
### Crypto backend
378+
379+
Open IoT SDK port supports two crypto backend implementations:
380+
381+
- [Mbed TLS](../guides/openiotsdk_platform_overview.md#mbed-tls) - it's the
382+
default option
383+
- [PSA crypto service](https://tf-m-user-guide.trustedfirmware.org/integration_guide/services/tfm_crypto_integration_guide.html)
384+
from the
385+
[TrustedFirmware-M (TF-M)](../guides/openiotsdk_platform_overview.md#trusted-firmware-m)
386+
component
387+
388+
The CMake variable `CONFIG_CHIP_CRYPTO` controls how cryptographic operations
389+
are implemented in Matter. It accepts two values:
390+
391+
- `mbedtls`: use Mbed TLS for crypto operations.
392+
- `psa`: use
393+
[PSA Cryptography API](https://armmbed.github.io/mbed-crypto/html/) for
394+
crypto operations.
395+
396+
This variable can be set in the main application `CMakeLists.txt`:
397+
398+
```
399+
set(CONFIG_CHIP_CRYPTO <mbedtls | psa>)
400+
```
401+
402+
The variable can also be defined with CMake CLI:
403+
404+
```
405+
cmake -G <...> -DCONFIG_CHIP_CRYPTO=<mbedtls | psa> <...>
406+
```
407+
408+
> 💡 **Notes**:
409+
>
410+
> The `TF-M PSA crypto` option requires enabling [TF-M](#trusted-firmware-m)
411+
> support.
412+
377413
## Building
378414
379415
You can build examples using the dedicated VSCode task or by calling directly
@@ -386,6 +422,7 @@ the build script from the command line.
386422
- Select `Build Open IoT SDK example`
387423
- Decide on debug mode support
388424
- Decide on LwIP debug logs support
425+
- Choose crypto algorithm
389426
- Choose example name
390427
391428
This will call the script with the selected parameters.
@@ -568,12 +605,12 @@ telnet> close
568605
569606
## Specific examples
570607
571-
### Build lock-app example and run it in the network namespace
608+
### Build lock-app example with PSA crypto backend support and run it in the network namespace
572609
573610
**Using CLI**
574611
575612
```
576-
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh lock-app
613+
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh -b psa lock-app
577614

578615
export TEST_NETWORK_NAME=OIStest
579616

@@ -593,6 +630,7 @@ Build example:
593630
- Select `Build Open IoT SDK example`
594631
- Deny debug mode support `false`
595632
- Deny LwIP debug logs support `false`
633+
- Choose crypto algorithm `psa`
596634
- Choose example name `lock-app`
597635
598636
Setup network environment:
@@ -614,12 +652,12 @@ Run example:
614652
615653
The example output should be seen in the terminal window.
616654
617-
### Build lock-app example and execute its test in the network namespace
655+
### Build lock-app example with mbedtls crypto backend support and execute its test in the network namespace
618656
619657
**Using CLI**
620658
621659
```
622-
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh lock-app
660+
${MATTER_ROOT}/scripts/examples/openiotsdk_example.sh -b mbedtls lock-app
623661

624662
export TEST_NETWORK_NAME=OIStest
625663

@@ -639,6 +677,7 @@ Build example:
639677
- Select `Build Open IoT SDK example`
640678
- Deny debug mode support `false`
641679
- Deny LwIP debug logs support `false`
680+
- Choose crypto algorithm `mbedtls`
642681
- Choose example name `lock-app`
643682
644683
Setup network environment:
@@ -658,7 +697,7 @@ Test example:
658697
- Enter network interface `OIStesttap`
659698
- Choose example name `lock-app`
660699
661-
### Build lock-app example in debug mode and debug it in the network namespace using the VSCode task
700+
### Build lock-app example with mbedtls crypto backend support in debug mode and debug it in the network namespace using the VSCode task
662701
663702
Build example:
664703
@@ -667,6 +706,7 @@ Build example:
667706
- Select `Build Open IoT SDK example`
668707
- Confirm debug mode support `true`
669708
- Deny LwIP debug logs support `false`
709+
- Choose crypto algorithm `mbedtls`
670710
- Choose example name `lock-app`
671711
672712
Setup network environment:
@@ -757,7 +797,7 @@ Example:
757797
id: build_new_example
758798
timeout-minutes: 10
759799
run: |
760-
scripts/examples/openiotsdk_example.sh new-example
800+
scripts/examples/openiotsdk_example.sh -b ${{ matrix.cryptoBackend }} new-example
761801
.environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
762802
openiotsdk release new-example \
763803
examples/new-example/openiotsdk/build/chip-openiotsdk-new-example-example.elf \

examples/platform/openiotsdk/app/openiotsdk_platform.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include "iotsdk/ip_network_api.h"
2828
#include "mbedtls/platform.h"
2929

30+
#ifdef CONFIG_CHIP_CRYPTO_PSA
31+
#include "psa/crypto.h"
32+
#endif
33+
3034
#include <DeviceInfoProviderImpl.h>
3135
#include <lib/support/CHIPMem.h>
3236
#include <lib/support/logging/CHIPLogging.h>
@@ -179,6 +183,15 @@ int openiotsdk_platform_init(void)
179183
return EXIT_FAILURE;
180184
}
181185

186+
#ifdef CONFIG_CHIP_CRYPTO_PSA
187+
ret = psa_crypto_init();
188+
if (ret)
189+
{
190+
ChipLogError(NotSpecified, "PSA crypto initialization failed: %d", ret);
191+
return EXIT_FAILURE;
192+
}
193+
#endif
194+
182195
#ifdef TFM_SUPPORT
183196
ret = get_psa_images_details();
184197
if (ret != 0)

0 commit comments

Comments
 (0)