9 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "kmac_regs.h"
15 OTTF_DEFINE_TEST_CONFIG();
17 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
28 .entropy_fast_process =
false,
29 .entropy_hash_threshold = 8,
30 .entropy_wait_timer = 0,
31 .entropy_prescaler = 0,
36 .entropy_mode = kDifKmacEntropyModeEdn,
37 .entropy_fast_process =
false,
38 .entropy_hash_threshold = 8,
39 .entropy_wait_timer = 0xffff,
40 .entropy_prescaler = 0x3ff,
45 .entropy_mode = kDifKmacEntropyModeEdn,
46 .entropy_fast_process =
false,
47 .entropy_hash_threshold = 0,
48 .entropy_wait_timer = 0,
49 .entropy_prescaler = 1,
54 .entropy_mode = kDifKmacEntropyModeEdn,
55 .entropy_fast_process =
false,
56 .entropy_hash_threshold = 1023,
57 .entropy_wait_timer = 0,
58 .entropy_prescaler = 1,
68 .entropy_mode = kDifKmacEntropyModeSoftware,
69 .entropy_fast_process =
false,
78 .entropy_hash_threshold = 8,
79 .entropy_wait_timer = 0xffff,
80 .entropy_prescaler = 0x3ff,
85 .entropy_mode = kDifKmacEntropyModeSoftware,
86 .entropy_fast_process =
false,
95 .entropy_hash_threshold = 0,
96 .entropy_wait_timer = 0xffff,
97 .entropy_prescaler = 0x3ff,
102 kKmacDigestLenMax = 100,
103 kTestConfigCount =
ARRAYSIZE(kKmacTestCases),
104 kKmacIterations = 1000,
119 const char *customization_string;
120 size_t customization_string_len;
122 const uint32_t digest[kKmacDigestLenMax];
134 .share0 = {0x43424140, 0x47464544, 0x4b4a4948, 0x4f4e4f4c,
135 0x53525150, 0x57565554, 0x5b5a5958, 0x5f5e5d5c},
140 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
141 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
142 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
143 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
144 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
145 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
146 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
147 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
148 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
149 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
150 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
151 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
152 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
154 .customization_string =
"My Tagged Application",
155 .customization_string_len = 21,
156 .digest = {0x1c73bed5, 0x73d74e95, 0x59bb4628, 0xe3a8e3db, 0x7ae7830f,
157 0x5944ff4b, 0xb4c2f1f2, 0xceb8ebec, 0xc601ba67, 0x57b88a2e,
158 0x9b492d8d, 0x6727bbd1, 0x90117868, 0x6a300a02, 0x1d28de97,
174 "Invalid test case: entropy_hash_threshold too high; greater than %d",
183 kKmacTestVector.customization_string,
184 kKmacTestVector.customization_string_len, &cust_str));
194 uint32_t hash_count, expected_hash_count = 0;
195 uint32_t cmd_reg = mmio_region_read32(kmac.base_addr, KMAC_CMD_REG_OFFSET);
197 mmio_region_write32(kmac.base_addr, KMAC_CMD_REG_OFFSET, cmd_reg);
199 TRY_CHECK(hash_count == 0,
"Failed to clear hash counter");
202 for (
size_t i = 0; i < kKmacIterations; i++) {
207 kKmacTestVector.digest_len,
208 &kKmacTestVector.key, &cust_str));
211 kKmacTestVector.message_len,
215 uint32_t out[kKmacDigestLenMax];
220 bool irq_err_pending;
221 TRY(dif_kmac_irq_is_pending(&kmac, kDifKmacIrqKmacErr, &irq_err_pending));
224 if (irq_err_pending) {
228 TRY_CHECK(!irq_err_pending,
"KMAC error occurred: 0x%x", err_status);
234 expected_hash_count =
239 expected_hash_count = MIN(expected_hash_count + 1, kHashCntMax);
241 TRY_CHECK(expected_hash_count == hash_count,
242 "Unexpected hash counter value: Got %d, Expected %d", hash_count,
243 expected_hash_count);
251 status_t test_kmac_entropy_stress(
void) {
252 LOG_INFO(
"Running KMAC entropy stress test...");
254 for (
size_t i = 0; i < kTestConfigCount; i++) {
255 LOG_INFO(
"Running test case %d", i);
256 test_kmac_config(&kKmacTestCases[i]);
266 return status_ok(result);