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 | acc3f4a | 2024-03-25 18:34:05 +0000 | [diff] [blame] | 16 | #include "platform_locality.h" |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
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 |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 26 | #define INVALID_NONCE_VALUE 0xFFFF |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 27 | |
Maulik Patel | f268d90 | 2024-02-09 14:25:51 +0000 | [diff] [blame] | 28 | /* Below configuration defines are platform dependent */ |
Tamas Ban | 4254579 | 2024-02-08 12:10:42 +0100 | [diff] [blame] | 29 | #define MAX_NUM_OF_COMPONENTS 20 |
Maulik Patel | 8ee20fc | 2024-02-28 15:01:51 +0000 | [diff] [blame] | 30 | #ifdef DPE_TEST_MODE |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 31 | #define MAX_NUM_OF_CERTIFICATES 6 |
Maulik Patel | 8ee20fc | 2024-02-28 15:01:51 +0000 | [diff] [blame] | 32 | #else |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 33 | #define MAX_NUM_OF_CERTIFICATES 4 |
Maulik Patel | 8ee20fc | 2024-02-28 15:01:51 +0000 | [diff] [blame] | 34 | #endif /* DPE_TEST_MODE */ |
Tamas Ban | 4254579 | 2024-02-08 12:10:42 +0100 | [diff] [blame] | 35 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 36 | /* Most significant 16 bits represent nonce & remaining 16 bits represent component index */ |
| 37 | #define GET_IDX(handle) ((handle) & 0xffff) |
| 38 | #define GET_NONCE(handle) ((handle >> 16) & 0xffff) |
| 39 | |
| 40 | #define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx) |
| 41 | #define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16)) |
| 42 | |
Maulik Patel | acc3f4a | 2024-03-25 18:34:05 +0000 | [diff] [blame] | 43 | /* Current locality by default */ |
| 44 | #define DEFAULT_TARGET_LOCALITY LOCALITY_NONE |
| 45 | |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 46 | struct component_context_data_t { |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 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]; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | struct component_context_t { |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 61 | struct component_context_data_t data; /* Component context data */ |
| 62 | bool in_use; /* Flag to indicate if element is used */ |
| 63 | bool is_allowed_to_derive; /* Is the component allowed to derive */ |
| 64 | bool is_export_cdi_allowed; /* Is CDI allowed to export */ |
| 65 | uint16_t nonce; /* Context handle nonce for the component */ |
| 66 | struct component_context_t *parent_comp_ctx; /* Pointer to parent component */ |
| 67 | struct cert_context_t *linked_cert_ctx; /* Pointer to linked certificate */ |
| 68 | int32_t target_locality; /* Identifies the locality to which the |
| 69 | * derived context will be bound */ |
| 70 | uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */ |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 73 | struct cert_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 | 4fed781 | 2023-12-08 09:55:22 +0000 | [diff] [blame] | 80 | uint8_t external_key_deriv_label[DPE_EXTERNAL_LABEL_MAX_SIZE]; |
| 81 | size_t external_key_deriv_label_len; |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 84 | enum cert_ctx_state_t { |
| 85 | CERT_CTX_UNASSIGNED = 0, |
| 86 | CERT_CTX_ASSIGNED, |
| 87 | CERT_CTX_FINALISED |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 90 | struct linked_components_t { |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 91 | struct component_context_t *ptr[MAX_NUM_OF_COMPONENTS]; /* Pointer to the linked components */ |
| 92 | uint16_t count; /* Count of the linked components */ |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 95 | struct cert_context_t { |
| 96 | struct cert_context_data_t data; |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 97 | struct linked_components_t linked_components; |
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 | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 99 | enum cert_ctx_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 | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 102 | bool is_rot_cert_ctx; |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 103 | uint32_t cert_id; |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 104 | struct cert_context_t *parent_cert_ptr; /* Pointer to parent certificate */ |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 108 | * \brief Initialise the DPE context manager. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 109 | * |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 110 | * \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] | 111 | * |
| 112 | * \return Returns error code of type dpe_error_t |
| 113 | */ |
Jamie Fox | 3468199 | 2023-09-04 18:14:06 +0100 | [diff] [blame] | 114 | dpe_error_t initialise_context_mngr(int *rot_ctx_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 115 | |
| 116 | /** |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 117 | * \brief Derives a component context and optionally creates certificate |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 118 | * chain. |
| 119 | * |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 120 | * \param[in] input_context_handle Input handle to parent component context. |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 121 | * \param[in] cert_id Logical certificate id to which derived |
| 122 | * context belongs to. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 123 | * \param[in] retain_parent_context Flag to indicate if parent context need |
| 124 | * to be retained. TRUE only if a client |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 125 | * is calling DPE commands multiple times. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 126 | * \param[in] allow_new_context_to_derive Flag to indicate if derived context can |
| 127 | * derive further. |
| 128 | * \param[in] create_certificate Flag to indicate if certificate needs |
| 129 | * to be created. TRUE only if it is the |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 130 | * last component in the certificate context. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 131 | * \param[in] dice_inputs Pointer to dice_input buffer. |
| 132 | * \param[in] client_id Identifier of the client calling the |
| 133 | * service. |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 134 | * \param[in] target_locality Identifier of the locality to which the |
| 135 | * derived context should be bound to. |
| 136 | * \param[in] return_certificate Indicates whether to return the generated |
| 137 | * certificate when create_certificate is true. |
| 138 | * \param[in] allow_new_context_to_export Indicates whether the DPE permits export of |
| 139 | * the CDI from the newly derived context. |
| 140 | * \param[in] export_cdi Indicates whether to export derived CDI. |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 141 | * \param[out] new_context_handle A new handle for derived context. |
| 142 | * \param[out] new_parent_context_handle A new handle for parent context. |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 143 | * \param[out] new_certificate_buf If create_certificate and return_certificate |
| 144 | * are both true, this argument holds the new |
| 145 | * certificate generated for the new context. |
| 146 | * \param[in] new_certificate_buf_size Size of the allocated buffer for |
| 147 | * new certificate. |
| 148 | * \param[out] new_certificate_actual_size Actual size of the new certificate. |
| 149 | * \param[out] exported_cdi_buf If export_cdi is true, this is the |
| 150 | * exported CDI value. |
| 151 | * \param[in] exported_cdi_buf_size Size of the allocated buffer for |
| 152 | * exported CDI. |
| 153 | * \param[out] exported_cdi_actual_size Actual size of the exported CDI. |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 154 | * |
| 155 | * \return Returns error code of type dpe_error_t |
| 156 | */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 157 | dpe_error_t derive_context_request(int input_ctx_handle, |
Maulik Patel | cb14cde | 2024-01-23 12:39:53 +0000 | [diff] [blame] | 158 | uint32_t cert_id, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 159 | bool retain_parent_context, |
| 160 | bool allow_new_context_to_derive, |
| 161 | bool create_certificate, |
| 162 | const DiceInputValues *dice_inputs, |
| 163 | int32_t client_id, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 164 | int32_t target_locality, |
| 165 | bool return_certificate, |
| 166 | bool allow_new_context_to_export, |
| 167 | bool export_cdi, |
Maulik Patel | a81605b | 2023-10-24 12:17:03 +0100 | [diff] [blame] | 168 | int *new_context_handle, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 169 | int *new_parent_context_handle, |
| 170 | uint8_t *new_certificate_buf, |
| 171 | size_t new_certificate_buf_size, |
| 172 | size_t *new_certificate_actual_size, |
| 173 | uint8_t *exported_cdi_buf, |
| 174 | size_t exported_cdi_buf_size, |
| 175 | size_t *exported_cdi_actual_size); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 176 | |
| 177 | /** |
Maulik Patel | 54d65f7 | 2023-06-28 13:04:36 +0100 | [diff] [blame] | 178 | * \brief Destroys a component context and optionally depending on argument |
| 179 | * destroy_recursively, destroys all its child context too. |
| 180 | * |
| 181 | * \param[in] input_context_handle Input handle to child component context |
| 182 | * \param[in] destroy_recursively Flag to indicate if all derived contexts |
| 183 | * should also be destroyed recursively. |
| 184 | * |
| 185 | * \return Returns error code of type dpe_error_t |
| 186 | */ |
| 187 | dpe_error_t destroy_context_request(int input_ctx_handle, |
| 188 | bool destroy_recursively); |
| 189 | |
| 190 | /** |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 191 | * \brief Certifies the attestation key and generates a leaf certificate. |
| 192 | * This command functionality depends on whether: |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 193 | * - last certificate context is finalised |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 194 | * - public key is supplied to the command |
| 195 | * - label is supplied to the command |
| 196 | * |
| 197 | * +---------------+------------+------------+----------------+ |
| 198 | * | | pub_key | no pub_key | | |
| 199 | * +---------------+------------+------------+----------------+ |
| 200 | * | | | see Note C | label | |
| 201 | * | finalized + see Note A +------------+----------------+ |
| 202 | * | | | see Note D | no label | |
| 203 | * +---------------+------------+------------+----------------+ |
| 204 | * | | | see Note E | label | |
| 205 | * | not finalized + see Note B +------------+----------------+ |
| 206 | * | | | see Note F | no label | |
| 207 | * +---------------+------------+------------+----------------+ |
| 208 | * |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 209 | * A - Assigns a new certificate context (if not assigned), and creates a leaf |
| 210 | * certificate which includes supplied key. |
| 211 | * B - Creates certificate for current (existing) context, which includes supplied |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 212 | * key. |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 213 | * C - Assigns a new certificate context (if not assigned), performs derivation |
| 214 | * which includes supplied label, and creates leaf certificate (including |
| 215 | * supplied label as a claim). |
| 216 | * D - Assigns a new certificate context (if not assigned), performs standard |
| 217 | * derivation, and creates a leaf certificate. |
| 218 | * E - Performs derivation (which includes supplied label) for current/existing |
| 219 | * certificate context and creates certificate which includes supplied label |
| 220 | * as a claim. |
| 221 | * F - Performs standard derivation for current/existing certificate context, |
| 222 | * and creates certificate. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 223 | * |
| 224 | * \param[in] input_ctx_handle Input handle to component context. |
| 225 | * \param[in] retain_context Flag to indicate if context needs |
| 226 | * to be retained. TRUE only if a client |
| 227 | * is calling DPE commands multiple times. |
| 228 | * \param[in] public_key The public key to certify. If omitted, |
| 229 | * key pair is deterministically derived |
| 230 | * from the context and label argument. |
| 231 | * \param[in] public_key_size Size of the input public key. |
| 232 | * \param[in] label Additional input to the key derivation |
| 233 | * from the context. If public key is |
| 234 | * already provided, this argument is |
| 235 | * ignored. |
| 236 | * \param[in] label_size Size of the input label. |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 237 | * \param[out] certificate_buf Pointer to the buffer where |
| 238 | * the certificate will be stored. |
| 239 | * \param[in] certificate_buf_size Size of the allocated buffer for |
| 240 | * the certificate. |
| 241 | * \param[out] certificate_actual_size Actual size of the certificate. |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 242 | * \param[out] derived_public_key_buf Pointer to the buffer where |
| 243 | * derived public key will be stored. |
| 244 | * \param[in] derived_public_key_buf_size Size of the allocated buffer for |
| 245 | * derived public key. |
| 246 | * \param[out] derived_public_key_actual_size Actual size of the derived public |
| 247 | * key. |
| 248 | * \param[out] new_context_handle A renewed handle for same context. |
| 249 | * |
| 250 | * \return Returns error code of type dpe_error_t |
| 251 | */ |
| 252 | dpe_error_t certify_key_request(int input_ctx_handle, |
| 253 | bool retain_context, |
| 254 | const uint8_t *public_key, |
| 255 | size_t public_key_size, |
| 256 | const uint8_t *label, |
| 257 | size_t label_size, |
Maulik Patel | cbded68 | 2023-12-07 11:50:16 +0000 | [diff] [blame] | 258 | uint8_t *certificate_buf, |
| 259 | size_t certificate_buf_size, |
| 260 | size_t *certificate_actual_size, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 261 | uint8_t *derived_public_key_buf, |
| 262 | size_t derived_public_key_buf_size, |
| 263 | size_t *derived_public_key_actual_size, |
| 264 | int *new_context_handle); |
| 265 | |
Maulik Patel | 83a6b59 | 2023-12-05 15:20:30 +0000 | [diff] [blame] | 266 | /** |
| 267 | * \brief Returns the certificate chain generated for a given DPE context. The |
| 268 | * order, format, and encoding of the certificate chain are specified by |
| 269 | * a DPE profile. |
| 270 | * |
| 271 | * \param[in] input_ctx_handle Input context handle for the DPE |
| 272 | * context. |
| 273 | * \param[in] retain_context Flag to indicate whether to |
| 274 | * retain the context. |
| 275 | * \param[in] clear_from_context Flag to indicate whether DPE must |
| 276 | * clear the certificate chain from |
| 277 | * the context so subsequent calls |
| 278 | * on a given context, or contexts |
| 279 | * derived from it do not include |
| 280 | * the certificates returned by this |
| 281 | * command. |
| 282 | * retain the context. |
| 283 | * \param[out] certificate_chain_buf Buffer to write the certificate |
| 284 | * chain output. |
| 285 | * \param[in] certificate_chain_buf_size Size of the certificate chain |
| 286 | * buffer. |
| 287 | * \param[out] certificate_chain_actual_size Size of the certificate chain |
| 288 | * output written to the buffer. |
| 289 | * \param[out] new_context_handle New handle for the DPE context. |
| 290 | * |
| 291 | * \return Returns error code of type dpe_error_t |
| 292 | */ |
| 293 | dpe_error_t get_certificate_chain_request(int input_ctx_handle, |
| 294 | bool retain_context, |
| 295 | bool clear_from_context, |
| 296 | uint8_t *certificate_chain_buf, |
| 297 | size_t certificate_chain_buf_size, |
| 298 | size_t *certificate_chain_actual_size, |
| 299 | int *new_context_handle); |
Maulik Patel | ad2f3db | 2023-05-17 15:41:36 +0100 | [diff] [blame] | 300 | #ifdef __cplusplus |
| 301 | } |
| 302 | #endif |
| 303 | |
| 304 | #endif /* __DPE_CONTEXT_MNGR_H__ */ |