blob: 19dc6f4bdbd28af7b9cd96068bbccd729dea43b3 [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
22#define PSA_CONNECT(service) \
23 psa_handle_t handle; \
24 handle = psa_connect(service##_SID, service##_MIN_VER); \
25 if (!PSA_IS_HANDLE_VALID(handle)) { \
26 return PSA_ERROR_UNKNOWN_ERROR; \
27 } \
28
29#define PSA_CLOSE() psa_close(handle)
30
31#define API_DISPATCH(sfn_name, sfn_id) \
32 psa_call(handle, /*PSA_IPC_CALL,*/ \
33 in_vec, ARRAY_SIZE(in_vec), \
34 out_vec, ARRAY_SIZE(out_vec))
35
36#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
37 psa_call(handle, /*PSA_IPC_CALL,*/ \
38 in_vec, ARRAY_SIZE(in_vec), \
39 (psa_outvec *)NULL, 0)
40#else
41#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), \
44 (uint32_t)out_vec, ARRAY_SIZE(out_vec))
45
46#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
60psa_status_t psa_import_key(psa_key_slot_t key,
61 psa_key_type_t type,
62 const uint8_t *data,
63 size_t data_length)
64{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000065 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010066 struct tfm_crypto_pack_iovec iov = {
67 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SFID,
68 .key = key,
69 .type = type,
70 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000071 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +010072 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000073 {.base = data, .len = data_length}
74 };
Antonio de Angelis8908f472018-08-31 15:44:25 +010075
Antonio de Angelis4743e672019-04-11 11:38:48 +010076#ifdef TFM_PSA_API
77 PSA_CONNECT(TFM_CRYPTO);
78#endif
79
80 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_import_key,
81 TFM_CRYPTO_IMPORT_KEY);
82#ifdef TFM_PSA_API
83 PSA_CLOSE();
84#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010085
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000086 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +010087}
88
89psa_status_t psa_destroy_key(psa_key_slot_t key)
90{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 struct tfm_crypto_pack_iovec iov = {
93 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SFID,
94 .key = key,
95 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000096 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +010097 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000098 };
Antonio de Angelis8908f472018-08-31 15:44:25 +010099
Antonio de Angelis4743e672019-04-11 11:38:48 +0100100#ifdef TFM_PSA_API
101 PSA_CONNECT(TFM_CRYPTO);
102#endif
103
104 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
105 TFM_CRYPTO_DESTROY_KEY);
106#ifdef TFM_PSA_API
107 PSA_CLOSE();
108#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100109
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000110 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100111}
112
113psa_status_t psa_get_key_information(psa_key_slot_t key,
114 psa_key_type_t *type,
115 size_t *bits)
116{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000117 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100118 struct tfm_crypto_pack_iovec iov = {
119 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SFID,
120 .key = key,
121 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100123 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000124 };
125 psa_outvec out_vec[] = {
126 {.base = type, .len = sizeof(psa_key_type_t)},
127 {.base = bits, .len = sizeof(size_t)}
128 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100129
Antonio de Angelis4743e672019-04-11 11:38:48 +0100130#ifdef TFM_PSA_API
131 PSA_CONNECT(TFM_CRYPTO);
132#endif
133
134 status = API_DISPATCH(tfm_crypto_get_key_information,
135 TFM_CRYPTO_GET_KEY_INFORMATION);
136#ifdef TFM_PSA_API
137 PSA_CLOSE();
138#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100139
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000140 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100141}
142
143psa_status_t psa_export_key(psa_key_slot_t key,
144 uint8_t *data,
145 size_t data_size,
146 size_t *data_length)
147{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000148 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100149 struct tfm_crypto_pack_iovec iov = {
150 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SFID,
151 .key = key,
152 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000153 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100154 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000155 };
156 psa_outvec out_vec[] = {
157 {.base = data, .len = data_size}
158 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100159
Antonio de Angelis4743e672019-04-11 11:38:48 +0100160#ifdef TFM_PSA_API
161 PSA_CONNECT(TFM_CRYPTO);
162#endif
163
164 status = API_DISPATCH(tfm_crypto_export_key,
165 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100166
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000167 *data_length = out_vec[0].len;
168
Antonio de Angelis4743e672019-04-11 11:38:48 +0100169#ifdef TFM_PSA_API
170 PSA_CLOSE();
171#endif
172
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000173 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174}
175
176psa_status_t psa_export_public_key(psa_key_slot_t key,
177 uint8_t *data,
178 size_t data_size,
179 size_t *data_length)
180{
Hugues de Valon8b442442019-02-19 14:30:52 +0000181 (void)key;
182 (void)data;
183 (void)data_size;
184 (void)data_length;
185
Antonio de Angelis8908f472018-08-31 15:44:25 +0100186 /* TODO: This API is not supported yet */
187 return PSA_ERROR_NOT_SUPPORTED;
188}
189
Jamie Foxefd82732018-11-26 10:34:32 +0000190void psa_key_policy_init(psa_key_policy_t *policy)
191{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100193 struct tfm_crypto_pack_iovec iov = {
194 .sfn_id = TFM_CRYPTO_KEY_POLICY_INIT_SFID,
195 };
196 psa_invec in_vec[] = {
197 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
198 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000199 psa_outvec out_vec[] = {
200 {.base = policy, .len = sizeof(psa_key_policy_t)},
201 };
202
Antonio de Angelis4743e672019-04-11 11:38:48 +0100203#ifdef TFM_PSA_API
204 psa_handle_t handle;
205 handle = psa_connect(TFM_CRYPTO_SID,
206 TFM_CRYPTO_MIN_VER);
207 if (!PSA_IS_HANDLE_VALID(handle)) {
208 return;
209 }
210#endif
211
Jamie Foxefd82732018-11-26 10:34:32 +0000212 /* PSA API returns void so just ignore error value returned */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100213 status = API_DISPATCH(tfm_crypto_key_policy_init,
214 TFM_CRYPTO_KEY_POLICY_INIT);
215#ifdef TFM_PSA_API
216 PSA_CLOSE();
217#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000218}
219
220void psa_key_policy_set_usage(psa_key_policy_t *policy,
221 psa_key_usage_t usage,
222 psa_algorithm_t alg)
223{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100225 struct tfm_crypto_pack_iovec iov = {
226 .sfn_id = TFM_CRYPTO_KEY_POLICY_SET_USAGE_SFID,
227 .usage = usage,
228 .alg = alg,
229 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000230 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100231 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000232 };
233 psa_outvec out_vec[] = {
234 {.base = policy, .len = sizeof(psa_key_policy_t)},
235 };
236
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237#ifdef TFM_PSA_API
238 psa_handle_t handle;
239 handle = psa_connect(TFM_CRYPTO_SID,
240 TFM_CRYPTO_MIN_VER);
241 if (!PSA_IS_HANDLE_VALID(handle)) {
242 return;
243 }
244#endif
245
Jamie Foxefd82732018-11-26 10:34:32 +0000246 /* PSA API returns void so just ignore error value returned */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100247 status = API_DISPATCH(tfm_crypto_key_policy_set_usage,
248 TFM_CRYPTO_KEY_POLICY_SET_USAGE);
249#ifdef TFM_PSA_API
250 PSA_CLOSE();
251#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000252}
253
254psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
255{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000256 psa_status_t status;
Jamie Foxefd82732018-11-26 10:34:32 +0000257 psa_key_usage_t usage;
258
Antonio de Angelis4743e672019-04-11 11:38:48 +0100259 struct tfm_crypto_pack_iovec iov = {
260 .sfn_id = TFM_CRYPTO_KEY_POLICY_GET_USAGE_SFID,
261 };
Jamie Foxefd82732018-11-26 10:34:32 +0000262
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000263 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100264 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000265 {.base = policy, .len = sizeof(psa_key_policy_t)},
266 };
267 psa_outvec out_vec[] = {
268 {.base = &usage, .len = sizeof(psa_key_usage_t)},
269 };
270
Antonio de Angelis4743e672019-04-11 11:38:48 +0100271 /* Initialise to a sensible default to avoid returning an uninitialised
272 * value in case the secure function fails.
273 */
274 usage = 0;
275
276#ifdef TFM_PSA_API
277 PSA_CONNECT(TFM_CRYPTO);
278#endif
279
Jamie Foxefd82732018-11-26 10:34:32 +0000280 /* The PSA API does not return an error, so ignore any error from TF-M */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100281 status = API_DISPATCH(tfm_crypto_key_policy_get_usage,
282 TFM_CRYPTO_KEY_POLICY_GET_USAGE);
283#ifdef TFM_PSA_API
284 PSA_CLOSE();
285#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000286
287 return usage;
288}
289
290psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
291{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000292 psa_status_t status;
Jamie Foxefd82732018-11-26 10:34:32 +0000293 psa_algorithm_t alg;
294
Antonio de Angelis4743e672019-04-11 11:38:48 +0100295 struct tfm_crypto_pack_iovec iov = {
296 .sfn_id = TFM_CRYPTO_KEY_POLICY_GET_ALGORITHM_SFID,
297 };
Jamie Foxefd82732018-11-26 10:34:32 +0000298
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000299 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100300 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000301 {.base = policy, .len = sizeof(psa_key_policy_t)},
302 };
303 psa_outvec out_vec[] = {
304 {.base = &alg, .len = sizeof(psa_algorithm_t)},
305 };
306
Antonio de Angelis4743e672019-04-11 11:38:48 +0100307 /* Initialise to a sensible default to avoid returning an uninitialised
308 * value in case the secure function fails.
309 */
310 alg = 0;
311
312#ifdef TFM_PSA_API
313 PSA_CONNECT(TFM_CRYPTO);
314#endif
315
Jamie Foxefd82732018-11-26 10:34:32 +0000316 /* The PSA API does not return an error, so ignore any error from TF-M */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100317 status = API_DISPATCH(tfm_crypto_key_policy_get_algorithm,
318 TFM_CRYPTO_KEY_POLICY_GET_ALGORITHM);
319#ifdef TFM_PSA_API
320 PSA_CLOSE();
321#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000322
323 return alg;
324}
325
326psa_status_t psa_set_key_policy(psa_key_slot_t key,
327 const psa_key_policy_t *policy)
328{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000329 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100330 struct tfm_crypto_pack_iovec iov = {
331 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SFID,
332 .key = key,
333 };
334
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000335 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100336 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000337 {.base = policy, .len = sizeof(psa_key_policy_t)},
338 };
Jamie Foxefd82732018-11-26 10:34:32 +0000339
Antonio de Angelis4743e672019-04-11 11:38:48 +0100340#ifdef TFM_PSA_API
341 PSA_CONNECT(TFM_CRYPTO);
342#endif
343
344 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
345 TFM_CRYPTO_SET_KEY_POLICY);
346#ifdef TFM_PSA_API
347 PSA_CLOSE();
348#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000349
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000350 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000351}
352
353psa_status_t psa_get_key_policy(psa_key_slot_t key,
354 psa_key_policy_t *policy)
355{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000356 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100357 struct tfm_crypto_pack_iovec iov = {
358 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SFID,
359 .key = key,
360 };
361
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000362 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100363 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000364 };
365 psa_outvec out_vec[] = {
366 {.base = policy, .len = sizeof(psa_key_policy_t)},
367 };
Jamie Foxefd82732018-11-26 10:34:32 +0000368
Antonio de Angelis4743e672019-04-11 11:38:48 +0100369#ifdef TFM_PSA_API
370 PSA_CONNECT(TFM_CRYPTO);
371#endif
372
373 status = API_DISPATCH(tfm_crypto_get_key_policy,
374 TFM_CRYPTO_GET_KEY_POLICY);
375#ifdef TFM_PSA_API
376 PSA_CLOSE();
377#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000378
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000379 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000380}
381
382psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
383 psa_key_lifetime_t lifetime)
384{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000385 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100386 struct tfm_crypto_pack_iovec iov = {
387 .sfn_id = TFM_CRYPTO_SET_KEY_LIFETIME_SFID,
388 .key = key,
389 .lifetime = lifetime,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000390 };
Jamie Foxefd82732018-11-26 10:34:32 +0000391
Antonio de Angelis4743e672019-04-11 11:38:48 +0100392 psa_invec in_vec[] = {
393 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
394 };
395
396#ifdef TFM_PSA_API
397 PSA_CONNECT(TFM_CRYPTO);
398#endif
399
400 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_lifetime,
401 TFM_CRYPTO_SET_KEY_LIFETIME);
402#ifdef TFM_PSA_API
403 PSA_CLOSE();
404#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000405
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000406 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000407}
408
409psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
410 psa_key_lifetime_t *lifetime)
411{
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_GET_KEY_LIFETIME_SFID,
415 .key = key,
416 };
417
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000418 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100419 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000420 };
421 psa_outvec out_vec[] = {
422 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
423 };
Jamie Foxefd82732018-11-26 10:34:32 +0000424
Antonio de Angelis4743e672019-04-11 11:38:48 +0100425#ifdef TFM_PSA_API
426 PSA_CONNECT(TFM_CRYPTO);
427#endif
428
429 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
430 TFM_CRYPTO_GET_KEY_LIFETIME);
431#ifdef TFM_PSA_API
432 PSA_CLOSE();
433#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000434
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000435 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000436}
437
Antonio de Angelis377a1552018-11-22 17:02:40 +0000438psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
439 const unsigned char *iv,
440 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100441{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000442 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100443 struct tfm_crypto_pack_iovec iov = {
444 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SFID,
445 .handle = operation->handle,
446 };
447
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000448 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100449 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000450 {.base = iv, .len = iv_length},
451 };
452 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100453 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000454 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100455
Antonio de Angelis4743e672019-04-11 11:38:48 +0100456#ifdef TFM_PSA_API
457 PSA_CONNECT(TFM_CRYPTO);
458#endif
459
460 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
461 TFM_CRYPTO_CIPHER_SET_IV);
462#ifdef TFM_PSA_API
463 PSA_CLOSE();
464#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100465
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000466 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100467}
468
Antonio de Angelis377a1552018-11-22 17:02:40 +0000469psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
470 psa_key_slot_t key,
471 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100472{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000473 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100474 struct tfm_crypto_pack_iovec iov = {
475 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SFID,
476 .key = key,
477 .alg = alg,
478 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000479 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100480
Antonio de Angelis4743e672019-04-11 11:38:48 +0100481 psa_invec in_vec[] = {
482 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
483 };
484 psa_outvec out_vec[] = {
485 {.base = &(operation->handle), .len = sizeof(uint32_t)},
486 };
487
488#ifdef TFM_PSA_API
489 PSA_CONNECT(TFM_CRYPTO);
490#endif
491
492 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
493 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
494#ifdef TFM_PSA_API
495 PSA_CLOSE();
496#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100497
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000498 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100499}
500
Antonio de Angelis377a1552018-11-22 17:02:40 +0000501psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
502 psa_key_slot_t key,
503 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100504{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000505 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100506 struct tfm_crypto_pack_iovec iov = {
507 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SFID,
508 .key = key,
509 .alg = alg,
510 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000511 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100512
Antonio de Angelis4743e672019-04-11 11:38:48 +0100513 psa_invec in_vec[] = {
514 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
515 };
516 psa_outvec out_vec[] = {
517 {.base = &(operation->handle), .len = sizeof(uint32_t)},
518 };
519
520#ifdef TFM_PSA_API
521 PSA_CONNECT(TFM_CRYPTO);
522#endif
523
524 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
525 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
526#ifdef TFM_PSA_API
527 PSA_CLOSE();
528#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100529
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000530 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100531}
532
533psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
534 const uint8_t *input,
535 size_t input_length,
536 unsigned char *output,
537 size_t output_size,
538 size_t *output_length)
539{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000540 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100541 struct tfm_crypto_pack_iovec iov = {
542 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SFID,
543 .handle = operation->handle,
544 };
545
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000546 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100547 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000548 {.base = input, .len = input_length},
549 };
550 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100551 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000552 {.base = output, .len = output_size}
553 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100554
Antonio de Angelis4743e672019-04-11 11:38:48 +0100555#ifdef TFM_PSA_API
556 PSA_CONNECT(TFM_CRYPTO);
557#endif
558
559 status = API_DISPATCH(tfm_crypto_cipher_update,
560 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100561
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000562 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100563
Antonio de Angelis4743e672019-04-11 11:38:48 +0100564#ifdef TFM_PSA_API
565 PSA_CLOSE();
566#endif
567
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000568 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100569}
570
571psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
572{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000573 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100574 struct tfm_crypto_pack_iovec iov = {
575 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SFID,
576 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000577 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100578
Antonio de Angelis4743e672019-04-11 11:38:48 +0100579 psa_invec in_vec[] = {
580 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
581 };
582 psa_outvec out_vec[] = {
583 {.base = &(operation->handle), .len = sizeof(uint32_t)},
584 };
585
586#ifdef TFM_PSA_API
587 PSA_CONNECT(TFM_CRYPTO);
588#endif
589
590 status = API_DISPATCH(tfm_crypto_cipher_abort,
591 TFM_CRYPTO_CIPHER_ABORT);
592#ifdef TFM_PSA_API
593 PSA_CLOSE();
594#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100595
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000596 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100597}
598
599psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
600 uint8_t *output,
601 size_t output_size,
602 size_t *output_length)
603{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000604 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100605 struct tfm_crypto_pack_iovec iov = {
606 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SFID,
607 .handle = operation->handle,
608 };
609
610 psa_invec in_vec[] = {
611 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
612 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000613 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100614 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000615 {.base = output, .len = output_size},
616 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100617
Antonio de Angelis4743e672019-04-11 11:38:48 +0100618#ifdef TFM_PSA_API
619 PSA_CONNECT(TFM_CRYPTO);
620#endif
621
622 status = API_DISPATCH(tfm_crypto_cipher_finish,
623 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100624
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000625 *output_length = out_vec[1].len;
626
Antonio de Angelis4743e672019-04-11 11:38:48 +0100627#ifdef TFM_PSA_API
628 PSA_CLOSE();
629#endif
630
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000631 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100632}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100633
Antonio de Angelis377a1552018-11-22 17:02:40 +0000634psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100635 psa_algorithm_t alg)
636{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000637 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100638 struct tfm_crypto_pack_iovec iov = {
639 .sfn_id = TFM_CRYPTO_HASH_SETUP_SFID,
640 .alg = alg,
641 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000642 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100643
Antonio de Angelis4743e672019-04-11 11:38:48 +0100644 psa_invec in_vec[] = {
645 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
646 };
647 psa_outvec out_vec[] = {
648 {.base = &(operation->handle), .len = sizeof(uint32_t)},
649 };
650
651#ifdef TFM_PSA_API
652 PSA_CONNECT(TFM_CRYPTO);
653#endif
654
655 status = API_DISPATCH(tfm_crypto_hash_setup,
656 TFM_CRYPTO_HASH_SETUP);
657
658#ifdef TFM_PSA_API
659 PSA_CLOSE();
660#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100661
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000662 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100663}
664
665psa_status_t psa_hash_update(psa_hash_operation_t *operation,
666 const uint8_t *input,
667 size_t input_length)
668{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000669 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100670 struct tfm_crypto_pack_iovec iov = {
671 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SFID,
672 .handle = operation->handle,
673 };
674
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000675 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100676 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000677 {.base = input, .len = input_length},
678 };
679 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100680 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000681 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100682
Antonio de Angelis4743e672019-04-11 11:38:48 +0100683#ifdef TFM_PSA_API
684 PSA_CONNECT(TFM_CRYPTO);
685#endif
686
687 status = API_DISPATCH(tfm_crypto_hash_update,
688 TFM_CRYPTO_HASH_UPDATE);
689
690#ifdef TFM_PSA_API
691 PSA_CLOSE();
692#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100693
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000694 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100695}
696
697psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
698 uint8_t *hash,
699 size_t hash_size,
700 size_t *hash_length)
701{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000702 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100703 struct tfm_crypto_pack_iovec iov = {
704 .sfn_id = TFM_CRYPTO_HASH_FINISH_SFID,
705 .handle = operation->handle,
706 };
707
708 psa_invec in_vec[] = {
709 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
710 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000711 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100712 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000713 {.base = hash, .len = hash_size},
714 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100715
Antonio de Angelis4743e672019-04-11 11:38:48 +0100716#ifdef TFM_PSA_API
717 PSA_CONNECT(TFM_CRYPTO);
718#endif
719
720 status = API_DISPATCH(tfm_crypto_hash_finish,
721 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100722
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000723 *hash_length = out_vec[1].len;
724
Antonio de Angelis4743e672019-04-11 11:38:48 +0100725#ifdef TFM_PSA_API
726 PSA_CLOSE();
727#endif
728
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000729 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100730}
731
732psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
733 const uint8_t *hash,
734 size_t hash_length)
735{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000736 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100737 struct tfm_crypto_pack_iovec iov = {
738 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SFID,
739 .handle = operation->handle,
740 };
741
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000742 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100743 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000744 {.base = hash, .len = hash_length},
745 };
746 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100747 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000748 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100749
Antonio de Angelis4743e672019-04-11 11:38:48 +0100750#ifdef TFM_PSA_API
751 PSA_CONNECT(TFM_CRYPTO);
752#endif
753
754 status = API_DISPATCH(tfm_crypto_hash_verify,
755 TFM_CRYPTO_HASH_VERIFY);
756#ifdef TFM_PSA_API
757 PSA_CLOSE();
758#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100759
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000760 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100761}
762
763psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
764{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000765 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100766 struct tfm_crypto_pack_iovec iov = {
767 .sfn_id = TFM_CRYPTO_HASH_ABORT_SFID,
768 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000769 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100770
Antonio de Angelis4743e672019-04-11 11:38:48 +0100771 psa_invec in_vec[] = {
772 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
773 };
774 psa_outvec out_vec[] = {
775 {.base = &(operation->handle), .len = sizeof(uint32_t)},
776 };
777
778#ifdef TFM_PSA_API
779 PSA_CONNECT(TFM_CRYPTO);
780#endif
781
782 status = API_DISPATCH(tfm_crypto_hash_abort,
783 TFM_CRYPTO_HASH_ABORT);
784#ifdef TFM_PSA_API
785 PSA_CLOSE();
786#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100787
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000788 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100789}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100790
791psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
792 psa_key_slot_t key,
793 psa_algorithm_t alg)
794{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000795 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100796 struct tfm_crypto_pack_iovec iov = {
797 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SFID,
798 .key = key,
799 .alg = alg,
800 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000801 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100802
Antonio de Angelis4743e672019-04-11 11:38:48 +0100803 psa_invec in_vec[] = {
804 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
805 };
806 psa_outvec out_vec[] = {
807 {.base = &(operation->handle), .len = sizeof(uint32_t)},
808 };
809
810#ifdef TFM_PSA_API
811 PSA_CONNECT(TFM_CRYPTO);
812#endif
813
814 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
815 TFM_CRYPTO_MAC_SIGN_SETUP);
816#ifdef TFM_PSA_API
817 PSA_CLOSE();
818#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100819
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000820 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100821}
822
823psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
824 psa_key_slot_t key,
825 psa_algorithm_t alg)
826{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000827 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100828 struct tfm_crypto_pack_iovec iov = {
829 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SFID,
830 .key = key,
831 .alg = alg,
832 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000833 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100834
Antonio de Angelis4743e672019-04-11 11:38:48 +0100835 psa_invec in_vec[] = {
836 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
837 };
838 psa_outvec out_vec[] = {
839 {.base = &(operation->handle), .len = sizeof(uint32_t)},
840 };
841
842#ifdef TFM_PSA_API
843 PSA_CONNECT(TFM_CRYPTO);
844#endif
845
846 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
847 TFM_CRYPTO_MAC_VERIFY_SETUP);
848#ifdef TFM_PSA_API
849 PSA_CLOSE();
850#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100851
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000852 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100853}
854
855psa_status_t psa_mac_update(psa_mac_operation_t *operation,
856 const uint8_t *input,
857 size_t input_length)
858{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000859 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100860 struct tfm_crypto_pack_iovec iov = {
861 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SFID,
862 .handle = operation->handle,
863 };
864
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000865 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100866 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000867 {.base = input, .len = input_length},
868 };
869 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100870 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000871 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100872
Antonio de Angelis4743e672019-04-11 11:38:48 +0100873#ifdef TFM_PSA_API
874 PSA_CONNECT(TFM_CRYPTO);
875#endif
876
877 status = API_DISPATCH(tfm_crypto_mac_update,
878 TFM_CRYPTO_MAC_UPDATE);
879#ifdef TFM_PSA_API
880 PSA_CLOSE();
881#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100882
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000883 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100884}
885
886psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
887 uint8_t *mac,
888 size_t mac_size,
889 size_t *mac_length)
890{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000891 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100892 struct tfm_crypto_pack_iovec iov = {
893 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SFID,
894 .handle = operation->handle,
895 };
896
897 psa_invec in_vec[] = {
898 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
899 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000900 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100901 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000902 {.base = mac, .len = mac_size},
903 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100904
Antonio de Angelis4743e672019-04-11 11:38:48 +0100905#ifdef TFM_PSA_API
906 PSA_CONNECT(TFM_CRYPTO);
907#endif
908
909 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
910 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100911
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000912 *mac_length = out_vec[1].len;
913
Antonio de Angelis4743e672019-04-11 11:38:48 +0100914#ifdef TFM_PSA_API
915 PSA_CLOSE();
916#endif
917
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000918 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100919}
920
921psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
922 const uint8_t *mac,
923 size_t mac_length)
924{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000925 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100926 struct tfm_crypto_pack_iovec iov = {
927 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SFID,
928 .handle = operation->handle,
929 };
930
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000931 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100932 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000933 {.base = mac, .len = mac_length},
934 };
935 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100936 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000937 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100938
Antonio de Angelis4743e672019-04-11 11:38:48 +0100939#ifdef TFM_PSA_API
940 PSA_CONNECT(TFM_CRYPTO);
941#endif
942
943 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
944 TFM_CRYPTO_MAC_VERIFY_FINISH);
945
946#ifdef TFM_PSA_API
947 PSA_CLOSE();
948#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100949
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000950 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100951}
952
953psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
954{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000955 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100956 struct tfm_crypto_pack_iovec iov = {
957 .sfn_id = TFM_CRYPTO_MAC_ABORT_SFID,
958 .handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000959 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100960
Antonio de Angelis4743e672019-04-11 11:38:48 +0100961 psa_invec in_vec[] = {
962 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
963 };
964 psa_outvec out_vec[] = {
965 {.base = &(operation->handle), .len = sizeof(uint32_t)},
966 };
967
968#ifdef TFM_PSA_API
969 PSA_CONNECT(TFM_CRYPTO);
970#endif
971
972 status = API_DISPATCH(tfm_crypto_mac_abort,
973 TFM_CRYPTO_MAC_ABORT);
974#ifdef TFM_PSA_API
975 PSA_CLOSE();
976#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100977
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000978 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100979}
Antonio de Angelis3a480992018-11-07 11:53:28 +0000980
981psa_status_t psa_aead_encrypt(psa_key_slot_t key,
982 psa_algorithm_t alg,
983 const uint8_t *nonce,
984 size_t nonce_length,
985 const uint8_t *additional_data,
986 size_t additional_data_length,
987 const uint8_t *plaintext,
988 size_t plaintext_length,
989 uint8_t *ciphertext,
990 size_t ciphertext_size,
991 size_t *ciphertext_length)
992{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000993 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100994 struct tfm_crypto_pack_iovec iov = {
995 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SFID,
996 .key = key,
997 .alg = alg,
998 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000999 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001000
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001001 size_t idx = 0;
1002 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001003 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001004 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001005 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001006 };
1007 psa_outvec out_vec[] = {
1008 {.base = ciphertext, .len = ciphertext_size},
1009 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001010
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001011 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1012 return PSA_ERROR_INVALID_ARGUMENT;
1013 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001014
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001015 if (nonce != NULL) {
1016 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001017 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001018 }
1019 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001020
Antonio de Angelis4743e672019-04-11 11:38:48 +01001021#ifdef TFM_PSA_API
1022 PSA_CONNECT(TFM_CRYPTO);
1023#endif
1024
1025#ifdef TFM_PSA_API
1026 size_t in_len = sizeof(in_vec)/sizeof(in_vec[0]);
1027 if (additional_data == NULL) {
1028 in_len--;
1029 }
1030 status = psa_call(handle, in_vec, in_len,
1031 out_vec, sizeof(out_vec)/sizeof(out_vec[0]));
1032#else
1033 status = API_DISPATCH(tfm_crypto_aead_encrypt,
1034 TFM_CRYPTO_AEAD_ENCRYPT);
1035#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001036
1037 *ciphertext_length = out_vec[0].len;
1038
Antonio de Angelis4743e672019-04-11 11:38:48 +01001039#ifdef TFM_PSA_API
1040 PSA_CLOSE();
1041#endif
1042
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001043 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001044}
1045
1046psa_status_t psa_aead_decrypt(psa_key_slot_t key,
1047 psa_algorithm_t alg,
1048 const uint8_t *nonce,
1049 size_t nonce_length,
1050 const uint8_t *additional_data,
1051 size_t additional_data_length,
1052 const uint8_t *ciphertext,
1053 size_t ciphertext_length,
1054 uint8_t *plaintext,
1055 size_t plaintext_size,
1056 size_t *plaintext_length)
1057{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001058 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001059 struct tfm_crypto_pack_iovec iov = {
1060 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SFID,
1061 .key = key,
1062 .alg = alg,
1063 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001064 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001065
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001066 size_t idx = 0;
1067 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001068 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001069 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001070 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001071 };
1072 psa_outvec out_vec[] = {
1073 {.base = plaintext, .len = plaintext_size},
1074 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001075
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001076 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1077 return PSA_ERROR_INVALID_ARGUMENT;
1078 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001079
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001080 if (nonce != NULL) {
1081 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001082 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001083 }
1084 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001085
Antonio de Angelis4743e672019-04-11 11:38:48 +01001086#ifdef TFM_PSA_API
1087 PSA_CONNECT(TFM_CRYPTO);
1088#endif
1089
1090#ifdef TFM_PSA_API
1091 size_t in_len = sizeof(in_vec)/sizeof(in_vec[0]);
1092 if (additional_data == NULL) {
1093 in_len--;
1094 }
1095 status = psa_call(handle, in_vec, in_len,
1096 out_vec, sizeof(out_vec)/sizeof(out_vec[0]));
1097#else
1098 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1099 TFM_CRYPTO_AEAD_DECRYPT);
1100#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001101
1102 *plaintext_length = out_vec[0].len;
1103
Antonio de Angelis4743e672019-04-11 11:38:48 +01001104#ifdef TFM_PSA_API
1105 PSA_CLOSE();
1106#endif
1107
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001108 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001109}