Skip to content

Commit ac21c4f

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 abeb556 commit ac21c4f

23 files changed

+816
-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
@@ -51,6 +51,46 @@ cc_library(
5151
],
5252
)
5353

54+
cc_library(
55+
name = "ecc256_keygen_sca",
56+
srcs = ["ecc256_keygen_sca.c"],
57+
hdrs = ["ecc256_keygen_sca.h"],
58+
deps = [
59+
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
60+
"//sw/device/lib/base:abs_mmio",
61+
"//sw/device/lib/base:memory",
62+
"//sw/device/lib/crypto/drivers:otbn",
63+
"//sw/device/lib/runtime:ibex",
64+
"//sw/device/lib/runtime:log",
65+
"//sw/device/lib/testing/test_framework:ottf_main",
66+
"//sw/device/lib/testing/test_framework:ujson_ottf",
67+
"//sw/device/lib/ujson",
68+
"//sw/device/sca/lib:prng",
69+
"//sw/device/sca/lib:sca",
70+
"//sw/device/tests/penetrationtests/json:otbn_sca_commands",
71+
"//sw/otbn/crypto:p256_key_from_seed_sca",
72+
],
73+
)
74+
75+
cc_library(
76+
name = "otbn_sca",
77+
srcs = ["otbn_sca.c"],
78+
hdrs = ["otbn_sca.h"],
79+
deps = [
80+
":ecc256_keygen_sca",
81+
"//sw/device/lib/base:memory",
82+
"//sw/device/lib/base:status",
83+
"//sw/device/lib/crypto/impl:status",
84+
"//sw/device/lib/runtime:log",
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/firmware:sca_lib",
90+
"//sw/device/tests/penetrationtests/json:otbn_sca_commands",
91+
],
92+
)
93+
5494
cc_library(
5595
name = "ibex_fi",
5696
srcs = [
@@ -176,11 +216,17 @@ cc_library(
176216
hdrs = ["sca_lib.h"],
177217
deps = [
178218
"//sw/device/lib/base:csr",
219+
"//sw/device/lib/base:mmio",
179220
"//sw/device/lib/dif:alert_handler",
221+
"//sw/device/lib/dif:csrng",
222+
"//sw/device/lib/dif:csrng_shared",
223+
"//sw/device/lib/dif:edn",
224+
"//sw/device/lib/dif:entropy_src",
180225
"//sw/device/lib/dif:rstmgr",
181226
"//sw/device/lib/dif:rv_core_ibex",
182227
"//sw/device/lib/dif:rv_plic",
183228
"//sw/device/lib/testing:alert_handler_testutils",
229+
"//sw/device/lib/testing:entropy_testutils",
184230
"//sw/device/lib/testing:rv_plic_testutils",
185231
"//sw/device/lib/testing/test_framework:ottf_main",
186232
"//sw/device/lib/ujson",
@@ -227,6 +273,7 @@ FIRMWARE_DEPS = [
227273
":ibex_fi",
228274
":ibex_sca",
229275
":kmac_sca",
276+
":otbn_sca",
230277
":otbn_fi",
231278
":prng_sca",
232279
":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)