5 #include "sw/device/tests/penetrationtests/firmware/lib/pentest_lib.h"
7 #include "hw/ip/aes/model/aes.h"
14 #include "sw/device/lib/base/status.h"
18 #include "sw/device/lib/dif/dif_csrng_shared.h"
23 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
30 #include "sw/device/lib/runtime/irq.h"
32 #include "sw/device/lib/testing/pinmux_testutils.h"
33 #include "sw/device/lib/testing/rv_plic_testutils.h"
34 #include "sw/device/lib/testing/test_framework/check.h"
35 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
38 #include "clkmgr_regs.h"
39 #include "csrng_regs.h"
41 #include "sensor_ctrl_regs.h"
42 #include "sram_ctrl_regs.h"
44 #if !OT_IS_ENGLISH_BREAKFAST
45 #include "sw/device/lib/crypto/drivers/otbn.h"
49 #include "sw/device/lib/testing/alert_handler_testutils.h"
50 #include "sw/device/lib/testing/entropy_testutils.h"
73 kTriggerHwGateBitIndex = 9,
79 kTriggerSwBitIndex = 8,
83 kRvTimerComparator = 0,
88 static unsigned int trigger_bit_index = kTriggerHwGateBitIndex;
90 static dif_gpio_t gpio;
91 static dif_pinmux_t pinmux;
92 static dif_rv_timer_t timer;
93 static dif_sensor_ctrl_t sensor_ctrl;
94 static dif_uart_t uart0;
95 static dif_uart_t uart1;
97 #if !OT_IS_ENGLISH_BREAKFAST
98 static dif_alert_handler_t alert_handler;
99 static dif_csrng_t csrng;
100 static dif_edn_t edn0;
101 static dif_edn_t edn1;
102 static dif_lc_ctrl_t lc;
103 static dif_rv_plic_t plic;
104 static dif_rstmgr_t rstmgr;
107 status_t pentest_configure_entropy_source_max_reseed_interval(
void) {
108 #if !OT_IS_ENGLISH_BREAKFAST
109 const dif_csrng_t csrng = {
111 const dif_edn_t edn0 = {
113 const dif_edn_t edn1 = {
116 TRY(entropy_testutils_stop_all());
119 TRY(entropy_testutils_entropy_src_init());
131 .cmd = csrng_cmd_header_build(kCsrngAppCmdInstantiate,
142 .cmd = csrng_cmd_header_build(
153 .cmd = csrng_cmd_header_build(kCsrngAppCmdGenerate,
163 .reseed_interval = 0xffffffff,
174 .cmd = csrng_cmd_header_build(kCsrngAppCmdInstantiate,
185 .cmd = csrng_cmd_header_build(
196 .cmd = csrng_cmd_header_build(kCsrngAppCmdGenerate,
206 .reseed_interval = 0xffffffff,
214 memset(registered.alerts, 0,
sizeof(registered.alerts));
222 void pentest_clear_sensor_recov_alerts(
void) {
223 for (
size_t it = 0; it < SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS; it++) {
230 memset(registered.alerts, 0,
sizeof(registered.alerts));
232 #if !OT_IS_ENGLISH_BREAKFAST
235 for (
size_t alert = 0; alert < ALERT_HANDLER_PARAM_N_ALERTS; alert++) {
240 registered.alerts[0] |= (1 << alert);
241 }
else if (alert < 64) {
242 registered.alerts[1] |= (1 << (alert - 32));
244 registered.alerts[2] |= (1 << (alert - 64));
258 void pentest_configure_alert_handler(
void) {
259 #if !OT_IS_ENGLISH_BREAKFAST
260 irq_global_ctrl(
true);
261 irq_external_ctrl(
true);
265 CHECK_DIF_OK(dif_rv_plic_init(base_addr, &plic));
268 CHECK_DIF_OK(dif_alert_handler_init(base_addr, &alert_handler));
270 CHECK_DIF_OK(dif_rstmgr_init(
279 alert_classes[i] = kDifAlertHandlerClassA;
285 .duration_cycles = 2000}};
289 .accumulator_threshold = 0,
290 .irq_deadline_cycles = 10000,
291 .escalation_phases = esc_phases,
292 .escalation_phases_len =
ARRAYSIZE(esc_phases),
301 .alert_classes = alert_classes,
304 .class_configs = class_configs,
309 CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
312 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
317 status_t pentest_read_device_id(uint32_t device_id[]) {
318 #if !OT_IS_ENGLISH_BREAKFAST
321 CHECK_DIF_OK(dif_lc_ctrl_init(lc_reg, &lc));
325 memcpy(device_id, lc_device_id.data, 8 *
sizeof(uint32_t));
327 memset(device_id, 0, 8 *
sizeof(uint32_t));
334 bool disable_icache,
bool disable_dummy_instr,
bool enable_jittery_clock,
335 bool enable_sram_readback,
bool *clock_jitter_locked,
bool *clock_jitter_en,
336 bool *sram_main_readback_locked,
bool *sram_ret_readback_locked,
337 bool *sram_main_readback_en,
bool *sram_ret_readback_en) {
338 uint32_t cpuctrl_csr;
340 CSR_READ(CSR_REG_CPUCTRL, &cpuctrl_csr);
342 if (disable_icache) {
348 if (disable_dummy_instr) {
365 if (!*clock_jitter_locked) {
366 if (enable_jittery_clock) {
369 mmio_region_write32(clkmgr.base_addr, CLKMGR_JITTER_ENABLE_REG_OFFSET,
370 kMultiBitBool4False);
378 dif_sram_ctrl_t sram_ctrl_ret;
379 dif_sram_ctrl_t sram_ctrl_main;
380 TRY(dif_sram_ctrl_init(
383 TRY(dif_sram_ctrl_init(
389 if (enable_sram_readback) {
399 uint32_t ret_status =
401 SRAM_CTRL_READBACK_REG_OFFSET);
402 uint32_t main_status =
404 SRAM_CTRL_READBACK_REG_OFFSET);
407 *sram_main_readback_locked = (main_locked ==
kDifLocked) ?
true :
false;
408 *sram_ret_readback_locked = (ret_locked ==
kDifLocked) ?
true :
false;
410 *sram_ret_readback_en = (ret_status == kMultiBitBool4True) ?
true :
false;
411 *sram_main_readback_en = (main_status == kMultiBitBool4True) ?
true :
false;
419 static void pentest_init_uart(
void) {
420 CHECK(
kUartBaudrate <= UINT32_MAX,
"kUartBaudrate must fit in uint32_t");
422 "kClockFreqPeripheralHz must fit in uint32_t");
437 #if !OT_IS_ENGLISH_BREAKFAST
449 static void pentest_init_gpio(pentest_trigger_source_t trigger) {
453 uint32_t select_mask =
459 for (
size_t i = 0; i < 32; ++i) {
460 if ((select_mask | enable_mask) & (1u << i)) {
482 static void pentest_init_timer(
void) {
492 irq_timer_ctrl(
true);
493 irq_global_ctrl(
true);
499 static void pentest_init_csrng(
void) {
500 #if !OT_IS_ENGLISH_BREAKFAST
508 static void pentest_init_sensor_ctrl(
void) {
509 #if !OT_IS_ENGLISH_BREAKFAST
510 CHECK_DIF_OK(dif_sensor_ctrl_init(
519 static void pentest_init_edn(
void) {
520 #if !OT_IS_ENGLISH_BREAKFAST
534 void ottf_timer_isr(uint32_t *exc_info) {
540 &timer, kDifRvTimerIrqTimerExpiredHart0Timer0));
554 void sca_disable_peripherals(pentest_peripherals_t disable) {
555 #if !OT_IS_ENGLISH_BREAKFAST
556 if (disable & kPentestPeripheralEdn) {
560 if (disable & kPentestPeripheralCsrng) {
563 if (disable & kPentestPeripheralEntropy) {
564 dif_entropy_src_t entropy;
576 if (disable & kPentestPeripheralAes) {
580 #if !OT_IS_ENGLISH_BREAKFAST
581 if (disable & kPentestPeripheralHmac) {
586 if (disable & kPentestPeripheralIoDiv4) {
588 &clkmgr, CLKMGR_CLK_ENABLES_CLK_IO_DIV4_PERI_EN_BIT,
591 if (disable & kPentestPeripheralIoDiv2) {
593 &clkmgr, CLKMGR_CLK_ENABLES_CLK_IO_DIV2_PERI_EN_BIT,
596 if (disable & kPentestPeripheralUsb) {
600 if (disable & kPentestPeripheralIo) {
605 #if !OT_IS_ENGLISH_BREAKFAST
606 if (disable & kPentestPeripheralKmac) {
610 if (disable & kPentestPeripheralOtbn) {
617 void pentest_init(pentest_trigger_source_t trigger,
618 pentest_peripherals_t enable) {
621 pinmux_testutils_init(&pinmux);
623 pentest_init_gpio(trigger);
624 pentest_init_timer();
625 pentest_init_csrng();
627 pentest_init_sensor_ctrl();
628 sca_disable_peripherals(~enable);
631 const dif_uart_t *pentest_get_uart(
void) {
return &uart1; }
633 void pentest_select_trigger_type(pentest_trigger_type_t trigger_type) {
634 if (trigger_type == kPentestTriggerTypeHwGated) {
635 trigger_bit_index = kTriggerHwGateBitIndex;
636 }
else if (trigger_type == kPentestTriggerTypeSw) {
637 trigger_bit_index = kTriggerSwBitIndex;
641 void pentest_set_trigger_high(
void) {
645 void pentest_set_trigger_low(
void) {
649 void pentest_call_and_sleep(sca_callee callee, uint32_t sleep_cycles,
650 bool sw_trigger,
bool otbn) {
660 uint64_t current_time;
665 current_time + sleep_cycles));
670 pentest_set_trigger_high();
677 #if !OT_IS_ENGLISH_BREAKFAST
679 otbn_busy_wait_for_done();
684 pentest_set_trigger_low();
692 static uint32_t sca_lfsr_state_masking = 0xdeadbeef;
693 static uint32_t sca_lfsr_state_order = 0x99999999;
695 void pentest_seed_lfsr(uint32_t seed, pentest_lfsr_context_t context) {
696 if (context == kPentestLfsrMasking) {
697 sca_lfsr_state_masking = seed;
699 if (context == kPentestLfsrOrder) {
700 sca_lfsr_state_order = seed;
704 uint32_t pentest_next_lfsr(uint16_t num_steps, pentest_lfsr_context_t context) {
705 uint32_t sca_lfsr_state;
706 if (context == kPentestLfsrMasking) {
707 sca_lfsr_state = sca_lfsr_state_masking;
709 if (context == kPentestLfsrOrder) {
710 sca_lfsr_state = sca_lfsr_state_order;
712 const uint32_t lfsr_out = sca_lfsr_state;
713 for (
size_t i = 0; i < num_steps; ++i) {
714 bool lfsr_bit = sca_lfsr_state & 0x00000001;
715 sca_lfsr_state = sca_lfsr_state >> 1;
717 sca_lfsr_state ^= 0x80000057;
720 if (context == kPentestLfsrMasking) {
721 sca_lfsr_state_masking = sca_lfsr_state;
723 if (context == kPentestLfsrOrder) {
724 sca_lfsr_state_order = sca_lfsr_state;
729 uint32_t pentest_linear_layer(uint32_t input) {
732 (((input >> 0) & 0x1) << 7) | (((input >> 6) & 0x1) << 6) |
733 (((input >> 11) & 0x1) << 5) | (((input >> 14) & 0x1) << 4) |
734 (((input >> 1) & 0x1) << 3) | (((input >> 7) & 0x1) << 2) |
735 (((input >> 10) & 0x1) << 1) | (((input >> 13) & 0x1) << 0) |
737 (((input >> 2) & 0x1) << 15) | (((input >> 4) & 0x1) << 14) |
738 (((input >> 9) & 0x1) << 13) | (((input >> 12) & 0x1) << 12) |
739 (((input >> 3) & 0x1) << 11) | (((input >> 5) & 0x1) << 10) |
740 (((input >> 8) & 0x1) << 9) | (((input >> 15) & 0x1) << 8) |
742 (((input >> 16) & 0x1) << 23) | (((input >> 22) & 0x1) << 22) |
743 (((input >> 27) & 0x1) << 21) | (((input >> 30) & 0x1) << 20) |
744 (((input >> 17) & 0x1) << 19) | (((input >> 23) & 0x1) << 18) |
745 (((input >> 26) & 0x1) << 17) | (((input >> 29) & 0x1) << 16) |
747 (((input >> 18) & 0x1) << 31) | (((input >> 20) & 0x1) << 30) |
748 (((input >> 25) & 0x1) << 29) | (((input >> 28) & 0x1) << 28) |
749 (((input >> 19) & 0x1) << 27) | (((input >> 21) & 0x1) << 26) |
750 (((input >> 24) & 0x1) << 25) | (((input >> 31) & 0x1) << 24);
755 uint32_t pentest_non_linear_layer(uint32_t input) {
759 output = (uint32_t)(sbox[(input >> 24) & 0xFF] << 24) |
760 (uint32_t)(sbox[(input >> 16) & 0xFF] << 16) |
761 (uint32_t)(sbox[(input >> 8) & 0xFF] << 8) | sbox[input & 0xFF];