Skip to content

Commit 49ced36

Browse files
AdityaHPatwardhanHarshal5
authored andcommitted
ci(hal/crypto): Add ECC hardware constant-time point multiplication test
1 parent 2bf6a3c commit 49ced36

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

components/hal/test_apps/crypto/main/ecc/test_ecc.c

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/
66

77
#include <stdio.h>
88
#include <stdbool.h>
99
#include <string.h>
10+
#include <sys/param.h>
1011
#include "sdkconfig.h"
1112
#include "esp_private/esp_crypto_lock_internal.h"
1213
#include "esp_log.h"
@@ -17,8 +18,8 @@
1718

1819
#include "memory_checks.h"
1920
#include "unity_fixture.h"
21+
#include "ccomp_timer.h"
2022

21-
#define _DEBUG_ 0
2223
#define SOC_ECC_SUPPORT_POINT_MULT 1
2324
#define SOC_ECC_SUPPORT_POINT_VERIFY 1
2425

@@ -86,6 +87,9 @@ static void ecc_point_mul(const uint8_t *k_le, const uint8_t *x_le, const uint8_
8687
} else {
8788
ecc_hal_set_mode(ECC_MODE_POINT_MUL);
8889
}
90+
#ifdef SOC_ECC_CONSTANT_TIME_POINT_MUL
91+
ecc_hal_enable_constant_time_point_mul(true);
92+
#endif /* SOC_ECC_CONSTANT_TIME_POINT_MUL */
8993
ecc_hal_start_calc();
9094

