blob: cf16a7da0a96ce53dd12eb3ab0956f7eb3a9d5ca [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __DPE_CONTEXT_MNGR_H__
9#define __DPE_CONTEXT_MNGR_H__
10
11#include <stddef.h>
12#include <stdint.h>
13#include <stdbool.h>
14#include "dice_protection_environment.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_config.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016
17#ifdef __cplusplus
18extern "C" {
19#endif
20
Maulik Patel2358bbb2023-07-21 10:56:56 +010021#define DICE_CERT_SIZE 3072
Maulik Patel83a6b592023-12-05 15:20:30 +000022#define DICE_CERT_CHAIN_SIZE 3200
Maulik Patel9fd8bd22023-10-30 10:58:30 +000023/* Below encoded CDI size accomodate both Attest and Seal CDI */
24#define DICE_MAX_ENCODED_CDI_SIZE ((2 * DICE_CDI_SIZE) + 16)
Maulik Patelad2f3db2023-05-17 15:41:36 +010025
26#define INVALID_HANDLE 0xFFFFFFFF
27#define INVALID_COMPONENT_IDX 0xFFFF
28#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patelad2f3db2023-05-17 15:41:36 +010029#define INVALID_LAYER_IDX 65535
Maulik Patel54d65f72023-06-28 13:04:36 +010030#define DPE_ROT_LAYER_IDX 0
31
32/* Below configuration defines are platform dependant */
33#define MAX_NUM_OF_COMPONENTS 30
Maulik Patel2358bbb2023-07-21 10:56:56 +010034#define MAX_NUM_OF_LAYERS 6
Maulik Patel54d65f72023-06-28 13:04:36 +010035#define DPE_PLATFORM_LAYER_IDX 1
36#define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2
37/* Below threshold defines the threshold below which a context cannot be destroyed */
38#define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010039
40/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
41#define GET_IDX(handle) ((handle) & 0xffff)
42#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
43
44#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
45#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
46
47struct component_context_data_t {
48 uint8_t measurement_value[DICE_HASH_SIZE];
49 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
50 size_t measurement_descriptor_size;
51 uint8_t signer_id[DICE_HASH_SIZE];
52 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
53 size_t signer_id_descriptor_size;
54 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
55 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
56 size_t config_descriptor_size;
57 DiceMode mode;
58 uint8_t hidden[DICE_HIDDEN_SIZE];
59};
60
61struct component_context_t {
62 struct component_context_data_t data; /* Component context data */
63 bool in_use; /* Flag to indicate if element is used */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000064 bool is_allowed_to_derive; /* Is the component allowed to derive */
65 bool is_export_cdi_allowed; /* Is CDI allowed to export */
Maulik Patelad2f3db2023-05-17 15:41:36 +010066 uint16_t nonce; /* Context handle nonce for the component */
67 uint16_t parent_idx; /* Parent component's index */
68 uint16_t linked_layer_idx; /* Layer component is linked to */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000069 int32_t target_locality; /* Identifies the locality to which the
70 * derived context will be bound */
Maulik Patelad2f3db2023-05-17 15:41:36 +010071 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
72};
73
74struct layer_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010075 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010076 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010077 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010078 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010079 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
80 size_t attest_pub_key_len;
Maulik Patele6adc112023-08-18 14:21:51 +010081 uint8_t attest_key_label[DPE_EXTERNAL_LABEL_MAX_SIZE];
82 size_t attest_key_label_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010083 uint8_t cert_buf[DICE_CERT_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010084 size_t cert_buf_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010085};
86
87enum layer_state_t {
88 LAYER_STATE_CLOSED = 0,
89 LAYER_STATE_OPEN,
90 LAYER_STATE_FINALISED
91};
92
93struct layer_context_t {
94 struct layer_context_data_t data;
95 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +010096 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010097 enum layer_state_t state;
Maulik Patele6adc112023-08-18 14:21:51 +010098 bool is_external_pub_key_provided;
Maulik Patel9fd8bd22023-10-30 10:58:30 +000099 bool is_cdi_to_be_exported;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100100};
101
102/**
Jamie Fox34681992023-09-04 18:14:06 +0100103 * \brief Initialise the DPE context manager.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100104 *
Jamie Fox34681992023-09-04 18:14:06 +0100105 * \param[out] rot_ctx_handle A new context handle for the RoT context.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100106 *
107 * \return Returns error code of type dpe_error_t
108 */
Jamie Fox34681992023-09-04 18:14:06 +0100109dpe_error_t initialise_context_mngr(int *rot_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100110
111/**
Maulik Patela81605b2023-10-24 12:17:03 +0100112 * \brief Derives a component context and optionally creates certificate
Maulik Patelad2f3db2023-05-17 15:41:36 +0100113 * chain.
114 *
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000115 * \param[in] input_context_handle Input handle to parent component context.
Maulik Patela81605b2023-10-24 12:17:03 +0100116 * \param[in] retain_parent_context Flag to indicate if parent context need
117 * to be retained. TRUE only if a client
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000118 * is calling DPE commands multiple times.
Maulik Patela81605b2023-10-24 12:17:03 +0100119 * \param[in] allow_new_context_to_derive Flag to indicate if derived context can
120 * derive further.
121 * \param[in] create_certificate Flag to indicate if certificate needs
122 * to be created. TRUE only if it is the
123 * last component in the layer.
124 * \param[in] dice_inputs Pointer to dice_input buffer.
125 * \param[in] client_id Identifier of the client calling the
126 * service.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000127 * \param[in] target_locality Identifier of the locality to which the
128 * derived context should be bound to.
129 * \param[in] return_certificate Indicates whether to return the generated
130 * certificate when create_certificate is true.
131 * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of
132 * the CDI from the newly derived context.
133 * \param[in] export_cdi Indicates whether to export derived CDI.
Maulik Patela81605b2023-10-24 12:17:03 +0100134 * \param[out] new_context_handle A new handle for derived context.
135 * \param[out] new_parent_context_handle A new handle for parent context.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000136 * \param[out] new_certificate_buf If create_certificate and return_certificate
137 * are both true, this argument holds the new
138 * certificate generated for the new context.
139 * \param[in] new_certificate_buf_size Size of the allocated buffer for
140 * new certificate.
141 * \param[out] new_certificate_actual_size Actual size of the new certificate.
142 * \param[out] exported_cdi_buf If export_cdi is true, this is the
143 * exported CDI value.
144 * \param[in] exported_cdi_buf_size Size of the allocated buffer for
145 * exported CDI.
146 * \param[out] exported_cdi_actual_size Actual size of the exported CDI.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100147 *
148 * \return Returns error code of type dpe_error_t
149 */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000150dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patela81605b2023-10-24 12:17:03 +0100151 bool retain_parent_context,
152 bool allow_new_context_to_derive,
153 bool create_certificate,
154 const DiceInputValues *dice_inputs,
155 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000156 int32_t target_locality,
157 bool return_certificate,
158 bool allow_new_context_to_export,
159 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100160 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000161 int *new_parent_context_handle,
162 uint8_t *new_certificate_buf,
163 size_t new_certificate_buf_size,
164 size_t *new_certificate_actual_size,
165 uint8_t *exported_cdi_buf,
166 size_t exported_cdi_buf_size,
167 size_t *exported_cdi_actual_size);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100168
169/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100170 * \brief Destroys a component context and optionally depending on argument
171 * destroy_recursively, destroys all its child context too.
172 *
173 * \param[in] input_context_handle Input handle to child component context
174 * \param[in] destroy_recursively Flag to indicate if all derived contexts
175 * should also be destroyed recursively.
176 *
177 * \return Returns error code of type dpe_error_t
178 */
179dpe_error_t destroy_context_request(int input_ctx_handle,
180 bool destroy_recursively);
181
182/**
Maulik Patel2358bbb2023-07-21 10:56:56 +0100183 * \brief Function to get the pointer to a component context if linked to a layer
184 *
185 * \param[in] layer_idx Index of the linked layer
186 * \param[in] component_idx Index of the component context in the array
187 *
188 * \return Returns pointer to the component context if it is linked to the input
189 * layer else returns NULL
190 */
191struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
192 uint16_t component_idx);
193
Maulik Patele6adc112023-08-18 14:21:51 +0100194/**
195 * \brief Function to get the pointer to a layer context
196 *
197 * \param[in] layer_idx Index of the layer in the layer context array
198 * for which pointer is required
199 *
200 * \return Returns pointer to the layer context if input index is valid
201 * else returns NULL
202 */
203struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx);
204
205/**
Maulik Patelcbded682023-12-07 11:50:16 +0000206 * \brief Certifies the attestation key and generates a leaf certificate.
207 * This command functionality depends on whether:
Maulik Patele6adc112023-08-18 14:21:51 +0100208 * - last layer is finalised
209 * - public key is supplied to the command
210 * - label is supplied to the command
211 *
212 * +---------------+------------+------------+----------------+
213 * | | pub_key | no pub_key | |
214 * +---------------+------------+------------+----------------+
215 * | | | see Note C | label |
216 * | finalized + see Note A +------------+----------------+
217 * | | | see Note D | no label |
218 * +---------------+------------+------------+----------------+
219 * | | | see Note E | label |
220 * | not finalized + see Note B +------------+----------------+
221 * | | | see Note F | no label |
222 * +---------------+------------+------------+----------------+
223 *
Maulik Patelcbded682023-12-07 11:50:16 +0000224 * A - Opens a new layer (if not opened), and creates a leaf certificate which
225 * includes supplied key.
Maulik Patele6adc112023-08-18 14:21:51 +0100226 * B - Creates certificate for current (existing) layer, which includes supplied
Maulik Patelcbded682023-12-07 11:50:16 +0000227 * key.
Maulik Patele6adc112023-08-18 14:21:51 +0100228 * C - Opens a new layer (if not opened), performs derivation which includes
Maulik Patelcbded682023-12-07 11:50:16 +0000229 * supplied label, and creates leaf certificate (including supplied label
230 * as a claim).
Maulik Patele6adc112023-08-18 14:21:51 +0100231 * D - Opens a new layer (if not opened), performs standard derivation,
Maulik Patelcbded682023-12-07 11:50:16 +0000232 * and creates a leaf certificate.
233 * E - Performs derivation (which includes supplied label) for current/existing layer
234 * and creates certificate which includes supplied label as a claim.
235 * F - Performs standard derivation for current/existing layer, and creates
236 * certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100237 *
238 * \param[in] input_ctx_handle Input handle to component context.
239 * \param[in] retain_context Flag to indicate if context needs
240 * to be retained. TRUE only if a client
241 * is calling DPE commands multiple times.
242 * \param[in] public_key The public key to certify. If omitted,
243 * key pair is deterministically derived
244 * from the context and label argument.
245 * \param[in] public_key_size Size of the input public key.
246 * \param[in] label Additional input to the key derivation
247 * from the context. If public key is
248 * already provided, this argument is
249 * ignored.
250 * \param[in] label_size Size of the input label.
Maulik Patelcbded682023-12-07 11:50:16 +0000251 * \param[out] certificate_buf Pointer to the buffer where
252 * the certificate will be stored.
253 * \param[in] certificate_buf_size Size of the allocated buffer for
254 * the certificate.
255 * \param[out] certificate_actual_size Actual size of the certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100256 * \param[out] derived_public_key_buf Pointer to the buffer where
257 * derived public key will be stored.
258 * \param[in] derived_public_key_buf_size Size of the allocated buffer for
259 * derived public key.
260 * \param[out] derived_public_key_actual_size Actual size of the derived public
261 * key.
262 * \param[out] new_context_handle A renewed handle for same context.
263 *
264 * \return Returns error code of type dpe_error_t
265 */
266dpe_error_t certify_key_request(int input_ctx_handle,
267 bool retain_context,
268 const uint8_t *public_key,
269 size_t public_key_size,
270 const uint8_t *label,
271 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000272 uint8_t *certificate_buf,
273 size_t certificate_buf_size,
274 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100275 uint8_t *derived_public_key_buf,
276 size_t derived_public_key_buf_size,
277 size_t *derived_public_key_actual_size,
278 int *new_context_handle);
279
Maulik Patel83a6b592023-12-05 15:20:30 +0000280/**
281 * \brief Returns the certificate chain generated for a given DPE context. The
282 * order, format, and encoding of the certificate chain are specified by
283 * a DPE profile.
284 *
285 * \param[in] input_ctx_handle Input context handle for the DPE
286 * context.
287 * \param[in] retain_context Flag to indicate whether to
288 * retain the context.
289 * \param[in] clear_from_context Flag to indicate whether DPE must
290 * clear the certificate chain from
291 * the context so subsequent calls
292 * on a given context, or contexts
293 * derived from it do not include
294 * the certificates returned by this
295 * command.
296 * retain the context.
297 * \param[out] certificate_chain_buf Buffer to write the certificate
298 * chain output.
299 * \param[in] certificate_chain_buf_size Size of the certificate chain
300 * buffer.
301 * \param[out] certificate_chain_actual_size Size of the certificate chain
302 * output written to the buffer.
303 * \param[out] new_context_handle New handle for the DPE context.
304 *
305 * \return Returns error code of type dpe_error_t
306 */
307dpe_error_t get_certificate_chain_request(int input_ctx_handle,
308 bool retain_context,
309 bool clear_from_context,
310 uint8_t *certificate_chain_buf,
311 size_t certificate_chain_buf_size,
312 size_t *certificate_chain_actual_size,
313 int *new_context_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100314#ifdef __cplusplus
315}
316#endif
317
318#endif /* __DPE_CONTEXT_MNGR_H__ */