Software APIs
keymgr_testutils.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_LIB_TESTING_KEYMGR_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_KEYMGR_TESTUTILS_H_
7
8#include "sw/device/lib/base/status.h"
11
12/**
13 * Versioned key parameters for testing.
14 *
15 * Change destination in order to sideload keys to hardware.
16 */
17static const dif_keymgr_versioned_key_params_t kKeyVersionedParams = {
19 .salt =
20 {
21 0xb6521d8f,
22 0x13a0e876,
23 0x1ca1567b,
24 0xb4fb0fdf,
25 0x9f89bc56,
26 0x4bd127c7,
27 0x322288d8,
28 0xde919d54,
29 },
30 .version = 0x11,
31};
32
33/**
34 * Software binding value for advancing to creator root key state.
35 */
36static const dif_keymgr_state_params_t kCreatorParams = {
37 .binding_value = {0xdc96c23d, 0xaf36e268, 0xcb68ff71, 0xe92f76e2,
38 0xb8a8379d, 0x426dc745, 0x19f5cff7, 0x4ec9c6d6},
39 .max_key_version = 0x11,
40};
41
42/**
43 * Software binding value for advancing to owner intermediate key state.
44 */
45static const dif_keymgr_state_params_t kOwnerIntParams = {
46 .binding_value = {0xe4987b39, 0x3f83d390, 0xc2f3bbaf, 0x3195dbfa,
47 0x23fb480c, 0xb012ae5e, 0xf1394d28, 0x1940ceeb},
48 .max_key_version = 0xaa,
49};
50
51/**
52 * Software binding value for advancing to owner root key state.
53 *
54 * Values were randomly generated.
55 */
56static const dif_keymgr_state_params_t kOwnerRootKeyParams = {
57 .binding_value =
58 {
59 0xd8a812ea,
60 0xb6ebe129,
61 0x217773d4,
62 0x35b37c77,
63 0xec8298be,
64 0x1f7dec77,
65 0x1803199e,
66 0xa02ad81d,
67 },
68 .max_key_version = 0xaa,
69};
70
71/**
72 * Struct to hold the creator or owner secrets for the key manager.
73 */
75 uint32_t value[8];
76} keymgr_testutils_secret_t;
77
78/**
79 * Key manager Creator Secret stored in info flash page.
80 */
81static const keymgr_testutils_secret_t kCreatorSecret = {
82 .value = {0x4e919d54, 0x322288d8, 0x4bd127c7, 0x9f89bc56, 0xb4fb0fdf,
83 0x1ca1567b, 0x13a0e876, 0xa6521d8f}};
84
85/**
86 * Key manager Owner Secret stored in info flash page.
87 */
88static const keymgr_testutils_secret_t kOwnerSecret = {.value = {
89 0xa6521d8f,
90 0x13a0e876,
91 0x1ca1567b,
92 0xb4fb0fdf,
93 0x9f89bc56,
94 0x4bd127c7,
95 0x322288d8,
96 0x4e919d54,
97 }};
98
99/**
100 * Programs flash with secrets so that the keymgr can be advanced to
101 * CreatorRootKey state.
102 *
103 * This is normally a subfunction of keymgr_testutils_startup, but some tests
104 * use the function separately as well.
105 *
106 * @param creator_secret The creator secret to be programmed to flash, or NULL
107 * to skip writing the creator secret.
108 * @param owner_secret The owner secret to be programmed to flash.
109 *
110 */
112status_t keymgr_testutils_nvm_init(
113 const keymgr_testutils_secret_t *creator_secret,
114 const keymgr_testutils_secret_t *owner_secret);
115
116/**
117 * Initializes the key manager and its dependencies for testing.
118 *
119 * This function initializes the key manager and its dependencies for testing.
120 *
121 * This fuction will call `keymgr_testutils_try_startup()` if the boot stage is
122 * `kBootStageOwner`; otherwise, it will call `keymgr_testutils_startup()`.
123 * Additional checks are performed to ensure that the key manager is in a valid
124 * state, and ready to perform key derivations.
125 *
126 * @param keymgr A key manager handle, may be uninitialized.
127 * @param kmac A KMAC handle, may be uninitialized.
128 * @return The result of the operation.
129 */
131status_t keymgr_testutils_initialize(dif_keymgr_t *keymgr, dif_kmac_t *kmac);
132
133/**
134 * Wrapper function to `keymgr_testutils_startup()`.
135 *
136 * This function checks the state of the key manager before attempting to
137 * initialize its dependencies and state.
138 *
139 * The function will return an error if the keymgr is disabled or in invalid
140 * state.
141 *
142 * @param keymgr A key manager handle, may be uninitialized.
143 * @param kmac A KMAC handle, may be uninitialized.
144 * @param[out] keymgr_state The state of the keymgr after startup.
145 */
147status_t keymgr_testutils_try_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac,
148 dif_keymgr_state_t *keymgr_state);
149
150/**
151 * Initialize non-volatile memory (flash and OTP) for keymgr and then reset, so
152 * that the relevant OTP partitions become accessible to keymgr. After calling
153 * this function, keymgr can be initialized.
154 */
156status_t keymgr_testutils_init_nvm_then_reset(void);
157
158/**
159 * Programs flash, restarts, and advances keymgr to CreatorRootKey state. Note
160 * that this function assumes that the key manager is in the initial reset state
161 * after ROM execution.
162 *
163 * This procedure essentially gets the keymgr into the first state where it can
164 * be used for tests. Tests should call it before anything else, like below:
165 *
166 * void test_main(void) {
167 * // Set up and advance to CreatorRootKey state.
168 * dif_keymgr_t keymgr;
169 * dif_kmac_t kmac;
170 * keymgr_testutils_startup(&keymgr, &kmac);
171 *
172 * // Remainder of test; optionally advance to OwnerIntKey state, generate
173 * // keys and identities.
174 * ...
175 * }
176 *
177 * Because the key manager uses KMAC, this procedure also initializes and
178 * configures KMAC. Software should not rely on the configuration here and
179 * should reconfigure KMAC if needed. The purpose of configuring KMAC in this
180 * procedure is so that the key manager will not use KMAC with the default
181 * entropy settings.
182 *
183 * @param keymgr A key manager handle, may be uninitialized.
184 * @param kmac A KMAC handle, may be uninitialized.
185 */
187status_t keymgr_testutils_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac);
188
189/**
190 * Issues a keymgr advance operation and wait for it to complete
191 *
192 * @param keymgr A key manager handle.
193 * @param params The binding and max key version value for the next state.
194 */
196status_t keymgr_testutils_advance_state(
197 const dif_keymgr_t *keymgr, const dif_keymgr_state_params_t *params);
198
199/**
200 * Checks if the current keymgr state matches the expected state
201 *
202 * @param keymgr A key manager handle.
203 * @param exp_state The expected key manager state.
204 */
206status_t keymgr_testutils_check_state(const dif_keymgr_t *keymgr,
207 const dif_keymgr_state_t exp_state);
208
209/**
210 * Issues a keymgr identity generation and wait for it to complete
211 *
212 * @param keymgr A key manager handle.
213 */
215status_t keymgr_testutils_generate_identity(
216 const dif_keymgr_t *keymgr, const dif_keymgr_identity_seed_params_t params);
217
218/**
219 * Issues a keymgr HW/SW versioned key generation and wait for it to complete
220 *
221 * @param keymgr A key manager handle.
222 * @param params Key generation parameters.
223 */
225status_t keymgr_testutils_generate_versioned_key(
226 const dif_keymgr_t *keymgr, const dif_keymgr_versioned_key_params_t params);
227
228/**
229 * Issues a keymgr disable and wait for it to complete
230 */
232status_t keymgr_testutils_disable(const dif_keymgr_t *keymgr);
233
234/**
235 * Polling keymgr status until it becomes idle.
236 * Fail the test if the status code indicates any error.
237 *
238 * @param keymgr A key manager handle.
239 */
241status_t keymgr_testutils_wait_for_operation_done(const dif_keymgr_t *keymgr);
242
243/**
244 * Get the maximum key version supported by the key manager.
245 *
246 * @param keymgr A key manager handle.
247 * @param[out] max_key_version The maximum key version supported by the key
248 * manager for its current operating state.
249 * @return The result of the operation.
250 */
252status_t keymgr_testutils_max_key_version_get(const dif_keymgr_t *keymgr,
253 uint32_t *max_key_version);
254
255/**
256 * Get the current state of the key manager.
257 *
258 * @param keymgr A key manager handle.
259 * @param[out] state The current state of the key manager in C string format.
260 *
261 * @return The result of the operation.
262 */
264status_t keymgr_testutils_state_string_get(const dif_keymgr_t *keymgr,
265 const char **stage_name);
266
267#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_KEYMGR_TESTUTILS_H_