blob: 19bc52ccd070be075ee5f19c9c076e6cefbacf0f [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 Angelis8908f472018-08-31 15:44:25 +010011#include "tfm_ns_lock.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000012
Antonio de Angelis4743e672019-04-11 11:38:48 +010013#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000014
Antonio de Angelis4743e672019-04-11 11:38:48 +010015#ifdef TFM_PSA_API
Jamie Foxcc31d402019-01-28 17:13:52 +000016#include "psa/client.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000017
Antonio de Angelis4743e672019-04-11 11:38:48 +010018/* Macro to check for a valid PSA handle */
19/* FixMe: Here temporarily until it's added to the framework headers */
20#define PSA_IS_HANDLE_VALID(handle) ((handle) > (psa_handle_t)0)
21
Jamie Fox0e54ebc2019-04-09 14:21:04 +010022#define PSA_CONNECT(service) \
23 psa_handle_t ipc_handle; \
24 ipc_handle = psa_connect(service##_SID, service##_MIN_VER); \
25 if (!PSA_IS_HANDLE_VALID(ipc_handle)) { \
26 return PSA_ERROR_GENERIC_ERROR; \
27 } \
Antonio de Angelis4743e672019-04-11 11:38:48 +010028
Jamie Fox0e54ebc2019-04-09 14:21:04 +010029#define PSA_CLOSE() psa_close(ipc_handle)
Antonio de Angelis4743e672019-04-11 11:38:48 +010030
Jamie Fox0e54ebc2019-04-09 14:21:04 +010031#define API_DISPATCH(sfn_name, sfn_id) \
32 psa_call(ipc_handle, /*PSA_IPC_CALL,*/ \
33 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010034 out_vec, ARRAY_SIZE(out_vec))
35
Jamie Fox0e54ebc2019-04-09 14:21:04 +010036#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
37 psa_call(ipc_handle, /*PSA_IPC_CALL,*/ \
38 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010039 (psa_outvec *)NULL, 0)
40#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +010041#define API_DISPATCH(sfn_name, sfn_id) \
42 tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
43 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010044 (uint32_t)out_vec, ARRAY_SIZE(out_vec))
45
Jamie Fox0e54ebc2019-04-09 14:21:04 +010046#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
47 tfm_ns_lock_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
48 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000049 (uint32_t)NULL, 0)
Antonio de Angelis4743e672019-04-11 11:38:48 +010050#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010051
52psa_status_t psa_crypto_init(void)
53{
54 /* Service init is performed during TFM boot up,
55 * so application level initialisation is empty
56 */
57 return PSA_SUCCESS;
58}
59
Jamie Fox0e54ebc2019-04-09 14:21:04 +010060psa_status_t psa_allocate_key(psa_key_handle_t *handle)
61{
62 psa_status_t status;
63 const struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080064 .sfn_id = TFM_CRYPTO_ALLOCATE_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010065 };
66 psa_invec in_vec[] = {
67 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
68 };
69 psa_outvec out_vec[] = {
70 {.base = handle, .len = sizeof(psa_key_handle_t)},
71 };
72
73#ifdef TFM_PSA_API
74 PSA_CONNECT(TFM_CRYPTO);
75#endif
76
77 status = API_DISPATCH(tfm_crypto_allocate_key,
78 TFM_CRYPTO_ALLOCATE_KEY);
79#ifdef TFM_PSA_API
80 PSA_CLOSE();
81#endif
82
83 return status;
84}
85
86psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
87 psa_key_id_t id,
88 psa_key_handle_t *handle)
89{
90 (void)lifetime;
91 (void)id;
92 (void)handle;
93
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010094 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010095 return PSA_ERROR_NOT_SUPPORTED;
96}
97
98psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
99 psa_key_id_t id,
100 psa_key_handle_t *handle)
101{
102 (void)lifetime;
103 (void)id;
104 (void)handle;
105
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100106 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100107 return PSA_ERROR_NOT_SUPPORTED;
108}
109
110psa_status_t psa_close_key(psa_key_handle_t handle)
111{
112 (void)handle;
113
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100114 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100115 return PSA_ERROR_NOT_SUPPORTED;
116}
117
118psa_status_t psa_import_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100119 psa_key_type_t type,
120 const uint8_t *data,
121 size_t data_length)
122{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000123 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100124 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800125 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100126 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100127 .type = type,
128 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000129 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100130 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000131 {.base = data, .len = data_length}
132 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100133
Antonio de Angelis4743e672019-04-11 11:38:48 +0100134#ifdef TFM_PSA_API
135 PSA_CONNECT(TFM_CRYPTO);
136#endif
137
138 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_import_key,
139 TFM_CRYPTO_IMPORT_KEY);
140#ifdef TFM_PSA_API
141 PSA_CLOSE();
142#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100143
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000144 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100145}
146
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100147psa_status_t psa_destroy_key(psa_key_handle_t handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100148{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000149 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100150 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800151 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100153 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000154 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100155 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000156 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100157
Antonio de Angelis4743e672019-04-11 11:38:48 +0100158#ifdef TFM_PSA_API
159 PSA_CONNECT(TFM_CRYPTO);
160#endif
161
162 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
163 TFM_CRYPTO_DESTROY_KEY);
164#ifdef TFM_PSA_API
165 PSA_CLOSE();
166#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100167
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000168 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100169}
170
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100171psa_status_t psa_get_key_information(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100172 psa_key_type_t *type,
173 size_t *bits)
174{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000175 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100176 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800177 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100178 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100179 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000180 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100181 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000182 };
183 psa_outvec out_vec[] = {
184 {.base = type, .len = sizeof(psa_key_type_t)},
185 {.base = bits, .len = sizeof(size_t)}
186 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100187
Antonio de Angelis4743e672019-04-11 11:38:48 +0100188#ifdef TFM_PSA_API
189 PSA_CONNECT(TFM_CRYPTO);
190#endif
191
192 status = API_DISPATCH(tfm_crypto_get_key_information,
193 TFM_CRYPTO_GET_KEY_INFORMATION);
194#ifdef TFM_PSA_API
195 PSA_CLOSE();
196#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100197
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000198 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100199}
200
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100201psa_status_t psa_export_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100202 uint8_t *data,
203 size_t data_size,
204 size_t *data_length)
205{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000206 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100207 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800208 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100209 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100210 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000211 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100212 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000213 };
214 psa_outvec out_vec[] = {
215 {.base = data, .len = data_size}
216 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100217
Antonio de Angelis4743e672019-04-11 11:38:48 +0100218#ifdef TFM_PSA_API
219 PSA_CONNECT(TFM_CRYPTO);
220#endif
221
222 status = API_DISPATCH(tfm_crypto_export_key,
223 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100224
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000225 *data_length = out_vec[0].len;
226
Antonio de Angelis4743e672019-04-11 11:38:48 +0100227#ifdef TFM_PSA_API
228 PSA_CLOSE();
229#endif
230
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000231 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100232}
233
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100234psa_status_t psa_export_public_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100235 uint8_t *data,
236 size_t data_size,
237 size_t *data_length)
238{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100239 psa_status_t status;
240 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800241 .sfn_id = TFM_CRYPTO_EXPORT_PUBLIC_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100242 .key_handle = handle,
243 };
Hugues de Valon8b442442019-02-19 14:30:52 +0000244
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100245 psa_invec in_vec[] = {
246 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
247 };
248 psa_outvec out_vec[] = {
249 {.base = data, .len = data_size}
250 };
251
252#ifdef TFM_PSA_API
253 PSA_CONNECT(TFM_CRYPTO);
254#endif
255
256 status = API_DISPATCH(tfm_crypto_export_public_key,
257 TFM_CRYPTO_EXPORT_PUBLIC_KEY);
258
259 *data_length = out_vec[0].len;
260
261#ifdef TFM_PSA_API
262 PSA_CLOSE();
263#endif
264
265 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100266}
267
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100268psa_status_t psa_copy_key(psa_key_handle_t source_handle,
269 psa_key_handle_t target_handle,
270 const psa_key_policy_t *constraint)
Jamie Foxefd82732018-11-26 10:34:32 +0000271{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100272 psa_status_t status;
273 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800274 .sfn_id = TFM_CRYPTO_COPY_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100275 .key_handle = source_handle,
276 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000277
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100278 psa_invec in_vec[] = {
279 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
280 {.base = &target_handle, .len = sizeof(psa_key_handle_t)},
281 {.base = constraint, .len = sizeof(psa_key_policy_t)},
282 };
283
284#ifdef TFM_PSA_API
285 PSA_CONNECT(TFM_CRYPTO);
286#endif
287
288 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_copy_key,
289 TFM_CRYPTO_COPY_KEY);
290#ifdef TFM_PSA_API
291 PSA_CLOSE();
292#endif
293
294 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000295}
296
297void psa_key_policy_set_usage(psa_key_policy_t *policy,
298 psa_key_usage_t usage,
299 psa_algorithm_t alg)
300{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100301 policy->usage = usage;
302 policy->alg = alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000303}
304
305psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
306{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100307 return policy->usage;
Jamie Foxefd82732018-11-26 10:34:32 +0000308}
309
310psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
311{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100312 return policy->alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000313}
314
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100315psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000316 const psa_key_policy_t *policy)
317{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000318 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100319 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800320 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100321 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100322 };
323
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000324 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100325 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000326 {.base = policy, .len = sizeof(psa_key_policy_t)},
327 };
Jamie Foxefd82732018-11-26 10:34:32 +0000328
Antonio de Angelis4743e672019-04-11 11:38:48 +0100329#ifdef TFM_PSA_API
330 PSA_CONNECT(TFM_CRYPTO);
331#endif
332
333 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
334 TFM_CRYPTO_SET_KEY_POLICY);
335#ifdef TFM_PSA_API
336 PSA_CLOSE();
337#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000338
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000339 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000340}
341
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100342psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000343 psa_key_policy_t *policy)
344{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000345 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100346 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800347 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100348 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100349 };
350
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000351 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100352 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000353 };
354 psa_outvec out_vec[] = {
355 {.base = policy, .len = sizeof(psa_key_policy_t)},
356 };
Jamie Foxefd82732018-11-26 10:34:32 +0000357
Antonio de Angelis4743e672019-04-11 11:38:48 +0100358#ifdef TFM_PSA_API
359 PSA_CONNECT(TFM_CRYPTO);
360#endif
361
362 status = API_DISPATCH(tfm_crypto_get_key_policy,
363 TFM_CRYPTO_GET_KEY_POLICY);
364#ifdef TFM_PSA_API
365 PSA_CLOSE();
366#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000367
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000368 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000369}
370
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100371psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000372 psa_key_lifetime_t *lifetime)
373{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000374 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100375 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800376 .sfn_id = TFM_CRYPTO_GET_KEY_LIFETIME_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100377 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100378 };
379
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000380 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100381 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000382 };
383 psa_outvec out_vec[] = {
384 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
385 };
Jamie Foxefd82732018-11-26 10:34:32 +0000386
Antonio de Angelis4743e672019-04-11 11:38:48 +0100387#ifdef TFM_PSA_API
388 PSA_CONNECT(TFM_CRYPTO);
389#endif
390
391 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
392 TFM_CRYPTO_GET_KEY_LIFETIME);
393#ifdef TFM_PSA_API
394 PSA_CLOSE();
395#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000396
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000397 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000398}
399
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100400psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
401 unsigned char *iv,
402 size_t iv_size,
403 size_t *iv_length)
404{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100405 psa_status_t status;
406 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800407 .sfn_id = TFM_CRYPTO_CIPHER_GENERATE_IV_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100408 .op_handle = operation->handle,
409 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100410
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100411 psa_invec in_vec[] = {
412 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
413 };
414 psa_outvec out_vec[] = {
415 {.base = &(operation->handle), .len = sizeof(uint32_t)},
416 {.base = iv, .len = iv_size},
417 };
418
419#ifdef TFM_PSA_API
420 PSA_CONNECT(TFM_CRYPTO);
421#endif
422
423 status = API_DISPATCH(tfm_crypto_cipher_generate_iv,
424 TFM_CRYPTO_CIPHER_GENERATE_IV);
425
426 *iv_length = out_vec[1].len;
427
428#ifdef TFM_PSA_API
429 PSA_CLOSE();
430#endif
431
432 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100433}
434
Antonio de Angelis377a1552018-11-22 17:02:40 +0000435psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
436 const unsigned char *iv,
437 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100438{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000439 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100440 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800441 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100442 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100443 };
444
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000445 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100446 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000447 {.base = iv, .len = iv_length},
448 };
449 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100450 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000451 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100452
Antonio de Angelis4743e672019-04-11 11:38:48 +0100453#ifdef TFM_PSA_API
454 PSA_CONNECT(TFM_CRYPTO);
455#endif
456
457 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
458 TFM_CRYPTO_CIPHER_SET_IV);
459#ifdef TFM_PSA_API
460 PSA_CLOSE();
461#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100462
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000463 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100464}
465
Antonio de Angelis377a1552018-11-22 17:02:40 +0000466psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100467 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000468 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100469{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000470 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100471 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800472 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100473 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100474 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100475 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000476 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100477
Antonio de Angelis4743e672019-04-11 11:38:48 +0100478 psa_invec in_vec[] = {
479 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
480 };
481 psa_outvec out_vec[] = {
482 {.base = &(operation->handle), .len = sizeof(uint32_t)},
483 };
484
485#ifdef TFM_PSA_API
486 PSA_CONNECT(TFM_CRYPTO);
487#endif
488
489 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
490 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
491#ifdef TFM_PSA_API
492 PSA_CLOSE();
493#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100494
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000495 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100496}
497
Antonio de Angelis377a1552018-11-22 17:02:40 +0000498psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100499 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000500 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100501{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000502 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100503 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800504 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100505 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100506 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100507 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000508 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100509
Antonio de Angelis4743e672019-04-11 11:38:48 +0100510 psa_invec in_vec[] = {
511 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
512 };
513 psa_outvec out_vec[] = {
514 {.base = &(operation->handle), .len = sizeof(uint32_t)},
515 };
516
517#ifdef TFM_PSA_API
518 PSA_CONNECT(TFM_CRYPTO);
519#endif
520
521 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
522 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
523#ifdef TFM_PSA_API
524 PSA_CLOSE();
525#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100526
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000527 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100528}
529
530psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
531 const uint8_t *input,
532 size_t input_length,
533 unsigned char *output,
534 size_t output_size,
535 size_t *output_length)
536{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000537 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100538 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800539 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100540 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100541 };
542
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000543 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100544 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000545 {.base = input, .len = input_length},
546 };
547 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100548 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000549 {.base = output, .len = output_size}
550 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100551
Antonio de Angelis4743e672019-04-11 11:38:48 +0100552#ifdef TFM_PSA_API
553 PSA_CONNECT(TFM_CRYPTO);
554#endif
555
556 status = API_DISPATCH(tfm_crypto_cipher_update,
557 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100558
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000559 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100560
Antonio de Angelis4743e672019-04-11 11:38:48 +0100561#ifdef TFM_PSA_API
562 PSA_CLOSE();
563#endif
564
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000565 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100566}
567
568psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
569{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000570 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100571 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800572 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100573 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000574 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100575
Antonio de Angelis4743e672019-04-11 11:38:48 +0100576 psa_invec in_vec[] = {
577 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
578 };
579 psa_outvec out_vec[] = {
580 {.base = &(operation->handle), .len = sizeof(uint32_t)},
581 };
582
583#ifdef TFM_PSA_API
584 PSA_CONNECT(TFM_CRYPTO);
585#endif
586
587 status = API_DISPATCH(tfm_crypto_cipher_abort,
588 TFM_CRYPTO_CIPHER_ABORT);
589#ifdef TFM_PSA_API
590 PSA_CLOSE();
591#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100592
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000593 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100594}
595
596psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
597 uint8_t *output,
598 size_t output_size,
599 size_t *output_length)
600{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000601 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100602 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800603 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100604 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100605 };
606
607 psa_invec in_vec[] = {
608 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
609 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000610 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100611 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000612 {.base = output, .len = output_size},
613 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100614
Antonio de Angelis4743e672019-04-11 11:38:48 +0100615#ifdef TFM_PSA_API
616 PSA_CONNECT(TFM_CRYPTO);
617#endif
618
619 status = API_DISPATCH(tfm_crypto_cipher_finish,
620 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100621
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000622 *output_length = out_vec[1].len;
623
Antonio de Angelis4743e672019-04-11 11:38:48 +0100624#ifdef TFM_PSA_API
625 PSA_CLOSE();
626#endif
627
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000628 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100629}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100630
Antonio de Angelis377a1552018-11-22 17:02:40 +0000631psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100632 psa_algorithm_t alg)
633{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000634 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100635 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800636 .sfn_id = TFM_CRYPTO_HASH_SETUP_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100637 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100638 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000639 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100640
Antonio de Angelis4743e672019-04-11 11:38:48 +0100641 psa_invec in_vec[] = {
642 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
643 };
644 psa_outvec out_vec[] = {
645 {.base = &(operation->handle), .len = sizeof(uint32_t)},
646 };
647
648#ifdef TFM_PSA_API
649 PSA_CONNECT(TFM_CRYPTO);
650#endif
651
652 status = API_DISPATCH(tfm_crypto_hash_setup,
653 TFM_CRYPTO_HASH_SETUP);
654
655#ifdef TFM_PSA_API
656 PSA_CLOSE();
657#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100658
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000659 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100660}
661
662psa_status_t psa_hash_update(psa_hash_operation_t *operation,
663 const uint8_t *input,
664 size_t input_length)
665{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000666 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100667 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800668 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100669 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100670 };
671
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000672 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100673 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000674 {.base = input, .len = input_length},
675 };
676 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100677 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000678 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100679
Antonio de Angelis4743e672019-04-11 11:38:48 +0100680#ifdef TFM_PSA_API
681 PSA_CONNECT(TFM_CRYPTO);
682#endif
683
684 status = API_DISPATCH(tfm_crypto_hash_update,
685 TFM_CRYPTO_HASH_UPDATE);
686
687#ifdef TFM_PSA_API
688 PSA_CLOSE();
689#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100690
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000691 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100692}
693
694psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
695 uint8_t *hash,
696 size_t hash_size,
697 size_t *hash_length)
698{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000699 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100700 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800701 .sfn_id = TFM_CRYPTO_HASH_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100702 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100703 };
704
705 psa_invec in_vec[] = {
706 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
707 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000708 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100709 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000710 {.base = hash, .len = hash_size},
711 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100712
Antonio de Angelis4743e672019-04-11 11:38:48 +0100713#ifdef TFM_PSA_API
714 PSA_CONNECT(TFM_CRYPTO);
715#endif
716
717 status = API_DISPATCH(tfm_crypto_hash_finish,
718 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100719
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000720 *hash_length = out_vec[1].len;
721
Antonio de Angelis4743e672019-04-11 11:38:48 +0100722#ifdef TFM_PSA_API
723 PSA_CLOSE();
724#endif
725
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000726 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100727}
728
729psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
730 const uint8_t *hash,
731 size_t hash_length)
732{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000733 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100734 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800735 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100736 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100737 };
738
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000739 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100740 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000741 {.base = hash, .len = hash_length},
742 };
743 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100744 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000745 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100746
Antonio de Angelis4743e672019-04-11 11:38:48 +0100747#ifdef TFM_PSA_API
748 PSA_CONNECT(TFM_CRYPTO);
749#endif
750
751 status = API_DISPATCH(tfm_crypto_hash_verify,
752 TFM_CRYPTO_HASH_VERIFY);
753#ifdef TFM_PSA_API
754 PSA_CLOSE();
755#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100756
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000757 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100758}
759
760psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
761{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000762 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100763 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800764 .sfn_id = TFM_CRYPTO_HASH_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100765 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000766 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100767
Antonio de Angelis4743e672019-04-11 11:38:48 +0100768 psa_invec in_vec[] = {
769 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
770 };
771 psa_outvec out_vec[] = {
772 {.base = &(operation->handle), .len = sizeof(uint32_t)},
773 };
774
775#ifdef TFM_PSA_API
776 PSA_CONNECT(TFM_CRYPTO);
777#endif
778
779 status = API_DISPATCH(tfm_crypto_hash_abort,
780 TFM_CRYPTO_HASH_ABORT);
781#ifdef TFM_PSA_API
782 PSA_CLOSE();
783#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100784
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000785 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100786}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100787
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100788psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
789 psa_hash_operation_t *target_operation)
790{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100791 psa_status_t status;
792 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800793 .sfn_id = TFM_CRYPTO_HASH_CLONE_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100794 .op_handle = source_operation->handle,
795 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100796
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100797 psa_invec in_vec[] = {
798 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
799 };
800 psa_outvec out_vec[] = {
801 {.base = target_operation, .len = sizeof(psa_hash_operation_t)},
802 };
803
804#ifdef TFM_PSA_API
805 PSA_CONNECT(TFM_CRYPTO);
806#endif
807
808 status = API_DISPATCH(tfm_crypto_hash_clone,
809 TFM_CRYPTO_HASH_CLONE);
810#ifdef TFM_PSA_API
811 PSA_CLOSE();
812#endif
813
814 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100815}
816
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100817psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100818 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100819 psa_algorithm_t alg)
820{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000821 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100822 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800823 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100824 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100825 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100826 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000827 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100828
Antonio de Angelis4743e672019-04-11 11:38:48 +0100829 psa_invec in_vec[] = {
830 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
831 };
832 psa_outvec out_vec[] = {
833 {.base = &(operation->handle), .len = sizeof(uint32_t)},
834 };
835
836#ifdef TFM_PSA_API
837 PSA_CONNECT(TFM_CRYPTO);
838#endif
839
840 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
841 TFM_CRYPTO_MAC_SIGN_SETUP);
842#ifdef TFM_PSA_API
843 PSA_CLOSE();
844#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100845
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000846 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100847}
848
849psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100850 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100851 psa_algorithm_t alg)
852{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000853 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100854 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800855 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100856 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100857 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100858 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000859 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100860
Antonio de Angelis4743e672019-04-11 11:38:48 +0100861 psa_invec in_vec[] = {
862 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
863 };
864 psa_outvec out_vec[] = {
865 {.base = &(operation->handle), .len = sizeof(uint32_t)},
866 };
867
868#ifdef TFM_PSA_API
869 PSA_CONNECT(TFM_CRYPTO);
870#endif
871
872 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
873 TFM_CRYPTO_MAC_VERIFY_SETUP);
874#ifdef TFM_PSA_API
875 PSA_CLOSE();
876#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100877
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000878 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100879}
880
881psa_status_t psa_mac_update(psa_mac_operation_t *operation,
882 const uint8_t *input,
883 size_t input_length)
884{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000885 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100886 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800887 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100888 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100889 };
890
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000891 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100892 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000893 {.base = input, .len = input_length},
894 };
895 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100896 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000897 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100898
Antonio de Angelis4743e672019-04-11 11:38:48 +0100899#ifdef TFM_PSA_API
900 PSA_CONNECT(TFM_CRYPTO);
901#endif
902
903 status = API_DISPATCH(tfm_crypto_mac_update,
904 TFM_CRYPTO_MAC_UPDATE);
905#ifdef TFM_PSA_API
906 PSA_CLOSE();
907#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100908
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000909 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100910}
911
912psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
913 uint8_t *mac,
914 size_t mac_size,
915 size_t *mac_length)
916{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000917 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100918 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800919 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100920 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100921 };
922
923 psa_invec in_vec[] = {
924 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
925 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000926 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100927 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000928 {.base = mac, .len = mac_size},
929 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100930
Antonio de Angelis4743e672019-04-11 11:38:48 +0100931#ifdef TFM_PSA_API
932 PSA_CONNECT(TFM_CRYPTO);
933#endif
934
935 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
936 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100937
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000938 *mac_length = out_vec[1].len;
939
Antonio de Angelis4743e672019-04-11 11:38:48 +0100940#ifdef TFM_PSA_API
941 PSA_CLOSE();
942#endif
943
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000944 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100945}
946
947psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
948 const uint8_t *mac,
949 size_t mac_length)
950{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000951 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100952 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800953 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100954 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100955 };
956
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000957 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100958 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000959 {.base = mac, .len = mac_length},
960 };
961 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100962 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000963 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100964
Antonio de Angelis4743e672019-04-11 11:38:48 +0100965#ifdef TFM_PSA_API
966 PSA_CONNECT(TFM_CRYPTO);
967#endif
968
969 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
970 TFM_CRYPTO_MAC_VERIFY_FINISH);
971
972#ifdef TFM_PSA_API
973 PSA_CLOSE();
974#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100975
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000976 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100977}
978
979psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
980{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000981 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100982 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800983 .sfn_id = TFM_CRYPTO_MAC_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100984 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000985 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100986
Antonio de Angelis4743e672019-04-11 11:38:48 +0100987 psa_invec in_vec[] = {
988 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
989 };
990 psa_outvec out_vec[] = {
991 {.base = &(operation->handle), .len = sizeof(uint32_t)},
992 };
993
994#ifdef TFM_PSA_API
995 PSA_CONNECT(TFM_CRYPTO);
996#endif
997
998 status = API_DISPATCH(tfm_crypto_mac_abort,
999 TFM_CRYPTO_MAC_ABORT);
1000#ifdef TFM_PSA_API
1001 PSA_CLOSE();
1002#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001003
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001004 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001005}
Antonio de Angelis3a480992018-11-07 11:53:28 +00001006
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001007psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001008 psa_algorithm_t alg,
1009 const uint8_t *nonce,
1010 size_t nonce_length,
1011 const uint8_t *additional_data,
1012 size_t additional_data_length,
1013 const uint8_t *plaintext,
1014 size_t plaintext_length,
1015 uint8_t *ciphertext,
1016 size_t ciphertext_size,
1017 size_t *ciphertext_length)
1018{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001019 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001020 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001021 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001022 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001023 .alg = alg,
1024 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001025 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001026
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001027 /* Sanitize the optional input */
1028 if ((additional_data == NULL) && (additional_data_length != 0)) {
1029 return PSA_ERROR_INVALID_ARGUMENT;
1030 }
1031
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001032 size_t idx = 0;
1033 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001034 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001035 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001036 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001037 };
1038 psa_outvec out_vec[] = {
1039 {.base = ciphertext, .len = ciphertext_size},
1040 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001041
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001042 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1043 return PSA_ERROR_INVALID_ARGUMENT;
1044 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001045
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001046 if (nonce != NULL) {
1047 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001048 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001049 }
1050 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001051
Antonio de Angelis4743e672019-04-11 11:38:48 +01001052#ifdef TFM_PSA_API
1053 PSA_CONNECT(TFM_CRYPTO);
1054#endif
1055
1056#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001057 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001058 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001059 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001060 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001061 status = psa_call(ipc_handle, in_vec, in_len,
1062 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001063#else
1064 status = API_DISPATCH(tfm_crypto_aead_encrypt,
1065 TFM_CRYPTO_AEAD_ENCRYPT);
1066#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001067
1068 *ciphertext_length = out_vec[0].len;
1069
Antonio de Angelis4743e672019-04-11 11:38:48 +01001070#ifdef TFM_PSA_API
1071 PSA_CLOSE();
1072#endif
1073
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001074 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001075}
1076
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001077psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001078 psa_algorithm_t alg,
1079 const uint8_t *nonce,
1080 size_t nonce_length,
1081 const uint8_t *additional_data,
1082 size_t additional_data_length,
1083 const uint8_t *ciphertext,
1084 size_t ciphertext_length,
1085 uint8_t *plaintext,
1086 size_t plaintext_size,
1087 size_t *plaintext_length)
1088{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001089 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001090 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001091 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001092 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001093 .alg = alg,
1094 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001095 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001096
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001097 /* Sanitize the optional input */
1098 if ((additional_data == NULL) && (additional_data_length != 0)) {
1099 return PSA_ERROR_INVALID_ARGUMENT;
1100 }
1101
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001102 size_t idx = 0;
1103 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001104 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001105 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001106 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001107 };
1108 psa_outvec out_vec[] = {
1109 {.base = plaintext, .len = plaintext_size},
1110 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001111
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001112 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1113 return PSA_ERROR_INVALID_ARGUMENT;
1114 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001115
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001116 if (nonce != NULL) {
1117 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001118 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001119 }
1120 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001121
Antonio de Angelis4743e672019-04-11 11:38:48 +01001122#ifdef TFM_PSA_API
1123 PSA_CONNECT(TFM_CRYPTO);
1124#endif
1125
1126#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001127 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001128 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001129 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001130 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001131 status = psa_call(ipc_handle, in_vec, in_len,
1132 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001133#else
1134 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1135 TFM_CRYPTO_AEAD_DECRYPT);
1136#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001137
1138 *plaintext_length = out_vec[0].len;
1139
Antonio de Angelis4743e672019-04-11 11:38:48 +01001140#ifdef TFM_PSA_API
1141 PSA_CLOSE();
1142#endif
1143
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001144 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001145}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001146
1147psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
1148 psa_algorithm_t alg,
1149 const uint8_t *hash,
1150 size_t hash_length,
1151 uint8_t *signature,
1152 size_t signature_size,
1153 size_t *signature_length)
1154{
1155 psa_status_t status;
1156 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001157 .sfn_id = TFM_CRYPTO_ASYMMETRIC_SIGN_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001158 .key_handle = handle,
1159 .alg = alg,
1160 };
1161
1162 psa_invec in_vec[] = {
1163 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1164 {.base = hash, .len = hash_length},
1165 };
1166 psa_outvec out_vec[] = {
1167 {.base = signature, .len = signature_size},
1168 };
1169
1170#ifdef TFM_PSA_API
1171 PSA_CONNECT(TFM_CRYPTO);
1172#endif
1173
1174 status = API_DISPATCH(tfm_crypto_asymmetric_sign,
1175 TFM_CRYPTO_ASYMMETRIC_SIGN);
1176
1177 *signature_length = out_vec[0].len;
1178
1179#ifdef TFM_PSA_API
1180 PSA_CLOSE();
1181#endif
1182
1183 return status;
1184}
1185
1186psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
1187 psa_algorithm_t alg,
1188 const uint8_t *hash,
1189 size_t hash_length,
1190 const uint8_t *signature,
1191 size_t signature_length)
1192{
1193 psa_status_t status;
1194 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001195 .sfn_id = TFM_CRYPTO_ASYMMETRIC_VERIFY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001196 .key_handle = handle,
1197 .alg = alg
1198 };
1199
1200 psa_invec in_vec[] = {
1201 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1202 {.base = hash, .len = hash_length},
1203 {.base = signature, .len = signature_length}
1204 };
1205
1206#ifdef TFM_PSA_API
1207 PSA_CONNECT(TFM_CRYPTO);
1208#endif
1209
1210 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_asymmetric_verify,
1211 TFM_CRYPTO_ASYMMETRIC_VERIFY);
1212#ifdef TFM_PSA_API
1213 PSA_CLOSE();
1214#endif
1215
1216 return status;
1217}
1218
1219psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
1220 psa_algorithm_t alg,
1221 const uint8_t *input,
1222 size_t input_length,
1223 const uint8_t *salt,
1224 size_t salt_length,
1225 uint8_t *output,
1226 size_t output_size,
1227 size_t *output_length)
1228{
1229 psa_status_t status;
1230 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001231 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001232 .key_handle = handle,
1233 .alg = alg
1234 };
1235
1236 /* Sanitize the optional input */
1237 if ((salt == NULL) && (salt_length != 0)) {
1238 return PSA_ERROR_INVALID_ARGUMENT;
1239 }
1240
1241 psa_invec in_vec[] = {
1242 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1243 {.base = input, .len = input_length},
1244 {.base = salt, .len = salt_length}
1245 };
1246
1247 psa_outvec out_vec[] = {
1248 {.base = output, .len = output_size},
1249 };
1250
1251#ifdef TFM_PSA_API
1252 PSA_CONNECT(TFM_CRYPTO);
1253#endif
1254
1255#ifdef TFM_PSA_API
1256 size_t in_len = ARRAY_SIZE(in_vec);
1257 if (salt == NULL) {
1258 in_len--;
1259 }
1260 status = psa_call(ipc_handle, in_vec, in_len,
1261 out_vec, ARRAY_SIZE(out_vec));
1262#else
1263 status = API_DISPATCH(tfm_crypto_asymmetric_encrypt,
1264 TFM_CRYPTO_ASYMMETRIC_ENCRYPT);
1265#endif
1266
1267 *output_length = out_vec[0].len;
1268
1269#ifdef TFM_PSA_API
1270 PSA_CLOSE();
1271#endif
1272
1273 return status;
1274}
1275
1276psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
1277 psa_algorithm_t alg,
1278 const uint8_t *input,
1279 size_t input_length,
1280 const uint8_t *salt,
1281 size_t salt_length,
1282 uint8_t *output,
1283 size_t output_size,
1284 size_t *output_length)
1285{
1286 psa_status_t status;
1287 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001288 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001289 .key_handle = handle,
1290 .alg = alg
1291 };
1292
1293 /* Sanitize the optional input */
1294 if ((salt == NULL) && (salt_length != 0)) {
1295 return PSA_ERROR_INVALID_ARGUMENT;
1296 }
1297
1298 psa_invec in_vec[] = {
1299 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1300 {.base = input, .len = input_length},
1301 {.base = salt, .len = salt_length}
1302 };
1303
1304 psa_outvec out_vec[] = {
1305 {.base = output, .len = output_size},
1306 };
1307
1308#ifdef TFM_PSA_API
1309 PSA_CONNECT(TFM_CRYPTO);
1310#endif
1311
1312#ifdef TFM_PSA_API
1313 size_t in_len = ARRAY_SIZE(in_vec);
1314 if (salt == NULL) {
1315 in_len--;
1316 }
1317 status = psa_call(ipc_handle, in_vec, in_len,
1318 out_vec, ARRAY_SIZE(out_vec));
1319#else
1320 status = API_DISPATCH(tfm_crypto_asymmetric_decrypt,
1321 TFM_CRYPTO_ASYMMETRIC_DECRYPT);
1322#endif
1323
1324 *output_length = out_vec[0].len;
1325
1326#ifdef TFM_PSA_API
1327 PSA_CLOSE();
1328#endif
1329
1330 return status;
1331}
1332
1333psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1334 size_t *capacity)
1335{
1336 psa_status_t status;
1337 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001338 .sfn_id = TFM_CRYPTO_GET_GENERATOR_CAPACITY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001339 .op_handle = generator->handle,
1340 };
1341
1342 psa_invec in_vec[] = {
1343 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1344 };
1345
1346 psa_outvec out_vec[] = {
1347 {.base = capacity, .len = sizeof(size_t)},
1348 };
1349
1350#ifdef TFM_PSA_API
1351 PSA_CONNECT(TFM_CRYPTO);
1352#endif
1353
1354 status = API_DISPATCH(tfm_crypto_get_generator_capacity,
1355 TFM_CRYPTO_GET_GENERATOR_CAPACITY);
1356#ifdef TFM_PSA_API
1357 PSA_CLOSE();
1358#endif
1359
1360 return status;
1361}
1362
1363psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1364 uint8_t *output,
1365 size_t output_length)
1366{
1367 psa_status_t status;
1368 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001369 .sfn_id = TFM_CRYPTO_GENERATOR_READ_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001370 .op_handle = generator->handle,
1371 };
1372
1373 psa_invec in_vec[] = {
1374 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1375 };
1376
1377 psa_outvec out_vec[] = {
1378 {.base = output, .len = output_length},
1379 };
1380
1381#ifdef TFM_PSA_API
1382 PSA_CONNECT(TFM_CRYPTO);
1383#endif
1384
1385 status = API_DISPATCH(tfm_crypto_generator_read,
1386 TFM_CRYPTO_GENERATOR_READ);
1387#ifdef TFM_PSA_API
1388 PSA_CLOSE();
1389#endif
1390
1391 return status;
1392}
1393
1394psa_status_t psa_generator_import_key(psa_key_handle_t handle,
1395 psa_key_type_t type,
1396 size_t bits,
1397 psa_crypto_generator_t *generator)
1398{
1399 psa_status_t status;
1400 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001401 .sfn_id = TFM_CRYPTO_GENERATOR_IMPORT_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001402 .key_handle = handle,
1403 .type = type,
1404 .op_handle = generator->handle,
1405 };
1406
1407 psa_invec in_vec[] = {
1408 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1409 {.base = &bits, .len = sizeof(size_t)},
1410 };
1411
1412#ifdef TFM_PSA_API
1413 PSA_CONNECT(TFM_CRYPTO);
1414#endif
1415
1416 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generator_import_key,
1417 TFM_CRYPTO_GENERATOR_IMPORT_KEY);
1418#ifdef TFM_PSA_API
1419 PSA_CLOSE();
1420#endif
1421
1422 return status;
1423}
1424
1425psa_status_t psa_generator_abort(psa_crypto_generator_t *generator)
1426{
1427 psa_status_t status;
1428 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001429 .sfn_id = TFM_CRYPTO_GENERATOR_ABORT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001430 .op_handle = generator->handle,
1431 };
1432
1433 psa_invec in_vec[] = {
1434 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1435 };
1436
1437 psa_outvec out_vec[] = {
1438 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1439 };
1440
1441#ifdef TFM_PSA_API
1442 PSA_CONNECT(TFM_CRYPTO);
1443#endif
1444
1445 status = API_DISPATCH(tfm_crypto_generator_abort,
1446 TFM_CRYPTO_GENERATOR_ABORT);
1447#ifdef TFM_PSA_API
1448 PSA_CLOSE();
1449#endif
1450
1451 return status;
1452}
1453
1454psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
1455 psa_key_handle_t handle,
1456 psa_algorithm_t alg,
1457 const uint8_t *salt,
1458 size_t salt_length,
1459 const uint8_t *label,
1460 size_t label_length,
1461 size_t capacity)
1462{
1463 psa_status_t status;
1464 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001465 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001466 .key_handle = handle,
1467 .alg = alg,
1468 .op_handle = generator->handle,
1469 .capacity = capacity,
1470 };
1471
1472 /* Sanitize the optional input */
1473 if ((salt == NULL) && (salt_length != 0)) {
1474 return PSA_ERROR_INVALID_ARGUMENT;
1475 }
1476
1477 if ((label == NULL) && (label_length != 0)) {
1478 return PSA_ERROR_INVALID_ARGUMENT;
1479 }
1480
1481 psa_invec in_vec[] = {
1482 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1483 {.base = salt, .len = salt_length},
1484 {.base = label, .len = label_length},
1485 };
1486
1487 psa_outvec out_vec[] = {
1488 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1489 };
1490
1491#ifdef TFM_PSA_API
1492 PSA_CONNECT(TFM_CRYPTO);
1493#endif
1494
1495#ifdef TFM_PSA_API
1496 size_t in_len = ARRAY_SIZE(in_vec);
1497 if (label == NULL) {
1498 in_len--;
1499 if (salt == NULL) {
1500 in_len--;
1501 }
1502 }
1503 status = psa_call(ipc_handle, in_vec, in_len,
1504 out_vec, ARRAY_SIZE(out_vec));
1505#else
1506 status = API_DISPATCH(tfm_crypto_key_derivation,
1507 TFM_CRYPTO_KEY_DERIVATION);
1508#endif
1509
1510#ifdef TFM_PSA_API
1511 PSA_CLOSE();
1512#endif
1513
1514 return status;
1515}
1516
1517psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
1518 psa_key_handle_t private_key,
1519 const uint8_t *peer_key,
1520 size_t peer_key_length,
1521 psa_algorithm_t alg)
1522{
1523 psa_status_t status;
1524 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001525 .sfn_id = TFM_CRYPTO_KEY_AGREEMENT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001526 .key_handle = private_key,
1527 .alg = alg,
1528 .op_handle = generator->handle,
1529 };
1530
1531 psa_invec in_vec[] = {
1532 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1533 {.base = peer_key, .len = peer_key_length},
1534 };
1535
1536 psa_outvec out_vec[] = {
1537 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1538 };
1539
1540#ifdef TFM_PSA_API
1541 PSA_CONNECT(TFM_CRYPTO);
1542#endif
1543
1544 status = API_DISPATCH(tfm_crypto_key_agreement,
1545 TFM_CRYPTO_KEY_AGREEMENT);
1546
1547#ifdef TFM_PSA_API
1548 PSA_CLOSE();
1549#endif
1550
1551 return status;
1552}
1553
1554psa_status_t psa_generate_random(uint8_t *output,
1555 size_t output_size)
1556{
1557 psa_status_t status;
1558 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001559 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001560 };
1561
1562 psa_invec in_vec[] = {
1563 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1564 };
1565
1566 psa_outvec out_vec[] = {
1567 {.base = output, .len = output_size},
1568 };
1569
1570 if (output_size == 0) {
1571 return PSA_SUCCESS;
1572 }
1573
1574#ifdef TFM_PSA_API
1575 PSA_CONNECT(TFM_CRYPTO);
1576#endif
1577
1578 status = API_DISPATCH(tfm_crypto_generate_random,
1579 TFM_CRYPTO_GENERATE_RANDOM);
1580
1581#ifdef TFM_PSA_API
1582 PSA_CLOSE();
1583#endif
1584
1585 return status;
1586}
1587
1588psa_status_t psa_generate_key(psa_key_handle_t handle,
1589 psa_key_type_t type,
1590 size_t bits,
1591 const void *extra,
1592 size_t extra_size)
1593{
1594 psa_status_t status;
1595 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001596 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001597 .key_handle = handle,
1598 .type = type,
1599 };
1600
1601 /* Sanitize the optional input */
1602 if ((extra == NULL) && (extra_size != 0)) {
1603 return PSA_ERROR_INVALID_ARGUMENT;
1604 }
1605
1606 psa_invec in_vec[] = {
1607 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1608 {.base = &bits, .len = sizeof(size_t)},
1609 {.base = extra, .len = extra_size},
1610 };
1611
1612#ifdef TFM_PSA_API
1613 PSA_CONNECT(TFM_CRYPTO);
1614#endif
1615
1616#ifdef TFM_PSA_API
1617 size_t in_len = ARRAY_SIZE(in_vec);
1618 if (extra == NULL) {
1619 in_len--;
1620 }
1621
1622 status = psa_call(ipc_handle, in_vec, in_len, NULL, 0);
1623#else
1624 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generate_key,
1625 TFM_CRYPTO_GENERATE_KEY);
1626#endif
1627
1628#ifdef TFM_PSA_API
1629 PSA_CLOSE();
1630#endif
1631
1632 return status;
1633}