5 #include "sw/device/silicon_creator/lib/otbn_boot_services.h"
8 #include "sw/device/silicon_creator/lib/attestation.h"
10 #include "sw/device/silicon_creator/lib/base/util.h"
11 #include "sw/device/silicon_creator/lib/dbg_print.h"
12 #include "sw/device/silicon_creator/lib/drivers/flash_ctrl.h"
13 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
14 #include "sw/device/silicon_creator/lib/drivers/keymgr.h"
15 #include "sw/device/silicon_creator/lib/drivers/otbn.h"
17 #include "otbn_regs.h"
19 static_assert(kAttestationSeedWords <= 16,
20 "Additional attestation seed needs must be <= 516 bits.");
32 boot, attestation_additional_seed);
43 static const sc_otbn_addr_t kOtbnVarBootAttestationAdditionalSeed =
50 kOtbnBootModeWords = 1,
56 kOtbnBootModeSigverify = 0x7d3,
62 kOtbnBootModeAttestationKeygen = 0x2bf,
68 kOtbnBootModeAttestationEndorse = 0x5e8,
74 kOtbnBootModeAttestationKeySave = 0x64d,
77 kOtbnAttestationSeedBufferWords =
78 ((kAttestationSeedWords + kScOtbnWideWordNumWords - 1) /
79 kScOtbnWideWordNumWords) *
80 kScOtbnWideWordNumWords,
84 static rom_error_t load_attestation_keygen_seed(uint32_t additional_seed_idx,
87 uint32_t seed_flash_offset =
88 0 + (additional_seed_idx * kAttestationSeedBytes);
90 flash_ctrl_info_read(&kFlashCtrlInfoPageAttestationKeySeeds,
91 seed_flash_offset, kAttestationSeedWords, seed);
93 if (err != kErrorOk) {
95 flash_ctrl_error_code_get(&flash_ctrl_err_code);
96 if (flash_ctrl_err_code.
rd_err) {
100 memset(seed, 0, kAttestationSeedBytes);
109 rom_error_t otbn_boot_app_load(
void) {
return sc_otbn_load_app(kOtbnAppBoot); }
111 rom_error_t otbn_boot_attestation_keygen(
112 uint32_t additional_seed_idx, sc_keymgr_key_type_t key_type,
116 HARDENED_RETURN_IF_ERROR(
117 sc_keymgr_generate_key_otbn(key_type, diversification));
120 uint32_t mode = kOtbnBootModeAttestationKeygen;
121 HARDENED_RETURN_IF_ERROR(
122 sc_otbn_dmem_write(kOtbnBootModeWords, &mode, kOtbnVarBootMode));
125 uint32_t seed[kAttestationSeedWords];
126 HARDENED_RETURN_IF_ERROR(
127 load_attestation_keygen_seed(additional_seed_idx, seed));
130 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(
131 kAttestationSeedWords, seed, kOtbnVarBootAttestationAdditionalSeed));
134 uint32_t zero_buf[kOtbnAttestationSeedBufferWords - kAttestationSeedWords] = {
136 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(
138 kOtbnVarBootAttestationAdditionalSeed + kAttestationSeedBytes));
141 HARDENED_RETURN_IF_ERROR(sc_otbn_execute());
147 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(kEcdsaP256PublicKeyCoordWords,
148 kOtbnVarBootX, public_key->
x));
149 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(kEcdsaP256PublicKeyCoordWords,
150 kOtbnVarBootY, public_key->
y));
160 util_reverse_bytes(pubkey->
x, kEcdsaP256PublicKeyCoordBytes);
161 util_reverse_bytes(pubkey->
y, kEcdsaP256PublicKeyCoordBytes);
167 HARDENED_RETURN_IF_ERROR(sc_keymgr_state_check(key.required_keymgr_state));
170 HARDENED_RETURN_IF_ERROR(otbn_boot_attestation_keygen(
171 key.keygen_seed_idx, key.type, *key.keymgr_diversifier, pubkey));
176 pubkey_le_to_be_convert(pubkey);
183 hmac_sha256(pubkey,
sizeof(*pubkey), pubkey_id);
184 util_reverse_bytes(pubkey_id,
sizeof(*pubkey_id));
189 rom_error_t otbn_boot_attestation_key_save(
190 uint32_t additional_seed_idx, sc_keymgr_key_type_t key_type,
193 HARDENED_RETURN_IF_ERROR(
194 sc_keymgr_generate_key_otbn(key_type, diversification));
197 uint32_t mode = kOtbnBootModeAttestationKeySave;
198 HARDENED_RETURN_IF_ERROR(
199 sc_otbn_dmem_write(kOtbnBootModeWords, &mode, kOtbnVarBootMode));
202 uint32_t seed[kAttestationSeedWords];
203 HARDENED_RETURN_IF_ERROR(
204 load_attestation_keygen_seed(additional_seed_idx, seed));
207 uint32_t zero_buf[kOtbnAttestationSeedBufferWords - kAttestationSeedWords] = {
209 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(
211 kOtbnVarBootAttestationAdditionalSeed + kAttestationSeedBytes));
214 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(
215 kAttestationSeedWords, seed, kOtbnVarBootAttestationAdditionalSeed));
218 HARDENED_RETURN_IF_ERROR(sc_otbn_execute());
226 rom_error_t otbn_boot_attestation_key_clear(
void) {
228 RETURN_IF_ERROR(sc_otbn_dmem_sec_wipe());
229 HARDENED_RETURN_IF_ERROR(sc_otbn_busy_wait_for_done());
234 return kErrorOtbnInvalidArgument;
237 const size_t data_num_words =
239 if (data_num_words > 0) {
240 HARDENED_RETURN_IF_ERROR(
247 rom_error_t otbn_boot_attestation_endorse(
const hmac_digest_t *digest,
250 uint32_t mode = kOtbnBootModeAttestationEndorse;
251 HARDENED_RETURN_IF_ERROR(
252 sc_otbn_dmem_write(kOtbnBootModeWords, &mode, kOtbnVarBootMode));
255 HARDENED_RETURN_IF_ERROR(
256 sc_otbn_dmem_write(kHmacDigestNumWords, digest->digest, kOtbnVarBootMsg));
259 HARDENED_RETURN_IF_ERROR(sc_otbn_execute());
265 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(kEcdsaP256SignatureComponentWords,
266 kOtbnVarBootR, sig->r));
267 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(kEcdsaP256SignatureComponentWords,
268 kOtbnVarBootS, sig->s));
276 uint32_t *recovered_r) {
278 uint32_t mode = kOtbnBootModeSigverify;
279 HARDENED_RETURN_IF_ERROR(
280 sc_otbn_dmem_write(kOtbnBootModeWords, &mode, kOtbnVarBootMode));
283 HARDENED_RETURN_IF_ERROR(
284 sc_otbn_dmem_write(kEcdsaP256PublicKeyCoordWords, key->x, kOtbnVarBootX));
285 HARDENED_RETURN_IF_ERROR(
286 sc_otbn_dmem_write(kEcdsaP256PublicKeyCoordWords, key->y, kOtbnVarBootY));
289 HARDENED_RETURN_IF_ERROR(
290 sc_otbn_dmem_write(kHmacDigestNumWords, digest->digest, kOtbnVarBootMsg));
293 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(kEcdsaP256SignatureComponentWords,
294 sig->r, kOtbnVarBootR));
295 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write(kEcdsaP256SignatureComponentWords,
296 sig->s, kOtbnVarBootS));
299 HARDENED_RETURN_IF_ERROR(sc_otbn_execute());
304 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(1, kOtbnVarBootOk, &ok));
306 return kErrorSigverifyBadEcdsaSignature;
310 HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_read(1, kOtbnVarBootOk, &ok));
316 return sc_otbn_dmem_read(kEcdsaP256SignatureComponentWords, kOtbnVarBootXr,