5 #include "sw/device/silicon_creator/lib/cert/dice.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
11 #include "sw/device/silicon_creator/lib/base/util.h"
12 #include "sw/device/silicon_creator/lib/cert/cdi_0.h"
13 #include "sw/device/silicon_creator/lib/cert/cdi_1.h"
14 #include "sw/device/silicon_creator/lib/cert/cert.h"
15 #include "sw/device/silicon_creator/lib/cert/dice_keys.h"
16 #include "sw/device/silicon_creator/lib/cert/template.h"
17 #include "sw/device/silicon_creator/lib/cert/uds.h"
18 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
19 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
20 #include "sw/device/silicon_creator/lib/error.h"
21 #include "sw/device/silicon_creator/lib/otbn_boot_services.h"
22 #include "sw/device/silicon_creator/lib/ownership/datatypes.h"
23 #include "sw/device/silicon_creator/lib/sigverify/ecdsa_p256_key.h"
24 #include "sw/device/silicon_creator/manuf/base/perso_tlv_data.h"
25 #include "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
29 static uint8_t cdi_0_tbs_buffer[kCdi0MaxTbsSizeBytes];
30 static uint8_t cdi_1_tbs_buffer[kCdi1MaxTbsSizeBytes];
32 const dice_cert_format_t kDiceCertFormat = kDiceCertFormatX509TcbInfo;
34 static_assert(kDiceMeasurementSizeInBytes == 32,
35 "The DICE attestation measurement size should equal the size of "
36 "the keymgr binding registers.");
41 static bool is_debug_exposed(
void) {
42 lifecycle_state_t lc_state = lifecycle_state_get();
43 if (lc_state == kLcStateProd || lc_state == kLcStateProdEnd) {
52 static bool get_debug_mode_cdi1(owner_app_domain_t key_domain) {
53 if (launder32(key_domain) != kOwnerAppDomainProd) {
60 rom_error_t dice_uds_tbs_cert_build(
66 uint8_t *tbs_cert,
size_t *tbs_cert_size) {
68 uds_tbs_values_t uds_tbs_params = {0};
70 TEMPLATE_SET(uds_tbs_params, Uds, OtpCreatorSwCfgHash,
71 otp_creator_sw_cfg_measurement->digest);
72 TEMPLATE_SET(uds_tbs_params, Uds, OtpOwnerSwCfgHash,
73 otp_owner_sw_cfg_measurement->digest);
74 TEMPLATE_SET(uds_tbs_params, Uds, OtpRotCreatorAuthCodesignHash,
75 otp_rot_creator_auth_codesign_measurement->digest);
76 TEMPLATE_SET(uds_tbs_params, Uds, OtpRotCreatorAuthStateHash,
77 otp_rot_creator_auth_state_measurement->digest);
78 TEMPLATE_SET(uds_tbs_params, Uds, DebugFlag, is_debug_exposed());
79 TEMPLATE_SET(uds_tbs_params, Uds, CreatorPubKeyEcX, uds_pubkey->
x);
80 TEMPLATE_SET(uds_tbs_params, Uds, CreatorPubKeyEcY, uds_pubkey->
y);
82 TEMPLATE_SET_TRUNCATED(uds_tbs_params, Uds, CreatorPubKeyId,
83 key_ids->
cert->digest, kCertKeyIdSizeInBytes);
84 TEMPLATE_SET_TRUNCATED(uds_tbs_params, Uds, AuthKeyKeyId,
85 key_ids->
endorsement->digest, kCertKeyIdSizeInBytes);
87 HARDENED_RETURN_IF_ERROR(
88 uds_build_tbs(&uds_tbs_params, tbs_cert, tbs_cert_size));
93 rom_error_t dice_cdi_0_cert_build(
hmac_digest_t *rom_ext_measurement,
94 uint32_t rom_ext_security_version,
97 uint8_t *cert,
size_t *cert_size) {
98 uint32_t rom_ext_security_version_be =
99 __builtin_bswap32(rom_ext_security_version);
101 util_reverse_bytes(&rom_ext_hash,
sizeof(rom_ext_hash));
104 cdi_0_tbs_values_t cdi_0_tbs_params = {0};
106 TEMPLATE_SET(cdi_0_tbs_params, Cdi0, RomExtHash, rom_ext_hash.digest);
107 TEMPLATE_SET(cdi_0_tbs_params, Cdi0, RomExtSecurityVersion,
108 &rom_ext_security_version_be);
109 TEMPLATE_SET(cdi_0_tbs_params, Cdi0, OwnerIntermediatePubKeyEcX,
111 TEMPLATE_SET(cdi_0_tbs_params, Cdi0, OwnerIntermediatePubKeyEcY,
114 TEMPLATE_SET_TRUNCATED(cdi_0_tbs_params, Cdi0, OwnerIntermediatePubKeyId,
115 key_ids->
cert->digest, kCertKeyIdSizeInBytes);
116 TEMPLATE_SET_TRUNCATED(cdi_0_tbs_params, Cdi0, CreatorPubKeyId,
117 key_ids->
endorsement->digest, kCertKeyIdSizeInBytes);
119 size_t tbs_size = kCdi0MaxTbsSizeBytes;
120 HARDENED_RETURN_IF_ERROR(
121 cdi_0_build_tbs(&cdi_0_tbs_params, cdi_0_tbs_buffer, &tbs_size));
125 hmac_sha256(cdi_0_tbs_buffer, tbs_size, &tbs_digest);
126 HARDENED_RETURN_IF_ERROR(
127 otbn_boot_attestation_endorse(&tbs_digest, &curr_tbs_signature));
128 util_p256_signature_le_to_be_convert(curr_tbs_signature.r,
129 curr_tbs_signature.s);
131 cdi_0_sig_values_t cdi_0_params = {0};
132 TEMPLATE_SET(cdi_0_params, Cdi0, Tbs, cdi_0_tbs_buffer);
134 TEMPLATE_ASSERT_FIXED_LENGTH(Cdi0, Tbs);
135 TEMPLATE_SET(cdi_0_params, Cdi0, CertSignatureR, curr_tbs_signature.r);
136 TEMPLATE_SET(cdi_0_params, Cdi0, CertSignatureS, curr_tbs_signature.s);
138 HARDENED_RETURN_IF_ERROR(cdi_0_build_cert(&cdi_0_params, cert, cert_size));
141 HARDENED_RETURN_IF_ERROR(otbn_boot_attestation_key_save(
148 rom_error_t dice_cdi_1_cert_build(
hmac_digest_t *owner_measurement,
150 uint32_t owner_security_version,
151 owner_app_domain_t key_domain,
154 uint8_t *cert,
size_t *cert_size) {
155 uint32_t owner_security_version_be =
156 __builtin_bswap32(owner_security_version);
158 hmac_digest_t owner_manifest_hash = *owner_manifest_measurement;
159 util_reverse_bytes(&owner_hash,
sizeof(owner_hash));
160 util_reverse_bytes(&owner_manifest_hash,
sizeof(owner_manifest_hash));
163 cdi_1_tbs_values_t cdi_1_tbs_params = {0};
165 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, OwnerHash, owner_hash.digest);
166 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, OwnerManifestHash,
167 owner_manifest_hash.digest);
168 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, OwnerSecurityVersion,
169 &owner_security_version_be);
170 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, DebugFlag,
171 get_debug_mode_cdi1(key_domain));
172 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, OwnerPubKeyEcX, cdi_1_pubkey->
x);
173 TEMPLATE_SET(cdi_1_tbs_params, Cdi1, OwnerPubKeyEcY, cdi_1_pubkey->
y);
175 TEMPLATE_SET_TRUNCATED(cdi_1_tbs_params, Cdi1, OwnerPubKeyId,
176 key_ids->
cert->digest, kCertKeyIdSizeInBytes);
177 TEMPLATE_SET_TRUNCATED(cdi_1_tbs_params, Cdi1, OwnerIntermediatePubKeyId,
178 key_ids->
endorsement->digest, kCertKeyIdSizeInBytes);
180 size_t tbs_size = kCdi1MaxTbsSizeBytes;
181 HARDENED_RETURN_IF_ERROR(
182 cdi_1_build_tbs(&cdi_1_tbs_params, cdi_1_tbs_buffer, &tbs_size));
186 hmac_sha256(cdi_1_tbs_buffer, tbs_size, &tbs_digest);
187 HARDENED_RETURN_IF_ERROR(
188 otbn_boot_attestation_endorse(&tbs_digest, &curr_tbs_signature));
189 util_p256_signature_le_to_be_convert(curr_tbs_signature.r,
190 curr_tbs_signature.s);
192 cdi_1_sig_values_t cdi_1_params = {0};
193 TEMPLATE_SET(cdi_1_params, Cdi1, Tbs, cdi_1_tbs_buffer);
195 TEMPLATE_ASSERT_FIXED_LENGTH(Cdi1, Tbs);
196 TEMPLATE_SET(cdi_1_params, Cdi1, CertSignatureR, curr_tbs_signature.r);
197 TEMPLATE_SET(cdi_1_params, Cdi1, CertSignatureS, curr_tbs_signature.s);
199 HARDENED_RETURN_IF_ERROR(cdi_1_build_cert(&cdi_1_params, cert, cert_size));
202 HARDENED_RETURN_IF_ERROR(otbn_boot_attestation_key_save(
218 static_assert(
sizeof(pubkey_id->digest) >=
sizeof(cert_key_id_t),
219 "Pubkey Id is too short.");
221 return cert_x509_asn1_check_serial_number(
223 (cert_key_id_t *)pubkey_id->digest, cert_valid_output);