Skip to content

Commit a71e0fc

Browse files
committed
Merge branch 'feature/enable_sha_support_for_esp32c61' into 'master'
feat: enable support for sha peripheral in esp32c61 Closes IDF-9234 See merge request espressif/esp-idf!32830
2 parents f5b55b2 + 12fc7a6 commit a71e0fc

File tree

9 files changed

+253
-25
lines changed

9 files changed

+253
-25
lines changed

components/bootloader_support/src/esp32c61/bootloader_sha.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
static SHA_CTX ctx;
1515

16-
//TODO: [ESP32C61] IDF-9234
17-
1816
bootloader_sha256_handle_t bootloader_sha256_start()
1917
{
2018
// Enable SHA hardware

components/esp_hw_support/include/esp_crypto_lock.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void esp_crypto_ds_lock_acquire(void);
4444
void esp_crypto_ds_lock_release(void);
4545
#endif /* SOC_DIG_SIGN_SUPPORTED */
4646

47-
#if defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED)
47+
#if defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED)
4848
/**
4949
* @brief Acquire lock for the SHA and AES cryptography peripheral.
5050
*
@@ -56,9 +56,9 @@ void esp_crypto_sha_aes_lock_acquire(void);
5656
*
5757
*/
5858
void esp_crypto_sha_aes_lock_release(void);
59-
#endif /* defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED) */
59+
#endif /* defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED) */
6060

61-
#if defined(SOC_SHA_CRYPTO_DMA) && defined(SOC_AES_CRYPTO_DMA)
61+
#if defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA)
6262
/**
6363
* This API should be used by all components which use the SHA, AES, HMAC and DS crypto hardware on the ESP32S2.
6464
* They can not be used in parallel because they use the same DMA or are calling each other.
@@ -76,7 +76,7 @@ void esp_crypto_dma_lock_acquire(void);
7676
* Release lock for the AES and SHA cryptography peripherals, which both use the crypto DMA.
7777
*/
7878
void esp_crypto_dma_lock_release(void);
79-
#endif /* defined(SOC_SHA_CRYPTO_DMA) && defined(SOC_AES_CRYPTO_DMA) */
79+
#endif /* defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA) */
8080

8181
#ifdef SOC_MPI_SUPPORTED
8282
/**

components/esp_security/src/esp_crypto_lock.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ static _lock_t s_crypto_hmac_lock;
3333
static _lock_t s_crypto_mpi_lock;
3434
#endif /* SOC_MPI_SUPPORTED */
3535

36-
#if defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED)
36+
#if defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED)
3737
/* Single lock for SHA and AES, sharing a reserved GDMA channel */
3838
static _lock_t s_crypto_sha_aes_lock;
39-
#endif /* defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED) */
39+
#endif /* defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED) */
4040

4141
#ifdef SOC_ECC_SUPPORTED
4242
/* Lock for ECC peripheral */
@@ -83,7 +83,7 @@ void esp_crypto_ds_lock_release(void)
8383
}
8484
#endif /* SOC_DIG_SIGN_SUPPORTED */
8585

