Skip to content

Commit 5ff8cef

Browse files
committed
feat: enable ecc support for esp32h21
This commmit enabled support for ECC peripheral in ESP32H21.
1 parent 3598909 commit 5ff8cef

File tree

3 files changed

+272
-2
lines changed

3 files changed

+272
-2
lines changed
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include <stdbool.h>
9+
#include <string.h>
10+
#include "hal/assert.h"
11+
#include "hal/ecc_types.h"
12+
#include "soc/ecc_mult_reg.h"
13+
#include "soc/pcr_struct.h"
14+
#include "soc/pcr_reg.h"
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
typedef enum {
21+
ECC_PARAM_PX = 0x0,
22+
ECC_PARAM_PY,
23+
ECC_PARAM_K,
24+
ECC_PARAM_QX,
25+
ECC_PARAM_QY,
26+
ECC_PARAM_QZ,
27+
} ecc_ll_param_t;
28+
29+
/**
30+
* @brief Enable the bus clock for ECC peripheral module
31+
*
32+
* @param true to enable the module, false to disable the module
33+
*/
34+
static inline void ecc_ll_enable_bus_clock(bool enable)
35+
{
36+
PCR.ecc_conf.ecc_clk_en = enable;
37+
}
38+
39+
/**
40+
* @brief Reset the ECC peripheral module
41+
*/
42+
static inline void ecc_ll_reset_register(void)
43+
{
44+
PCR.ecc_conf.ecc_rst_en = 1;
45+
PCR.ecc_conf.ecc_rst_en = 0;
46+
47+
// Clear reset on ECDSA, otherwise ECC is held in reset
48+
PCR.ecdsa_conf.ecdsa_rst_en = 0;
49+
}
50+
51+
static inline void ecc_ll_power_up(void)
52+
{
53+
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD);
54+
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PD);
55+
}
56+
57+
static inline void ecc_ll_power_down(void)
58+
{
59+
REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PU);
60+
REG_SET_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD);
61+
}
62+
63+
static inline void ecc_ll_enable_interrupt(void)
64+
{
65+
REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 1);
66+
}
67+
68+
static inline void ecc_ll_disable_interrupt(void)
69+
{
70+
REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 0);
71+
}
72+
73+
static inline void ecc_ll_clear_interrupt(void)
74+
{
75+
REG_SET_FIELD(ECC_MULT_INT_CLR_REG, ECC_MULT_CALC_DONE_INT_CLR, 1);
76+
}
77+
78+
static inline void ecc_ll_set_mode(ecc_mode_t mode)
79+
{
80+
switch(mode) {
81+
case ECC_MODE_POINT_MUL:
82+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 0);
83+
break;
84+
case ECC_MODE_VERIFY:
85+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 2);
86+
break;
87+
case ECC_MODE_VERIFY_THEN_POINT_MUL:
88+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 3);
89+
break;
90+
case ECC_MODE_JACOBIAN_POINT_MUL:
91+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 4);
92+
break;
93+
case ECC_MODE_POINT_ADD:
94+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 5);
95+
break;
96+
case ECC_MODE_JACOBIAN_POINT_VERIFY:
97+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 6);
98+
break;
99+
case ECC_MODE_POINT_VERIFY_JACOBIAN_MUL:
100+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 7);
101+
break;
102+
case ECC_MODE_MOD_ADD:
103+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 8);
104+
break;
105+
case ECC_MODE_MOD_SUB:
106+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 9);
107+
break;
108+
case ECC_MODE_MOD_MUL:
109+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 10);
110+
break;
111+
case ECC_MODE_INVERSE_MUL:
112+
REG_SET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE, 11);
113+
break;
114+
default:
115+
HAL_ASSERT(false && "Unsupported mode");
116+
break;
117+
}
118+
}
119+
120+
static inline void ecc_ll_set_curve(ecc_curve_t curve)
121+
{
122+
switch(curve) {
123+
case ECC_CURVE_SECP256R1:
124+
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH);
125+
break;
126+
case ECC_CURVE_SECP192R1:
127+
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH);
128+
break;
129+
default:
130+
HAL_ASSERT(false && "Unsupported curve");
131+
return;
132+
}
133+
}
134+
135+
static inline void ecc_ll_set_mod_base(ecc_mod_base_t base)
136+
{
137+
switch(base) {
138+
case ECC_MOD_N:
139+
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE);
140+
break;
141+
case ECC_MOD_P:
142+
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE);
143+
break;
144+
default:
145+
HAL_ASSERT(false && "Unsupported curve");
146+
return;
147+
}
148+
}
149+
150+
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
151+
{
152+
if (enable) {
153+
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
154+
} else {
155+
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
156+
}
157+
}
158+
159+
static inline void ecc_ll_write_param(ecc_ll_param_t param, const uint8_t *buf, uint16_t len)
160+
{
161+
uint32_t reg;
162+
uint32_t word;
163+
switch (param) {
164+
case ECC_PARAM_PX:
165+
reg = ECC_MULT_PX_MEM;
166+
break;
167+
case ECC_PARAM_PY:
168+
reg = ECC_MULT_PY_MEM;
169+
break;
170+
case ECC_PARAM_K:
171+
reg = ECC_MULT_K_MEM;
172+
break;
173+
case ECC_PARAM_QX:
174+
reg = ECC_MULT_QX_MEM;
175+
break;
176+
case ECC_PARAM_QY:
177+
reg = ECC_MULT_QY_MEM;
178+
break;
179+
case ECC_PARAM_QZ:
180+
reg = ECC_MULT_QZ_MEM;
181+
break;
182+
default:
183+
HAL_ASSERT(false && "Invalid parameter");
184+
return;
185+
}
186+
187+
for (int i = 0; i < len; i += 4) {
188+
memcpy(&word, buf + i, 4);
189+
REG_WRITE(reg + i, word);
190+
}
191+
}
192+
193+
static inline void ecc_ll_start_calc(void)
194+
{
195+
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_START);
196+
}
197+
198+
static inline int ecc_ll_is_calc_finished(void)
199+
{
200+
return REG_GET_FIELD(ECC_MULT_INT_RAW_REG, ECC_MULT_CALC_DONE_INT_RAW);
201+
}
202+
203+
static inline ecc_mode_t ecc_ll_get_mode(void)
204+
{
205+
return (ecc_mode_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_WORK_MODE));
206+
}
207+
208+
static inline int ecc_ll_get_verification_result(void)
209+
{
210+
return REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_VERIFICATION_RESULT);
211+
}
212+
213+
static inline ecc_curve_t ecc_ll_get_curve(void)
214+
{
215+
return (ecc_curve_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_KEY_LENGTH));
216+
}
217+
218+
static inline ecc_mod_base_t ecc_ll_get_mod_base(void)
219+
{
220+
return (ecc_mod_base_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE));
221+
}
222+
223+
static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_t len)
224+
{
225+
uint32_t reg;
226+
switch (param) {
227+
case ECC_PARAM_PX:
228+
reg = ECC_MULT_PX_MEM;
229+
break;
230+
case ECC_PARAM_PY:
231+
reg = ECC_MULT_PY_MEM;
232+
break;
233+
case ECC_PARAM_K:
234+
reg = ECC_MULT_K_MEM;
235+
break;
236+
case ECC_PARAM_QX:
237+
reg = ECC_MULT_QX_MEM;
238+
break;
239+
case ECC_PARAM_QY:
240+
reg = ECC_MULT_QY_MEM;
241+
break;
242+
case ECC_PARAM_QZ:
243+
reg = ECC_MULT_QZ_MEM;
244+
break;
245+
default:
246+
HAL_ASSERT(false && "Invalid parameter");
247+
return;
248+
}
249+
250+
memcpy(buf, (void *)reg, len);
251+
}
252+
253+
#ifdef __cplusplus
254+
}
255+
#endif

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ config SOC_SYSTIMER_SUPPORTED
2323
bool
2424
default y
2525

