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/rstmgr_testutils.h"
34 #include "sw/device/lib/testing/rv_plic_testutils.h"
35 #include "sw/device/lib/testing/test_framework/check.h"
36 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
38 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
39 #include "sw/device/lib/ujson/ujson.h"
41 #include "clkmgr_regs.h"
42 #include "csrng_regs.h"
44 #include "sensor_ctrl_regs.h"
45 #include "sram_ctrl_regs.h"
47 #if !OT_IS_ENGLISH_BREAKFAST
48 #include "sw/device/lib/crypto/drivers/otbn.h"
52 #include "sw/device/lib/testing/alert_handler_testutils.h"
53 #include "sw/device/lib/testing/entropy_testutils.h"
76 kTriggerHwGateBitIndex = 9,
82 kTriggerSwBitIndex = 8,
86 kRvTimerComparator = 0,
91 static unsigned int trigger_bit_index = kTriggerHwGateBitIndex;
93 static dif_gpio_t gpio;
94 static dif_pinmux_t pinmux;
95 static dif_rv_timer_t timer;
96 static dif_sensor_ctrl_t sensor_ctrl;
97 static dif_uart_t uart0;
98 static dif_uart_t uart1;
100 #if !OT_IS_ENGLISH_BREAKFAST
101 static dif_alert_handler_t alert_handler;
102 static dif_csrng_t csrng;
103 static dif_edn_t edn0;
104 static dif_edn_t edn1;
105 static dif_lc_ctrl_t lc;
106 static dif_rv_plic_t plic;
107 static dif_rstmgr_t rstmgr;
110 status_t pentest_configure_entropy_source_max_reseed_interval(
void) {
111 #if !OT_IS_ENGLISH_BREAKFAST
112 const dif_csrng_t csrng = {
114 const dif_edn_t edn0 = {
116 const dif_edn_t edn1 = {
119 TRY(entropy_testutils_stop_all());
122 TRY(entropy_testutils_entropy_src_init());
134 .cmd = csrng_cmd_header_build(kCsrngAppCmdInstantiate,
145 .cmd = csrng_cmd_header_build(
156 .cmd = csrng_cmd_header_build(kCsrngAppCmdGenerate,
166 .reseed_interval = 0xffffffff,
177 .cmd = csrng_cmd_header_build(kCsrngAppCmdInstantiate,
188 .cmd = csrng_cmd_header_build(
199 .cmd = csrng_cmd_header_build(kCsrngAppCmdGenerate,
209 .reseed_interval = 0xffffffff,
217 memset(registered.alerts, 0,
sizeof(registered.alerts));
225 void pentest_clear_sensor_recov_alerts(
void) {
226 for (
size_t it = 0; it < SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS; it++) {
233 memset(registered.alerts, 0,
sizeof(registered.alerts));
235 #if !OT_IS_ENGLISH_BREAKFAST
238 for (
size_t alert = 0; alert < ALERT_HANDLER_PARAM_N_ALERTS; alert++) {
243 registered.alerts[0] |= (1 << alert);
244 }
else if (alert < 64) {
245 registered.alerts[1] |= (1 << (alert - 32));
247 registered.alerts[2] |= (1 << (alert - 64));
261 void pentest_configure_alert_handler(
void) {
262 #if !OT_IS_ENGLISH_BREAKFAST
263 irq_global_ctrl(
true);
264 irq_external_ctrl(
true);
268 CHECK_DIF_OK(dif_rv_plic_init(base_addr, &plic));
271 CHECK_DIF_OK(dif_alert_handler_init(base_addr, &alert_handler));
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);
636 for (uint32_t k = 0; k < SENSOR_CTRL_PARAM_NUM_ALERT_EVENTS; k++) {
640 LOG_INFO(
"Enabling alert %d in sensor control did not work", k);
645 const dif_uart_t *pentest_get_uart(
void) {
return &uart1; }
647 void pentest_select_trigger_type(pentest_trigger_type_t trigger_type) {
648 if (trigger_type == kPentestTriggerTypeHwGated) {
649 trigger_bit_index = kTriggerHwGateBitIndex;
650 }
else if (trigger_type == kPentestTriggerTypeSw) {
651 trigger_bit_index = kTriggerSwBitIndex;
655 void pentest_set_trigger_high(
void) {
659 void pentest_set_trigger_low(
void) {
663 void pentest_call_and_sleep(sca_callee callee, uint32_t sleep_cycles,
664 bool sw_trigger,
bool otbn) {
674 uint64_t current_time;
679 current_time + sleep_cycles));
684 pentest_set_trigger_high();
691 #if !OT_IS_ENGLISH_BREAKFAST
693 otbn_busy_wait_for_done();
698 pentest_set_trigger_low();
706 static uint32_t sca_lfsr_state_masking = 0xdeadbeef;
707 static uint32_t sca_lfsr_state_order = 0x99999999;
709 void pentest_seed_lfsr(uint32_t seed, pentest_lfsr_context_t context) {
710 if (context == kPentestLfsrMasking) {
711 sca_lfsr_state_masking = seed;
713 if (context == kPentestLfsrOrder) {
714 sca_lfsr_state_order = seed;
718 uint32_t pentest_next_lfsr(uint16_t num_steps, pentest_lfsr_context_t context) {
719 uint32_t sca_lfsr_state;
720 if (context == kPentestLfsrMasking) {
721 sca_lfsr_state = sca_lfsr_state_masking;
723 if (context == kPentestLfsrOrder) {
724 sca_lfsr_state = sca_lfsr_state_order;
726 const uint32_t lfsr_out = sca_lfsr_state;
727 for (
size_t i = 0; i < num_steps; ++i) {
728 bool lfsr_bit = sca_lfsr_state & 0x00000001;
729 sca_lfsr_state = sca_lfsr_state >> 1;
731 sca_lfsr_state ^= 0x80000057;
734 if (context == kPentestLfsrMasking) {
735 sca_lfsr_state_masking = sca_lfsr_state;
737 if (context == kPentestLfsrOrder) {
738 sca_lfsr_state_order = sca_lfsr_state;
743 uint32_t pentest_linear_layer(uint32_t input) {
746 (((input >> 0) & 0x1) << 7) | (((input >> 6) & 0x1) << 6) |
747 (((input >> 11) & 0x1) << 5) | (((input >> 14) & 0x1) << 4) |
748 (((input >> 1) & 0x1) << 3) | (((input >> 7) & 0x1) << 2) |
749 (((input >> 10) & 0x1) << 1) | (((input >> 13) & 0x1) << 0) |
751 (((input >> 2) & 0x1) << 15) | (((input >> 4) & 0x1) << 14) |
752 (((input >> 9) & 0x1) << 13) | (((input >> 12) & 0x1) << 12) |
753 (((input >> 3) & 0x1) << 11) | (((input >> 5) & 0x1) << 10) |
754 (((input >> 8) & 0x1) << 9) | (((input >> 15) & 0x1) << 8) |
756 (((input >> 16) & 0x1) << 23) | (((input >> 22) & 0x1) << 22) |
757 (((input >> 27) & 0x1) << 21) | (((input >> 30) & 0x1) << 20) |
758 (((input >> 17) & 0x1) << 19) | (((input >> 23) & 0x1) << 18) |
759 (((input >> 26) & 0x1) << 17) | (((input >> 29) & 0x1) << 16) |
761 (((input >> 18) & 0x1) << 31) | (((input >> 20) & 0x1) << 30) |
762 (((input >> 25) & 0x1) << 29) | (((input >> 28) & 0x1) << 28) |
763 (((input >> 19) & 0x1) << 27) | (((input >> 21) & 0x1) << 26) |
764 (((input >> 24) & 0x1) << 25) | (((input >> 31) & 0x1) << 24);
769 uint32_t pentest_non_linear_layer(uint32_t input) {
773 output = (uint32_t)(sbox[(input >> 24) & 0xFF] << 24) |
774 (uint32_t)(sbox[(input >> 16) & 0xFF] << 16) |
775 (uint32_t)(sbox[(input >> 8) & 0xFF] << 8) | sbox[input & 0xFF];
790 rst_info = rstmgr_testutils_reason_get();
791 rstmgr_testutils_reason_clear();
799 TRY(alert_handler_testutils_info_parse(dump, (
int)seg_size, &actual_info));
802 penetrationtest_alert_dump_t uj_output;
803 uj_output.rst_reason = rst_info;
806 memset(uj_output.alert_cause, 0,
sizeof(uj_output.alert_cause));
807 memset(uj_output.class_accum_cnt, 0,
sizeof(uj_output.class_accum_cnt));
808 memset(uj_output.class_esc_cnt, 0,
sizeof(uj_output.class_esc_cnt));
809 memset(uj_output.class_esc_state, 0,
sizeof(uj_output.class_esc_state));
812 memcpy(uj_output.alert_cause, actual_info.alert_cause,
813 sizeof(uj_output.alert_cause));
814 uj_output.loc_alert_cause = actual_info.loc_alert_cause;
815 memcpy(uj_output.class_accum_cnt, actual_info.class_accum_cnt,
816 sizeof(uj_output.class_accum_cnt));
817 memcpy(uj_output.class_esc_cnt, actual_info.class_esc_cnt,
818 sizeof(uj_output.class_esc_cnt));
819 memcpy(uj_output.class_esc_state, actual_info.class_esc_state,
820 sizeof(uj_output.class_esc_state));
822 return RESP_OK(ujson_serialize_penetrationtest_alert_dump_t, uj, &uj_output);