Skip to content

Commit 649741f

Browse files
committed
Merge branch 'feat/nvs_flash_deregister_sec_scheme' into 'master'
feat(nvs_flash): Added an API to deregister the NVS security scheme context Closes IDF-12456 and IDFGH-16210 See merge request espressif/esp-idf!41073
2 parents 33f92c8 + 536ec82 commit 649741f

File tree

25 files changed

+73
-13
lines changed

25 files changed

+73
-13
lines changed

components/esp_tee/test_apps/tee_cli_app/sdkconfig.ci.sb_fe

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CONFIG_SECURE_BOOT_SIGNING_KEY="test_keys/secure_boot_signing_key.pem"
99
# Flash Encryption
1010
CONFIG_SECURE_FLASH_ENC_ENABLED=y
1111
CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=y
12+
# NVS Encryption
13+
CONFIG_NVS_SEC_KEY_PROTECT_USING_FLASH_ENC=y
1214

1315
# TEE Secure Storage: Release mode
1416
CONFIG_SECURE_TEE_SEC_STG_MODE_RELEASE=y

components/nvs_flash/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ if(BOOTLOADER_BUILD)
77
"src/nvs_bootloader_xts_aes.c")
88

99
set(requires "esp_partition")
10+
set(priv_requires "mbedtls" "nvs_sec_provider")
1011

1112
idf_component_register(SRCS "${srcs}"
1213
REQUIRES "${requires}"
13-
PRIV_REQUIRES "mbedtls"
14+
PRIV_REQUIRES "${priv_requires}"
1415
INCLUDE_DIRS "include"
1516
PRIV_INCLUDE_DIRS "private_include"
1617
)
@@ -60,10 +61,9 @@ else()
6061
"src/nvs_bootloader.c")
6162

6263
set(requires esp_partition)
63-
if(${target} STREQUAL "linux")
64-
set(priv_requires spi_flash)
65-
else()
66-
set(priv_requires spi_flash esp_libc esptool_py)
64+
set(priv_requires spi_flash)
65+
if(NOT ${target} STREQUAL "linux")
66+
list(APPEND priv_requires esp_libc esptool_py nvs_sec_provider)
6767
endif()
6868

