9 #include "sw/device/lib/testing/entropy_testutils.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "kmac_regs.h"
15 static_assert(kDtKmacCount >= 1,
16 "This test requires at least one KMAC instance");
18 static dt_kmac_t kTestKmac = (dt_kmac_t)0;
20 OTTF_DEFINE_TEST_CONFIG();
29 bool timeout_expected;
39 .timeout_expected =
true,
44 .timeout_expected =
true,
49 .timeout_expected =
false,
54 .timeout_expected =
false,
59 kKmacTimeoutTestCount =
ARRAYSIZE(kTestTimeoutVals),
61 kKmacDigestLenMax = 100,
62 kKmacEntropyHashThresholdStall = 2,
63 kNumIterationsStall = kKmacEntropyHashThresholdStall,
76 const char *customization_string;
77 size_t customization_string_len;
79 const uint32_t digest[kKmacDigestLenMax];
81 bool digest_len_is_fixed;
92 .share0 = {0x43424140, 0x47464544, 0x4b4a4948, 0x4f4e4f4c,
93 0x53525150, 0x57565554, 0x5b5a5958, 0x5f5e5d5c},
98 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
99 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
100 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
101 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
102 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
103 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
104 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
105 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
106 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
107 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
108 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
109 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
110 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
112 .customization_string =
"My Tagged Application",
113 .customization_string_len = 21,
114 .digest = {0x1c73bed5, 0x73d74e95, 0x59bb4628, 0xe3a8e3db, 0x7ae7830f,
115 0x5944ff4b, 0xb4c2f1f2, 0xceb8ebec, 0xc601ba67, 0x57b88a2e,
116 0x9b492d8d, 0x6727bbd1, 0x90117868, 0x6a300a02, 0x1d28de97,
119 .digest_len_is_fixed =
false,
123 status_t disable_entropy_complex(
void) {
126 TRY(entropy_testutils_stop_all());
127 LOG_INFO(
"The entire entropy complex is stopped");
132 status_t enable_entropy_complex(
void) {
135 TRY(entropy_testutils_auto_mode_init());
136 LOG_INFO(
"The entire entropy complex is enabled");
140 status_t test_kmac_entropy_stall(
void) {
141 LOG_INFO(
"Running KMAC ENTROPY STALL test...");
145 CHECK_DIF_OK(dif_kmac_init_from_dt(kTestKmac, &kmac));
150 .entropy_fast_process =
false,
151 .entropy_hash_threshold = kKmacEntropyHashThresholdStall,
152 .message_big_endian =
false,
153 .output_big_endian =
false,
156 .entropy_prescaler = 0x3ff,
157 .entropy_wait_timer = 0xffff,
163 kKmacTestVector.customization_string,
164 kKmacTestVector.customization_string_len, &str_buffer));
168 kKmacTestVector.customization_string_len == 0 ? NULL : &str_buffer;
170 size_t test_digest_len =
171 kKmacTestVector.digest_len_is_fixed ? kKmacTestVector.digest_len : 0;
175 for (
size_t i = 0; i < kNumIterationsStall; i++) {
178 if (i == kKmacEntropyHashThresholdStall - 1) {
179 LOG_INFO(
"Disabling entropy complex to trigger a stall");
180 TRY(disable_entropy_complex());
187 kKmacTestVector.mode, test_digest_len,
188 &kKmacTestVector.key, str_buffer_ptr));
190 TRY(
dif_kmac_absorb(&kmac, &kmac_operation_state, kKmacTestVector.message,
191 kKmacTestVector.message_len, NULL));
192 CHECK(kKmacDigestLenMax >= kKmacTestVector.digest_len);
194 uint32_t out[kKmacDigestLenMax];
195 if (i == kKmacEntropyHashThresholdStall - 1) {
199 KMAC_CMD_CMD_VALUE_PROCESS);
200 mmio_region_write32(kmac.base_addr, KMAC_CMD_REG_OFFSET, cmd_reg);
206 "KMAC not stalling");
207 LOG_INFO(
"KMAC stalled as expected after disabling entropy complex");
209 TRY(enable_entropy_complex());
212 "KMAC not in squeeze state");
213 LOG_INFO(
"KMAC is is squeeze state after enabling entropy complex");
216 kKmacTestVector.digest_len,
220 CHECK_DIF_OK(
dif_kmac_end(&kmac, &kmac_operation_state));
227 LOG_INFO(
"Running KMAC ENTROPY test...");
231 CHECK_DIF_OK(dif_kmac_init_from_dt(kTestKmac, &kmac));
236 .entropy_fast_process =
false,
238 .entropy_hash_threshold = kKmacTimeoutTestCount,
239 .message_big_endian =
false,
240 .output_big_endian =
false,
248 kKmacTestVector.customization_string,
249 kKmacTestVector.customization_string_len, &str_buffer));
253 kKmacTestVector.customization_string_len == 0 ? NULL : &str_buffer;
255 size_t test_digest_len =
256 kKmacTestVector.digest_len_is_fixed ? kKmacTestVector.digest_len : 0;
258 for (
size_t i = 0; i < kKmacTimeoutTestCount; ++i) {
270 &kmac, &kmac_operation_state, kKmacTestVector.mode, test_digest_len,
271 &kKmacTestVector.key, str_buffer_ptr));
274 kKmacTestVector.message,
275 kKmacTestVector.message_len, NULL));
277 CHECK(kKmacDigestLenMax >= kKmacTestVector.digest_len);
280 uint32_t out[kKmacDigestLenMax];
282 kKmacTestVector.digest_len,
289 bool irq_err_pending;
291 dif_kmac_irq_is_pending(&kmac, kDifKmacIrqKmacErr, &irq_err_pending));
292 if (irq_err_pending) {
297 "Error other than EDN timeout occurred.");
300 LOG_INFO(
"EDN seed received before timeout.");
304 CHECK(irq_err_pending == timeout_test.timeout_expected,
305 "EDN timeout expectation doesn't match observation.");
308 CHECK_DIF_OK(
dif_kmac_end(&kmac, &kmac_operation_state));
311 if (irq_err_pending) {
313 CHECK_DIF_OK(dif_kmac_irq_acknowledge_all(&kmac));
319 dif_kmac_irq_state_snapshot_t intr_snapshot;
320 CHECK_DIF_OK(dif_kmac_irq_get_state(&kmac, &intr_snapshot));
321 CHECK(intr_snapshot == 0,
322 "INTR_STATE is non-zero after timeout clean-up.");
326 CHECK(!is_kmac_locked,
"KMAC still locked after timeout clean-up.");
332 CHECK(hash_ctr == (i + 1) % (kKmacTimeoutTestCount),
333 "Hash counter increment is wrong.");
334 LOG_INFO(
"hash_ctr = %d", hash_ctr);
346 return status_ok(result);