Skip to content

Commit 06bd290

Browse files
committed
Merge branch 'bugfix/esp32c5_flash_enc_issue' into 'master'
fix(bootloader): self encryption workflow in bootloader not working on C5 Closes IDF-11229 See merge request espressif/esp-idf!33621
2 parents 72f83c3 + e52e2d2 commit 06bd290

File tree

6 files changed

+95
-53
lines changed

6 files changed

+95
-53
lines changed

components/bootloader_support/include/esp_flash_encrypt.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@ void esp_flash_encryption_init_checks(void);
180180
/** @brief Set all secure eFuse features related to flash encryption
181181
*
182182
* @return
183-
* - ESP_OK - Successfully
183+
* - ESP_OK - On success
184184
*/
185185
esp_err_t esp_flash_encryption_enable_secure_features(void);
186+
187+
/** @brief Enable the key manager for flash encryption
188+
*
189+
* @return
190+
* - ESP_OK - On success
191+
*/
192+
esp_err_t esp_flash_encryption_enable_key_mgr(void);
193+
186194
#endif /* BOOTLOADER_BUILD && CONFIG_SECURE_FLASH_ENC_ENABLED */
187195

188196
/** @brief Returns the verification status for all physical security features of flash encryption in release mode

components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "esp_efuse_table.h"
1212
#include "esp_log.h"
1313
#include "sdkconfig.h"
14+
#include "soc/keymng_reg.h"
15+
#include "soc/pcr_reg.h"
16+
#include "soc/pcr_struct.h"
1417

1518
static __attribute__((unused)) const char *TAG = "flash_encrypt";
1619

@@ -58,3 +61,31 @@ esp_err_t esp_flash_encryption_enable_secure_features(void)
5861

5962
return ESP_OK;
6063
}
64+
65+
// TODO: Update to use LL APIs once key manager support added in IDF-8621
66+
esp_err_t esp_flash_encryption_enable_key_mgr(void)
67+
{
68+
// Set the force power down bit to 0 to enable key manager
69+
PCR.km_pd_ctrl.km_mem_force_pd = 0;
70+
// Reset the key manager
71+
PCR.km_conf.km_clk_en = 1;
72+
PCR.km_conf.km_rst_en = 1;
73+
PCR.km_conf.km_rst_en = 0;
74+
75+
// Wait for key manager to be ready
76+
while (!PCR.km_conf.km_ready) {
77+
};
78+
79+
// Wait for key manager state machine to be idle
80+
while (REG_READ(KEYMNG_STATE_REG) != 0) {
81+
};
82+
83+
// Set the key manager to use efuse key
84+
REG_SET_FIELD(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY, 2);
85+
86+
// Reset MSPI to re-load the flash encryption key
87+
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
88+
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
89+
90+
return ESP_OK;
91+
}

components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,8 @@
1111
#include "esp_efuse_table.h"
1212
#include "esp_log.h"
1313
#include "sdkconfig.h"
14+
#include "hal/key_mgr_ll.h"
15+
#include "hal/mspi_timing_tuning_ll.h"
1416

1517
static __attribute__((unused)) const char *TAG = "flash_encrypt";
1618

@@ -48,3 +50,22 @@ esp_err_t esp_flash_encryption_enable_secure_features(void)
4850

4951
return ESP_OK;
5052
}
53+
54+
esp_err_t esp_flash_encryption_enable_key_mgr(void)
55+
{
56+
// Enable and reset key manager
57+
// To suppress build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
58+
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));
59+
key_mgr_ll_enable_bus_clock(true);
60+
key_mgr_ll_enable_peripheral_clock(true);
61+
key_mgr_ll_reset_register();
62+
63+
while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) {
64+
};
65+
66+
// Force Key Manager to use eFuse key for XTS-AES operation
67+
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
68+
_mspi_timing_ll_reset_mspi();
69+
70+
return ESP_OK;
71+
}

components/bootloader_support/src/flash_encryption/flash_encrypt.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,7 @@
1515
#include "esp_efuse_table.h"
1616
#include "esp_log.h"
1717
#include "hal/wdt_hal.h"
18-
19-
// Need to remove check and merge accordingly for ESP32C5 once key manager support added in IDF-8621
20-
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
21-
#if CONFIG_IDF_TARGET_ESP32C5
22-
#include "soc/keymng_reg.h"
23-
#include "soc/pcr_reg.h"
24-
#else /* CONFIG_IDF_TARGET_ESP32C5 */
25-
#include "hal/key_mgr_ll.h"
26-
#include "hal/mspi_timing_tuning_ll.h"
27-
#endif /* !CONFIG_IDF_TARGET_ESP32C5 */
28-
#endif /* SOC_KEY_MANAGER_FE_KEY_DEPLOY */
18+
#include "sdkconfig.h"
2919

