13 #include "sw/device/lib/testing/keymgr_testutils.h"
14 #include "sw/device/lib/testing/kmac_testutils.h"
15 #include "sw/device/lib/testing/lc_ctrl_testutils.h"
16 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
17 #include "sw/device/lib/testing/rstmgr_testutils.h"
18 #include "sw/device/lib/testing/test_framework/check.h"
19 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
21 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
24 #include "otp_ctrl_regs.h"
26 static dif_lc_ctrl_t lc;
27 static dif_otp_ctrl_t otp;
28 static dif_rstmgr_t rstmgr;
29 static dif_keymgr_t keymgr;
30 static dif_kmac_t kmac;
34 static const uint8_t kOtpRmaToken[OTP_CTRL_PARAM_RMA_TOKEN_SIZE] = {
35 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
36 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
41 kOtpRootKeyShare0[OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE0_SIZE] = {
42 0x6e, 0x70, 0x48, 0x68, 0xa3, 0x2f, 0xfa, 0x1b, 0xd8, 0x36, 0x4d,
43 0x00, 0x79, 0xbd, 0x04, 0xe6, 0x53, 0x18, 0xfc, 0xc3, 0xc9, 0xdd,
44 0x39, 0xfa, 0xe0, 0x18, 0x49, 0x69, 0xc9, 0x81, 0x05, 0x17,
49 kOtpRootKeyShare1[OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE1_SIZE] = {
50 0x2f, 0x78, 0x6f, 0x93, 0x9a, 0xc4, 0xcf, 0x95, 0xb6, 0xec, 0x61,
51 0x76, 0xbd, 0x08, 0x05, 0x0c, 0x09, 0x89, 0xe1, 0x73, 0x4b, 0x0a,
52 0x59, 0x1b, 0xdd, 0x34, 0x84, 0x82, 0x32, 0x9a, 0x93, 0x31};
55 typedef enum { kWriteMode, kReadMode, kWriteReadMode } test_mode_t;
57 OTTF_DEFINE_TEST_CONFIG();
62 static void init_peripherals(
void) {
65 .integrity_period_mask = 0x3ffff,
66 .consistency_period_mask = 0x3ffffff,
69 CHECK_DIF_OK(dif_lc_ctrl_init(
72 CHECK_DIF_OK(dif_otp_ctrl_init(
76 CHECK_DIF_OK(dif_rstmgr_init(
81 CHECK_STATUS_OK(kmac_testutils_config(&kmac,
true));
83 CHECK_DIF_OK(dif_keymgr_init(
96 static void otp_write_test(uint32_t address,
const uint8_t *buffer,
97 uint32_t size, exp_test_result_t exp_result) {
98 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
99 for (uint32_t i = address; i < address + size; i +=
sizeof(uint64_t)) {
101 memcpy(&word, &buffer[i],
sizeof(word));
104 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
105 CHECK(address <= INT32_MAX,
"address must fit into int32_t");
106 CHECK_STATUS_OK(otp_ctrl_testutils_dai_access_error_check(
107 &otp, exp_result, (int32_t)address));
120 static void otp_read_test(uint32_t address,
const uint8_t *buffer,
121 uint32_t size, exp_test_result_t exp_result) {
122 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
123 for (uint32_t i = address; i < address + size; i +=
sizeof(uint64_t)) {
127 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
128 CHECK(address <= INT32_MAX,
"address must fit into int32_t");
129 CHECK_STATUS_OK(otp_ctrl_testutils_dai_access_error_check(
130 &otp, exp_result, (int32_t)address));
133 memcpy(&exp, &buffer[i],
sizeof(exp));
135 if (exp_result == kExpectFailed) {
137 "0x%x == 0x%x (got == exp) is not expected at address 0x%x",
138 (uint32_t)got, (uint32_t)exp, address);
141 "0x%x != 0x%x (got != exp) is not expected at address 0x%x",
142 (uint32_t)got, (uint32_t)exp, address);
150 static void otp_write_read_test(test_mode_t test_mode, uint32_t address,
151 const uint8_t *buffer, uint32_t size,
152 exp_test_result_t exp_result) {
153 if (test_mode == kWriteMode || test_mode == kWriteReadMode) {
154 LOG_INFO(
"Write test at 0x%x...", address);
155 otp_write_test(address, buffer, size, exp_result);
157 if (test_mode == kReadMode || test_mode == kWriteReadMode) {
158 LOG_INFO(
"Read test at 0x%x...", address);
159 otp_read_test(address, buffer, size, exp_result);
167 static void run_otp_access_tests(test_mode_t test_mode,
168 exp_test_result_t exp_result) {
171 OTP_CTRL_PARAM_RMA_TOKEN_OFFSET - OTP_CTRL_PARAM_SECRET2_OFFSET,
172 (uint8_t *)kOtpRmaToken, OTP_CTRL_PARAM_RMA_TOKEN_SIZE, exp_result);
173 otp_write_read_test(test_mode,
174 OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE0_OFFSET -
175 OTP_CTRL_PARAM_SECRET2_OFFSET,
176 (uint8_t *)kOtpRootKeyShare0,
177 OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE0_SIZE, exp_result);
178 otp_write_read_test(test_mode,
179 OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE1_OFFSET -
180 OTP_CTRL_PARAM_SECRET2_OFFSET,
181 (uint8_t *)kOtpRootKeyShare1,
182 OTP_CTRL_PARAM_CREATOR_ROOT_KEY_SHARE1_SIZE, exp_result);
190 static void keymgr_advance_to_creator_root_key(
bool expect_init_fail) {
193 bool is_using_test_rom =
201 CHECK_STATUS_OK(keymgr_testutils_advance_state(&keymgr, NULL));
204 CHECK_STATUS_OK(keymgr_testutils_check_state(&keymgr, exp_state));
207 if (is_using_test_rom) {
208 LOG_INFO(
"Using test_rom, setting inputs and advancing state...");
212 LOG_INFO(
"Using rom, only advancing state...");
220 static void keymgr_check_cannot_advance(
void) {
221 LOG_INFO(
"Check that the Keymgr cannot advance...");
226 "Keymgr is not expected to be available in this LC state.");
234 static void keymgr_check_root_key_is_invalid(
void) {
235 LOG_INFO(
"Trying to advance to Creator Root Key and expecting failure...");
236 keymgr_advance_to_creator_root_key(
true);
242 static void keymgr_check_can_generate_key(
void) {
243 LOG_INFO(
"Trying to advance to Creator Root Key and expecting success...");
244 keymgr_advance_to_creator_root_key(
false);
245 LOG_INFO(
"Check that the Keymgr can generate a key...");
246 CHECK_STATUS_OK(keymgr_testutils_wait_for_operation_done(&keymgr));
250 keymgr_testutils_generate_versioned_key(&keymgr, kKeyVersionedParams));
256 static void reset_chip(
void) {
259 CHECK(
false,
"Should have reset before this line");
298 rst_info = rstmgr_testutils_reason_get();
299 rstmgr_testutils_reason_clear();
302 CHECK_STATUS_OK(lc_ctrl_testutils_lc_state_log(&state));
310 LOG_INFO(
"First access test iteration...");
312 CHECK_STATUS_OK(keymgr_testutils_flash_init(&flash, &kCreatorSecret,
315 run_otp_access_tests(kWriteReadMode, kExpectPassed);
317 keymgr_check_root_key_is_invalid();
325 LOG_INFO(
"Second access test iteration...");
327 run_otp_access_tests(kReadMode, kExpectPassed);
330 keymgr_check_root_key_is_invalid();
332 CHECK_STATUS_OK(otp_ctrl_testutils_lock_partition(
336 LOG_INFO(
"Third access test iteration...");
338 run_otp_access_tests(kWriteReadMode, kExpectFailed);
341 keymgr_check_can_generate_key();
346 LOG_ERROR(
"Unexpected reset info 0x%02X", rst_info);
352 run_otp_access_tests(kWriteReadMode, kExpectFailed);
354 keymgr_check_cannot_advance();