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