blob: e18d61194c4756996ca1e9977dfd28085cb4b354 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis377a1552018-11-22 17:02:40 +00002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00008#include "tfm_veneers.h"
9#include "tfm_crypto_defs.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000010#include "psa/crypto.h"
Antonio de Angelis05b24192019-07-04 15:28:46 +010011#include "tfm_ns_interface.h"
Edison Aicc4c6162019-06-21 13:52:49 +080012#ifdef TFM_PSA_API
13#include "psa_manifest/sid.h"
14#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000015
Antonio de Angelis4743e672019-04-11 11:38:48 +010016#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000017
Antonio de Angelis4743e672019-04-11 11:38:48 +010018#ifdef TFM_PSA_API
Jamie Foxcc31d402019-01-28 17:13:52 +000019#include "psa/client.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000020
Jamie Fox0e54ebc2019-04-09 14:21:04 +010021#define PSA_CONNECT(service) \
22 psa_handle_t ipc_handle; \
Edison Aicc4c6162019-06-21 13:52:49 +080023 ipc_handle = psa_connect(service##_SID, service##_VERSION); \
Summer Qinb207a152019-07-03 16:36:49 +080024 if (!PSA_HANDLE_IS_VALID(ipc_handle)) { \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010025 return PSA_ERROR_GENERIC_ERROR; \
26 } \
Antonio de Angelis4743e672019-04-11 11:38:48 +010027
Jamie Fox0e54ebc2019-04-09 14:21:04 +010028#define PSA_CLOSE() psa_close(ipc_handle)
Antonio de Angelis4743e672019-04-11 11:38:48 +010029
Jamie Fox0e54ebc2019-04-09 14:21:04 +010030#define API_DISPATCH(sfn_name, sfn_id) \
Summer Qin4b1d03b2019-07-02 14:56:08 +080031 psa_call(ipc_handle, PSA_IPC_CALL, \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010033 out_vec, ARRAY_SIZE(out_vec))
34
Jamie Fox0e54ebc2019-04-09 14:21:04 +010035#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
Summer Qin4b1d03b2019-07-02 14:56:08 +080036 psa_call(ipc_handle, PSA_IPC_CALL, \
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037 in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010038 (psa_outvec *)NULL, 0)
39#else
Antonio de Angelis05b24192019-07-04 15:28:46 +010040#define API_DISPATCH(sfn_name, sfn_id) \
41 tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
42 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010043 (uint32_t)out_vec, ARRAY_SIZE(out_vec))
44
Antonio de Angelis05b24192019-07-04 15:28:46 +010045#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
46 tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
47 (uint32_t)in_vec, ARRAY_SIZE(in_vec), \
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000048 (uint32_t)NULL, 0)
Antonio de Angelis4743e672019-04-11 11:38:48 +010049#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010050
51psa_status_t psa_crypto_init(void)
52{
53 /* Service init is performed during TFM boot up,
54 * so application level initialisation is empty
55 */
56 return PSA_SUCCESS;
57}
58
Jamie Fox0e54ebc2019-04-09 14:21:04 +010059psa_status_t psa_allocate_key(psa_key_handle_t *handle)
60{
61 psa_status_t status;
62 const struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080063 .sfn_id = TFM_CRYPTO_ALLOCATE_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064 };
65 psa_invec in_vec[] = {
66 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
67 };
68 psa_outvec out_vec[] = {
69 {.base = handle, .len = sizeof(psa_key_handle_t)},
70 };
71
72#ifdef TFM_PSA_API
73 PSA_CONNECT(TFM_CRYPTO);
74#endif
75
76 status = API_DISPATCH(tfm_crypto_allocate_key,
77 TFM_CRYPTO_ALLOCATE_KEY);
78#ifdef TFM_PSA_API
79 PSA_CLOSE();
80#endif
81
82 return status;
83}
84
85psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
86 psa_key_id_t id,
87 psa_key_handle_t *handle)
88{
89 (void)lifetime;
90 (void)id;
91 (void)handle;
92
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010093 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010094 return PSA_ERROR_NOT_SUPPORTED;
95}
96
97psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
98 psa_key_id_t id,
99 psa_key_handle_t *handle)
100{
101 (void)lifetime;
102 (void)id;
103 (void)handle;
104
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100105 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100106 return PSA_ERROR_NOT_SUPPORTED;
107}
108
109psa_status_t psa_close_key(psa_key_handle_t handle)
110{
111 (void)handle;
112
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100113 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100114 return PSA_ERROR_NOT_SUPPORTED;
115}
116
117psa_status_t psa_import_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100118 psa_key_type_t type,
119 const uint8_t *data,
120 size_t data_length)
121{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100123 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800124 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100125 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100126 .type = type,
127 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000128 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000130 {.base = data, .len = data_length}
131 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100132
Antonio de Angelis4743e672019-04-11 11:38:48 +0100133#ifdef TFM_PSA_API
134 PSA_CONNECT(TFM_CRYPTO);
135#endif
136
137 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_import_key,
138 TFM_CRYPTO_IMPORT_KEY);
139#ifdef TFM_PSA_API
140 PSA_CLOSE();
141#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100142
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000143 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100144}
145
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100146psa_status_t psa_destroy_key(psa_key_handle_t handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100147{
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 = {
Edison Ai080b2e22019-04-17 16:27:21 +0800150 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100151 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100152 };
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 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100156
Antonio de Angelis4743e672019-04-11 11:38:48 +0100157#ifdef TFM_PSA_API
158 PSA_CONNECT(TFM_CRYPTO);
159#endif
160
161 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
162 TFM_CRYPTO_DESTROY_KEY);
163#ifdef TFM_PSA_API
164 PSA_CLOSE();
165#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100166
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000167 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100168}
169
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100170psa_status_t psa_get_key_information(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100171 psa_key_type_t *type,
172 size_t *bits)
173{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000174 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100175 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800176 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100177 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100178 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000179 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100180 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000181 };
182 psa_outvec out_vec[] = {
183 {.base = type, .len = sizeof(psa_key_type_t)},
184 {.base = bits, .len = sizeof(size_t)}
185 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100186
Antonio de Angelis4743e672019-04-11 11:38:48 +0100187#ifdef TFM_PSA_API
188 PSA_CONNECT(TFM_CRYPTO);
189#endif
190
191 status = API_DISPATCH(tfm_crypto_get_key_information,
192 TFM_CRYPTO_GET_KEY_INFORMATION);
193#ifdef TFM_PSA_API
194 PSA_CLOSE();
195#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100196
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000197 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100198}
199
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100200psa_status_t psa_export_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100201 uint8_t *data,
202 size_t data_size,
203 size_t *data_length)
204{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000205 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100206 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800207 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100208 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100209 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000210 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100211 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000212 };
213 psa_outvec out_vec[] = {
214 {.base = data, .len = data_size}
215 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100216
Antonio de Angelis4743e672019-04-11 11:38:48 +0100217#ifdef TFM_PSA_API
218 PSA_CONNECT(TFM_CRYPTO);
219#endif
220
221 status = API_DISPATCH(tfm_crypto_export_key,
222 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100223
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224 *data_length = out_vec[0].len;
225
Antonio de Angelis4743e672019-04-11 11:38:48 +0100226#ifdef TFM_PSA_API
227 PSA_CLOSE();
228#endif
229
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000230 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100231}
232
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100233psa_status_t psa_export_public_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100234 uint8_t *data,
235 size_t data_size,
236 size_t *data_length)
237{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100238 psa_status_t status;
239 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800240 .sfn_id = TFM_CRYPTO_EXPORT_PUBLIC_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100241 .key_handle = handle,
242 };
Hugues de Valon8b442442019-02-19 14:30:52 +0000243
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100244 psa_invec in_vec[] = {
245 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
246 };
247 psa_outvec out_vec[] = {
248 {.base = data, .len = data_size}
249 };
250
251#ifdef TFM_PSA_API
252 PSA_CONNECT(TFM_CRYPTO);
253#endif
254
255 status = API_DISPATCH(tfm_crypto_export_public_key,
256 TFM_CRYPTO_EXPORT_PUBLIC_KEY);
257
258 *data_length = out_vec[0].len;
259
260#ifdef TFM_PSA_API
261 PSA_CLOSE();
262#endif
263
264 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100265}
266
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100267psa_status_t psa_copy_key(psa_key_handle_t source_handle,
268 psa_key_handle_t target_handle,
269 const psa_key_policy_t *constraint)
Jamie Foxefd82732018-11-26 10:34:32 +0000270{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100271 psa_status_t status;
272 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800273 .sfn_id = TFM_CRYPTO_COPY_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100274 .key_handle = source_handle,
275 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000276
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100277 psa_invec in_vec[] = {
278 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
279 {.base = &target_handle, .len = sizeof(psa_key_handle_t)},
280 {.base = constraint, .len = sizeof(psa_key_policy_t)},
281 };
282
283#ifdef TFM_PSA_API
284 PSA_CONNECT(TFM_CRYPTO);
285#endif
286
287 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_copy_key,
288 TFM_CRYPTO_COPY_KEY);
289#ifdef TFM_PSA_API
290 PSA_CLOSE();
291#endif
292
293 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000294}
295
296void psa_key_policy_set_usage(psa_key_policy_t *policy,
297 psa_key_usage_t usage,
298 psa_algorithm_t alg)
299{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100300 policy->usage = usage;
301 policy->alg = alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000302}
303
304psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
305{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100306 return policy->usage;
Jamie Foxefd82732018-11-26 10:34:32 +0000307}
308
309psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
310{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100311 return policy->alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000312}
313
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100314psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000315 const psa_key_policy_t *policy)
316{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000317 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100318 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800319 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100320 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100321 };
322
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000323 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100324 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000325 {.base = policy, .len = sizeof(psa_key_policy_t)},
326 };
Jamie Foxefd82732018-11-26 10:34:32 +0000327
Antonio de Angelis4743e672019-04-11 11:38:48 +0100328#ifdef TFM_PSA_API
329 PSA_CONNECT(TFM_CRYPTO);
330#endif
331
332 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
333 TFM_CRYPTO_SET_KEY_POLICY);
334#ifdef TFM_PSA_API
335 PSA_CLOSE();
336#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000337
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000338 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000339}
340
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100341psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000342 psa_key_policy_t *policy)
343{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000344 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100345 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800346 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100347 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100348 };
349
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000350 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100351 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000352 };
353 psa_outvec out_vec[] = {
354 {.base = policy, .len = sizeof(psa_key_policy_t)},
355 };
Jamie Foxefd82732018-11-26 10:34:32 +0000356
Antonio de Angelis4743e672019-04-11 11:38:48 +0100357#ifdef TFM_PSA_API
358 PSA_CONNECT(TFM_CRYPTO);
359#endif
360
361 status = API_DISPATCH(tfm_crypto_get_key_policy,
362 TFM_CRYPTO_GET_KEY_POLICY);
363#ifdef TFM_PSA_API
364 PSA_CLOSE();
365#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000366
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000367 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000368}
369
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100370psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000371 psa_key_lifetime_t *lifetime)
372{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000373 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100374 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800375 .sfn_id = TFM_CRYPTO_GET_KEY_LIFETIME_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100376 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100377 };
378
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000379 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100380 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000381 };
382 psa_outvec out_vec[] = {
383 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
384 };
Jamie Foxefd82732018-11-26 10:34:32 +0000385
Antonio de Angelis4743e672019-04-11 11:38:48 +0100386#ifdef TFM_PSA_API
387 PSA_CONNECT(TFM_CRYPTO);
388#endif
389
390 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
391 TFM_CRYPTO_GET_KEY_LIFETIME);
392#ifdef TFM_PSA_API
393 PSA_CLOSE();
394#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000395
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000396 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000397}
398
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100399psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
400 unsigned char *iv,
401 size_t iv_size,
402 size_t *iv_length)
403{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100404 psa_status_t status;
405 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800406 .sfn_id = TFM_CRYPTO_CIPHER_GENERATE_IV_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100407 .op_handle = operation->handle,
408 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100409
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100410 psa_invec in_vec[] = {
411 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
412 };
413 psa_outvec out_vec[] = {
414 {.base = &(operation->handle), .len = sizeof(uint32_t)},
415 {.base = iv, .len = iv_size},
416 };
417
418#ifdef TFM_PSA_API
419 PSA_CONNECT(TFM_CRYPTO);
420#endif
421
422 status = API_DISPATCH(tfm_crypto_cipher_generate_iv,
423 TFM_CRYPTO_CIPHER_GENERATE_IV);
424
425 *iv_length = out_vec[1].len;
426
427#ifdef TFM_PSA_API
428 PSA_CLOSE();
429#endif
430
431 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100432}
433
Antonio de Angelis377a1552018-11-22 17:02:40 +0000434psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
435 const unsigned char *iv,
436 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100437{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000438 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100439 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800440 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100441 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100442 };
443
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000444 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100445 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000446 {.base = iv, .len = iv_length},
447 };
448 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100449 {.base = &(operation->handle), .len = sizeof(uint32_t)},
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#ifdef TFM_PSA_API
453 PSA_CONNECT(TFM_CRYPTO);
454#endif
455
456 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
457 TFM_CRYPTO_CIPHER_SET_IV);
458#ifdef TFM_PSA_API
459 PSA_CLOSE();
460#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100461
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000462 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100463}
464
Antonio de Angelis377a1552018-11-22 17:02:40 +0000465psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100466 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000467 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100468{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000469 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100470 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800471 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100472 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100473 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100474 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000475 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100476
Antonio de Angelis4743e672019-04-11 11:38:48 +0100477 psa_invec in_vec[] = {
478 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
479 };
480 psa_outvec out_vec[] = {
481 {.base = &(operation->handle), .len = sizeof(uint32_t)},
482 };
483
484#ifdef TFM_PSA_API
485 PSA_CONNECT(TFM_CRYPTO);
486#endif
487
488 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
489 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
490#ifdef TFM_PSA_API
491 PSA_CLOSE();
492#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100493
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000494 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100495}
496
Antonio de Angelis377a1552018-11-22 17:02:40 +0000497psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100498 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000499 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100500{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000501 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100502 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800503 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100504 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100505 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100506 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000507 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100508
Antonio de Angelis4743e672019-04-11 11:38:48 +0100509 psa_invec in_vec[] = {
510 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
511 };
512 psa_outvec out_vec[] = {
513 {.base = &(operation->handle), .len = sizeof(uint32_t)},
514 };
515
516#ifdef TFM_PSA_API
517 PSA_CONNECT(TFM_CRYPTO);
518#endif
519
520 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
521 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
522#ifdef TFM_PSA_API
523 PSA_CLOSE();
524#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100525
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000526 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100527}
528
529psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
530 const uint8_t *input,
531 size_t input_length,
532 unsigned char *output,
533 size_t output_size,
534 size_t *output_length)
535{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000536 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100537 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800538 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100539 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100540 };
541
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000542 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100543 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000544 {.base = input, .len = input_length},
545 };
546 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100547 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000548 {.base = output, .len = output_size}
549 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100550
Antonio de Angelis4743e672019-04-11 11:38:48 +0100551#ifdef TFM_PSA_API
552 PSA_CONNECT(TFM_CRYPTO);
553#endif
554
555 status = API_DISPATCH(tfm_crypto_cipher_update,
556 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100557
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000558 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100559
Antonio de Angelis4743e672019-04-11 11:38:48 +0100560#ifdef TFM_PSA_API
561 PSA_CLOSE();
562#endif
563
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000564 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100565}
566
567psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
568{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000569 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100570 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800571 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100572 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000573 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100574
Antonio de Angelis4743e672019-04-11 11:38:48 +0100575 psa_invec in_vec[] = {
576 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
577 };
578 psa_outvec out_vec[] = {
579 {.base = &(operation->handle), .len = sizeof(uint32_t)},
580 };
581
582#ifdef TFM_PSA_API
583 PSA_CONNECT(TFM_CRYPTO);
584#endif
585
586 status = API_DISPATCH(tfm_crypto_cipher_abort,
587 TFM_CRYPTO_CIPHER_ABORT);
588#ifdef TFM_PSA_API
589 PSA_CLOSE();
590#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100591
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000592 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100593}
594
595psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
596 uint8_t *output,
597 size_t output_size,
598 size_t *output_length)
599{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000600 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100601 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800602 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100603 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100604 };
605
606 psa_invec in_vec[] = {
607 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
608 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000609 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100610 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000611 {.base = output, .len = output_size},
612 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100613
Antonio de Angelis4743e672019-04-11 11:38:48 +0100614#ifdef TFM_PSA_API
615 PSA_CONNECT(TFM_CRYPTO);
616#endif
617
618 status = API_DISPATCH(tfm_crypto_cipher_finish,
619 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100620
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000621 *output_length = out_vec[1].len;
622
Antonio de Angelis4743e672019-04-11 11:38:48 +0100623#ifdef TFM_PSA_API
624 PSA_CLOSE();
625#endif
626
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000627 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100628}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100629
Antonio de Angelis377a1552018-11-22 17:02:40 +0000630psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100631 psa_algorithm_t alg)
632{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000633 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100634 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800635 .sfn_id = TFM_CRYPTO_HASH_SETUP_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100636 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100637 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000638 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100639
Antonio de Angelis4743e672019-04-11 11:38:48 +0100640 psa_invec in_vec[] = {
641 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
642 };
643 psa_outvec out_vec[] = {
644 {.base = &(operation->handle), .len = sizeof(uint32_t)},
645 };
646
647#ifdef TFM_PSA_API
648 PSA_CONNECT(TFM_CRYPTO);
649#endif
650
651 status = API_DISPATCH(tfm_crypto_hash_setup,
652 TFM_CRYPTO_HASH_SETUP);
653
654#ifdef TFM_PSA_API
655 PSA_CLOSE();
656#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100657
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000658 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100659}
660
661psa_status_t psa_hash_update(psa_hash_operation_t *operation,
662 const uint8_t *input,
663 size_t input_length)
664{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000665 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100666 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800667 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100668 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100669 };
670
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000671 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100672 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000673 {.base = input, .len = input_length},
674 };
675 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100676 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000677 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100678
Antonio de Angelis4743e672019-04-11 11:38:48 +0100679#ifdef TFM_PSA_API
680 PSA_CONNECT(TFM_CRYPTO);
681#endif
682
683 status = API_DISPATCH(tfm_crypto_hash_update,
684 TFM_CRYPTO_HASH_UPDATE);
685
686#ifdef TFM_PSA_API
687 PSA_CLOSE();
688#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100689
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000690 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100691}
692
693psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
694 uint8_t *hash,
695 size_t hash_size,
696 size_t *hash_length)
697{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000698 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100699 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800700 .sfn_id = TFM_CRYPTO_HASH_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100701 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100702 };
703
704 psa_invec in_vec[] = {
705 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
706 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000707 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100708 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000709 {.base = hash, .len = hash_size},
710 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100711
Antonio de Angelis4743e672019-04-11 11:38:48 +0100712#ifdef TFM_PSA_API
713 PSA_CONNECT(TFM_CRYPTO);
714#endif
715
716 status = API_DISPATCH(tfm_crypto_hash_finish,
717 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100718
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000719 *hash_length = out_vec[1].len;
720
Antonio de Angelis4743e672019-04-11 11:38:48 +0100721#ifdef TFM_PSA_API
722 PSA_CLOSE();
723#endif
724
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000725 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100726}
727
728psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
729 const uint8_t *hash,
730 size_t hash_length)
731{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000732 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100733 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800734 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100735 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100736 };
737
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000738 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100739 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000740 {.base = hash, .len = hash_length},
741 };
742 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100743 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000744 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100745
Antonio de Angelis4743e672019-04-11 11:38:48 +0100746#ifdef TFM_PSA_API
747 PSA_CONNECT(TFM_CRYPTO);
748#endif
749
750 status = API_DISPATCH(tfm_crypto_hash_verify,
751 TFM_CRYPTO_HASH_VERIFY);
752#ifdef TFM_PSA_API
753 PSA_CLOSE();
754#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100755
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000756 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100757}
758
759psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
760{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000761 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100762 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800763 .sfn_id = TFM_CRYPTO_HASH_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100764 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000765 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100766
Antonio de Angelis4743e672019-04-11 11:38:48 +0100767 psa_invec in_vec[] = {
768 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
769 };
770 psa_outvec out_vec[] = {
771 {.base = &(operation->handle), .len = sizeof(uint32_t)},
772 };
773
774#ifdef TFM_PSA_API
775 PSA_CONNECT(TFM_CRYPTO);
776#endif
777
778 status = API_DISPATCH(tfm_crypto_hash_abort,
779 TFM_CRYPTO_HASH_ABORT);
780#ifdef TFM_PSA_API
781 PSA_CLOSE();
782#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100783
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000784 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100785}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100786
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100787psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
788 psa_hash_operation_t *target_operation)
789{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100790 psa_status_t status;
791 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800792 .sfn_id = TFM_CRYPTO_HASH_CLONE_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100793 .op_handle = source_operation->handle,
794 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100795
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100796 psa_invec in_vec[] = {
797 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
798 };
799 psa_outvec out_vec[] = {
800 {.base = target_operation, .len = sizeof(psa_hash_operation_t)},
801 };
802
803#ifdef TFM_PSA_API
804 PSA_CONNECT(TFM_CRYPTO);
805#endif
806
807 status = API_DISPATCH(tfm_crypto_hash_clone,
808 TFM_CRYPTO_HASH_CLONE);
809#ifdef TFM_PSA_API
810 PSA_CLOSE();
811#endif
812
813 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100814}
815
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100816psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100817 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100818 psa_algorithm_t alg)
819{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000820 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100821 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800822 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100823 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100824 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100825 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000826 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100827
Antonio de Angelis4743e672019-04-11 11:38:48 +0100828 psa_invec in_vec[] = {
829 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
830 };
831 psa_outvec out_vec[] = {
832 {.base = &(operation->handle), .len = sizeof(uint32_t)},
833 };
834
835#ifdef TFM_PSA_API
836 PSA_CONNECT(TFM_CRYPTO);
837#endif
838
839 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
840 TFM_CRYPTO_MAC_SIGN_SETUP);
841#ifdef TFM_PSA_API
842 PSA_CLOSE();
843#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100844
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000845 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100846}
847
848psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100849 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100850 psa_algorithm_t alg)
851{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000852 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100853 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800854 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100855 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100856 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100857 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000858 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100859
Antonio de Angelis4743e672019-04-11 11:38:48 +0100860 psa_invec in_vec[] = {
861 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
862 };
863 psa_outvec out_vec[] = {
864 {.base = &(operation->handle), .len = sizeof(uint32_t)},
865 };
866
867#ifdef TFM_PSA_API
868 PSA_CONNECT(TFM_CRYPTO);
869#endif
870
871 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
872 TFM_CRYPTO_MAC_VERIFY_SETUP);
873#ifdef TFM_PSA_API
874 PSA_CLOSE();
875#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100876
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000877 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100878}
879
880psa_status_t psa_mac_update(psa_mac_operation_t *operation,
881 const uint8_t *input,
882 size_t input_length)
883{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000884 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100885 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800886 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100887 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100888 };
889
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000890 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100891 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000892 {.base = input, .len = input_length},
893 };
894 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100895 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000896 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100897
Antonio de Angelis4743e672019-04-11 11:38:48 +0100898#ifdef TFM_PSA_API
899 PSA_CONNECT(TFM_CRYPTO);
900#endif
901
902 status = API_DISPATCH(tfm_crypto_mac_update,
903 TFM_CRYPTO_MAC_UPDATE);
904#ifdef TFM_PSA_API
905 PSA_CLOSE();
906#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100907
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000908 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100909}
910
911psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
912 uint8_t *mac,
913 size_t mac_size,
914 size_t *mac_length)
915{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000916 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100917 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800918 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100919 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100920 };
921
922 psa_invec in_vec[] = {
923 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
924 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000925 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100926 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000927 {.base = mac, .len = mac_size},
928 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100929
Antonio de Angelis4743e672019-04-11 11:38:48 +0100930#ifdef TFM_PSA_API
931 PSA_CONNECT(TFM_CRYPTO);
932#endif
933
934 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
935 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100936
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000937 *mac_length = out_vec[1].len;
938
Antonio de Angelis4743e672019-04-11 11:38:48 +0100939#ifdef TFM_PSA_API
940 PSA_CLOSE();
941#endif
942
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000943 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100944}
945
946psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
947 const uint8_t *mac,
948 size_t mac_length)
949{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000950 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100951 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800952 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100953 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100954 };
955
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000956 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100957 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000958 {.base = mac, .len = mac_length},
959 };
960 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100961 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000962 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100963
Antonio de Angelis4743e672019-04-11 11:38:48 +0100964#ifdef TFM_PSA_API
965 PSA_CONNECT(TFM_CRYPTO);
966#endif
967
968 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
969 TFM_CRYPTO_MAC_VERIFY_FINISH);
970
971#ifdef TFM_PSA_API
972 PSA_CLOSE();
973#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100974
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000975 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100976}
977
978psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
979{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000980 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100981 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800982 .sfn_id = TFM_CRYPTO_MAC_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100983 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000984 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100985
Antonio de Angelis4743e672019-04-11 11:38:48 +0100986 psa_invec in_vec[] = {
987 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
988 };
989 psa_outvec out_vec[] = {
990 {.base = &(operation->handle), .len = sizeof(uint32_t)},
991 };
992
993#ifdef TFM_PSA_API
994 PSA_CONNECT(TFM_CRYPTO);
995#endif
996
997 status = API_DISPATCH(tfm_crypto_mac_abort,
998 TFM_CRYPTO_MAC_ABORT);
999#ifdef TFM_PSA_API
1000 PSA_CLOSE();
1001#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001002
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001003 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001004}
Antonio de Angelis3a480992018-11-07 11:53:28 +00001005
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001006psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001007 psa_algorithm_t alg,
1008 const uint8_t *nonce,
1009 size_t nonce_length,
1010 const uint8_t *additional_data,
1011 size_t additional_data_length,
1012 const uint8_t *plaintext,
1013 size_t plaintext_length,
1014 uint8_t *ciphertext,
1015 size_t ciphertext_size,
1016 size_t *ciphertext_length)
1017{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001018 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001019 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001020 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001021 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001022 .alg = alg,
1023 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001024 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001025
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001026 /* Sanitize the optional input */
1027 if ((additional_data == NULL) && (additional_data_length != 0)) {
1028 return PSA_ERROR_INVALID_ARGUMENT;
1029 }
1030
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001031 size_t idx = 0;
1032 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001033 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001034 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001035 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001036 };
1037 psa_outvec out_vec[] = {
1038 {.base = ciphertext, .len = ciphertext_size},
1039 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001040
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001041 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1042 return PSA_ERROR_INVALID_ARGUMENT;
1043 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001044
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001045 if (nonce != NULL) {
1046 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001047 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001048 }
1049 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001050
Antonio de Angelis4743e672019-04-11 11:38:48 +01001051#ifdef TFM_PSA_API
1052 PSA_CONNECT(TFM_CRYPTO);
1053#endif
1054
1055#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001056 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001057 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001058 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001059 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001060 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001061 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001062#else
1063 status = API_DISPATCH(tfm_crypto_aead_encrypt,
1064 TFM_CRYPTO_AEAD_ENCRYPT);
1065#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001066
1067 *ciphertext_length = out_vec[0].len;
1068
Antonio de Angelis4743e672019-04-11 11:38:48 +01001069#ifdef TFM_PSA_API
1070 PSA_CLOSE();
1071#endif
1072
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001073 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001074}
1075
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001076psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001077 psa_algorithm_t alg,
1078 const uint8_t *nonce,
1079 size_t nonce_length,
1080 const uint8_t *additional_data,
1081 size_t additional_data_length,
1082 const uint8_t *ciphertext,
1083 size_t ciphertext_length,
1084 uint8_t *plaintext,
1085 size_t plaintext_size,
1086 size_t *plaintext_length)
1087{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001088 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001089 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001090 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001091 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001092 .alg = alg,
1093 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001094 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001095
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001096 /* Sanitize the optional input */
1097 if ((additional_data == NULL) && (additional_data_length != 0)) {
1098 return PSA_ERROR_INVALID_ARGUMENT;
1099 }
1100
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001101 size_t idx = 0;
1102 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001103 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001104 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001105 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001106 };
1107 psa_outvec out_vec[] = {
1108 {.base = plaintext, .len = plaintext_size},
1109 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001110
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001111 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1112 return PSA_ERROR_INVALID_ARGUMENT;
1113 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001114
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001115 if (nonce != NULL) {
1116 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001117 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001118 }
1119 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001120
Antonio de Angelis4743e672019-04-11 11:38:48 +01001121#ifdef TFM_PSA_API
1122 PSA_CONNECT(TFM_CRYPTO);
1123#endif
1124
1125#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001126 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001127 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001128 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001129 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001130 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001131 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001132#else
1133 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1134 TFM_CRYPTO_AEAD_DECRYPT);
1135#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001136
1137 *plaintext_length = out_vec[0].len;
1138
Antonio de Angelis4743e672019-04-11 11:38:48 +01001139#ifdef TFM_PSA_API
1140 PSA_CLOSE();
1141#endif
1142
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001143 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +00001144}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001145
1146psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
1147 psa_algorithm_t alg,
1148 const uint8_t *hash,
1149 size_t hash_length,
1150 uint8_t *signature,
1151 size_t signature_size,
1152 size_t *signature_length)
1153{
1154 psa_status_t status;
1155 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001156 .sfn_id = TFM_CRYPTO_ASYMMETRIC_SIGN_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001157 .key_handle = handle,
1158 .alg = alg,
1159 };
1160
1161 psa_invec in_vec[] = {
1162 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1163 {.base = hash, .len = hash_length},
1164 };
1165 psa_outvec out_vec[] = {
1166 {.base = signature, .len = signature_size},
1167 };
1168
1169#ifdef TFM_PSA_API
1170 PSA_CONNECT(TFM_CRYPTO);
1171#endif
1172
1173 status = API_DISPATCH(tfm_crypto_asymmetric_sign,
1174 TFM_CRYPTO_ASYMMETRIC_SIGN);
1175
1176 *signature_length = out_vec[0].len;
1177
1178#ifdef TFM_PSA_API
1179 PSA_CLOSE();
1180#endif
1181
1182 return status;
1183}
1184
1185psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
1186 psa_algorithm_t alg,
1187 const uint8_t *hash,
1188 size_t hash_length,
1189 const uint8_t *signature,
1190 size_t signature_length)
1191{
1192 psa_status_t status;
1193 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001194 .sfn_id = TFM_CRYPTO_ASYMMETRIC_VERIFY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001195 .key_handle = handle,
1196 .alg = alg
1197 };
1198
1199 psa_invec in_vec[] = {
1200 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1201 {.base = hash, .len = hash_length},
1202 {.base = signature, .len = signature_length}
1203 };
1204
1205#ifdef TFM_PSA_API
1206 PSA_CONNECT(TFM_CRYPTO);
1207#endif
1208
1209 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_asymmetric_verify,
1210 TFM_CRYPTO_ASYMMETRIC_VERIFY);
1211#ifdef TFM_PSA_API
1212 PSA_CLOSE();
1213#endif
1214
1215 return status;
1216}
1217
1218psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
1219 psa_algorithm_t alg,
1220 const uint8_t *input,
1221 size_t input_length,
1222 const uint8_t *salt,
1223 size_t salt_length,
1224 uint8_t *output,
1225 size_t output_size,
1226 size_t *output_length)
1227{
1228 psa_status_t status;
1229 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001230 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001231 .key_handle = handle,
1232 .alg = alg
1233 };
1234
1235 /* Sanitize the optional input */
1236 if ((salt == NULL) && (salt_length != 0)) {
1237 return PSA_ERROR_INVALID_ARGUMENT;
1238 }
1239
1240 psa_invec in_vec[] = {
1241 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1242 {.base = input, .len = input_length},
1243 {.base = salt, .len = salt_length}
1244 };
1245
1246 psa_outvec out_vec[] = {
1247 {.base = output, .len = output_size},
1248 };
1249
1250#ifdef TFM_PSA_API
1251 PSA_CONNECT(TFM_CRYPTO);
1252#endif
1253
1254#ifdef TFM_PSA_API
1255 size_t in_len = ARRAY_SIZE(in_vec);
1256 if (salt == NULL) {
1257 in_len--;
1258 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001259 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001260 out_vec, ARRAY_SIZE(out_vec));
1261#else
1262 status = API_DISPATCH(tfm_crypto_asymmetric_encrypt,
1263 TFM_CRYPTO_ASYMMETRIC_ENCRYPT);
1264#endif
1265
1266 *output_length = out_vec[0].len;
1267
1268#ifdef TFM_PSA_API
1269 PSA_CLOSE();
1270#endif
1271
1272 return status;
1273}
1274
1275psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
1276 psa_algorithm_t alg,
1277 const uint8_t *input,
1278 size_t input_length,
1279 const uint8_t *salt,
1280 size_t salt_length,
1281 uint8_t *output,
1282 size_t output_size,
1283 size_t *output_length)
1284{
1285 psa_status_t status;
1286 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001287 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001288 .key_handle = handle,
1289 .alg = alg
1290 };
1291
1292 /* Sanitize the optional input */
1293 if ((salt == NULL) && (salt_length != 0)) {
1294 return PSA_ERROR_INVALID_ARGUMENT;
1295 }
1296
1297 psa_invec in_vec[] = {
1298 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1299 {.base = input, .len = input_length},
1300 {.base = salt, .len = salt_length}
1301 };
1302
1303 psa_outvec out_vec[] = {
1304 {.base = output, .len = output_size},
1305 };
1306
1307#ifdef TFM_PSA_API
1308 PSA_CONNECT(TFM_CRYPTO);
1309#endif
1310
1311#ifdef TFM_PSA_API
1312 size_t in_len = ARRAY_SIZE(in_vec);
1313 if (salt == NULL) {
1314 in_len--;
1315 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001316 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001317 out_vec, ARRAY_SIZE(out_vec));
1318#else
1319 status = API_DISPATCH(tfm_crypto_asymmetric_decrypt,
1320 TFM_CRYPTO_ASYMMETRIC_DECRYPT);
1321#endif
1322
1323 *output_length = out_vec[0].len;
1324
1325#ifdef TFM_PSA_API
1326 PSA_CLOSE();
1327#endif
1328
1329 return status;
1330}
1331
1332psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1333 size_t *capacity)
1334{
1335 psa_status_t status;
1336 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001337 .sfn_id = TFM_CRYPTO_GET_GENERATOR_CAPACITY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001338 .op_handle = generator->handle,
1339 };
1340
1341 psa_invec in_vec[] = {
1342 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1343 };
1344
1345 psa_outvec out_vec[] = {
1346 {.base = capacity, .len = sizeof(size_t)},
1347 };
1348
1349#ifdef TFM_PSA_API
1350 PSA_CONNECT(TFM_CRYPTO);
1351#endif
1352
1353 status = API_DISPATCH(tfm_crypto_get_generator_capacity,
1354 TFM_CRYPTO_GET_GENERATOR_CAPACITY);
1355#ifdef TFM_PSA_API
1356 PSA_CLOSE();
1357#endif
1358
1359 return status;
1360}
1361
1362psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1363 uint8_t *output,
1364 size_t output_length)
1365{
1366 psa_status_t status;
1367 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001368 .sfn_id = TFM_CRYPTO_GENERATOR_READ_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001369 .op_handle = generator->handle,
1370 };
1371
1372 psa_invec in_vec[] = {
1373 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1374 };
1375
1376 psa_outvec out_vec[] = {
1377 {.base = output, .len = output_length},
1378 };
1379
1380#ifdef TFM_PSA_API
1381 PSA_CONNECT(TFM_CRYPTO);
1382#endif
1383
1384 status = API_DISPATCH(tfm_crypto_generator_read,
1385 TFM_CRYPTO_GENERATOR_READ);
1386#ifdef TFM_PSA_API
1387 PSA_CLOSE();
1388#endif
1389
1390 return status;
1391}
1392
1393psa_status_t psa_generator_import_key(psa_key_handle_t handle,
1394 psa_key_type_t type,
1395 size_t bits,
1396 psa_crypto_generator_t *generator)
1397{
1398 psa_status_t status;
1399 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001400 .sfn_id = TFM_CRYPTO_GENERATOR_IMPORT_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001401 .key_handle = handle,
1402 .type = type,
1403 .op_handle = generator->handle,
1404 };
1405
1406 psa_invec in_vec[] = {
1407 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1408 {.base = &bits, .len = sizeof(size_t)},
1409 };
1410
1411#ifdef TFM_PSA_API
1412 PSA_CONNECT(TFM_CRYPTO);
1413#endif
1414
1415 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generator_import_key,
1416 TFM_CRYPTO_GENERATOR_IMPORT_KEY);
1417#ifdef TFM_PSA_API
1418 PSA_CLOSE();
1419#endif
1420
1421 return status;
1422}
1423
1424psa_status_t psa_generator_abort(psa_crypto_generator_t *generator)
1425{
1426 psa_status_t status;
1427 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001428 .sfn_id = TFM_CRYPTO_GENERATOR_ABORT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001429 .op_handle = generator->handle,
1430 };
1431
1432 psa_invec in_vec[] = {
1433 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1434 };
1435
1436 psa_outvec out_vec[] = {
1437 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1438 };
1439
1440#ifdef TFM_PSA_API
1441 PSA_CONNECT(TFM_CRYPTO);
1442#endif
1443
1444 status = API_DISPATCH(tfm_crypto_generator_abort,
1445 TFM_CRYPTO_GENERATOR_ABORT);
1446#ifdef TFM_PSA_API
1447 PSA_CLOSE();
1448#endif
1449
1450 return status;
1451}
1452
1453psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
1454 psa_key_handle_t handle,
1455 psa_algorithm_t alg,
1456 const uint8_t *salt,
1457 size_t salt_length,
1458 const uint8_t *label,
1459 size_t label_length,
1460 size_t capacity)
1461{
1462 psa_status_t status;
1463 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001464 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001465 .key_handle = handle,
1466 .alg = alg,
1467 .op_handle = generator->handle,
1468 .capacity = capacity,
1469 };
1470
1471 /* Sanitize the optional input */
1472 if ((salt == NULL) && (salt_length != 0)) {
1473 return PSA_ERROR_INVALID_ARGUMENT;
1474 }
1475
1476 if ((label == NULL) && (label_length != 0)) {
1477 return PSA_ERROR_INVALID_ARGUMENT;
1478 }
1479
1480 psa_invec in_vec[] = {
1481 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1482 {.base = salt, .len = salt_length},
1483 {.base = label, .len = label_length},
1484 };
1485
1486 psa_outvec out_vec[] = {
1487 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1488 };
1489
1490#ifdef TFM_PSA_API
1491 PSA_CONNECT(TFM_CRYPTO);
1492#endif
1493
1494#ifdef TFM_PSA_API
1495 size_t in_len = ARRAY_SIZE(in_vec);
1496 if (label == NULL) {
1497 in_len--;
1498 if (salt == NULL) {
1499 in_len--;
1500 }
1501 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001502 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001503 out_vec, ARRAY_SIZE(out_vec));
1504#else
1505 status = API_DISPATCH(tfm_crypto_key_derivation,
1506 TFM_CRYPTO_KEY_DERIVATION);
1507#endif
1508
1509#ifdef TFM_PSA_API
1510 PSA_CLOSE();
1511#endif
1512
1513 return status;
1514}
1515
1516psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
1517 psa_key_handle_t private_key,
1518 const uint8_t *peer_key,
1519 size_t peer_key_length,
1520 psa_algorithm_t alg)
1521{
1522 psa_status_t status;
1523 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001524 .sfn_id = TFM_CRYPTO_KEY_AGREEMENT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001525 .key_handle = private_key,
1526 .alg = alg,
1527 .op_handle = generator->handle,
1528 };
1529
1530 psa_invec in_vec[] = {
1531 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1532 {.base = peer_key, .len = peer_key_length},
1533 };
1534
1535 psa_outvec out_vec[] = {
1536 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1537 };
1538
1539#ifdef TFM_PSA_API
1540 PSA_CONNECT(TFM_CRYPTO);
1541#endif
1542
1543 status = API_DISPATCH(tfm_crypto_key_agreement,
1544 TFM_CRYPTO_KEY_AGREEMENT);
1545
1546#ifdef TFM_PSA_API
1547 PSA_CLOSE();
1548#endif
1549
1550 return status;
1551}
1552
1553psa_status_t psa_generate_random(uint8_t *output,
1554 size_t output_size)
1555{
1556 psa_status_t status;
1557 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001558 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001559 };
1560
1561 psa_invec in_vec[] = {
1562 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1563 };
1564
1565 psa_outvec out_vec[] = {
1566 {.base = output, .len = output_size},
1567 };
1568
1569 if (output_size == 0) {
1570 return PSA_SUCCESS;
1571 }
1572
1573#ifdef TFM_PSA_API
1574 PSA_CONNECT(TFM_CRYPTO);
1575#endif
1576
1577 status = API_DISPATCH(tfm_crypto_generate_random,
1578 TFM_CRYPTO_GENERATE_RANDOM);
1579
1580#ifdef TFM_PSA_API
1581 PSA_CLOSE();
1582#endif
1583
1584 return status;
1585}
1586
1587psa_status_t psa_generate_key(psa_key_handle_t handle,
1588 psa_key_type_t type,
1589 size_t bits,
1590 const void *extra,
1591 size_t extra_size)
1592{
1593 psa_status_t status;
1594 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001595 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001596 .key_handle = handle,
1597 .type = type,
1598 };
1599
1600 /* Sanitize the optional input */
1601 if ((extra == NULL) && (extra_size != 0)) {
1602 return PSA_ERROR_INVALID_ARGUMENT;
1603 }
1604
1605 psa_invec in_vec[] = {
1606 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1607 {.base = &bits, .len = sizeof(size_t)},
1608 {.base = extra, .len = extra_size},
1609 };
1610
1611#ifdef TFM_PSA_API
1612 PSA_CONNECT(TFM_CRYPTO);
1613#endif
1614
1615#ifdef TFM_PSA_API
1616 size_t in_len = ARRAY_SIZE(in_vec);
1617 if (extra == NULL) {
1618 in_len--;
1619 }
1620
Summer Qin4b1d03b2019-07-02 14:56:08 +08001621 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len, NULL, 0);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001622#else
1623 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generate_key,
1624 TFM_CRYPTO_GENERATE_KEY);
1625#endif
1626
1627#ifdef TFM_PSA_API
1628 PSA_CLOSE();
1629#endif
1630
1631 return status;
1632}