Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | #include "sw/device/lib/testing/keymgr_testutils.h" |
| 6 | |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 7 | #include "sw/device/lib/dif/dif_flash_ctrl.h" |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 8 | #include "sw/device/lib/dif/dif_keymgr.h" |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 9 | #include "sw/device/lib/dif/dif_kmac.h" |
| 10 | #include "sw/device/lib/dif/dif_otp_ctrl.h" |
| 11 | #include "sw/device/lib/dif/dif_rstmgr.h" |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 12 | #include "sw/device/lib/runtime/ibex.h" |
| 13 | #include "sw/device/lib/runtime/log.h" |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 14 | #include "sw/device/lib/testing/flash_ctrl_testutils.h" |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 15 | #include "sw/device/lib/testing/kmac_testutils.h" |
| 16 | #include "sw/device/lib/testing/otp_ctrl_testutils.h" |
| 17 | #include "sw/device/lib/testing/rstmgr_testutils.h" |
Miguel Young de la Sota | 09d4d01 | 2022-04-19 11:05:36 -0400 | [diff] [blame] | 18 | #include "sw/device/lib/testing/test_framework/check.h" |
Alphan Ulusoy | 3352a27 | 2022-09-27 09:10:38 -0400 | [diff] [blame] | 19 | #include "sw/device/silicon_creator/lib/base/chip.h" |
| 20 | #include "sw/device/silicon_creator/lib/drivers/retention_sram.h" |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 21 | |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 22 | #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h" |
| 23 | |
| 24 | enum { |
| 25 | /** Flash Secret partition ID. */ |
| 26 | kFlashInfoPartitionId = 0, |
| 27 | |
| 28 | /** Secret partition flash bank ID. */ |
| 29 | kFlashInfoBankId = 0, |
| 30 | |
| 31 | /** Creator Secret flash info page ID. */ |
| 32 | kFlashInfoPageIdCreatorSecret = 1, |
| 33 | |
| 34 | /** Owner Secret flash info page ID. */ |
| 35 | kFlashInfoPageIdOwnerSecret = 2, |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static void write_info_page(dif_flash_ctrl_state_t *flash, uint32_t page_id, |
Timothy Chen | da28430 | 2022-10-26 22:17:54 -0700 | [diff] [blame] | 39 | const keymgr_testutils_secret_t *data, |
| 40 | bool scramble) { |
| 41 | uint32_t address; |
| 42 | if (scramble) { |
| 43 | address = flash_ctrl_testutils_info_region_scrambled_setup( |
| 44 | flash, page_id, kFlashInfoBankId, kFlashInfoPartitionId); |
| 45 | } else { |
| 46 | address = flash_ctrl_testutils_info_region_setup( |
| 47 | flash, page_id, kFlashInfoBankId, kFlashInfoPartitionId); |
| 48 | } |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 49 | |
| 50 | CHECK(flash_ctrl_testutils_erase_and_write_page( |
Michael Schaffner | 29e262a | 2022-10-10 21:28:44 -0700 | [diff] [blame] | 51 | flash, address, kFlashInfoPartitionId, data->value, |
| 52 | kDifFlashCtrlPartitionTypeInfo, ARRAYSIZE(data->value))); |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 53 | |
Michael Schaffner | 29e262a | 2022-10-10 21:28:44 -0700 | [diff] [blame] | 54 | keymgr_testutils_secret_t readback_data; |
| 55 | CHECK(flash_ctrl_testutils_read( |
| 56 | flash, address, kFlashInfoPartitionId, readback_data.value, |
| 57 | kDifFlashCtrlPartitionTypeInfo, ARRAYSIZE(readback_data.value), 0)); |
| 58 | CHECK_ARRAYS_EQ(data->value, readback_data.value, ARRAYSIZE(data->value)); |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Michael Schaffner | 29e262a | 2022-10-10 21:28:44 -0700 | [diff] [blame] | 61 | void keymgr_testutils_flash_init( |
| 62 | dif_flash_ctrl_state_t *flash, |
| 63 | const keymgr_testutils_secret_t *creator_secret, |
| 64 | const keymgr_testutils_secret_t *owner_secret) { |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 65 | // Initialize flash secrets. |
Timothy Chen | da28430 | 2022-10-26 22:17:54 -0700 | [diff] [blame] | 66 | write_info_page(flash, kFlashInfoPageIdCreatorSecret, creator_secret, |
| 67 | /*scramble=*/true); |
| 68 | write_info_page(flash, kFlashInfoPageIdOwnerSecret, owner_secret, |
| 69 | /*scramble=*/true); |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 72 | void keymgr_testutils_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac) { |
Michael Schaffner | 29e262a | 2022-10-10 21:28:44 -0700 | [diff] [blame] | 73 | dif_flash_ctrl_state_t flash; |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 74 | dif_rstmgr_t rstmgr; |
| 75 | dif_rstmgr_reset_info_bitfield_t info; |
| 76 | |
| 77 | CHECK_DIF_OK(dif_rstmgr_init( |
| 78 | mmio_region_from_addr(TOP_EARLGREY_RSTMGR_AON_BASE_ADDR), &rstmgr)); |
| 79 | info = rstmgr_testutils_reason_get(); |
| 80 | |
Alphan Ulusoy | 3352a27 | 2022-09-27 09:10:38 -0400 | [diff] [blame] | 81 | // Check the last word of the retention SRAM creator area to determine the |
| 82 | // type of the ROM. |
| 83 | bool is_using_test_rom = |
| 84 | retention_sram_get() |
| 85 | ->reserved_creator[ARRAYSIZE((retention_sram_t){0}.reserved_creator) - |
| 86 | 1] == TEST_ROM_IDENTIFIER; |
| 87 | |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 88 | // POR reset. |
| 89 | if (info == kDifRstmgrResetInfoPor) { |
| 90 | LOG_INFO("Powered up for the first time, program flash"); |
| 91 | |
Michael Schaffner | 29e262a | 2022-10-10 21:28:44 -0700 | [diff] [blame] | 92 | CHECK_DIF_OK(dif_flash_ctrl_init_state( |
| 93 | &flash, mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR))); |
| 94 | |
| 95 | keymgr_testutils_flash_init(&flash, &kCreatorSecret, &kOwnerSecret); |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 96 | |
| 97 | // Lock otp secret partition. |
| 98 | dif_otp_ctrl_t otp; |
| 99 | CHECK_DIF_OK(dif_otp_ctrl_init( |
| 100 | mmio_region_from_addr(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR), &otp)); |
| 101 | otp_ctrl_testutils_lock_partition(&otp, kDifOtpCtrlPartitionSecret2, 0); |
| 102 | |
| 103 | // Reboot device. |
| 104 | rstmgr_testutils_reason_clear(); |
| 105 | CHECK_DIF_OK(dif_rstmgr_software_device_reset(&rstmgr)); |
| 106 | |
| 107 | // Wait here until device reset. |
| 108 | wait_for_interrupt(); |
| 109 | |
| 110 | } else { |
Alphan Ulusoy | cdabaf7 | 2022-09-14 20:13:28 -0400 | [diff] [blame] | 111 | CHECK(info == kDifRstmgrResetInfoSw, "Unexpected reset reason: %08x", info); |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 112 | LOG_INFO( |
| 113 | "Powered up for the second time, actuate keymgr and perform test."); |
| 114 | |
| 115 | // Initialize KMAC in preparation for keymgr use. |
| 116 | CHECK_DIF_OK(dif_kmac_init( |
| 117 | mmio_region_from_addr(TOP_EARLGREY_KMAC_BASE_ADDR), kmac)); |
| 118 | |
| 119 | // We shouldn't use the KMAC block's default entropy setting for keymgr, so |
| 120 | // configure it to use software entropy (and a sideloaded key, although it |
| 121 | // shouldn't matter here and tests should reconfigure if needed). |
| 122 | kmac_testutils_config(kmac, true); |
| 123 | |
| 124 | // Initialize keymgr context. |
| 125 | CHECK_DIF_OK(dif_keymgr_init( |
| 126 | mmio_region_from_addr(TOP_EARLGREY_KEYMGR_BASE_ADDR), keymgr)); |
| 127 | |
| 128 | // Advance to Initialized state. |
| 129 | keymgr_testutils_check_state(keymgr, kDifKeymgrStateReset); |
| 130 | keymgr_testutils_advance_state(keymgr, NULL); |
| 131 | keymgr_testutils_check_state(keymgr, kDifKeymgrStateInitialized); |
| 132 | LOG_INFO("Keymgr entered Init State"); |
| 133 | |
| 134 | // Advance to CreatorRootKey state. |
Alphan Ulusoy | 3352a27 | 2022-09-27 09:10:38 -0400 | [diff] [blame] | 135 | if (is_using_test_rom) { |
| 136 | LOG_INFO("Using test_rom, setting inputs and advancing state..."); |
| 137 | keymgr_testutils_advance_state(keymgr, &kCreatorParams); |
| 138 | } else { |
| 139 | LOG_INFO("Using rom, only advancing state..."); |
| 140 | CHECK_DIF_OK(dif_keymgr_advance_state_raw(keymgr)); |
| 141 | keymgr_testutils_wait_for_operation_done(keymgr); |
| 142 | } |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 143 | keymgr_testutils_check_state(keymgr, kDifKeymgrStateCreatorRootKey); |
| 144 | LOG_INFO("Keymgr entered CreatorRootKey State"); |
Fatih Balli | f3f2a41 | 2022-11-03 07:56:19 -0700 | [diff] [blame] | 145 | |
| 146 | // Identity generation is not really necessary for all tests, but it is |
| 147 | // added to make sure each test using this function is also compatible with |
| 148 | // the DV_WAIT sequences from keymgr_key_derivation vseq |
| 149 | keymgr_testutils_generate_identity(keymgr); |
| 150 | LOG_INFO("Keymgr generated identity at CreatorRootKey State"); |
Jade Philipoom | 8f8d0dd | 2022-08-05 10:12:29 +0100 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 154 | void keymgr_testutils_advance_state(const dif_keymgr_t *keymgr, |
| 155 | const dif_keymgr_state_params_t *params) { |
| 156 | CHECK_DIF_OK(dif_keymgr_advance_state(keymgr, params)); |
Weicai Yang | bc6e0b9 | 2022-04-07 23:07:20 -0700 | [diff] [blame] | 157 | keymgr_testutils_wait_for_operation_done(keymgr); |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void keymgr_testutils_check_state(const dif_keymgr_t *keymgr, |
| 161 | const dif_keymgr_state_t exp_state) { |
| 162 | dif_keymgr_state_t act_state; |
| 163 | CHECK_DIF_OK(dif_keymgr_get_state(keymgr, &act_state)); |
Douglas Reis | be73450 | 2022-07-05 11:35:22 +0100 | [diff] [blame] | 164 | CHECK(act_state == exp_state, |
| 165 | "Keymgr in unexpected state: %x, expected to be %x", act_state, |
| 166 | exp_state); |
Weicai Yang | bf7fb1e | 2022-03-27 22:45:08 -0700 | [diff] [blame] | 167 | } |
Weicai Yang | 1c94384 | 2022-03-30 17:32:49 -0700 | [diff] [blame] | 168 | |
| 169 | void keymgr_testutils_generate_identity(const dif_keymgr_t *keymgr) { |
| 170 | CHECK_DIF_OK(dif_keymgr_generate_identity_seed(keymgr)); |
Weicai Yang | bc6e0b9 | 2022-04-07 23:07:20 -0700 | [diff] [blame] | 171 | keymgr_testutils_wait_for_operation_done(keymgr); |
Weicai Yang | 1c94384 | 2022-03-30 17:32:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void keymgr_testutils_generate_versioned_key( |
| 175 | const dif_keymgr_t *keymgr, |
| 176 | const dif_keymgr_versioned_key_params_t params) { |
| 177 | CHECK_DIF_OK(dif_keymgr_generate_versioned_key(keymgr, params)); |
Weicai Yang | bc6e0b9 | 2022-04-07 23:07:20 -0700 | [diff] [blame] | 178 | keymgr_testutils_wait_for_operation_done(keymgr); |
| 179 | } |
Weicai Yang | 1c94384 | 2022-03-30 17:32:49 -0700 | [diff] [blame] | 180 | |
Weicai Yang | bc6e0b9 | 2022-04-07 23:07:20 -0700 | [diff] [blame] | 181 | void keymgr_testutils_disable(const dif_keymgr_t *keymgr) { |
| 182 | CHECK_DIF_OK(dif_keymgr_disable(keymgr)); |
| 183 | keymgr_testutils_wait_for_operation_done(keymgr); |
| 184 | } |
| 185 | |
| 186 | void keymgr_testutils_wait_for_operation_done(const dif_keymgr_t *keymgr) { |
Weicai Yang | 1c94384 | 2022-03-30 17:32:49 -0700 | [diff] [blame] | 187 | dif_keymgr_status_codes_t status; |
| 188 | do { |
| 189 | CHECK_DIF_OK(dif_keymgr_get_status_codes(keymgr, &status)); |
Weicai Yang | bc6e0b9 | 2022-04-07 23:07:20 -0700 | [diff] [blame] | 190 | } while (status == 0); |
Jade Philipoom | 3de23f1 | 2022-08-08 11:45:35 +0100 | [diff] [blame] | 191 | CHECK(status == kDifKeymgrStatusCodeIdle, "Unexpected status: %x", status); |
Weicai Yang | 1c94384 | 2022-03-30 17:32:49 -0700 | [diff] [blame] | 192 | } |