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
16
#include "
sw/device/lib/base/macros.h
"
17
#include "
sw/device/lib/base/mmio.h
"
18
#include "
sw/device/lib/dif/dif_base.h
"
19
20
#include "sw/device/lib/dif/autogen/dif_keymgr_dpe_autogen.h"
21
22
#ifdef __cplusplus
23
extern
"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
*/
38
typedef
enum
dif_keymgr_dpe_state
{
39
kDifKeymgrDpeStateReset = 0,
40
kDifKeymgrDpeStateAvailable = 1,
41
kDifKeymgrDpeStateDisabled = 2,
42
kDifKeymgrDpeStateInvalid = 3
43
}
dif_keymgr_dpe_state_t
;
44
45
/**
46
* Enumeration for side load slot clearing.
47
*/
48
typedef
enum
dif_keymgr_sideload_clr
{
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,
57
}
dif_keymgr_dpe_sideload_clr_t
;
58
59
/**
60
* Runtime configuration for keymgr dpe.
61
*/
62
typedef
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
*/
70
uint16_t
entropy_reseed_interval
;
71
}
dif_keymgr_dpe_config_t
;
72
73
/**
74
* Engine used by keymgr dpe to perform key derivation.
75
*/
76
typedef
enum
dif_keymgr_dpe_kdf_engine
{
77
/**
78
* Use KMAC as KDF engine (default after reset).
79
*/
80
kDifKeymgrDpeKdfEngineKmac
= 0,
81
/**
82
* Use OTBN as KDF engine (HKDF).
83
*/
84
kDifKeymgrDpeKdfEngineOtbn
= 1,
85
}
dif_keymgr_dpe_kdf_engine_t
;
86
87
/**
88
* Input parameters for advancing a DPE context/slot.
89
*/
90
typedef
struct
dif_keymgr_dpe_advance_params
{
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
*/
103
uint32_t
max_key_version
;
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
;
119
}
dif_keymgr_dpe_advance_params_t
;
120
121
/**
122
* Output metadata from one HW slot
123
*/
124
typedef
struct
dif_keymgr_dpe_metadata
{
125
/**
126
* Maximum allowed version for keys to be generated from this DPE context
127
*/
128
uint32_t
max_key_version
;
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
;
145
}
dif_keymgr_dpe_metadata_t
;
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
*/
155
typedef
enum
dif_keymgr_dpe_key_dest
{
156
/**
157
* Diversify the generated key for no HW IP (and don't sideload it).
158
*/
159
kDifKeymgrDpeKeyDestNone
= 0,
160
/**
161
* Diversify the generated key for AES (and load it to AES peripheral port if
162
* sideload key).
163
*/
164
kDifKeymgrDpeKeyDestAes
= 1,
165
/**
166
* Diversify the generated key for KMAC (and load it to KMAC peripheral port
167
* if sideload key).
168
*/
169
kDifKeymgrDpeKeyDestKmac
= 2,
170
/**
171
* Diversify the generated key for OTBN (and load it to OTBN peripheral port
172
* if sideload key).
173
*/
174
kDifKeymgrDpeKeyDestOtbn
= 3,
175
/**
176
* Diversify the generated key for HMAC (and load it to HMAC peripheral port
177
* if sideload key).
178
*/
179
kDifKeymgrDpeKeyDestHmac
= 4,
180
}
dif_keymgr_dpe_key_dest_t
;
181
182
/**
183
* Input parameters for advancing a DPE context/slot.
184
*/
185
typedef
struct
dif_keymgr_dpe_generate_params
{
186
/**
187
* Destination for {AES, KMAC, OTBN, HMAC}, which is used for diversification.
188
*/
189
dif_keymgr_dpe_key_dest_t
key_dest
;
190
191
/**
192
* Set to true, if this is a sideload key, otherwise set to false.
193
*/
194
bool
sideload_key
;
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
;
213
}
dif_keymgr_dpe_generate_params_t
;
214
215
/**
216
* Input parameters for erasing a DPE context/slot.
217
*/
218
typedef
struct
dif_keymgr_dpe_erase_params
{
219
/**
220
* Index for the slot to be erased.
221
*/
222
uint32_t
slot_dst_sel
;
223
}
dif_keymgr_dpe_erase_params_t
;
224
225
/**
226
* Useed to represent the output of SW generated key.
227
*/
228
typedef
struct
dif_keymgr_dpe_output
{
229
uint32_t value[2][12];
230
}
dif_keymgr_dpe_output_t
;
231
232
/**
233
* Status code bit flags.
234
*
235
* See also: `dif_keymgr_dpe_status_codes_t`.
236
*/
237
typedef
enum
dif_keymgr_dpe_status_code
{
238
/**
239
* Key manager is idle.
240
*/
241
kDifKeymgrDpeStatusCodeIdle
= 1 << 0,
242
/**
243
* Software invoked an invalid operation.
244
*/
245
kDifKeymgrDpeStatusCodeInvalidOperation
= 1 << 1,
246
/**
247
* Key manager issued invalid data to KMAC interface.
248
*/
249
kDifKeymgrDpeStatusCodeInvalidKmacInput
= 1 << 2,
250
/**
251
* Key manager encountered invalid state.
252
*/
253
kDifKeymgrDpeStatusCodeInvalidState
= 1 << 3,
254
255
}
dif_keymgr_dpe_status_code_t
;
256
257
/**
258
* Define mask for error fields of `dif_keymgr_dpe_status_code_t`.
259
*/
260
static
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
*/
268
static
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
*/
287
typedef
uint8_t
dif_keymgr_dpe_status_codes_t
;
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
*/
301
dif_result_t
dif_keymgr_dpe_initialize
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
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
*/
311
OT_WARN_UNUSED_RESULT
312
dif_result_t
dif_keymgr_dpe_advance_state
(
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
*/
323
OT_WARN_UNUSED_RESULT
324
dif_result_t
dif_keymgr_dpe_erase_slot
(
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
*/
337
OT_WARN_UNUSED_RESULT
338
dif_result_t
dif_keymgr_dpe_get_metadata
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
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
*/
353
OT_WARN_UNUSED_RESULT
354
dif_result_t
dif_keymgr_dpe_disable
(
const
dif_keymgr_dpe_t
*keymgr_dpe);
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
*/
363
OT_WARN_UNUSED_RESULT
364
dif_result_t
dif_keymgr_dpe_generate
(
365
const
dif_keymgr_dpe_t
*keymgr_dpe,
366
const
dif_keymgr_dpe_generate_params_t
*params);
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
*/
378
OT_WARN_UNUSED_RESULT
379
dif_result_t
dif_keymgr_dpe_get_status_codes
(
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
*/
390
OT_WARN_UNUSED_RESULT
391
dif_result_t
dif_keymgr_dpe_get_state
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
392
dif_keymgr_dpe_state_t
*state);
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
*/
402
OT_WARN_UNUSED_RESULT
403
dif_result_t
dif_keymgr_dpe_read_output
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
404
dif_keymgr_dpe_output_t
*output);
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
*/
420
OT_WARN_UNUSED_RESULT
421
dif_result_t
dif_keymgr_dpe_clear_sideload_key
(
422
const
dif_keymgr_dpe_t
*keymgr_dpe,
423
dif_keymgr_dpe_sideload_clr_t
clear_dest);
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
*/
432
OT_WARN_UNUSED_RESULT
433
dif_result_t
dif_keymgr_dpe_configure
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
434
dif_keymgr_dpe_config_t
config);
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
*/
451
OT_WARN_UNUSED_RESULT
452
dif_result_t
dif_keymgr_dpe_set_kdf_engine
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
453
dif_keymgr_dpe_kdf_engine_t
engine);
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
*/
461
OT_WARN_UNUSED_RESULT
462
dif_result_t
dif_keymgr_dpe_lock_kdf_engine
(
const
dif_keymgr_dpe_t
*keymgr_dpe);
463
464
#ifdef __cplusplus
465
}
// extern "C"
466
#endif
// __cplusplus
467
468
#endif
// OPENTITAN_SW_DEVICE_LIB_DIF_DIF_KEYMGR_DPE_H_
sw
device
lib
dif
dif_keymgr_dpe.h
Return to
OpenTitan Documentation