5 #include "sw/device/lib/crypto/drivers/entropy.h"
8 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
9 #include "sw/device/lib/testing/keymgr_testutils.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
12 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
13 #include "sw/device/silicon_creator/lib/otbn_boot_services.h"
14 #include "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
18 OTTF_DEFINE_TEST_CONFIG();
21 static dif_keymgr_t keymgr;
24 size_t num_keymgr_advances = 0;
27 const char kTestMessage[] =
"Test message.";
28 const size_t kTestMessageLen =
sizeof(kTestMessage) - 1;
32 .
x = {0x1ceb402b, 0x9dc600d1, 0x182ec21b, 0x5ede3640, 0x3566bdac,
33 0x1debf94b, 0x1a286a75, 0x8904d749},
34 .y = {0x63eab6dc, 0x0c53bf99, 0x086d3ee7, 0x1076efa6, 0x8dd8ece2,
35 0xbfececf0, 0x9b94e34d, 0x59b12f3c},
40 .r = {0x4811545a, 0x088d927b, 0x5d8624b5, 0x2ef1f329, 0x184ba14a,
41 0xf655eede, 0xaaed0d54, 0xa20e1ac7},
42 .s = {0x729b945d, 0x181dc116, 0x1025dba4, 0xb99828a0, 0xe7225df3,
43 0x0e200e9b, 0x785690b4, 0xf47efe98}};
47 .
salt = {0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f, 0xf0f1f2f3,
48 0xf4f5f6f7, 0xf8f9fafb, 0xfcfdfeff},
53 static const uint32_t kSeedValues[3][kAttestationSeedWords] = {
54 {0x70717273, 0x74757677, 0x78797a7b, 0x7c7d7e7f, 0x80818283, 0x84858687,
55 0x88898a8b, 0x8c8d8e8f, 0x90b1b2b3, 0x94959697},
56 {0xa0a1a2a3, 0xa4a5a6a7, 0xa8a9aaab, 0xacadaeaf, 0xb0b1b2b3, 0xb4b5b6b7,
57 0xb8b9babb, 0xbcbdbebf, 0xc0b1b2b3, 0xc4c5c6c7},
58 {0xd0d1d2d3, 0xd4d5d6d7, 0xd8d9dadb, 0xdcdddedf, 0xe0e1e2e3, 0xe4e5e6e7,
59 0xe8e9eaeb, 0xecedeeef, 0xf0b1b2b3, 0xf4f5f6f7},
62 rom_error_t sigverify_test(
void) {
65 hmac_sha256(kTestMessage, kTestMessageLen, &digest);
69 uint32_t recovered_r[kEcdsaP256SignatureComponentWords];
71 otbn_boot_sigverify(&kEcdsaKey, &kEcdsaSignature, &digest, recovered_r));
72 CHECK_ARRAYS_EQ(recovered_r, kEcdsaSignature.r,
ARRAYSIZE(kEcdsaSignature.r));
76 rom_error_t attestation_keygen_test(
void) {
79 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldUdsKeySeedIdx,
80 kScKeymgrKeyTypeAttestation,
81 kDiversification, &pk_uds));
83 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldCdi0KeySeedIdx,
84 kScKeymgrKeyTypeAttestation,
85 kDiversification, &pk_cdi0));
87 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldCdi1KeySeedIdx,
88 kScKeymgrKeyTypeAttestation,
89 kDiversification, &pk_cdi1));
91 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldTpmEkKeySeedIdx,
92 kScKeymgrKeyTypeSealing,
93 kDiversification, &pk_tpm_ek));
94 CHECK_ARRAYS_NE((
unsigned char *)&pk_uds, (
unsigned char *)&pk_cdi0,
96 CHECK_ARRAYS_NE((
unsigned char *)&pk_uds, (
unsigned char *)&pk_cdi1,
98 CHECK_ARRAYS_NE((
unsigned char *)&pk_cdi0, (
unsigned char *)&pk_cdi1,
100 CHECK_ARRAYS_NE((
unsigned char *)&pk_tpm_ek, (
unsigned char *)&pk_cdi1,
105 RETURN_IF_ERROR(otbn_boot_attestation_keygen(
106 kFlashInfoFieldUdsKeySeedIdx, kScKeymgrKeyTypeAttestation,
107 kDiversification, &pk_uds_again));
108 CHECK_ARRAYS_EQ((
unsigned char *)&pk_uds_again, (
unsigned char *)&pk_uds,
114 memcpy(&diversification_modified, &kDiversification,
115 sizeof(diversification_modified));
116 diversification_modified.
salt[0] ^= 1;
118 RETURN_IF_ERROR(otbn_boot_attestation_keygen(
119 kFlashInfoFieldUdsKeySeedIdx, kScKeymgrKeyTypeAttestation,
120 diversification_modified, &pk_uds_div));
121 CHECK_ARRAYS_NE((
unsigned char *)&pk_uds_div, (
unsigned char *)&pk_uds,
126 rom_error_t attestation_advance_and_endorse_test(
void) {
129 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldUdsKeySeedIdx,
130 kScKeymgrKeyTypeAttestation,
131 kDiversification, &pk));
132 RETURN_IF_ERROR(otbn_boot_attestation_key_save(kFlashInfoFieldUdsKeySeedIdx,
133 kScKeymgrKeyTypeAttestation,
137 if (num_keymgr_advances == 0) {
140 CHECK_STATUS_OK(keymgr_testutils_advance_state(&keymgr, &kOwnerIntParams));
141 num_keymgr_advances++;
143 CHECK(num_keymgr_advances == 1);
144 CHECK_STATUS_OK(keymgr_testutils_check_state(
147 keymgr_testutils_advance_state(&keymgr, &kOwnerRootKeyParams));
148 num_keymgr_advances++;
153 hmac_sha256(kTestMessage, kTestMessageLen, &digest);
155 RETURN_IF_ERROR(otbn_boot_attestation_endorse(&digest, &sig));
158 uint32_t recovered_r[kEcdsaP256SignatureComponentWords];
159 RETURN_IF_ERROR(otbn_boot_sigverify(&pk, &sig, &digest, recovered_r));
160 CHECK_ARRAYS_EQ(recovered_r, sig.r,
ARRAYSIZE(sig.r));
164 RETURN_IF_ERROR(otbn_boot_attestation_endorse(&digest, &sig));
167 RETURN_IF_ERROR(otbn_boot_sigverify(&pk, &sig, &digest, recovered_r));
168 CHECK_ARRAYS_NE(recovered_r, sig.r,
ARRAYSIZE(sig.r));
173 RETURN_IF_ERROR(otbn_boot_attestation_keygen(kFlashInfoFieldUdsKeySeedIdx,
174 kScKeymgrKeyTypeAttestation,
175 kDiversification, &pk_adv));
176 CHECK_ARRAYS_NE((
unsigned char *)&pk, (
unsigned char *)&pk_adv,
sizeof(pk));
182 rom_error_t attestation_save_clear_key_test(
void) {
184 RETURN_IF_ERROR(otbn_boot_attestation_key_save(kFlashInfoFieldUdsKeySeedIdx,
185 kScKeymgrKeyTypeAttestation,
187 RETURN_IF_ERROR(otbn_boot_attestation_key_clear());
190 RETURN_IF_ERROR(otbn_boot_attestation_key_save(kFlashInfoFieldUdsKeySeedIdx,
191 kScKeymgrKeyTypeAttestation,
194 hmac_sha256(kTestMessage, kTestMessageLen, &digest);
196 RETURN_IF_ERROR(otbn_boot_attestation_endorse(&digest, &sig));
200 RETURN_IF_ERROR(otbn_boot_attestation_key_clear());
201 hmac_sha256(kTestMessage, kTestMessageLen, &digest);
202 CHECK(otbn_boot_attestation_endorse(&digest, &sig) ==
203 kErrorOtbnExecutionFailed);
211 CHECK_STATUS_OK(entropy_complex_init());
213 CHECK_STATUS_OK(keymgr_testutils_startup(&keymgr, &kmac));
222 CHECK_STATUS_OK(flash_ctrl_testutils_wait_for_init(&flash_ctrl));
227 kFlashInfoFieldUdsAttestationKeySeed,
228 kFlashInfoFieldCdi0AttestationKeySeed,
229 kFlashInfoFieldCdi1AttestationKeySeed,
231 uint32_t page_address = 0;
232 CHECK_STATUS_OK(flash_ctrl_testutils_info_region_scrambled_setup(
233 &flash_ctrl, seed_fields[0].page, seed_fields[0].bank,
234 seed_fields[0].partition, &page_address));
235 CHECK_STATUS_OK(flash_ctrl_testutils_erase_and_write_page(
236 &flash_ctrl, page_address, seed_fields[0].partition, kSeedValues[0],
237 kDifFlashCtrlPartitionTypeInfo, kAttestationSeedWords));
239 for (
size_t i = 1; i <
ARRAYSIZE(seed_fields); i++) {
240 CHECK(seed_fields[i].page == seed_fields[i - 1].page);
241 CHECK(seed_fields[i].bank == seed_fields[i - 1].bank);
242 CHECK(seed_fields[i].partition == seed_fields[i - 1].partition);
243 CHECK_STATUS_OK(flash_ctrl_testutils_write(
244 &flash_ctrl, page_address + seed_fields[i].byte_offset,
245 seed_fields[i].partition, kSeedValues[i],
246 kDifFlashCtrlPartitionTypeInfo, kAttestationSeedWords));
250 CHECK(otbn_boot_app_load() == kErrorOk);
254 EXECUTE_TEST(result, attestation_advance_and_endorse_test);
256 EXECUTE_TEST(result, attestation_advance_and_endorse_test);
259 return status_ok(result);