blob: 892ce111e762e6b6036fc9c4a78c211d13eb78e9 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Foxefd82732018-11-26 10:34:32 +00008#include <stddef.h>
Jamie Fox0e54ebc2019-04-09 14:21:04 +01009#include <stdint.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010012
Jamie Fox0e54ebc2019-04-09 14:21:04 +010013#include "tfm_crypto_api.h"
14#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010015#include "tfm_crypto_private.h"
Jamie Fox82b87ca2018-12-11 16:41:11 +000016
David Hu42e77b52021-07-24 21:14:30 +080017#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 Angelis60a6fe62019-06-18 15:27:34 +010019#endif
David Hu105b4872021-05-19 16:43:19 +080020
Antonio de Angelis8908f472018-08-31 15:44:25 +010021/*!
22 * \defgroup public Public functions
23 *
24 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010025/*!@{*/
Jamie Fox98ab4412020-01-17 17:12:30 +000026psa_status_t tfm_crypto_key_attributes_from_client(
Maulik Patel28659c42021-01-06 14:09:22 +000027 const struct psa_client_key_attributes_s *client_key_attr,
28 int32_t client_id,
29 psa_key_attributes_t *key_attributes)
Jamie Fox98ab4412020-01-17 17:12:30 +000030{
Summer Qin359167d2021-07-05 18:11:50 +080031 psa_core_key_attributes_t *core;
32
Jamie Fox98ab4412020-01-17 17:12:30 +000033 if (client_key_attr == NULL || key_attributes == NULL) {
34 return PSA_ERROR_PROGRAMMER_ERROR;
35 }
36
Soby Mathewd7b79f22020-05-21 15:06:54 +010037 *key_attributes = psa_key_attributes_init();
Summer Qin359167d2021-07-05 18:11:50 +080038 core = &(key_attributes->MBEDTLS_PRIVATE(core));
Jamie Fox98ab4412020-01-17 17:12:30 +000039
40 /* Copy core key attributes from the client core key attributes */
Summer Qin359167d2021-07-05 18:11:50 +080041 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 Fox98ab4412020-01-17 17:12:30 +000048
49 /* Use the client key id as the key_id and its partition id as the owner */
Summer Qin359167d2021-07-05 18:11:50 +080050 core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = client_key_attr->id;
51 core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = client_id;
Jamie Fox98ab4412020-01-17 17:12:30 +000052
53 return PSA_SUCCESS;
54}
55
56psa_status_t tfm_crypto_key_attributes_to_client(
Maulik Patel28659c42021-01-06 14:09:22 +000057 const psa_key_attributes_t *key_attributes,
58 struct psa_client_key_attributes_s *client_key_attr)
Jamie Fox98ab4412020-01-17 17:12:30 +000059{
60 if (client_key_attr == NULL || key_attributes == NULL) {
61 return PSA_ERROR_PROGRAMMER_ERROR;
62 }
63
Soby Mathewd7b79f22020-05-21 15:06:54 +010064 struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT;
65 *client_key_attr = v;
Summer Qin359167d2021-07-05 18:11:50 +080066 psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core);
Jamie Fox98ab4412020-01-17 17:12:30 +000067
Soby Mathewd7b79f22020-05-21 15:06:54 +010068 /* Copy core key attributes from the client core key attributes */
Summer Qin359167d2021-07-05 18:11:50 +080069 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 Fox98ab4412020-01-17 17:12:30 +000074
75 /* Return the key_id as the client key id, do not return the owner */
Summer Qin359167d2021-07-05 18:11:50 +080076 client_key_attr->id = core.MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id);
Jamie Fox98ab4412020-01-17 17:12:30 +000077
78 return PSA_SUCCESS;
79}
80
Maulik Patel28659c42021-01-06 14:09:22 +000081psa_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 Angelis04debbd2019-10-14 12:12:52 +0100102psa_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 Angeliscf85ba22018-10-09 13:29:40 +0100106{
Kevin Peng96f802e2019-12-26 16:10:25 +0800107#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100108 return PSA_ERROR_NOT_SUPPORTED;
109#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100110 /* FixMe: To be implemented */
111 return PSA_ERROR_NOT_SUPPORTED;
112#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
113}
114
115psa_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
128psa_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 Mathewd8abdfd2020-10-14 10:28:01 +0100137 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 3, out_len, 1, 1);
Jamie Foxefd82732018-11-26 10:34:32 +0000138
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100139 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100140 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) ||
Maulik Patel28659c42021-01-06 14:09:22 +0000141 (out_vec[0].len != sizeof(psa_key_id_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100142 return PSA_ERROR_PROGRAMMER_ERROR;
Jamie Foxefd82732018-11-26 10:34:32 +0000143 }
Soby Mathewd7b79f22020-05-21 15:06:54 +0100144 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100145 const uint8_t *data = in_vec[2].base;
146 size_t data_length = in_vec[2].len;
Maulik Patel28659c42021-01-06 14:09:22 +0000147 psa_key_id_t *psa_key = out_vec[0].base;
148
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100149 psa_status_t status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000150 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Maulik Patel28659c42021-01-06 14:09:22 +0000151 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100152 int32_t partition_id = 0;
Jamie Foxefd82732018-11-26 10:34:32 +0000153
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100154 status = tfm_crypto_get_caller_id(&partition_id);
155 if (status != PSA_SUCCESS) {
156 return status;
157 }
158
Jamie Fox98ab4412020-01-17 17:12:30 +0000159 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 Patel28659c42021-01-06 14:09:22 +0000166 status = psa_import_key(&key_attributes, data, data_length, &encoded_key);
David Hu42e77b52021-07-24 21:14:30 +0800167 if (status != PSA_SUCCESS) {
168 return status;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100169 }
170
David Hu42e77b52021-07-24 21:14:30 +0800171 /* Update the imported key id */
172 *psa_key = encoded_key.MBEDTLS_PRIVATE(key_id);
173
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100174 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100175#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000176}
177
Jamie Foxdadb4e82019-09-03 17:59:41 +0100178psa_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 Peng96f802e2019-12-26 16:10:25 +0800183#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Jamie Foxdadb4e82019-09-03 17:59:41 +0100184 return PSA_ERROR_NOT_SUPPORTED;
185#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100186
187 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1);
Jamie Foxdadb4e82019-09-03 17:59:41 +0100188
189 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Maulik Patel28659c42021-01-06 14:09:22 +0000190 (in_vec[1].len != sizeof(psa_key_id_t)) ||
191 (out_vec[0].len != sizeof(psa_key_id_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100192 return PSA_ERROR_PROGRAMMER_ERROR;
Jamie Foxdadb4e82019-09-03 17:59:41 +0100193 }
194
Maulik Patel28659c42021-01-06 14:09:22 +0000195 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 Fox98ab4412020-01-17 17:12:30 +0000197 psa_status_t status;
Maulik Patel28659c42021-01-06 14:09:22 +0000198 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800199 int32_t partition_id = 0;
Jamie Fox98ab4412020-01-17 17:12:30 +0000200
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 Patel28659c42021-01-06 14:09:22 +0000207 encoded_key = mbedtls_svc_key_id_make(partition_id, client_key_id);
Jamie Fox98ab4412020-01-17 17:12:30 +0000208
Maulik Patel28659c42021-01-06 14:09:22 +0000209 status = psa_open_key(encoded_key, &encoded_key);
David Hu42e77b52021-07-24 21:14:30 +0800210 if (status != PSA_SUCCESS) {
211 return status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000212 }
213
David Hu42e77b52021-07-24 21:14:30 +0800214 *key = encoded_key.MBEDTLS_PRIVATE(key_id);
215
Jamie Fox98ab4412020-01-17 17:12:30 +0000216 return status;
Jamie Foxdadb4e82019-09-03 17:59:41 +0100217#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
218}
219
220psa_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 Peng96f802e2019-12-26 16:10:25 +0800225#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Jamie Foxdadb4e82019-09-03 17:59:41 +0100226 return PSA_ERROR_NOT_SUPPORTED;
227#else
228 (void)out_vec;
229
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100230 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0);
Jamie Foxdadb4e82019-09-03 17:59:41 +0100231
232 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100233 return PSA_ERROR_PROGRAMMER_ERROR;
Jamie Foxdadb4e82019-09-03 17:59:41 +0100234 }
235 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
236
Maulik Patel28659c42021-01-06 14:09:22 +0000237 psa_key_id_t key = iov->key_id;
Maulik Patel28659c42021-01-06 14:09:22 +0000238 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800239 int32_t partition_id = 0;
David Hu105b4872021-05-19 16:43:19 +0800240 psa_status_t status;
Jamie Foxdadb4e82019-09-03 17:59:41 +0100241
David Hu42e77b52021-07-24 21:14:30 +0800242 status = tfm_crypto_get_caller_id(&partition_id);
Jamie Foxdadb4e82019-09-03 17:59:41 +0100243 if (status != PSA_SUCCESS) {
244 return status;
245 }
246
David Hu42e77b52021-07-24 21:14:30 +0800247 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
Jamie Foxdadb4e82019-09-03 17:59:41 +0100248
David Hu42e77b52021-07-24 21:14:30 +0800249 return psa_close_key(encoded_key);
Jamie Foxdadb4e82019-09-03 17:59:41 +0100250#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
251}
252
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000253psa_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 Angelis8908f472018-08-31 15:44:25 +0100257{
Kevin Peng96f802e2019-12-26 16:10:25 +0800258#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100259 return PSA_ERROR_NOT_SUPPORTED;
260#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100261 (void)out_vec;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100262
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100263 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000264
Antonio de Angelis4743e672019-04-11 11:38:48 +0100265 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100266 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000267 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100268 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000269 psa_key_id_t key = iov->key_id;
Maulik Patel28659c42021-01-06 14:09:22 +0000270 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800271 int32_t partition_id = 0;
David Hu105b4872021-05-19 16:43:19 +0800272 psa_status_t status;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000273
David Hu42e77b52021-07-24 21:14:30 +0800274 status = tfm_crypto_get_caller_id(&partition_id);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100275 if (status != PSA_SUCCESS) {
276 return status;
277 }
278
David Hu42e77b52021-07-24 21:14:30 +0800279 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100280
David Hu42e77b52021-07-24 21:14:30 +0800281 return psa_destroy_key(encoded_key);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100282#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100283}
284
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100285psa_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 Angelis8908f472018-08-31 15:44:25 +0100289{
Kevin Peng96f802e2019-12-26 16:10:25 +0800290#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100291 return PSA_ERROR_NOT_SUPPORTED;
292#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100293
294 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Jamie Foxefd82732018-11-26 10:34:32 +0000295
Antonio de Angelis4743e672019-04-11 11:38:48 +0100296 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100297 (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100298 return PSA_ERROR_PROGRAMMER_ERROR;
Jamie Foxefd82732018-11-26 10:34:32 +0000299 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100300 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Foxefd82732018-11-26 10:34:32 +0000301
Maulik Patel28659c42021-01-06 14:09:22 +0000302 psa_key_id_t key = iov->key_id;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100303 struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base;
Jamie Fox98ab4412020-01-17 17:12:30 +0000304 psa_status_t status;
305 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Maulik Patel28659c42021-01-06 14:09:22 +0000306 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800307 int32_t partition_id = 0;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000308
David Hu42e77b52021-07-24 21:14:30 +0800309 status = tfm_crypto_get_caller_id(&partition_id);
Jamie Fox98ab4412020-01-17 17:12:30 +0000310 if (status != PSA_SUCCESS) {
311 return status;
312 }
313
David Hu42e77b52021-07-24 21:14:30 +0800314 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
Jamie Fox98ab4412020-01-17 17:12:30 +0000315
Maulik Patel28659c42021-01-06 14:09:22 +0000316 status = psa_get_key_attributes(encoded_key, &key_attributes);
Jamie Fox98ab4412020-01-17 17:12:30 +0000317 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 Angelis04debbd2019-10-14 12:12:52 +0100323#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
324}
325
326psa_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 Mathewd8abdfd2020-10-14 10:28:01 +0100334
335 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100336
337 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100338 (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100339 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100340 }
341
Soby Mathewd7b79f22020-05-21 15:06:54 +0100342 struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base;
Jamie Fox98ab4412020-01-17 17:12:30 +0000343 psa_status_t status;
344 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
345 int32_t partition_id;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100346
Jamie Fox98ab4412020-01-17 17:12:30 +0000347 status = tfm_crypto_get_caller_id(&partition_id);
348 if (status != PSA_SUCCESS) {
349 return status;
350 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100351
Jamie Fox98ab4412020-01-17 17:12:30 +0000352 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 Angelis7740b382019-07-16 10:59:25 +0100363#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100364}
365
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000366psa_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 Angelis8908f472018-08-31 15:44:25 +0100370{
Kevin Peng96f802e2019-12-26 16:10:25 +0800371#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100372 return PSA_ERROR_NOT_SUPPORTED;
373#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100374
375 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100376
Antonio de Angelis4743e672019-04-11 11:38:48 +0100377 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100378 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100379 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100380 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100381
Maulik Patel28659c42021-01-06 14:09:22 +0000382 psa_key_id_t key = iov->key_id;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000383 uint8_t *data = out_vec[0].base;
384 size_t data_size = out_vec[0].len;
Maulik Patel28659c42021-01-06 14:09:22 +0000385 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800386 int32_t partition_id = 0;
David Hu105b4872021-05-19 16:43:19 +0800387 psa_status_t status;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000388
David Hu42e77b52021-07-24 21:14:30 +0800389 status = tfm_crypto_get_caller_id(&partition_id);
Maulik Patel28659c42021-01-06 14:09:22 +0000390 if (status != PSA_SUCCESS) {
391 return status;
392 }
393
David Hu42e77b52021-07-24 21:14:30 +0800394 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
David Hu105b4872021-05-19 16:43:19 +0800395
Maulik Patel28659c42021-01-06 14:09:22 +0000396 return psa_export_key(encoded_key, data, data_size,
397 &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100398#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100399}
400
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000401psa_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 Angelis8908f472018-08-31 15:44:25 +0100405{
Kevin Peng96f802e2019-12-26 16:10:25 +0800406#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100407 return PSA_ERROR_NOT_SUPPORTED;
408#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100409
410 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1);
Hugues de Valon8b442442019-02-19 14:30:52 +0000411
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100412 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100413 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100414 }
415 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000416 psa_key_id_t key = iov->key_id;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100417 uint8_t *data = out_vec[0].base;
418 size_t data_size = out_vec[0].len;
Maulik Patel28659c42021-01-06 14:09:22 +0000419 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800420 int32_t partition_id = 0;
David Hu105b4872021-05-19 16:43:19 +0800421 psa_status_t status;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100422
David Hu42e77b52021-07-24 21:14:30 +0800423 status = tfm_crypto_get_caller_id(&partition_id);
Maulik Patel28659c42021-01-06 14:09:22 +0000424 if (status != PSA_SUCCESS) {
425 return status;
426 }
427
David Hu42e77b52021-07-24 21:14:30 +0800428 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
Maulik Patel28659c42021-01-06 14:09:22 +0000429
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
435psa_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 Patel28659c42021-01-06 14:09:22 +0000452 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800453 int32_t partition_id = 0;
David Hu105b4872021-05-19 16:43:19 +0800454 psa_status_t status;
Maulik Patel28659c42021-01-06 14:09:22 +0000455
David Hu42e77b52021-07-24 21:14:30 +0800456 status = tfm_crypto_get_caller_id(&partition_id);
Maulik Patel28659c42021-01-06 14:09:22 +0000457 if (status != PSA_SUCCESS) {
458 return status;
459 }
460
David Hu42e77b52021-07-24 21:14:30 +0800461 encoded_key = mbedtls_svc_key_id_make(partition_id, key);
Maulik Patel28659c42021-01-06 14:09:22 +0000462
David Hu42e77b52021-07-24 21:14:30 +0800463 return psa_purge_key(encoded_key);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100464#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100465}
466
467psa_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 Peng96f802e2019-12-26 16:10:25 +0800472#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100473 return PSA_ERROR_NOT_SUPPORTED;
474#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100475
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100476 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100477
478 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Maulik Patel28659c42021-01-06 14:09:22 +0000479 (out_vec[0].len != sizeof(psa_key_id_t)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100480 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100481 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100482 }
483 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
484
Maulik Patel28659c42021-01-06 14:09:22 +0000485 psa_key_id_t source_key_id = iov->key_id;
486 psa_key_id_t *target_key_id = out_vec[0].base;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100487 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100488 psa_status_t status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000489 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100490 int32_t partition_id = 0;
Maulik Patel28659c42021-01-06 14:09:22 +0000491 mbedtls_svc_key_id_t target_key;
492 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100493
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100494 status = tfm_crypto_get_caller_id(&partition_id);
495 if (status != PSA_SUCCESS) {
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100496 return status;
497 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100498
Jamie Fox98ab4412020-01-17 17:12:30 +0000499 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 Hu42e77b52021-07-24 21:14:30 +0800506 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 Patel28659c42021-01-06 14:09:22 +0000509 if (status != PSA_SUCCESS) {
510 return status;
511 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100512
Summer Qin359167d2021-07-05 18:11:50 +0800513 *target_key_id = target_key.MBEDTLS_PRIVATE(key_id);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100514
515 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100516#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000517}
518
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519psa_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 Foxefd82732018-11-26 10:34:32 +0000523{
Kevin Peng96f802e2019-12-26 16:10:25 +0800524#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100525 return PSA_ERROR_NOT_SUPPORTED;
526#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100527
528 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1);
Jamie Foxefd82732018-11-26 10:34:32 +0000529
Antonio de Angelis4743e672019-04-11 11:38:48 +0100530 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100531 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) ||
Maulik Patel28659c42021-01-06 14:09:22 +0000532 (out_vec[0].len != sizeof(psa_key_id_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100533 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000534 }
Maulik Patel28659c42021-01-06 14:09:22 +0000535 psa_key_id_t *key_handle = out_vec[0].base;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100536 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100537 psa_status_t status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000538 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100539 int32_t partition_id = 0;
Maulik Patel28659c42021-01-06 14:09:22 +0000540 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000541
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100542 status = tfm_crypto_get_caller_id(&partition_id);
543 if (status != PSA_SUCCESS) {
544 return status;
545 }
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000546
Jamie Fox98ab4412020-01-17 17:12:30 +0000547 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 Patel28659c42021-01-06 14:09:22 +0000554 status = psa_generate_key(&key_attributes, &encoded_key);
David Hu42e77b52021-07-24 21:14:30 +0800555 if (status != PSA_SUCCESS) {
556 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100557 }
558
David Hu42e77b52021-07-24 21:14:30 +0800559 *key_handle = encoded_key.MBEDTLS_PRIVATE(key_id);
560
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100561 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100562#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000563}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100564/*!@}*/