From 2a8874a7ff72a0641042337ed14b5820dce6207b Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 21 Oct 2024 12:35:18 +0200 Subject: [PATCH 1/3] mbedtls: auto-select MBEDTLS_CIPHER_AES_ENABLED when built-in in PSA Auto-select MBEDTLS_CIPHER_AES_ENABLED when AES support is requested through PSA (i.e. CONFIG_PSA_WANT_KEY_TYPE_AES) and the PSA support is provided through Mbed TLS itself (i.e. CONFIG_MBEDTLS_PSA_CRYPTO_C). This mimic what happens in Mbed TLS at build time: if AES support is required through PSA, but there's no one else providing it (i.e. no TF-M in Zephyr) then provide this support through legacy AES module. This is useful in samples/tests so that the user can simply use the PSA_WANT symbol to ask for AES support in PSA crypto and then tune the AES features (ex: CONFIG_MBEDTLS_AES_ROM_TABLES) without the need to also define CONFIG_MBEDTLS_CIPHER_AES_ENABLED. Signed-off-by: Valerio Setti --- modules/mbedtls/Kconfig.tls-generic | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/mbedtls/Kconfig.tls-generic b/modules/mbedtls/Kconfig.tls-generic index 3ea0a49dd3424f..10825873157156 100644 --- a/modules/mbedtls/Kconfig.tls-generic +++ b/modules/mbedtls/Kconfig.tls-generic @@ -246,6 +246,7 @@ config MBEDTLS_SOME_CIPHER_ENABLED config MBEDTLS_CIPHER_AES_ENABLED bool "AES block cipher" + default y if PSA_WANT_KEY_TYPE_AES && MBEDTLS_PSA_CRYPTO_C if MBEDTLS_CIPHER_AES_ENABLED From a2fb85cd49dabee05a5af334f53f7cbebfdc2797 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 22 Oct 2024 21:34:46 +0200 Subject: [PATCH 2/3] mbedtls: use CSPRNG whenever possible as PSA random source The main problem of MBEDTLS_PSA_CRYPTO_LEGACY_RNG is that it brings in some legacy modules (entropy + ctr_drbg/hmac_drbg) which means extra ROM/RAM footprint. MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG instead simply calls to the CSPRNG which makes it definitely smaller. Signed-off-by: Valerio Setti --- doc/releases/migration-guide-4.1.rst | 6 ++++++ modules/mbedtls/Kconfig.tls-generic | 1 + samples/psa/its/overlay-entropy_driver.conf | 1 - samples/psa/persistent_key/overlay-entropy_driver.conf | 1 - tests/bsim/bluetooth/host/gatt/caching/psa_overlay.conf | 1 - tests/bsim/bluetooth/ll/conn/psa_overlay.conf | 1 - tests/crypto/mbedtls_psa/prj.conf | 1 - tests/crypto/mbedtls_psa/testcase.yaml | 5 +++-- tests/crypto/secp256r1/mbedtls.conf | 1 - tests/crypto/secp256r1/p256-m_raw.conf | 1 - tests/net/socket/tls_configurations/prj.conf | 1 - tests/subsys/jwt/testcase.yaml | 6 ++++++ 12 files changed, 16 insertions(+), 10 deletions(-) diff --git a/doc/releases/migration-guide-4.1.rst b/doc/releases/migration-guide-4.1.rst index d35ab96073c736..db7e1ffd8d8eab 100644 --- a/doc/releases/migration-guide-4.1.rst +++ b/doc/releases/migration-guide-4.1.rst @@ -30,6 +30,12 @@ Modules Mbed TLS ======== +* If a platform has a CSPRNG source available (i.e. :kconfig:option:`CONFIG_CSPRNG_ENABLED` + is set), then the Kconfig option :kconfig:option:`CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG` + is the default choice for random number source instead of + :kconfig:option:`CONFIG_MBEDTLS_PSA_CRYPTO_LEGACY_RNG`. This helps in reducing + ROM/RAM footprint of the Mbed TLS library. + Trusted Firmware-M ================== diff --git a/modules/mbedtls/Kconfig.tls-generic b/modules/mbedtls/Kconfig.tls-generic index 10825873157156..2e6e6b7f8c322f 100644 --- a/modules/mbedtls/Kconfig.tls-generic +++ b/modules/mbedtls/Kconfig.tls-generic @@ -481,6 +481,7 @@ config MBEDTLS_SSL_EXTENDED_MASTER_SECRET choice MBEDTLS_PSA_CRYPTO_RNG_SOURCE prompt "Select random source for built-in PSA crypto" depends on MBEDTLS_PSA_CRYPTO_C + default MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG if CSPRNG_ENABLED default MBEDTLS_PSA_CRYPTO_LEGACY_RNG config MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG diff --git a/samples/psa/its/overlay-entropy_driver.conf b/samples/psa/its/overlay-entropy_driver.conf index b2fea61e044a3c..0feb3ad09493f3 100644 --- a/samples/psa/its/overlay-entropy_driver.conf +++ b/samples/psa/its/overlay-entropy_driver.conf @@ -1,4 +1,3 @@ # SPDX-License-Identifier: Apache-2.0 CONFIG_ENTROPY_GENERATOR=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y diff --git a/samples/psa/persistent_key/overlay-entropy_driver.conf b/samples/psa/persistent_key/overlay-entropy_driver.conf index b2fea61e044a3c..0feb3ad09493f3 100644 --- a/samples/psa/persistent_key/overlay-entropy_driver.conf +++ b/samples/psa/persistent_key/overlay-entropy_driver.conf @@ -1,4 +1,3 @@ # SPDX-License-Identifier: Apache-2.0 CONFIG_ENTROPY_GENERATOR=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y diff --git a/tests/bsim/bluetooth/host/gatt/caching/psa_overlay.conf b/tests/bsim/bluetooth/host/gatt/caching/psa_overlay.conf index 7086f66d96d521..49282c5e502db1 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/psa_overlay.conf +++ b/tests/bsim/bluetooth/host/gatt/caching/psa_overlay.conf @@ -4,4 +4,3 @@ CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_PSA_CRYPTO_ENABLE_ALL=y CONFIG_ENTROPY_GENERATOR=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y diff --git a/tests/bsim/bluetooth/ll/conn/psa_overlay.conf b/tests/bsim/bluetooth/ll/conn/psa_overlay.conf index 7086f66d96d521..49282c5e502db1 100644 --- a/tests/bsim/bluetooth/ll/conn/psa_overlay.conf +++ b/tests/bsim/bluetooth/ll/conn/psa_overlay.conf @@ -4,4 +4,3 @@ CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_PSA_CRYPTO_ENABLE_ALL=y CONFIG_ENTROPY_GENERATOR=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y diff --git a/tests/crypto/mbedtls_psa/prj.conf b/tests/crypto/mbedtls_psa/prj.conf index 0f4585d6b49d37..54dd833011b1f2 100644 --- a/tests/crypto/mbedtls_psa/prj.conf +++ b/tests/crypto/mbedtls_psa/prj.conf @@ -3,4 +3,3 @@ CONFIG_ZTEST=y CONFIG_MBEDTLS=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y diff --git a/tests/crypto/mbedtls_psa/testcase.yaml b/tests/crypto/mbedtls_psa/testcase.yaml index 6b96e8ff5f6fa5..187e25b1976c8d 100644 --- a/tests/crypto/mbedtls_psa/testcase.yaml +++ b/tests/crypto/mbedtls_psa/testcase.yaml @@ -11,8 +11,6 @@ # - no TF-M enabled devices because we assume that the TF-M implementation # of PSA crypto is working fine on the platforms that support TF-M. # - platform should be testable by the CI. -# - enable CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG in order to reduce as much -# as possible usage of legacy modules in Mbed TLS. # - pick 1 platform which supports entropy driver and 1 which does not. The # latter case will allow to test # CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG. @@ -34,8 +32,11 @@ tests: # Pick a platform which does not have an entropy driver. In this case we # enable the timer random generator because it's always available on all # platforms. + # Explicitly select CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG because this is + # not "automatically selected" when there is no CSPRNG available. integration_platforms: - qemu_x86 extra_configs: + - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG=y - CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/tests/crypto/secp256r1/mbedtls.conf b/tests/crypto/secp256r1/mbedtls.conf index dd8231a21ca547..e87e14abd71ae2 100644 --- a/tests/crypto/secp256r1/mbedtls.conf +++ b/tests/crypto/secp256r1/mbedtls.conf @@ -1,5 +1,4 @@ CONFIG_MBEDTLS=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=y diff --git a/tests/crypto/secp256r1/p256-m_raw.conf b/tests/crypto/secp256r1/p256-m_raw.conf index 801a31df91a866..5ac706ef29bf60 100644 --- a/tests/crypto/secp256r1/p256-m_raw.conf +++ b/tests/crypto/secp256r1/p256-m_raw.conf @@ -1,5 +1,4 @@ CONFIG_MBEDTLS=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=y CONFIG_MBEDTLS_PSA_P256M_DRIVER_RAW=y diff --git a/tests/net/socket/tls_configurations/prj.conf b/tests/net/socket/tls_configurations/prj.conf index 93a8c0f8b1230d..23842f6a64149a 100644 --- a/tests/net/socket/tls_configurations/prj.conf +++ b/tests/net/socket/tls_configurations/prj.conf @@ -28,7 +28,6 @@ CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048 CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y # Build the PSA Crypto core so that the TLS stack uses the PSA crypto API. CONFIG_MBEDTLS_PSA_CRYPTO_C=y -CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y CONFIG_ENTROPY_GENERATOR=y # Disable some Kconfigs that are implied by CONFIG_NET_SOCKETS_SOCKOPT_TLS. diff --git a/tests/subsys/jwt/testcase.yaml b/tests/subsys/jwt/testcase.yaml index f439e9aea9e1ce..8744d51bbd03ba 100644 --- a/tests/subsys/jwt/testcase.yaml +++ b/tests/subsys/jwt/testcase.yaml @@ -12,6 +12,9 @@ tests: libraries.encoding.jwt.ecdsa.psa: extra_configs: - CONFIG_JWT_SIGN_ECDSA_PSA=y + # Explicitly select CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG because this + # is not automatically selected on platforms that do not have a CSPRNG + # source. - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG=y libraries.encoding.jwt.rsa.legacy: @@ -21,5 +24,8 @@ tests: libraries.encoding.jwt.rsa.psa: extra_configs: - CONFIG_JWT_SIGN_RSA_PSA=y + # Explicitly select CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG because this + # is not automatically selected on platforms that do not have a CSPRNG + # source. - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=y - CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG=y From d5ba5466f43e6501dff2186ea48b99d210d96022 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 5 Nov 2024 14:59:24 +0100 Subject: [PATCH 3/3] mbedtls: MBEDTLS_ENTROPY_POLL_ZEPHYR default on if MBEDTLS_ENTROPY_C As long as MBEDTLS_ENTROPY_C is enabled, Mbed TLS needs to poll some entropy source to gather data that will then be processed by CTR/HMAC-DRBG modules. This means that in most of the cases, once MBEDTLS_ENTROPY_C is enabled then also MBEDTLS_ENTROPY_POLL_ZEPHYR needs to be enabled. This was done manually until now, as the long list of samples/tests demonstrate. This commit solves this dependency by defaulting MBEDTLS_ENTROPY_POLL_ZEPHYR to on as soon as MBEDTLS_ENTROPY_C is set. As a consequence, all manual enablement of MBEDTLS_ENTROPY_POLL_ZEPHYR in samples/tests are removed. Signed-off-by: Valerio Setti --- drivers/bluetooth/hci/Kconfig | 1 - drivers/wifi/esp32/Kconfig.esp32 | 1 - modules/mbedtls/Kconfig.tls-generic | 1 + samples/net/wifi/shell/boards/frdm_rw612.conf | 1 - samples/net/wifi/shell/boards/rd_rw612_bga.conf | 1 - samples/psa/its/overlay-entropy_not_secure.conf | 1 - samples/psa/persistent_key/overlay-entropy_not_secure.conf | 1 - subsys/bluetooth/mesh/Kconfig | 1 - tests/modules/uoscore/prj.conf | 1 - .../subsys/secure_storage/psa/crypto/overlay-secure_storage.conf | 1 - .../subsys/secure_storage/psa/its/overlay-default_transform.conf | 1 - tests/subsys/storage/flash_map/overlay-psa.conf | 1 + 12 files changed, 2 insertions(+), 10 deletions(-) diff --git a/drivers/bluetooth/hci/Kconfig b/drivers/bluetooth/hci/Kconfig index e15ba3bb1f388b..d2a68ae3758913 100644 --- a/drivers/bluetooth/hci/Kconfig +++ b/drivers/bluetooth/hci/Kconfig @@ -112,7 +112,6 @@ config BT_SILABS_EFR32 select MBEDTLS select MBEDTLS_PSA_CRYPTO_C select MBEDTLS_ENTROPY_C - select MBEDTLS_ENTROPY_POLL_ZEPHYR help Use Silicon Labs binary Bluetooth library to connect to the controller. diff --git a/drivers/wifi/esp32/Kconfig.esp32 b/drivers/wifi/esp32/Kconfig.esp32 index 6732bd5ff7eb8e..0da3e58c62ae45 100644 --- a/drivers/wifi/esp32/Kconfig.esp32 +++ b/drivers/wifi/esp32/Kconfig.esp32 @@ -377,7 +377,6 @@ config ESP32_WIFI_MBEDTLS_CRYPTO select MBEDTLS_CIPHER_MODE_CTR_ENABLED select MBEDTLS_CMAC select MBEDTLS_ENTROPY_C - select MBEDTLS_ENTROPY_POLL_ZEPHYR help Select this option to use MbedTLS crypto APIs which utilize hardware acceleration. diff --git a/modules/mbedtls/Kconfig.tls-generic b/modules/mbedtls/Kconfig.tls-generic index 2e6e6b7f8c322f..779d3b356f8268 100644 --- a/modules/mbedtls/Kconfig.tls-generic +++ b/modules/mbedtls/Kconfig.tls-generic @@ -398,6 +398,7 @@ config MBEDTLS_ENTROPY_C config MBEDTLS_ENTROPY_POLL_ZEPHYR bool "Provide entropy data to Mbed TLS through entropy driver or random generator" + default y depends on MBEDTLS_ENTROPY_C help Provide entropy data to the Mbed TLS's entropy module through either diff --git a/samples/net/wifi/shell/boards/frdm_rw612.conf b/samples/net/wifi/shell/boards/frdm_rw612.conf index 87de6bc8dbdeee..e9ba4d7df6f340 100644 --- a/samples/net/wifi/shell/boards/frdm_rw612.conf +++ b/samples/net/wifi/shell/boards/frdm_rw612.conf @@ -103,7 +103,6 @@ CONFIG_MBEDTLS_USER_CONFIG_FILE="wpa_supp_els_pkc_mbedtls_config.h" CONFIG_ENTROPY_GENERATOR=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_ENTROPY_C=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=8192 # power management diff --git a/samples/net/wifi/shell/boards/rd_rw612_bga.conf b/samples/net/wifi/shell/boards/rd_rw612_bga.conf index 633137e3fb02d3..5ecd5b4f1e705b 100644 --- a/samples/net/wifi/shell/boards/rd_rw612_bga.conf +++ b/samples/net/wifi/shell/boards/rd_rw612_bga.conf @@ -102,7 +102,6 @@ CONFIG_MBEDTLS_USER_CONFIG_FILE="wpa_supp_els_pkc_mbedtls_config.h" CONFIG_ENTROPY_GENERATOR=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_MBEDTLS_ENTROPY_C=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=8192 # power management diff --git a/samples/psa/its/overlay-entropy_not_secure.conf b/samples/psa/its/overlay-entropy_not_secure.conf index 2aba3a2c7e27d4..f2ab17793542e6 100644 --- a/samples/psa/its/overlay-entropy_not_secure.conf +++ b/samples/psa/its/overlay-entropy_not_secure.conf @@ -2,4 +2,3 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y diff --git a/samples/psa/persistent_key/overlay-entropy_not_secure.conf b/samples/psa/persistent_key/overlay-entropy_not_secure.conf index 2aba3a2c7e27d4..f2ab17793542e6 100644 --- a/samples/psa/persistent_key/overlay-entropy_not_secure.conf +++ b/samples/psa/persistent_key/overlay-entropy_not_secure.conf @@ -2,4 +2,3 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index e300e18d799eab..8a08631291201a 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -1469,7 +1469,6 @@ config BT_MESH_USES_MBEDTLS_PSA select EXPERIMENTAL select MBEDTLS select MBEDTLS_ENTROPY_C - select MBEDTLS_ENTROPY_POLL_ZEPHYR select MBEDTLS_PSA_CRYPTO_C select MBEDTLS_USE_PSA_CRYPTO select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT diff --git a/tests/modules/uoscore/prj.conf b/tests/modules/uoscore/prj.conf index f661156dbc1d63..d86ae838b21d35 100644 --- a/tests/modules/uoscore/prj.conf +++ b/tests/modules/uoscore/prj.conf @@ -13,7 +13,6 @@ CONFIG_MBEDTLS=y CONFIG_MBEDTLS_ENABLE_HEAP=y CONFIG_MBEDTLS_HEAP_SIZE=2048 CONFIG_MBEDTLS_ENTROPY_C=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y # PSA Crypto options diff --git a/tests/subsys/secure_storage/psa/crypto/overlay-secure_storage.conf b/tests/subsys/secure_storage/psa/crypto/overlay-secure_storage.conf index 84d933c2332ba2..063c04fd2b5e75 100644 --- a/tests/subsys/secure_storage/psa/crypto/overlay-secure_storage.conf +++ b/tests/subsys/secure_storage/psa/crypto/overlay-secure_storage.conf @@ -4,7 +4,6 @@ CONFIG_MAIN_STACK_SIZE=2048 CONFIG_MBEDTLS=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y CONFIG_SECURE_STORAGE=y diff --git a/tests/subsys/secure_storage/psa/its/overlay-default_transform.conf b/tests/subsys/secure_storage/psa/its/overlay-default_transform.conf index 52751db59b622e..2f49f5d6593aaa 100644 --- a/tests/subsys/secure_storage/psa/its/overlay-default_transform.conf +++ b/tests/subsys/secure_storage/psa/its/overlay-default_transform.conf @@ -1,7 +1,6 @@ CONFIG_MBEDTLS=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y # SETTINGS_MAX_VAL_LEN (256) - flags (1) - CONFIG_SECURE_STORAGE_ITS_TRANSFORM_OUTPUT_OVERHEAD (28) diff --git a/tests/subsys/storage/flash_map/overlay-psa.conf b/tests/subsys/storage/flash_map/overlay-psa.conf index 4b5dcfd9af67df..e70359a2d54e69 100644 --- a/tests/subsys/storage/flash_map/overlay-psa.conf +++ b/tests/subsys/storage/flash_map/overlay-psa.conf @@ -1,3 +1,4 @@ CONFIG_FLASH_AREA_CHECK_INTEGRITY_PSA=y CONFIG_MBEDTLS=y CONFIG_MBEDTLS_PSA_CRYPTO_C=y +CONFIG_TEST_RANDOM_GENERATOR=y