6969
idf_component_register(SRCS "${srcs}"

components/nvs_flash/include/nvs_flash.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -278,6 +278,13 @@ esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partition, nvs_sec_
278278
*/
279279
esp_err_t nvs_flash_register_security_scheme(nvs_sec_scheme_t *scheme_cfg);
280280

281+
/**
282+
* @brief Deregister the security scheme previously registered using
283+
* nvs_flash_register_security_scheme
284+
*
285+
*/
286+
void nvs_flash_deregister_security_scheme(void);
287+
281288
/**
282289
* @brief Fetch the configuration structure for the default active
283290
* security scheme for NVS encryption

components/nvs_flash/src/nvs_api.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -736,6 +736,11 @@ extern "C" esp_err_t nvs_flash_register_security_scheme(nvs_sec_scheme_t *scheme
736736
return ESP_OK;
737737
}
738738

739+
extern "C" void nvs_flash_deregister_security_scheme(void)
740+
{
741+
memset(&nvs_sec_default_scheme_cfg, 0x00, sizeof(nvs_sec_scheme_t));
742+
}
743+
739744
extern "C" nvs_sec_scheme_t *nvs_flash_get_default_security_scheme(void)
740745
{
741746
return &nvs_sec_default_scheme_cfg;

components/nvs_sec_provider/CMakeLists.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ if(${target} STREQUAL "linux")
44
return() # This component is not supported by the POSIX/Linux simulator
55
endif()
66

7-
if(BOOTLOADER_BUILD)
8-
set(srcs "nvs_bootloader_sec_provider.c")
9-
else()
10-
set(srcs "nvs_sec_provider.c")
7+
if(NOT CONFIG_NVS_SEC_KEY_PROTECT_NONE)
8+
if(BOOTLOADER_BUILD)
9+
set(srcs "nvs_bootloader_sec_provider.c")
10+
else()
11+
set(srcs "nvs_sec_provider.c")
12+
endif()
1113
endif()
1214

1315
idf_component_register(SRCS ${srcs}
@@ -22,4 +24,6 @@ idf_component_register(SRCS ${srcs}
2224
# Thus, the symbols from this component are not placed in the .map file and
2325
# hence the constructor, which initialises the encryption scheme for the default
2426
# NVS partition, never executes. The following is a workaround for the same.
25-
target_link_libraries(${COMPONENT_LIB} PRIVATE "-u nvs_sec_provider_include_impl")
27+
if(NOT CONFIG_NVS_SEC_KEY_PROTECT_NONE)
28+
target_link_libraries(${COMPONENT_LIB} PRIVATE "-u nvs_sec_provider_include_impl")
29+
endif()

components/nvs_sec_provider/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ menu "NVS Security Provider"
44
choice NVS_SEC_KEY_PROTECTION_SCHEME
55
prompt "NVS Encryption: Key Protection Scheme"
66
depends on NVS_ENCRYPTION
7-
default NVS_SEC_KEY_PROTECT_USING_FLASH_ENC
7+
default NVS_SEC_KEY_PROTECT_USING_HMAC if SOC_HMAC_SUPPORTED
8+
default NVS_SEC_KEY_PROTECT_USING_FLASH_ENC if !SOC_HMAC_SUPPORTED
89
help
910
This choice defines the default NVS encryption keys protection scheme;
1011
which will be used for the default NVS partition.
@@ -27,6 +28,12 @@ menu "NVS Security Provider"
2728
Requires the specified eFuse block (NVS_SEC_HMAC_EFUSE_KEY_ID or the v2 API argument)
2829
to be empty or pre-written with a key with the purpose ESP_EFUSE_KEY_PURPOSE_HMAC_UP
2930

31+
config NVS_SEC_KEY_PROTECT_NONE
32+
bool "None"
33+
help
34+
Select this option if key derivation/protection is handled by
35+
a custom implementation, and not by the nvs_sec_provider component.
36+
3037
endchoice
3138

3239
config NVS_SEC_HMAC_EFUSE_KEY_ID

components/nvs_sec_provider/nvs_sec_provider.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ esp_err_t nvs_sec_provider_deregister(nvs_sec_scheme_t *sec_scheme_handle)
291291

292292
free(sec_scheme_handle);
293293

294+
nvs_flash_deregister_security_scheme();
294295
return ESP_OK;
295296
}
296297

docs/en/api-reference/storage/nvs_encryption.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ The component :component:`nvs_sec_provider` stores all the implementation-specif
219219

220220
This component offers factory functions with which a particular security scheme can be registered without having to worry about the APIs to generate and read the encryption keys (e.g., :cpp:func:`nvs_sec_provider_register_hmac`). Refer to the :example:`security/nvs_encryption_hmac` example for API usage.
221221

222+
.. note::
223+
224+
To use a custom implementation for NVS encryption key derivation or protection (instead of the ones provided by the :component:`nvs_sec_provider` component), select the :ref:`CONFIG_NVS_SEC_KEY_PROTECTION_SCHEME` -> ``CONFIG_NVS_SEC_KEY_PROTECT_NONE`` configuration option.
222225

223226
API Reference
224227
-------------

docs/en/migration-guides/release-6.x/6.0/security.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ Bootloader Support
3030
The following deprecated functions have been removed:
3131

3232
- :cpp:func:`esp_secure_boot_verify_signature_block` – Use :cpp:func:`esp_secure_boot_verify_ecdsa_signature_block` instead.
33+
34+
.. only:: SOC_HMAC_SUPPORTED
35+
36+
NVS Security Provider
37+
---------------------
38+
39+
- When NVS encryption is enabled on SoCs with the HMAC peripheral that have flash encryption enabled, the HMAC-based NVS encryption scheme is now selected as default instead of the flash encryption-based scheme. If your application previously used the flash encryption-based scheme, you need to manually configure the NVS encryption scheme to flash encryption from HMAC through ``menuconfig`` or your project's ``sdkconfig`` (i.e., setting ``CONFIG_NVS_SEC_KEY_PROTECT_USING_FLASH_ENC=y``).

docs/zh_CN/api-reference/storage/nvs_encryption.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ NVS Security Provider
219219

220220
该组件通过工厂函数注册了特殊的安全框架,可以实现出厂即用的安全方案。在该方案中,无需使用 API 来生成、读取加密密钥(如 :cpp:func:`nvs_sec_provider_register_hmac`)。要了解 API 的使用,参考示例 :example:`security/nvs_encryption_hmac`。
221221

222+
.. note::
223+
224+
如果不希望使用 :component: `nvs_sec_provider` 组件的默认实现,而使用自定义方式生成或者保护 NVS 加密密钥,请选择 :ref:`CONFIG_NVS_SEC_KEY_PROTECTION_SCHEME` -> ``CONFIG_NVS_SEC_KEY_PROTECT_NONE`` 配置项。
222225

223226
API 参考
224227
-------------

0 commit comments

Comments
 (0)