From 5a1726c18c87df42ecf45192330377bc3f025bdf Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 12 Oct 2023 16:55:40 +0530 Subject: [PATCH] feat(hal): Update HAL layer for Key manager 1)Added new clk related changes --- components/esp_hw_support/esp_key_mgr.c | 53 ++++++++++++++----- .../esp_hw_support/include/esp_key_mgr.h | 4 +- components/hal/esp32p4/include/hal/huk_ll.h | 14 ++--- .../hal/esp32p4/include/hal/key_mgr_ll.h | 2 - components/hal/huk_hal.c | 1 + 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/components/esp_hw_support/esp_key_mgr.c b/components/esp_hw_support/esp_key_mgr.c index c5b2e2d232..57930d9555 100644 --- a/components/esp_hw_support/esp_key_mgr.c +++ b/components/esp_hw_support/esp_key_mgr.c @@ -9,11 +9,15 @@ #include "hal/huk_types.h" #include "hal/huk_hal.h" #include "esp_key_mgr.h" -#include "hal/clk_gate_ll.h" +#include "esp_private/periph_ctrl.h" +#include "hal/key_mgr_ll.h" #include "esp_log.h" #include "esp_err.h" #include "assert.h" #include "string.h" +#include "esp_heap_caps.h" + +#define KEY_MANAGER_RCC_ATOMIC() PERIPH_RCC_ATOMIC() static const char *TAG = "esp_key_mgr"; static void key_mgr_wait_for_state(esp_key_mgr_state_t state) @@ -27,14 +31,17 @@ esp_err_t esp_key_mgr_deploy_key_in_aes_mode(esp_key_mgr_aes_key_config_t *key_c { ESP_LOGI(TAG, "Key Deployment"); // Reset the Key Manager Clock - periph_ll_enable_clk_clear_rst(PERIPH_KEY_MANAGER_MODULE); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(true); + key_mgr_ll_reset_register(); + } key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); - uint8_t *huk_recovery_info = (uint8_t *) calloc(1, sizeof(KEY_MGR_HUK_INFO_SIZE)); + 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; } - uint8_t *key_recovery_info = (uint8_t *) calloc(1, sizeof(KEY_MGR_KEY_RECOVERY_INFO_SIZE)); + 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; } @@ -100,15 +107,23 @@ esp_err_t esp_key_mgr_deploy_key_in_aes_mode(esp_key_mgr_aes_key_config_t *key_c //memcpy(&key_info->huk_recovery_info[0], huk_recovery_info, KEY_MGR_HUK_INFO_SIZE); //memcpy(&key_info->key_recovery_info[0], key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); key_info->key_purpose = key_config->key_purpose; - free(key_recovery_info); - free(huk_recovery_info); + heap_caps_free(key_recovery_info); + heap_caps_free(huk_recovery_info); key_mgr_hal_set_key_usage(ESP_KEY_MGR_XTS_KEY, ESP_KEY_MGR_USE_OWN_KEY); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(false); + key_mgr_ll_reset_register(); + } return ESP_OK; } esp_err_t esp_key_mgr_recover_key(esp_key_mgr_key_recovery_info_t *key_recovery_info) { - periph_ll_enable_clk_clear_rst(PERIPH_KEY_MANAGER_MODULE); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(true); + key_mgr_ll_reset_register(); + } + key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); huk_hal_configure(ESP_HUK_MODE_RECOVERY, key_recovery_info->huk_recovery_info); if (key_mgr_hal_is_huk_valid()) { @@ -132,6 +147,10 @@ esp_err_t esp_key_mgr_recover_key(esp_key_mgr_key_recovery_info_t *key_recovery_ ESP_LOGI(TAG, "Key deployment valid"); key_mgr_hal_continue(); key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(false); + key_mgr_ll_reset_register(); + } return ESP_OK; } @@ -139,14 +158,17 @@ esp_err_t esp_key_mgr_deploy_key_in_ecdh0_mode(esp_key_mgr_ecdh0_key_config_t *k { ESP_LOGI(TAG, "Key Deployment"); // Reset the Key Manager Clock - periph_ll_enable_clk_clear_rst(PERIPH_KEY_MANAGER_MODULE); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(true); + key_mgr_ll_reset_register(); + } key_mgr_wait_for_state(ESP_KEY_MGR_STATE_IDLE); - uint8_t *huk_recovery_info = (uint8_t *) calloc(1, sizeof(KEY_MGR_HUK_INFO_SIZE)); + 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; } - uint8_t *key_recovery_info = (uint8_t *) calloc(1, sizeof(KEY_MGR_KEY_RECOVERY_INFO_SIZE)); + 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; } @@ -212,8 +234,13 @@ esp_err_t esp_key_mgr_deploy_key_in_ecdh0_mode(esp_key_mgr_ecdh0_key_config_t *k //memcpy(&key_info->huk_recovery_info[0], huk_recovery_info, KEY_MGR_HUK_INFO_SIZE); //memcpy(&key_info->key_recovery_info[0], key_recovery_info, KEY_MGR_KEY_RECOVERY_INFO_SIZE); key_info->key_purpose = key_config->key_purpose; - free(key_recovery_info); - free(huk_recovery_info); - key_mgr_hal_set_key_usage(ESP_KEY_MGR_XTS_KEY, ESP_KEY_MGR_USE_OWN_KEY); + heap_caps_free(key_recovery_info); + heap_caps_free(huk_recovery_info); + ESP_LOGI(TAG, "\nKey deployment complete\n"); + //key_mgr_hal_set_key_usage(ESP_KEY_MGR_XTS_KEY, ESP_KEY_MGR_USE_OWN_KEY); + KEY_MANAGER_RCC_ATOMIC() { + key_mgr_ll_enable_bus_clock(false); + key_mgr_ll_reset_register(); + } return ESP_OK; } diff --git a/components/esp_hw_support/include/esp_key_mgr.h b/components/esp_hw_support/include/esp_key_mgr.h index 04ac53283f..61fea463aa 100644 --- a/components/esp_hw_support/include/esp_key_mgr.h +++ b/components/esp_hw_support/include/esp_key_mgr.h @@ -17,7 +17,7 @@ extern "C" { #define KEY_MGR_SW_INIT_KEY_SIZE 32 #define KEY_MGR_ASSIST_INFO_SIZE 64 #define KEY_MGR_KEY_RECOVERY_INFO_SIZE 64 -#define KEY_MGR_HUK_INFO_SIZE 64 +#define KEY_MGR_HUK_INFO_SIZE HUK_INFO_SIZE /* AES deploy mode */ #define KEY_MGR_K2_INFO_SIZE 64 #define KEY_MGR_K1_ENCRYPTED_SIZE 32 @@ -76,7 +76,7 @@ esp_err_t esp_key_mgr_deploy_key_in_aes_mode(esp_key_mgr_aes_key_config_t *key_c * ESP_OK for success * ESP_FAIL/relevant error code for failure */ -esp_err_t esp_key_mgr_deploy_key_in_aes_mode(esp_key_mgr_aes_key_config_t *key_config, esp_key_mgr_key_recovery_info_t *key_info); +esp_err_t esp_key_mgr_deploy_key_in_ecdh0_mode(esp_key_mgr_ecdh0_key_config_t *key_config, esp_key_mgr_key_recovery_info_t *key_info); /* * @brief Recover a key from the given key info diff --git a/components/hal/esp32p4/include/hal/huk_ll.h b/components/hal/esp32p4/include/hal/huk_ll.h index ce87ac8984..9c4d987749 100644 --- a/components/hal/esp32p4/include/hal/huk_ll.h +++ b/components/hal/esp32p4/include/hal/huk_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,8 +12,8 @@ #pragma once #include "soc/soc_caps.h" - #if SOC_KEY_MANAGER_SUPPORTED + #include #include #include @@ -32,12 +32,12 @@ static inline void huk_ll_configure_mode(const esp_huk_mode_t huk_mode) REG_SET_FIELD(HUK_CONF_REG, HUK_MODE, huk_mode); } -void huk_ll_write_info(const uint8_t *buffer, const size_t size) +static inline void huk_ll_write_info(const uint8_t *buffer, const size_t size) { memcpy((uint8_t *)HUK_INFO_MEM, buffer, size); } -void huk_ll_read_info(uint8_t *buffer, const size_t size) +static inline void huk_ll_read_info(uint8_t *buffer, const size_t size) { memcpy(buffer, (uint8_t *)HUK_INFO_MEM, size); } @@ -91,15 +91,15 @@ static inline void huk_ll_clear_int(const esp_huk_interrupt_type_t intr) */ static inline esp_huk_state_t huk_ll_get_state(void) { - return REG_GET_FIELD(HUK_STATE_REG, HUK_STATE); + return (esp_huk_state_t) REG_GET_FIELD(HUK_STATE_REG, HUK_STATE); } /** - * @brief Get the HUK generation status: esp_huk_gen_status_t + * @brief Get the HUK generation status */ static inline esp_huk_gen_status_t huk_ll_get_gen_status(void) { - return REG_GET_FIELD(HUK_STATUS_REG, HUK_STATUS); + return (esp_huk_gen_status_t) REG_GET_FIELD(HUK_STATUS_REG, HUK_STATUS); } /** diff --git a/components/hal/esp32p4/include/hal/key_mgr_ll.h b/components/hal/esp32p4/include/hal/key_mgr_ll.h index 17ae6263d1..107b1d338c 100644 --- a/components/hal/esp32p4/include/hal/key_mgr_ll.h +++ b/components/hal/esp32p4/include/hal/key_mgr_ll.h @@ -10,9 +10,7 @@ ******************************************************************************/ #pragma once - #include "soc/soc_caps.h" - #if SOC_KEY_MANAGER_SUPPORTED #include #include diff --git a/components/hal/huk_hal.c b/components/hal/huk_hal.c index 41b4d06238..70aa576459 100644 --- a/components/hal/huk_hal.c +++ b/components/hal/huk_hal.c @@ -12,6 +12,7 @@ #include "hal/assert.h" #include "hal/log.h" #include "rom/km.h" +#include "esp_err.h" esp_huk_state_t huk_hal_get_state(void) {