15 #include "sw/device/lib/testing/keymgr_testutils.h"
16 #include "sw/device/lib/testing/kmac_testutils.h"
17 #include "sw/device/lib/testing/test_framework/check.h"
21 #include "keymgr_regs.h"
22 #include "kmac_regs.h"
32 .share0 = {0x43424140, 0x47464544, 0x4B4A4948, 0x4F4E4D4C, 0x53525150,
33 0x57565554, 0x5B5A5958, 0x5F5E5D5C},
38 static const size_t kKmacOutputLen = 8;
39 static const uint32_t kSoftwareKeyExpectedOutput[8] = {
40 0x0D0B78E5, 0xD3F7A63E, 0x70C529A4, 0x003AA46A,
41 0xD4D7DBFA, 0x9E832896, 0x3F248731, 0x4EE16E45};
42 static const char *kCustomString = NULL;
43 static const size_t kCustomStringLen = 0;
44 static const char kKmacMessage[] =
"\x00\x01\x02\x03";
45 static const size_t kKmacMessageLen = 4;
47 static dif_keymgr_t keymgr;
48 static dif_kmac_t kmac;
51 static volatile const uint8_t sideload_digest_result[32] = {0};
53 OTTF_DEFINE_TEST_CONFIG();
58 static void init_peripheral_handles(
void) {
61 CHECK_DIF_OK(dif_keymgr_init(
85 size_t *capacity_total_len) {
89 capacity_len = 256 / 8 /
sizeof(uint32_t);
91 capacity_len = 512 / 8 /
sizeof(uint32_t);
93 return INVALID_ARGUMENT();
96 *capacity_total_len =
ceil_div(digest_len, rate_len) * capacity_len;
106 static void test_kmac_with_sideloaded_key(dif_keymgr_t *keymgr,
109 CHECK_STATUS_OK(kmac_testutils_config(kmac,
false));
113 size_t capacity_total_len;
115 get_total_capacity_len(kKmacMode, kKmacOutputLen, &capacity_total_len));
116 uint32_t capacity[capacity_total_len];
117 uint32_t zero_array[capacity_total_len];
118 memset(zero_array, 0,
sizeof(zero_array));
121 memset(capacity, 0,
sizeof(capacity));
123 uint32_t output[kKmacOutputLen];
124 CHECK_STATUS_OK(kmac_testutils_kmac(
125 kmac, kKmacMode, &kSoftwareKey, kCustomString, kCustomStringLen,
126 kKmacMessage, kKmacMessageLen, kKmacOutputLen, output, capacity));
127 LOG_INFO(
"Computed KMAC output for software key.");
130 CHECK_ARRAYS_EQ(output, kSoftwareKeyExpectedOutput, kKmacOutputLen);
133 CHECK_ARRAYS_NE(capacity, zero_array,
ARRAYSIZE(capacity));
136 CHECK_STATUS_OK(kmac_testutils_config(kmac,
true));
143 uint32_t max_key_version;
145 keymgr_testutils_max_key_version_get(keymgr, &max_key_version));
147 if (sideload_params.
version > max_key_version) {
148 LOG_INFO(
"Key version %d is greater than the maximum key version %d",
149 sideload_params.
version, max_key_version);
150 LOG_INFO(
"Setting key version to the maximum key version %d",
152 sideload_params.
version = max_key_version;
156 keymgr_testutils_generate_versioned_key(keymgr, sideload_params));
157 LOG_INFO(
"Keymgr generated HW output for Kmac at OwnerIntKey State");
159 uint32_t output_sideload_good0[kKmacOutputLen];
161 kmac_testutils_kmac(kmac, kKmacMode, &kSoftwareKey, kCustomString,
162 kCustomStringLen, kKmacMessage, kKmacMessageLen,
163 kKmacOutputLen, output_sideload_good0, capacity));
164 LOG_INFO(
"Computed KMAC output for sideloaded key.");
167 CHECK_ARRAYS_EQ(capacity, zero_array,
ARRAYSIZE(capacity));
174 CHECK_ARRAYS_EQ(output_sideload_good0, (uint32_t *)sideload_digest_result,
178 LOG_INFO(
"Clearing the sideloaded key.");
191 uint32_t output_sideload_bad[kKmacOutputLen];
194 for (
size_t i = 0; i < kKmacOutputLen; i++) {
195 output_sideload_bad[i] = i;
197 CHECK(kFailedPrecondition ==
198 status_err(kmac_testutils_kmac(
199 kmac, kKmacMode, &kSoftwareKey, kCustomString, kCustomStringLen,
200 kKmacMessage, kKmacMessageLen, kKmacOutputLen, output_sideload_bad,
202 LOG_INFO(
"Ran KMAC with an invalid sideload key and checked that it fails.");
205 for (
size_t i = 0; i < kKmacOutputLen; i++) {
206 CHECK(output_sideload_bad[i] == i);
212 keymgr_testutils_generate_versioned_key(keymgr, sideload_params));
213 LOG_INFO(
"Keymgr regenerated HW output for Kmac at OwnerIntKey State");
215 uint32_t output_sideload_good1[kKmacOutputLen];
216 CHECK_STATUS_OK(kmac_testutils_kmac(
217 kmac, kKmacMode, &kSoftwareKey, kCustomString, kCustomStringLen,
218 kKmacMessage, kKmacMessageLen, kKmacOutputLen, output_sideload_good1,
220 LOG_INFO(
"Re-computed KMAC output for sideloaded key.");
222 CHECK_ARRAYS_EQ(output_sideload_good1, output_sideload_good0, kKmacOutputLen);
226 init_peripheral_handles();
228 CHECK_STATUS_OK(keymgr_testutils_initialize(&keymgr, &kmac));
231 test_kmac_with_sideloaded_key(&keymgr, &kmac);