From 540c719c66e813915e10c8883fe5f85301d726eb Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Wed, 15 Oct 2025 00:04:13 +0530 Subject: [PATCH] change(esp_key_mgr): Make Key Manager driver bootloader compatible - Independent of heap --- .../main/ld/esp32c5/bootloader.ld.in | 5 + .../main/ld/esp32p4/bootloader.ld.in | 1 + .../main/ld/esp32p4/bootloader.rev3.ld.in | 6 +- .../flash_encryption_secure_features.c | 4 +- .../flash_encryption_secure_features.c | 8 +- components/esp_security/CMakeLists.txt | 4 +- components/esp_security/include/esp_key_mgr.h | 6 +- .../esp_security/src/esp_crypto_periph_clk.c | 5 + components/esp_security/src/esp_key_mgr.c | 112 ++++-------------- 9 files changed, 48 insertions(+), 103 deletions(-) diff --git a/components/bootloader/subproject/main/ld/esp32c5/bootloader.ld.in b/components/bootloader/subproject/main/ld/esp32c5/bootloader.ld.in index b392b18adb3..aad2b39b2db 100644 --- a/components/bootloader/subproject/main/ld/esp32c5/bootloader.ld.in +++ b/components/bootloader/subproject/main/ld/esp32c5/bootloader.ld.in @@ -91,6 +91,11 @@ SECTIONS *libhal.a:cache_hal.*(.literal .text .literal.* .text.*) *libhal.a:efuse_hal.*(.literal .text .literal.* .text.*) *libesp_hal_wdt.a:wdt_hal_iram.*(.literal .text .literal.* .text.*) + *libhal.a:huk_hal.*(.literal .text .literal.* .text.*) + *libhal.a:key_mgr_hal.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_key_mgr.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_crypto_periph_clk.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_crypto_lock.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_clk.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_time.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:regi2c_ctrl.*(.literal .text .literal.* .text.*) diff --git a/components/bootloader/subproject/main/ld/esp32p4/bootloader.ld.in b/components/bootloader/subproject/main/ld/esp32p4/bootloader.ld.in index 0ee5dfe65f8..1d763d2d222 100644 --- a/components/bootloader/subproject/main/ld/esp32p4/bootloader.ld.in +++ b/components/bootloader/subproject/main/ld/esp32p4/bootloader.ld.in @@ -90,6 +90,7 @@ SECTIONS *libhal.a:cache_hal.*(.literal .text .literal.* .text.*) *libhal.a:efuse_hal.*(.literal .text .literal.* .text.*) *libhal.a:key_mgr_hal.*(.literal.key_mgr_hal_set_key_usage .text.key_mgr_hal_set_key_usage) + *libesp_security.a:esp_crypto_periph_clk.*(.literal .text .literal.* .text.*) *libesp_hal_wdt.a:wdt_hal_iram.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_clk.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_time.*(.literal .text .literal.* .text.*) diff --git a/components/bootloader/subproject/main/ld/esp32p4/bootloader.rev3.ld.in b/components/bootloader/subproject/main/ld/esp32p4/bootloader.rev3.ld.in index 9a6ae2ddf11..f51c5e63afe 100644 --- a/components/bootloader/subproject/main/ld/esp32p4/bootloader.rev3.ld.in +++ b/components/bootloader/subproject/main/ld/esp32p4/bootloader.rev3.ld.in @@ -89,8 +89,12 @@ SECTIONS *libhal.a:mmu_hal.*(.literal .text .literal.* .text.*) *libhal.a:cache_hal.*(.literal .text .literal.* .text.*) *libhal.a:efuse_hal.*(.literal .text .literal.* .text.*) - *libhal.a:key_mgr_hal.*(.literal.key_mgr_hal_set_key_usage .text.key_mgr_hal_set_key_usage) *libesp_hal_wdt.a:wdt_hal_iram.*(.literal .text .literal.* .text.*) + *libhal.a:huk_hal.*(.literal .text .literal.* .text.*) + *libhal.a:key_mgr_hal.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_key_mgr.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_crypto_periph_clk.*(.literal .text .literal.* .text.*) + *libesp_security.a:esp_crypto_lock.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_clk.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_time.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:regi2c_ctrl.*(.literal .text .literal.* .text.*) diff --git a/components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c b/components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c index 80b2407ff85..a1804969bfd 100644 --- a/components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c +++ b/components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c @@ -11,6 +11,7 @@ #include "esp_efuse.h" #include "esp_efuse_table.h" #include "esp_log.h" +#include "esp_crypto_periph_clk.h" #include "esp_key_mgr.h" #include "hal/key_mgr_hal.h" #include "hal/mspi_ll.h" @@ -73,13 +74,10 @@ esp_err_t esp_flash_encryption_enable_secure_features(void) esp_err_t esp_flash_encryption_enable_key_mgr(void) { -#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_EFUSES esp_crypto_key_mgr_enable_periph_clk(true); - key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); // Force Key Manager to use eFuse key for XTS-AES operation key_mgr_hal_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY); -#endif // In case Flash Encryption is enabled by a key deployed using the Key Manager, // we just need to reset the SPI flash to ensure the key is used. diff --git a/components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c b/components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c index cf7a4a8c2d1..c111cbf665c 100644 --- a/components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c +++ b/components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c @@ -11,6 +11,7 @@ #include "esp_efuse_table.h" #include "esp_log.h" #include "sdkconfig.h" +#include "esp_crypto_periph_clk.h" #include "hal/key_mgr_ll.h" #include "hal/mspi_ll.h" @@ -53,12 +54,7 @@ esp_err_t esp_flash_encryption_enable_secure_features(void) esp_err_t esp_flash_encryption_enable_key_mgr(void) { - _key_mgr_ll_enable_bus_clock(true); - _key_mgr_ll_enable_peripheral_clock(true); - _key_mgr_ll_reset_register(); - - while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) { - }; + esp_crypto_key_mgr_enable_periph_clk(true); // Force Key Manager to use eFuse key for XTS-AES operation key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY); diff --git a/components/esp_security/CMakeLists.txt b/components/esp_security/CMakeLists.txt index 745b0480700..362f942aebd 100644 --- a/components/esp_security/CMakeLists.txt +++ b/components/esp_security/CMakeLists.txt @@ -6,7 +6,7 @@ if(${target} STREQUAL "linux") endif() set(srcs "") -set(priv_requires "esp_hw_support") +set(priv_requires esp_hw_support hal efuse) set(priv_includes "") if(NOT non_os_build) @@ -30,7 +30,7 @@ if(NOT non_os_build) endif() list(APPEND srcs "src/esp_crypto_lock.c" "src/esp_crypto_periph_clk.c") - list(APPEND priv_requires efuse esp_system esp_timer) + list(APPEND priv_requires esp_system esp_timer) elseif(esp_tee_build) list(APPEND srcs "src/esp_crypto_lock.c" "src/esp_crypto_periph_clk.c") list(APPEND includes "src/${IDF_TARGET}") diff --git a/components/esp_security/include/esp_key_mgr.h b/components/esp_security/include/esp_key_mgr.h index 91488b1cbb9..09f7fb4cf59 100644 --- a/components/esp_security/include/esp_key_mgr.h +++ b/components/esp_security/include/esp_key_mgr.h @@ -73,7 +73,7 @@ void key_mgr_wait_for_state(esp_key_mgr_state_t state); * @input * key_config(input) AES key configuration * key_info(output) A writable struct of esp_key_mgr_key_info_t type. - * The recovery information for the the deployed key shall be stored here + * The recovery information for the the deployed key shall be stored here (Make sure that the memory is valid during the deployment process). * @return * ESP_OK for success * ESP_FAIL/relevant error code for failure @@ -84,7 +84,7 @@ esp_err_t esp_key_mgr_deploy_key_in_aes_mode(const esp_key_mgr_aes_key_config_t * @brief Deploy key in ECDH0 deployment mode * @input * key_config(input) ECDH0 key configuration - * key_info(output) A writable struct of esp_key_mgr_key_info_t type. The recovery key info for the deployed key shall be stored here + * key_info(output) A writable struct of esp_key_mgr_key_info_t type. The recovery key info for the deployed key shall be stored here (Make sure that the memory is valid during the deployment process). * ecdh0_key_info A writable struct of esp_key_mgr_ecdh0_info_t. The ecdh0 info to recover the actual key shall be stored here. * @return * ESP_OK for success @@ -96,7 +96,7 @@ esp_err_t esp_key_mgr_deploy_key_in_ecdh0_mode(const esp_key_mgr_ecdh0_key_confi * @brief Deploy key in Random deployment mode * @input * key_config(input) Random key configuration - * key_info(output) A writable struct of esp_key_mgr_key_info_t type. The recovery key info for the deployed key shall be stored here + * key_info(output) A writable struct of esp_key_mgr_key_info_t type. The recovery key info for the deployed key shall be stored here (Make sure that the memory is valid during the deployment process). * @return * ESP_OK for success * ESP_FAIL/relevant error code for failure diff --git a/components/esp_security/src/esp_crypto_periph_clk.c b/components/esp_security/src/esp_crypto_periph_clk.c index 12f245da550..329f6c8eca6 100644 --- a/components/esp_security/src/esp_crypto_periph_clk.c +++ b/components/esp_security/src/esp_crypto_periph_clk.c @@ -39,6 +39,11 @@ #include "hal/crypto_dma_ll.h" #endif +#if NON_OS_BUILD +// To suppress build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV +int __DECLARE_RCC_ATOMIC_ENV __attribute__((unused)); +#endif + #if SOC_AES_SUPPORTED void esp_crypto_aes_enable_periph_clk(bool enable) { diff --git a/components/esp_security/src/esp_key_mgr.c b/components/esp_security/src/esp_key_mgr.c index ccc53f93a8d..f1ccecb4b0c 100644 --- a/components/esp_security/src/esp_key_mgr.c +++ b/components/esp_security/src/esp_key_mgr.c @@ -11,7 +11,6 @@ #include "esp_crypto_lock.h" #include "esp_log.h" #include "esp_err.h" -#include "esp_heap_caps.h" #include "esp_rom_crc.h" #include "esp_efuse.h" #include "hal/key_mgr_types.h" @@ -126,6 +125,7 @@ static void esp_key_mgr_release_key_lock(esp_key_mgr_key_type_t key_type) case ESP_KEY_MGR_DS_KEY: case ESP_KEY_MGR_PSRAM_128_KEY: case ESP_KEY_MGR_PSRAM_256_KEY: + break; default: ESP_LOGE(TAG, "Invalid key type"); break; @@ -228,8 +228,6 @@ typedef struct { esp_key_mgr_huk_info_t *huk_recovery_info; } huk_deploy_config_t; -static const uint8_t zeros[KEY_MGR_HUK_INFO_SIZE] = {0}; - static esp_err_t configure_huk(esp_huk_mode_t huk_mode, uint8_t *huk_info) { esp_err_t ret = huk_hal_configure(huk_mode, huk_info); @@ -242,11 +240,9 @@ static esp_err_t configure_huk(esp_huk_mode_t huk_mode, uint8_t *huk_info) huk_hal_recharge_huk_memory(); ret = huk_hal_configure(huk_mode, huk_info); if (ret != ESP_OK) { - // heap_caps_free(huk_recovery_info_zeros); return ret; } } - // heap_caps_free(huk_recovery_info_zeros); #endif if (!key_mgr_hal_is_huk_valid()) { @@ -260,13 +256,6 @@ static esp_err_t deploy_huk(huk_deploy_config_t *config) { esp_err_t esp_ret = ESP_FAIL; - // TODO: Could we use config->huk_recovery_info->info directly instead of allocating a copy of it? - // Advantage: BOOTLOADER_BUILD would be able to use this function. - // Note: We can memset it to zeros in case of an error. - uint8_t *huk_recovery_info = (uint8_t *) heap_caps_calloc(1, KEY_MGR_HUK_INFO_SIZE, MALLOC_CAP_INTERNAL); - if (!huk_recovery_info) { - return ESP_ERR_NO_MEM; - } if (config->use_pre_generated_huk_info) { ESP_LOGD(TAG, "Using pre-generated HUK info"); @@ -275,41 +264,34 @@ static esp_err_t deploy_huk(huk_deploy_config_t *config) if (!check_huk_info_validity(config->pre_generated_huk_info)) { ESP_LOGE(TAG, "HUK info is not valid"); - heap_caps_free(huk_recovery_info); return ESP_ERR_INVALID_ARG; } - memcpy(huk_recovery_info, config->pre_generated_huk_info->info, KEY_MGR_HUK_INFO_SIZE); ESP_LOGD(TAG, "Recovering HUK from given HUK recovery info"); - esp_ret = configure_huk(ESP_HUK_MODE_RECOVERY, huk_recovery_info); + esp_ret = configure_huk(ESP_HUK_MODE_RECOVERY, (uint8_t *) config->pre_generated_huk_info->info); if (esp_ret != ESP_OK) { ESP_LOGE(TAG, "Failed to recover HUK"); - heap_caps_free(huk_recovery_info); return esp_ret; } // Copy the pre generated huk info in the output key recovery info - memcpy(config->huk_recovery_info->info, huk_recovery_info, KEY_MGR_HUK_INFO_SIZE); + memcpy(config->huk_recovery_info->info, config->pre_generated_huk_info->info, KEY_MGR_HUK_INFO_SIZE); config->huk_recovery_info->crc = config->pre_generated_huk_info->crc; } else { // Generate new HUK and corresponding HUK info ESP_LOGD(TAG, "Generating new HUK"); - esp_ret = configure_huk(ESP_HUK_MODE_GENERATION, huk_recovery_info); + esp_ret = configure_huk(ESP_HUK_MODE_GENERATION, config->huk_recovery_info->info); if (esp_ret != ESP_OK) { ESP_LOGE(TAG, "Failed to generate HUK"); - heap_caps_free(huk_recovery_info); + memset(config->huk_recovery_info->info, 0, KEY_MGR_HUK_INFO_SIZE); return esp_ret; } - memcpy(config->huk_recovery_info->info, huk_recovery_info, KEY_MGR_HUK_INFO_SIZE); - config->huk_recovery_info->crc = esp_rom_crc32_le(0, huk_recovery_info, KEY_MGR_HUK_INFO_SIZE); + config->huk_recovery_info->crc = esp_rom_crc32_le(0, config->huk_recovery_info->info, KEY_MGR_HUK_INFO_SIZE); } - ESP_LOG_BUFFER_HEX_LEVEL("HUK INFO", huk_recovery_info, KEY_MGR_HUK_INFO_SIZE, ESP_LOG_DEBUG); - // Free the local buffer for huk recovery info - heap_caps_free(huk_recovery_info); return ESP_OK; } @@ -342,14 +324,13 @@ static esp_err_t key_mgr_deploy_key_aes_mode(aes_deploy_config_t *config) ESP_LOGD(TAG, "HUK deployed successfully"); } - // TODO: Could we use config->key_info->info directly instead of allocating a copy of it? - // Advantage: BOOTLOADER_BUILD would be able to use this function. - // Note: We can memset it to zeros in case of an error. - uint8_t *key_recovery_info = (uint8_t *) heap_caps_calloc(1, KEY_MGR_KEY_RECOVERY_INFO_SIZE, MALLOC_CAP_INTERNAL); - if (!key_recovery_info) { - return ESP_ERR_NO_MEM; + uint8_t key_recovery_info_index = 0; + if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { + key_recovery_info_index = 1; } + uint8_t *key_recovery_info = config->key_info->key_info[key_recovery_info_index].info; + // STEP 1: Init Step // Set mode key_mgr_hal_set_key_generator_mode(ESP_KEY_MGR_KEYGEN_MODE_AES); @@ -370,7 +351,6 @@ static esp_err_t key_mgr_deploy_key_aes_mode(aes_deploy_config_t *config) key_mgr_hal_use_sw_init_key(); } else if (!esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_KM_INIT_KEY, NULL)) { ESP_LOGE(TAG, "Could not find key with purpose KM_INIT_KEY"); - heap_caps_free(key_recovery_info); return ESP_FAIL; } @@ -381,15 +361,12 @@ static esp_err_t key_mgr_deploy_key_aes_mode(aes_deploy_config_t *config) if (config->key_config->use_pre_generated_sw_init_key) { key_mgr_hal_write_sw_init_key(config->key_config->sw_init_key, KEY_MGR_SW_INIT_KEY_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("SW_INIT_KEY", config->key_config->sw_init_key, KEY_MGR_SW_INIT_KEY_SIZE, ESP_LOG_DEBUG); } ESP_LOGD(TAG, "Writing Information into Key Manager Registers"); key_mgr_hal_write_assist_info(config->key_config->k2_info, KEY_MGR_K2_INFO_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("K2_INFO", config->key_config->k2_info, KEY_MGR_K2_INFO_SIZE, ESP_LOG_DEBUG); key_mgr_hal_write_public_info(config->k1_encrypted, KEY_MGR_K1_ENCRYPTED_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("K1_ENCRYPTED", config->k1_encrypted, KEY_MGR_K1_ENCRYPTED_SIZE, ESP_LOG_DEBUG); key_mgr_hal_continue(); @@ -397,12 +374,10 @@ static esp_err_t key_mgr_deploy_key_aes_mode(aes_deploy_config_t *config) key_mgr_wait_for_state(ESP_KEY_MGR_STATE_GAIN); key_mgr_hal_read_public_info(key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("KEY_RECOVERY_INFO", key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE, ESP_LOG_DEBUG); if (config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_1 && config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_1) { if (!key_mgr_hal_is_key_deployment_valid(key_type)) { ESP_LOGE(TAG, "Key deployment is not valid"); - heap_caps_free(key_recovery_info); return ESP_FAIL; } } @@ -412,17 +387,7 @@ static esp_err_t key_mgr_deploy_key_aes_mode(aes_deploy_config_t *config) key_mgr_hal_continue(); key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); - if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { - memcpy(config->key_info->key_info[1].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[1].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - - } else { - memcpy(config->key_info->key_info[0].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[0].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - } - - heap_caps_free(key_recovery_info); - + config->key_info->key_info[key_recovery_info_index].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); config->key_info->key_type = key_type; config->key_info->magic = KEY_HUK_SECTOR_MAGIC; @@ -437,8 +402,6 @@ esp_err_t esp_key_mgr_deploy_key_in_aes_mode(const esp_key_mgr_aes_key_config_t ESP_LOGD(TAG, "Key deployment in AES mode"); - // TODO: Would making this static help in saving static memory? - // if so, memset the structure to 0 before using it aes_deploy_config_t aes_deploy_config = { .key_config = key_config, .key_info = key_recovery_info, @@ -504,7 +467,6 @@ static esp_err_t key_mgr_recover_key(key_recovery_config_t *config) } ESP_LOGD(TAG, "HUK recovered successfully"); - ESP_LOG_BUFFER_HEX_LEVEL("HUK INFO", config->key_recovery_info->huk_info.info, KEY_MGR_HUK_INFO_SIZE, ESP_LOG_DEBUG); config->huk_recovered = true; } @@ -530,14 +492,12 @@ static esp_err_t key_mgr_recover_key(key_recovery_config_t *config) return ESP_FAIL; } key_mgr_hal_write_assist_info(config->key_recovery_info->key_info[1].info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("RECOVERY_INFO[1]", config->key_recovery_info->key_info[0].info, KEY_MGR_KEY_RECOVERY_INFO_SIZE, ESP_LOG_DEBUG); } else { if (!check_key_info_validity(&config->key_recovery_info->key_info[0])) { ESP_LOGE(TAG, "Key info not valid"); return ESP_FAIL; } key_mgr_hal_write_assist_info(config->key_recovery_info->key_info[0].info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("RECOVERY_INFO[0]", config->key_recovery_info->key_info[0].info, KEY_MGR_KEY_RECOVERY_INFO_SIZE, ESP_LOG_DEBUG); } key_mgr_hal_continue(); @@ -567,8 +527,6 @@ esp_err_t esp_key_mgr_activate_key(esp_key_mgr_key_recovery_info_t *key_recovery ESP_LOGD(TAG, "Activating key of type %d", key_type); - // TODO: Would making this static help in saving static memory? - // if so, memset the structure to 0 before using it key_recovery_config_t key_recovery_config = { .key_recovery_info = key_recovery_info, }; @@ -650,11 +608,13 @@ static esp_err_t key_mgr_deploy_key_ecdh0_mode(ecdh0_deploy_config_t *config) ESP_LOGD(TAG, "HUK deployed successfully"); } - uint8_t *key_recovery_info = (uint8_t *) heap_caps_calloc(1, KEY_MGR_KEY_RECOVERY_INFO_SIZE, MALLOC_CAP_INTERNAL); - if (!key_recovery_info) { - return ESP_ERR_NO_MEM; + uint8_t key_recovery_info_index = 0; + if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { + key_recovery_info_index = 1; } + uint8_t *key_recovery_info = config->key_info->key_info[key_recovery_info_index].info; + // Step 1 : Initialization // Configure deployment mode to ECDH0 key_mgr_hal_set_key_generator_mode(ESP_KEY_MGR_KEYGEN_MODE_ECDH0); @@ -686,12 +646,10 @@ static esp_err_t key_mgr_deploy_key_ecdh0_mode(ecdh0_deploy_config_t *config) key_mgr_hal_read_public_info(key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); key_mgr_hal_read_assist_info(config->ecdh0_key_info); - ESP_LOG_BUFFER_HEX_LEVEL("KEY_RECOVERY_INFO", key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE, ESP_LOG_DEBUG); if (config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_1 && config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_1) { if (!key_mgr_hal_is_key_deployment_valid(key_type)) { ESP_LOGE(TAG, "Key deployment is not valid"); - heap_caps_free(key_recovery_info); return ESP_FAIL; } } @@ -701,16 +659,7 @@ static esp_err_t key_mgr_deploy_key_ecdh0_mode(ecdh0_deploy_config_t *config) key_mgr_hal_continue(); key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); - if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { - memcpy(config->key_info->key_info[1].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[1].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - } else { - memcpy(config->key_info->key_info[0].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[0].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - } - - heap_caps_free(key_recovery_info); - + config->key_info->key_info[key_recovery_info_index].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); config->key_info->key_type = key_type; config->key_info->magic = KEY_HUK_SECTOR_MAGIC; @@ -728,8 +677,6 @@ esp_err_t esp_key_mgr_deploy_key_in_ecdh0_mode(const esp_key_mgr_ecdh0_key_confi esp_key_mgr_key_type_t key_type = key_config->key_type; - // TODO: Would making this static help in saving static memory? - // if so, memset the structure to 0 before using it ecdh0_deploy_config_t ecdh0_deploy_config = { .key_config = key_config, .key_info = key_info, @@ -799,11 +746,13 @@ static esp_err_t key_mgr_deploy_key_random_mode(random_deploy_config_t *config) ESP_LOGD(TAG, "HUK deployed successfully"); } - uint8_t *key_recovery_info = (uint8_t *) heap_caps_calloc(1, KEY_MGR_KEY_RECOVERY_INFO_SIZE, MALLOC_CAP_INTERNAL); - if (!key_recovery_info) { - return ESP_ERR_NO_MEM; + uint8_t key_recovery_info_index = 0; + if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { + key_recovery_info_index = 1; } + uint8_t *key_recovery_info = config->key_info->key_info[key_recovery_info_index].info; + // Configure deployment mode to RANDOM key_mgr_hal_set_key_generator_mode(ESP_KEY_MGR_KEYGEN_MODE_RANDOM); @@ -827,12 +776,10 @@ static esp_err_t key_mgr_deploy_key_random_mode(random_deploy_config_t *config) // No configuration for Random deploy mode key_mgr_wait_for_state(ESP_KEY_MGR_STATE_GAIN); key_mgr_hal_read_public_info(key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - ESP_LOG_BUFFER_HEX_LEVEL("KEY_RECOVERY_INFO", key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE, ESP_LOG_DEBUG); if (config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_1 && config->key_purpose != ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_1) { if (!key_mgr_hal_is_key_deployment_valid(key_type)) { ESP_LOGE(TAG, "Key deployment is not valid"); - heap_caps_free(key_recovery_info); return ESP_FAIL; } } @@ -842,16 +789,7 @@ static esp_err_t key_mgr_deploy_key_random_mode(random_deploy_config_t *config) key_mgr_hal_continue(); key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); - if (config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_XTS_AES_256_2 || config->key_purpose == ESP_KEY_MGR_KEY_PURPOSE_PSRAM_256_2) { - memcpy(config->key_info->key_info[1].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[1].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - } else { - memcpy(config->key_info->key_info[0].info, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - config->key_info->key_info[0].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); - } - - heap_caps_free(key_recovery_info); - + config->key_info->key_info[key_recovery_info_index].crc = esp_rom_crc32_le(0, key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); config->key_info->key_type = key_type; config->key_info->magic = KEY_HUK_SECTOR_MAGIC; @@ -866,8 +804,6 @@ esp_err_t esp_key_mgr_deploy_key_in_random_mode(const esp_key_mgr_random_key_con ESP_LOGD(TAG, "Key deployment in Random mode"); - // TODO: Would making this static help in saving static memory? - // if so, memset the structure to 0 before using it random_deploy_config_t random_deploy_config = { .key_config = key_config, .key_info = key_recovery_info,