Skip to content

Commit 1c7e750

Browse files
authored
Merge pull request #67 from espressif/get_flash_encryption
feat(security): add api to check flash is encrypted
2 parents ee158e0 + 96e788b commit 1c7e750

File tree

18 files changed

+78
-6
lines changed

18 files changed

+78
-6
lines changed

example/stub_main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ static void example_mem_utils(void)
4646

4747
static void example_security(void)
4848
{
49+
bool encryption_enabled = stub_lib_security_flash_is_encrypted();
50+
STUB_LOGI("Flash encryption enabled: %d\n", encryption_enabled);
51+
4952
uint32_t size = stub_lib_security_info_size();
5053
STUB_LOGI("Security info size: %u bytes\n", size);
5154

include/esp-stub-lib/security.h

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

77
#pragma once
88

9+
#include <stdbool.h>
910
#include <stdint.h>
1011

1112
#ifdef __cplusplus
@@ -36,6 +37,13 @@ uint32_t stub_lib_security_info_size(void);
3637
*/
3738
int stub_lib_get_security_info(uint8_t *buffer, uint32_t buffer_size);
3839

40+
/**
41+
* @brief Check whether flash encryption is enabled.
42+
*
43+
* @return true if flash encryption is enabled, false otherwise.
44+
*/
45+
bool stub_lib_security_flash_is_encrypted(void);
46+
3947
#ifdef __cplusplus
4048
}
4149
#endif // __cplusplus

src/security.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0 OR MIT
55
*/
66

77
#include <stddef.h>
8+
#include <stdbool.h>
89
#include <esp-stub-lib/security.h>
910
#include <esp-stub-lib/err.h>
1011
#include <target/security.h>
@@ -18,3 +19,8 @@ int stub_lib_get_security_info(uint8_t *buffer, uint32_t buffer_size)
1819
{
1920
return stub_target_get_security_info(buffer, buffer_size);
2021
}
22+
23+
bool stub_lib_security_flash_is_encrypted(void)
24+
{
25+
return stub_target_security_flash_is_encrypted();
26+
}

src/target/base/include/target/security.h

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

77
#pragma once
88

9+
#include <stdbool.h>
910
#include <stdint.h>
1011

1112
/**
@@ -31,3 +32,10 @@ uint32_t stub_target_security_info_size(void);
3132
* - STUB_LIB_FAIL if the operation failed
3233
*/
3334
int stub_target_get_security_info(uint8_t *buffer, uint32_t buffer_size);
35+
36+
/**
37+
* @brief Check whether flash encryption is enabled.
38+
*
39+
* @return true if flash encryption is enabled, false otherwise.
40+
*/
41+
bool stub_target_security_flash_is_encrypted(void);

src/target/common/src/security.c

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

77
#include <stddef.h>
8+
#include <stdbool.h>
89
#include <stdint.h>
910
#include <target/security.h>
1011
#include <err.h>
1112

1213
/* GetSecurityInfoProc function from ROM */
1314
extern uint32_t GetSecurityInfoProc(int *pMsg, int *pnErr, uint8_t *buf);
15+
extern bool esp_rom_efuse_flash_encryption_enabled(void);
1416

1517
#define SECURITY_INFO_BYTES_DEFAULT 20
1618

@@ -37,3 +39,8 @@ int __attribute__((weak)) stub_target_get_security_info(uint8_t *buffer, uint32_
3739

3840
return STUB_LIB_FAIL;
3941
}
42+
43+
bool __attribute__((weak)) stub_target_security_flash_is_encrypted(void)
44+
{
45+
return esp_rom_efuse_flash_encryption_enabled();
46+
}

src/target/esp32/src/security.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0 OR MIT
55
*/
66

77
#include <stddef.h>
8+
#include <stdbool.h>
89
#include <stdint.h>
910
#include <target/security.h>
1011
#include <err.h>
12+
#include <esp-stub-lib/soc_utils.h>
13+
#include <soc/reg_base.h>
14+
15+
#define EFUSE_BLK0_RDATA0_REG (DR_REG_EFUSE_BASE + 0x0)
16+
#define EFUSE_RD_FLASH_CRYPT_CNT_V 0x0000007FU
17+
#define EFUSE_RD_FLASH_CRYPT_CNT_S 20
1118

