From 19186838f2a5289b897e92ba313ccf995c624029 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 24 Oct 2024 14:32:18 +0200 Subject: [PATCH 1/3] manifest: mbedtls: include patch for static key slots Update the Mbed TLS revision so as to take in a patch which allows to use static key slot buffers for the PSA Crypto core instead of dynamic (i.e. heap based) ones. Signed-off-by: Valerio Setti --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 87287fb39ac7cc..36d5903e742d65 100644 --- a/west.yml +++ b/west.yml @@ -280,7 +280,7 @@ manifest: revision: 2b498e6f36d6b82ae1da12c8b7742e318624ecf5 path: modules/lib/gui/lvgl - name: mbedtls - revision: a78176c6ff0733ba08018cba4447bd3f20de7978 + revision: 4952e1328529ee549d412b498ea71c54f30aa3b1 path: modules/crypto/mbedtls groups: - crypto From c06a692ca9af25a147db3ff6c5ad9a00f083d7a6 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 24 Oct 2024 14:35:53 +0200 Subject: [PATCH 2/3] mbedtls: add Kconfig symbol CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS Adding the Kconfig symbol CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS to allow Mbed TLS's PSA Crypto core to use static key buffers to store key's material. This helps reducing heap memory usage and, potentially, it also discard code implementing heap memory management if there's no other module in the build that makes use of it. Signed-off-by: Valerio Setti --- doc/releases/release-notes-4.1.rst | 6 ++++++ modules/mbedtls/Kconfig.tls-generic | 19 ++++++++++++++++++- modules/mbedtls/configs/config-tls-generic.h | 4 ++++ tests/crypto/secp256r1/mbedtls.conf | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/doc/releases/release-notes-4.1.rst b/doc/releases/release-notes-4.1.rst index dff3f100c2c51d..00d28be78a0e08 100644 --- a/doc/releases/release-notes-4.1.rst +++ b/doc/releases/release-notes-4.1.rst @@ -250,6 +250,12 @@ Libraries / Subsystems * Crypto + * The Kconfig symbol :kconfig:option:`CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS` was + added to allow Mbed TLS to use statically allocated buffers to store key material + in its PSA Crypto core instead of heap-allocated ones. This can help reduce + (or remove, if no other component makes use of it) heap memory requirements + from the final application. + * CMSIS-NN * FPGA diff --git a/modules/mbedtls/Kconfig.tls-generic b/modules/mbedtls/Kconfig.tls-generic index 779d3b356f8268..f65c86a2d967d0 100644 --- a/modules/mbedtls/Kconfig.tls-generic +++ b/modules/mbedtls/Kconfig.tls-generic @@ -554,9 +554,10 @@ config MBEDTLS_LMS depends on MBEDTLS_SHA256 select PSA_WANT_ALG_SHA_256 +if MBEDTLS_PSA_CRYPTO_C + config MBEDTLS_PSA_P256M_DRIVER_ENABLED bool "P256-M driver" - depends on MBEDTLS_PSA_CRYPTO_C imply PSA_WANT_ALG_SHA_256 help Enable support for the optimized sofware implementation of the secp256r1 @@ -570,6 +571,22 @@ config MBEDTLS_PSA_P256M_DRIVER_RAW Warning: Usage of this Kconfig option is prohibited in Zephyr's codebase. Users can enable it in case of very memory-constrained devices, but be aware that the p256-m interface is absolutely not guaranted to remain stable over time. +config MBEDTLS_PSA_STATIC_KEY_SLOTS + bool "Use statically allocated key buffers to store key material" + default y if !MBEDTLS_ENABLE_HEAP + help + By default Mbed TLS's PSA Crypto core uses heap memory to store the + key material for each key slot. This might impose an undesired + requirement to support heap memory and its management code, affecting + RAM and ROM footprints at the same time. + Enabling this symbol causes Mbed TLS to pre-allocate all the key slot + buffers that are used to store the key material at build time, thus + removing the need for heap memory. Each buffer will be sized to + contain the largest asymmetric/symmetric key type enabled in the build + through PSA_WANT symbols. + +endif # MBEDTLS_PSA_CRYPTO_C + config MBEDTLS_SSL_DTLS_CONNECTION_ID bool "DTLS Connection ID extension" depends on MBEDTLS_DTLS diff --git a/modules/mbedtls/configs/config-tls-generic.h b/modules/mbedtls/configs/config-tls-generic.h index aff59f9e17e763..3f2bc5354bbd56 100644 --- a/modules/mbedtls/configs/config-tls-generic.h +++ b/modules/mbedtls/configs/config-tls-generic.h @@ -494,6 +494,10 @@ #endif /* CONFIG_MBEDTLS_PSA_CRYPTO_C */ +#if defined(CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS) +#define MBEDTLS_PSA_STATIC_KEY_SLOTS +#endif + #if defined(CONFIG_MBEDTLS_USE_PSA_CRYPTO) #define MBEDTLS_USE_PSA_CRYPTO #endif diff --git a/tests/crypto/secp256r1/mbedtls.conf b/tests/crypto/secp256r1/mbedtls.conf index e87e14abd71ae2..7c3a56ce20b5ec 100644 --- a/tests/crypto/secp256r1/mbedtls.conf +++ b/tests/crypto/secp256r1/mbedtls.conf @@ -1,6 +1,7 @@ CONFIG_MBEDTLS=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=y +CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS=y CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT=y CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE=y From 7dc3c1827596ce90d17b1089005e9d9625c142aa Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 21 Oct 2024 12:12:29 +0200 Subject: [PATCH 3/3] mbedtls: add Kconfig to select the number of key slot in PSA Crypto core Adding new CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT to select the number of key slots in PSA Crypto core. The default value is 16. Be aware that key slots consume RAM memory even if unused, so the proper value should be a compromise between the number of slots required by the application and the available RAM in the system. This commit also: - updates tests/crypto/secp256r1/mbedtls.conf to showcase how to use this new symbol to reduce RAM footprint. - tests/bsim/bluetooth/mesh/overlay_psa.conf to support all the keys used in the test. Signed-off-by: Valerio Setti --- doc/releases/migration-guide-4.1.rst | 8 ++++++++ doc/releases/release-notes-4.1.rst | 6 ++++++ modules/mbedtls/Kconfig.tls-generic | 13 +++++++++++++ modules/mbedtls/configs/config-tls-generic.h | 5 ++++- tests/bsim/bluetooth/mesh/overlay_psa.conf | 3 +++ tests/crypto/secp256r1/mbedtls.conf | 1 + 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/doc/releases/migration-guide-4.1.rst b/doc/releases/migration-guide-4.1.rst index 90dfad9a0e2370..8fa72c627cc7cb 100644 --- a/doc/releases/migration-guide-4.1.rst +++ b/doc/releases/migration-guide-4.1.rst @@ -36,6 +36,14 @@ Mbed TLS :kconfig:option:`CONFIG_MBEDTLS_PSA_CRYPTO_LEGACY_RNG`. This helps in reducing ROM/RAM footprint of the Mbed TLS library. +* The newly-added Kconfig option :kconfig:option:`CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT` + allows to specify the number of key slots available in the PSA Crypto core. + Previously this value was not explicitly set, so Mbed TLS's default value of + 32 was used. The new Kconfig option defaults to 16 instead in order to find + a reasonable compromise between RAM consumption and most common use cases. + It can be further trimmed down to reduce RAM consumption if the final + application doesn't need that many key slots simultaneously. + Trusted Firmware-M ================== diff --git a/doc/releases/release-notes-4.1.rst b/doc/releases/release-notes-4.1.rst index 00d28be78a0e08..0549b4b8c30cbc 100644 --- a/doc/releases/release-notes-4.1.rst +++ b/doc/releases/release-notes-4.1.rst @@ -256,6 +256,12 @@ Libraries / Subsystems (or remove, if no other component makes use of it) heap memory requirements from the final application. + * The Kconfig symbol :kconfig:option:`CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT` was + added to allow selecting the number of key slots available in the Mbed TLS + implementation of the PSA Crypto core. It defaults to 16. Since each + slot consumes RAM memory even if unused, this value can be tweaked in order + to minimize RAM usage. + * CMSIS-NN * FPGA diff --git a/modules/mbedtls/Kconfig.tls-generic b/modules/mbedtls/Kconfig.tls-generic index f65c86a2d967d0..5c8ac8b569b15d 100644 --- a/modules/mbedtls/Kconfig.tls-generic +++ b/modules/mbedtls/Kconfig.tls-generic @@ -585,6 +585,19 @@ config MBEDTLS_PSA_STATIC_KEY_SLOTS contain the largest asymmetric/symmetric key type enabled in the build through PSA_WANT symbols. +config MBEDTLS_PSA_KEY_SLOT_COUNT + int "Number of key slots in PSA Crypto core" + default 16 + help + Set the number of key slots that are available in the PSA Crypto core. + Be aware that each slot, even if unused, increases RAM consumption + by ~40 bytes plus: + * the length of the largest asymmetric/symmetric key type enabled in + the build through PSA_WANT symbols, if MBEDTLS_PSA_STATIC_KEY_SLOTS + is set. (This is all defined statically at build time). + * the heap-allocated memory to store the key material of a given slot, + if it is used and MBEDTLS_PSA_STATIC_KEY_SLOTS is not set. + endif # MBEDTLS_PSA_CRYPTO_C config MBEDTLS_SSL_DTLS_CONNECTION_ID diff --git a/modules/mbedtls/configs/config-tls-generic.h b/modules/mbedtls/configs/config-tls-generic.h index 3f2bc5354bbd56..989d0ad70f2cd5 100644 --- a/modules/mbedtls/configs/config-tls-generic.h +++ b/modules/mbedtls/configs/config-tls-generic.h @@ -483,7 +483,6 @@ #endif #if defined(CONFIG_ARCH_POSIX) && !defined(CONFIG_PICOLIBC) -#define MBEDTLS_PSA_KEY_SLOT_COUNT 64 /* for BLE Mesh tests */ #define MBEDTLS_PSA_ITS_FILE_C #define MBEDTLS_FS_IO #endif @@ -498,6 +497,10 @@ #define MBEDTLS_PSA_STATIC_KEY_SLOTS #endif +#if defined(CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT) +#define MBEDTLS_PSA_KEY_SLOT_COUNT CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT +#endif + #if defined(CONFIG_MBEDTLS_USE_PSA_CRYPTO) #define MBEDTLS_USE_PSA_CRYPTO #endif diff --git a/tests/bsim/bluetooth/mesh/overlay_psa.conf b/tests/bsim/bluetooth/mesh/overlay_psa.conf index 764d8cb6ea4949..f5a776bc6da322 100644 --- a/tests/bsim/bluetooth/mesh/overlay_psa.conf +++ b/tests/bsim/bluetooth/mesh/overlay_psa.conf @@ -1,5 +1,8 @@ # Enable PSA as a crypto backend in host CONFIG_BT_USE_PSA_API=y +# Increase the number of key slots in PSA Crypto core +CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=64 + # Enable mbedTLS PSA as a crypto backend CONFIG_BT_MESH_USES_MBEDTLS_PSA=y diff --git a/tests/crypto/secp256r1/mbedtls.conf b/tests/crypto/secp256r1/mbedtls.conf index 7c3a56ce20b5ec..bbc2eb0e65638c 100644 --- a/tests/crypto/secp256r1/mbedtls.conf +++ b/tests/crypto/secp256r1/mbedtls.conf @@ -2,6 +2,7 @@ CONFIG_MBEDTLS=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=y CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS=y +CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=2 CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT=y CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE=y