blob: 4aaaffd191a1de14b98840bad144f27f48e3f8b4 [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
Tamas Ban645e5022024-02-07 11:04:44 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Maulik Patelad2f3db2023-05-17 15:41:36 +01003 *
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 Patelacc3f4a2024-03-25 18:34:05 +000016#include "platform_locality.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Maulik Patel9fd8bd22023-10-30 10:58:30 +000022/* Below encoded CDI size accomodate both Attest and Seal CDI */
23#define DICE_MAX_ENCODED_CDI_SIZE ((2 * DICE_CDI_SIZE) + 16)
Maulik Patelad2f3db2023-05-17 15:41:36 +010024
25#define INVALID_HANDLE 0xFFFFFFFF
26#define INVALID_COMPONENT_IDX 0xFFFF
27#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patelad2f3db2023-05-17 15:41:36 +010028#define INVALID_LAYER_IDX 65535
Maulik Patel54d65f72023-06-28 13:04:36 +010029#define DPE_ROT_LAYER_IDX 0
30
Maulik Patelf268d902024-02-09 14:25:51 +000031/* Below configuration defines are platform dependent */
Tamas Ban42545792024-02-08 12:10:42 +010032#define MAX_NUM_OF_COMPONENTS 20
Maulik Patel54d65f72023-06-28 13:04:36 +010033#define DPE_PLATFORM_LAYER_IDX 1
34#define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2
Maulik Patel8ee20fc2024-02-28 15:01:51 +000035#ifdef DPE_TEST_MODE
36#define MAX_NUM_OF_LAYERS 6
37#else
38#define MAX_NUM_OF_LAYERS 4
39#endif /* DPE_TEST_MODE */
Tamas Ban42545792024-02-08 12:10:42 +010040
Maulik Patel54d65f72023-06-28 13:04:36 +010041/* Below threshold defines the threshold below which a context cannot be destroyed */
42#define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010043
44/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
45#define GET_IDX(handle) ((handle) & 0xffff)
46#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
47
48#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
49#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
50
Maulik Patelacc3f4a2024-03-25 18:34:05 +000051/* Current locality by default */
52#define DEFAULT_TARGET_LOCALITY LOCALITY_NONE
53
Maulik Patelad2f3db2023-05-17 15:41:36 +010054struct component_context_data_t {
55 uint8_t measurement_value[DICE_HASH_SIZE];
56 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
57 size_t measurement_descriptor_size;
58 uint8_t signer_id[DICE_HASH_SIZE];
59 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
60 size_t signer_id_descriptor_size;
61 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
62 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
63 size_t config_descriptor_size;
64 DiceMode mode;
65 uint8_t hidden[DICE_HIDDEN_SIZE];
66};
67
68struct component_context_t {
69 struct component_context_data_t data; /* Component context data */
70 bool in_use; /* Flag to indicate if element is used */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000071 bool is_allowed_to_derive; /* Is the component allowed to derive */
72 bool is_export_cdi_allowed; /* Is CDI allowed to export */
Maulik Patelad2f3db2023-05-17 15:41:36 +010073 uint16_t nonce; /* Context handle nonce for the component */
74 uint16_t parent_idx; /* Parent component's index */
75 uint16_t linked_layer_idx; /* Layer component is linked to */
Maulik Patel9fd8bd22023-10-30 10:58:30 +000076 int32_t target_locality; /* Identifies the locality to which the
77 * derived context will be bound */
Maulik Patelad2f3db2023-05-17 15:41:36 +010078 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
79};
80
81struct layer_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010082 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010083 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010084 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010085 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010086 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
87 size_t attest_pub_key_len;
Maulik Patel4fed7812023-12-08 09:55:22 +000088 uint8_t external_key_deriv_label[DPE_EXTERNAL_LABEL_MAX_SIZE];
89 size_t external_key_deriv_label_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010090};
91
92enum layer_state_t {
93 LAYER_STATE_CLOSED = 0,
94 LAYER_STATE_OPEN,
95 LAYER_STATE_FINALISED
96};
97
Maulik Patel009450d2024-04-23 12:03:10 +010098struct linked_components_t {
99 uint16_t idx[MAX_NUM_OF_COMPONENTS];
100 uint16_t count;
101};
102
Maulik Patelad2f3db2023-05-17 15:41:36 +0100103struct layer_context_t {
104 struct layer_context_data_t data;
Maulik Patelaa6b24f2024-04-05 15:13:08 +0100105 uint16_t idx;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100106 uint16_t parent_layer_idx;
Maulik Patel009450d2024-04-23 12:03:10 +0100107 struct linked_components_t linked_components;
Maulik Patel58595d32023-06-22 10:08:53 +0100108 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +0100109 enum layer_state_t state;
Maulik Patele6adc112023-08-18 14:21:51 +0100110 bool is_external_pub_key_provided;
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000111 bool is_cdi_to_be_exported;
Maulik Patel87c47ce2024-04-22 13:30:56 +0100112 bool is_rot_layer;
Maulik Patelcb14cde2024-01-23 12:39:53 +0000113 uint32_t cert_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +0100114};
115
116/**
Jamie Fox34681992023-09-04 18:14:06 +0100117 * \brief Initialise the DPE context manager.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100118 *
Jamie Fox34681992023-09-04 18:14:06 +0100119 * \param[out] rot_ctx_handle A new context handle for the RoT context.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100120 *
121 * \return Returns error code of type dpe_error_t
122 */
Jamie Fox34681992023-09-04 18:14:06 +0100123dpe_error_t initialise_context_mngr(int *rot_ctx_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100124
125/**
Maulik Patela81605b2023-10-24 12:17:03 +0100126 * \brief Derives a component context and optionally creates certificate
Maulik Patelad2f3db2023-05-17 15:41:36 +0100127 * chain.
128 *
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000129 * \param[in] input_context_handle Input handle to parent component context.
Maulik Patelcb14cde2024-01-23 12:39:53 +0000130 * \param[in] cert_id Logical certificate id to which derived
131 * context belongs to.
Maulik Patela81605b2023-10-24 12:17:03 +0100132 * \param[in] retain_parent_context Flag to indicate if parent context need
133 * to be retained. TRUE only if a client
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000134 * is calling DPE commands multiple times.
Maulik Patela81605b2023-10-24 12:17:03 +0100135 * \param[in] allow_new_context_to_derive Flag to indicate if derived context can
136 * derive further.
137 * \param[in] create_certificate Flag to indicate if certificate needs
138 * to be created. TRUE only if it is the
139 * last component in the layer.
140 * \param[in] dice_inputs Pointer to dice_input buffer.
141 * \param[in] client_id Identifier of the client calling the
142 * service.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000143 * \param[in] target_locality Identifier of the locality to which the
144 * derived context should be bound to.
145 * \param[in] return_certificate Indicates whether to return the generated
146 * certificate when create_certificate is true.
147 * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of
148 * the CDI from the newly derived context.
149 * \param[in] export_cdi Indicates whether to export derived CDI.
Maulik Patela81605b2023-10-24 12:17:03 +0100150 * \param[out] new_context_handle A new handle for derived context.
151 * \param[out] new_parent_context_handle A new handle for parent context.
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000152 * \param[out] new_certificate_buf If create_certificate and return_certificate
153 * are both true, this argument holds the new
154 * certificate generated for the new context.
155 * \param[in] new_certificate_buf_size Size of the allocated buffer for
156 * new certificate.
157 * \param[out] new_certificate_actual_size Actual size of the new certificate.
158 * \param[out] exported_cdi_buf If export_cdi is true, this is the
159 * exported CDI value.
160 * \param[in] exported_cdi_buf_size Size of the allocated buffer for
161 * exported CDI.
162 * \param[out] exported_cdi_actual_size Actual size of the exported CDI.
Maulik Patelad2f3db2023-05-17 15:41:36 +0100163 *
164 * \return Returns error code of type dpe_error_t
165 */
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000166dpe_error_t derive_context_request(int input_ctx_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +0000167 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +0100168 bool retain_parent_context,
169 bool allow_new_context_to_derive,
170 bool create_certificate,
171 const DiceInputValues *dice_inputs,
172 int32_t client_id,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000173 int32_t target_locality,
174 bool return_certificate,
175 bool allow_new_context_to_export,
176 bool export_cdi,
Maulik Patela81605b2023-10-24 12:17:03 +0100177 int *new_context_handle,
Maulik Patel9fd8bd22023-10-30 10:58:30 +0000178 int *new_parent_context_handle,
179 uint8_t *new_certificate_buf,
180 size_t new_certificate_buf_size,
181 size_t *new_certificate_actual_size,
182 uint8_t *exported_cdi_buf,
183 size_t exported_cdi_buf_size,
184 size_t *exported_cdi_actual_size);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100185
186/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100187 * \brief Destroys a component context and optionally depending on argument
188 * destroy_recursively, destroys all its child context too.
189 *
190 * \param[in] input_context_handle Input handle to child component context
191 * \param[in] destroy_recursively Flag to indicate if all derived contexts
192 * should also be destroyed recursively.
193 *
194 * \return Returns error code of type dpe_error_t
195 */
196dpe_error_t destroy_context_request(int input_ctx_handle,
197 bool destroy_recursively);
198
199/**
Maulik Patele6adc112023-08-18 14:21:51 +0100200 * \brief Function to get the pointer to a layer context
201 *
202 * \param[in] layer_idx Index of the layer in the layer context array
203 * for which pointer is required
204 *
205 * \return Returns pointer to the layer context if input index is valid
206 * else returns NULL
207 */
208struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx);
209
210/**
Maulik Patel009450d2024-04-23 12:03:10 +0100211 * \brief Function to get the pointer to a component context
212 *
213 * \param[in] component_idx Index of the component in the component context array
214 * for which pointer is required
215 *
216 * \return Returns pointer to the component context if input index is valid
217 * else returns NULL
218 */
219struct component_context_t* get_component_ctx_ptr(uint16_t component_idx);
220
221/**
Maulik Patelcbded682023-12-07 11:50:16 +0000222 * \brief Certifies the attestation key and generates a leaf certificate.
223 * This command functionality depends on whether:
Maulik Patele6adc112023-08-18 14:21:51 +0100224 * - last layer is finalised
225 * - public key is supplied to the command
226 * - label is supplied to the command
227 *
228 * +---------------+------------+------------+----------------+
229 * | | pub_key | no pub_key | |
230 * +---------------+------------+------------+----------------+
231 * | | | see Note C | label |
232 * | finalized + see Note A +------------+----------------+
233 * | | | see Note D | no label |
234 * +---------------+------------+------------+----------------+
235 * | | | see Note E | label |
236 * | not finalized + see Note B +------------+----------------+
237 * | | | see Note F | no label |
238 * +---------------+------------+------------+----------------+
239 *
Maulik Patelcbded682023-12-07 11:50:16 +0000240 * A - Opens a new layer (if not opened), and creates a leaf certificate which
241 * includes supplied key.
Maulik Patele6adc112023-08-18 14:21:51 +0100242 * B - Creates certificate for current (existing) layer, which includes supplied
Maulik Patelcbded682023-12-07 11:50:16 +0000243 * key.
Maulik Patele6adc112023-08-18 14:21:51 +0100244 * C - Opens a new layer (if not opened), performs derivation which includes
Maulik Patelcbded682023-12-07 11:50:16 +0000245 * supplied label, and creates leaf certificate (including supplied label
246 * as a claim).
Maulik Patele6adc112023-08-18 14:21:51 +0100247 * D - Opens a new layer (if not opened), performs standard derivation,
Maulik Patelcbded682023-12-07 11:50:16 +0000248 * and creates a leaf certificate.
249 * E - Performs derivation (which includes supplied label) for current/existing layer
250 * and creates certificate which includes supplied label as a claim.
251 * F - Performs standard derivation for current/existing layer, and creates
252 * certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100253 *
254 * \param[in] input_ctx_handle Input handle to component context.
255 * \param[in] retain_context Flag to indicate if context needs
256 * to be retained. TRUE only if a client
257 * is calling DPE commands multiple times.
258 * \param[in] public_key The public key to certify. If omitted,
259 * key pair is deterministically derived
260 * from the context and label argument.
261 * \param[in] public_key_size Size of the input public key.
262 * \param[in] label Additional input to the key derivation
263 * from the context. If public key is
264 * already provided, this argument is
265 * ignored.
266 * \param[in] label_size Size of the input label.
Maulik Patelcbded682023-12-07 11:50:16 +0000267 * \param[out] certificate_buf Pointer to the buffer where
268 * the certificate will be stored.
269 * \param[in] certificate_buf_size Size of the allocated buffer for
270 * the certificate.
271 * \param[out] certificate_actual_size Actual size of the certificate.
Maulik Patele6adc112023-08-18 14:21:51 +0100272 * \param[out] derived_public_key_buf Pointer to the buffer where
273 * derived public key will be stored.
274 * \param[in] derived_public_key_buf_size Size of the allocated buffer for
275 * derived public key.
276 * \param[out] derived_public_key_actual_size Actual size of the derived public
277 * key.
278 * \param[out] new_context_handle A renewed handle for same context.
279 *
280 * \return Returns error code of type dpe_error_t
281 */
282dpe_error_t certify_key_request(int input_ctx_handle,
283 bool retain_context,
284 const uint8_t *public_key,
285 size_t public_key_size,
286 const uint8_t *label,
287 size_t label_size,
Maulik Patelcbded682023-12-07 11:50:16 +0000288 uint8_t *certificate_buf,
289 size_t certificate_buf_size,
290 size_t *certificate_actual_size,
Maulik Patele6adc112023-08-18 14:21:51 +0100291 uint8_t *derived_public_key_buf,
292 size_t derived_public_key_buf_size,
293 size_t *derived_public_key_actual_size,
294 int *new_context_handle);
295
Maulik Patel83a6b592023-12-05 15:20:30 +0000296/**
297 * \brief Returns the certificate chain generated for a given DPE context. The
298 * order, format, and encoding of the certificate chain are specified by
299 * a DPE profile.
300 *
301 * \param[in] input_ctx_handle Input context handle for the DPE
302 * context.
303 * \param[in] retain_context Flag to indicate whether to
304 * retain the context.
305 * \param[in] clear_from_context Flag to indicate whether DPE must
306 * clear the certificate chain from
307 * the context so subsequent calls
308 * on a given context, or contexts
309 * derived from it do not include
310 * the certificates returned by this
311 * command.
312 * retain the context.
313 * \param[out] certificate_chain_buf Buffer to write the certificate
314 * chain output.
315 * \param[in] certificate_chain_buf_size Size of the certificate chain
316 * buffer.
317 * \param[out] certificate_chain_actual_size Size of the certificate chain
318 * output written to the buffer.
319 * \param[out] new_context_handle New handle for the DPE context.
320 *
321 * \return Returns error code of type dpe_error_t
322 */
323dpe_error_t get_certificate_chain_request(int input_ctx_handle,
324 bool retain_context,
325 bool clear_from_context,
326 uint8_t *certificate_chain_buf,
327 size_t certificate_chain_buf_size,
328 size_t *certificate_chain_actual_size,
329 int *new_context_handle);
Maulik Patelad2f3db2023-05-17 15:41:36 +0100330#ifdef __cplusplus
331}
332#endif
333
334#endif /* __DPE_CONTEXT_MNGR_H__ */