Skip to content

Commit 78c57c5

Browse files
committed
[pentest] Add ECC256 Keygen SCA test
This commit adds the ECC256 key generation side-channel penetration test to the codebase. The host code is located in lowRISC/ot-sca#347 Signed-off-by: Pascal Nasahl <[email protected]>
1 parent 3f457a0 commit 78c57c5

23 files changed

+812
-16
lines changed

sw/device/sca/aes_serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static void aes_encrypt(const uint8_t *plaintext, size_t plaintext_len) {
245245
// Using the SecAesStartTriggerDelay hardware parameter, the AES unit is
246246
// configured to start operation 40 cycles after receiving the start trigger.
247247
// This allows Ibex to go to sleep in order to not disturb the capture.
248-
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, false);
248+
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, false, false);
249249
}
250250

251251
/**

sw/device/sca/kmac_serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static void sha3_serial_absorb(const uint8_t *msg, size_t msg_len) {
480480
// configured to start operation 40 cycles after receiving the START and PROC
481481
// commands. This allows Ibex to go to sleep in order to not disturb the
482482
// capture.
483-
sca_call_and_sleep(kmac_msg_proc, kIbexSha3SleepCycles, false);
483+
sca_call_and_sleep(kmac_msg_proc, kIbexSha3SleepCycles, false, false);
484484
}
485485

486486
/**

sw/device/sca/lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cc_library(
5555
"//sw/device/lib/arch:device",
5656
"//sw/device/lib/base:bitfield",
5757
"//sw/device/lib/base:macros",
58+
"//sw/device/lib/crypto/drivers:otbn",
5859
"//sw/device/lib/dif:clkmgr",
5960
"//sw/device/lib/dif:csrng",
6061
"//sw/device/lib/dif:edn",

sw/device/sca/lib/sca.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sw/device/lib/arch/device.h"
99
#include "sw/device/lib/base/bitfield.h"
1010
#include "sw/device/lib/base/macros.h"
11+
#include "sw/device/lib/crypto/drivers/otbn.h"
1112
#include "sw/device/lib/dif/dif_clkmgr.h"
1213
#include "sw/device/lib/dif/dif_entropy_src.h"
1314
#include "sw/device/lib/dif/dif_gpio.h"
@@ -310,7 +311,7 @@ void sca_set_trigger_low(void) {
310311
}
311312

312313
void sca_call_and_sleep(sca_callee callee, uint32_t sleep_cycles,
313-
bool sw_trigger) {
314+
bool sw_trigger, bool otbn) {
314315
// Disable the IO_DIV4_PERI clock to reduce noise during the actual capture.
315316
// This also disables the UART(s) and GPIO modules required for
316317
// communication with the scope. Therefore, it has to be re-enabled after
@@ -335,12 +336,16 @@ void sca_call_and_sleep(sca_callee callee, uint32_t sleep_cycles,
335336

336337
callee();
337338

339+
wait_for_interrupt();
340+
341+
if (otbn) {
342+
otbn_busy_wait_for_done();
343+
}
344+
338345
if (sw_trigger) {
339346
sca_set_trigger_low();
340347
}
341348

342-
wait_for_interrupt();
343-
344349
// Re-enable IO_DIV4_PERI clock to resume communication with the scope.
345350
OT_DISCARD(dif_clkmgr_gateable_clock_set_enabled(
346351
&clkmgr, CLKMGR_CLK_ENABLES_CLK_IO_DIV4_PERI_EN_BIT, kDifToggleEnabled));

sw/device/sca/lib/sca.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ typedef void (*sca_callee)(void);
197197
* @param callee Function to call before putting Ibex to sleep.
198198
* @param sleep_cycles Number of cycles to sleep.
199199
* @param sw_trigger Raise trigger before calling the target function.
200+
* @param otbn Wait until OTBN execution has finished.
200201
*/
201202
void sca_call_and_sleep(sca_callee callee, uint32_t sleep_cycles,
202-
bool sw_trigger);
203+
bool sw_trigger, bool otbn);
203204