3020
#ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
3121
#include "soc/sensitive_reg.h"
@@ -221,26 +211,6 @@ static esp_err_t check_and_generate_encryption_keys(void)
221211
}
222212
ESP_LOGI(TAG, "Using pre-loaded flash encryption key in efuse");
223213
}
224-
// Need to remove check for ESP32C5 and merge accordingly once key manager support added in IDF-8621
225-
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
226-
#if CONFIG_IDF_TARGET_ESP32C5
227-
REG_SET_FIELD(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY, 2);
228-
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
229-
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
230-
#else /* CONFIG_IDF_TARGET_ESP32C5 */
231-
// Enable and reset key manager
232-
// To suppress build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
233-
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));
234-
key_mgr_ll_enable_bus_clock(true);
235-
key_mgr_ll_enable_peripheral_clock(true);
236-
key_mgr_ll_reset_register();
237-
while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) {
238-
};
239-
// Force Key Manager to use eFuse key for XTS-AES operation
240-
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
241-
_mspi_timing_ll_reset_mspi();
242-
#endif /* !CONFIG_IDF_TARGET_ESP32C5 */
243-
#endif /* SOC_KEY_MANAGER_FE_KEY_DEPLOY */
244214

245215
return ESP_OK;
246216
}
@@ -288,6 +258,11 @@ esp_err_t esp_flash_encrypt_contents(void)
288258
REG_WRITE(SENSITIVE_XTS_AES_KEY_UPDATE_REG, 1);
289259
#endif
290260

261+
// TODO: Remove C5 target config after key manager LL support- see IDF-8621
262+
#if CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
263+
esp_flash_encryption_enable_key_mgr();
264+
#endif
265+
291266
err = encrypt_bootloader();
292267
if (err != ESP_OK) {
293268
return err;

components/esp_security/src/init.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,44 @@
66

77
#include "esp_private/startup_internal.h"
88
#include "sdkconfig.h"
9+
#include "soc/soc_caps.h"
910
#include "esp_crypto_clk.h"
1011
#include "esp_efuse.h"
1112
#include "esp_efuse_table.h"
1213
#include "esp_security_priv.h"
1314
#include "esp_err.h"
15+
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY
16+
#include "hal/key_mgr_ll.h"
17+
#endif
1418

1519
__attribute__((unused)) static const char *TAG = "esp_security";
1620

21+
static void esp_key_mgr_init(void)
22+
{
23+
// The following operation makes the Key Manager to use eFuse key for ECDSA and XTS-AES operation by default
24+
// This is to keep the default behavior same as the other chips
25+
// If the Key Manager configuration is already locked then following operation does not have any effect
26+
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY
27+
// Enable key manager clock
28+
// Using ll APIs which do not require critical section
29+
_key_mgr_ll_enable_bus_clock(true);
30+
_key_mgr_ll_enable_peripheral_clock(true);
31+
32+
while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) {
33+
};
34+
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY
35+
key_mgr_ll_set_key_usage(ESP_KEY_MGR_ECDSA_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
36+
#endif
37+
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY
38+
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
39+
#endif
40+
#endif /* SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY */
41+
}
42+
1743
ESP_SYSTEM_INIT_FN(esp_security_init, SECONDARY, BIT(0), 103)
1844
{
1945
esp_crypto_clk_init();
46+
esp_key_mgr_init();
2047
#if CONFIG_ESP_CRYPTO_DPA_PROTECTION_AT_STARTUP
2148
esp_crypto_dpa_protection_startup();
2249
#endif

components/esp_system/port/cpu_start.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@
7171
#include "soc/hp_sys_clkrst_reg.h"
7272
#endif
7373

74-
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY
75-
#include "hal/key_mgr_ll.h"
76-
#endif
77-
7874
#include "esp_private/rtc_clk.h"
7975

8076
#if SOC_INT_CLIC_SUPPORTED
@@ -319,22 +315,6 @@ static void start_other_core(void)
319315
}
320316
#endif
321317

322-
// The following operation makes the Key Manager to use eFuse key for ECDSA and XTS-AES operation by default
323-
// This is to keep the default behavior same as the other chips
324-
// If the Key Manager configuration is already locked then following operation does not have any effect
325-
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY
326-
// Enable key manager clock
327-
// Using ll APIs which do not require critical section
328-
_key_mgr_ll_enable_bus_clock(true);
329-
_key_mgr_ll_enable_peripheral_clock(true);
330-
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY
331-
key_mgr_ll_set_key_usage(ESP_KEY_MGR_ECDSA_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
332-
#endif
333-
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY
334-
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
335-
#endif
336-
#endif /* SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY */
337-
338318
ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
339319

340320
bool cpus_up = false;

0 commit comments

Comments
 (0)