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
* Input parameters for advancing a DPE context/slot.
75
*/
76
typedef
struct
dif_keymgr_dpe_advance_params
{
77
/**
78
* This value is used by key manager as input to DICE computation and can be
79
* either a value that represents the measurement of a boot stage or simply a
80
* tag from a manifest.
81
*/
82
uint32_t
binding_value
[8];
83
84
/**
85
* Maximum allowed version for keys to be generated at a state. This value is
86
* stored inside keymgr slot so that it can later be compared against the
87
* `key_version` input provided along with generation request.
88
*/
89
uint32_t
max_key_version
;
90
91
/**
92
* The source slot to be used as parent DPE context.
93
*/
94
uint32_t
slot_src_sel
;
95
96
/**
97
* The destination slot which recieves the derived child DPE context.
98
*/
99
uint32_t
slot_dst_sel
;
100
101
/**
102
* The slot policy bits for the derived child DPE context.
103
*/
104
uint32_t
slot_policy
;
105
}
dif_keymgr_dpe_advance_params_t
;
106
107
/**
108
* Output metadata from one HW slot
109
*/
110
typedef
struct
dif_keymgr_dpe_metadata
{
111
/**
112
* Maximum allowed version for keys to be generated from this DPE context
113
*/
114
uint32_t
max_key_version
;
115
116
/**
117
* The current boot_stage of this DPE context.
118
*/
119
uint32_t
boot_stage
;
120
121
/**
122
* The current slot policy bits for this DPE context, using the same bit
123
* layout as the `SLOT_POLICY` register.
124
*/
125
uint32_t
slot_policy
;
126
127
/**
128
* Validity of this DPE context.
129
*/
130
uint32_t
valid
;
131
}
dif_keymgr_dpe_metadata_t
;
132
133
/**
134
* Key destination of a versioned key generation operation.
135
*
136
* Regardless of whether the generated key is SW or sideload key, HW uses a
137
* unique diversification constant for each cryptographic use case. In the case
138
* of sideload key, this enum value is also used to determine the target
139
* peripheral port to which the generated key is loaded.
140
*/
141
typedef
enum
dif_keymgr_dpe_key_dest
{
142
/**
143
* Diversify the generated key for no HW IP (and don't sideload it).
144
*/
145
kDifKeymgrDpeKeyDestNone
= 0,
146
/**
147
* Diversify the generated key for AES (and load it to AES peripheral port if
148
* sideload key).
149
*/
150
kDifKeymgrDpeKeyDestAes
= 1,
151
/**
152
* Diversify the generated key for KMAC (and load it to KMAC peripheral port
153
* if sideload key).
154
*/
155
kDifKeymgrDpeKeyDestKmac
= 2,
156
/**
157
* Diversify the generated key for OTBN (and load it to OTBN peripheral port
158
* if sideload key).
159
*/
160
kDifKeymgrDpeKeyDestOtbn
= 3,
161
/**
162
* Diversify the generated key for HMAC (and load it to HMAC peripheral port
163
* if sideload key).
164
*/
165
kDifKeymgrDpeKeyDestHmac
= 4,
166
}
dif_keymgr_dpe_key_dest_t
;
167
168
/**
169
* Input parameters for advancing a DPE context/slot.
170
*/
171
typedef
struct
dif_keymgr_dpe_generate_params
{
172
/**
173
* Destination for {AES, KMAC, OTBN, HMAC}, which is used for diversification.
174
*/
175
dif_keymgr_dpe_key_dest_t
key_dest
;
176
177
/**
178
* Set to true, if this is a sideload key, otherwise set to false.
179
*/
180
bool
sideload_key
;
181
182
/**
183
* Salt value used as input for key generation (i.e. becomes part of the
184
* message payload sent to KMAC during computation).
185
*/
186
uint32_t
salt
[8];
187
188
/**
189
* The key version used for generating versioned key. This value should not be
190
* greater than the `max_key_version` value stored inside the source slot that
191
* is used to generate the key.
192
*/
193
uint32_t
version
;
194
195
/**
196
* The source slot from which the key is derived.
197
*/
198
uint32_t
slot_src_sel
;
199
}
dif_keymgr_dpe_generate_params_t
;
200
201
/**
202
* Input parameters for erasing a DPE context/slot.
203
*/
204
typedef
struct
dif_keymgr_dpe_erase_params
{
205
/**
206
* Index for the slot to be erased.
207
*/
208
uint32_t
slot_dst_sel
;
209
}
dif_keymgr_dpe_erase_params_t
;
210
211
/**
212
* Useed to represent the output of SW generated key.
213
*/
214
typedef
struct
dif_keymgr_dpe_output
{
215
uint32_t value[2][8];
216
}
dif_keymgr_dpe_output_t
;
217
218
/**
219
* Status code bit flags.
220
*
221
* See also: `dif_keymgr_dpe_status_codes_t`.
222
*/
223
typedef
enum
dif_keymgr_dpe_status_code
{
224
/**
225
* Key manager is idle.
226
*/
227
kDifKeymgrDpeStatusCodeIdle
= 1 << 0,
228
/**
229
* Software invoked an invalid operation.
230
*/
231
kDifKeymgrDpeStatusCodeInvalidOperation
= 1 << 1,
232
/**
233
* Key manager issued invalid data to KMAC interface.
234
*/
235
kDifKeymgrDpeStatusCodeInvalidKmacInput
= 1 << 2,
236
/**
237
* Key manager encountered invalid state.
238
*/
239
kDifKeymgrDpeStatusCodeInvalidState
= 1 << 3,
240
241
}
dif_keymgr_dpe_status_code_t
;
242
243
/**
244
* Define mask for error fields of `dif_keymgr_dpe_status_code_t`.
245
*/
246
static
const
bitfield_field32_t
kIdleBitfield = (
bitfield_field32_t
){
247
.mask = 0x1,
248
.index = 0,
249
};
250
251
/**
252
* Define mask for idle field of `dif_keymgr_dpe_status_code_t`.
253
*/
254
static
const
bitfield_field32_t
kErrorBitfield = (
bitfield_field32_t
){
255
.mask = 0x7,
256
.index = 1,
257
};
258
259
/**
260
* A bit vector of status codes.
261
*
262
* The following snippet can be used to check if key manager is idle:
263
*
264
* `bool is_idle = (status_codes & kDifKeymgrDpeStatusCodeIdle);`
265
*
266
* The following snippet can be used to check if key manager is idle and
267
* error-free:
268
*
269
* `bool is_idle_and_ok = (status_codes == kDifKeymgrDpeStatusCodeIdle);`
270
*
271
* See also: `dif_keymgr_dpe_status_code_t`.
272
*/
273
typedef
uint8_t
dif_keymgr_dpe_status_codes_t
;
274
275
/**
276
* Initializes the keymgr_pde block by performing an advance operation.
277
*
278
* The hardware does not have an explicit initialize command. Initialization is
279
* simple the first advance call without software binding, max version or
280
* policy registers set. Use this call before calling
281
* `dif_keymgr_dpe_advance_state()`.
282
*
283
* @param keymgr_dpe A key manager handle.
284
* @param slot_dst_sel Target slot used to latch the UDS key.
285
* @return The result of the operation.
286
*/
287
dif_result_t
dif_keymgr_dpe_initialize
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
288
uint32_t slot_dst_sel);
289
290
/**
291
* Advances a keymgr_dpe slot with given parameters.
292
*
293
* @param keymgr_dpe A key manager handle.
294
* @param params Struct to pass inputs consumed by HW during advance.
295
* @return The result of the operation.
296
*/
297
OT_WARN_UNUSED_RESULT
298
dif_result_t
dif_keymgr_dpe_advance_state
(
299
const
dif_keymgr_dpe_t
*keymgr_dpe,
300
const
dif_keymgr_dpe_advance_params_t
*params);
301
302
/**
303
* Erases a given keymgr_dpe slot.
304
*
305
* @param keymgr_dpe A key manager handle.
306
* @param params A struct that selects the slot to be erased.
307
* @return The result of the operation.
308
*/
309
OT_WARN_UNUSED_RESULT
310
dif_result_t
dif_keymgr_dpe_erase_slot
(
311
const
dif_keymgr_dpe_t
*keymgr_dpe,
312
const
dif_keymgr_dpe_erase_params_t
*params);
313
314
/**
315
* Reads back the metadata of a keymgr_dpe HW slot.
316
*
317
* @param keymgr_dpe A key manager handle.
318
* @param slot Index of the HW slot to read metadata from.
319
* @param[out] metadata The maximum key version, boot stage, slot policy and
320
* validity of the DPE context held in `slot`.
321
* @return The result of the operation.
322
*/
323
OT_WARN_UNUSED_RESULT
324
dif_result_t
dif_keymgr_dpe_get_metadata
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
325
uint32_t slot,
326
dif_keymgr_dpe_metadata_t
*metadata);
327
328
/**
329
* Disables key manager dpe.
330
*
331
* This function disables keymgr dpe until the next power cycle by making
332
* it transition to the "disabled" state. The "disabled" state is a terminal
333
* state where the keymgr dpe is no longer operational and its secret values
334
* are wiped.
335
*
336
* @param keymgr_dpe A key manager handle.
337
* @return The result of the operation.
338
*/
339
OT_WARN_UNUSED_RESULT
340
dif_result_t
dif_keymgr_dpe_disable
(
const
dif_keymgr_dpe_t
*keymgr_dpe);
341
342
/**
343
* Generate a SW/HW key from a chosen keymgr_dpe slot.
344
*
345
* @param keymgr_dpe A key manager handle.
346
* @param params Struct to pass inputs consumed by HW generate operation.
347
* @return The result of the operation.
348
*/
349
OT_WARN_UNUSED_RESULT
350
dif_result_t
dif_keymgr_dpe_generate
(
351
const
dif_keymgr_dpe_t
*keymgr_dpe,
352
const
dif_keymgr_dpe_generate_params_t
*params);
353
354
/**
355
* Gets the operational status of keymgr_dpe.
356
*
357
* This function also clears OP_STATUS and ERR_CODE registers after reading
358
* them.
359
*
360
* @param keymgr_dpe A key manager handle.
361
* @param[out] status_codes Out-param for key manager status codes.
362
* @return The result of the operation.
363
*/
364
OT_WARN_UNUSED_RESULT
365
dif_result_t
dif_keymgr_dpe_get_status_codes
(
366
const
dif_keymgr_dpe_t
*keymgr_dpe,
367
dif_keymgr_dpe_status_codes_t
*status_codes);
368
369
/**
370
* Gets the current state of key manager.
371
*
372
* @param keymgr_dpe A key manager handle.
373
* @param[out] state Out-param for current key manager state.
374
* @return The result of the operation.
375
*/
376
OT_WARN_UNUSED_RESULT
377
dif_result_t
dif_keymgr_dpe_get_state
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
378
dif_keymgr_dpe_state_t
*state);
379
380
/**
381
* Read the value of SW generated key from its related CSR. It is the
382
* responsibility of the caller to check that key generation has completed.
383
*
384
* @param keymgr_dpe A key manager handle.
385
* @param[out] output The key value in two shares.
386
* @return The result of the operation.
387
*/
388
OT_WARN_UNUSED_RESULT
389
dif_result_t
dif_keymgr_dpe_read_output
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
390
dif_keymgr_dpe_output_t
*output);
391
392
/**
393
* Starts or stops clearing of sideload keys.
394
*
395
* Selecting a sideload destination other than `kDifKeymgrDpeSideLoadClearNone`
396
* causes keymgr_dpe to continuously overwrite the corresponding sideload key
397
* port with randomness (Destination can either be AES, KMAC or OTBN).
398
* Any value that is not one of the enumerated destinations clears all sideload
399
* key ports. To resume normal operation, call this function again with
400
* `kDifKeymgrDpeSideLoadClearNone` to stop the clearing.
401
*
402
* @param keymgr_dpe A key manager handle.
403
* @param clear_dest Target sideload key.
404
* @return The result of the operation.
405
*/
406
OT_WARN_UNUSED_RESULT
407
dif_result_t
dif_keymgr_dpe_clear_sideload_key
(
408
const
dif_keymgr_dpe_t
*keymgr_dpe,
409
dif_keymgr_dpe_sideload_clr_t
clear_dest);
410
411
/**
412
* Runtime configuration for keymgr dpe.
413
*
414
* @param keymgr_dpe A key manager handle.
415
* @param config configuration for the keymgr dpe.
416
* @return The result of the operation.
417
*/
418
OT_WARN_UNUSED_RESULT
419
dif_result_t
dif_keymgr_dpe_configure
(
const
dif_keymgr_dpe_t
*keymgr_dpe,
420
dif_keymgr_dpe_config_t
config);
421
422
#ifdef __cplusplus
423
}
// extern "C"
424
#endif
// __cplusplus
425
426
#endif
// OPENTITAN_SW_DEVICE_LIB_DIF_DIF_KEYMGR_DPE_H_
sw
device
lib
dif
dif_keymgr_dpe.h
Return to
OpenTitan Documentation