Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 1 | /* |
Tamas Ban | 645e502 | 2024-02-07 11:04:44 +0100 | [diff] [blame] | 2 | * Copyright (c) 2023-2024, Arm Limited. All rights reserved. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 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 Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 15 | #include "dpe_crypto_config.h" |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 21 | /* Below encoded CDI size accomodate both Attest and Seal CDI */ |
| 22 | #define DICE_MAX_ENCODED_CDI_SIZE ((2 * DICE_CDI_SIZE) + 16) |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 23 | |
| 24 | #define INVALID_HANDLE 0xFFFFFFFF |
| 25 | #define INVALID_COMPONENT_IDX 0xFFFF |
| 26 | #define INVALID_NONCE_VALUE 0xFFFF |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 27 | #define INVALID_LAYER_IDX 65535 |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 28 | #define DPE_ROT_LAYER_IDX 0 |
| 29 | |
Maulik Patel | f268d90 | 2024-02-09 14:25:51 +0000 | [diff] [blame] | 30 | /* Below configuration defines are platform dependent */ |
Tamas Ban | 4254579 | 2024-02-08 12:10:42 +0100 | [diff] [blame] | 31 | #define MAX_NUM_OF_COMPONENTS 20 |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 32 | #define DPE_PLATFORM_LAYER_IDX 1 |
| 33 | #define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2 |
Maulik Patel | 8ee20fc | 2024-02-28 15:01:51 +0000 | [diff] [blame] | 34 | #ifdef DPE_TEST_MODE |
| 35 | #define MAX_NUM_OF_LAYERS 6 |
| 36 | #else |
| 37 | #define MAX_NUM_OF_LAYERS 4 |
| 38 | #endif /* DPE_TEST_MODE */ |
Tamas Ban | 4254579 | 2024-02-08 12:10:42 +0100 | [diff] [blame] | 39 | |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 40 | /* Below threshold defines the threshold below which a context cannot be destroyed */ |
| 41 | #define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 42 | |
| 43 | /* Most significant 16 bits represent nonce & remaining 16 bits represent component index */ |
| 44 | #define GET_IDX(handle) ((handle) & 0xffff) |
| 45 | #define GET_NONCE(handle) ((handle >> 16) & 0xffff) |
| 46 | |
| 47 | #define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx) |
| 48 | #define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16)) |
| 49 | |
| 50 | struct component_context_data_t { |
| 51 | uint8_t measurement_value[DICE_HASH_SIZE]; |
| 52 | uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE]; |
| 53 | size_t measurement_descriptor_size; |
| 54 | uint8_t signer_id[DICE_HASH_SIZE]; |
| 55 | uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE]; |
| 56 | size_t signer_id_descriptor_size; |
| 57 | uint8_t config_value[DICE_INLINE_CONFIG_SIZE]; |
| 58 | uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE]; |
| 59 | size_t config_descriptor_size; |
| 60 | DiceMode mode; |
| 61 | uint8_t hidden[DICE_HIDDEN_SIZE]; |
| 62 | }; |
| 63 | |
| 64 | struct component_context_t { |
| 65 | struct component_context_data_t data; /* Component context data */ |
| 66 | bool in_use; /* Flag to indicate if element is used */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 67 | bool is_allowed_to_derive; /* Is the component allowed to derive */ |
| 68 | bool is_export_cdi_allowed; /* Is CDI allowed to export */ |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 69 | uint16_t nonce; /* Context handle nonce for the component */ |
| 70 | uint16_t parent_idx; /* Parent component's index */ |
| 71 | uint16_t linked_layer_idx; /* Layer component is linked to */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 72 | int32_t target_locality; /* Identifies the locality to which the |
| 73 | * derived context will be bound */ |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 74 | uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */ |
| 75 | }; |
| 76 | |
| 77 | struct layer_context_data_t { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 78 | psa_key_id_t cdi_key_id; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 79 | uint8_t cdi_seal[DICE_CDI_SIZE]; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 80 | uint8_t cdi_id[DICE_ID_SIZE]; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 81 | psa_key_id_t attest_key_id; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 82 | uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE]; |
| 83 | size_t attest_pub_key_len; |
Maulik Patel | 4fed781 | 2023-12-08 09:55:22 +0000 | [diff] [blame] | 84 | uint8_t external_key_deriv_label[DPE_EXTERNAL_LABEL_MAX_SIZE]; |
| 85 | size_t external_key_deriv_label_len; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | enum layer_state_t { |
| 89 | LAYER_STATE_CLOSED = 0, |
| 90 | LAYER_STATE_OPEN, |
| 91 | LAYER_STATE_FINALISED |
| 92 | }; |
| 93 | |
| 94 | struct layer_context_t { |
| 95 | struct layer_context_data_t data; |
Maulik Patel | aa6b24f | 2024-04-05 15:13:08 +0100 | [diff] [blame] | 96 | uint16_t idx; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 97 | uint16_t parent_layer_idx; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 98 | uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE]; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 99 | enum layer_state_t state; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 100 | bool is_external_pub_key_provided; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 101 | bool is_cdi_to_be_exported; |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 102 | uint32_t cert_id; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /** |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 106 | * \brief Initialise the DPE context manager. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 107 | * |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 108 | * \param[out] rot_ctx_handle A new context handle for the RoT context. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 109 | * |
| 110 | * \return Returns error code of type dpe_error_t |
| 111 | */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 112 | dpe_error_t initialise_context_mngr(int *rot_ctx_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 115 | * \brief Derives a component context and optionally creates certificate |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 116 | * chain. |
| 117 | * |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 118 | * \param[in] input_context_handle Input handle to parent component context. |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 119 | * \param[in] cert_id Logical certificate id to which derived |
| 120 | * context belongs to. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 121 | * \param[in] retain_parent_context Flag to indicate if parent context need |
| 122 | * to be retained. TRUE only if a client |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 123 | * is calling DPE commands multiple times. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 124 | * \param[in] allow_new_context_to_derive Flag to indicate if derived context can |
| 125 | * derive further. |
| 126 | * \param[in] create_certificate Flag to indicate if certificate needs |
| 127 | * to be created. TRUE only if it is the |
| 128 | * last component in the layer. |
| 129 | * \param[in] dice_inputs Pointer to dice_input buffer. |
| 130 | * \param[in] client_id Identifier of the client calling the |
| 131 | * service. |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 132 | * \param[in] target_locality Identifier of the locality to which the |
| 133 | * derived context should be bound to. |
| 134 | * \param[in] return_certificate Indicates whether to return the generated |
| 135 | * certificate when create_certificate is true. |
| 136 | * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of |
| 137 | * the CDI from the newly derived context. |
| 138 | * \param[in] export_cdi Indicates whether to export derived CDI. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 139 | * \param[out] new_context_handle A new handle for derived context. |
| 140 | * \param[out] new_parent_context_handle A new handle for parent context. |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 141 | * \param[out] new_certificate_buf If create_certificate and return_certificate |
| 142 | * are both true, this argument holds the new |
| 143 | * certificate generated for the new context. |
| 144 | * \param[in] new_certificate_buf_size Size of the allocated buffer for |
| 145 | * new certificate. |
| 146 | * \param[out] new_certificate_actual_size Actual size of the new certificate. |
| 147 | * \param[out] exported_cdi_buf If export_cdi is true, this is the |
| 148 | * exported CDI value. |
| 149 | * \param[in] exported_cdi_buf_size Size of the allocated buffer for |
| 150 | * exported CDI. |
| 151 | * \param[out] exported_cdi_actual_size Actual size of the exported CDI. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 152 | * |
| 153 | * \return Returns error code of type dpe_error_t |
| 154 | */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 155 | dpe_error_t derive_context_request(int input_ctx_handle, |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 156 | uint32_t cert_id, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 157 | bool retain_parent_context, |
| 158 | bool allow_new_context_to_derive, |
| 159 | bool create_certificate, |
| 160 | const DiceInputValues *dice_inputs, |
| 161 | int32_t client_id, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 162 | int32_t target_locality, |
| 163 | bool return_certificate, |
| 164 | bool allow_new_context_to_export, |
| 165 | bool export_cdi, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 166 | int *new_context_handle, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 167 | int *new_parent_context_handle, |
| 168 | uint8_t *new_certificate_buf, |
| 169 | size_t new_certificate_buf_size, |
| 170 | size_t *new_certificate_actual_size, |
| 171 | uint8_t *exported_cdi_buf, |
| 172 | size_t exported_cdi_buf_size, |
| 173 | size_t *exported_cdi_actual_size); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 174 | |
| 175 | /** |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 176 | * \brief Destroys a component context and optionally depending on argument |
| 177 | * destroy_recursively, destroys all its child context too. |
| 178 | * |
| 179 | * \param[in] input_context_handle Input handle to child component context |
| 180 | * \param[in] destroy_recursively Flag to indicate if all derived contexts |
| 181 | * should also be destroyed recursively. |
| 182 | * |
| 183 | * \return Returns error code of type dpe_error_t |
| 184 | */ |
| 185 | dpe_error_t destroy_context_request(int input_ctx_handle, |
| 186 | bool destroy_recursively); |
| 187 | |
| 188 | /** |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 189 | * \brief Function to get the pointer to a component context if linked to a layer |
| 190 | * |
| 191 | * \param[in] layer_idx Index of the linked layer |
| 192 | * \param[in] component_idx Index of the component context in the array |
| 193 | * |
| 194 | * \return Returns pointer to the component context if it is linked to the input |
| 195 | * layer else returns NULL |
| 196 | */ |
| 197 | struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx, |
| 198 | uint16_t component_idx); |
| 199 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 200 | /** |
| 201 | * \brief Function to get the pointer to a layer context |
| 202 | * |
| 203 | * \param[in] layer_idx Index of the layer in the layer context array |
| 204 | * for which pointer is required |
| 205 | * |
| 206 | * \return Returns pointer to the layer context if input index is valid |
| 207 | * else returns NULL |
| 208 | */ |
| 209 | struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx); |
| 210 | |
| 211 | /** |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 212 | * \brief Certifies the attestation key and generates a leaf certificate. |
| 213 | * This command functionality depends on whether: |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 214 | * - last layer is finalised |
| 215 | * - public key is supplied to the command |
| 216 | * - label is supplied to the command |
| 217 | * |
| 218 | * +---------------+------------+------------+----------------+ |
| 219 | * | | pub_key | no pub_key | | |
| 220 | * +---------------+------------+------------+----------------+ |
| 221 | * | | | see Note C | label | |
| 222 | * | finalized + see Note A +------------+----------------+ |
| 223 | * | | | see Note D | no label | |
| 224 | * +---------------+------------+------------+----------------+ |
| 225 | * | | | see Note E | label | |
| 226 | * | not finalized + see Note B +------------+----------------+ |
| 227 | * | | | see Note F | no label | |
| 228 | * +---------------+------------+------------+----------------+ |
| 229 | * |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 230 | * A - Opens a new layer (if not opened), and creates a leaf certificate which |
| 231 | * includes supplied key. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 232 | * B - Creates certificate for current (existing) layer, which includes supplied |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 233 | * key. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 234 | * C - Opens a new layer (if not opened), performs derivation which includes |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 235 | * supplied label, and creates leaf certificate (including supplied label |
| 236 | * as a claim). |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 237 | * D - Opens a new layer (if not opened), performs standard derivation, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 238 | * and creates a leaf certificate. |
| 239 | * E - Performs derivation (which includes supplied label) for current/existing layer |
| 240 | * and creates certificate which includes supplied label as a claim. |
| 241 | * F - Performs standard derivation for current/existing layer, and creates |
| 242 | * certificate. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 243 | * |
| 244 | * \param[in] input_ctx_handle Input handle to component context. |
| 245 | * \param[in] retain_context Flag to indicate if context needs |
| 246 | * to be retained. TRUE only if a client |
| 247 | * is calling DPE commands multiple times. |
| 248 | * \param[in] public_key The public key to certify. If omitted, |
| 249 | * key pair is deterministically derived |
| 250 | * from the context and label argument. |
| 251 | * \param[in] public_key_size Size of the input public key. |
| 252 | * \param[in] label Additional input to the key derivation |
| 253 | * from the context. If public key is |
| 254 | * already provided, this argument is |
| 255 | * ignored. |
| 256 | * \param[in] label_size Size of the input label. |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 257 | * \param[out] certificate_buf Pointer to the buffer where |
| 258 | * the certificate will be stored. |
| 259 | * \param[in] certificate_buf_size Size of the allocated buffer for |
| 260 | * the certificate. |
| 261 | * \param[out] certificate_actual_size Actual size of the certificate. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 262 | * \param[out] derived_public_key_buf Pointer to the buffer where |
| 263 | * derived public key will be stored. |
| 264 | * \param[in] derived_public_key_buf_size Size of the allocated buffer for |
| 265 | * derived public key. |
| 266 | * \param[out] derived_public_key_actual_size Actual size of the derived public |
| 267 | * key. |
| 268 | * \param[out] new_context_handle A renewed handle for same context. |
| 269 | * |
| 270 | * \return Returns error code of type dpe_error_t |
| 271 | */ |
| 272 | dpe_error_t certify_key_request(int input_ctx_handle, |
| 273 | bool retain_context, |
| 274 | const uint8_t *public_key, |
| 275 | size_t public_key_size, |
| 276 | const uint8_t *label, |
| 277 | size_t label_size, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 278 | uint8_t *certificate_buf, |
| 279 | size_t certificate_buf_size, |
| 280 | size_t *certificate_actual_size, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 281 | uint8_t *derived_public_key_buf, |
| 282 | size_t derived_public_key_buf_size, |
| 283 | size_t *derived_public_key_actual_size, |
| 284 | int *new_context_handle); |
| 285 | |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 286 | /** |
| 287 | * \brief Returns the certificate chain generated for a given DPE context. The |
| 288 | * order, format, and encoding of the certificate chain are specified by |
| 289 | * a DPE profile. |
| 290 | * |
| 291 | * \param[in] input_ctx_handle Input context handle for the DPE |
| 292 | * context. |
| 293 | * \param[in] retain_context Flag to indicate whether to |
| 294 | * retain the context. |
| 295 | * \param[in] clear_from_context Flag to indicate whether DPE must |
| 296 | * clear the certificate chain from |
| 297 | * the context so subsequent calls |
| 298 | * on a given context, or contexts |
| 299 | * derived from it do not include |
| 300 | * the certificates returned by this |
| 301 | * command. |
| 302 | * retain the context. |
| 303 | * \param[out] certificate_chain_buf Buffer to write the certificate |
| 304 | * chain output. |
| 305 | * \param[in] certificate_chain_buf_size Size of the certificate chain |
| 306 | * buffer. |
| 307 | * \param[out] certificate_chain_actual_size Size of the certificate chain |
| 308 | * output written to the buffer. |
| 309 | * \param[out] new_context_handle New handle for the DPE context. |
| 310 | * |
| 311 | * \return Returns error code of type dpe_error_t |
| 312 | */ |
| 313 | dpe_error_t get_certificate_chain_request(int input_ctx_handle, |
| 314 | bool retain_context, |
| 315 | bool clear_from_context, |
| 316 | uint8_t *certificate_chain_buf, |
| 317 | size_t certificate_chain_buf_size, |
| 318 | size_t *certificate_chain_actual_size, |
| 319 | int *new_context_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 320 | #ifdef __cplusplus |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | #endif /* __DPE_CONTEXT_MNGR_H__ */ |