Software APIs
otbn_boot_services.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_OTBN_BOOT_SERVICES_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_OTBN_BOOT_SERVICES_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "sw/device/silicon_creator/lib/attestation.h"
12 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
13 #include "sw/device/silicon_creator/lib/drivers/keymgr.h"
14 #include "sw/device/silicon_creator/lib/sigverify/ecdsa_p256_key.h"
15 #include "sw/device/silicon_creator/lib/sigverify/rsa_key.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif // __cplusplus
20 
21 /**
22  * Loads the OTBN boot-services application.
23  *
24  * Loads the OTBN program that runs attestation and code-signature
25  * verification. The program can later be cleared by wiping OTBN's IMEM and
26  * DMEM, or by loading a diffierent OTBN application.
27  *
28  * @return The result of the operation.
29  */
31 rom_error_t otbn_boot_app_load(void);
32 
33 /**
34  * Generate an attestation public key from a keymgr-derived secret.
35  *
36  * This routine triggers the key manager to sideload key material into OTBN,
37  * and also loads in an extra seed to XOR with the key material. The final
38  * private key is:
39  * d = (additional_seed ^ keymgr_seed) mod n
40  * ...where n is the P256 curve order. The public key is d*G, where G is the
41  * P256 base point.
42  *
43  * The extra seed is expected to be the output from a specially seeded DRBG, and
44  * is provisioned into flash at manufacturing time. It must be fully independent
45  * from the key manager seed.
46  *
47  * Expects the OTBN boot-services program to already be loaded; see
48  * `otbn_boot_app_load`.
49  *
50  * @param additional_seed_idx The attestation key generation seed index to load.
51  * The index corresponds to the seed offset in flash
52  * info page `kFlashCtrlInfoPageAttestationKeySeeds`.
53  * @param key_type Keymgr key type to generate, attestation or sealing.
54  * @param diversification Salt and version information for key manager.
55  * @param[out] public_key Attestation public key.
56  * @return The result of the operation.
57  */
59 rom_error_t otbn_boot_attestation_keygen(
60  uint32_t additional_seed_idx, sc_keymgr_key_type_t key_type,
61  sc_keymgr_diversification_t diversification,
62  ecdsa_p256_public_key_t *public_key);
63 
64 /**
65  * Generate a deterministic ECC P256 attestation public key from a
66  * keymgr-derived secret.
67  *
68  * This routine may be used to generate DICE attestation keys, TPM identity
69  * keys, or any other deterministic asymmetric ECC P256 keys required. DICE keys
70  * should be contructed from the "attestation" keymgr key type, while TPM keys
71  * should be constructed form the "sealing" key type. The former are bound to
72  * firmware updates, and change when ROM_EXT or Owner firmware is updated. The
73  * latter remain stable across the lifetime of an ownership of the chip.
74  *
75  * This routine triggers the key manager to sideload key material into OTBN,
76  * and also loads in an extra seed to XOR with the key material. The final
77  * private key is:
78  * d = (additional_seed ^ keymgr_seed) mod n
79  * ...where n is the P256 curve order. The public key is d*G, where G is the
80  * P256 base point.
81  *
82  * The extra seed is expected to be the output from a specially seeded DRBG, and
83  * is provisioned into flash at manufacturing time. It must be fully independent
84  * from the key manager seed.
85  *
86  * Expects the OTBN boot-services program to already be loaded; see
87  * `otbn_boot_app_load`.
88  *
89  * @param additional_seed_idx The attestation key generation seed index to load.
90  * The index corresponds to the seed offset in flash
91  * info page `kFlashCtrlInfoPageAttestationKeySeeds`.
92  * @param key_type Keymgr key type to generate, attestation or sealing.
93  * @param diversification Salt and version information for key manager.
94  * @param[out] public_key Attestation public key.
95  * @return The result of the operation.
96  */
98 rom_error_t otbn_boot_attestation_keygen(
99  uint32_t additional_seed_idx, sc_keymgr_key_type_t key_type,
100  sc_keymgr_diversification_t diversification,
101  ecdsa_p256_public_key_t *public_key);
102 
103 /**
104  * Wrapper for `otbn_boot_attestation_keygen()` that generates an ECC P256
105  * keypair to build a certificate around, using Keymgr and OTBN, returning the
106  * public key (in big endian order for inserting into a cert) and a key ID
107  * (which is a SHA256 digest of the public key).
108  *
109  * Preconditions: keymgr has been initialized and cranked to the desired stage.
110  *
111  * @param key The description of the desired key to generate.
112  * @param[out] pubkey_id The public key ID (for embedding into certificates).
113  * @param[out] pubkey The public key.
114  * @return The result of the operation.
115  */
117 rom_error_t otbn_boot_cert_ecc_p256_keygen(sc_keymgr_ecc_key_t key,
118  hmac_digest_t *pubkey_id,
119  ecdsa_p256_public_key_t *pubkey);
120 
121 /**
122  * Saves an attestation private key to OTBN's scratchpad.
123  *
124  * This routine takes the same arguments as `otbn_boot_attestation_keygen`, but
125  * instead of computing the public key, it computes only the private key and
126  * saves it to OTBN's scratchpad memory.
127  *
128  * Expects the OTBN boot-services program to already be loaded; see
129  * `otbn_boot_app_load`.
130  *
131  * @param additional_seed The attestation key generation seed to load.
132  * @param key_type OTBN attestation key type to generate. "DICE" attestation
133  * keys are based on "attestation" keys from the keymgr; "TPM"
134  * attestation keys are based on "sealing keys from the keymgr.
135  * @param diversification Salt and version information for key manager.
136  * @return The result of the operation.
137  */
139 rom_error_t otbn_boot_attestation_key_save(
140  uint32_t additional_seed, sc_keymgr_key_type_t key_type,
141  sc_keymgr_diversification_t diversification);
142 
143 /**
144  * Clears any saved attestation key from OTBN's scratchpad.
145  *
146  * This routine clears OTBN's DMEM. If called after
147  * `otbn_boot_attestation_key_save`, it will clear the saved key.
148  *
149  * @return The result of the operation.
150  */
152 rom_error_t otbn_boot_attestation_key_clear(void);
153 
154 /**
155  * Signs the message with the saved attestation key, and clears the key.
156  *
157  * Must be called when there is a saved attestation key in OTBN's scratchpad;
158  * use `otbn_boot_attestation_key_save` to store one.
159  *
160  * The intended purpose of this function is to sign the current stage's
161  * attestation certificate with the private key of the previous stage. The
162  * caller should hash the certificate with SHA-256 before calling this
163  * function.
164  *
165  * Note that the digest gets interpreted by OTBN in little-endian order. If the
166  * HMAC block has not been set to produce little-endian digests, then the
167  * digest bytes should be reversed before they are passed here.
168  *
169  * Expects the OTBN boot-services program to already be loaded; see
170  * `otbn_boot_app_load`.
171  *
172  * @param digest Digest to sign.
173  * @param[out] sig Resulting signature.
174  * @return The result of the operation.
175  */
177 rom_error_t otbn_boot_attestation_endorse(const hmac_digest_t *digest,
179 
180 /**
181  * Computes an ECDSA-P256 signature verification on OTBN.
182  *
183  * May be used for code signatures as well as attestation signatures. Returns
184  * the recovered `r` value in `result`. The signature is valid if this `r`
185  * value matches the `r` component of the signature, but the caller is
186  * responsible for the final comparison.
187  *
188  * Expects the OTBN boot-services program to already be loaded; see
189  * `otbn_boot_app_load`.
190  *
191  * @param key An ECDSA-P256 public key.
192  * @param sig An ECDSA-P256 signature.
193  * @param digest Message digest to check against.
194  * @param[out] recovered_r Buffer for the recovered `r` value.
195  * @return The result of the operation.
196  */
198 rom_error_t otbn_boot_sigverify(const ecdsa_p256_public_key_t *key,
199  const ecdsa_p256_signature_t *sig,
200  const hmac_digest_t *digest,
201  uint32_t *recovered_r);
202 
203 #ifdef __cplusplus
204 } // extern "C"
205 #endif // __cplusplus
206 
207 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_OTBN_BOOT_SERVICES_H_