Skip to content

Commit 649f9a7

Browse files
committed
Merge branch 'feat/support_aes_pseudo_round_func_in_esp32h2_eco5_v5.4' into 'release/v5.4'
Support AES and XTS-AES's pseudo round function in ESP32H2-ECO5 (v5.4) See merge request espressif/esp-idf!36463
2 parents 52807c1 + 69f7880 commit 649f9a7

File tree

36 files changed

+701
-66
lines changed

36 files changed

+701
-66
lines changed

components/bootloader/Kconfig.projbuild

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,44 @@ menu "Security features"
11341134

11351135
If not set, the app does not care if the flash encryption eFuse bit is set or not.
11361136

1137+
config SECURE_FLASH_PSEUDO_ROUND_FUNC
1138+
bool "Permanently enable XTS-AES's pseudo rounds function"
1139+
default y
1140+
depends on SECURE_FLASH_ENCRYPTION_MODE_RELEASE && SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND
1141+
help
1142+
If set (default), the bootloader will permanently enable the XTS-AES peripheral's pseudo rounds function.
1143+
Note: Enabling this config would burn an efuse.
1144+
1145+
choice SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH
1146+
prompt "Strength of the pseudo rounds function"
1147+
depends on SECURE_FLASH_PSEUDO_ROUND_FUNC
1148+
default SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_LOW
1149+
help
1150+
The strength of the pseudo rounds functions can be configured to low, medium and high,
1151+
each denoting the values that would be stored in the efuses field.
1152+
By default the value to set to low.
1153+
You can configure the strength of the pseudo rounds functions according to your use cases,
1154+
for example, increasing the strength would provide higher security but would slow down the
1155+
flash encryption/decryption operations.
1156+
For more info regarding the performance impact, please checkout the pseudo round function section of the
1157+
security guide documentation.
1158+
1159+
config SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_LOW
1160+
bool "Low"
1161+
1162+
config SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_MEDIUM
1163+
bool "Medium"
1164+
1165+
config SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_HIGH
1166+
bool "High"
1167+
endchoice
1168+
1169+
config SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH
1170+
int
1171+
default 1 if SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_LOW
1172+
default 2 if SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_MEDIUM
1173+
default 3 if SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH_HIGH
1174+
11371175
config SECURE_ROM_DL_MODE_ENABLED
11381176
bool
11391177
default y if SOC_SUPPORTS_SECURE_DL_MODE && !SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT

components/bootloader_support/src/esp32h2/flash_encryption_secure_features.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <stdint.h>
78
#include <strings.h>
89
#include "esp_flash_encrypt.h"
910
#include "esp_secure_boot.h"
1011
#include "esp_efuse.h"
1112
#include "esp_efuse_table.h"
1213
#include "esp_log.h"
14+
#include "hal/spi_flash_encrypted_ll.h"
15+
#include "soc/soc_caps.h"
1316
#include "sdkconfig.h"
1417

1518
static __attribute__((unused)) const char *TAG = "flash_encrypt";
@@ -33,6 +36,14 @@ esp_err_t esp_flash_encryption_enable_secure_features(void)
3336

3437
esp_efuse_write_field_bit(ESP_EFUSE_DIS_DIRECT_BOOT);
3538

39+
#if defined(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE) && defined(SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND)
40+
if (spi_flash_encrypt_ll_is_pseudo_rounds_function_supported()) {
41+
ESP_LOGI(TAG, "Enable XTS-AES pseudo rounds function...");
42+
uint8_t xts_pseudo_level = CONFIG_SECURE_FLASH_PSEUDO_ROUND_FUNC_STRENGTH;
43+
esp_efuse_write_field_blob(ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL, &xts_pseudo_level, ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL[0]->bit_count);
44+
}
45+
#endif
46+
3647
#if defined(CONFIG_SECURE_BOOT_V2_ENABLED) && !defined(CONFIG_SECURE_BOOT_V2_ALLOW_EFUSE_RD_DIS)
3748
// This bit is set when enabling Secure Boot V2, but we can't enable it until this later point in the first boot
3849
// otherwise the Flash Encryption key cannot be read protected

components/bootloader_support/src/flash_encrypt.c

Lines changed: 22 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
*/
@@ -12,6 +12,9 @@
1212
#include "esp_flash_encrypt.h"
1313
#include "esp_secure_boot.h"
1414
#include "hal/efuse_hal.h"
15+
#include "hal/spi_flash_encrypted_ll.h"
16+
#include "hal/spi_flash_encrypt_hal.h"
17+
#include "soc/soc_caps.h"
1518

1619
#if CONFIG_IDF_TARGET_ESP32
1720
#define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT
@@ -207,6 +210,13 @@ void esp_flash_encryption_set_release_mode(void)
207210
#endif // CONFIG_SOC_FLASH_ENCRYPTION_XTS_AES_128_DERIVED
208211
#endif // !CONFIG_IDF_TARGET_ESP32
209212

213+
#ifdef SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND
214+
if (spi_flash_encrypt_ll_is_pseudo_rounds_function_supported()) {
215+
uint8_t xts_pseudo_level = ESP_XTS_AES_PSEUDO_ROUNDS_LOW;
216+
esp_efuse_write_field_blob(ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL, &xts_pseudo_level, ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL[0]->bit_count);
217+
}
218+
#endif
219+
210220
#ifdef CONFIG_IDF_TARGET_ESP32
211221
esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_DIS_CACHE);
212222
#else
@@ -468,6 +478,17 @@ bool esp_flash_encryption_cfg_verify_release_mode(void)
468478
}
469479
result &= secure;
470480