86-
#if defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED)
86+
#if defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED)
8787
void esp_crypto_sha_aes_lock_acquire(void)
8888
{
8989
_lock_acquire(&s_crypto_sha_aes_lock);
@@ -93,9 +93,9 @@ void esp_crypto_sha_aes_lock_release(void)
9393
{
9494
_lock_release(&s_crypto_sha_aes_lock);
9595
}
96-
#endif /* defined(SOC_SHA_SUPPORTED) && defined(SOC_AES_SUPPORTED) */
96+
#endif /* defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED) */
9797

98-
#if defined(SOC_SHA_CRYPTO_DMA) && defined(SOC_AES_CRYPTO_DMA)
98+
#if defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA)
9999
void esp_crypto_dma_lock_acquire(void)
100100
{
101101
_lock_acquire(&s_crypto_sha_aes_lock);
@@ -105,7 +105,7 @@ void esp_crypto_dma_lock_release(void)
105105
{
106106
_lock_release(&s_crypto_sha_aes_lock);
107107
}
108-
#endif /* defined(SOC_SHA_CRYPTO_DMA) && defined(SOC_AES_CRYPTO_DMA) */
108+
#endif /* defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA) */
109109

110110
#ifdef SOC_MPI_SUPPORTED
111111
void esp_crypto_mpi_lock_acquire(void)
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include <stdbool.h>
9+
#include "soc/hwcrypto_reg.h"
10+
#include "soc/pcr_struct.h"
11+
#include "hal/sha_types.h"
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
18+
/**
19+
* @brief Enable the bus clock for SHA peripheral module
20+
*
21+
* @param enable true to enable the module, false to disable the module
22+
*/
23+
static inline void sha_ll_enable_bus_clock(bool enable)
24+
{
25+
PCR.sha_conf.sha_clk_en = enable;
26+
}
27+
28+
/**
29+
* @brief Reset the SHA peripheral module
30+
*/
31+
static inline void sha_ll_reset_register(void)
32+
{
33+
PCR.sha_conf.sha_rst_en = 1;
34+
PCR.sha_conf.sha_rst_en = 0;
35+
36+
// Clear reset on digital signature, hmac and ecdsa also, otherwise SHA is held in reset
37+
PCR.ds_conf.ds_rst_en = 0;
38+
PCR.hmac_conf.hmac_rst_en = 0;
39+
PCR.ecdsa_conf.ecdsa_rst_en = 0;
40+
}
41+
42+
/**
43+
* @brief Start a new SHA block conversions (no initial hash in HW)
44+
*
45+
* @param sha_type The SHA algorithm type
46+
*/
47+
static inline void sha_ll_start_block(esp_sha_type sha_type)
48+
{
49+
REG_WRITE(SHA_MODE_REG, sha_type);
50+
REG_WRITE(SHA_START_REG, 1);
51+
}
52+
53+
/**
54+
* @brief Continue a SHA block conversion (initial hash in HW)
55+
*
56+
* @param sha_type The SHA algorithm type
57+
*/
58+
static inline void sha_ll_continue_block(esp_sha_type sha_type)
59+
{
60+
REG_WRITE(SHA_MODE_REG, sha_type);
61+
REG_WRITE(SHA_CONTINUE_REG, 1);
62+
}
63+
64+
/**
65+
* @brief Start a new SHA message conversion using DMA (no initial hash in HW)
66+
*
67+
* @param sha_type The SHA algorithm type
68+
*/
69+
static inline void sha_ll_start_dma(esp_sha_type sha_type)
70+
{
71+
REG_WRITE(SHA_MODE_REG, sha_type);
72+
REG_WRITE(SHA_DMA_START_REG, 1);
73+
}
74+
75+
/**
76+
* @brief Continue a SHA message conversion using DMA (initial hash in HW)
77+
*
78+
* @param sha_type The SHA algorithm type
79+
*/
80+
static inline void sha_ll_continue_dma(esp_sha_type sha_type)
81+
{
82+
REG_WRITE(SHA_MODE_REG, sha_type);
83+
REG_WRITE(SHA_DMA_CONTINUE_REG, 1);
84+
}
85+
86+
/**
87+
* @brief Load the current hash digest to digest register
88+
*
89+
* @note Happens automatically on ESP32C6
90+
*
91+
* @param sha_type The SHA algorithm type
92+
*/
93+
static inline void sha_ll_load(esp_sha_type sha_type)
94+
{
95+
}
96+
97+
/**
98+
* @brief Sets the number of message blocks to be hashed
99+
*
100+
* @note DMA operation only
101+
*
102+
* @param num_blocks Number of message blocks to process
103+
*/
104+
static inline void sha_ll_set_block_num(size_t num_blocks)
105+
{
106+
REG_WRITE(SHA_DMA_BLOCK_NUM_REG, num_blocks);
107+
}
108+
109+
/**
110+
* @brief Checks if the SHA engine is currently busy hashing a block
111+
*
112+
* @return true SHA engine busy
113+
* @return false SHA engine idle
114+
*/
115+
static inline bool sha_ll_busy(void)
116+
{
117+
return REG_READ(SHA_BUSY_REG);
118+
}
119+
120+
/**
121+
* @brief Write a text (message) block to the SHA engine
122+
*
123+
* @param input_text Input buffer to be written to the SHA engine
124+
* @param block_word_len Number of words in block
125+
*/
126+
static inline void sha_ll_fill_text_block(const void *input_text, size_t block_word_len)
127+
{
128+
uint32_t *data_words = (uint32_t *)input_text;
129+
uint32_t *reg_addr_buf = (uint32_t *)(SHA_M_MEM);
130+
131+
for (int i = 0; i < block_word_len; i++) {
132+
REG_WRITE(&reg_addr_buf[i], data_words[i]);
133+
}
134+
}
135+
136+
/**
137+
* @brief Read the message digest from the SHA engine
138+
*
139+
* @param sha_type The SHA algorithm type
140+
* @param digest_state Buffer that message digest will be written to
141+
* @param digest_word_len Length of the message digest
142+
*/
143+
static inline void sha_ll_read_digest(esp_sha_type sha_type, void *digest_state, size_t digest_word_len)
144+
{
145+
uint32_t *digest_state_words = (uint32_t *)digest_state;
146+
const size_t REG_WIDTH = sizeof(uint32_t);
147+
148+
for (size_t i = 0; i < digest_word_len; i++) {
149+
digest_state_words[i] = REG_READ(SHA_H_MEM + (i * REG_WIDTH));
150+
}
151+
152+
}
153+
154+
/**
155+
* @brief Write the message digest to the SHA engine
156+
*
157+
* @param sha_type The SHA algorithm type
158+
* @param digest_state Message digest to be written to SHA engine
159+
* @param digest_word_len Length of the message digest
160+
*/
161+
static inline void sha_ll_write_digest(esp_sha_type sha_type, void *digest_state, size_t digest_word_len)
162+
{
163+
uint32_t *digest_state_words = (uint32_t *)digest_state;
164+
uint32_t *reg_addr_buf = (uint32_t *)(SHA_H_MEM);
165+
166+
for (int i = 0; i < digest_word_len; i++) {
167+
REG_WRITE(&reg_addr_buf[i], digest_state_words[i]);
168+
}
169+
}
170+
171+
172+
#ifdef __cplusplus
173+
}
174+
#endif

components/hal/test_apps/crypto/main/sha/test_sha.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ TEST_GROUP(sha);
205205
TEST_SETUP(sha)
206206
{
207207
test_utils_record_free_mem();
208-
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
208+
TEST_ESP_OK(test_utils_set_leak_level(400, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
209209
}
210210

211211
TEST_TEAR_DOWN(sha)

components/mbedtls/port/crypto_shared_gdma/esp_crypto_shared_gdma.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ static esp_err_t crypto_shared_gdma_init(void)
9292
transfer_cfg.max_data_burst_size = 0;
9393
gdma_config_transfer(rx_channel, &transfer_cfg);
9494

95+
#ifdef SOC_AES_SUPPORTED
9596
gdma_connect(rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_AES, 0));
9697
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_AES, 0));
98+
#elif SOC_SHA_SUPPORTED
99+
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SHA, 0));
100+
#endif
97101

