99#include "hal/ecdsa_hal.h"
1010#include "hal/efuse_hal.h"
1111
12+ #if CONFIG_HAL_ECDSA_GEN_SIG_CM
13+ #include "esp_fault.h"
14+ #include "esp_random.h"
15+ #endif
16+
1217// Need to remove in IDF-8621
1318#if CONFIG_IDF_TARGET_ESP32C5
1419#include "soc/keymng_reg.h"
@@ -64,23 +69,9 @@ bool ecdsa_hal_get_operation_result(void)
6469 return ecdsa_ll_get_operation_result ();
6570}
6671
67- void ecdsa_hal_gen_signature ( ecdsa_hal_config_t * conf , const uint8_t * hash ,
68- uint8_t * r_out , uint8_t * s_out , uint16_t len )
72+ static void ecdsa_hal_gen_signature_inner ( const uint8_t * hash , uint8_t * r_out ,
73+ uint8_t * s_out , uint16_t len )
6974{
70- if (len != ECDSA_HAL_P192_COMPONENT_LEN && len != ECDSA_HAL_P256_COMPONENT_LEN ) {
71- HAL_ASSERT (false && "Incorrect length" );
72- }
73-
74- if (conf -> sha_mode == ECDSA_Z_USER_PROVIDED && hash == NULL ) {
75- HAL_ASSERT (false && "Mismatch in SHA configuration" );
76- }
77-
78- if (ecdsa_ll_get_state () != ECDSA_STATE_IDLE ) {
79- HAL_ASSERT (false && "Incorrect ECDSA state" );
80- }
81-
82- configure_ecdsa_periph (conf );
83-
8475 ecdsa_ll_set_stage (ECDSA_STAGE_START_CALC );
8576
8677 while (ecdsa_ll_get_state () != ECDSA_STATE_LOAD ) {
@@ -105,6 +96,63 @@ void ecdsa_hal_gen_signature(ecdsa_hal_config_t *conf, const uint8_t *hash,
10596 }
10697}
10798
99+ #if CONFIG_HAL_ECDSA_GEN_SIG_CM
100+ __attribute__((optimize ("O0" ))) static void ecdsa_hal_gen_signature_with_countermeasure (const uint8_t * hash , uint8_t * r_out ,
101+ uint8_t * s_out , uint16_t len )
102+ {
103+ uint8_t tmp_r_out [32 ] = {};
104+ uint8_t tmp_s_out [32 ] = {};
105+ uint8_t tmp_hash [64 ] = {};
106+
107+ uint8_t dummy_op_count_prior = esp_random () % ECDSA_SIGN_MAX_DUMMY_OP_COUNT ;
108+ uint8_t dummy_op_count_later = ECDSA_SIGN_MAX_DUMMY_OP_COUNT - dummy_op_count_prior ;
109+ ESP_FAULT_ASSERT ((dummy_op_count_prior != 0 ) || (dummy_op_count_later != 0 ));
110+ ESP_FAULT_ASSERT (dummy_op_count_prior + dummy_op_count_later == ECDSA_SIGN_MAX_DUMMY_OP_COUNT );
111+
112+ esp_fill_random (tmp_hash , 64 );
113+ /* Dummy ecdsa signature operations prior to the actual one */
114+ for (int i = 0 ; i < dummy_op_count_prior ; i ++ ) {
115+ ecdsa_hal_gen_signature_inner (tmp_hash + ((6 * i ) % 32 ), (uint8_t * ) tmp_r_out , (uint8_t * ) tmp_s_out , len );
116+ }
117+
118+ /* Actual ecdsa signature operation */
119+ ecdsa_hal_gen_signature_inner (hash , r_out , s_out , len );
120+
121+ /* Dummy ecdsa signature operations after the actual one */
122+ for (int i = 0 ; i < dummy_op_count_later ; i ++ ) {
123+ ecdsa_hal_gen_signature_inner (tmp_hash + ((6 * i ) % 32 ), (uint8_t * )tmp_r_out , (uint8_t * )tmp_s_out , len );
124+ }
125+
126+ }
127+ #endif /* CONFIG_HAL_ECDSA_GEN_SIG_CM */
128+
129+
130+
131+ void ecdsa_hal_gen_signature (ecdsa_hal_config_t * conf , const uint8_t * hash ,
132+ uint8_t * r_out , uint8_t * s_out , uint16_t len )
133+ {
134+ if (len != ECDSA_HAL_P192_COMPONENT_LEN && len != ECDSA_HAL_P256_COMPONENT_LEN ) {
135+ HAL_ASSERT (false && "Incorrect length" );
136+ }
137+
138+ if (conf -> sha_mode == ECDSA_Z_USER_PROVIDED && hash == NULL ) {
139+ HAL_ASSERT (false && "Mismatch in SHA configuration" );
140+ }
141+
142+ if (ecdsa_ll_get_state () != ECDSA_STATE_IDLE ) {
143+ HAL_ASSERT (false && "Incorrect ECDSA state" );
144+ }
145+
146+ configure_ecdsa_periph (conf );
147+
148+ #if CONFIG_HAL_ECDSA_GEN_SIG_CM
149+ ecdsa_hal_gen_signature_with_countermeasure (hash , r_out , s_out , len );
150+ #else /* CONFIG_HAL_ECDSA_GEN_SIG_CM */
151+ ecdsa_hal_gen_signature_inner (hash , r_out , s_out , len );
152+ #endif /* !CONFIG_HAL_ECDSA_GEN_SIG_CM */
153+
154+ }
155+
108156int ecdsa_hal_verify_signature (ecdsa_hal_config_t * conf , const uint8_t * hash , const uint8_t * r , const uint8_t * s ,
109157 const uint8_t * pub_x , const uint8_t * pub_y , uint16_t len )
110158{
0 commit comments