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; |
| 18 | static const char attest_key_pair_label[] = DPE_ATTEST_KEY_PAIR_LABEL; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 19 | static const char id_label[] = DPE_ID_LABEL; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 20 | static const uint8_t attest_key_salt[] = DPE_ATTEST_KEY_SALT; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 21 | static const uint8_t id_salt[] = DPE_ID_SALT; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 22 | |
| 23 | static psa_status_t perform_derivation(psa_key_id_t base_key, |
| 24 | const psa_key_attributes_t *key_attr, |
| 25 | const uint8_t *key_label, |
| 26 | size_t key_label_len, |
| 27 | const uint8_t *salt, |
| 28 | size_t salt_len, |
| 29 | psa_key_id_t *out_key_id) |
| 30 | { |
| 31 | psa_status_t status; |
| 32 | psa_key_derivation_operation_t op = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 33 | |
| 34 | assert((key_label_len != 0) && (key_label != NULL) && |
| 35 | (base_key != 0) && (key_attr != NULL) && |
| 36 | (salt_len != 0) && (salt != NULL)); |
| 37 | |
| 38 | status = psa_key_derivation_setup(&op, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 39 | if (status != PSA_SUCCESS) { |
| 40 | return status; |
| 41 | } |
| 42 | |
| 43 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_SALT, |
| 44 | salt, salt_len); |
| 45 | if (status != PSA_SUCCESS) { |
| 46 | goto err_abort; |
| 47 | } |
| 48 | |
| 49 | status = psa_key_derivation_input_key(&op, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 50 | base_key); |
| 51 | if (status != PSA_SUCCESS) { |
| 52 | goto err_abort; |
| 53 | } |
| 54 | |
| 55 | /* Supply the key label as an input to the key derivation */ |
| 56 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_INFO, |
| 57 | key_label, key_label_len); |
| 58 | if (status != PSA_SUCCESS) { |
| 59 | goto err_abort; |
| 60 | } |
| 61 | |
| 62 | status = psa_key_derivation_output_key(key_attr, &op, out_key_id); |
| 63 | if (status != PSA_SUCCESS) { |
| 64 | goto err_abort; |
| 65 | } |
| 66 | |
| 67 | /* Free resources associated with the key derivation operation */ |
| 68 | status = psa_key_derivation_abort(&op); |
| 69 | if (status == PSA_SUCCESS) { |
| 70 | goto done; |
| 71 | } |
| 72 | |
| 73 | (void)psa_destroy_key(*out_key_id); |
| 74 | |
| 75 | err_abort: |
| 76 | (void)psa_key_derivation_abort(&op); |
| 77 | |
| 78 | done: |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | psa_status_t derive_attestation_cdi(struct layer_context_t *layer_ctx, |
| 83 | const struct layer_context_t *parent_layer_ctx) |
| 84 | { |
| 85 | psa_key_attributes_t derive_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 86 | |
| 87 | /* Set key attributes for CDI key */ |
| 88 | psa_set_key_type(&derive_key_attr, DPE_CDI_KEY_TYPE); |
| 89 | psa_set_key_algorithm(&derive_key_attr, DPE_CDI_KEY_ALG); |
| 90 | psa_set_key_bits(&derive_key_attr, DPE_CDI_KEY_BITS); |
| 91 | psa_set_key_usage_flags(&derive_key_attr, DPE_CDI_KEY_USAGE); |
| 92 | |
| 93 | /* Perform CDI derivation */ |
| 94 | /* Parent layer CDI is the base key (input secret to key derivation) */ |
| 95 | return perform_derivation(parent_layer_ctx->data.cdi_key_id, |
| 96 | &derive_key_attr, |
| 97 | (uint8_t *) &attest_cdi_label[0], |
| 98 | sizeof(attest_cdi_label), |
| 99 | layer_ctx->attest_cdi_hash_input, |
| 100 | sizeof(layer_ctx->attest_cdi_hash_input), |
| 101 | &layer_ctx->data.cdi_key_id); |
| 102 | } |
| 103 | |
| 104 | psa_status_t derive_attestation_key(struct layer_context_t *layer_ctx) |
| 105 | { |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 106 | psa_status_t status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 107 | psa_key_attributes_t attest_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 108 | |
| 109 | /* Set key attributes for Attest key pair derivation */ |
| 110 | psa_set_key_type(&attest_key_attr, DPE_ATTEST_KEY_TYPE); |
| 111 | psa_set_key_algorithm(&attest_key_attr, DPE_ATTEST_KEY_ALG); |
| 112 | psa_set_key_bits(&attest_key_attr, DPE_ATTEST_KEY_BITS); |
| 113 | psa_set_key_usage_flags(&attest_key_attr, DPE_ATTEST_KEY_USAGE); |
| 114 | |
| 115 | /* Perform key pair derivation */ |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 116 | status = perform_derivation(layer_ctx->data.cdi_key_id, |
| 117 | &attest_key_attr, |
| 118 | (uint8_t *)&attest_key_pair_label[0], |
| 119 | sizeof(attest_key_pair_label), |
| 120 | attest_key_salt, |
| 121 | sizeof(attest_key_salt), |
| 122 | &layer_ctx->data.attest_key_id); |
| 123 | if (status != PSA_SUCCESS) { |
| 124 | return status; |
| 125 | } |
| 126 | |
| 127 | return psa_export_public_key(layer_ctx->data.attest_key_id, |
| 128 | &layer_ctx->data.attest_pub_key[0], |
| 129 | sizeof(layer_ctx->data.attest_pub_key), |
| 130 | &layer_ctx->data.attest_pub_key_len); |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | psa_status_t create_layer_cdi_key(struct layer_context_t *layer_ctx, |
| 134 | const uint8_t *cdi_input, |
| 135 | size_t cdi_input_size) |
| 136 | { |
| 137 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 138 | |
| 139 | /* Set key attributes for CDI key */ |
| 140 | psa_set_key_type(&base_attributes, DPE_CDI_KEY_TYPE); |
| 141 | psa_set_key_algorithm(&base_attributes, DPE_CDI_KEY_ALG); |
| 142 | psa_set_key_bits(&base_attributes, DPE_CDI_KEY_BITS); |
| 143 | psa_set_key_usage_flags(&base_attributes, DPE_CDI_KEY_USAGE); |
| 144 | |
| 145 | return psa_import_key(&base_attributes, |
| 146 | cdi_input, |
| 147 | cdi_input_size, |
| 148 | &layer_ctx->data.cdi_key_id); |
| 149 | } |
| 150 | |
| 151 | psa_status_t derive_sealing_cdi(struct layer_context_t *layer_ctx) |
| 152 | { |
| 153 | //TODO: |
| 154 | (void)layer_ctx; |
| 155 | return PSA_SUCCESS; |
| 156 | } |
| 157 | |
| 158 | psa_status_t derive_wrapping_key(struct layer_context_t *layer_ctx) |
| 159 | { |
| 160 | //TODO: |
| 161 | (void)layer_ctx; |
| 162 | return PSA_SUCCESS; |
| 163 | } |
| 164 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 165 | 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] | 166 | { |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 167 | psa_status_t status; |
| 168 | psa_key_attributes_t derive_key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 169 | psa_key_attributes_t base_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 170 | size_t output_id_len; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 171 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame^] | 172 | psa_key_id_t base_key = PSA_KEY_ID_NULL; |
| 173 | psa_key_id_t derived_key_id = PSA_KEY_ID_NULL; |
| 174 | |
| 175 | psa_set_key_type(&base_attr, PSA_KEY_TYPE_DERIVE); |
| 176 | psa_set_key_algorithm(&base_attr, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 177 | psa_set_key_bits(&base_attr, PSA_BYTES_TO_BITS(layer_ctx->data.attest_pub_key_len)); |
| 178 | psa_set_key_usage_flags(&base_attr, PSA_KEY_USAGE_DERIVE); |
| 179 | |
| 180 | status = psa_import_key(&base_attr, |
| 181 | &layer_ctx->data.attest_pub_key[0], |
| 182 | layer_ctx->data.attest_pub_key_len, |
| 183 | &base_key); |
| 184 | if (status != PSA_SUCCESS) { |
| 185 | return status; |
| 186 | } |
| 187 | |
| 188 | /* Derive Key attributes same as CDI attributes except the label */ |
| 189 | psa_set_key_type(&derive_key_attr, PSA_KEY_TYPE_RAW_DATA); |
| 190 | psa_set_key_algorithm(&derive_key_attr, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 191 | psa_set_key_bits(&derive_key_attr, PSA_BYTES_TO_BITS(DICE_ID_SIZE)); |
| 192 | psa_set_key_usage_flags(&derive_key_attr, PSA_KEY_USAGE_EXPORT); |
| 193 | |
| 194 | /* Perform ID derivation */ |
| 195 | /* Supply the ID label as an input to the key derivation */ |
| 196 | status = perform_derivation(base_key, |
| 197 | &derive_key_attr, |
| 198 | (uint8_t *) &id_label[0], |
| 199 | sizeof(id_label), |
| 200 | id_salt, |
| 201 | sizeof(id_salt), |
| 202 | &derived_key_id); |
| 203 | if (status != PSA_SUCCESS) { |
| 204 | goto err_destroy_base_key; |
| 205 | } |
| 206 | status = psa_export_key(derived_key_id, |
| 207 | &layer_ctx->data.cdi_id[0], |
| 208 | sizeof(layer_ctx->data.cdi_id), |
| 209 | &output_id_len); |
| 210 | |
| 211 | (void)psa_destroy_key(derived_key_id); |
| 212 | |
| 213 | err_destroy_base_key: |
| 214 | (void)psa_destroy_key(base_key); |
| 215 | |
| 216 | return status; |
Maulik Patel | 58595d3 | 2023-06-22 10:08:53 +0100 | [diff] [blame] | 217 | } |