1219
/* ESP32 does not support GetSecurityInfoProc */
1320
uint32_t stub_target_security_info_size(void)
@@ -21,3 +28,19 @@ int stub_target_get_security_info(uint8_t *buffer, uint32_t buffer_size)
2128
(void)buffer_size;
2229
return STUB_LIB_ERR_NOT_SUPPORTED;
2330
}
31+
32+
bool stub_target_security_flash_is_encrypted(void)
33+
{
34+
uint32_t flash_crypt_cnt = REG_READ(EFUSE_BLK0_RDATA0_REG);
35+
flash_crypt_cnt = (flash_crypt_cnt >> EFUSE_RD_FLASH_CRYPT_CNT_S) & EFUSE_RD_FLASH_CRYPT_CNT_V;
36+
37+
bool enabled = false;
38+
while (flash_crypt_cnt) {
39+
if (flash_crypt_cnt & 1U) {
40+
enabled = !enabled;
41+
}
42+
flash_crypt_cnt >>= 1;
43+
}
44+
45+
return enabled;
46+
}

src/target/esp32c2/ld/esp32c2.rom.api.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ PROVIDE ( esp_rom_gpio_connect_out_signal = gpio_matrix_out );
1616

1717
PROVIDE ( esp_rom_efuse_mac_address_crc8 = esp_crc8 );
1818
PROVIDE ( esp_rom_efuse_is_secure_boot_enabled = ets_efuse_secure_boot_enabled );
19+
PROVIDE ( esp_rom_efuse_flash_encryption_enabled = ets_efuse_cache_encryption_enabled );
1920

2021
PROVIDE ( esp_rom_uart_flush_tx = uart_tx_flush );
2122
PROVIDE ( esp_rom_uart_tx_one_char = uart_tx_one_char );

src/target/esp32c3/ld/esp32c3.rom.api.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PROVIDE ( esp_rom_gpio_connect_out_signal = gpio_matrix_out );
1818
PROVIDE ( esp_rom_efuse_mac_address_crc8 = esp_crc8 );
1919
PROVIDE ( esp_rom_efuse_get_flash_gpio_info = ets_efuse_get_spiconfig );
2020
PROVIDE ( esp_rom_efuse_is_secure_boot_enabled = ets_efuse_secure_boot_enabled );
21+
PROVIDE ( esp_rom_efuse_flash_encryption_enabled = ets_efuse_cache_encryption_enabled );
2122
PROVIDE ( esp_rom_efuse_get_flash_wp_gpio = ets_efuse_get_wp_pad );
2223

2324
PROVIDE ( esp_rom_uart_flush_tx = uart_tx_flush );

src/target/esp32c5/ld/esp32c5.rom.api.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PROVIDE ( esp_rom_gpio_connect_out_signal = gpio_matrix_out );
1717

1818
PROVIDE ( esp_rom_efuse_mac_address_crc8 = esp_crc8 );
1919
PROVIDE ( esp_rom_efuse_is_secure_boot_enabled = ets_efuse_secure_boot_enabled );
20+
PROVIDE ( esp_rom_efuse_flash_encryption_enabled = ets_efuse_cache_encryption_enabled );
2021

2122
PROVIDE ( esp_rom_uart_flush_tx = uart_tx_flush );
2223
PROVIDE ( esp_rom_uart_tx_one_char = uart_tx_one_char2 );

src/target/esp32c6/ld/esp32c6.rom.api.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PROVIDE ( esp_rom_gpio_connect_out_signal = gpio_matrix_out );
1717

1818
PROVIDE ( esp_rom_efuse_mac_address_crc8 = esp_crc8 );
1919
PROVIDE ( esp_rom_efuse_is_secure_boot_enabled = ets_efuse_secure_boot_enabled );
20+
PROVIDE ( esp_rom_efuse_flash_encryption_enabled = ets_efuse_cache_encryption_enabled );
2021

2122
PROVIDE ( esp_rom_uart_flush_tx = uart_tx_flush );
2223
PROVIDE ( esp_rom_uart_tx_one_char = uart_tx_one_char2 );

0 commit comments

Comments
 (0)