204205
/**
205206
* Seeds the software LFSR usable e.g. for key masking.

sw/device/sca/otbn_vertical/ecc256_keygen_serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void p256_run_keygen(uint32_t mode, const uint32_t *share0,
204204

205205
// Execute program.
206206
sca_set_trigger_high();
207-
sca_call_and_sleep(otbn_manual_trigger, kIbexOtbnSleepCycles, false);
207+
sca_call_and_sleep(otbn_manual_trigger, kIbexOtbnSleepCycles, false, false);
208208
SS_CHECK_STATUS_OK(otbn_busy_wait_for_done());
209209
sca_set_trigger_low();
210210
}

sw/device/sca/otbn_vertical/ecc256_modinv_serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void p256_run_modinv(uint32_t *k0, uint32_t *k1) {
9292

9393
// Execute program.
9494
sca_set_trigger_high();
95-
sca_call_and_sleep(otbn_manual_trigger, kIbexOtbnSleepCycles, false);
95+
sca_call_and_sleep(otbn_manual_trigger, kIbexOtbnSleepCycles, false, false);
9696
otbn_busy_wait_for_done();
9797
sca_set_trigger_low();
9898
}

sw/device/sca/sha3_serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static void sha3_serial_absorb(const uint8_t *msg, size_t msg_len) {
405405
// configured to start operation 40 cycles after receiving the START and PROC
406406
// commands. This allows Ibex to go to sleep in order to not disturb the
407407
// capture.
408-
sca_call_and_sleep(kmac_msg_proc, kIbexSha3SleepCycles, true);
408+
sca_call_and_sleep(kmac_msg_proc, kIbexSha3SleepCycles, true, false);
409409
}
410410

411411
/**

sw/device/tests/penetrationtests/firmware/BUILD

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,46 @@ cc_library(
7070
],
7171
)
7272

73+
cc_library(
74+
name = "ecc256_keygen_sca",
75+
srcs = ["ecc256_keygen_sca.c"],
76+
hdrs = ["ecc256_keygen_sca.h"],
77+
deps = [
78+
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
79+
"//sw/device/lib/base:abs_mmio",
80+
"//sw/device/lib/base:memory",
81+
"//sw/device/lib/crypto/drivers:otbn",
82+
"//sw/device/lib/runtime:ibex",
83+
"//sw/device/lib/runtime:log",
84+
"//sw/device/lib/testing/test_framework:ottf_main",
85+
"//sw/device/lib/testing/test_framework:ujson_ottf",
86+
"//sw/device/lib/ujson",
87+
"//sw/device/sca/lib:prng",
88+
"//sw/device/sca/lib:sca",
89+
"//sw/device/tests/penetrationtests/json:otbn_sca_commands",
90+
"//sw/otbn/crypto:p256_key_from_seed_sca",
91+
],
92+
)
93+
94+
cc_library(
95+
name = "otbn_sca",
96+
srcs = ["otbn_sca.c"],
97+
hdrs = ["otbn_sca.h"],
98+
deps = [
99+
":ecc256_keygen_sca",
100+
"//sw/device/lib/base:memory",
101+
"//sw/device/lib/base:status",
102+
"//sw/device/lib/crypto/impl:status",
103+
"//sw/device/lib/runtime:log",
104+
"//sw/device/lib/testing/test_framework:ujson_ottf",
105+
"//sw/device/lib/ujson",
106+
"//sw/device/sca/lib:prng",
107+
"//sw/device/sca/lib:sca",
108+
"//sw/device/tests/penetrationtests/firmware:sca_lib",
109+
"//sw/device/tests/penetrationtests/json:otbn_sca_commands",
110+
],
111+
)
112+
73113
cc_library(
74114
name = "ibex_fi",
75115
srcs = [
@@ -195,12 +235,18 @@ cc_library(
195235
hdrs = ["sca_lib.h"],
196236
deps = [
197237
"//sw/device/lib/base:csr",
238+
"//sw/device/lib/base:mmio",
198239
"//sw/device/lib/dif:alert_handler",
240+
"//sw/device/lib/dif:csrng",
241+
"//sw/device/lib/dif:csrng_shared",
242+
"//sw/device/lib/dif:edn",
243+
"//sw/device/lib/dif:entropy_src",
199244
"//sw/device/lib/dif:lc_ctrl",
200245
"//sw/device/lib/dif:rstmgr",
201246
"//sw/device/lib/dif:rv_core_ibex",
202247
"//sw/device/lib/dif:rv_plic",
203248
"//sw/device/lib/testing:alert_handler_testutils",
249+
"//sw/device/lib/testing:entropy_testutils",
204250
"//sw/device/lib/testing:rv_plic_testutils",
205251
"//sw/device/lib/testing/test_framework:ottf_main",
206252
"//sw/device/lib/testing/test_framework:ujson_ottf",
@@ -250,6 +296,7 @@ FIRMWARE_DEPS = [
250296
":ibex_fi",
251297
":ibex_sca",
252298
":kmac_sca",
299+
":otbn_sca",
253300
":otbn_fi",
254301
":prng_sca",
255302
":sha3_sca",

sw/device/tests/penetrationtests/firmware/aes_sca.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ static aes_sca_error_t aes_encrypt(const uint8_t *plaintext,
250250
// Start AES operation (this triggers the capture) and go to sleep.
251251
if (fpga_mode) {
252252
// On the FPGA, the AES block automatically sets and unsets the trigger.
253-
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, false);
253+
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, false, false);
254254
} else {
255255
// On the chip, we need to manually set and unset the trigger. This is done
256256
// in this function to have the trigger as close as possible to the AES
257257
// operation.
258-
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, true);
258+
sca_call_and_sleep(aes_manual_trigger, kIbexAesSleepCycles, true, false);
259259
}
260260

261261
return aesScaOk;

0 commit comments

Comments
 (0)