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 | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 22 | |
| 23 | #define INVALID_HANDLE 0xFFFFFFFF |
| 24 | #define INVALID_COMPONENT_IDX 0xFFFF |
| 25 | #define INVALID_NONCE_VALUE 0xFFFF |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 26 | #define INVALID_LAYER_IDX 65535 |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 27 | #define DPE_ROT_LAYER_IDX 0 |
| 28 | |
| 29 | /* Below configuration defines are platform dependant */ |
| 30 | #define MAX_NUM_OF_COMPONENTS 30 |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 31 | #define MAX_NUM_OF_LAYERS 6 |
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 |
| 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 Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 36 | |
| 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 | |
| 44 | struct 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 | |
| 58 | struct 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 | |
| 68 | struct layer_context_data_t { |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 69 | psa_key_id_t cdi_key_id; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 70 | uint8_t cdi_seal[DICE_CDI_SIZE]; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 71 | uint8_t cdi_id[DICE_ID_SIZE]; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 72 | psa_key_id_t attest_key_id; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 73 | uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE]; |
| 74 | size_t attest_pub_key_len; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 75 | uint8_t attest_key_label[DPE_EXTERNAL_LABEL_MAX_SIZE]; |
| 76 | size_t attest_key_label_len; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 77 | uint8_t cert_buf[DICE_CERT_SIZE]; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 78 | size_t cert_buf_len; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | enum layer_state_t { |
| 82 | LAYER_STATE_CLOSED = 0, |
| 83 | LAYER_STATE_OPEN, |
| 84 | LAYER_STATE_FINALISED |
| 85 | }; |
| 86 | |
| 87 | struct layer_context_t { |
| 88 | struct layer_context_data_t data; |
| 89 | uint16_t parent_layer_idx; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 90 | uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE]; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 91 | enum layer_state_t state; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 92 | bool is_external_pub_key_provided; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /** |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 96 | * \brief Initialise the DPE context manager. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 97 | * |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 98 | * \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] | 99 | * |
| 100 | * \return Returns error code of type dpe_error_t |
| 101 | */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 102 | dpe_error_t initialise_context_mngr(int *rot_ctx_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame^] | 105 | * \brief Derives a component context and optionally creates certificate |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 106 | * chain. |
| 107 | * |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame^] | 108 | * \param[in] input_context_handle Input handle to parent component context |
| 109 | * \param[in] retain_parent_context Flag to indicate if parent context need |
| 110 | * to be retained. TRUE only if a client |
| 111 | * is calling DPE commands multiple times |
| 112 | * \param[in] allow_new_context_to_derive Flag to indicate if derived context can |
| 113 | * derive further. |
| 114 | * \param[in] create_certificate Flag to indicate if certificate needs |
| 115 | * to be created. TRUE only if it is the |
| 116 | * last component in the layer. |
| 117 | * \param[in] dice_inputs Pointer to dice_input buffer. |
| 118 | * \param[in] client_id Identifier of the client calling the |
| 119 | * service. |
| 120 | * \param[out] new_context_handle A new handle for derived context. |
| 121 | * \param[out] new_parent_context_handle A new handle for parent context. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 122 | * |
| 123 | * \return Returns error code of type dpe_error_t |
| 124 | */ |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame^] | 125 | dpe_error_t derive_context_request(int input_context_handle, |
| 126 | bool retain_parent_context, |
| 127 | bool allow_new_context_to_derive, |
| 128 | bool create_certificate, |
| 129 | const DiceInputValues *dice_inputs, |
| 130 | int32_t client_id, |
| 131 | int *new_context_handle, |
| 132 | int *new_parent_context_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 133 | |
| 134 | /** |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 135 | * \brief Destroys a component context and optionally depending on argument |
| 136 | * destroy_recursively, destroys all its child context too. |
| 137 | * |
| 138 | * \param[in] input_context_handle Input handle to child component context |
| 139 | * \param[in] destroy_recursively Flag to indicate if all derived contexts |
| 140 | * should also be destroyed recursively. |
| 141 | * |
| 142 | * \return Returns error code of type dpe_error_t |
| 143 | */ |
| 144 | dpe_error_t destroy_context_request(int input_ctx_handle, |
| 145 | bool destroy_recursively); |
| 146 | |
| 147 | /** |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 148 | * \brief Function to get the pointer to a component context if linked to a layer |
| 149 | * |
| 150 | * \param[in] layer_idx Index of the linked layer |
| 151 | * \param[in] component_idx Index of the component context in the array |
| 152 | * |
| 153 | * \return Returns pointer to the component context if it is linked to the input |
| 154 | * layer else returns NULL |
| 155 | */ |
| 156 | struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx, |
| 157 | uint16_t component_idx); |
| 158 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 159 | /** |
| 160 | * \brief Function to get the pointer to a layer context |
| 161 | * |
| 162 | * \param[in] layer_idx Index of the layer in the layer context array |
| 163 | * for which pointer is required |
| 164 | * |
| 165 | * \return Returns pointer to the layer context if input index is valid |
| 166 | * else returns NULL |
| 167 | */ |
| 168 | struct layer_context_t* get_layer_ctx_ptr(uint16_t layer_idx); |
| 169 | |
| 170 | /** |
| 171 | * \brief Generates a leaf certificate and returns all the certificate chain |
| 172 | * leading to it. This command functionality depends on whether: |
| 173 | * - last layer is finalised |
| 174 | * - public key is supplied to the command |
| 175 | * - label is supplied to the command |
| 176 | * |
| 177 | * +---------------+------------+------------+----------------+ |
| 178 | * | | pub_key | no pub_key | | |
| 179 | * +---------------+------------+------------+----------------+ |
| 180 | * | | | see Note C | label | |
| 181 | * | finalized + see Note A +------------+----------------+ |
| 182 | * | | | see Note D | no label | |
| 183 | * +---------------+------------+------------+----------------+ |
| 184 | * | | | see Note E | label | |
| 185 | * | not finalized + see Note B +------------+----------------+ |
| 186 | * | | | see Note F | no label | |
| 187 | * +---------------+------------+------------+----------------+ |
| 188 | * |
| 189 | * A - Opens a new layer (if not opened), creates a leaf certificate which |
| 190 | * includes supplied key and generates certificate chain. |
| 191 | * B - Creates certificate for current (existing) layer, which includes supplied |
| 192 | * key and generates certificate chain. |
| 193 | * C - Opens a new layer (if not opened), performs derivation which includes |
| 194 | * supplied label, creates leaf certificate (including supplied label as a |
| 195 | * claim) and generates certificate chain. |
| 196 | * D - Opens a new layer (if not opened), performs standard derivation, |
| 197 | * creates a leaf certificate and generates certificate chain. |
| 198 | * E - Performs derivation (which includes supplied label) for current/existing layer, |
| 199 | * creates certificate which includes supplied label as a claim, and generates |
| 200 | * certificate chain. |
| 201 | * F - Performs standard derivation for current/existing layer, creates certificate |
| 202 | * and generates certificate chain. |
| 203 | * |
| 204 | * \param[in] input_ctx_handle Input handle to component context. |
| 205 | * \param[in] retain_context Flag to indicate if context needs |
| 206 | * to be retained. TRUE only if a client |
| 207 | * is calling DPE commands multiple times. |
| 208 | * \param[in] public_key The public key to certify. If omitted, |
| 209 | * key pair is deterministically derived |
| 210 | * from the context and label argument. |
| 211 | * \param[in] public_key_size Size of the input public key. |
| 212 | * \param[in] label Additional input to the key derivation |
| 213 | * from the context. If public key is |
| 214 | * already provided, this argument is |
| 215 | * ignored. |
| 216 | * \param[in] label_size Size of the input label. |
| 217 | * \param[out] certificate_chain_buf Pointer to the buffer where |
| 218 | * certificate chain will be stored. |
| 219 | * \param[in] certificate_chain_buf_size Size of the allocated buffer for |
| 220 | * certificate chain. |
| 221 | * \param[out] certificate_chain_actual_size Actual size of the certificate |
| 222 | * chain. |
| 223 | * \param[out] derived_public_key_buf Pointer to the buffer where |
| 224 | * derived public key will be stored. |
| 225 | * \param[in] derived_public_key_buf_size Size of the allocated buffer for |
| 226 | * derived public key. |
| 227 | * \param[out] derived_public_key_actual_size Actual size of the derived public |
| 228 | * key. |
| 229 | * \param[out] new_context_handle A renewed handle for same context. |
| 230 | * |
| 231 | * \return Returns error code of type dpe_error_t |
| 232 | */ |
| 233 | dpe_error_t certify_key_request(int input_ctx_handle, |
| 234 | bool retain_context, |
| 235 | const uint8_t *public_key, |
| 236 | size_t public_key_size, |
| 237 | const uint8_t *label, |
| 238 | size_t label_size, |
| 239 | uint8_t *certificate_chain_buf, |
| 240 | size_t certificate_chain_buf_size, |
| 241 | size_t *certificate_chain_actual_size, |
| 242 | uint8_t *derived_public_key_buf, |
| 243 | size_t derived_public_key_buf_size, |
| 244 | size_t *derived_public_key_actual_size, |
| 245 | int *new_context_handle); |
| 246 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 247 | #ifdef __cplusplus |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | #endif /* __DPE_CONTEXT_MNGR_H__ */ |