blob: 554cdb56d76bfba62ffcc9792f51d718209e3d04 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis377a1552018-11-22 17:02:40 +00002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00008#include "tfm_veneers.h"
9#include "tfm_crypto_defs.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000010#include "psa/crypto.h"
Antonio de Angelis05b24192019-07-04 15:28:46 +010011#include "tfm_ns_interface.h"
Edison Aicc4c6162019-06-21 13:52:49 +080012#ifdef TFM_PSA_API
13#include "psa_manifest/sid.h"
14#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000015
Antonio de Angelis4743e672019-04-11 11:38:48 +010016#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000017
Antonio de Angelis4743e672019-04-11 11:38:48 +010018#ifdef TFM_PSA_API
Jamie Foxcc31d402019-01-28 17:13:52 +000019#include "psa/client.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000020
Antonio de Angelis4743e672019-04-11 11:38:48 +010021/* Macro to check for a valid PSA handle */
22/* FixMe: Here temporarily until it's added to the framework headers */
23#define PSA_IS_HANDLE_VALID(handle) ((handle) > (psa_handle_t)0)
24
Jamie Fox0e54ebc2019-04-09 14:21:04 +010025#define PSA_CONNECT(service) \
26 psa_handle_t ipc_handle; \
Edison Aicc4c6162019-06-21 13:52:49 +080027 ipc_handle = psa_connect(service##_SID, service##_VERSION); \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010028 if (!PSA_IS_HANDLE_VALID(ipc_handle)) { \
29 return PSA_ERROR_GENERIC_ERROR; \
30 } \
Antonio de Angelis4743e672019-04-11 11:38:48 +010031
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032#define PSA_CLOSE() psa_close(ipc_handle)
Antonio de Angelis4743e672019-04-11 11:38:48 +010033
Jamie Fox0e54ebc2019-04-09 14:21:04 +010034#define API_DISPATCH(sfn_name, sfn_id) \
Summer Qin4b1d03b2019-07-02 14:56:08 +080035 psa_call(ipc_handle, PSA_IPC_CALL, \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010036 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010037 out_vec, ARRAY_SIZE(out_vec))
38
Jamie Fox0e54ebc2019-04-09 14:21:04 +010039#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
Summer Qin4b1d03b2019-07-02 14:56:08 +080040 psa_call(ipc_handle, PSA_IPC_CALL, \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010041 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010042 (psa_outvec *)NULL, 0)
43#else
Antonio de Angelis05b24192019-07-04 15:28:46 +010044#define API_DISPATCH(sfn_name, sfn_id) \
45 tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
46 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010047 (uint32_t)out_vec, ARRAY_SIZE(out_vec))
48
Antonio de Angelis05b24192019-07-04 15:28:46 +010049#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
50 tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
51 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000052 (uint32_t)NULL, 0)
Antonio de Angelis4743e672019-04-11 11:38:48 +010053#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010054
55psa_status_t psa_crypto_init(void)
56{
57 /* Service init is performed during TFM boot up,
58 * so application level initialisation is empty
59 */
60 return PSA_SUCCESS;
61}
62
Jamie Fox0e54ebc2019-04-09 14:21:04 +010063psa_status_t psa_allocate_key(psa_key_handle_t *handle)
64{
65 psa_status_t status;
66 const struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080067 .sfn_id = TFM_CRYPTO_ALLOCATE_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010068 };
69 psa_invec in_vec[] = {
70 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
71 };
72 psa_outvec out_vec[] = {
73 {.base = handle, .len = sizeof(psa_key_handle_t)},
74 };
75
76#ifdef TFM_PSA_API
77 PSA_CONNECT(TFM_CRYPTO);
78#endif
79
80 status = API_DISPATCH(tfm_crypto_allocate_key,
81 TFM_CRYPTO_ALLOCATE_KEY);
82#ifdef TFM_PSA_API
83 PSA_CLOSE();
84#endif
85
86 return status;
87}
88
89psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
90 psa_key_id_t id,
91 psa_key_handle_t *handle)
92{
93 (void)lifetime;
94 (void)id;
95 (void)handle;
96
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010097 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010098 return PSA_ERROR_NOT_SUPPORTED;
99}
100
101psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
102 psa_key_id_t id,
103 psa_key_handle_t *handle)
104{
105 (void)lifetime;
106 (void)id;
107 (void)handle;
108
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100109 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100110 return PSA_ERROR_NOT_SUPPORTED;
111}
112
113psa_status_t psa_close_key(psa_key_handle_t handle)
114{
115 (void)handle;
116
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100117 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100118 return PSA_ERROR_NOT_SUPPORTED;
119}
120
121psa_status_t psa_import_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100122 psa_key_type_t type,
123 const uint8_t *data,
124 size_t data_length)
125{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000126 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100127 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800128 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100129 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100130 .type = type,
131 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000132 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100133 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000134 {.base = data, .len = data_length}
135 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100136
Antonio de Angelis4743e672019-04-11 11:38:48 +0100137#ifdef TFM_PSA_API
138 PSA_CONNECT(TFM_CRYPTO);
139#endif
140
141 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_import_key,
142 TFM_CRYPTO_IMPORT_KEY);
143#ifdef TFM_PSA_API
144 PSA_CLOSE();
145#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100146
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000147 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100148}
149
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100150psa_status_t psa_destroy_key(psa_key_handle_t handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100151{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000152 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100153 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800154 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100155 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100156 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000157 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100158 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000159 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100160
Antonio de Angelis4743e672019-04-11 11:38:48 +0100161#ifdef TFM_PSA_API
162 PSA_CONNECT(TFM_CRYPTO);
163#endif
164
165 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
166 TFM_CRYPTO_DESTROY_KEY);
167#ifdef TFM_PSA_API
168 PSA_CLOSE();
169#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100170
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000171 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100172}
173
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100174psa_status_t psa_get_key_information(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100175 psa_key_type_t *type,
176 size_t *bits)
177{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000178 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100179 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800180 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100181 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100182 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000183 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100184 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000185 };
186 psa_outvec out_vec[] = {
187 {.base = type, .len = sizeof(psa_key_type_t)},
188 {.base = bits, .len = sizeof(size_t)}
189 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100190
Antonio de Angelis4743e672019-04-11 11:38:48 +0100191#ifdef TFM_PSA_API
192 PSA_CONNECT(TFM_CRYPTO);
193#endif
194
195 status = API_DISPATCH(tfm_crypto_get_key_information,
196 TFM_CRYPTO_GET_KEY_INFORMATION);
197#ifdef TFM_PSA_API
198 PSA_CLOSE();
199#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100200
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000201 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100202}
203
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100204psa_status_t psa_export_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100205 uint8_t *data,
206 size_t data_size,
207 size_t *data_length)
208{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000209 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100210 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800211 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100212 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100213 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000214 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100215 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000216 };
217 psa_outvec out_vec[] = {
218 {.base = data, .len = data_size}
219 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100220
Antonio de Angelis4743e672019-04-11 11:38:48 +0100221#ifdef TFM_PSA_API
222 PSA_CONNECT(TFM_CRYPTO);
223#endif
224
225 status = API_DISPATCH(tfm_crypto_export_key,
226 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100227
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000228 *data_length = out_vec[0].len;
229
Antonio de Angelis4743e672019-04-11 11:38:48 +0100230#ifdef TFM_PSA_API
231 PSA_CLOSE();
232#endif
233
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000234 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100235}
236
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100237psa_status_t psa_export_public_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100238 uint8_t *data,
239 size_t data_size,
240 size_t *data_length)
241{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100242 psa_status_t status;
243 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800244 .sfn_id = TFM_CRYPTO_EXPORT_PUBLIC_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100245 .key_handle = handle,
246 };
Hugues de Valon8b442442019-02-19 14:30:52 +0000247
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100248 psa_invec in_vec[] = {
249 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
250 };
251 psa_outvec out_vec[] = {
252 {.base = data, .len = data_size}
253 };
254
255#ifdef TFM_PSA_API
256 PSA_CONNECT(TFM_CRYPTO);
257#endif
258
259 status = API_DISPATCH(tfm_crypto_export_public_key,
260 TFM_CRYPTO_EXPORT_PUBLIC_KEY);
261
262 *data_length = out_vec[0].len;
263
264#ifdef TFM_PSA_API
265 PSA_CLOSE();
266#endif
267
268 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100269}
270
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100271psa_status_t psa_copy_key(psa_key_handle_t source_handle,
272 psa_key_handle_t target_handle,
273 const psa_key_policy_t *constraint)
Jamie Foxefd82732018-11-26 10:34:32 +0000274{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100275 psa_status_t status;
276 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800277 .sfn_id = TFM_CRYPTO_COPY_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100278 .key_handle = source_handle,
279 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000280
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100281 psa_invec in_vec[] = {
282 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
283 {.base = &target_handle, .len = sizeof(psa_key_handle_t)},
284 {.base = constraint, .len = sizeof(psa_key_policy_t)},
285 };
286
287#ifdef TFM_PSA_API
288 PSA_CONNECT(TFM_CRYPTO);
289#endif
290
291 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_copy_key,
292 TFM_CRYPTO_COPY_KEY);
293#ifdef TFM_PSA_API
294 PSA_CLOSE();
295#endif
296
297 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000298}
299
300void psa_key_policy_set_usage(psa_key_policy_t *policy,
301 psa_key_usage_t usage,
302 psa_algorithm_t alg)
303{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100304 policy->usage = usage;
305 policy->alg = alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000306}
307
308psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
309{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100310 return policy->usage;
Jamie Foxefd82732018-11-26 10:34:32 +0000311}
312
313psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
314{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100315 return policy->alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000316}
317
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100318psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000319 const psa_key_policy_t *policy)
320{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000321 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100322 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800323 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100324 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100325 };
326
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000327 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100328 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000329 {.base = policy, .len = sizeof(psa_key_policy_t)},
330 };
Jamie Foxefd82732018-11-26 10:34:32 +0000331
Antonio de Angelis4743e672019-04-11 11:38:48 +0100332#ifdef TFM_PSA_API
333 PSA_CONNECT(TFM_CRYPTO);
334#endif
335
336 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
337 TFM_CRYPTO_SET_KEY_POLICY);
338#ifdef TFM_PSA_API
339 PSA_CLOSE();
340#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000341
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000342 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000343}
344
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100345psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000346 psa_key_policy_t *policy)
347{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000348 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100349 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800350 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100351 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100352 };
353
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000354 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100355 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000356 };
357 psa_outvec out_vec[] = {
358 {.base = policy, .len = sizeof(psa_key_policy_t)},
359 };
Jamie Foxefd82732018-11-26 10:34:32 +0000360
Antonio de Angelis4743e672019-04-11 11:38:48 +0100361#ifdef TFM_PSA_API
362 PSA_CONNECT(TFM_CRYPTO);
363#endif
364
365 status = API_DISPATCH(tfm_crypto_get_key_policy,
366 TFM_CRYPTO_GET_KEY_POLICY);
367#ifdef TFM_PSA_API
368 PSA_CLOSE();
369#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000370
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000371 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000372}
373
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100374psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000375 psa_key_lifetime_t *lifetime)
376{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000377 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100378 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800379 .sfn_id = TFM_CRYPTO_GET_KEY_LIFETIME_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100380 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100381 };
382
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000383 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100384 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000385 };
386 psa_outvec out_vec[] = {
387 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
388 };
Jamie Foxefd82732018-11-26 10:34:32 +0000389
Antonio de Angelis4743e672019-04-11 11:38:48 +0100390#ifdef TFM_PSA_API
391 PSA_CONNECT(TFM_CRYPTO);
392#endif
393
394 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
395 TFM_CRYPTO_GET_KEY_LIFETIME);
396#ifdef TFM_PSA_API
397 PSA_CLOSE();
398#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000399
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000400 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000401}
402
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100403psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
404 unsigned char *iv,
405 size_t iv_size,
406 size_t *iv_length)
407{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100408 psa_status_t status;
409 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800410 .sfn_id = TFM_CRYPTO_CIPHER_GENERATE_IV_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100411 .op_handle = operation->handle,
412 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100413
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100414 psa_invec in_vec[] = {
415 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
416 };
417 psa_outvec out_vec[] = {
418 {.base = &(operation->handle), .len = sizeof(uint32_t)},
419 {.base = iv, .len = iv_size},
420 };
421
422#ifdef TFM_PSA_API
423 PSA_CONNECT(TFM_CRYPTO);
424#endif
425
426 status = API_DISPATCH(tfm_crypto_cipher_generate_iv,
427 TFM_CRYPTO_CIPHER_GENERATE_IV);
428
429 *iv_length = out_vec[1].len;
430
431#ifdef TFM_PSA_API
432 PSA_CLOSE();
433#endif
434
435 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100436}
437
Antonio de Angelis377a1552018-11-22 17:02:40 +0000438psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
439 const unsigned char *iv,
440 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100441{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000442 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100443 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800444 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100445 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100446 };
447
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000448 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100449 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000450 {.base = iv, .len = iv_length},
451 };
452 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100453 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000454 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100455
Antonio de Angelis4743e672019-04-11 11:38:48 +0100456#ifdef TFM_PSA_API
457 PSA_CONNECT(TFM_CRYPTO);
458#endif
459
460 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
461 TFM_CRYPTO_CIPHER_SET_IV);
462#ifdef TFM_PSA_API
463 PSA_CLOSE();
464#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100465
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000466 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100467}
468
Antonio de Angelis377a1552018-11-22 17:02:40 +0000469psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100470 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000471 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100472{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000473 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100474 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800475 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100476 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100477 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100478 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000479 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100480
Antonio de Angelis4743e672019-04-11 11:38:48 +0100481 psa_invec in_vec[] = {
482 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
483 };
484 psa_outvec out_vec[] = {
485 {.base = &(operation->handle), .len = sizeof(uint32_t)},
486 };
487
488#ifdef TFM_PSA_API
489 PSA_CONNECT(TFM_CRYPTO);
490#endif
491
492 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
493 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
494#ifdef TFM_PSA_API
495 PSA_CLOSE();
496#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100497
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000498 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100499}
500
Antonio de Angelis377a1552018-11-22 17:02:40 +0000501psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100502 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000503 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100504{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000505 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100506 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800507 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100508 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100509 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100510 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000511 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100512
Antonio de Angelis4743e672019-04-11 11:38:48 +0100513 psa_invec in_vec[] = {
514 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
515 };
516 psa_outvec out_vec[] = {
517 {.base = &(operation->handle), .len = sizeof(uint32_t)},
518 };
519
520#ifdef TFM_PSA_API
521 PSA_CONNECT(TFM_CRYPTO);
522#endif
523
524 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
525 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
526#ifdef TFM_PSA_API
527 PSA_CLOSE();
528#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100529
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000530 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100531}
532
533psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
534 const uint8_t *input,
535 size_t input_length,
536 unsigned char *output,
537 size_t output_size,
538 size_t *output_length)
539{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000540 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100541 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800542 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100543 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100544 };
545
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000546 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100547 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000548 {.base = input, .len = input_length},
549 };
550 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100551 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000552 {.base = output, .len = output_size}
553 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100554
Antonio de Angelis4743e672019-04-11 11:38:48 +0100555#ifdef TFM_PSA_API
556 PSA_CONNECT(TFM_CRYPTO);
557#endif
558
559 status = API_DISPATCH(tfm_crypto_cipher_update,
560 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100561
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000562 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100563
Antonio de Angelis4743e672019-04-11 11:38:48 +0100564#ifdef TFM_PSA_API
565 PSA_CLOSE();
566#endif
567
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000568 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100569}
570
571psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
572{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000573 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100574 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800575 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100576 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000577 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100578
Antonio de Angelis4743e672019-04-11 11:38:48 +0100579 psa_invec in_vec[] = {
580 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
581 };
582 psa_outvec out_vec[] = {
583 {.base = &(operation->handle), .len = sizeof(uint32_t)},
584 };
585
586#ifdef TFM_PSA_API
587 PSA_CONNECT(TFM_CRYPTO);
588#endif
589
590 status = API_DISPATCH(tfm_crypto_cipher_abort,
591 TFM_CRYPTO_CIPHER_ABORT);
592#ifdef TFM_PSA_API
593 PSA_CLOSE();
594#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100595
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000596 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100597}
598
599psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
600 uint8_t *output,
601 size_t output_size,
602 size_t *output_length)
603{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000604 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100605 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800606 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100607 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100608 };
609
610 psa_invec in_vec[] = {
611 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
612 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000613 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100614 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000615 {.base = output, .len = output_size},
616 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100617
Antonio de Angelis4743e672019-04-11 11:38:48 +0100618#ifdef TFM_PSA_API
619 PSA_CONNECT(TFM_CRYPTO);
620#endif
621
622 status = API_DISPATCH(tfm_crypto_cipher_finish,
623 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100624
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000625 *output_length = out_vec[1].len;
626
Antonio de Angelis4743e672019-04-11 11:38:48 +0100627#ifdef TFM_PSA_API
628 PSA_CLOSE();
629#endif
630
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000631 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100632}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100633
Antonio de Angelis377a1552018-11-22 17:02:40 +0000634psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100635 psa_algorithm_t alg)
636{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000637 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100638 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800639 .sfn_id = TFM_CRYPTO_HASH_SETUP_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100640 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100641 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000642 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100643
Antonio de Angelis4743e672019-04-11 11:38:48 +0100644 psa_invec in_vec[] = {
645 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
646 };
647 psa_outvec out_vec[] = {
648 {.base = &(operation->handle), .len = sizeof(uint32_t)},
649 };
650
651#ifdef TFM_PSA_API
652 PSA_CONNECT(TFM_CRYPTO);
653#endif
654
655 status = API_DISPATCH(tfm_crypto_hash_setup,
656 TFM_CRYPTO_HASH_SETUP);
657
658#ifdef TFM_PSA_API
659 PSA_CLOSE();
660#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100661
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000662 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100663}
664
665psa_status_t psa_hash_update(psa_hash_operation_t *operation,
666 const uint8_t *input,
667 size_t input_length)
668{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000669 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100670 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800671 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100672 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100673 };
674
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000675 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100676 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000677 {.base = input, .len = input_length},
678 };
679 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100680 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000681 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100682
Antonio de Angelis4743e672019-04-11 11:38:48 +0100683#ifdef TFM_PSA_API
684 PSA_CONNECT(TFM_CRYPTO);
685#endif
686
687 status = API_DISPATCH(tfm_crypto_hash_update,
688 TFM_CRYPTO_HASH_UPDATE);
689
690#ifdef TFM_PSA_API
691 PSA_CLOSE();
692#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100693
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000694 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100695}
696
697psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
698 uint8_t *hash,
699 size_t hash_size,
700 size_t *hash_length)
701{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000702 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100703 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800704 .sfn_id = TFM_CRYPTO_HASH_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100705 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100706 };
707
708 psa_invec in_vec[] = {
709 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
710 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000711 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100712 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000713 {.base = hash, .len = hash_size},
714 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100715
Antonio de Angelis4743e672019-04-11 11:38:48 +0100716#ifdef TFM_PSA_API
717 PSA_CONNECT(TFM_CRYPTO);
718#endif
719
720 status = API_DISPATCH(tfm_crypto_hash_finish,
721 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100722
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000723 *hash_length = out_vec[1].len;
724
Antonio de Angelis4743e672019-04-11 11:38:48 +0100725#ifdef TFM_PSA_API
726 PSA_CLOSE();
727#endif
728
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000729 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100730}
731
732psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
733 const uint8_t *hash,
734 size_t hash_length)
735{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000736 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100737 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800738 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100739 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100740 };
741
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000742 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100743 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000744 {.base = hash, .len = hash_length},
745 };
746 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100747 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000748 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100749
Antonio de Angelis4743e672019-04-11 11:38:48 +0100750#ifdef TFM_PSA_API
751 PSA_CONNECT(TFM_CRYPTO);
752#endif
753
754 status = API_DISPATCH(tfm_crypto_hash_verify,
755 TFM_CRYPTO_HASH_VERIFY);
756#ifdef TFM_PSA_API
757 PSA_CLOSE();
758#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100759
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000760 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100761}
762
763psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
764{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000765 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100766 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800767 .sfn_id = TFM_CRYPTO_HASH_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100768 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000769 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100770
Antonio de Angelis4743e672019-04-11 11:38:48 +0100771 psa_invec in_vec[] = {
772 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
773 };
774 psa_outvec out_vec[] = {
775 {.base = &(operation->handle), .len = sizeof(uint32_t)},
776 };
777
778#ifdef TFM_PSA_API
779 PSA_CONNECT(TFM_CRYPTO);
780#endif
781
782 status = API_DISPATCH(tfm_crypto_hash_abort,
783 TFM_CRYPTO_HASH_ABORT);
784#ifdef TFM_PSA_API
785 PSA_CLOSE();
786#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100787
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000788 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100789}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100790
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100791psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
792 psa_hash_operation_t *target_operation)
793{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100794 psa_status_t status;
795 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800796 .sfn_id = TFM_CRYPTO_HASH_CLONE_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100797 .op_handle = source_operation->handle,
798 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100799
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100800 psa_invec in_vec[] = {
801 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
802 };
803 psa_outvec out_vec[] = {
804 {.base = target_operation, .len = sizeof(psa_hash_operation_t)},
805 };
806
807#ifdef TFM_PSA_API
808 PSA_CONNECT(TFM_CRYPTO);
809#endif
810
811 status = API_DISPATCH(tfm_crypto_hash_clone,
812 TFM_CRYPTO_HASH_CLONE);
813#ifdef TFM_PSA_API
814 PSA_CLOSE();
815#endif
816
817 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100818}
819
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100820psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100821 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100822 psa_algorithm_t alg)
823{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000824 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100825 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800826 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100827 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100828 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100829 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000830 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100831
Antonio de Angelis4743e672019-04-11 11:38:48 +0100832 psa_invec in_vec[] = {
833 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
834 };
835 psa_outvec out_vec[] = {
836 {.base = &(operation->handle), .len = sizeof(uint32_t)},
837 };
838
839#ifdef TFM_PSA_API
840 PSA_CONNECT(TFM_CRYPTO);
841#endif
842
843 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
844 TFM_CRYPTO_MAC_SIGN_SETUP);
845#ifdef TFM_PSA_API
846 PSA_CLOSE();
847#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100848
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000849 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100850}
851
852psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100853 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100854 psa_algorithm_t alg)
855{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000856 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100857 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800858 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100859 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100860 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100861 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000862 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100863
Antonio de Angelis4743e672019-04-11 11:38:48 +0100864 psa_invec in_vec[] = {
865 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
866 };
867 psa_outvec out_vec[] = {
868 {.base = &(operation->handle), .len = sizeof(uint32_t)},
869 };
870
871#ifdef TFM_PSA_API
872 PSA_CONNECT(TFM_CRYPTO);
873#endif
874
875 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
876 TFM_CRYPTO_MAC_VERIFY_SETUP);
877#ifdef TFM_PSA_API
878 PSA_CLOSE();
879#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100880
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000881 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100882}
883
884psa_status_t psa_mac_update(psa_mac_operation_t *operation,
885 const uint8_t *input,
886 size_t input_length)
887{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000888 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100889 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800890 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100891 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100892 };
893
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000894 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100895 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000896 {.base = input, .len = input_length},
897 };
898 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100899 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000900 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100901
Antonio de Angelis4743e672019-04-11 11:38:48 +0100902#ifdef TFM_PSA_API
903 PSA_CONNECT(TFM_CRYPTO);
904#endif
905
906 status = API_DISPATCH(tfm_crypto_mac_update,
907 TFM_CRYPTO_MAC_UPDATE);
908#ifdef TFM_PSA_API
909 PSA_CLOSE();
910#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100911
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000912 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100913}
914
915psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
916 uint8_t *mac,
917 size_t mac_size,
918 size_t *mac_length)
919{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000920 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100921 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800922 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100923 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100924 };
925
926 psa_invec in_vec[] = {
927 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
928 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000929 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100930 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000931 {.base = mac, .len = mac_size},
932 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100933
Antonio de Angelis4743e672019-04-11 11:38:48 +0100934#ifdef TFM_PSA_API
935 PSA_CONNECT(TFM_CRYPTO);
936#endif
937
938 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
939 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100940
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000941 *mac_length = out_vec[1].len;
942
Antonio de Angelis4743e672019-04-11 11:38:48 +0100943#ifdef TFM_PSA_API
944 PSA_CLOSE();
945#endif
946
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000947 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100948}
949
950psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
951 const uint8_t *mac,
952 size_t mac_length)
953{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000954 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100955 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800956 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100957 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100958 };
959
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000960 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100961 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000962 {.base = mac, .len = mac_length},
963 };
964 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100965 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000966 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100967
Antonio de Angelis4743e672019-04-11 11:38:48 +0100968#ifdef TFM_PSA_API
969 PSA_CONNECT(TFM_CRYPTO);
970#endif
971
972 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
973 TFM_CRYPTO_MAC_VERIFY_FINISH);
974
975#ifdef TFM_PSA_API
976 PSA_CLOSE();
977#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100978
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000979 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100980}
981
982psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
983{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000984 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100985 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800986 .sfn_id = TFM_CRYPTO_MAC_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100987 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000988 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100989
Antonio de Angelis4743e672019-04-11 11:38:48 +0100990 psa_invec in_vec[] = {
991 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
992 };
993 psa_outvec out_vec[] = {
994 {.base = &(operation->handle), .len = sizeof(uint32_t)},
995 };
996
997#ifdef TFM_PSA_API
998 PSA_CONNECT(TFM_CRYPTO);
999#endif
1000
1001 status = API_DISPATCH(tfm_crypto_mac_abort,
1002 TFM_CRYPTO_MAC_ABORT);
1003#ifdef TFM_PSA_API
1004 PSA_CLOSE();
1005#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001006
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001007 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001008}
Antonio de Angelis3a480992018-11-07 11:53:28 +00001009
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001010psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001011 psa_algorithm_t alg,
1012 const uint8_t *nonce,
1013 size_t nonce_length,
1014 const uint8_t *additional_data,
1015 size_t additional_data_length,
1016 const uint8_t *plaintext,
1017 size_t plaintext_length,
1018 uint8_t *ciphertext,
1019 size_t ciphertext_size,
1020 size_t *ciphertext_length)
1021{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001022 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001023 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001024 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001025 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001026 .alg = alg,
1027 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001028 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001029
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001030 /* Sanitize the optional input */
1031 if ((additional_data == NULL) && (additional_data_length != 0)) {
1032 return PSA_ERROR_INVALID_ARGUMENT;
1033 }
1034
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001035 size_t idx = 0;
1036 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001037 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001038 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001039 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001040 };
1041 psa_outvec out_vec[] = {
1042 {.base = ciphertext, .len = ciphertext_size},
1043 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001044
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001045 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1046 return PSA_ERROR_INVALID_ARGUMENT;
1047 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001048
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001049 if (nonce != NULL) {
1050 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001051 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001052 }
1053 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001054
Antonio de Angelis4743e672019-04-11 11:38:48 +01001055#ifdef TFM_PSA_API
1056 PSA_CONNECT(TFM_CRYPTO);
1057#endif
1058
1059#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001060 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001061 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001062 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001063 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001064 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001065 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001066#else
1067 status = API_DISPATCH(tfm_crypto_aead_encrypt,
1068 TFM_CRYPTO_AEAD_ENCRYPT);
1069#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001070
1071 *ciphertext_length = out_vec[0].len;
1072
Antonio de Angelis4743e672019-04-11 11:38:48 +01001073#ifdef TFM_PSA_API
1074 PSA_CLOSE();
1075#endif
1076
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001077 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001078}
1079
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001080psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001081 psa_algorithm_t alg,
1082 const uint8_t *nonce,
1083 size_t nonce_length,
1084 const uint8_t *additional_data,
1085 size_t additional_data_length,
1086 const uint8_t *ciphertext,
1087 size_t ciphertext_length,
1088 uint8_t *plaintext,
1089 size_t plaintext_size,
1090 size_t *plaintext_length)
1091{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001092 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001093 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001094 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001095 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001096 .alg = alg,
1097 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001098 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001099
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001100 /* Sanitize the optional input */
1101 if ((additional_data == NULL) && (additional_data_length != 0)) {
1102 return PSA_ERROR_INVALID_ARGUMENT;
1103 }
1104
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001105 size_t idx = 0;
1106 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001107 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001108 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001109 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001110 };
1111 psa_outvec out_vec[] = {
1112 {.base = plaintext, .len = plaintext_size},
1113 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001114
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001115 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1116 return PSA_ERROR_INVALID_ARGUMENT;
1117 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001118
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001119 if (nonce != NULL) {
1120 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001121 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001122 }
1123 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001124
Antonio de Angelis4743e672019-04-11 11:38:48 +01001125#ifdef TFM_PSA_API
1126 PSA_CONNECT(TFM_CRYPTO);
1127#endif
1128
1129#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001130 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001131 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001132 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001133 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001134 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001135 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001136#else
1137 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1138 TFM_CRYPTO_AEAD_DECRYPT);
1139#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001140
1141 *plaintext_length = out_vec[0].len;
1142
Antonio de Angelis4743e672019-04-11 11:38:48 +01001143#ifdef TFM_PSA_API
1144 PSA_CLOSE();
1145#endif
1146
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001147 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001148}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001149
1150psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
1151 psa_algorithm_t alg,
1152 const uint8_t *hash,
1153 size_t hash_length,
1154 uint8_t *signature,
1155 size_t signature_size,
1156 size_t *signature_length)
1157{
1158 psa_status_t status;
1159 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001160 .sfn_id = TFM_CRYPTO_ASYMMETRIC_SIGN_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001161 .key_handle = handle,
1162 .alg = alg,
1163 };
1164
1165 psa_invec in_vec[] = {
1166 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1167 {.base = hash, .len = hash_length},
1168 };
1169 psa_outvec out_vec[] = {
1170 {.base = signature, .len = signature_size},
1171 };
1172
1173#ifdef TFM_PSA_API
1174 PSA_CONNECT(TFM_CRYPTO);
1175#endif
1176
1177 status = API_DISPATCH(tfm_crypto_asymmetric_sign,
1178 TFM_CRYPTO_ASYMMETRIC_SIGN);
1179
1180 *signature_length = out_vec[0].len;
1181
1182#ifdef TFM_PSA_API
1183 PSA_CLOSE();
1184#endif
1185
1186 return status;
1187}
1188
1189psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
1190 psa_algorithm_t alg,
1191 const uint8_t *hash,
1192 size_t hash_length,
1193 const uint8_t *signature,
1194 size_t signature_length)
1195{
1196 psa_status_t status;
1197 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001198 .sfn_id = TFM_CRYPTO_ASYMMETRIC_VERIFY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001199 .key_handle = handle,
1200 .alg = alg
1201 };
1202
1203 psa_invec in_vec[] = {
1204 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1205 {.base = hash, .len = hash_length},
1206 {.base = signature, .len = signature_length}
1207 };
1208
1209#ifdef TFM_PSA_API
1210 PSA_CONNECT(TFM_CRYPTO);
1211#endif
1212
1213 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_asymmetric_verify,
1214 TFM_CRYPTO_ASYMMETRIC_VERIFY);
1215#ifdef TFM_PSA_API
1216 PSA_CLOSE();
1217#endif
1218
1219 return status;
1220}
1221
1222psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
1223 psa_algorithm_t alg,
1224 const uint8_t *input,
1225 size_t input_length,
1226 const uint8_t *salt,
1227 size_t salt_length,
1228 uint8_t *output,
1229 size_t output_size,
1230 size_t *output_length)
1231{
1232 psa_status_t status;
1233 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001234 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001235 .key_handle = handle,
1236 .alg = alg
1237 };
1238
1239 /* Sanitize the optional input */
1240 if ((salt == NULL) && (salt_length != 0)) {
1241 return PSA_ERROR_INVALID_ARGUMENT;
1242 }
1243
1244 psa_invec in_vec[] = {
1245 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1246 {.base = input, .len = input_length},
1247 {.base = salt, .len = salt_length}
1248 };
1249
1250 psa_outvec out_vec[] = {
1251 {.base = output, .len = output_size},
1252 };
1253
1254#ifdef TFM_PSA_API
1255 PSA_CONNECT(TFM_CRYPTO);
1256#endif
1257
1258#ifdef TFM_PSA_API
1259 size_t in_len = ARRAY_SIZE(in_vec);
1260 if (salt == NULL) {
1261 in_len--;
1262 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001263 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001264 out_vec, ARRAY_SIZE(out_vec));
1265#else
1266 status = API_DISPATCH(tfm_crypto_asymmetric_encrypt,
1267 TFM_CRYPTO_ASYMMETRIC_ENCRYPT);
1268#endif
1269
1270 *output_length = out_vec[0].len;
1271
1272#ifdef TFM_PSA_API
1273 PSA_CLOSE();
1274#endif
1275
1276 return status;
1277}
1278
1279psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
1280 psa_algorithm_t alg,
1281 const uint8_t *input,
1282 size_t input_length,
1283 const uint8_t *salt,
1284 size_t salt_length,
1285 uint8_t *output,
1286 size_t output_size,
1287 size_t *output_length)
1288{
1289 psa_status_t status;
1290 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001291 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001292 .key_handle = handle,
1293 .alg = alg
1294 };
1295
1296 /* Sanitize the optional input */
1297 if ((salt == NULL) && (salt_length != 0)) {
1298 return PSA_ERROR_INVALID_ARGUMENT;
1299 }
1300
1301 psa_invec in_vec[] = {
1302 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1303 {.base = input, .len = input_length},
1304 {.base = salt, .len = salt_length}
1305 };
1306
1307 psa_outvec out_vec[] = {
1308 {.base = output, .len = output_size},
1309 };
1310
1311#ifdef TFM_PSA_API
1312 PSA_CONNECT(TFM_CRYPTO);
1313#endif
1314
1315#ifdef TFM_PSA_API
1316 size_t in_len = ARRAY_SIZE(in_vec);
1317 if (salt == NULL) {
1318 in_len--;
1319 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001320 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001321 out_vec, ARRAY_SIZE(out_vec));
1322#else
1323 status = API_DISPATCH(tfm_crypto_asymmetric_decrypt,
1324 TFM_CRYPTO_ASYMMETRIC_DECRYPT);
1325#endif
1326
1327 *output_length = out_vec[0].len;
1328
1329#ifdef TFM_PSA_API
1330 PSA_CLOSE();
1331#endif
1332
1333 return status;
1334}
1335
1336psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1337 size_t *capacity)
1338{
1339 psa_status_t status;
1340 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001341 .sfn_id = TFM_CRYPTO_GET_GENERATOR_CAPACITY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001342 .op_handle = generator->handle,
1343 };
1344
1345 psa_invec in_vec[] = {
1346 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1347 };
1348
1349 psa_outvec out_vec[] = {
1350 {.base = capacity, .len = sizeof(size_t)},
1351 };
1352
1353#ifdef TFM_PSA_API
1354 PSA_CONNECT(TFM_CRYPTO);
1355#endif
1356
1357 status = API_DISPATCH(tfm_crypto_get_generator_capacity,
1358 TFM_CRYPTO_GET_GENERATOR_CAPACITY);
1359#ifdef TFM_PSA_API
1360 PSA_CLOSE();
1361#endif
1362
1363 return status;
1364}
1365
1366psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1367 uint8_t *output,
1368 size_t output_length)
1369{
1370 psa_status_t status;
1371 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001372 .sfn_id = TFM_CRYPTO_GENERATOR_READ_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001373 .op_handle = generator->handle,
1374 };
1375
1376 psa_invec in_vec[] = {
1377 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1378 };
1379
1380 psa_outvec out_vec[] = {
1381 {.base = output, .len = output_length},
1382 };
1383
1384#ifdef TFM_PSA_API
1385 PSA_CONNECT(TFM_CRYPTO);
1386#endif
1387
1388 status = API_DISPATCH(tfm_crypto_generator_read,
1389 TFM_CRYPTO_GENERATOR_READ);
1390#ifdef TFM_PSA_API
1391 PSA_CLOSE();
1392#endif
1393
1394 return status;
1395}
1396
1397psa_status_t psa_generator_import_key(psa_key_handle_t handle,
1398 psa_key_type_t type,
1399 size_t bits,
1400 psa_crypto_generator_t *generator)
1401{
1402 psa_status_t status;
1403 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001404 .sfn_id = TFM_CRYPTO_GENERATOR_IMPORT_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001405 .key_handle = handle,
1406 .type = type,
1407 .op_handle = generator->handle,
1408 };
1409
1410 psa_invec in_vec[] = {
1411 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1412 {.base = &bits, .len = sizeof(size_t)},
1413 };
1414
1415#ifdef TFM_PSA_API
1416 PSA_CONNECT(TFM_CRYPTO);
1417#endif
1418
1419 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generator_import_key,
1420 TFM_CRYPTO_GENERATOR_IMPORT_KEY);
1421#ifdef TFM_PSA_API
1422 PSA_CLOSE();
1423#endif
1424
1425 return status;
1426}
1427
1428psa_status_t psa_generator_abort(psa_crypto_generator_t *generator)
1429{
1430 psa_status_t status;
1431 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001432 .sfn_id = TFM_CRYPTO_GENERATOR_ABORT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001433 .op_handle = generator->handle,
1434 };
1435
1436 psa_invec in_vec[] = {
1437 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1438 };
1439
1440 psa_outvec out_vec[] = {
1441 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1442 };
1443
1444#ifdef TFM_PSA_API
1445 PSA_CONNECT(TFM_CRYPTO);
1446#endif
1447
1448 status = API_DISPATCH(tfm_crypto_generator_abort,
1449 TFM_CRYPTO_GENERATOR_ABORT);
1450#ifdef TFM_PSA_API
1451 PSA_CLOSE();
1452#endif
1453
1454 return status;
1455}
1456
1457psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
1458 psa_key_handle_t handle,
1459 psa_algorithm_t alg,
1460 const uint8_t *salt,
1461 size_t salt_length,
1462 const uint8_t *label,
1463 size_t label_length,
1464 size_t capacity)
1465{
1466 psa_status_t status;
1467 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001468 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001469 .key_handle = handle,
1470 .alg = alg,
1471 .op_handle = generator->handle,
1472 .capacity = capacity,
1473 };
1474
1475 /* Sanitize the optional input */
1476 if ((salt == NULL) && (salt_length != 0)) {
1477 return PSA_ERROR_INVALID_ARGUMENT;
1478 }
1479
1480 if ((label == NULL) && (label_length != 0)) {
1481 return PSA_ERROR_INVALID_ARGUMENT;
1482 }
1483
1484 psa_invec in_vec[] = {
1485 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1486 {.base = salt, .len = salt_length},
1487 {.base = label, .len = label_length},
1488 };
1489
1490 psa_outvec out_vec[] = {
1491 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1492 };
1493
1494#ifdef TFM_PSA_API
1495 PSA_CONNECT(TFM_CRYPTO);
1496#endif
1497
1498#ifdef TFM_PSA_API
1499 size_t in_len = ARRAY_SIZE(in_vec);
1500 if (label == NULL) {
1501 in_len--;
1502 if (salt == NULL) {
1503 in_len--;
1504 }
1505 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001506 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001507 out_vec, ARRAY_SIZE(out_vec));
1508#else
1509 status = API_DISPATCH(tfm_crypto_key_derivation,
1510 TFM_CRYPTO_KEY_DERIVATION);
1511#endif
1512
1513#ifdef TFM_PSA_API
1514 PSA_CLOSE();
1515#endif
1516
1517 return status;
1518}
1519
1520psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
1521 psa_key_handle_t private_key,
1522 const uint8_t *peer_key,
1523 size_t peer_key_length,
1524 psa_algorithm_t alg)
1525{
1526 psa_status_t status;
1527 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001528 .sfn_id = TFM_CRYPTO_KEY_AGREEMENT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001529 .key_handle = private_key,
1530 .alg = alg,
1531 .op_handle = generator->handle,
1532 };
1533
1534 psa_invec in_vec[] = {
1535 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1536 {.base = peer_key, .len = peer_key_length},
1537 };
1538
1539 psa_outvec out_vec[] = {
1540 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1541 };
1542
1543#ifdef TFM_PSA_API
1544 PSA_CONNECT(TFM_CRYPTO);
1545#endif
1546
1547 status = API_DISPATCH(tfm_crypto_key_agreement,
1548 TFM_CRYPTO_KEY_AGREEMENT);
1549
1550#ifdef TFM_PSA_API
1551 PSA_CLOSE();
1552#endif
1553
1554 return status;
1555}
1556
1557psa_status_t psa_generate_random(uint8_t *output,
1558 size_t output_size)
1559{
1560 psa_status_t status;
1561 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001562 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001563 };
1564
1565 psa_invec in_vec[] = {
1566 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1567 };
1568
1569 psa_outvec out_vec[] = {
1570 {.base = output, .len = output_size},
1571 };
1572
1573 if (output_size == 0) {
1574 return PSA_SUCCESS;
1575 }
1576
1577#ifdef TFM_PSA_API
1578 PSA_CONNECT(TFM_CRYPTO);
1579#endif
1580
1581 status = API_DISPATCH(tfm_crypto_generate_random,
1582 TFM_CRYPTO_GENERATE_RANDOM);
1583
1584#ifdef TFM_PSA_API
1585 PSA_CLOSE();
1586#endif
1587
1588 return status;
1589}
1590
1591psa_status_t psa_generate_key(psa_key_handle_t handle,
1592 psa_key_type_t type,
1593 size_t bits,
1594 const void *extra,
1595 size_t extra_size)
1596{
1597 psa_status_t status;
1598 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001599 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001600 .key_handle = handle,
1601 .type = type,
1602 };
1603
1604 /* Sanitize the optional input */
1605 if ((extra == NULL) && (extra_size != 0)) {
1606 return PSA_ERROR_INVALID_ARGUMENT;
1607 }
1608
1609 psa_invec in_vec[] = {
1610 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1611 {.base = &bits, .len = sizeof(size_t)},
1612 {.base = extra, .len = extra_size},
1613 };
1614
1615#ifdef TFM_PSA_API
1616 PSA_CONNECT(TFM_CRYPTO);
1617#endif
1618
1619#ifdef TFM_PSA_API
1620 size_t in_len = ARRAY_SIZE(in_vec);
1621 if (extra == NULL) {
1622 in_len--;
1623 }
1624
Summer Qin4b1d03b2019-07-02 14:56:08 +08001625 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len, NULL, 0);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001626#else
1627 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generate_key,
1628 TFM_CRYPTO_GENERATE_KEY);
1629#endif
1630
1631#ifdef TFM_PSA_API
1632 PSA_CLOSE();
1633#endif
1634
1635 return status;
1636}