9195
while (!ecc_hal_is_calc_finished()) {
@@ -118,13 +122,11 @@ static void test_ecc_point_mul_inner(bool verify_first)
118122
ecc_be_to_le(ecc_p256_mul_res_x, x_mul_le, 32);
119123
ecc_be_to_le(ecc_p256_mul_res_y, y_mul_le, 32);
120124

121-
#if _DEBUG_
122-
ESP_LOG_BUFFER_HEX("Expected X:", x_mul_le, 32);
123-
ESP_LOG_BUFFER_HEX("Got X:", x_res_le, 32);
125+
ESP_LOG_BUFFER_HEXDUMP("Expected X:", x_mul_le, 32, ESP_LOG_DEBUG);
126+
ESP_LOG_BUFFER_HEXDUMP("Got X:", x_res_le, 32, ESP_LOG_DEBUG);
124127

125-
ESP_LOG_BUFFER_HEX("Expected Y:", y_mul_le, 32);
126-
ESP_LOG_BUFFER_HEX("Got Y:", y_res_le, 32);
127-
#endif
128+
ESP_LOG_BUFFER_HEXDUMP("Expected Y:", y_mul_le, 32, ESP_LOG_DEBUG);
129+
ESP_LOG_BUFFER_HEXDUMP("Got Y:", y_res_le, 32, ESP_LOG_DEBUG);
128130

129131
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(x_mul_le, x_res_le, 32, "X coordinate of P256 point multiplication ");
130132
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(y_mul_le, y_res_le, 32, "Y coordinate of P256 point multiplication ");
@@ -144,13 +146,11 @@ static void test_ecc_point_mul_inner(bool verify_first)
144146
ecc_be_to_le(ecc_p192_mul_res_x, x_mul_le, 24);
145147
ecc_be_to_le(ecc_p192_mul_res_y, y_mul_le, 24);
146148

147-
#if _DEBUG_
148-
ESP_LOG_BUFFER_HEX("Expected X:", x_mul_le, 32);
149-
ESP_LOG_BUFFER_HEX("Got X:", x_res_le, 32);
149+
ESP_LOG_BUFFER_HEXDUMP("Expected X:", x_mul_le, 32, ESP_LOG_DEBUG);
150+
ESP_LOG_BUFFER_HEXDUMP("Got X:", x_res_le, 32, ESP_LOG_DEBUG);
150151

151-
ESP_LOG_BUFFER_HEX("Expected Y:", y_mul_le, 32);
152-
ESP_LOG_BUFFER_HEX("Got Y:", y_res_le, 32);
153-
#endif
152+
ESP_LOG_BUFFER_HEXDUMP("Expected Y:", y_mul_le, 32, ESP_LOG_DEBUG);
153+
ESP_LOG_BUFFER_HEXDUMP("Got Y:", y_res_le, 32, ESP_LOG_DEBUG);
154154

155155
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(x_mul_le, x_res_le, 24, "X coordinate of P192 point multiplication ");
156156
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(y_mul_le, y_res_le, 24, "Y coordinate of P192 point multiplication ");
@@ -160,6 +160,74 @@ TEST(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1)
160160
{
161161
test_ecc_point_mul_inner(false);
162162
}
163+
164+
#if SOC_ECC_CONSTANT_TIME_POINT_MUL
165+
166+
#define CONST_TIME_DEVIATION_PERCENT 0.002
167+
168+
static void test_ecc_point_mul_inner_constant_time(void)
169+
{
170+
uint8_t scalar_le[32];
171+
uint8_t x_le[32];
172+
uint8_t y_le[32];
173+
174+
/* P256 */
175+
ecc_be_to_le(ecc_p256_scalar, scalar_le, 32);
176+
ecc_be_to_le(ecc_p256_point_x, x_le, 32);
177+
ecc_be_to_le(ecc_p256_point_y, y_le, 32);
178+
179+
uint8_t x_res_le[32];
180+
uint8_t y_res_le[32];
181+
182+
double deviation = 0;
183+
uint32_t elapsed_time, mean_elapsed_time, total_elapsed_time = 0;
184+
uint32_t max_time = 0, min_time = UINT32_MAX;
185+
int loop_count = 10;
186+
187+
for (int i = 0; i < loop_count; i++) {
188+
ccomp_timer_start();
189+
ecc_point_mul(scalar_le, x_le, y_le, 32, 0, x_res_le, y_res_le);
190+
elapsed_time = ccomp_timer_stop();
191+
192+
max_time = MAX(elapsed_time, max_time);
193+
min_time = MIN(elapsed_time, min_time);
194+
total_elapsed_time += elapsed_time;
195+
}
196+
mean_elapsed_time = total_elapsed_time / loop_count;
197+
deviation = ((double)(max_time - mean_elapsed_time) / mean_elapsed_time);
198+
199+
TEST_ASSERT_LESS_THAN_DOUBLE(CONST_TIME_DEVIATION_PERCENT, deviation);
200+
201+
/* P192 */
202+
ecc_be_to_le(ecc_p192_scalar, scalar_le, 24);
203+
ecc_be_to_le(ecc_p192_point_x, x_le, 24);
204+
ecc_be_to_le(ecc_p192_point_y, y_le, 24);
205+
206+
max_time = 0;
207+
min_time = UINT32_MAX;
208+
total_elapsed_time = 0;
209+
210+
for (int i = 0; i < loop_count; i++) {
211+
ccomp_timer_start();
212+
ecc_point_mul(scalar_le, x_le, y_le, 24, 0, x_res_le, y_res_le);
213+
elapsed_time = ccomp_timer_stop();
214+
215+
max_time = MAX(elapsed_time, max_time);
216+
min_time = MIN(elapsed_time, min_time);
217+
total_elapsed_time += elapsed_time;
218+
}
219+
mean_elapsed_time = total_elapsed_time / loop_count;
220+
deviation = ((double)(max_time - mean_elapsed_time) / mean_elapsed_time);
221+
222+
TEST_ASSERT_LESS_THAN_DOUBLE(CONST_TIME_DEVIATION_PERCENT, deviation);
223+
}
224+
225+
TEST(ecc, ecc_point_multiplication_const_time_check_on_SECP192R1_and_SECP256R1)
226+
{
227+
test_ecc_point_mul_inner_constant_time();
228+
}
229+
#endif
230+
163231
#endif
164232

165233
#if SOC_ECC_SUPPORT_POINT_VERIFY && !defined(SOC_ECC_SUPPORT_POINT_VERIFY_QUIRK)
@@ -493,6 +561,9 @@ TEST_GROUP_RUNNER(ecc)
493561
{
494562
#if SOC_ECC_SUPPORT_POINT_MULT
495563
RUN_TEST_CASE(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1);
564+
#if SOC_ECC_CONSTANT_TIME_POINT_MUL
565+
RUN_TEST_CASE(ecc, ecc_point_multiplication_const_time_check_on_SECP192R1_and_SECP256R1);
566+
#endif
496567
#endif
497568

498569
#if SOC_ECC_SUPPORT_POINT_VERIFY && !defined(SOC_ECC_SUPPORT_POINT_VERIFY_QUIRK)
@@ -534,5 +605,4 @@ TEST_GROUP_RUNNER(ecc)
534605
#if SOC_ECC_SUPPORT_MOD_MUL
535606
RUN_TEST_CASE(ecc, ecc_mod_multiplication_using_SECP192R1_and_SECP256R1_order_of_curve);
536607
#endif
537-
538608
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/ccomp_timer: ">=1.0.0"

0 commit comments

Comments
 (0)