mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
feat(bootloader_support): Support Flash Encryption using Key Manager
This commit is contained in:
@@ -839,6 +839,26 @@ menu "Security features"
|
||||
Read https://docs.espressif.com/projects/esp-idf/en/latest/security/flash-encryption.html
|
||||
before enabling.
|
||||
|
||||
choice SECURE_FLASH_ENCRYPTION_KEY_SOURCE
|
||||
bool "Flash Encryption Key Source"
|
||||
depends on SECURE_FLASH_ENC_ENABLED
|
||||
default SECURE_FLASH_ENCRYPTION_KEY_SOURCE_EFUSES
|
||||
help
|
||||
Specify the key source for the Flash Encryption Key
|
||||
|
||||
config SECURE_FLASH_ENCRYPTION_KEY_SOURCE_EFUSES
|
||||
bool "eFuse Key Block"
|
||||
help
|
||||
Use a key that is stored in the eFuses key blocks.
|
||||
|
||||
config SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
bool "Key Manager"
|
||||
depends on SOC_KEY_MANAGER_SUPPORTED && SOC_KEY_MANAGER_FE_KEY_DEPLOY && \
|
||||
!(IDF_TARGET_ESP32P4 && ESP32P4_SELECTS_REV_LESS_V3)
|
||||
help
|
||||
Use a key that is deployed using the Key Manager
|
||||
endchoice
|
||||
|
||||
choice SECURE_FLASH_ENCRYPTION_KEYSIZE
|
||||
bool "Size of generated XTS-AES key"
|
||||
default SECURE_FLASH_ENCRYPTION_AES128
|
||||
|
||||
@@ -93,6 +93,8 @@ endif()
|
||||
|
||||
if(BOOTLOADER_BUILD)
|
||||
list(APPEND srcs "src/bootloader_panic.c")
|
||||
list(APPEND priv_requires esp_security)
|
||||
|
||||
if(CONFIG_SECURE_FLASH_ENC_ENABLED)
|
||||
list(APPEND srcs "src/flash_encryption/flash_encrypt.c"
|
||||
"src/${IDF_TARGET}/flash_encryption_secure_features.c")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "hal/efuse_ll.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -184,14 +183,14 @@ void esp_flash_encryption_init_checks(void);
|
||||
*/
|
||||
esp_err_t esp_flash_encryption_enable_secure_features(void);
|
||||
|
||||
#if CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY
|
||||
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY
|
||||
/** @brief Enable the key manager for flash encryption
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK - On success
|
||||
*/
|
||||
esp_err_t esp_flash_encryption_enable_key_mgr(void);
|
||||
#endif // CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY
|
||||
#endif // SOC_KEY_MANAGER_FE_KEY_DEPLOY
|
||||
|
||||
#endif /* BOOTLOADER_BUILD && CONFIG_SECURE_FLASH_ENC_ENABLED */
|
||||
|
||||
|
||||
@@ -11,10 +11,12 @@
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "esp_log.h"
|
||||
#include "hal/key_mgr_ll.h"
|
||||
#include "esp_key_mgr.h"
|
||||
#include "hal/key_mgr_hal.h"
|
||||
#include "hal/mspi_ll.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_crypto_periph_clk.h"
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "flash_encrypt");
|
||||
|
||||
@@ -71,15 +73,18 @@ 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) {
|
||||
};
|
||||
#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_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
|
||||
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.
|
||||
// Enabling Key Manager and forcing it to use its OWN key is handled in the
|
||||
// Key Manager's key deployment API itself.
|
||||
_mspi_timing_ll_reset_mspi();
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
#include "hal/spi_flash_encrypt_hal.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
#include "esp_key_mgr.h"
|
||||
#endif /* SOC_KEY_MANAGER_SUPPORTED */
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT
|
||||
#define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT
|
||||
@@ -444,6 +448,7 @@ bool esp_flash_encryption_cfg_verify_release_mode(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_EFUSES
|
||||
esp_efuse_purpose_t purposes[] = {
|
||||
#if SOC_FLASH_ENCRYPTION_XTS_AES_256
|
||||
ESP_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_1,
|
||||
@@ -482,6 +487,23 @@ bool esp_flash_encryption_cfg_verify_release_mode(void)
|
||||
}
|
||||
}
|
||||
result &= secure;
|
||||
#elif CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_AES128
|
||||
secure = esp_efuse_read_field_bit(ESP_EFUSE_KM_XTS_KEY_LENGTH_256);
|
||||
result &= secure;
|
||||
if (!secure) {
|
||||
ESP_LOGW(TAG, "Not enabled Key Manager XTS-AES-128 key (set KM_XTS_KEY_LENGTH_256->1)");
|
||||
}
|
||||
#endif
|
||||
|
||||
const uint32_t force_key_mgr_key = esp_efuse_read_field_bit(ESP_EFUSE_FORCE_USE_KEY_MANAGER_KEY);
|
||||
secure = (force_key_mgr_key & (1 << ESP_KEY_MGR_FORCE_USE_KM_XTS_AES_KEY));
|
||||
result &= secure;
|
||||
|
||||
if (!secure) {
|
||||
ESP_LOGW(TAG, "Not forcing Key Manager to use XTS-AES key (set FORCE_USE_KEY_MANAGER_KEY->1)");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND
|
||||
if (spi_flash_encrypt_ll_is_pseudo_rounds_function_supported()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -16,6 +16,14 @@
|
||||
#include "esp_log.h"
|
||||
#include "hal/wdt_hal.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
#include "esp_key_mgr.h"
|
||||
#include "hal/key_mgr_ll.h"
|
||||
#include "rom/key_mgr.h"
|
||||
#include "esp_rom_crc.h"
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
|
||||
#include "soc/sensitive_reg.h"
|
||||
@@ -124,8 +132,151 @@ esp_err_t esp_flash_encrypt_check_and_update(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
static esp_err_t key_manager_read_key_recovery_info(esp_key_mgr_key_recovery_info_t *key_recovery_info)
|
||||
{
|
||||
esp_err_t err = ESP_FAIL;
|
||||
uint32_t crc = 0;
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
err = bootloader_flash_read(KEY_HUK_SECTOR_OFFSET(i), (uint32_t *)key_recovery_info, sizeof(esp_key_mgr_key_recovery_info_t), false);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGD(TAG, "Failed to read key recovery info from Key Manager sector %d: %x", i, err);
|
||||
continue;
|
||||
}
|
||||
|
||||
// check Key Recovery Info magic
|
||||
if (key_recovery_info->magic != KEY_HUK_SECTOR_MAGIC) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d Magic %08x failed", i, key_recovery_info->magic);
|
||||
continue;
|
||||
}
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_AES256
|
||||
if (key_recovery_info->key_type != ESP_KEY_MGR_XTS_AES_256_KEY) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d has incorrect key type", i);
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
if (key_recovery_info->key_type != ESP_KEY_MGR_XTS_AES_128_KEY) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d has incorrect key type", i);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
// check HUK Info CRC
|
||||
crc = esp_rom_crc32_le(0, key_recovery_info->huk_info.info, HUK_INFO_LEN);
|
||||
if (crc != key_recovery_info->huk_info.crc) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d HUK Info CRC error", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
// check Key Info 0 CRC
|
||||
crc = esp_rom_crc32_le(0, key_recovery_info->key_info[0].info, KEY_INFO_LEN);
|
||||
if (crc != key_recovery_info->key_info[0].crc) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d Key Info 0 CRC error", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_AES256
|
||||
// check Key Info 1 CRC
|
||||
crc = esp_rom_crc32_le(0, key_recovery_info->key_info[1].info, KEY_INFO_LEN);
|
||||
if (crc != key_recovery_info->key_info[1].crc) {
|
||||
ESP_LOGD(TAG, "Key Manager sector %d Key Info 1 CRC error", i);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
ESP_LOGI(TAG, "Valid Key Manager key recovery info found in sector %d", i);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "No valid key recovery info found");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
static esp_err_t key_manager_generate_key(esp_key_mgr_key_recovery_info_t *key_recovery_info)
|
||||
{
|
||||
ESP_LOGI(TAG, "Deploying new flash encryption key using Key Manager");
|
||||
|
||||
esp_key_mgr_random_key_config_t key_config;
|
||||
memset(&key_config, 0, sizeof(esp_key_mgr_random_key_config_t));
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_AES256
|
||||
key_config.key_type = ESP_KEY_MGR_XTS_AES_256_KEY;
|
||||
#else
|
||||
key_config.key_type = ESP_KEY_MGR_XTS_AES_128_KEY;
|
||||
#endif
|
||||
|
||||
// Generate a new key and load it into Key Manager
|
||||
esp_err_t err = esp_key_mgr_deploy_key_in_random_mode(&key_config, key_recovery_info);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to generate key for Key Manager: %x", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "Successfully deployed new flash encryption key using Key Manager");
|
||||
|
||||
// Write the key recovery info of the newly generated key into the flash
|
||||
for (int i = 0; i < 2; i++) {
|
||||
err = bootloader_flash_erase_sector(i);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to erase sector %d: %x", i, err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the key recovery info of the newly generated key into the flash
|
||||
err = bootloader_flash_write(KEY_HUK_SECTOR_OFFSET(0), (uint32_t *)key_recovery_info, sizeof(esp_key_mgr_key_recovery_info_t), false);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to write key recovery info to flash: %x", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "Successfully wrote the newly generated Flash Encryption key recovery info into the flash");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t key_manager_check_and_generate_key(void)
|
||||
{
|
||||
/*
|
||||
1. Check if we have a valid key info in the first two sectors of the flash
|
||||
2. If we have a valid key info, check if it is valid
|
||||
1. If the key is valid, use it
|
||||
2. If the key is not valid, generate a new key and load it into key manager
|
||||
3. If not, generate a new key and load it into key manager
|
||||
*/
|
||||
esp_key_mgr_key_recovery_info_t key_recovery_info;
|
||||
|
||||
memset(&key_recovery_info, 0, sizeof(esp_key_mgr_key_recovery_info_t));
|
||||
|
||||
esp_err_t err = key_manager_read_key_recovery_info(&key_recovery_info);
|
||||
if (err == ESP_ERR_NOT_FOUND) {
|
||||
// No valid key recovery info found, generate a new key
|
||||
err = key_manager_generate_key(&key_recovery_info);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to generate key for Key Manager: %x", err);
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
// Valid key recovery info found, use it
|
||||
ESP_LOGI(TAG, "Using pre-deployed Key Manager key for flash encryption");
|
||||
}
|
||||
|
||||
// Recover key using the key recovery info
|
||||
err = esp_key_mgr_activate_key(&key_recovery_info);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to activate Key Manager key: %x", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
static esp_err_t check_and_generate_encryption_keys(void)
|
||||
{
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_EFUSES
|
||||
size_t key_size = 32;
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
enum { BLOCKS_NEEDED = 1 };
|
||||
@@ -213,13 +364,56 @@ static esp_err_t check_and_generate_encryption_keys(void)
|
||||
}
|
||||
ESP_LOGI(TAG, "Using pre-loaded flash encryption key in efuse");
|
||||
}
|
||||
#elif CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
esp_err_t err = key_manager_check_and_generate_key();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to check and generate key using Key Manager: %x", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_AES128
|
||||
err = esp_efuse_write_field_bit(ESP_EFUSE_KM_XTS_KEY_LENGTH_256);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set the efuse bit KM_XTS_KEY_LENGTH_256: %x", err);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
const uint32_t force_key_mgr_key_for_fe = 1 << ESP_KEY_MGR_FORCE_USE_KM_XTS_AES_KEY;
|
||||
err = esp_efuse_write_field_blob(ESP_EFUSE_FORCE_USE_KEY_MANAGER_KEY, &force_key_mgr_key_for_fe, ESP_EFUSE_FORCE_USE_KEY_MANAGER_KEY[0]->bit_count);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set the efuse bit %d (XTS-AES key) of FORCE_USE_KEY_MANAGER_KEY: %x", ESP_KEY_MGR_FORCE_USE_KM_XTS_AES_KEY, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "Successfully activated the flash encryption key using Key Manager");
|
||||
#endif
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_flash_encrypt_init(void)
|
||||
{
|
||||
if (esp_flash_encryption_enabled() || esp_flash_encrypt_initialized_once()) {
|
||||
if (esp_flash_encryption_enabled()) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
if (!(key_mgr_ll_is_supported() && key_mgr_ll_flash_encryption_supported())) {
|
||||
ESP_LOGE(TAG, "Flash Encryption using Key Manager is not supported, please use efuses instead");
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (esp_flash_encrypt_initialized_once()) {
|
||||
#if CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR
|
||||
// Allow generating a new key if the key recovery info is not present in the flash
|
||||
esp_err_t err = key_manager_check_and_generate_key();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to recover key using Key Manager: %x", err);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -260,10 +454,6 @@ esp_err_t esp_flash_encrypt_contents(void)
|
||||
REG_WRITE(SENSITIVE_XTS_AES_KEY_UPDATE_REG, 1);
|
||||
#endif
|
||||
|
||||
#if CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY
|
||||
esp_flash_encryption_enable_key_mgr();
|
||||
#endif
|
||||
|
||||
err = encrypt_bootloader(); // PART_SUBTYPE_BOOTLOADER_PRIMARY
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
|
||||
@@ -15,7 +15,7 @@ extern "C" {
|
||||
|
||||
// NOTE: [ESP-TEE] Since the clock configuration APIs are part
|
||||
// of the TEE, the XYZ_RCC_ATOMIC macros need to be defined as void.
|
||||
#if SOC_RCC_IS_INDEPENDENT || ESP_TEE_BUILD
|
||||
#if SOC_RCC_IS_INDEPENDENT || NON_OS_BUILD
|
||||
#define MPI_RCC_ATOMIC()
|
||||
#define ECC_RCC_ATOMIC()
|
||||
#define HMAC_RCC_ATOMIC()
|
||||
|
||||
@@ -6,7 +6,7 @@ if(${target} STREQUAL "linux")
|
||||
endif()
|
||||
|
||||
set(srcs "")
|
||||
set(priv_requires "")
|
||||
set(priv_requires "esp_hw_support")
|
||||
set(priv_includes "")
|
||||
|
||||
if(NOT non_os_build)
|
||||
@@ -42,6 +42,12 @@ elseif(esp_tee_build)
|
||||
if(CONFIG_SOC_DIG_SIGN_SUPPORTED)
|
||||
list(APPEND srcs "src/esp_ds.c")
|
||||
endif()
|
||||
else() # BOOTLOADER_BUILD
|
||||
list(APPEND srcs "src/esp_crypto_lock.c" "src/esp_crypto_periph_clk.c")
|
||||
|
||||
if(CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY)
|
||||
list(APPEND srcs "src/esp_key_mgr.c")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
idf_component_register(SRCS ${srcs}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -25,6 +25,8 @@ extern "C" {
|
||||
#define KEY_MGR_HUK_INFO_SIZE HUK_INFO_LEN
|
||||
#define KEY_MGR_HUK_RISK_ALERT_LEVEL HUK_RISK_ALERT_LEVEL
|
||||
|
||||
#define KEY_MGR_KEY_INFO_SIZE KEY_INFO_LEN
|
||||
|
||||
/* AES deploy mode */
|
||||
#define KEY_MGR_K2_INFO_SIZE 64
|
||||
#define KEY_MGR_K1_ENCRYPTED_SIZE 32
|
||||
@@ -59,6 +61,13 @@ typedef struct {
|
||||
WORD_ALIGNED_ATTR uint8_t k2_G[2][KEY_MGR_ECDH0_INFO_SIZE];
|
||||
} esp_key_mgr_ecdh0_info_t;
|
||||
|
||||
/**
|
||||
* @brief Wait for the Key Manager to reach the given state
|
||||
*
|
||||
* @param state The state to wait for
|
||||
*/
|
||||
void key_mgr_wait_for_state(esp_key_mgr_state_t state);
|
||||
|
||||
/**
|
||||
* @brief Deploy key in AES deployment mode
|
||||
* @input
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "soc/soc_caps.h"
|
||||
#include "esp_private/esp_crypto_lock_internal.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_crypto_periph_clk.h"
|
||||
|
||||
#if SOC_AES_SUPPORTED
|
||||
#include "hal/aes_ll.h"
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
// The Hardware Support layer for Key manager
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <sys/lock.h>
|
||||
#include "esp_key_mgr.h"
|
||||
#include "esp_crypto_periph_clk.h"
|
||||
#include "esp_crypto_lock.h"
|
||||
@@ -24,16 +23,19 @@
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
static const char *TAG = "esp_key_mgr";
|
||||
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_key_recovery_info_t) == sizeof(struct huk_key_block), "Size of esp_key_mgr_key_recovery_info_t should match huk_key_block (from ROM)");
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_key_info_t) == sizeof(struct key_info), "Size of esp_key_mgr_key_info_t should match key_info (from ROM)");
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_huk_info_t) == sizeof(struct huk_info), "Size of esp_key_mgr_huk_info_t should match huk_info (from ROM)");
|
||||
|
||||
#if !NON_OS_BUILD
|
||||
#include <sys/lock.h>
|
||||
|
||||
static _lock_t s_key_mgr_ecdsa_key_lock;
|
||||
static _lock_t s_key_mgr_xts_aes_key_lock;
|
||||
static _lock_t s_key_mgr_hmac_key_lock;
|
||||
static _lock_t s_key_mgr_ds_key_lock;
|
||||
static _lock_t s_key_mgr_psram_key_lock;
|
||||
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_key_recovery_info_t) == sizeof(struct huk_key_block), "Size of esp_key_mgr_key_recovery_info_t should match huk_key_block (from ROM)");
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_key_info_t) == sizeof(struct key_info), "Size of esp_key_mgr_key_info_t should match key_info (from ROM)");
|
||||
ESP_STATIC_ASSERT(sizeof(esp_key_mgr_huk_info_t) == sizeof(struct huk_info), "Size of esp_key_mgr_huk_info_t should match huk_info (from ROM)");
|
||||
|
||||
static void esp_key_mgr_acquire_key_lock(esp_key_mgr_key_type_t key_type)
|
||||
{
|
||||
switch (key_type) {
|
||||
@@ -91,6 +93,46 @@ static void esp_key_mgr_release_key_lock(esp_key_mgr_key_type_t key_type)
|
||||
}
|
||||
ESP_LOGV(TAG, "Key lock released for key type %d", key_type);
|
||||
}
|
||||
#else /* !NON_OS_BUILD */
|
||||
static void esp_key_mgr_acquire_key_lock(esp_key_mgr_key_type_t key_type)
|
||||
{
|
||||
switch (key_type) {
|
||||
case ESP_KEY_MGR_ECDSA_192_KEY:
|
||||
case ESP_KEY_MGR_ECDSA_256_KEY:
|
||||
case ESP_KEY_MGR_ECDSA_384_KEY:
|
||||
case ESP_KEY_MGR_XTS_AES_128_KEY:
|
||||
case ESP_KEY_MGR_XTS_AES_256_KEY:
|
||||
case ESP_KEY_MGR_HMAC_KEY:
|
||||
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;
|
||||
}
|
||||
ESP_LOGV(TAG, "Key lock acquired for key type %d", key_type);
|
||||
}
|
||||
|
||||
static void esp_key_mgr_release_key_lock(esp_key_mgr_key_type_t key_type)
|
||||
{
|
||||
switch (key_type) {
|
||||
case ESP_KEY_MGR_ECDSA_192_KEY:
|
||||
case ESP_KEY_MGR_ECDSA_256_KEY:
|
||||
case ESP_KEY_MGR_ECDSA_384_KEY:
|
||||
case ESP_KEY_MGR_XTS_AES_128_KEY:
|
||||
case ESP_KEY_MGR_XTS_AES_256_KEY:
|
||||
case ESP_KEY_MGR_HMAC_KEY:
|
||||
case ESP_KEY_MGR_DS_KEY:
|
||||
case ESP_KEY_MGR_PSRAM_128_KEY:
|
||||
case ESP_KEY_MGR_PSRAM_256_KEY:
|
||||
default:
|
||||
ESP_LOGE(TAG, "Invalid key type");
|
||||
break;
|
||||
}
|
||||
ESP_LOGV(TAG, "Key lock released for key type %d", key_type);
|
||||
}
|
||||
#endif /* NON_OS_BUILD */
|
||||
|
||||
static void esp_key_mgr_acquire_hardware(bool deployment_mode)
|
||||
{
|
||||
@@ -155,7 +197,7 @@ static void check_huk_risk_level(void)
|
||||
"It is recommended to immediately regenerate HUK in order"
|
||||
"to avoid permanently losing the deployed keys", huk_risk_level);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "HUK Risk level - %" PRId8 " within acceptable limit (%" PRIu32 ")", huk_risk_level, (uint32_t)KEY_MGR_HUK_RISK_ALERT_LEVEL);
|
||||
ESP_LOGD(TAG, "HUK Risk level - %d within acceptable limit (%d)", huk_risk_level, (int) KEY_MGR_HUK_RISK_ALERT_LEVEL);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -186,6 +228,8 @@ 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);
|
||||
@@ -198,9 +242,11 @@ 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()) {
|
||||
@@ -213,6 +259,10 @@ static esp_err_t configure_huk(esp_huk_mode_t huk_mode, uint8_t *huk_info)
|
||||
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;
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "soc/keymng_reg.h"
|
||||
#include "soc/pcr_struct.h"
|
||||
#include "soc/pcr_reg.h"
|
||||
#include "hal/efuse_hal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -443,6 +444,14 @@ static inline bool key_mgr_ll_is_supported(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool key_mgr_ll_flash_encryption_supported(void)
|
||||
{
|
||||
if (!key_mgr_ll_is_supported() || efuse_hal_chip_revision() <= 100) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -484,6 +484,14 @@ static inline bool key_mgr_ll_is_supported(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline bool key_mgr_ll_flash_encryption_supported(void)
|
||||
{
|
||||
if (!key_mgr_ll_is_supported()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -110,6 +110,17 @@ typedef enum {
|
||||
ESP_KEY_MGR_INT_POST_DONE,
|
||||
} esp_key_mgr_interrupt_type_t;
|
||||
|
||||
/**
|
||||
* @brief Force use key manager key type
|
||||
* @note This is used to force the key manager to use a specific key type.
|
||||
*/
|
||||
typedef enum {
|
||||
ESP_KEY_MGR_FORCE_USE_KM_ECDSA_KEY = 0,
|
||||
ESP_KEY_MGR_FORCE_USE_KM_XTS_AES_KEY = 1,
|
||||
ESP_KEY_MGR_FORCE_USE_KM_HMAC_KEY = 2,
|
||||
ESP_KEY_MGR_FORCE_USE_KM_DS_KEY = 3,
|
||||
} esp_key_mgr_force_use_km_key_t;
|
||||
|
||||
// store huk info, occupy 96 words
|
||||
typedef struct PACKED_ATTR {
|
||||
#define HUK_INFO_LEN 660
|
||||
|
||||
@@ -1343,6 +1343,10 @@ config SOC_FLASH_ENCRYPTION_XTS_AES
|
||||
bool
|
||||
default y
|
||||
|
||||
config SOC_FLASH_ENCRYPTION_XTS_AES_OPTIONS
|
||||
bool
|
||||
default y
|
||||
|
||||
config SOC_FLASH_ENCRYPTION_XTS_AES_128
|
||||
bool
|
||||
default y
|
||||
|
||||
@@ -534,8 +534,9 @@
|
||||
|
||||
/*-------------------------- Flash Encryption CAPS----------------------------*/
|
||||
#define SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX (64)
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES 1
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES_128 1
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES 1
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES_OPTIONS 1
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES_128 1
|
||||
#define SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND 1
|
||||
|
||||
/*-------------------------- PSRAM Encryption CAPS----------------------------*/
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
tools/test_apps/system/bootloader_sections:
|
||||
disable:
|
||||
- if: CONFIG_NAME == "rtc_retain" and SOC_RTC_FAST_MEM_SUPPORTED != 1
|
||||
- if: CONFIG_NAME == "flash_encryption_key_mgr" and (SOC_KEY_MANAGER_FE_KEY_DEPLOY != 1 or IDF_TARGET == "esp32p4")
|
||||
- if: CONFIG_NAME == "flash_encryption_key_mgr_esp32p4" and IDF_TARGET != "esp32p4"
|
||||
|
||||
tools/test_apps/system/build_tests/client_only_mbedtls:
|
||||
disable:
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
CONFIG_SECURE_FLASH_ENC_ENABLED=y
|
||||
CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=y
|
||||
CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR=y
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0xC000
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
CONFIG_SECURE_FLASH_ENC_ENABLED=y
|
||||
CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=y
|
||||
CONFIG_SECURE_FLASH_ENCRYPTION_KEY_SOURCE_KEY_MGR=y
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0xC000
|
||||
|
||||
CONFIG_ESP32P4_SELECTS_REV_LESS_V3=n
|
||||
Reference in New Issue
Block a user