5 #include "hw/ip/aes/model/aes_modes.h"
17 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
21 #include "sw/device/lib/testing/aes_testutils.h"
22 #include "sw/device/lib/testing/entropy_testutils.h"
24 #include "sw/device/lib/testing/test_framework/check.h"
26 #include "sw/device/tests/otbn_randomness_impl.h"
29 #include "sw/device/lib/dif/autogen/dif_entropy_src_autogen.h"
31 #define TIMEOUT (1000 * 1000)
33 OTTF_DEFINE_TEST_CONFIG();
36 static dif_entropy_src_t entropy_src;
37 static dif_csrng_t csrng;
38 static dif_edn_t edn0;
39 static dif_edn_t edn1;
41 static dif_otbn_t otbn;
42 static dif_alert_handler_t alert_handler;
44 status_t init_test_environment(
void) {
46 "Initializing modules sntropy_src, csrng, edn0, edn1, aes, otbn and "
48 TRY(dif_entropy_src_init(
56 TRY(dif_alert_handler_init(
63 status_t disable_entropy_complex(
void) {
66 LOG_INFO(
"Disabling the entropy complex...");
67 return entropy_testutils_stop_all();
70 status_t configure_loose_health_tests(
void) {
71 LOG_INFO(
"Configuring loose health test thresholds...");
77 "Entropy source is locked. Cannot configure ENTROPY_SRC");
82 .high_threshold = 512,
90 .high_threshold = 512,
98 .high_threshold = 512,
106 .high_threshold = 512,
114 status_t enable_loose_entropy_src_bypass_mode(
void) {
115 LOG_INFO(
"Enabling ENTROPY_SRC in bypass mode...");
120 TRY_CHECK(!is_locked,
121 "Entropy source is locked. Cannot configure ENTROPY_SRC");
126 .route_to_firmware =
false,
129 .bypass_conditioner =
true,
131 .health_test_threshold_scope =
false,
132 .health_test_window_size = 512,
133 .alert_threshold = 0xFFFF,
143 LOG_INFO(
"Enabling and instantiating CSRNG...");
151 .seed_material = {0},
159 "csrng error status. err: 0x%x, kind: 0x%x",
status.cmd_sts,
162 LOG_INFO(
"CSRNG is now instantiated and enabled");
167 status_t enable_edns_boot_mode(
void) {
168 LOG_INFO(
"Enabling EDNs in boot-time request mode...");
176 status_t entropy_testutils_error_check_b4_proceeding(
void) {
177 LOG_INFO(
"Debugging error checks b4 proceeding...");
178 return entropy_testutils_error_check(&csrng, &edn0, &edn1);
181 status_t test_and_verify_aes_operation(
void) {
182 LOG_INFO(
"Triggering AES operation in ECB mode...");
192 .reseed_on_key_change =
false,
193 .ctrl_aux_lock =
false,
197 CHECK_STATUS_OK(aes_testutils_setup_encryption(transaction, &aes));
203 CHECK_STATUS_OK(aes_testutils_decrypt_ciphertext(transaction, &aes));
205 LOG_INFO(
"AES operation in ECB mode verified successfully");
210 LOG_INFO(
"Starting OTBN randomness test...");
213 otbn_randomness_test_start(&otbn, 1);
215 LOG_INFO(
"OTBN randomness test started");
220 LOG_INFO(
"Verifying OTBN program hang...");
223 const uint32_t kIterateMaxRetries = 1000000;
224 bool otbn_busy =
true;
225 uint32_t iter_cntr = kIterateMaxRetries;
229 while (iter_cntr > 0) {
234 otbn_busy = (otbn_status &
235 (kDifOtbnStatusBusyExecute | kDifOtbnStatusBusySecWipeDmem |
236 kDifOtbnStatusBusySecWipeImem)) != 0;
238 "OTBN program completed unexpectedly; expected it to hang");
245 LOG_INFO(
"OTBN program is still running as expected (hang detected)");
250 LOG_INFO(
"OTBN status: 0x%x", otbn_status);
251 LOG_INFO(
"OTBN error bits: 0x%x", otbn_err_bits);
255 if (otbn_err_bits != kDifOtbnErrBitsNoError) {
256 LOG_ERROR(
"OTBN encountered unexpected errors");
260 LOG_ERROR(
"A BAD_DATA_ADDR error was observed");
263 LOG_ERROR(
"A BAD_INSN_ADDR error was observed");
266 LOG_ERROR(
"A CALL_STACK error was observed");
269 LOG_ERROR(
"An ILLEGAL_INSN error was observed");
275 otbn_randomness_test_end(&otbn, 1);
280 LOG_ERROR(
"OTBN program did not hang as expected");
285 status_t set_threshold_and_enable_stringent_entropy_src_bypass_mode(
void) {
286 LOG_INFO(
"Enabling ENTROPY_SRC in bypass mode...");
291 TRY_CHECK(!is_locked,
292 "Entropy source is locked. Cannot configure ENTROPY_SRC");
297 .route_to_firmware =
false,
300 .bypass_conditioner =
true,
302 .health_test_threshold_scope =
false,
303 .health_test_window_size = 512,
304 .alert_threshold = 1,
313 status_t configure_stringent_health_tests(
void) {
314 LOG_INFO(
"Configuring stringent health test thresholds...");
319 TRY_CHECK(!is_locked,
320 "Entropy source is locked. Cannot configure ENTROPY_SRC");
357 status_t verify_recoverable_alert(
void) {
358 LOG_INFO(
"Verifying recoverable alerts...");
365 LOG_INFO(
"Recoverable alert detected. Alerts: 0x%x", alerts);
368 const uint32_t kEsMainSmAlertBit = 1 << 12;
369 const uint32_t kEsThreshCfgAlertBit = 1 << 14;
372 TRY_CHECK((alerts & kEsThreshCfgAlertBit) == 0,
373 "ES_THRESH_CFG_ALERT is asserted");
374 LOG_INFO(
"ES_THRESH_CFG_ALERT is not asserted and expected");
377 TRY_CHECK((alerts & kEsMainSmAlertBit) != 0,
378 "ES_MAIN_SM_ALERT is not asserted when expected");
379 LOG_INFO(
"ES_MAIN_SM_ALERT is asserted as expected");
387 TRY_CHECK(alerts == 0,
"Recoverable alerts not cleared. Alerts: 0x%x",
389 LOG_INFO(
"Recoverable alerts successfully cleared");
392 TRY(dif_entropy_src_irq_acknowledge(&entropy_src,
393 kDifEntropySrcIrqEsHealthTestFailed));
396 dif_entropy_src_irq_state_snapshot_t irq_state;
397 TRY(dif_entropy_src_irq_get_state(&entropy_src, &irq_state));
398 LOG_INFO(
"ENTROPY_SRC IRQ State after acknowledging: 0x%x", irq_state);
402 LOG_ERROR(
"No recoverable alerts detected when expected");
409 CHECK_STATUS_OK(init_test_environment());
412 CHECK_STATUS_OK(disable_entropy_complex());
416 CHECK_STATUS_OK(configure_loose_health_tests());
419 CHECK_STATUS_OK(enable_loose_entropy_src_bypass_mode());
422 CHECK_STATUS_OK(enable_csrng());
425 CHECK_STATUS_OK(enable_edns_boot_mode());
428 CHECK_STATUS_OK(entropy_testutils_error_check_b4_proceeding());
434 CHECK_STATUS_OK(test_and_verify_aes_operation());
438 CHECK_STATUS_OK(start_otbn_program());
441 CHECK_STATUS_OK(verify_otbn_hang());
444 CHECK_STATUS_OK(disable_entropy_complex());
448 CHECK_STATUS_OK(configure_stringent_health_tests());
453 CHECK_STATUS_OK(set_threshold_and_enable_stringent_entropy_src_bypass_mode());
454 CHECK_STATUS_OK(enable_csrng());
455 CHECK_STATUS_OK(enable_edns_boot_mode());
458 CHECK_STATUS_OK(entropy_testutils_error_check_b4_proceeding());
462 CHECK_STATUS_OK(verify_recoverable_alert());
464 LOG_INFO(
"Entropy source bypass mode health test completed");
470 LOG_INFO(
"Entering Entropy Source Bypass Mode Health Test");
472 return status_ok(execute_test());