98102
return ESP_OK;
99103

@@ -123,11 +127,17 @@ esp_err_t esp_crypto_shared_gdma_start(const lldesc_t *input, const lldesc_t *ou
123127
/* Tx channel is shared between AES and SHA, need to connect to peripheral every time */
124128
gdma_disconnect(tx_channel);
125129

130+
#ifdef SOC_SHA_SUPPORTED
126131
if (peripheral == GDMA_TRIG_PERIPH_SHA) {
127132
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SHA, 0));
128-
} else if (peripheral == GDMA_TRIG_PERIPH_AES) {
133+
} else
134+
#endif // SOC_SHA_SUPPORTED
135+
#ifdef SOC_AES_SUPPORTED
136+
if (peripheral == GDMA_TRIG_PERIPH_AES) {
129137
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_AES, 0));
130-
} else {
138+
} else
139+
#endif // SOC_AES_SUPPORTED
140+
{
131141
return ESP_ERR_INVALID_ARG;
132142
}
133143

@@ -176,11 +186,17 @@ esp_err_t esp_crypto_shared_gdma_start_axi_ahb(const crypto_dma_desc_t *input, c
176186
/* Tx channel is shared between AES and SHA, need to connect to peripheral every time */
177187
gdma_disconnect(tx_channel);
178188

189+
#ifdef SOC_SHA_SUPPORTED
179190
if (peripheral == GDMA_TRIG_PERIPH_SHA) {
180191
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SHA, 0));
181-
} else if (peripheral == GDMA_TRIG_PERIPH_AES) {
192+
} else
193+
#endif // SOC_SHA_SUPPORTED
194+
#ifdef SOC_AES_SUPPORTED
195+
if (peripheral == GDMA_TRIG_PERIPH_AES) {
182196
gdma_connect(tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_AES, 0));
183-
} else {
197+
} else
198+
#endif // SOC_AES_SUPPORTED
199+
{
184200
return ESP_ERR_INVALID_ARG;
185201
}
186202

components/soc/esp32c61/include/soc/Kconfig.soc_caps.in

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ config SOC_SYSTIMER_SUPPORTED
7171
bool
7272
default y
7373

74+
config SOC_SHA_SUPPORTED
75+
bool
76+
default y
77+
7478
config SOC_ECC_SUPPORTED
7579
bool
7680
default y
@@ -471,6 +475,34 @@ config SOC_MPU_REGION_WO_SUPPORTED
471475
bool
472476
default n
473477

478+
config SOC_SHA_DMA_MAX_BUFFER_SIZE
479+
int
480+
default 3968
481+
482+
config SOC_SHA_SUPPORT_DMA
483+
bool
484+
default y
485+
486+
config SOC_SHA_SUPPORT_RESUME
487+
bool
488+
default y
489+
490+
config SOC_SHA_GDMA
491+
bool
492+
default y
493+
494+
config SOC_SHA_SUPPORT_SHA1
495+
bool
496+
default y
497+
498+
config SOC_SHA_SUPPORT_SHA224
499+
bool
500+
default y
501+
502+
config SOC_SHA_SUPPORT_SHA256
503+
bool
504+
default y
505+
474506
config SOC_ECDSA_SUPPORT_EXPORT_PUBKEY
475507
bool
476508
default y
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include "soc/sha_reg.h"

0 commit comments

Comments
 (0)