blob: ff062ac4fb47ba77fb8da2b544e5deb04bca1e18 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, 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
Summer Qin4b1d03b2019-07-02 14:56:08 +080011/* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter
Antonio de Angelis4743e672019-04-11 11:38:48 +010012 * integrity checks but this will have to be revised
13 * when the full set of error codes mandated by PSA FF
14 * is available.
15 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010016#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010017
Jamie Fox0e54ebc2019-04-09 14:21:04 +010018#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010020#include <stdbool.h>
Jamie Fox82b87ca2018-12-11 16:41:11 +000021
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010022#ifndef TFM_CRYPTO_MAX_KEY_HANDLES
23#define TFM_CRYPTO_MAX_KEY_HANDLES (16)
24#endif
25struct tfm_crypto_handle_owner_s {
26 int32_t owner; /*!< Owner of the allocated handle */
27 psa_key_handle_t handle; /*!< Allocated handle */
28 uint8_t in_use; /*!< Flag to indicate if this in use */
29};
30
Kevin Peng96f802e2019-12-26 16:10:25 +080031#ifndef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010032static struct tfm_crypto_handle_owner_s
33 handle_owner[TFM_CRYPTO_MAX_KEY_HANDLES] = {0};
Antonio de Angelis7740b382019-07-16 10:59:25 +010034#endif
Jamie Foxdadb4e82019-09-03 17:59:41 +010035
Antonio de Angelis8908f472018-08-31 15:44:25 +010036/*!
37 * \defgroup public Public functions
38 *
39 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010040/*!@{*/
Jamie Fox98ab4412020-01-17 17:12:30 +000041psa_status_t tfm_crypto_key_attributes_from_client(
Soby Mathewd7b79f22020-05-21 15:06:54 +010042 const struct psa_client_key_attributes_s *client_key_attr,
Jamie Fox98ab4412020-01-17 17:12:30 +000043 int32_t client_id,
44 psa_key_attributes_t *key_attributes)
45{
46 if (client_key_attr == NULL || key_attributes == NULL) {
47 return PSA_ERROR_PROGRAMMER_ERROR;
48 }
49
Soby Mathewd7b79f22020-05-21 15:06:54 +010050 *key_attributes = psa_key_attributes_init();
Jamie Fox98ab4412020-01-17 17:12:30 +000051
52 /* Copy core key attributes from the client core key attributes */
Soby Mathewd7b79f22020-05-21 15:06:54 +010053 key_attributes->core.type = client_key_attr->type;
54 key_attributes->core.lifetime = client_key_attr->lifetime;
55 key_attributes->core.policy.usage = client_key_attr->usage;
56 key_attributes->core.policy.alg = client_key_attr->alg;
57 key_attributes->core.bits = client_key_attr->bits;
Jamie Fox98ab4412020-01-17 17:12:30 +000058
59 /* Use the client key id as the key_id and its partition id as the owner */
Soby Mathewd7b79f22020-05-21 15:06:54 +010060 key_attributes->core.id.key_id = client_key_attr->id;
Jamie Fox98ab4412020-01-17 17:12:30 +000061 key_attributes->core.id.owner = client_id;
62
63 return PSA_SUCCESS;
64}
65
66psa_status_t tfm_crypto_key_attributes_to_client(
67 const psa_key_attributes_t *key_attributes,
Soby Mathewd7b79f22020-05-21 15:06:54 +010068 struct psa_client_key_attributes_s *client_key_attr)
Jamie Fox98ab4412020-01-17 17:12:30 +000069{
70 if (client_key_attr == NULL || key_attributes == NULL) {
71 return PSA_ERROR_PROGRAMMER_ERROR;
72 }
73
Soby Mathewd7b79f22020-05-21 15:06:54 +010074 struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT;
75 *client_key_attr = v;
Jamie Fox98ab4412020-01-17 17:12:30 +000076
Soby Mathewd7b79f22020-05-21 15:06:54 +010077 /* Copy core key attributes from the client core key attributes */
78 client_key_attr->type = key_attributes->core.type;
79 client_key_attr->lifetime = key_attributes->core.lifetime;
80 client_key_attr->usage = key_attributes->core.policy.usage;
81 client_key_attr->alg = key_attributes->core.policy.alg;
82 client_key_attr->bits = key_attributes->core.bits;
Jamie Fox98ab4412020-01-17 17:12:30 +000083
84 /* Return the key_id as the client key id, do not return the owner */
Soby Mathewd7b79f22020-05-21 15:06:54 +010085 client_key_attr->id = key_attributes->core.id.key_id;
Jamie Fox98ab4412020-01-17 17:12:30 +000086
87 return PSA_SUCCESS;
88}
89
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010090psa_status_t tfm_crypto_check_handle_owner(psa_key_handle_t handle,
91 uint32_t *index)
92{
Kevin Peng96f802e2019-12-26 16:10:25 +080093#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010094 return PSA_ERROR_NOT_SUPPORTED;
95#else
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010096 int32_t partition_id = 0;
97 uint32_t i = 0;
98 psa_status_t status;
99
100 status = tfm_crypto_get_caller_id(&partition_id);
101 if (status != PSA_SUCCESS) {
102 return status;
103 }
104
105 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
106 if (handle_owner[i].in_use && handle_owner[i].handle == handle) {
107 if (handle_owner[i].owner == partition_id) {
108 if (index != NULL) {
109 *index = i;
110 }
111 return PSA_SUCCESS;
112 } else {
113 return PSA_ERROR_NOT_PERMITTED;
114 }
115 }
116 }
117
118 return PSA_ERROR_INVALID_HANDLE;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100119#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100120}
121
Jamie Fox99360e82020-02-20 16:00:09 +0000122psa_status_t tfm_crypto_check_key_storage(uint32_t *index)
123{
124#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
125 return PSA_ERROR_NOT_SUPPORTED;
126#else
127 uint32_t i;
128
129 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
130 if (handle_owner[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
131 *index = i;
132 return PSA_SUCCESS;
133 }
134 }
135
136 return PSA_ERROR_INSUFFICIENT_MEMORY;
137#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
138}
139
140psa_status_t tfm_crypto_set_key_storage(uint32_t index,
141 psa_key_handle_t key_handle)
142{
143#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
144 return PSA_ERROR_NOT_SUPPORTED;
145#else
146 psa_status_t status;
147 int32_t partition_id;
148
149 status = tfm_crypto_get_caller_id(&partition_id);
150 if (status != PSA_SUCCESS) {
151 return status;
152 }
153
154 handle_owner[index].owner = partition_id;
155 handle_owner[index].handle = key_handle;
156 handle_owner[index].in_use = TFM_CRYPTO_IN_USE;
157
158 return PSA_SUCCESS;
159#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
160}
161
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100162psa_status_t tfm_crypto_set_key_domain_parameters(psa_invec in_vec[],
163 size_t in_len,
164 psa_outvec out_vec[],
165 size_t out_len)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100166{
Kevin Peng96f802e2019-12-26 16:10:25 +0800167#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100168 return PSA_ERROR_NOT_SUPPORTED;
169#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100170 /* FixMe: To be implemented */
171 return PSA_ERROR_NOT_SUPPORTED;
172#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
173}
174
175psa_status_t tfm_crypto_get_key_domain_parameters(psa_invec in_vec[],
176 size_t in_len,
177 psa_outvec out_vec[],
178 size_t out_len)
179{
180#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
181 return PSA_ERROR_NOT_SUPPORTED;
182#else
183 /* FixMe: To be implemented */
184 return PSA_ERROR_NOT_SUPPORTED;
185#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
186}
187
188psa_status_t tfm_crypto_import_key(psa_invec in_vec[],
189 size_t in_len,
190 psa_outvec out_vec[],
191 size_t out_len)
192{
193#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
194 return PSA_ERROR_NOT_SUPPORTED;
195#else
196
197 if ((in_len != 3) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800198 return PSA_ERROR_CONNECTION_REFUSED;
Jamie Foxefd82732018-11-26 10:34:32 +0000199 }
200
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100201 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100202 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) ||
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100203 (out_vec[0].len != sizeof(psa_key_handle_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800204 return PSA_ERROR_CONNECTION_REFUSED;
Jamie Foxefd82732018-11-26 10:34:32 +0000205 }
Soby Mathewd7b79f22020-05-21 15:06:54 +0100206 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100207 const uint8_t *data = in_vec[2].base;
208 size_t data_length = in_vec[2].len;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100209 psa_key_handle_t *key_handle = out_vec[0].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100210 psa_status_t status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000211 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100212 uint32_t i = 0;
213 int32_t partition_id = 0;
214 bool empty_found = false;
Jamie Foxefd82732018-11-26 10:34:32 +0000215
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100216 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
217 if (handle_owner[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
218 empty_found = true;
219 break;
220 }
221 }
222
223 if (!empty_found) {
224 return PSA_ERROR_INSUFFICIENT_MEMORY;
225 }
226
227 status = tfm_crypto_get_caller_id(&partition_id);
228 if (status != PSA_SUCCESS) {
229 return status;
230 }
231
Jamie Fox98ab4412020-01-17 17:12:30 +0000232 status = tfm_crypto_key_attributes_from_client(client_key_attr,
233 partition_id,
234 &key_attributes);
235 if (status != PSA_SUCCESS) {
236 return status;
237 }
238
239 status = psa_import_key(&key_attributes, data, data_length, key_handle);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100240
241 if (status == PSA_SUCCESS) {
242 handle_owner[i].owner = partition_id;
243 handle_owner[i].handle = *key_handle;
244 handle_owner[i].in_use = TFM_CRYPTO_IN_USE;
245 }
246
247 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100248#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000249}
250
Jamie Foxdadb4e82019-09-03 17:59:41 +0100251psa_status_t tfm_crypto_open_key(psa_invec in_vec[],
252 size_t in_len,
253 psa_outvec out_vec[],
254 size_t out_len)
255{
Kevin Peng96f802e2019-12-26 16:10:25 +0800256#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Jamie Foxdadb4e82019-09-03 17:59:41 +0100257 return PSA_ERROR_NOT_SUPPORTED;
258#else
259 if ((in_len != 2) || (out_len != 1)) {
260 return PSA_ERROR_CONNECTION_REFUSED;
261 }
262
263 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Jamie Fox98ab4412020-01-17 17:12:30 +0000264 (in_vec[1].len != sizeof(psa_app_key_id_t)) ||
Jamie Foxdadb4e82019-09-03 17:59:41 +0100265 (out_vec[0].len != sizeof(psa_key_handle_t))) {
266 return PSA_ERROR_CONNECTION_REFUSED;
267 }
268
Jamie Fox98ab4412020-01-17 17:12:30 +0000269 psa_app_key_id_t client_key_id = *((psa_app_key_id_t *)in_vec[1].base);
270 psa_key_handle_t *key_handle = out_vec[0].base;
271 psa_status_t status;
272 psa_key_id_t id;
273 int32_t partition_id;
274 uint32_t i;
275
276 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
277 if (handle_owner[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
278 break;
279 }
280 }
281
282 if (i == TFM_CRYPTO_MAX_KEY_HANDLES) {
283 return PSA_ERROR_INSUFFICIENT_MEMORY;
284 }
285
286 status = tfm_crypto_get_caller_id(&partition_id);
287 if (status != PSA_SUCCESS) {
288 return status;
289 }
290
291 /* Use the client key id as the key_id and its partition id as the owner */
292 id = (psa_key_id_t){ .key_id = client_key_id, .owner = partition_id };
293
294 status = psa_open_key(id, key_handle);
295
296 if (status == PSA_SUCCESS) {
297 handle_owner[i].owner = partition_id;
298 handle_owner[i].handle = *key_handle;
299 handle_owner[i].in_use = TFM_CRYPTO_IN_USE;
300 }
301
302 return status;
Jamie Foxdadb4e82019-09-03 17:59:41 +0100303#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
304}
305
306psa_status_t tfm_crypto_close_key(psa_invec in_vec[],
307 size_t in_len,
308 psa_outvec out_vec[],
309 size_t out_len)
310{
Kevin Peng96f802e2019-12-26 16:10:25 +0800311#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Jamie Foxdadb4e82019-09-03 17:59:41 +0100312 return PSA_ERROR_NOT_SUPPORTED;
313#else
314 (void)out_vec;
315
316 if ((in_len != 1) || (out_len != 0)) {
317 return PSA_ERROR_CONNECTION_REFUSED;
318 }
319
320 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
321 return PSA_ERROR_CONNECTION_REFUSED;
322 }
323 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
324
325 psa_key_handle_t key = iov->key_handle;
326 uint32_t index;
327 psa_status_t status = tfm_crypto_check_handle_owner(key, &index);
328
329 if (status != PSA_SUCCESS) {
330 return status;
331 }
332
333 status = psa_close_key(key);
334
335 if (status == PSA_SUCCESS) {
336 handle_owner[index].owner = 0;
337 handle_owner[index].handle = 0;
338 handle_owner[index].in_use = TFM_CRYPTO_NOT_IN_USE;
339 }
340
341 return status;
342#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
343}
344
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000345psa_status_t tfm_crypto_destroy_key(psa_invec in_vec[],
346 size_t in_len,
347 psa_outvec out_vec[],
348 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100349{
Kevin Peng96f802e2019-12-26 16:10:25 +0800350#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100351 return PSA_ERROR_NOT_SUPPORTED;
352#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100353 (void)out_vec;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100354
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000355 if ((in_len != 1) || (out_len != 0)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800356 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000357 }
358
Antonio de Angelis4743e672019-04-11 11:38:48 +0100359 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800360 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000361 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100362 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000363
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100364 psa_key_handle_t key = iov->key_handle;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100365 uint32_t index;
366 psa_status_t status = tfm_crypto_check_handle_owner(key, &index);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000367
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100368 if (status != PSA_SUCCESS) {
369 return status;
370 }
371
372 status = psa_destroy_key(key);
373
374 if (status == PSA_SUCCESS) {
375 handle_owner[index].owner = 0;
376 handle_owner[index].handle = 0;
377 handle_owner[index].in_use = TFM_CRYPTO_NOT_IN_USE;
378 }
379
380 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100381#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100382}
383
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100384psa_status_t tfm_crypto_get_key_attributes(psa_invec in_vec[],
385 size_t in_len,
386 psa_outvec out_vec[],
387 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100388{
Kevin Peng96f802e2019-12-26 16:10:25 +0800389#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100390 return PSA_ERROR_NOT_SUPPORTED;
391#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100392 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800393 return PSA_ERROR_CONNECTION_REFUSED;
Jamie Foxefd82732018-11-26 10:34:32 +0000394 }
395
Antonio de Angelis4743e672019-04-11 11:38:48 +0100396 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100397 (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800398 return PSA_ERROR_CONNECTION_REFUSED;
Jamie Foxefd82732018-11-26 10:34:32 +0000399 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100400 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Foxefd82732018-11-26 10:34:32 +0000401
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100402 psa_key_handle_t key = iov->key_handle;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100403 struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base;
Jamie Fox98ab4412020-01-17 17:12:30 +0000404 psa_status_t status;
405 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000406
Jamie Fox98ab4412020-01-17 17:12:30 +0000407 status = tfm_crypto_check_handle_owner(key, NULL);
408 if (status != PSA_SUCCESS) {
409 return status;
410 }
411
412 status = psa_get_key_attributes(key, &key_attributes);
413
414 if (status == PSA_SUCCESS) {
415 status = tfm_crypto_key_attributes_to_client(&key_attributes,
416 client_key_attr);
417 }
418
419 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100420#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
421}
422
423psa_status_t tfm_crypto_reset_key_attributes(psa_invec in_vec[],
424 size_t in_len,
425 psa_outvec out_vec[],
426 size_t out_len)
427{
428#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
429 return PSA_ERROR_NOT_SUPPORTED;
430#else
431 if ((in_len != 1) || (out_len != 1)) {
432 return PSA_ERROR_CONNECTION_REFUSED;
433 }
434
435 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100436 (out_vec[0].len != sizeof(struct psa_client_key_attributes_s))) {
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100437 return PSA_ERROR_CONNECTION_REFUSED;
438 }
439
Soby Mathewd7b79f22020-05-21 15:06:54 +0100440 struct psa_client_key_attributes_s *client_key_attr = out_vec[0].base;
Jamie Fox98ab4412020-01-17 17:12:30 +0000441 psa_status_t status;
442 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
443 int32_t partition_id;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100444
Jamie Fox98ab4412020-01-17 17:12:30 +0000445 status = tfm_crypto_get_caller_id(&partition_id);
446 if (status != PSA_SUCCESS) {
447 return status;
448 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100449
Jamie Fox98ab4412020-01-17 17:12:30 +0000450 status = tfm_crypto_key_attributes_from_client(client_key_attr,
451 partition_id,
452 &key_attributes);
453 if (status != PSA_SUCCESS) {
454 return status;
455 }
456
457 psa_reset_key_attributes(&key_attributes);
458
459 return tfm_crypto_key_attributes_to_client(&key_attributes,
460 client_key_attr);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100461#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100462}
463
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000464psa_status_t tfm_crypto_export_key(psa_invec in_vec[],
465 size_t in_len,
466 psa_outvec out_vec[],
467 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100468{
Kevin Peng96f802e2019-12-26 16:10:25 +0800469#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100470 return PSA_ERROR_NOT_SUPPORTED;
471#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000472 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800473 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100474 }
475
Antonio de Angelis4743e672019-04-11 11:38:48 +0100476 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800477 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100478 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100479 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100480
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100481 psa_key_handle_t key = iov->key_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000482 uint8_t *data = out_vec[0].base;
483 size_t data_size = out_vec[0].len;
484
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100485 return psa_export_key(key, data, data_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100486#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100487}
488
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000489psa_status_t tfm_crypto_export_public_key(psa_invec in_vec[],
490 size_t in_len,
491 psa_outvec out_vec[],
492 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100493{
Kevin Peng96f802e2019-12-26 16:10:25 +0800494#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100495 return PSA_ERROR_NOT_SUPPORTED;
496#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100497 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800498 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100499 }
Hugues de Valon8b442442019-02-19 14:30:52 +0000500
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100501 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800502 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100503 }
504 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
505
506 psa_key_handle_t key = iov->key_handle;
507 uint8_t *data = out_vec[0].base;
508 size_t data_size = out_vec[0].len;
509
510 return psa_export_public_key(key, data, data_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100511#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100512}
513
514psa_status_t tfm_crypto_copy_key(psa_invec in_vec[],
515 size_t in_len,
516 psa_outvec out_vec[],
517 size_t out_len)
518{
Kevin Peng96f802e2019-12-26 16:10:25 +0800519#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100520 return PSA_ERROR_NOT_SUPPORTED;
521#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100522
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100523 if ((in_len != 2) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800524 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100525 }
526
527 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100528 (out_vec[0].len != sizeof(psa_key_handle_t)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100529 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800530 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100531 }
532 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
533
534 psa_key_handle_t source_handle = iov->key_handle;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100535 psa_key_handle_t *target_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 uint32_t i = 0;
540 int32_t partition_id = 0;
541 bool empty_found = false;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100542
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100543 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
544 if (handle_owner[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
545 empty_found = true;
546 break;
547 }
Jamie Foxefd82732018-11-26 10:34:32 +0000548 }
549
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100550 if (!empty_found) {
551 return PSA_ERROR_INSUFFICIENT_MEMORY;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000552 }
553
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554 status = tfm_crypto_get_caller_id(&partition_id);
555 if (status != PSA_SUCCESS) {
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100556 return status;
557 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100558
Jamie Fox98ab4412020-01-17 17:12:30 +0000559 status = tfm_crypto_key_attributes_from_client(client_key_attr,
560 partition_id,
561 &key_attributes);
562 if (status != PSA_SUCCESS) {
563 return status;
564 }
565
566 status = psa_copy_key(source_handle, &key_attributes, target_handle);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100567
568 if (status == PSA_SUCCESS) {
569 handle_owner[i].owner = partition_id;
570 handle_owner[i].handle = *target_handle;
571 handle_owner[i].in_use = TFM_CRYPTO_IN_USE;
572 }
573
574 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100575#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000576}
577
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100578psa_status_t tfm_crypto_generate_key(psa_invec in_vec[],
579 size_t in_len,
580 psa_outvec out_vec[],
581 size_t out_len)
Jamie Foxefd82732018-11-26 10:34:32 +0000582{
Kevin Peng96f802e2019-12-26 16:10:25 +0800583#ifdef TFM_CRYPTO_KEY_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100584 return PSA_ERROR_NOT_SUPPORTED;
585#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100586 if ((in_len != 2) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800587 return PSA_ERROR_CONNECTION_REFUSED;
Jamie Foxefd82732018-11-26 10:34:32 +0000588 }
589
Antonio de Angelis4743e672019-04-11 11:38:48 +0100590 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100591 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) ||
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100592 (out_vec[0].len != sizeof(psa_key_handle_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800593 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000594 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100595 psa_key_handle_t *key_handle = out_vec[0].base;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100596 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100597 psa_status_t status;
Jamie Fox98ab4412020-01-17 17:12:30 +0000598 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100599 uint32_t i = 0;
600 int32_t partition_id = 0;
601 bool empty_found = false;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000602
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100603 for (i = 0; i < TFM_CRYPTO_MAX_KEY_HANDLES; i++) {
604 if (handle_owner[i].in_use == TFM_CRYPTO_NOT_IN_USE) {
605 empty_found = true;
606 break;
607 }
Jamie Foxefd82732018-11-26 10:34:32 +0000608 }
609
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100610 if (!empty_found) {
611 return PSA_ERROR_INSUFFICIENT_MEMORY;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000612 }
613
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100614 status = tfm_crypto_get_caller_id(&partition_id);
615 if (status != PSA_SUCCESS) {
616 return status;
617 }
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000618
Jamie Fox98ab4412020-01-17 17:12:30 +0000619 status = tfm_crypto_key_attributes_from_client(client_key_attr,
620 partition_id,
621 &key_attributes);
622 if (status != PSA_SUCCESS) {
623 return status;
624 }
625
626 status = psa_generate_key(&key_attributes, key_handle);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100627
628 if (status == PSA_SUCCESS) {
629 handle_owner[i].owner = partition_id;
630 handle_owner[i].handle = *key_handle;
631 handle_owner[i].in_use = TFM_CRYPTO_IN_USE;
632 }
633
634 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100635#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000636}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100637/*!@}*/