Software APIs
dif_keymgr_dpe.h
Go to the documentation of this file.
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_DIF_DIF_KEYMGR_DPE_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_KEYMGR_DPE_H_
7
8/**
9 * @file
10 * @brief <a href="/book/hw/ip/keymgr_dpe/">Key Manager DPE</a> Device Interface
11 * Functions
12 */
13
14#include <stdint.h>
15
19
20#include "sw/device/lib/dif/autogen/dif_keymgr_dpe_autogen.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif // __cplusplus
25
26/**
27 * SW-visible key manager DPE states.
28 *
29 * Key manager RTL has more than 4 finite state machine (FSM) states, but it
30 * simply truncates the reported state into four states given below. The reason
31 * behind this truncation is that FSM lingers on some states temporarily (i.e.
32 * few clock cycles) and the transition into the next state does not require
33 * further invocation.
34 *
35 * From SW point of view, key manager FSM transitions follow a sequence
36 * sequential manner and these transitions are irreversible until a power cycle.
37 */
39 kDifKeymgrDpeStateReset = 0,
40 kDifKeymgrDpeStateAvailable = 1,
41 kDifKeymgrDpeStateDisabled = 2,
42 kDifKeymgrDpeStateInvalid = 3
44
45/**
46 * Enumeration for side load slot clearing.
47 */
49 kDifKeymgrDpeSideLoadClearNone = 0,
50 kDifKeymgrDpeSideLoadClearAes = 1,
51 kDifKeymgrDpeSideLoadClearKmac = 2,
52 kDifKeymgrDpeSideLoadClearOtbn = 3,
53 kDifKeymgrDpeSideLoadClearHmac = 4,
54 // Using different value than those enumerated above should clear all slots,
55 // so we can use the mask value of this field to denote ALL case.
56 kDifKeymgrDpeSideLoadClearAll = 7,
58
59/**
60 * Runtime configuration for keymgr dpe.
61 */
62typedef struct dif_keymgr_dpe_config {
63 /**
64 * Number of keymgr_dpe cycles before the entropy is reseeded.
65 *
66 * Keymgr dpe uses random values generated by the entropy source for
67 * initializing its state and clearing sideload keys. This value determines
68 * the frequency at which this random value is updated.
69 */
72
73/**
74 * Engine used by keymgr dpe to perform key derivation.
75 */
77 /**
78 * Use KMAC as KDF engine (default after reset).
79 */
81 /**
82 * Use OTBN as KDF engine (HKDF).
83 */
86
87/**
88 * Input parameters for advancing a DPE context/slot.
89 */
91 /**
92 * This value is used by key manager as input to DICE computation and can be
93 * either a value that represents the measurement of a boot stage or simply a
94 * tag from a manifest.
95 */
96 uint32_t binding_value[8];
97
98 /**
99 * Maximum allowed version for keys to be generated at a state. This value is
100 * stored inside keymgr slot so that it can later be compared against the
101 * `key_version` input provided along with generation request.
102 */
104
105 /**
106 * The source slot to be used as parent DPE context.
107 */
108 uint32_t slot_src_sel;
109
110 /**
111 * The destination slot which recieves the derived child DPE context.
112 */
113 uint32_t slot_dst_sel;
114
115 /**
116 * The slot policy bits for the derived child DPE context.
117 */
118 uint32_t slot_policy;
120
121/**
122 * Output metadata from one HW slot
123 */
125 /**
126 * Maximum allowed version for keys to be generated from this DPE context
127 */
129
130 /**
131 * The current boot_stage of this DPE context.
132 */
133 uint32_t boot_stage;
134
135 /**
136 * The current slot policy bits for this DPE context, using the same bit
137 * layout as the `SLOT_POLICY` register.
138 */
139 uint32_t slot_policy;
140
141 /**
142 * Validity of this DPE context.
143 */
144 uint32_t valid;
146
147/**
148 * Key destination of a versioned key generation operation.
149 *
150 * Regardless of whether the generated key is SW or sideload key, HW uses a
151 * unique diversification constant for each cryptographic use case. In the case
152 * of sideload key, this enum value is also used to determine the target
153 * peripheral port to which the generated key is loaded.
154 */
156 /**
157 * Diversify the generated key for no HW IP (and don't sideload it).
158 */
160 /**
161 * Diversify the generated key for AES (and load it to AES peripheral port if
162 * sideload key).
163 */
165 /**
166 * Diversify the generated key for KMAC (and load it to KMAC peripheral port
167 * if sideload key).
168 */
170 /**
171 * Diversify the generated key for OTBN (and load it to OTBN peripheral port
172 * if sideload key).
173 */
175 /**
176 * Diversify the generated key for HMAC (and load it to HMAC peripheral port
177 * if sideload key).
178 */
181
182/**
183 * Input parameters for advancing a DPE context/slot.
184 */
186 /**
187 * Destination for {AES, KMAC, OTBN, HMAC}, which is used for diversification.
188 */
190
191 /**
192 * Set to true, if this is a sideload key, otherwise set to false.
193 */
195
196 /**
197 * Salt value used as input for key generation (i.e. becomes part of the
198 * message payload sent to KMAC during computation).
199 */
200 uint32_t salt[8];
201
202 /**
203 * The key version used for generating versioned key. This value should not be
204 * greater than the `max_key_version` value stored inside the source slot that
205 * is used to generate the key.
206 */
207 uint32_t version;
208
209 /**
210 * The source slot from which the key is derived.
211 */
212 uint32_t slot_src_sel;
214
215/**
216 * Input parameters for erasing a DPE context/slot.
217 */
219 /**
220 * Index for the slot to be erased.
221 */
222 uint32_t slot_dst_sel;
224
225/**
226 * Useed to represent the output of SW generated key.
227 */
228typedef struct dif_keymgr_dpe_output {
229 uint32_t value[2][12];
231
232/**
233 * Status code bit flags.
234 *
235 * See also: `dif_keymgr_dpe_status_codes_t`.
236 */
238 /**
239 * Key manager is idle.
240 */
242 /**
243 * Software invoked an invalid operation.
244 */
246 /**
247 * Key manager issued invalid data to KMAC interface.
248 */
250 /**
251 * Key manager encountered invalid state.
252 */
254
256
257/**
258 * Define mask for error fields of `dif_keymgr_dpe_status_code_t`.
259 */
260static const bitfield_field32_t kIdleBitfield = (bitfield_field32_t){
261 .mask = 0x1,
262 .index = 0,
263};
264
265/**
266 * Define mask for idle field of `dif_keymgr_dpe_status_code_t`.
267 */
268static const bitfield_field32_t kErrorBitfield = (bitfield_field32_t){
269 .mask = 0x7,
270 .index = 1,
271};
272
273/**
274 * A bit vector of status codes.
275 *
276 * The following snippet can be used to check if key manager is idle:
277 *
278 * `bool is_idle = (status_codes & kDifKeymgrDpeStatusCodeIdle);`
279 *
280 * The following snippet can be used to check if key manager is idle and
281 * error-free:
282 *
283 * `bool is_idle_and_ok = (status_codes == kDifKeymgrDpeStatusCodeIdle);`
284 *
285 * See also: `dif_keymgr_dpe_status_code_t`.
286 */
288
289/**
290 * Initializes the keymgr_pde block by performing an advance operation.
291 *
292 * The hardware does not have an explicit initialize command. Initialization is
293 * simple the first advance call without software binding, max version or
294 * policy registers set. Use this call before calling
295 * `dif_keymgr_dpe_advance_state()`.
296 *
297 * @param keymgr_dpe A key manager handle.
298 * @param slot_dst_sel Target slot used to latch the UDS key.
299 * @return The result of the operation.
300 */
302 uint32_t slot_dst_sel);
303
304/**
305 * Advances a keymgr_dpe slot with given parameters.
306 *
307 * @param keymgr_dpe A key manager handle.
308 * @param params Struct to pass inputs consumed by HW during advance.
309 * @return The result of the operation.
310 */
313 const dif_keymgr_dpe_t *keymgr_dpe,
314 const dif_keymgr_dpe_advance_params_t *params);
315
316/**
317 * Erases a given keymgr_dpe slot.
318 *
319 * @param keymgr_dpe A key manager handle.
320 * @param params A struct that selects the slot to be erased.
321 * @return The result of the operation.
322 */
325 const dif_keymgr_dpe_t *keymgr_dpe,
326 const dif_keymgr_dpe_erase_params_t *params);
327
328/**
329 * Reads back the metadata of a keymgr_dpe HW slot.
330 *
331 * @param keymgr_dpe A key manager handle.
332 * @param slot Index of the HW slot to read metadata from.
333 * @param[out] metadata The maximum key version, boot stage, slot policy and
334 * validity of the DPE context held in `slot`.
335 * @return The result of the operation.
336 */
339 uint32_t slot,
340 dif_keymgr_dpe_metadata_t *metadata);
341
342/**
343 * Disables key manager dpe.
344 *
345 * This function disables keymgr dpe until the next power cycle by making
346 * it transition to the "disabled" state. The "disabled" state is a terminal
347 * state where the keymgr dpe is no longer operational and its secret values
348 * are wiped.
349 *
350 * @param keymgr_dpe A key manager handle.
351 * @return The result of the operation.
352 */
355
356/**
357 * Generate a SW/HW key from a chosen keymgr_dpe slot.
358 *
359 * @param keymgr_dpe A key manager handle.
360 * @param params Struct to pass inputs consumed by HW generate operation.
361 * @return The result of the operation.
362 */
365 const dif_keymgr_dpe_t *keymgr_dpe,
367
368/**
369 * Gets the operational status of keymgr_dpe.
370 *
371 * This function also clears OP_STATUS and ERR_CODE registers after reading
372 * them.
373 *
374 * @param keymgr_dpe A key manager handle.
375 * @param[out] status_codes Out-param for key manager status codes.
376 * @return The result of the operation.
377 */
380 const dif_keymgr_dpe_t *keymgr_dpe,
381 dif_keymgr_dpe_status_codes_t *status_codes);
382
383/**
384 * Gets the current state of key manager.
385 *
386 * @param keymgr_dpe A key manager handle.
387 * @param[out] state Out-param for current key manager state.
388 * @return The result of the operation.
389 */
393
394/**
395 * Read the value of SW generated key from its related CSR. It is the
396 * responsibility of the caller to check that key generation has completed.
397 *
398 * @param keymgr_dpe A key manager handle.
399 * @param[out] output The key value in two shares.
400 * @return The result of the operation.
401 */
405
406/**
407 * Starts or stops clearing of sideload keys.
408 *
409 * Selecting a sideload destination other than `kDifKeymgrDpeSideLoadClearNone`
410 * causes keymgr_dpe to continuously overwrite the corresponding sideload key
411 * port with randomness (Destination can either be AES, KMAC or OTBN).
412 * Any value that is not one of the enumerated destinations clears all sideload
413 * key ports. To resume normal operation, call this function again with
414 * `kDifKeymgrDpeSideLoadClearNone` to stop the clearing.
415 *
416 * @param keymgr_dpe A key manager handle.
417 * @param clear_dest Target sideload key.
418 * @return The result of the operation.
419 */
422 const dif_keymgr_dpe_t *keymgr_dpe,
424
425/**
426 * Runtime configuration for keymgr dpe.
427 *
428 * @param keymgr_dpe A key manager handle.
429 * @param config configuration for the keymgr dpe.
430 * @return The result of the operation.
431 */
435
436/**
437 * Selects the engine used for key derivation.
438 *
439 * The selection only has an effect if the hardware is instantiated with
440 * `KdfEngineSel = KdfEngineBoth`, otherwise the selection is ignored.
441 *
442 * The hardware only takes over the new selection when no operation is
443 * ongoing, i.e. the selection can change during an operation but the change
444 * won't take effect until the operation has finished.
445 *
446 * @param keymgr_dpe A key manager handle.
447 * @param engine The KDF engine to be used.
448 * @return The result of the operation, `kDifLocked` if the selection is
449 * locked.
450 */
454
455/**
456 * Locks the KDF engine selection until the next reset.
457 *
458 * @param keymgr_dpe A key manager handle.
459 * @return The result of the operation.
460 */
463
464#ifdef __cplusplus
465} // extern "C"
466#endif // __cplusplus
467
468#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_KEYMGR_DPE_H_