481+
#if SOC_FLASH_ENCRYPTION_XTS_AES_SUPPORT_PSEUDO_ROUND
482+
if (spi_flash_encrypt_ll_is_pseudo_rounds_function_supported()) {
483+
uint8_t xts_pseudo_level = 0;
484+
esp_efuse_read_field_blob(ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL, &xts_pseudo_level, ESP_EFUSE_XTS_DPA_PSEUDO_LEVEL[0]->bit_count);
485+
if (!xts_pseudo_level) {
486+
result &= false;
487+
ESP_LOGW(TAG, "Not enabled XTS-AES pseudo rounds function (set XTS_DPA_PSEUDO_LEVEL->1 or more)");
488+
}
489+
}
490+
#endif
491+
471492
return result;
472493
}
473494
#endif // not CONFIG_IDF_TARGET_ESP32

components/hal/aes_hal.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -39,8 +39,17 @@ void aes_hal_transform_block(const void *input_block, void *output_block)
3939
aes_ll_read_block(output_block);
4040
}
4141

42-
#if SOC_AES_SUPPORT_DMA
4342

43+
#ifdef SOC_AES_SUPPORT_PSEUDO_ROUND_FUNCTION
44+
void aes_hal_enable_pseudo_rounds(bool enable, uint8_t base, uint8_t increment, uint8_t key_rng_cnt)
45+
{
46+
if (aes_ll_is_pseudo_rounds_function_supported()) {
47+
aes_ll_enable_pseudo_rounds(enable, base, increment, key_rng_cnt);
48+
}
49+
}
50+
#endif // SOC_AES_SUPPORT_PSEUDO_ROUND_FUNCTION
51+
52+
#if SOC_AES_SUPPORT_DMA
4453

4554
void aes_hal_transform_dma_start(size_t num_blocks)
4655
{
@@ -61,7 +70,7 @@ void aes_hal_transform_dma_finish(void)
6170

6271
void aes_hal_mode_init(esp_aes_mode_t mode)
6372
{
64-
/* Set the algorith mode CBC, CFB ... */
73+
/* Set the algorithm mode CBC, CFB ... */
6574
aes_ll_set_block_mode(mode);
6675
/* Presently hard-coding the INC function to 32 bit */
6776
if (mode == ESP_AES_BLOCK_MODE_CTR) {
@@ -83,8 +92,6 @@ void aes_hal_wait_done()
8392
{
8493
while (aes_ll_get_state() != ESP_AES_STATE_DONE) {}
8594
}
86-
87-
8895
#endif //SOC_AES_SUPPORT_DMA
8996

9097
#if SOC_AES_SUPPORT_GCM

components/hal/esp32/include/hal/spi_flash_encrypted_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include "soc/dport_reg.h"
1617
#include "soc/flash_encryption_reg.h"

components/hal/esp32c2/include/hal/spi_flash_encrypted_ll.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include <stdbool.h>
1617
#include <string.h>
@@ -24,7 +25,7 @@
2425
extern "C" {
2526
#endif
2627

27-
/// Choose type of chip you want to encrypt manully
28+
/// Choose type of chip you want to encrypt manually
2829
typedef enum
2930
{
3031
FLASH_ENCRYPTION_MANU = 0, ///!< Manually encrypt the flash chip.
@@ -51,7 +52,7 @@ static inline void spi_flash_encrypt_ll_disable(void)
5152
}
5253

5354
/**
54-
* Choose type of chip you want to encrypt manully
55+
* Choose type of chip you want to encrypt manually
5556
*
5657
* @param type The type of chip to be encrypted
5758
*

components/hal/esp32c3/include/hal/spi_flash_encrypted_ll.h

Lines changed: 4 additions & 3 deletions
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
*/
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include <stdbool.h>
1617
#include <string.h>
@@ -24,7 +25,7 @@
2425
extern "C" {
2526
#endif
2627

27-
/// Choose type of chip you want to encrypt manully
28+
/// Choose type of chip you want to encrypt manually
2829
typedef enum
2930
{
3031
FLASH_ENCRYPTION_MANU = 0, ///!< Manually encrypt the flash chip.
@@ -51,7 +52,7 @@ static inline void spi_flash_encrypt_ll_disable(void)
5152
}
5253

5354
/**
54-
* Choose type of chip you want to encrypt manully
55+
* Choose type of chip you want to encrypt manually
5556
*
5657
* @param type The type of chip to be encrypted
5758
*

components/hal/esp32c5/include/hal/spi_flash_encrypted_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include <stdbool.h>
1617
#include <string.h>

components/hal/esp32c6/include/hal/spi_flash_encrypted_ll.h

Lines changed: 4 additions & 3 deletions
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-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include <stdbool.h>
1617
#include <string.h>
@@ -24,7 +25,7 @@
2425
extern "C" {
2526
#endif
2627

27-
/// Choose type of chip you want to encrypt manully
28+
/// Choose type of chip you want to encrypt manually
2829
typedef enum
2930
{
3031
FLASH_ENCRYPTION_MANU = 0, ///!< Manually encrypt the flash chip.
@@ -51,7 +52,7 @@ static inline void spi_flash_encrypt_ll_disable(void)
5152
}
5253

5354
/**
54-
* Choose type of chip you want to encrypt manully
55+
* Choose type of chip you want to encrypt manually
5556
*
5657
* @param type The type of chip to be encrypted
5758
*

components/hal/esp32c61/include/hal/spi_flash_encrypted_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
// The Lowlevel layer for SPI Flash Encryption.
14+
#pragma once
1415

1516
#include <stdbool.h>
1617
#include <string.h>

0 commit comments

Comments
 (0)