15 #include "sw/device/lib/testing/csrng_testutils.h"
16 #include "sw/device/lib/testing/entropy_src_testutils.h"
17 #include "sw/device/lib/testing/entropy_testutils.h"
18 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
19 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
20 #include "sw/device/lib/testing/rand_testutils.h"
21 #include "sw/device/lib/testing/rstmgr_testutils.h"
22 #include "sw/device/lib/testing/test_framework/check.h"
27 OTTF_DEFINE_TEST_CONFIG();
34 kEntropyFifoBufferSize = 12,
39 kTestParamEntropySrcMaxAttempts = 256,
43 kExitTokenSizeInBytes = 16,
44 kExitTokenSizeInWords = kExitTokenSizeInBytes /
sizeof(uint32_t),
49 uint32_t nv_csrng_output[kEntropyFifoBufferSize];)
51 static dif_lc_ctrl_t lc_ctrl;
52 static dif_otp_ctrl_t otp_ctrl;
53 static dif_csrng_t csrng;
54 static dif_entropy_src_t entropy_src;
56 static dif_kmac_t kmac;
57 static dif_rstmgr_t rstmgr;
81 static_assert(kExitTokenSizeInBytes ==
ARRAYSIZE(kLcExitToken.data),
82 "Invalid exit token size.");
88 static void kmac_init(
void) {
95 .entropy_seed = {0xb153e3fe, 0x09596819, 0x3e85a6e8, 0xb6dcdaba,
96 0x50dc409c, 0x11e1ebd1},
105 static void peripherals_init(
void) {
106 CHECK_DIF_OK(dif_csrng_init(
108 CHECK_DIF_OK(dif_lc_ctrl_init(
110 CHECK_DIF_OK(dif_entropy_src_init(
112 CHECK_DIF_OK(dif_otp_ctrl_init(
114 CHECK_DIF_OK(dif_rstmgr_init(
131 static void exit_token_cshake_hash(uint64_t *otp_token_l,
132 uint64_t *otp_token_h) {
144 uint32_t token_hash[kExitTokenSizeInWords];
146 kExitTokenSizeInWords, NULL,
152 uint8_t *p_token = (uint8_t *)token_hash;
153 for (
size_t i = 0; i < kExitTokenSizeInBytes; i++) {
154 if (i < kExitTokenSizeInBytes / 2) {
155 *otp_token_l |= (uint64_t)p_token[i] << (i * 8);
157 *otp_token_h |= (uint64_t)p_token[i]
158 << ((i - kExitTokenSizeInBytes / 2) * 8);
166 static void lock_otp_secret0_partition(
void) {
167 uint64_t otp_token_l;
168 uint64_t opt_token_h;
169 exit_token_cshake_hash(&otp_token_l, &opt_token_h);
175 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp_ctrl));
180 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp_ctrl));
184 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp_ctrl));
191 uint32_t index = rand_testutils_gen32_range(0, 2);
204 CHECK(
false,
"Unexpected case index: %d", index);
215 static void entropy_conditioner_stop(
const dif_entropy_src_t *entropy_src) {
216 uint32_t fail_count = 0;
221 CHECK(fail_count++ < kTestParamEntropySrcMaxAttempts);
223 CHECK_DIF_OK(op_result);
233 static void fw_override_conditioner_write(
234 const dif_entropy_src_t *entropy_src) {
237 const uint32_t kInputMsg[kEntropyFifoBufferSize] = {
238 0xa52a0da9, 0xcae141b2, 0x6d5bab9d, 0x2c3e5cc0, 0x225afc93, 0x5d31a610,
239 0x91b7f960, 0x0d566bb3, 0xef35e170, 0x94ba7d8e, 0x534eb741, 0x6b60b0da,
242 uint32_t fail_count = 0;
247 entropy_src, kInputMsg + total,
ARRAYSIZE(kInputMsg) - total, &count);
250 CHECK(fail_count++ < kTestParamEntropySrcMaxAttempts);
253 CHECK_DIF_OK(op_result);
256 entropy_conditioner_stop(entropy_src);
266 static void csrng_static_generate_run(uint32_t *output,
size_t output_len) {
267 CHECK_STATUS_OK(entropy_testutils_stop_all());
270 CHECK_STATUS_OK(entropy_src_testutils_fw_override_enable(
271 &entropy_src, kEntropyFifoBufferSize,
275 fw_override_conditioner_write(&entropy_src);
279 &kEmptySeedMaterial));
281 CHECK_STATUS_OK(csrng_testutils_cmd_generate_run(&csrng, output, output_len));
282 uint32_t prev_word = 0;
283 for (
size_t i = 0; i < output_len; ++i) {
284 CHECK(prev_word != output[i],
285 "Unexpected duplicate value at index: %d value: 0x%x", i, prev_word);
286 prev_word = output[i];
293 flash_ctrl_testutils_default_region_access(&flash_ctrl_state,
302 rstmgr_testutils_reason_clear();
315 uint32_t expected[kEntropyFifoBufferSize];
316 csrng_static_generate_run(expected,
ARRAYSIZE(expected));
317 CHECK_STATUS_OK(flash_ctrl_testutils_write(&flash_ctrl_state, address,
319 kDifFlashCtrlPartitionTypeData,
321 CHECK_ARRAYS_EQ(nv_csrng_output, expected,
ARRAYSIZE(expected));
323 lock_otp_secret0_partition();
328 CHECK(
false,
"Unexpected wakeup.");
331 uint32_t got[kEntropyFifoBufferSize];
332 csrng_static_generate_run(got,
ARRAYSIZE(got));
334 "Array size mismatch.");
335 CHECK_ARRAYS_EQ(got, nv_csrng_output,
ARRAYSIZE(got));
339 false, &kLcExitToken),
340 "LC transition configuration failed!");
345 LOG_INFO(
"LC transition in progress.");
347 CHECK(
false,
"Unexpected wakeup.");
351 "Unexpected LC state: %d", lc_state);
352 uint32_t got[kEntropyFifoBufferSize];
353 csrng_static_generate_run(got,
ARRAYSIZE(got));
355 "Array size mismatch.");
358 CHECK_ARRAYS_EQ(got, nv_csrng_output,
ARRAYSIZE(got));
360 CHECK_ARRAYS_NE(got, nv_csrng_output,
ARRAYSIZE(got));
364 CHECK(
false,
"Invalid reset: %d, or LC state: %d", rst_info, lc_state);