26+
config SOC_ECC_SUPPORTED
27+
bool
28+
default y
29+
30+
config SOC_ECC_EXTENDED_MODES_SUPPORTED
31+
bool
32+
default y
33+
2634
config SOC_FLASH_ENC_SUPPORTED
2735
bool
2836
default y
@@ -299,6 +307,10 @@ config SOC_SHA_SUPPORT_SHA256
299307
bool
300308
default y
301309

310+
config SOC_ECC_CONSTANT_TIME_POINT_MUL
311+
bool
312+
default y
313+
302314
config SOC_SPI_PERIPH_NUM
303315
int
304316
default 2

components/soc/esp32h21/include/soc/soc_caps.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
// #define SOC_SHA_SUPPORTED 1 //TODO: [ESP32H21] IDF-11501
4848
// #define SOC_HMAC_SUPPORTED 1 //TODO: [ESP32H21] IDF-11495
4949
// #define SOC_DIG_SIGN_SUPPORTED 1 //TODO: [ESP32H21] IDF-11497
50-
// #define SOC_ECC_SUPPORTED 1 //TODO: [ESP32H21] IDF-11502
51-
// #define SOC_ECC_EXTENDED_MODES_SUPPORTED 1 //TODO: [ESP32H21] IDF-11502
50+
#define SOC_ECC_SUPPORTED 1
51+
#define SOC_ECC_EXTENDED_MODES_SUPPORTED 1
5252
// #define SOC_ECDSA_SUPPORTED 1 //TODO: [ESP32H21] IDF-11496
5353
#define SOC_FLASH_ENC_SUPPORTED 1 //TODO: [ESP32H21] IDF-11499
5454
// #define SOC_SECURE_BOOT_SUPPORTED 1 //TODO: [ESP32H21] IDF-11500
@@ -373,6 +373,9 @@
373373
#define SOC_SHA_SUPPORT_SHA224 (1)
374374
#define SOC_SHA_SUPPORT_SHA256 (1)
375375

376+
/*--------------------------- ECC CAPS ---------------------------------------*/
377+
#define SOC_ECC_CONSTANT_TIME_POINT_MUL 1
378+
376379
/*-------------------------- SPI CAPS ----------------------------------------*/
377380
#define SOC_SPI_PERIPH_NUM 2
378381
#define SOC_SPI_PERIPH_CS_NUM(i) 6

0 commit comments

Comments
 (0)