5 #include "sw/device/lib/testing/keymgr_testutils.h"
15 #include "sw/device/lib/testing/entropy_testutils.h"
16 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
17 #include "sw/device/lib/testing/kmac_testutils.h"
18 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
19 #include "sw/device/lib/testing/rstmgr_testutils.h"
20 #include "sw/device/lib/testing/test_framework/check.h"
22 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
26 #define MODULE_ID MAKE_MODULE_ID('k', 'm', 't')
30 kFlashInfoPartitionId = 0,
36 kFlashInfoPageIdCreatorSecret = 1,
39 kFlashInfoPageIdOwnerSecret = 2,
42 const static char *kKeymgrStageNames[] = {
57 TRY(flash_ctrl_testutils_info_region_scrambled_setup(
58 flash, page_id, kFlashInfoBankId, kFlashInfoPartitionId, &address));
60 TRY(flash_ctrl_testutils_info_region_setup(
61 flash, page_id, kFlashInfoBankId, kFlashInfoPartitionId, &address));
64 TRY(flash_ctrl_testutils_erase_and_write_page(
65 flash, address, kFlashInfoPartitionId, data->value,
66 kDifFlashCtrlPartitionTypeInfo,
ARRAYSIZE(data->value)));
69 TRY(flash_ctrl_testutils_read(
70 flash, address, kFlashInfoPartitionId, readback_data.value,
71 kDifFlashCtrlPartitionTypeInfo,
ARRAYSIZE(readback_data.value), 0));
72 TRY_CHECK(
memcmp(data->value, readback_data.value,
sizeof(data->value)) == 0);
76 status_t keymgr_testutils_flash_init(
81 write_info_page(flash, kFlashInfoPageIdCreatorSecret, creator_secret,
83 write_info_page(flash, kFlashInfoPageIdOwnerSecret, owner_secret,
88 static status_t check_lock_otp_partition(
void) {
90 TRY(dif_otp_ctrl_init(
99 LOG_INFO(
"OTP partition locked. Digest: %x-%x", ((uint32_t *)&digest)[0],
100 ((uint32_t *)&digest)[1]);
108 static status_t dif_init(dif_keymgr_t *keymgr, dif_kmac_t *kmac) {
115 TRY(kmac_testutils_config(kmac,
true));
123 status_t keymgr_initialize_sim_dv(dif_keymgr_t *keymgr, dif_kmac_t *kmac) {
125 TRY(keymgr_testutils_startup(keymgr, kmac));
126 LOG_INFO(
"Keymgr entered CreatorRootKey State");
129 TRY(keymgr_testutils_generate_identity(
132 LOG_INFO(
"Keymgr generated identity at CreatorRootKey State");
136 TRY(keymgr_testutils_advance_state(keymgr, &kOwnerIntParams));
137 TRY(keymgr_testutils_check_state(keymgr,
139 LOG_INFO(
"Keymgr entered OwnerIntKey State");
143 status_t keymgr_initialize_sival(dif_keymgr_t *keymgr, dif_kmac_t *kmac) {
145 TRY(keymgr_testutils_try_startup(keymgr, kmac, &keymgr_state));
148 TRY(keymgr_testutils_advance_state(keymgr, &kOwnerIntParams));
153 TRY(keymgr_testutils_advance_state(keymgr, &kOwnerRootKeyParams));
159 status_t keymgr_testutils_initialize(dif_keymgr_t *keymgr, dif_kmac_t *kmac) {
161 return keymgr_initialize_sival(keymgr, kmac);
164 return keymgr_initialize_sim_dv(keymgr, kmac);
167 status_t keymgr_testutils_try_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac,
169 TRY(dif_init(keymgr, kmac));
177 LOG_INFO(
"Unexpected keymgr state: 0x%x", keymgr_state);
182 TRY(keymgr_testutils_startup(keymgr, kmac));
189 status_t keymgr_testutils_init_nvm_then_reset(
void) {
196 rstmgr_testutils_reason_get();
200 LOG_INFO(
"Powered up for the first time, program flash");
205 TRY(keymgr_testutils_flash_init(&flash, &kCreatorSecret, &kOwnerSecret));
207 TRY(check_lock_otp_partition());
210 LOG_INFO(
"Requesting a reset to make OTP partitions accessible to keymgr");
211 rstmgr_testutils_reason_clear();
227 status_t keymgr_testutils_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac) {
232 bool is_using_test_rom =
238 TRY(keymgr_testutils_init_nvm_then_reset());
247 LOG_INFO(
"Initializing entropy complex in Auto mode");
249 TRY(entropy_testutils_auto_mode_init());
251 LOG_INFO(
"Powered up for the second time, actuate keymgr and perform test.");
253 TRY(dif_init(keymgr, kmac));
257 TRY(keymgr_testutils_advance_state(keymgr, NULL));
259 LOG_INFO(
"Keymgr entered Init State");
262 if (is_using_test_rom) {
263 LOG_INFO(
"Using test_rom, setting inputs and advancing state...");
264 TRY(keymgr_testutils_advance_state(keymgr, &kCreatorParams));
266 LOG_INFO(
"Using rom, only advancing state...");
268 TRY(keymgr_testutils_wait_for_operation_done(keymgr));
271 LOG_INFO(
"Keymgr entered CreatorRootKey State");
276 TRY(keymgr_testutils_generate_identity(
279 LOG_INFO(
"Keymgr generated identity at CreatorRootKey State");
284 status_t keymgr_testutils_advance_state(
287 return keymgr_testutils_wait_for_operation_done(keymgr);
290 status_t keymgr_testutils_check_state(
const dif_keymgr_t *keymgr,
294 TRY_CHECK(act_state == exp_state,
295 "Keymgr in unexpected state: %x, expected to be %x", act_state,
300 status_t keymgr_testutils_generate_identity(
301 const dif_keymgr_t *keymgr,
304 return keymgr_testutils_wait_for_operation_done(keymgr);
307 status_t keymgr_testutils_generate_versioned_key(
308 const dif_keymgr_t *keymgr,
311 return keymgr_testutils_wait_for_operation_done(keymgr);
314 status_t keymgr_testutils_disable(
const dif_keymgr_t *keymgr) {
316 return keymgr_testutils_wait_for_operation_done(keymgr);
319 status_t keymgr_testutils_wait_for_operation_done(
const dif_keymgr_t *keymgr) {
329 status_t keymgr_testutils_max_key_version_get(
const dif_keymgr_t *keymgr,
330 uint32_t *max_key_version) {
337 LOG_INFO(
"Unexpected keymgr state: 0x%x", keymgr_state);
344 switch (keymgr_state) {
361 status_t keymgr_testutils_state_string_get(
const dif_keymgr_t *keymgr,
362 const char **stage_name) {
366 if (state >=
ARRAYSIZE(kKeymgrStageNames)) {
371 *stage_name = kKeymgrStageNames[state];