Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 12 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 13 | #include "tfm_crypto_api.h" |
| 14 | #include "tfm_crypto_defs.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 15 | #include "tfm_crypto_private.h" |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 16 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 17 | #ifndef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 18 | #error "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER must be selected in Mbed TLS config file" |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 19 | #endif |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 20 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 21 | /*! |
| 22 | * \defgroup public Public functions |
| 23 | * |
| 24 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 25 | /*!@{*/ |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 26 | psa_status_t tfm_crypto_key_attributes_from_client( |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 27 | const struct psa_client_key_attributes_s *client_key_attr, |
| 28 | int32_t client_id, |
| 29 | psa_key_attributes_t *key_attributes) |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 30 | { |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 31 | psa_core_key_attributes_t *core; |
| 32 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 33 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 34 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 35 | } |
| 36 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 37 | *key_attributes = psa_key_attributes_init(); |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 38 | core = &(key_attributes->MBEDTLS_PRIVATE(core)); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 39 | |
| 40 | /* Copy core key attributes from the client core key attributes */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 41 | core->MBEDTLS_PRIVATE(type) = client_key_attr->type; |
| 42 | core->MBEDTLS_PRIVATE(lifetime) = client_key_attr->lifetime; |
| 43 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = |
| 44 | client_key_attr->usage; |
| 45 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = |
| 46 | client_key_attr->alg; |
| 47 | core->MBEDTLS_PRIVATE(bits) = client_key_attr->bits; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 48 | |
| 49 | /* Use the client key id as the key_id and its partition id as the owner */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 50 | core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = client_key_attr->id; |
| 51 | core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = client_id; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 52 | |
| 53 | return PSA_SUCCESS; |
| 54 | } |
| 55 | |
| 56 | psa_status_t tfm_crypto_key_attributes_to_client( |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 57 | const psa_key_attributes_t *key_attributes, |
| 58 | struct psa_client_key_attributes_s *client_key_attr) |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 59 | { |
| 60 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 61 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 62 | } |
| 63 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 64 | struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT; |
| 65 | *client_key_attr = v; |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 66 | psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 67 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 68 | /* Copy core key attributes from the client core key attributes */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 69 | client_key_attr->type = core.MBEDTLS_PRIVATE(type); |
| 70 | client_key_attr->lifetime = core.MBEDTLS_PRIVATE(lifetime); |
| 71 | client_key_attr->usage = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
| 72 | client_key_attr->alg = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
| 73 | client_key_attr->bits = core.MBEDTLS_PRIVATE(bits); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 74 | |
| 75 | /* Return the key_id as the client key id, do not return the owner */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 76 | client_key_attr->id = core.MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 77 | |
| 78 | return PSA_SUCCESS; |
| 79 | } |
| 80 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 81 | psa_status_t tfm_crypto_encode_id_and_owner(psa_key_id_t key_id, |
| 82 | mbedtls_svc_key_id_t *enc_key_ptr) |
| 83 | { |
| 84 | int32_t partition_id = 0; |
| 85 | psa_status_t status = tfm_crypto_get_caller_id(&partition_id); |
| 86 | |
| 87 | if (status != PSA_SUCCESS) { |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | /* If Null Pointer, return PSA_ERROR_PROGRAMMER_ERROR */ |
| 92 | if (enc_key_ptr == NULL) { |
| 93 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 94 | } |
| 95 | |
| 96 | /* Use the client key id as the key_id and its partition id as the owner */ |
| 97 | *enc_key_ptr = mbedtls_svc_key_id_make(partition_id, key_id); |
| 98 | |
| 99 | return PSA_SUCCESS; |
| 100 | } |
| 101 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 102 | psa_status_t tfm_crypto_set_key_domain_parameters(psa_invec in_vec[], |
| 103 | size_t in_len, |
| 104 | psa_outvec out_vec[], |
| 105 | size_t out_len) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 106 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 107 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 108 | return PSA_ERROR_NOT_SUPPORTED; |
| 109 | #else |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 110 | /* FixMe: To be implemented */ |
| 111 | return PSA_ERROR_NOT_SUPPORTED; |
| 112 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 113 | } |
| 114 | |
| 115 | psa_status_t tfm_crypto_get_key_domain_parameters(psa_invec in_vec[], |
| 116 | size_t in_len, |
| 117 | psa_outvec out_vec[], |
| 118 | size_t out_len) |
| 119 | { |
| 120 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
| 121 | return PSA_ERROR_NOT_SUPPORTED; |
| 122 | #else |
| 123 | /* FixMe: To be implemented */ |
| 124 | return PSA_ERROR_NOT_SUPPORTED; |
| 125 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 126 | } |
| 127 | |
| 128 | psa_status_t tfm_crypto_import_key(psa_invec in_vec[], |
| 129 | size_t in_len, |
| 130 | psa_outvec out_vec[], |
| 131 | size_t out_len) |
| 132 | { |
| 133 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
| 134 | return PSA_ERROR_NOT_SUPPORTED; |
| 135 | #else |
| 136 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 137 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 3, out_len, 1, 1); |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 138 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 139 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 140 | (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) || |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 141 | (out_vec[0].len != sizeof(psa_key_id_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 142 | return PSA_ERROR_PROGRAMMER_ERROR; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 143 | } |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 144 | 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] | 145 | const uint8_t *data = in_vec[2].base; |
| 146 | size_t data_length = in_vec[2].len; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 147 | psa_key_id_t *psa_key = out_vec[0].base; |
| 148 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 149 | psa_status_t status; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 150 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 151 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 152 | int32_t partition_id = 0; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 153 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 154 | status = tfm_crypto_get_caller_id(&partition_id); |
| 155 | if (status != PSA_SUCCESS) { |
| 156 | return status; |
| 157 | } |
| 158 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 159 | status = tfm_crypto_key_attributes_from_client(client_key_attr, |
| 160 | partition_id, |
| 161 | &key_attributes); |
| 162 | if (status != PSA_SUCCESS) { |
| 163 | return status; |
| 164 | } |
| 165 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 166 | status = psa_import_key(&key_attributes, data, data_length, &encoded_key); |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 167 | if (status != PSA_SUCCESS) { |
| 168 | return status; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 169 | } |
| 170 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 171 | /* Update the imported key id */ |
| 172 | *psa_key = encoded_key.MBEDTLS_PRIVATE(key_id); |
| 173 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 174 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 175 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 178 | psa_status_t tfm_crypto_open_key(psa_invec in_vec[], |
| 179 | size_t in_len, |
| 180 | psa_outvec out_vec[], |
| 181 | size_t out_len) |
| 182 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 183 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 184 | return PSA_ERROR_NOT_SUPPORTED; |
| 185 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 186 | |
| 187 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1); |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 188 | |
| 189 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 190 | (in_vec[1].len != sizeof(psa_key_id_t)) || |
| 191 | (out_vec[0].len != sizeof(psa_key_id_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 192 | return PSA_ERROR_PROGRAMMER_ERROR; |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 195 | psa_key_id_t client_key_id = *((psa_key_id_t *)in_vec[1].base); |
| 196 | psa_key_id_t *key = out_vec[0].base; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 197 | psa_status_t status; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 198 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 199 | int32_t partition_id = 0; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 200 | |
| 201 | status = tfm_crypto_get_caller_id(&partition_id); |
| 202 | if (status != PSA_SUCCESS) { |
| 203 | return status; |
| 204 | } |
| 205 | |
| 206 | /* Use the client key id as the key_id and its partition id as the owner */ |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 207 | encoded_key = mbedtls_svc_key_id_make(partition_id, client_key_id); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 208 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 209 | status = psa_open_key(encoded_key, &encoded_key); |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 210 | if (status != PSA_SUCCESS) { |
| 211 | return status; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 214 | *key = encoded_key.MBEDTLS_PRIVATE(key_id); |
| 215 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 216 | return status; |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 217 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 218 | } |
| 219 | |
| 220 | psa_status_t tfm_crypto_close_key(psa_invec in_vec[], |
| 221 | size_t in_len, |
| 222 | psa_outvec out_vec[], |
| 223 | size_t out_len) |
| 224 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 225 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 226 | return PSA_ERROR_NOT_SUPPORTED; |
| 227 | #else |
| 228 | (void)out_vec; |
| 229 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 230 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0); |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +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; |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 234 | } |
| 235 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 236 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 237 | psa_key_id_t key = iov->key_id; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 238 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 239 | int32_t partition_id = 0; |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 240 | psa_status_t status; |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 241 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 242 | status = tfm_crypto_get_caller_id(&partition_id); |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 243 | if (status != PSA_SUCCESS) { |
| 244 | return status; |
| 245 | } |
| 246 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 247 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 248 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 249 | return psa_close_key(encoded_key); |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 250 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 251 | } |
| 252 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 253 | psa_status_t tfm_crypto_destroy_key(psa_invec in_vec[], |
| 254 | size_t in_len, |
| 255 | psa_outvec out_vec[], |
| 256 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 257 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 258 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 259 | return PSA_ERROR_NOT_SUPPORTED; |
| 260 | #else |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 261 | (void)out_vec; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 262 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 263 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 264 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 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 | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 267 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 268 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 269 | psa_key_id_t key = iov->key_id; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 270 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 271 | int32_t partition_id = 0; |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 272 | psa_status_t status; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 273 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 274 | status = tfm_crypto_get_caller_id(&partition_id); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 275 | if (status != PSA_SUCCESS) { |
| 276 | return status; |
| 277 | } |
| 278 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 279 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 280 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 281 | return psa_destroy_key(encoded_key); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 282 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 285 | psa_status_t tfm_crypto_get_key_attributes(psa_invec in_vec[], |
| 286 | size_t in_len, |
| 287 | psa_outvec out_vec[], |
| 288 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 289 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 290 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 291 | return PSA_ERROR_NOT_SUPPORTED; |
| 292 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 293 | |
| 294 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 295 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 296 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 297 | (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 298 | return PSA_ERROR_PROGRAMMER_ERROR; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 299 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 300 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 301 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 302 | psa_key_id_t key = iov->key_id; |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 303 | struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 304 | psa_status_t status; |
| 305 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 306 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 307 | int32_t partition_id = 0; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 308 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 309 | status = tfm_crypto_get_caller_id(&partition_id); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 310 | if (status != PSA_SUCCESS) { |
| 311 | return status; |
| 312 | } |
| 313 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 314 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 315 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 316 | status = psa_get_key_attributes(encoded_key, &key_attributes); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 317 | if (status == PSA_SUCCESS) { |
| 318 | status = tfm_crypto_key_attributes_to_client(&key_attributes, |
| 319 | client_key_attr); |
| 320 | } |
| 321 | |
| 322 | return status; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 323 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 324 | } |
| 325 | |
| 326 | psa_status_t tfm_crypto_reset_key_attributes(psa_invec in_vec[], |
| 327 | size_t in_len, |
| 328 | psa_outvec out_vec[], |
| 329 | size_t out_len) |
| 330 | { |
| 331 | #if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0) |
| 332 | return PSA_ERROR_NOT_SUPPORTED; |
| 333 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 334 | |
| 335 | 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] | 336 | |
| 337 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 338 | (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 339 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 342 | struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 343 | psa_status_t status; |
| 344 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 345 | int32_t partition_id; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 346 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 347 | status = tfm_crypto_get_caller_id(&partition_id); |
| 348 | if (status != PSA_SUCCESS) { |
| 349 | return status; |
| 350 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 351 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 352 | status = tfm_crypto_key_attributes_from_client(client_key_attr, |
| 353 | partition_id, |
| 354 | &key_attributes); |
| 355 | if (status != PSA_SUCCESS) { |
| 356 | return status; |
| 357 | } |
| 358 | |
| 359 | psa_reset_key_attributes(&key_attributes); |
| 360 | |
| 361 | return tfm_crypto_key_attributes_to_client(&key_attributes, |
| 362 | client_key_attr); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 363 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 366 | psa_status_t tfm_crypto_export_key(psa_invec in_vec[], |
| 367 | size_t in_len, |
| 368 | psa_outvec out_vec[], |
| 369 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 370 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 371 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 372 | return PSA_ERROR_NOT_SUPPORTED; |
| 373 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 374 | |
| 375 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 376 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 377 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 378 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 379 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 380 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 381 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 382 | psa_key_id_t key = iov->key_id; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 383 | uint8_t *data = out_vec[0].base; |
| 384 | size_t data_size = out_vec[0].len; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 385 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 386 | int32_t partition_id = 0; |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 387 | psa_status_t status; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 388 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 389 | status = tfm_crypto_get_caller_id(&partition_id); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 390 | if (status != PSA_SUCCESS) { |
| 391 | return status; |
| 392 | } |
| 393 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 394 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 395 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 396 | return psa_export_key(encoded_key, data, data_size, |
| 397 | &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 398 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 401 | psa_status_t tfm_crypto_export_public_key(psa_invec in_vec[], |
| 402 | size_t in_len, |
| 403 | psa_outvec out_vec[], |
| 404 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 405 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 406 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 407 | return PSA_ERROR_NOT_SUPPORTED; |
| 408 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 409 | |
| 410 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1); |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 411 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 412 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 413 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 414 | } |
| 415 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 416 | psa_key_id_t key = iov->key_id; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 417 | uint8_t *data = out_vec[0].base; |
| 418 | size_t data_size = out_vec[0].len; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 419 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 420 | int32_t partition_id = 0; |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 421 | psa_status_t status; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 422 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 423 | status = tfm_crypto_get_caller_id(&partition_id); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 424 | if (status != PSA_SUCCESS) { |
| 425 | return status; |
| 426 | } |
| 427 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 428 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 429 | |
| 430 | return psa_export_public_key(encoded_key, data, data_size, |
| 431 | &(out_vec[0].len)); |
| 432 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
| 433 | } |
| 434 | |
| 435 | psa_status_t tfm_crypto_purge_key(psa_invec in_vec[], |
| 436 | size_t in_len, |
| 437 | psa_outvec out_vec[], |
| 438 | size_t out_len) |
| 439 | { |
| 440 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
| 441 | return PSA_ERROR_NOT_SUPPORTED; |
| 442 | #else |
| 443 | (void)out_vec; |
| 444 | |
| 445 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0); |
| 446 | |
| 447 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
| 448 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 449 | } |
| 450 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 451 | psa_key_id_t key = iov->key_id; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 452 | mbedtls_svc_key_id_t encoded_key; |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 453 | int32_t partition_id = 0; |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 454 | psa_status_t status; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 455 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 456 | status = tfm_crypto_get_caller_id(&partition_id); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 457 | if (status != PSA_SUCCESS) { |
| 458 | return status; |
| 459 | } |
| 460 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 461 | encoded_key = mbedtls_svc_key_id_make(partition_id, key); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 462 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 463 | return psa_purge_key(encoded_key); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 464 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | psa_status_t tfm_crypto_copy_key(psa_invec in_vec[], |
| 468 | size_t in_len, |
| 469 | psa_outvec out_vec[], |
| 470 | size_t out_len) |
| 471 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 472 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 473 | return PSA_ERROR_NOT_SUPPORTED; |
| 474 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 475 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 476 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 477 | |
| 478 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 479 | (out_vec[0].len != sizeof(psa_key_id_t)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 480 | (in_vec[1].len != sizeof(struct psa_client_key_attributes_s))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 481 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 482 | } |
| 483 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 484 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 485 | psa_key_id_t source_key_id = iov->key_id; |
| 486 | psa_key_id_t *target_key_id = out_vec[0].base; |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 487 | 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] | 488 | psa_status_t status; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 489 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 490 | int32_t partition_id = 0; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 491 | mbedtls_svc_key_id_t target_key; |
| 492 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 493 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 494 | status = tfm_crypto_get_caller_id(&partition_id); |
| 495 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 496 | return status; |
| 497 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 498 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 499 | status = tfm_crypto_key_attributes_from_client(client_key_attr, |
| 500 | partition_id, |
| 501 | &key_attributes); |
| 502 | if (status != PSA_SUCCESS) { |
| 503 | return status; |
| 504 | } |
| 505 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 506 | encoded_key = mbedtls_svc_key_id_make(partition_id, source_key_id); |
| 507 | |
| 508 | status = psa_copy_key(encoded_key, &key_attributes, &target_key); |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 509 | if (status != PSA_SUCCESS) { |
| 510 | return status; |
| 511 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 512 | |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 513 | *target_key_id = target_key.MBEDTLS_PRIVATE(key_id); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 514 | |
| 515 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 516 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 519 | psa_status_t tfm_crypto_generate_key(psa_invec in_vec[], |
| 520 | size_t in_len, |
| 521 | psa_outvec out_vec[], |
| 522 | size_t out_len) |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 523 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 524 | #ifdef TFM_CRYPTO_KEY_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 525 | return PSA_ERROR_NOT_SUPPORTED; |
| 526 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 527 | |
| 528 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1); |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 529 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 530 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 531 | (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) || |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 532 | (out_vec[0].len != sizeof(psa_key_id_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 533 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 534 | } |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 535 | psa_key_id_t *key_handle = out_vec[0].base; |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 536 | 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] | 537 | psa_status_t status; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 538 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 539 | int32_t partition_id = 0; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 540 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 541 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 542 | status = tfm_crypto_get_caller_id(&partition_id); |
| 543 | if (status != PSA_SUCCESS) { |
| 544 | return status; |
| 545 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 546 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 547 | status = tfm_crypto_key_attributes_from_client(client_key_attr, |
| 548 | partition_id, |
| 549 | &key_attributes); |
| 550 | if (status != PSA_SUCCESS) { |
| 551 | return status; |
| 552 | } |
| 553 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 554 | status = psa_generate_key(&key_attributes, &encoded_key); |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 555 | if (status != PSA_SUCCESS) { |
| 556 | return status; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 557 | } |
| 558 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame^] | 559 | *key_handle = encoded_key.MBEDTLS_PRIVATE(key_id); |
| 560 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 561 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 562 | #endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */ |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 563 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 564 | /*!@}*/ |