blob: fe8a53776f2359f7e7a03b6b694a63511853e402 [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"
Antonio de Angelis8908f472018-08-31 15:44:25 +010010#include "psa_crypto.h"
11#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
16#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 = {
64 .sfn_id = TFM_CRYPTO_ALLOCATE_KEY_SFID,
65 };
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
94 /* TODO: This API is not supported yet */
95 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
106 /* TODO: This API is not supported yet */
107 return PSA_ERROR_NOT_SUPPORTED;
108}
109
110psa_status_t psa_close_key(psa_key_handle_t handle)
111{
112 (void)handle;
113
114 /* TODO: This API is not supported yet */
115 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 = {
125 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SFID,
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 = {
151 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SFID,
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 = {
177 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SFID,
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 = {
208 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SFID,
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{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100239 (void)handle;
Hugues de Valon8b442442019-02-19 14:30:52 +0000240 (void)data;
241 (void)data_size;
242 (void)data_length;
243
Antonio de Angelis8908f472018-08-31 15:44:25 +0100244 /* TODO: This API is not supported yet */
245 return PSA_ERROR_NOT_SUPPORTED;
246}
247
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100248psa_status_t psa_copy_key(psa_key_handle_t source_handle,
249 psa_key_handle_t target_handle,
250 const psa_key_policy_t *constraint)
Jamie Foxefd82732018-11-26 10:34:32 +0000251{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100252 (void)source_handle;
253 (void)target_handle;
254 (void)constraint;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000255
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100256 /* TODO: This API is not supported yet */
257 return PSA_ERROR_NOT_SUPPORTED;
Jamie Foxefd82732018-11-26 10:34:32 +0000258}
259
260void psa_key_policy_set_usage(psa_key_policy_t *policy,
261 psa_key_usage_t usage,
262 psa_algorithm_t alg)
263{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100264 policy->usage = usage;
265 policy->alg = alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000266}
267
268psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
269{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100270 return policy->usage;
Jamie Foxefd82732018-11-26 10:34:32 +0000271}
272
273psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
274{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100275 return policy->alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000276}
277
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100278psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000279 const psa_key_policy_t *policy)
280{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000281 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100282 struct tfm_crypto_pack_iovec iov = {
283 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100284 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100285 };
286
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000287 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100288 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000289 {.base = policy, .len = sizeof(psa_key_policy_t)},
290 };
Jamie Foxefd82732018-11-26 10:34:32 +0000291
Antonio de Angelis4743e672019-04-11 11:38:48 +0100292#ifdef TFM_PSA_API
293 PSA_CONNECT(TFM_CRYPTO);
294#endif
295
296 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
297 TFM_CRYPTO_SET_KEY_POLICY);
298#ifdef TFM_PSA_API
299 PSA_CLOSE();
300#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000301
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000302 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000303}
304
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100305psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000306 psa_key_policy_t *policy)
307{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000308 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100309 struct tfm_crypto_pack_iovec iov = {
310 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100311 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100312 };
313
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000314 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100315 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000316 };
317 psa_outvec out_vec[] = {
318 {.base = policy, .len = sizeof(psa_key_policy_t)},
319 };
Jamie Foxefd82732018-11-26 10:34:32 +0000320
Antonio de Angelis4743e672019-04-11 11:38:48 +0100321#ifdef TFM_PSA_API
322 PSA_CONNECT(TFM_CRYPTO);
323#endif
324
325 status = API_DISPATCH(tfm_crypto_get_key_policy,
326 TFM_CRYPTO_GET_KEY_POLICY);
327#ifdef TFM_PSA_API
328 PSA_CLOSE();
329#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000330
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000331 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000332}
333
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100334psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000335 psa_key_lifetime_t *lifetime)
336{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000337 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100338 struct tfm_crypto_pack_iovec iov = {
339 .sfn_id = TFM_CRYPTO_GET_KEY_LIFETIME_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100340 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100341 };
342
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000343 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100344 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000345 };
346 psa_outvec out_vec[] = {
347 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
348 };
Jamie Foxefd82732018-11-26 10:34:32 +0000349
Antonio de Angelis4743e672019-04-11 11:38:48 +0100350#ifdef TFM_PSA_API
351 PSA_CONNECT(TFM_CRYPTO);
352#endif
353
354 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
355 TFM_CRYPTO_GET_KEY_LIFETIME);
356#ifdef TFM_PSA_API
357 PSA_CLOSE();
358#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000359
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000360 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000361}
362
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100363psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
364 unsigned char *iv,
365 size_t iv_size,
366 size_t *iv_length)
367{
368 (void) operation;
369 (void) iv;
370 (void) iv_size;
371 (void) iv_length;
372
373 /* TODO: This API is not supported yet */
374 return PSA_ERROR_NOT_SUPPORTED;
375}
376
Antonio de Angelis377a1552018-11-22 17:02:40 +0000377psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
378 const unsigned char *iv,
379 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100380{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000381 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100382 struct tfm_crypto_pack_iovec iov = {
383 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100384 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100385 };
386
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000387 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100388 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000389 {.base = iv, .len = iv_length},
390 };
391 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100392 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000393 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100394
Antonio de Angelis4743e672019-04-11 11:38:48 +0100395#ifdef TFM_PSA_API
396 PSA_CONNECT(TFM_CRYPTO);
397#endif
398
399 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
400 TFM_CRYPTO_CIPHER_SET_IV);
401#ifdef TFM_PSA_API
402 PSA_CLOSE();
403#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100404
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000405 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100406}
407
Antonio de Angelis377a1552018-11-22 17:02:40 +0000408psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100409 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000410 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100411{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000412 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100413 struct tfm_crypto_pack_iovec iov = {
414 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100415 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100416 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100417 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000418 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100419
Antonio de Angelis4743e672019-04-11 11:38:48 +0100420 psa_invec in_vec[] = {
421 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
422 };
423 psa_outvec out_vec[] = {
424 {.base = &(operation->handle), .len = sizeof(uint32_t)},
425 };
426
427#ifdef TFM_PSA_API
428 PSA_CONNECT(TFM_CRYPTO);
429#endif
430
431 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
432 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
433#ifdef TFM_PSA_API
434 PSA_CLOSE();
435#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100436
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000437 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100438}
439
Antonio de Angelis377a1552018-11-22 17:02:40 +0000440psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100441 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000442 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100443{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000444 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100445 struct tfm_crypto_pack_iovec iov = {
446 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100447 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100448 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100449 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000450 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100451
Antonio de Angelis4743e672019-04-11 11:38:48 +0100452 psa_invec in_vec[] = {
453 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
454 };
455 psa_outvec out_vec[] = {
456 {.base = &(operation->handle), .len = sizeof(uint32_t)},
457 };
458
459#ifdef TFM_PSA_API
460 PSA_CONNECT(TFM_CRYPTO);
461#endif
462
463 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
464 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
465#ifdef TFM_PSA_API
466 PSA_CLOSE();
467#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100468
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000469 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100470}
471
472psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
473 const uint8_t *input,
474 size_t input_length,
475 unsigned char *output,
476 size_t output_size,
477 size_t *output_length)
478{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000479 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100480 struct tfm_crypto_pack_iovec iov = {
481 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100482 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100483 };
484
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000485 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100486 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000487 {.base = input, .len = input_length},
488 };
489 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100490 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000491 {.base = output, .len = output_size}
492 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100493
Antonio de Angelis4743e672019-04-11 11:38:48 +0100494#ifdef TFM_PSA_API
495 PSA_CONNECT(TFM_CRYPTO);
496#endif
497
498 status = API_DISPATCH(tfm_crypto_cipher_update,
499 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100500
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000501 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100502
Antonio de Angelis4743e672019-04-11 11:38:48 +0100503#ifdef TFM_PSA_API
504 PSA_CLOSE();
505#endif
506
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000507 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100508}
509
510psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
511{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000512 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100513 struct tfm_crypto_pack_iovec iov = {
514 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100515 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000516 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100517
Antonio de Angelis4743e672019-04-11 11:38:48 +0100518 psa_invec in_vec[] = {
519 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
520 };
521 psa_outvec out_vec[] = {
522 {.base = &(operation->handle), .len = sizeof(uint32_t)},
523 };
524
525#ifdef TFM_PSA_API
526 PSA_CONNECT(TFM_CRYPTO);
527#endif
528
529 status = API_DISPATCH(tfm_crypto_cipher_abort,
530 TFM_CRYPTO_CIPHER_ABORT);
531#ifdef TFM_PSA_API
532 PSA_CLOSE();
533#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100534
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000535 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100536}
537
538psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
539 uint8_t *output,
540 size_t output_size,
541 size_t *output_length)
542{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000543 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100544 struct tfm_crypto_pack_iovec iov = {
545 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100546 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100547 };
548
549 psa_invec in_vec[] = {
550 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
551 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000552 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100553 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000554 {.base = output, .len = output_size},
555 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100556
Antonio de Angelis4743e672019-04-11 11:38:48 +0100557#ifdef TFM_PSA_API
558 PSA_CONNECT(TFM_CRYPTO);
559#endif
560
561 status = API_DISPATCH(tfm_crypto_cipher_finish,
562 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100563
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000564 *output_length = out_vec[1].len;
565
Antonio de Angelis4743e672019-04-11 11:38:48 +0100566#ifdef TFM_PSA_API
567 PSA_CLOSE();
568#endif
569
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000570 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100571}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100572
Antonio de Angelis377a1552018-11-22 17:02:40 +0000573psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100574 psa_algorithm_t alg)
575{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000576 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100577 struct tfm_crypto_pack_iovec iov = {
578 .sfn_id = TFM_CRYPTO_HASH_SETUP_SFID,
579 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100580 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000581 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100582
Antonio de Angelis4743e672019-04-11 11:38:48 +0100583 psa_invec in_vec[] = {
584 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
585 };
586 psa_outvec out_vec[] = {
587 {.base = &(operation->handle), .len = sizeof(uint32_t)},
588 };
589
590#ifdef TFM_PSA_API
591 PSA_CONNECT(TFM_CRYPTO);
592#endif
593
594 status = API_DISPATCH(tfm_crypto_hash_setup,
595 TFM_CRYPTO_HASH_SETUP);
596
597#ifdef TFM_PSA_API
598 PSA_CLOSE();
599#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100600
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000601 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100602}
603
604psa_status_t psa_hash_update(psa_hash_operation_t *operation,
605 const uint8_t *input,
606 size_t input_length)
607{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000608 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100609 struct tfm_crypto_pack_iovec iov = {
610 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100611 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100612 };
613
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000614 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100615 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000616 {.base = input, .len = input_length},
617 };
618 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100619 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000620 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100621
Antonio de Angelis4743e672019-04-11 11:38:48 +0100622#ifdef TFM_PSA_API
623 PSA_CONNECT(TFM_CRYPTO);
624#endif
625
626 status = API_DISPATCH(tfm_crypto_hash_update,
627 TFM_CRYPTO_HASH_UPDATE);
628
629#ifdef TFM_PSA_API
630 PSA_CLOSE();
631#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100632
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000633 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100634}
635
636psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
637 uint8_t *hash,
638 size_t hash_size,
639 size_t *hash_length)
640{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000641 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100642 struct tfm_crypto_pack_iovec iov = {
643 .sfn_id = TFM_CRYPTO_HASH_FINISH_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100644 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100645 };
646
647 psa_invec in_vec[] = {
648 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
649 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000650 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100651 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000652 {.base = hash, .len = hash_size},
653 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100654
Antonio de Angelis4743e672019-04-11 11:38:48 +0100655#ifdef TFM_PSA_API
656 PSA_CONNECT(TFM_CRYPTO);
657#endif
658
659 status = API_DISPATCH(tfm_crypto_hash_finish,
660 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100661
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000662 *hash_length = out_vec[1].len;
663
Antonio de Angelis4743e672019-04-11 11:38:48 +0100664#ifdef TFM_PSA_API
665 PSA_CLOSE();
666#endif
667
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000668 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100669}
670
671psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
672 const uint8_t *hash,
673 size_t hash_length)
674{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000675 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100676 struct tfm_crypto_pack_iovec iov = {
677 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100678 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100679 };
680
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000681 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100682 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000683 {.base = hash, .len = hash_length},
684 };
685 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100686 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000687 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100688
Antonio de Angelis4743e672019-04-11 11:38:48 +0100689#ifdef TFM_PSA_API
690 PSA_CONNECT(TFM_CRYPTO);
691#endif
692
693 status = API_DISPATCH(tfm_crypto_hash_verify,
694 TFM_CRYPTO_HASH_VERIFY);
695#ifdef TFM_PSA_API
696 PSA_CLOSE();
697#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100698
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000699 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100700}
701
702psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
703{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000704 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100705 struct tfm_crypto_pack_iovec iov = {
706 .sfn_id = TFM_CRYPTO_HASH_ABORT_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100707 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000708 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100709
Antonio de Angelis4743e672019-04-11 11:38:48 +0100710 psa_invec in_vec[] = {
711 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
712 };
713 psa_outvec out_vec[] = {
714 {.base = &(operation->handle), .len = sizeof(uint32_t)},
715 };
716
717#ifdef TFM_PSA_API
718 PSA_CONNECT(TFM_CRYPTO);
719#endif
720
721 status = API_DISPATCH(tfm_crypto_hash_abort,
722 TFM_CRYPTO_HASH_ABORT);
723#ifdef TFM_PSA_API
724 PSA_CLOSE();
725#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100726
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000727 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100728}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100729
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100730psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
731 psa_hash_operation_t *target_operation)
732{
733 (void)source_operation;
734 (void)target_operation;
735
736 /* TODO: This API is not supported yet */
737 return PSA_ERROR_NOT_SUPPORTED;
738}
739
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100740psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100741 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100742 psa_algorithm_t alg)
743{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000744 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100745 struct tfm_crypto_pack_iovec iov = {
746 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100747 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100748 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100749 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000750 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100751
Antonio de Angelis4743e672019-04-11 11:38:48 +0100752 psa_invec in_vec[] = {
753 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
754 };
755 psa_outvec out_vec[] = {
756 {.base = &(operation->handle), .len = sizeof(uint32_t)},
757 };
758
759#ifdef TFM_PSA_API
760 PSA_CONNECT(TFM_CRYPTO);
761#endif
762
763 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
764 TFM_CRYPTO_MAC_SIGN_SETUP);
765#ifdef TFM_PSA_API
766 PSA_CLOSE();
767#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100768
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000769 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100770}
771
772psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100773 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100774 psa_algorithm_t alg)
775{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000776 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100777 struct tfm_crypto_pack_iovec iov = {
778 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100779 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100780 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100781 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000782 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100783
Antonio de Angelis4743e672019-04-11 11:38:48 +0100784 psa_invec in_vec[] = {
785 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
786 };
787 psa_outvec out_vec[] = {
788 {.base = &(operation->handle), .len = sizeof(uint32_t)},
789 };
790
791#ifdef TFM_PSA_API
792 PSA_CONNECT(TFM_CRYPTO);
793#endif
794
795 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
796 TFM_CRYPTO_MAC_VERIFY_SETUP);
797#ifdef TFM_PSA_API
798 PSA_CLOSE();
799#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100800
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000801 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100802}
803
804psa_status_t psa_mac_update(psa_mac_operation_t *operation,
805 const uint8_t *input,
806 size_t input_length)
807{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000808 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100809 struct tfm_crypto_pack_iovec iov = {
810 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100811 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100812 };
813
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000814 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100815 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000816 {.base = input, .len = input_length},
817 };
818 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100819 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000820 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100821
Antonio de Angelis4743e672019-04-11 11:38:48 +0100822#ifdef TFM_PSA_API
823 PSA_CONNECT(TFM_CRYPTO);
824#endif
825
826 status = API_DISPATCH(tfm_crypto_mac_update,
827 TFM_CRYPTO_MAC_UPDATE);
828#ifdef TFM_PSA_API
829 PSA_CLOSE();
830#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100831
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000832 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100833}
834
835psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
836 uint8_t *mac,
837 size_t mac_size,
838 size_t *mac_length)
839{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000840 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100841 struct tfm_crypto_pack_iovec iov = {
842 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100843 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100844 };
845
846 psa_invec in_vec[] = {
847 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
848 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000849 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100850 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000851 {.base = mac, .len = mac_size},
852 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100853
Antonio de Angelis4743e672019-04-11 11:38:48 +0100854#ifdef TFM_PSA_API
855 PSA_CONNECT(TFM_CRYPTO);
856#endif
857
858 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
859 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100860
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000861 *mac_length = out_vec[1].len;
862
Antonio de Angelis4743e672019-04-11 11:38:48 +0100863#ifdef TFM_PSA_API
864 PSA_CLOSE();
865#endif
866
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000867 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100868}
869
870psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
871 const uint8_t *mac,
872 size_t mac_length)
873{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000874 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100875 struct tfm_crypto_pack_iovec iov = {
876 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100877 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100878 };
879
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000880 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100881 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000882 {.base = mac, .len = mac_length},
883 };
884 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100885 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000886 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100887
Antonio de Angelis4743e672019-04-11 11:38:48 +0100888#ifdef TFM_PSA_API
889 PSA_CONNECT(TFM_CRYPTO);
890#endif
891
892 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
893 TFM_CRYPTO_MAC_VERIFY_FINISH);
894
895#ifdef TFM_PSA_API
896 PSA_CLOSE();
897#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100898
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000899 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100900}
901
902psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
903{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000904 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100905 struct tfm_crypto_pack_iovec iov = {
906 .sfn_id = TFM_CRYPTO_MAC_ABORT_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100907 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000908 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100909
Antonio de Angelis4743e672019-04-11 11:38:48 +0100910 psa_invec in_vec[] = {
911 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
912 };
913 psa_outvec out_vec[] = {
914 {.base = &(operation->handle), .len = sizeof(uint32_t)},
915 };
916
917#ifdef TFM_PSA_API
918 PSA_CONNECT(TFM_CRYPTO);
919#endif
920
921 status = API_DISPATCH(tfm_crypto_mac_abort,
922 TFM_CRYPTO_MAC_ABORT);
923#ifdef TFM_PSA_API
924 PSA_CLOSE();
925#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100926
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000927 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100928}
Antonio de Angelis3a480992018-11-07 11:53:28 +0000929
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100930psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +0000931 psa_algorithm_t alg,
932 const uint8_t *nonce,
933 size_t nonce_length,
934 const uint8_t *additional_data,
935 size_t additional_data_length,
936 const uint8_t *plaintext,
937 size_t plaintext_length,
938 uint8_t *ciphertext,
939 size_t ciphertext_size,
940 size_t *ciphertext_length)
941{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000942 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100943 struct tfm_crypto_pack_iovec iov = {
944 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100945 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100946 .alg = alg,
947 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000948 };
Antonio de Angelis4743e672019-04-11 11:38:48 +0100949
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000950 size_t idx = 0;
951 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100952 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000953 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +0100954 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000955 };
956 psa_outvec out_vec[] = {
957 {.base = ciphertext, .len = ciphertext_size},
958 };
Antonio de Angelis3a480992018-11-07 11:53:28 +0000959
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000960 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
961 return PSA_ERROR_INVALID_ARGUMENT;
962 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000963
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000964 if (nonce != NULL) {
965 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100966 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000967 }
968 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000969
Antonio de Angelis4743e672019-04-11 11:38:48 +0100970#ifdef TFM_PSA_API
971 PSA_CONNECT(TFM_CRYPTO);
972#endif
973
974#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100975 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100976 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100977 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100978 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100979 status = psa_call(ipc_handle, in_vec, in_len,
980 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +0100981#else
982 status = API_DISPATCH(tfm_crypto_aead_encrypt,
983 TFM_CRYPTO_AEAD_ENCRYPT);
984#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000985
986 *ciphertext_length = out_vec[0].len;
987
Antonio de Angelis4743e672019-04-11 11:38:48 +0100988#ifdef TFM_PSA_API
989 PSA_CLOSE();
990#endif
991
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000992 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +0000993}
994
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100995psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +0000996 psa_algorithm_t alg,
997 const uint8_t *nonce,
998 size_t nonce_length,
999 const uint8_t *additional_data,
1000 size_t additional_data_length,
1001 const uint8_t *ciphertext,
1002 size_t ciphertext_length,
1003 uint8_t *plaintext,
1004 size_t plaintext_size,
1005 size_t *plaintext_length)
1006{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001007 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001008 struct tfm_crypto_pack_iovec iov = {
1009 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SFID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001010 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001011 .alg = alg,
1012 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001013 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001014
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001015 size_t idx = 0;
1016 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001017 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001018 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001019 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001020 };
1021 psa_outvec out_vec[] = {
1022 {.base = plaintext, .len = plaintext_size},
1023 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001024
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001025 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1026 return PSA_ERROR_INVALID_ARGUMENT;
1027 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001028
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001029 if (nonce != NULL) {
1030 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001031 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001032 }
1033 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001034
Antonio de Angelis4743e672019-04-11 11:38:48 +01001035#ifdef TFM_PSA_API
1036 PSA_CONNECT(TFM_CRYPTO);
1037#endif
1038
1039#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001040 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001041 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001042 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001043 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001044 status = psa_call(ipc_handle, in_vec, in_len,
1045 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001046#else
1047 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1048 TFM_CRYPTO_AEAD_DECRYPT);
1049#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001050
1051 *plaintext_length = out_vec[0].len;
1052
Antonio de Angelis4743e672019-04-11 11:38:48 +01001053#ifdef TFM_PSA_API
1054 PSA_CLOSE();
1055#endif
1056
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001057 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001058}