Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
| 12 | |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 13 | /* Required for mbedtls_calloc in tfm_crypto_huk_derivation_input_bytes */ |
| 14 | #include "mbedtls/platform.h" |
| 15 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 16 | #include "tfm_crypto_api.h" |
| 17 | #include "tfm_crypto_defs.h" |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 18 | #include "tfm_memory_utils.h" |
| 19 | |
Mingyang Sun | 8a19e7a | 2020-06-04 15:36:58 +0800 | [diff] [blame] | 20 | #include "tfm_plat_crypto_keys.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 21 | #include "tfm_crypto_private.h" |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 22 | |
Kevin Peng | c6d7450 | 2020-03-04 16:55:37 +0800 | [diff] [blame] | 23 | #ifdef TFM_PARTITION_TEST_PS |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 24 | #include "psa_manifest/pid.h" |
Kevin Peng | c6d7450 | 2020-03-04 16:55:37 +0800 | [diff] [blame] | 25 | #endif /* TFM_PARTITION_TEST_PS */ |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 26 | |
| 27 | #ifndef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 28 | static psa_status_t tfm_crypto_huk_derivation_setup( |
| 29 | psa_key_derivation_operation_t *operation, |
| 30 | psa_algorithm_t alg) |
| 31 | { |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 32 | operation->MBEDTLS_PRIVATE(alg) = TFM_CRYPTO_ALG_HUK_DERIVATION; |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 33 | return PSA_SUCCESS; |
| 34 | } |
| 35 | |
| 36 | static psa_status_t tfm_crypto_huk_derivation_input_bytes( |
| 37 | psa_key_derivation_operation_t *operation, |
| 38 | psa_key_derivation_step_t step, |
| 39 | const uint8_t *data, |
| 40 | size_t data_length) |
| 41 | { |
| 42 | psa_status_t status; |
| 43 | int32_t partition_id; |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 44 | psa_tls12_prf_key_derivation_t *tls12_prf; |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 45 | |
| 46 | if (step != PSA_KEY_DERIVATION_INPUT_LABEL) { |
| 47 | return PSA_ERROR_INVALID_ARGUMENT; |
| 48 | } |
| 49 | |
| 50 | /* Concatenate the caller's partition ID with the supplied label to prevent |
| 51 | * two different partitions from deriving the same key. |
| 52 | */ |
| 53 | status = tfm_crypto_get_caller_id(&partition_id); |
| 54 | if (status != PSA_SUCCESS) { |
| 55 | return status; |
| 56 | } |
| 57 | |
Kevin Peng | c6d7450 | 2020-03-04 16:55:37 +0800 | [diff] [blame] | 58 | #ifdef TFM_PARTITION_TEST_PS |
| 59 | /* The PS tests run some operations under the wrong partition ID - this |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 60 | * causes the key derivation to change. |
| 61 | */ |
Kevin Peng | c6d7450 | 2020-03-04 16:55:37 +0800 | [diff] [blame] | 62 | if (partition_id == TFM_SP_PS_TEST) { |
| 63 | partition_id = TFM_SP_PS; |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 64 | } |
Kevin Peng | c6d7450 | 2020-03-04 16:55:37 +0800 | [diff] [blame] | 65 | #endif /* TFM_PARTITION_TEST_PS */ |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 66 | |
| 67 | /* Put the label in the tls12_prf ctx to make it available in the output key |
| 68 | * step. |
| 69 | */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 70 | tls12_prf = &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf)); |
| 71 | tls12_prf->MBEDTLS_PRIVATE(label) = |
| 72 | mbedtls_calloc(1, sizeof(partition_id) + data_length); |
| 73 | if (tls12_prf->MBEDTLS_PRIVATE(label) == NULL) { |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 74 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 75 | } |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 76 | (void)tfm_memcpy(tls12_prf->MBEDTLS_PRIVATE(label), &partition_id, |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 77 | sizeof(partition_id)); |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 78 | (void)tfm_memcpy(tls12_prf->MBEDTLS_PRIVATE(label) + sizeof(partition_id), |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 79 | data, data_length); |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 80 | tls12_prf->MBEDTLS_PRIVATE(label_length) = sizeof(partition_id) + |
| 81 | data_length; |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 82 | |
| 83 | return PSA_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | static psa_status_t tfm_crypto_huk_derivation_output_key( |
| 87 | const psa_key_attributes_t *attributes, |
| 88 | psa_key_derivation_operation_t *operation, |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 89 | mbedtls_svc_key_id_t *key_id) |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 90 | { |
| 91 | enum tfm_plat_err_t err; |
| 92 | size_t bytes = PSA_BITS_TO_BYTES(psa_get_key_bits(attributes)); |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 93 | psa_tls12_prf_key_derivation_t *tls12_prf = |
| 94 | &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf)); |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 95 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 96 | if (sizeof(tls12_prf->MBEDTLS_PRIVATE(output_block)) < bytes) { |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 97 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 98 | } |
| 99 | |
| 100 | /* Derive key material from the HUK and output it to the operation buffer */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 101 | err = tfm_plat_get_huk_derived_key(tls12_prf->MBEDTLS_PRIVATE(label), |
| 102 | tls12_prf->MBEDTLS_PRIVATE(label_length), |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 103 | NULL, 0, |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 104 | tls12_prf->MBEDTLS_PRIVATE(output_block), |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 105 | bytes); |
| 106 | if (err != TFM_PLAT_ERR_SUCCESS) { |
| 107 | return PSA_ERROR_HARDWARE_FAILURE; |
| 108 | } |
| 109 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 110 | return psa_import_key(attributes, tls12_prf->MBEDTLS_PRIVATE(output_block), |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 111 | bytes, key_id); |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static psa_status_t tfm_crypto_huk_derivation_abort( |
| 115 | psa_key_derivation_operation_t *operation) |
| 116 | { |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 117 | psa_tls12_prf_key_derivation_t *tls12_prf = |
| 118 | &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf)); |
| 119 | |
| 120 | if (tls12_prf->MBEDTLS_PRIVATE(label) != NULL) { |
| 121 | (void)tfm_memset(tls12_prf->MBEDTLS_PRIVATE(label), 0, |
| 122 | tls12_prf->MBEDTLS_PRIVATE(label_length)); |
| 123 | mbedtls_free(tls12_prf->MBEDTLS_PRIVATE(label)); |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | (void)tfm_memset(operation, 0, sizeof(*operation)); |
| 127 | |
| 128 | return PSA_SUCCESS; |
| 129 | } |
| 130 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 131 | |
| 132 | /*! |
| 133 | * \defgroup public_psa Public functions, PSA |
| 134 | * |
| 135 | */ |
| 136 | |
| 137 | /*!@{*/ |
| 138 | psa_status_t tfm_crypto_key_derivation_setup(psa_invec in_vec[], |
| 139 | size_t in_len, |
| 140 | psa_outvec out_vec[], |
| 141 | size_t out_len) |
| 142 | { |
| 143 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 144 | return PSA_ERROR_NOT_SUPPORTED; |
| 145 | #else |
| 146 | psa_status_t status = PSA_SUCCESS; |
| 147 | psa_key_derivation_operation_t *operation = NULL; |
| 148 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 149 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 150 | |
| 151 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 152 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 153 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 154 | } |
| 155 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 156 | uint32_t handle = iov->op_handle; |
| 157 | uint32_t *handle_out = out_vec[0].base; |
| 158 | psa_algorithm_t alg = iov->alg; |
| 159 | |
| 160 | /* Allocate the operation context in the secure world */ |
| 161 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 162 | &handle, |
| 163 | (void **)&operation); |
| 164 | if (status != PSA_SUCCESS) { |
| 165 | return status; |
| 166 | } |
| 167 | |
| 168 | *handle_out = handle; |
| 169 | |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 170 | if (alg == TFM_CRYPTO_ALG_HUK_DERIVATION) { |
| 171 | status = tfm_crypto_huk_derivation_setup(operation, alg); |
| 172 | } else { |
| 173 | status = psa_key_derivation_setup(operation, alg); |
| 174 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 175 | if (status != PSA_SUCCESS) { |
| 176 | /* Release the operation context, ignore if the operation fails. */ |
| 177 | (void)tfm_crypto_operation_release(handle_out); |
| 178 | return status; |
| 179 | } |
| 180 | |
| 181 | return status; |
| 182 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 183 | } |
| 184 | |
| 185 | psa_status_t tfm_crypto_key_derivation_get_capacity(psa_invec in_vec[], |
| 186 | size_t in_len, |
| 187 | psa_outvec out_vec[], |
| 188 | size_t out_len) |
| 189 | { |
| 190 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 191 | return PSA_ERROR_NOT_SUPPORTED; |
| 192 | #else |
| 193 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 194 | |
| 195 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 196 | |
| 197 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 198 | (out_vec[0].len != sizeof(size_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 199 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 200 | } |
| 201 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 202 | |
| 203 | uint32_t handle = iov->op_handle; |
| 204 | size_t *capacity = out_vec[0].base; |
| 205 | psa_key_derivation_operation_t *operation = NULL; |
| 206 | |
| 207 | /* Look up the corresponding operation context */ |
| 208 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 209 | handle, |
| 210 | (void **)&operation); |
| 211 | if (status != PSA_SUCCESS) { |
| 212 | *capacity = 0; |
| 213 | return status; |
| 214 | } |
| 215 | |
| 216 | return psa_key_derivation_get_capacity(operation, capacity); |
| 217 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 218 | } |
| 219 | |
| 220 | psa_status_t tfm_crypto_key_derivation_set_capacity(psa_invec in_vec[], |
| 221 | size_t in_len, |
| 222 | psa_outvec out_vec[], |
| 223 | size_t out_len) |
| 224 | { |
| 225 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 226 | return PSA_ERROR_NOT_SUPPORTED; |
| 227 | #else |
| 228 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 229 | |
| 230 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 231 | |
| 232 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 233 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 234 | } |
| 235 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 236 | |
| 237 | uint32_t handle = iov->op_handle; |
| 238 | size_t capacity = iov->capacity; |
| 239 | psa_key_derivation_operation_t *operation = NULL; |
| 240 | |
| 241 | /* Look up the corresponding operation context */ |
| 242 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 243 | handle, |
| 244 | (void **)&operation); |
| 245 | if (status != PSA_SUCCESS) { |
| 246 | return status; |
| 247 | } |
| 248 | |
| 249 | return psa_key_derivation_set_capacity(operation, capacity); |
| 250 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 251 | } |
| 252 | |
| 253 | psa_status_t tfm_crypto_key_derivation_input_bytes(psa_invec in_vec[], |
| 254 | size_t in_len, |
| 255 | psa_outvec out_vec[], |
| 256 | size_t out_len) |
| 257 | { |
| 258 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 259 | return PSA_ERROR_NOT_SUPPORTED; |
| 260 | #else |
| 261 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 262 | |
| 263 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 0); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 264 | |
| 265 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 266 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 267 | } |
| 268 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 269 | |
| 270 | uint32_t handle = iov->op_handle; |
| 271 | psa_key_derivation_step_t step = iov->step; |
| 272 | const uint8_t *data = in_vec[1].base; |
| 273 | size_t data_length = in_vec[1].len; |
| 274 | psa_key_derivation_operation_t *operation = NULL; |
| 275 | |
| 276 | /* Look up the corresponding operation context */ |
| 277 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 278 | handle, |
| 279 | (void **)&operation); |
| 280 | if (status != PSA_SUCCESS) { |
| 281 | return status; |
| 282 | } |
| 283 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 284 | if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) { |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 285 | return tfm_crypto_huk_derivation_input_bytes(operation, step, data, |
| 286 | data_length); |
| 287 | } else { |
| 288 | return psa_key_derivation_input_bytes(operation, step, data, |
| 289 | data_length); |
| 290 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 291 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 292 | } |
| 293 | |
| 294 | psa_status_t tfm_crypto_key_derivation_output_bytes(psa_invec in_vec[], |
| 295 | size_t in_len, |
| 296 | psa_outvec out_vec[], |
| 297 | size_t out_len) |
| 298 | { |
| 299 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 300 | return PSA_ERROR_NOT_SUPPORTED; |
| 301 | #else |
| 302 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 303 | |
| 304 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 305 | |
| 306 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 307 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 308 | } |
| 309 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 310 | |
| 311 | uint32_t handle = iov->op_handle; |
| 312 | uint8_t *output = out_vec[0].base; |
| 313 | size_t output_length = out_vec[0].len; |
| 314 | psa_key_derivation_operation_t *operation = NULL; |
| 315 | |
| 316 | /* Look up the corresponding operation context */ |
| 317 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 318 | handle, |
| 319 | (void **)&operation); |
| 320 | if (status != PSA_SUCCESS) { |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | return psa_key_derivation_output_bytes(operation, output, output_length); |
| 325 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 326 | } |
| 327 | |
| 328 | psa_status_t tfm_crypto_key_derivation_input_key(psa_invec in_vec[], |
| 329 | size_t in_len, |
| 330 | psa_outvec out_vec[], |
| 331 | size_t out_len) |
| 332 | { |
| 333 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 334 | return PSA_ERROR_NOT_SUPPORTED; |
| 335 | #else |
| 336 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 337 | |
| 338 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 339 | |
| 340 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 341 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 342 | } |
| 343 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 344 | uint32_t handle = iov->op_handle; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 345 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 346 | psa_key_derivation_step_t step = iov->step; |
| 347 | psa_key_derivation_operation_t *operation = NULL; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 348 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 349 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 350 | /* Look up the corresponding operation context */ |
| 351 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 352 | handle, |
| 353 | (void **)&operation); |
| 354 | if (status != PSA_SUCCESS) { |
| 355 | return status; |
| 356 | } |
| 357 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 358 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 359 | if (status != PSA_SUCCESS) { |
| 360 | return status; |
| 361 | } |
| 362 | |
| 363 | return psa_key_derivation_input_key(operation, step, encoded_key); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 364 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 365 | } |
| 366 | |
| 367 | psa_status_t tfm_crypto_key_derivation_output_key(psa_invec in_vec[], |
| 368 | size_t in_len, |
| 369 | psa_outvec out_vec[], |
| 370 | size_t out_len) |
| 371 | { |
| 372 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 373 | return PSA_ERROR_NOT_SUPPORTED; |
| 374 | #else |
| 375 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 376 | |
| 377 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 378 | |
| 379 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 380 | (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) || |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 381 | (out_vec[0].len != sizeof(psa_key_id_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 382 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 383 | } |
| 384 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 385 | |
| 386 | uint32_t handle = iov->op_handle; |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 387 | const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 388 | psa_key_derivation_operation_t *operation = NULL; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 389 | psa_key_id_t *key_handle = out_vec[0].base; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 390 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 391 | int32_t partition_id; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 392 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 393 | |
| 394 | /* Look up the corresponding operation context */ |
| 395 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 396 | handle, |
| 397 | (void **)&operation); |
| 398 | if (status != PSA_SUCCESS) { |
| 399 | return status; |
| 400 | } |
Jamie Fox | 99360e8 | 2020-02-20 16:00:09 +0000 | [diff] [blame] | 401 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 402 | status = tfm_crypto_get_caller_id(&partition_id); |
| 403 | if (status != PSA_SUCCESS) { |
| 404 | return status; |
| 405 | } |
| 406 | |
| 407 | status = tfm_crypto_key_attributes_from_client(client_key_attr, |
| 408 | partition_id, |
| 409 | &key_attributes); |
| 410 | if (status != PSA_SUCCESS) { |
| 411 | return status; |
| 412 | } |
| 413 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 414 | if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) { |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 415 | status = tfm_crypto_huk_derivation_output_key(&key_attributes, |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 416 | operation, &encoded_key); |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 417 | } else { |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 418 | status = psa_key_derivation_output_key(&key_attributes, operation, |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 419 | &encoded_key); |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 420 | } |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 421 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame] | 422 | *key_handle = encoded_key.MBEDTLS_PRIVATE(key_id); |
Jamie Fox | 99360e8 | 2020-02-20 16:00:09 +0000 | [diff] [blame] | 423 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 424 | return status; |
| 425 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 426 | } |
| 427 | |
| 428 | psa_status_t tfm_crypto_key_derivation_abort(psa_invec in_vec[], |
| 429 | size_t in_len, |
| 430 | psa_outvec out_vec[], |
| 431 | size_t out_len) |
| 432 | { |
| 433 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 434 | return PSA_ERROR_NOT_SUPPORTED; |
| 435 | #else |
| 436 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 437 | |
| 438 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 439 | |
| 440 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 441 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 442 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 443 | } |
| 444 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 445 | |
| 446 | uint32_t handle = iov->op_handle; |
| 447 | uint32_t *handle_out = out_vec[0].base; |
| 448 | psa_key_derivation_operation_t *operation = NULL; |
| 449 | |
| 450 | /* Init the handle in the operation with the one passed from the iov */ |
| 451 | *handle_out = iov->op_handle; |
| 452 | |
| 453 | /* Look up the corresponding operation context */ |
| 454 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 455 | handle, |
| 456 | (void **)&operation); |
| 457 | if (status != PSA_SUCCESS) { |
| 458 | /* Operation does not exist, so abort has no effect */ |
| 459 | return PSA_SUCCESS; |
| 460 | } |
| 461 | |
| 462 | *handle_out = handle; |
| 463 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 464 | if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) { |
Jamie Fox | 044bd8e | 2020-02-20 13:34:57 +0000 | [diff] [blame] | 465 | status = tfm_crypto_huk_derivation_abort(operation); |
| 466 | } else { |
| 467 | status = psa_key_derivation_abort(operation); |
| 468 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 469 | if (status != PSA_SUCCESS) { |
| 470 | /* Release the operation context, ignore if the operation fails. */ |
| 471 | (void)tfm_crypto_operation_release(handle_out); |
| 472 | return status; |
| 473 | } |
| 474 | |
| 475 | status = tfm_crypto_operation_release(handle_out); |
| 476 | |
| 477 | return status; |
| 478 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 479 | } |
| 480 | |
| 481 | psa_status_t tfm_crypto_key_derivation_key_agreement(psa_invec in_vec[], |
| 482 | size_t in_len, |
| 483 | psa_outvec out_vec[], |
| 484 | size_t out_len) |
| 485 | { |
| 486 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 487 | return PSA_ERROR_NOT_SUPPORTED; |
| 488 | #else |
| 489 | psa_status_t status; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 490 | |
| 491 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 0); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 492 | |
| 493 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 494 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 495 | } |
| 496 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 497 | |
| 498 | uint32_t handle = iov->op_handle; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 499 | psa_key_id_t private_key = iov->key_id; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 500 | const uint8_t *peer_key = in_vec[1].base; |
| 501 | size_t peer_key_length = in_vec[1].len; |
| 502 | psa_key_derivation_operation_t *operation = NULL; |
| 503 | psa_key_derivation_step_t step = iov->step; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 504 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 505 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 506 | /* Look up the corresponding operation context */ |
| 507 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION, |
| 508 | handle, |
| 509 | (void **)&operation); |
| 510 | if (status != PSA_SUCCESS) { |
| 511 | return status; |
| 512 | } |
| 513 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 514 | status = tfm_crypto_encode_id_and_owner(private_key, &encoded_key); |
| 515 | if (status != PSA_SUCCESS) { |
| 516 | return status; |
| 517 | } |
| 518 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 519 | return psa_key_derivation_key_agreement(operation, step, |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 520 | encoded_key, |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 521 | peer_key, |
| 522 | peer_key_length); |
| 523 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 524 | } |
| 525 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 526 | psa_status_t tfm_crypto_raw_key_agreement(psa_invec in_vec[], |
| 527 | size_t in_len, |
| 528 | psa_outvec out_vec[], |
| 529 | size_t out_len) |
| 530 | { |
| 531 | #ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED |
| 532 | return PSA_ERROR_NOT_SUPPORTED; |
| 533 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 534 | |
| 535 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 536 | |
| 537 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 538 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 539 | } |
| 540 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 541 | uint8_t *output = out_vec[0].base; |
| 542 | size_t output_size = out_vec[0].len; |
| 543 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 544 | psa_key_id_t private_key = iov->key_id; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 545 | const uint8_t *peer_key = in_vec[1].base; |
| 546 | size_t peer_key_length = in_vec[1].len; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 547 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame] | 548 | psa_status_t status; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 549 | |
| 550 | status = tfm_crypto_encode_id_and_owner(private_key, &encoded_key); |
| 551 | if (status != PSA_SUCCESS) { |
| 552 | return status; |
| 553 | } |
| 554 | |
| 555 | return psa_raw_key_agreement(alg, encoded_key, peer_key, peer_key_length, |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 556 | output, output_size, &out_vec[0].len); |
| 557 | #endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */ |
| 558 | } |
| 559 | /*!@}*/ |