blob: 04bdceeb5669c0044b106a7c6754a50b6fd40092 [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 Patelad2f3db2023-05-17 15:41:36 +010022
23#define INVALID_HANDLE 0xFFFFFFFF
24#define INVALID_COMPONENT_IDX 0xFFFF
25#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patelad2f3db2023-05-17 15:41:36 +010026#define INVALID_LAYER_IDX 65535
Maulik Patel54d65f72023-06-28 13:04:36 +010027#define DPE_ROT_LAYER_IDX 0
28
29/* Below configuration defines are platform dependant */
30#define MAX_NUM_OF_COMPONENTS 30
Maulik Patel2358bbb2023-07-21 10:56:56 +010031#define MAX_NUM_OF_LAYERS 6
Maulik Patel54d65f72023-06-28 13:04:36 +010032#define DPE_PLATFORM_LAYER_IDX 1
33#define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2
34/* Below threshold defines the threshold below which a context cannot be destroyed */
35#define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010036
37/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
38#define GET_IDX(handle) ((handle) & 0xffff)
39#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
40
41#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
42#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
43
44struct component_context_data_t {
45 uint8_t measurement_value[DICE_HASH_SIZE];
46 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
47 size_t measurement_descriptor_size;
48 uint8_t signer_id[DICE_HASH_SIZE];
49 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
50 size_t signer_id_descriptor_size;
51 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
52 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
53 size_t config_descriptor_size;
54 DiceMode mode;
55 uint8_t hidden[DICE_HIDDEN_SIZE];
56};
57
58struct component_context_t {
59 struct component_context_data_t data; /* Component context data */
60 bool in_use; /* Flag to indicate if element is used */
61 bool is_leaf; /* Is the component allowed to derive */
62 uint16_t nonce; /* Context handle nonce for the component */
63 uint16_t parent_idx; /* Parent component's index */
64 uint16_t linked_layer_idx; /* Layer component is linked to */
65 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
66};
67
68struct layer_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010069 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010070 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010071 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010072 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010073 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
74 size_t attest_pub_key_len;
Maulik Patele6adc112023-08-18 14:21:51 +010075 uint8_t attest_key_label[DPE_EXTERNAL_LABEL_MAX_SIZE];
76 size_t attest_key_label_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010077 uint8_t cert_buf[DICE_CERT_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010078 size_t cert_buf_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010079};
80
81enum layer_state_t {
82 LAYER_STATE_CLOSED = 0,
83 LAYER_STATE_OPEN,
84 LAYER_STATE_FINALISED
85};
86
87struct layer_context_t {
88 struct layer_context_data_t data;
89 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +010090 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010091 enum layer_state_t state;
Maulik Patele6adc112023-08-18 14:21:51 +010092 bool is_external_pub_key_provided;
Maulik Patelad2f3db2023-05-17 15:41:36 +010093};
94
95/**
96 * \brief Derives a root of trust component context and creates certificate.
97 *
98 * \param[in] dice_inputs Pointer to dice_input buffer.
99 * \param[out] new_child_context_handle A new handle for child context.
100 * \param[out] new_parent_context_handle A new handle for parent context.
101 *
102 * \return Returns error code of type dpe_error_t
103 */
104dpe_error_t derive_rot_context(const DiceInputValues *dice_inputs,
105 int *new_child_ctx_handle,
106 int *new_parent_ctx_handle);
107
108/**
109 * \brief Derives a child component context and optionally creates certificate
110 * chain.
111 *
112 * \param[in] input_context_handle Input handle to child component context
113 * \param[in] retain_parent_context Flag to indicate if parent context need
114 * to be retained. TRUE only if a client
115 * is calling DPE commands multiple times
116 * \param[in] allow_child_to_derive Flag to indicate if requested child can
117 * derive further.
118 * \param[in] create_certificate Flag to indicate if certificate needs
119 * to be created. TRUE only if it is the
120 * last component in the layer.
121 * \param[in] dice_inputs Pointer to dice_input buffer.
122 * \param[in] client_id Identifier of the client calling the
123 * service.
124 * \param[out] new_child_context_handle A new handle for child context.
125 * \param[out] new_parent_context_handle A new handle for parent context.
126 *
127 * \return Returns error code of type dpe_error_t
128 */
129dpe_error_t derive_child_request(int input_context_handle,
130 bool retain_parent_context,
131 bool allow_child_to_derive,
132 bool create_certificate,
133 const DiceInputValues *dice_inputs,
134 int32_t client_id,
135 int *new_child_context_handle,
136 int *new_parent_context_handle);
137
138/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100139 * \brief Destroys a component context and optionally depending on argument
140 * destroy_recursively, destroys all its child context too.
141 *
142 * \param[in] input_context_handle Input handle to child component context
143 * \param[in] destroy_recursively Flag to indicate if all derived contexts
144 * should also be destroyed recursively.
145 *
146 * \return Returns error code of type dpe_error_t
147 */
148dpe_error_t destroy_context_request(int input_ctx_handle,
149 bool destroy_recursively);
150
151/**
Maulik Patelad2f3db2023-05-17 15:41:36 +0100152 * \brief Initialise all DPE Layer and component contexts
153 *
154 */
155void initialise_all_dpe_contexts(void);
156
Maulik Patel2358bbb2023-07-21 10:56:56 +0100157/**
158 * \brief Function to get the pointer to a component context if linked to a layer
159 *
160 * \param[in] layer_idx Index of the linked layer
161 * \param[in] component_idx Index of the component context in the array
162 *
163 * \return Returns pointer to the component context if it is linked to the input
164 * layer else returns NULL
165 */
166struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
167 uint16_t component_idx);
168
Maulik Patele6adc112023-08-18 14:21:51 +0100169/**
170 * \brief Function to get the pointer to a layer context
171 *
172 * \param[in] layer_idx Index of the layer in the layer context array
173 * for which pointer is required
174 *
175 * \return Returns pointer to the layer context if input index is valid
176 * else returns NULL
177 */
178struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx);
179
180/**
181 * \brief Generates a leaf certificate and returns all the certificate chain
182 * leading to it. This command functionality depends on whether:
183 * - last layer is finalised
184 * - public key is supplied to the command
185 * - label is supplied to the command
186 *
187 * +---------------+------------+------------+----------------+
188 * | | pub_key | no pub_key | |
189 * +---------------+------------+------------+----------------+
190 * | | | see Note C | label |
191 * | finalized + see Note A +------------+----------------+
192 * | | | see Note D | no label |
193 * +---------------+------------+------------+----------------+
194 * | | | see Note E | label |
195 * | not finalized + see Note B +------------+----------------+
196 * | | | see Note F | no label |
197 * +---------------+------------+------------+----------------+
198 *
199 * A - Opens a new layer (if not opened), creates a leaf certificate which
200 * includes supplied key and generates certificate chain.
201 * B - Creates certificate for current (existing) layer, which includes supplied
202 * key and generates certificate chain.
203 * C - Opens a new layer (if not opened), performs derivation which includes
204 * supplied label, creates leaf certificate (including supplied label as a
205 * claim) and generates certificate chain.
206 * D - Opens a new layer (if not opened), performs standard derivation,
207 * creates a leaf certificate and generates certificate chain.
208 * E - Performs derivation (which includes supplied label) for current/existing layer,
209 * creates certificate which includes supplied label as a claim, and generates
210 * certificate chain.
211 * F - Performs standard derivation for current/existing layer, creates certificate
212 * and generates certificate chain.
213 *
214 * \param[in] input_ctx_handle Input handle to component context.
215 * \param[in] retain_context Flag to indicate if context needs
216 * to be retained. TRUE only if a client
217 * is calling DPE commands multiple times.
218 * \param[in] public_key The public key to certify. If omitted,
219 * key pair is deterministically derived
220 * from the context and label argument.
221 * \param[in] public_key_size Size of the input public key.
222 * \param[in] label Additional input to the key derivation
223 * from the context. If public key is
224 * already provided, this argument is
225 * ignored.
226 * \param[in] label_size Size of the input label.
227 * \param[out] certificate_chain_buf Pointer to the buffer where
228 * certificate chain will be stored.
229 * \param[in] certificate_chain_buf_size Size of the allocated buffer for
230 * certificate chain.
231 * \param[out] certificate_chain_actual_size Actual size of the certificate
232 * chain.
233 * \param[out] derived_public_key_buf Pointer to the buffer where
234 * derived public key will be stored.
235 * \param[in] derived_public_key_buf_size Size of the allocated buffer for
236 * derived public key.
237 * \param[out] derived_public_key_actual_size Actual size of the derived public
238 * key.
239 * \param[out] new_context_handle A renewed handle for same context.
240 *
241 * \return Returns error code of type dpe_error_t
242 */
243dpe_error_t certify_key_request(int input_ctx_handle,
244 bool retain_context,
245 const uint8_t *public_key,
246 size_t public_key_size,
247 const uint8_t *label,
248 size_t label_size,
249 uint8_t *certificate_chain_buf,
250 size_t certificate_chain_buf_size,
251 size_t *certificate_chain_actual_size,
252 uint8_t *derived_public_key_buf,
253 size_t derived_public_key_buf_size,
254 size_t *derived_public_key_actual_size,
255 int *new_context_handle);
256
Maulik Patelad2f3db2023-05-17 15:41:36 +0100257#ifdef __cplusplus
258}
259#endif
260
261#endif /* __DPE_CONTEXT_MNGR_H__ */