Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +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 | #include "dpe_crypto_interface.h" |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 9 | #include <assert.h> |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | #include <string.h> |
| 12 | #include "dpe_context_mngr.h" |
| 13 | #include "dpe_crypto_config.h" |
| 14 | #include "psa/crypto.h" |
| 15 | #include "tfm_crypto_defs.h" |
| 16 | |
| 17 | static const char attest_cdi_label[] = DPE_ATTEST_CDI_LABEL; |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame^] | 18 | static const char exported_attest_cdi_label[] = DPE_ATTEST_EXPORTED_CDI_LABEL; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 19 | static const char attest_key_pair_label[] = DPE_ATTEST_KEY_PAIR_LABEL; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 20 | static const char id_label[] = DPE_ID_LABEL; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 21 | static const uint8_t attest_key_salt[] = DPE_ATTEST_KEY_SALT; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 22 | static const uint8_t id_salt[] = DPE_ID_SALT; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 23 | |
| 24 | static psa_status_t perform_derivation(psa_key_id_t base_key, |
| 25 | const psa_key_attributes_t *key_attr, |
| 26 | const uint8_t *key_label, |
| 27 | size_t key_label_len, |
| 28 | const uint8_t *salt, |
| 29 | size_t salt_len, |
| 30 | psa_key_id_t *out_key_id) |
| 31 | { |
| 32 | psa_status_t status; |
| 33 | psa_key_derivation_operation_t op = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 34 | |
| 35 | assert((key_label_len != 0) && (key_label != NULL) && |
| 36 | (base_key != 0) && (key_attr != NULL) && |
| 37 | (salt_len != 0) && (salt != NULL)); |
| 38 | |
| 39 | status = psa_key_derivation_setup(&op, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 40 | if (status != PSA_SUCCESS) { |
| 41 | return status; |
| 42 | } |
| 43 | |
| 44 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_SALT, |
| 45 | salt, salt_len); |
| 46 | if (status != PSA_SUCCESS) { |
| 47 | goto err_abort; |
| 48 | } |
| 49 | |
| 50 | status = psa_key_derivation_input_key(&op, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 51 | base_key); |
| 52 | if (status != PSA_SUCCESS) { |
| 53 | goto err_abort; |
| 54 | } |
| 55 | |
| 56 | /* Supply the key label as an input to the key derivation */ |
| 57 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_INFO, |
| 58 | key_label, key_label_len); |
| 59 | if (status != PSA_SUCCESS) { |
| 60 | goto err_abort; |
| 61 | } |
| 62 | |
| 63 | status = psa_key_derivation_output_key(key_attr, &op, out_key_id); |
| 64 | if (status != PSA_SUCCESS) { |
| 65 | goto err_abort; |
| 66 | } |
| 67 | |
| 68 | /* Free resources associated with the key derivation operation */ |
| 69 | status = psa_key_derivation_abort(&op); |
| 70 | if (status == PSA_SUCCESS) { |
| 71 | goto done; |
| 72 | } |
| 73 | |
| 74 | (void)psa_destroy_key(*out_key_id); |
| 75 | |
| 76 | err_abort: |
| 77 | (void)psa_key_derivation_abort(&op); |
| 78 | |
| 79 | done: |
| 80 | return status; |
| 81 | } |
| 82 | |
| 83 | psa_status_t derive_attestation_cdi(struct layer_context_t *layer_ctx, |
| 84 | const struct layer_context_t *parent_layer_ctx) |
| 85 | { |
| 86 | psa_key_attributes_t derive_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 87 | |
| 88 | /* Set key attributes for CDI key */ |
| 89 | psa_set_key_type(&derive_key_attr, DPE_CDI_KEY_TYPE); |
| 90 | psa_set_key_algorithm(&derive_key_attr, DPE_CDI_KEY_ALG); |
| 91 | psa_set_key_bits(&derive_key_attr, DPE_CDI_KEY_BITS); |
| 92 | psa_set_key_usage_flags(&derive_key_attr, DPE_CDI_KEY_USAGE); |
| 93 | |
| 94 | /* Perform CDI derivation */ |
| 95 | /* Parent layer CDI is the base key (input secret to key derivation) */ |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame^] | 96 | |
| 97 | if (layer_ctx->is_cdi_to_be_exported) { |
| 98 | return perform_derivation(parent_layer_ctx->data.cdi_key_id, |
| 99 | &derive_key_attr, |
| 100 | (uint8_t *) &exported_attest_cdi_label[0], |
| 101 | sizeof(exported_attest_cdi_label), |
| 102 | layer_ctx->attest_cdi_hash_input, |
| 103 | sizeof(layer_ctx->attest_cdi_hash_input), |
| 104 | &layer_ctx->data.cdi_key_id); |
| 105 | |
| 106 | } else { |
| 107 | return perform_derivation(parent_layer_ctx->data.cdi_key_id, |
| 108 | &derive_key_attr, |
| 109 | (uint8_t *) &attest_cdi_label[0], |
| 110 | sizeof(attest_cdi_label), |
| 111 | layer_ctx->attest_cdi_hash_input, |
| 112 | sizeof(layer_ctx->attest_cdi_hash_input), |
| 113 | &layer_ctx->data.cdi_key_id); |
| 114 | } |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | psa_status_t derive_attestation_key(struct layer_context_t *layer_ctx) |
| 118 | { |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 119 | psa_status_t status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 120 | psa_key_attributes_t attest_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 121 | |
| 122 | /* Set key attributes for Attest key pair derivation */ |
| 123 | psa_set_key_type(&attest_key_attr, DPE_ATTEST_KEY_TYPE); |
| 124 | psa_set_key_algorithm(&attest_key_attr, DPE_ATTEST_KEY_ALG); |
| 125 | psa_set_key_bits(&attest_key_attr, DPE_ATTEST_KEY_BITS); |
| 126 | psa_set_key_usage_flags(&attest_key_attr, DPE_ATTEST_KEY_USAGE); |
| 127 | |
| 128 | /* Perform key pair derivation */ |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 129 | status = perform_derivation(layer_ctx->data.cdi_key_id, |
| 130 | &attest_key_attr, |
| 131 | (uint8_t *)&attest_key_pair_label[0], |
| 132 | sizeof(attest_key_pair_label), |
| 133 | attest_key_salt, |
| 134 | sizeof(attest_key_salt), |
| 135 | &layer_ctx->data.attest_key_id); |
| 136 | if (status != PSA_SUCCESS) { |
| 137 | return status; |
| 138 | } |
| 139 | |
| 140 | return psa_export_public_key(layer_ctx->data.attest_key_id, |
| 141 | &layer_ctx->data.attest_pub_key[0], |
| 142 | sizeof(layer_ctx->data.attest_pub_key), |
| 143 | &layer_ctx->data.attest_pub_key_len); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | psa_status_t create_layer_cdi_key(struct layer_context_t *layer_ctx, |
| 147 | const uint8_t *cdi_input, |
| 148 | size_t cdi_input_size) |
| 149 | { |
| 150 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 151 | |
| 152 | /* Set key attributes for CDI key */ |
| 153 | psa_set_key_type(&base_attributes, DPE_CDI_KEY_TYPE); |
| 154 | psa_set_key_algorithm(&base_attributes, DPE_CDI_KEY_ALG); |
| 155 | psa_set_key_bits(&base_attributes, DPE_CDI_KEY_BITS); |
| 156 | psa_set_key_usage_flags(&base_attributes, DPE_CDI_KEY_USAGE); |
| 157 | |
| 158 | return psa_import_key(&base_attributes, |
| 159 | cdi_input, |
| 160 | cdi_input_size, |
| 161 | &layer_ctx->data.cdi_key_id); |
| 162 | } |
| 163 | |
| 164 | psa_status_t derive_sealing_cdi(struct layer_context_t *layer_ctx) |
| 165 | { |
| 166 | //TODO: |
| 167 | (void)layer_ctx; |
| 168 | return PSA_SUCCESS; |
| 169 | } |
| 170 | |
| 171 | psa_status_t derive_wrapping_key(struct layer_context_t *layer_ctx) |
| 172 | { |
| 173 | //TODO: |
| 174 | (void)layer_ctx; |
| 175 | return PSA_SUCCESS; |
| 176 | } |
| 177 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 178 | psa_status_t derive_id_from_public_key(struct layer_context_t *layer_ctx) |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 179 | { |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 180 | psa_status_t status; |
| 181 | psa_key_attributes_t derive_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 182 | psa_key_attributes_t base_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 183 | size_t output_id_len; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 184 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 185 | psa_key_id_t base_key = PSA_KEY_ID_NULL; |
| 186 | psa_key_id_t derived_key_id = PSA_KEY_ID_NULL; |
| 187 | |
| 188 | psa_set_key_type(&base_attr, PSA_KEY_TYPE_DERIVE); |
| 189 | psa_set_key_algorithm(&base_attr, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 190 | psa_set_key_bits(&base_attr, PSA_BYTES_TO_BITS(layer_ctx->data.attest_pub_key_len)); |
| 191 | psa_set_key_usage_flags(&base_attr, PSA_KEY_USAGE_DERIVE); |
| 192 | |
| 193 | status = psa_import_key(&base_attr, |
| 194 | &layer_ctx->data.attest_pub_key[0], |
| 195 | layer_ctx->data.attest_pub_key_len, |
| 196 | &base_key); |
| 197 | if (status != PSA_SUCCESS) { |
| 198 | return status; |
| 199 | } |
| 200 | |
| 201 | /* Derive Key attributes same as CDI attributes except the label */ |
| 202 | psa_set_key_type(&derive_key_attr, PSA_KEY_TYPE_RAW_DATA); |
| 203 | psa_set_key_algorithm(&derive_key_attr, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 204 | psa_set_key_bits(&derive_key_attr, PSA_BYTES_TO_BITS(DICE_ID_SIZE)); |
| 205 | psa_set_key_usage_flags(&derive_key_attr, PSA_KEY_USAGE_EXPORT); |
| 206 | |
| 207 | /* Perform ID derivation */ |
| 208 | /* Supply the ID label as an input to the key derivation */ |
| 209 | status = perform_derivation(base_key, |
| 210 | &derive_key_attr, |
| 211 | (uint8_t *) &id_label[0], |
| 212 | sizeof(id_label), |
| 213 | id_salt, |
| 214 | sizeof(id_salt), |
| 215 | &derived_key_id); |
| 216 | if (status != PSA_SUCCESS) { |
| 217 | goto err_destroy_base_key; |
| 218 | } |
| 219 | status = psa_export_key(derived_key_id, |
| 220 | &layer_ctx->data.cdi_id[0], |
| 221 | sizeof(layer_ctx->data.cdi_id), |
| 222 | &output_id_len); |
| 223 | |
| 224 | (void)psa_destroy_key(derived_key_id); |
| 225 | |
| 226 | err_destroy_base_key: |
| 227 | (void)psa_destroy_key(base_key); |
| 228 | |
| 229 | return status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 230 | } |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 231 | |
| 232 | psa_status_t derive_cdi_id(psa_key_id_t attest_key_id, uint8_t *cdi_id, |
| 233 | size_t cdi_id_size) |
| 234 | { |
| 235 | psa_status_t status; |
| 236 | psa_key_derivation_operation_t op = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 237 | uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE]; |
| 238 | size_t attest_pub_key_len; |
| 239 | |
| 240 | status = psa_export_public_key(attest_key_id, attest_pub_key, |
| 241 | sizeof(attest_pub_key), &attest_pub_key_len); |
| 242 | if (status != PSA_SUCCESS) { |
| 243 | return status; |
| 244 | } |
| 245 | |
| 246 | status = psa_key_derivation_setup(&op, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 247 | if (status != PSA_SUCCESS) { |
| 248 | return status; |
| 249 | } |
| 250 | |
| 251 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_SALT, |
| 252 | id_salt, sizeof(id_salt)); |
| 253 | if (status != PSA_SUCCESS) { |
| 254 | goto err_abort; |
| 255 | } |
| 256 | |
| 257 | status = psa_key_derivation_input_bytes(&op, |
| 258 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 259 | attest_pub_key, attest_pub_key_len); |
| 260 | if (status != PSA_SUCCESS) { |
| 261 | goto err_abort; |
| 262 | } |
| 263 | |
| 264 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_INFO, |
| 265 | (const uint8_t *)id_label, |
| 266 | sizeof(id_label)); |
| 267 | if (status != PSA_SUCCESS) { |
| 268 | goto err_abort; |
| 269 | } |
| 270 | |
| 271 | status = psa_key_derivation_output_bytes(&op, cdi_id, cdi_id_size); |
| 272 | if (status != PSA_SUCCESS) { |
| 273 | goto err_abort; |
| 274 | } |
| 275 | |
| 276 | return psa_key_derivation_abort(&op); |
| 277 | |
| 278 | err_abort: |
| 279 | (void)psa_key_derivation_abort(&op); |
| 280 | return status; |
| 281 | } |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame^] | 282 | |
| 283 | psa_status_t get_layer_cdi_value(const struct layer_context_t *layer_ctx, |
| 284 | uint8_t *cdi_buf, |
| 285 | size_t cdi_buf_size, |
| 286 | size_t *cdi_actual_size) |
| 287 | { |
| 288 | psa_status_t status; |
| 289 | |
| 290 | //TODO: Sealing CDI to be added later |
| 291 | status = psa_export_key(layer_ctx->data.cdi_key_id, |
| 292 | cdi_buf, |
| 293 | sizeof(cdi_buf), |
| 294 | &cdi_actual_size); |
| 295 | if (status != PSA_SUCCESS) { |
| 296 | *cdi_actual_size = 0; |
| 297 | } |
| 298 | |
| 299 | return status; |
| 300 | } |