blob: c53641b54386671e8c616afc42920a8978c5eb00 [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{
Antonio de Angelis7740b382019-07-16 10:59:25 +010061#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
62 return PSA_ERROR_NOT_SUPPORTED;
63#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064 psa_status_t status;
65 const struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080066 .sfn_id = TFM_CRYPTO_ALLOCATE_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010067 };
68 psa_invec in_vec[] = {
69 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
70 };
71 psa_outvec out_vec[] = {
72 {.base = handle, .len = sizeof(psa_key_handle_t)},
73 };
74
75#ifdef TFM_PSA_API
76 PSA_CONNECT(TFM_CRYPTO);
77#endif
78
79 status = API_DISPATCH(tfm_crypto_allocate_key,
80 TFM_CRYPTO_ALLOCATE_KEY);
81#ifdef TFM_PSA_API
82 PSA_CLOSE();
83#endif
84
85 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010086#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010087}
88
89psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
90 psa_key_id_t id,
91 psa_key_handle_t *handle)
92{
Antonio de Angelis7740b382019-07-16 10:59:25 +010093#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
94 return PSA_ERROR_NOT_SUPPORTED;
95#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +010096 (void)lifetime;
97 (void)id;
98 (void)handle;
99
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100100 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100101 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100102#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100103}
104
105psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
106 psa_key_id_t id,
107 psa_key_handle_t *handle)
108{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100109#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
110 return PSA_ERROR_NOT_SUPPORTED;
111#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100112 (void)lifetime;
113 (void)id;
114 (void)handle;
115
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100116 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100117 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100118#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100119}
120
121psa_status_t psa_close_key(psa_key_handle_t handle)
122{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100123#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
124 return PSA_ERROR_NOT_SUPPORTED;
125#else
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100126 (void)handle;
127
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100128 /* TODO: Persistent key APIs are not supported yet */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100129 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100130#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100131}
132
133psa_status_t psa_import_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100134 psa_key_type_t type,
135 const uint8_t *data,
136 size_t data_length)
137{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100138#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
139 return PSA_ERROR_NOT_SUPPORTED;
140#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000141 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100142 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800143 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100144 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100145 .type = type,
146 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000147 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100148 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000149 {.base = data, .len = data_length}
150 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100151
Antonio de Angelis4743e672019-04-11 11:38:48 +0100152#ifdef TFM_PSA_API
153 PSA_CONNECT(TFM_CRYPTO);
154#endif
155
156 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_import_key,
157 TFM_CRYPTO_IMPORT_KEY);
158#ifdef TFM_PSA_API
159 PSA_CLOSE();
160#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100161
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000162 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100163#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100164}
165
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100166psa_status_t psa_destroy_key(psa_key_handle_t handle)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100167{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100168#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
169 return PSA_ERROR_NOT_SUPPORTED;
170#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000171 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100172 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800173 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100174 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100175 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000176 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100177 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000178 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100179
Antonio de Angelis4743e672019-04-11 11:38:48 +0100180#ifdef TFM_PSA_API
181 PSA_CONNECT(TFM_CRYPTO);
182#endif
183
184 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
185 TFM_CRYPTO_DESTROY_KEY);
186#ifdef TFM_PSA_API
187 PSA_CLOSE();
188#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100189
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000190 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100191#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100192}
193
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100194psa_status_t psa_get_key_information(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100195 psa_key_type_t *type,
196 size_t *bits)
197{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100198#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
199 return PSA_ERROR_NOT_SUPPORTED;
200#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000201 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100202 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800203 .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100204 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100205 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000206 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100207 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000208 };
209 psa_outvec out_vec[] = {
210 {.base = type, .len = sizeof(psa_key_type_t)},
211 {.base = bits, .len = sizeof(size_t)}
212 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100213
Antonio de Angelis4743e672019-04-11 11:38:48 +0100214#ifdef TFM_PSA_API
215 PSA_CONNECT(TFM_CRYPTO);
216#endif
217
218 status = API_DISPATCH(tfm_crypto_get_key_information,
219 TFM_CRYPTO_GET_KEY_INFORMATION);
220#ifdef TFM_PSA_API
221 PSA_CLOSE();
222#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100223
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100225#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100226}
227
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100228psa_status_t psa_export_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100229 uint8_t *data,
230 size_t data_size,
231 size_t *data_length)
232{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100233#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
234 return PSA_ERROR_NOT_SUPPORTED;
235#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000236 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800238 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100239 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100240 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000241 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100242 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000243 };
244 psa_outvec out_vec[] = {
245 {.base = data, .len = data_size}
246 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100247
Antonio de Angelis4743e672019-04-11 11:38:48 +0100248#ifdef TFM_PSA_API
249 PSA_CONNECT(TFM_CRYPTO);
250#endif
251
252 status = API_DISPATCH(tfm_crypto_export_key,
253 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100254
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000255 *data_length = out_vec[0].len;
256
Antonio de Angelis4743e672019-04-11 11:38:48 +0100257#ifdef TFM_PSA_API
258 PSA_CLOSE();
259#endif
260
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000261 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100262#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100263}
264
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100265psa_status_t psa_export_public_key(psa_key_handle_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100266 uint8_t *data,
267 size_t data_size,
268 size_t *data_length)
269{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100270#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
271 return PSA_ERROR_NOT_SUPPORTED;
272#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100273 psa_status_t status;
274 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800275 .sfn_id = TFM_CRYPTO_EXPORT_PUBLIC_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100276 .key_handle = handle,
277 };
Hugues de Valon8b442442019-02-19 14:30:52 +0000278
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100279 psa_invec in_vec[] = {
280 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
281 };
282 psa_outvec out_vec[] = {
283 {.base = data, .len = data_size}
284 };
285
286#ifdef TFM_PSA_API
287 PSA_CONNECT(TFM_CRYPTO);
288#endif
289
290 status = API_DISPATCH(tfm_crypto_export_public_key,
291 TFM_CRYPTO_EXPORT_PUBLIC_KEY);
292
293 *data_length = out_vec[0].len;
294
295#ifdef TFM_PSA_API
296 PSA_CLOSE();
297#endif
298
299 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100300#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100301}
302
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100303psa_status_t psa_copy_key(psa_key_handle_t source_handle,
304 psa_key_handle_t target_handle,
305 const psa_key_policy_t *constraint)
Jamie Foxefd82732018-11-26 10:34:32 +0000306{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100307#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
308 return PSA_ERROR_NOT_SUPPORTED;
309#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100310 psa_status_t status;
311 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800312 .sfn_id = TFM_CRYPTO_COPY_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100313 .key_handle = source_handle,
314 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000315
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100316 psa_invec in_vec[] = {
317 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
318 {.base = &target_handle, .len = sizeof(psa_key_handle_t)},
319 {.base = constraint, .len = sizeof(psa_key_policy_t)},
320 };
321
322#ifdef TFM_PSA_API
323 PSA_CONNECT(TFM_CRYPTO);
324#endif
325
326 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_copy_key,
327 TFM_CRYPTO_COPY_KEY);
328#ifdef TFM_PSA_API
329 PSA_CLOSE();
330#endif
331
332 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100333#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000334}
335
336void psa_key_policy_set_usage(psa_key_policy_t *policy,
337 psa_key_usage_t usage,
338 psa_algorithm_t alg)
339{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100340 policy->usage = usage;
341 policy->alg = alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000342}
343
344psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy)
345{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100346 return policy->usage;
Jamie Foxefd82732018-11-26 10:34:32 +0000347}
348
349psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy)
350{
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100351 return policy->alg;
Jamie Foxefd82732018-11-26 10:34:32 +0000352}
353
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100354psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000355 const psa_key_policy_t *policy)
356{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100357#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
358 return PSA_ERROR_NOT_SUPPORTED;
359#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000360 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100361 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800362 .sfn_id = TFM_CRYPTO_SET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100363 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100364 };
365
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000366 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100367 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000368 {.base = policy, .len = sizeof(psa_key_policy_t)},
369 };
Jamie Foxefd82732018-11-26 10:34:32 +0000370
Antonio de Angelis4743e672019-04-11 11:38:48 +0100371#ifdef TFM_PSA_API
372 PSA_CONNECT(TFM_CRYPTO);
373#endif
374
375 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_set_key_policy,
376 TFM_CRYPTO_SET_KEY_POLICY);
377#ifdef TFM_PSA_API
378 PSA_CLOSE();
379#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000380
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000381 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100382#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000383}
384
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100385psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000386 psa_key_policy_t *policy)
387{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100388#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
389 return PSA_ERROR_NOT_SUPPORTED;
390#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000391 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100392 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800393 .sfn_id = TFM_CRYPTO_GET_KEY_POLICY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100394 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100395 };
396
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000397 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100398 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000399 };
400 psa_outvec out_vec[] = {
401 {.base = policy, .len = sizeof(psa_key_policy_t)},
402 };
Jamie Foxefd82732018-11-26 10:34:32 +0000403
Antonio de Angelis4743e672019-04-11 11:38:48 +0100404#ifdef TFM_PSA_API
405 PSA_CONNECT(TFM_CRYPTO);
406#endif
407
408 status = API_DISPATCH(tfm_crypto_get_key_policy,
409 TFM_CRYPTO_GET_KEY_POLICY);
410#ifdef TFM_PSA_API
411 PSA_CLOSE();
412#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000413
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000414 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100415#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000416}
417
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100418psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Jamie Foxefd82732018-11-26 10:34:32 +0000419 psa_key_lifetime_t *lifetime)
420{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100421#if (TFM_CRYPTO_KEY_MODULE_DISABLED != 0)
422 return PSA_ERROR_NOT_SUPPORTED;
423#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000424 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100425 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800426 .sfn_id = TFM_CRYPTO_GET_KEY_LIFETIME_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100427 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100428 };
429
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000430 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100431 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000432 };
433 psa_outvec out_vec[] = {
434 {.base = lifetime, .len = sizeof(psa_key_lifetime_t)},
435 };
Jamie Foxefd82732018-11-26 10:34:32 +0000436
Antonio de Angelis4743e672019-04-11 11:38:48 +0100437#ifdef TFM_PSA_API
438 PSA_CONNECT(TFM_CRYPTO);
439#endif
440
441 status = API_DISPATCH(tfm_crypto_get_key_lifetime,
442 TFM_CRYPTO_GET_KEY_LIFETIME);
443#ifdef TFM_PSA_API
444 PSA_CLOSE();
445#endif
Jamie Foxefd82732018-11-26 10:34:32 +0000446
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000447 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100448#endif /* TFM_CRYPTO_KEY_MODULE_DISABLED */
Jamie Foxefd82732018-11-26 10:34:32 +0000449}
450
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100451psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
452 unsigned char *iv,
453 size_t iv_size,
454 size_t *iv_length)
455{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100456#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
457 return PSA_ERROR_NOT_SUPPORTED;
458#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100459 psa_status_t status;
460 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800461 .sfn_id = TFM_CRYPTO_CIPHER_GENERATE_IV_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100462 .op_handle = operation->handle,
463 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100464
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100465 psa_invec in_vec[] = {
466 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
467 };
468 psa_outvec out_vec[] = {
469 {.base = &(operation->handle), .len = sizeof(uint32_t)},
470 {.base = iv, .len = iv_size},
471 };
472
473#ifdef TFM_PSA_API
474 PSA_CONNECT(TFM_CRYPTO);
475#endif
476
477 status = API_DISPATCH(tfm_crypto_cipher_generate_iv,
478 TFM_CRYPTO_CIPHER_GENERATE_IV);
479
480 *iv_length = out_vec[1].len;
481
482#ifdef TFM_PSA_API
483 PSA_CLOSE();
484#endif
485
486 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100487#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100488}
489
Antonio de Angelis377a1552018-11-22 17:02:40 +0000490psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
491 const unsigned char *iv,
492 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100493{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100494#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
495 return PSA_ERROR_NOT_SUPPORTED;
496#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000497 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100498 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800499 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100500 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100501 };
502
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000503 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100504 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000505 {.base = iv, .len = iv_length},
506 };
507 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100508 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000509 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100510
Antonio de Angelis4743e672019-04-11 11:38:48 +0100511#ifdef TFM_PSA_API
512 PSA_CONNECT(TFM_CRYPTO);
513#endif
514
515 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
516 TFM_CRYPTO_CIPHER_SET_IV);
517#ifdef TFM_PSA_API
518 PSA_CLOSE();
519#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100520
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000521 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100522#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100523}
524
Antonio de Angelis377a1552018-11-22 17:02:40 +0000525psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100526 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000527 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100528{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100529#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
530 return PSA_ERROR_NOT_SUPPORTED;
531#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000532 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100533 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800534 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100535 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100536 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100537 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000538 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100539
Antonio de Angelis4743e672019-04-11 11:38:48 +0100540 psa_invec in_vec[] = {
541 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
542 };
543 psa_outvec out_vec[] = {
544 {.base = &(operation->handle), .len = sizeof(uint32_t)},
545 };
546
547#ifdef TFM_PSA_API
548 PSA_CONNECT(TFM_CRYPTO);
549#endif
550
551 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
552 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
553#ifdef TFM_PSA_API
554 PSA_CLOSE();
555#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100556
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000557 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100558#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100559}
560
Antonio de Angelis377a1552018-11-22 17:02:40 +0000561psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100562 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000563 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100564{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100565#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
566 return PSA_ERROR_NOT_SUPPORTED;
567#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000568 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100569 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800570 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100571 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100572 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100573 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000574 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100575
Antonio de Angelis4743e672019-04-11 11:38:48 +0100576 psa_invec in_vec[] = {
577 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
578 };
579 psa_outvec out_vec[] = {
580 {.base = &(operation->handle), .len = sizeof(uint32_t)},
581 };
582
583#ifdef TFM_PSA_API
584 PSA_CONNECT(TFM_CRYPTO);
585#endif
586
587 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
588 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
589#ifdef TFM_PSA_API
590 PSA_CLOSE();
591#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100592
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000593 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100594#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100595}
596
597psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
598 const uint8_t *input,
599 size_t input_length,
600 unsigned char *output,
601 size_t output_size,
602 size_t *output_length)
603{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100604#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
605 return PSA_ERROR_NOT_SUPPORTED;
606#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000607 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100608 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800609 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100610 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100611 };
612
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000613 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100614 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000615 {.base = input, .len = input_length},
616 };
617 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100618 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000619 {.base = output, .len = output_size}
620 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100621
Antonio de Angelis4743e672019-04-11 11:38:48 +0100622#ifdef TFM_PSA_API
623 PSA_CONNECT(TFM_CRYPTO);
624#endif
625
626 status = API_DISPATCH(tfm_crypto_cipher_update,
627 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100628
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000629 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100630
Antonio de Angelis4743e672019-04-11 11:38:48 +0100631#ifdef TFM_PSA_API
632 PSA_CLOSE();
633#endif
634
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000635 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100636#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100637}
638
639psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
640{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100641#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
642 return PSA_ERROR_NOT_SUPPORTED;
643#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000644 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100645 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800646 .sfn_id = TFM_CRYPTO_CIPHER_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100647 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000648 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100649
Antonio de Angelis4743e672019-04-11 11:38:48 +0100650 psa_invec in_vec[] = {
651 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
652 };
653 psa_outvec out_vec[] = {
654 {.base = &(operation->handle), .len = sizeof(uint32_t)},
655 };
656
657#ifdef TFM_PSA_API
658 PSA_CONNECT(TFM_CRYPTO);
659#endif
660
661 status = API_DISPATCH(tfm_crypto_cipher_abort,
662 TFM_CRYPTO_CIPHER_ABORT);
663#ifdef TFM_PSA_API
664 PSA_CLOSE();
665#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +0100666
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000667 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100668#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100669}
670
671psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
672 uint8_t *output,
673 size_t output_size,
674 size_t *output_length)
675{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100676#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
677 return PSA_ERROR_NOT_SUPPORTED;
678#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000679 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100680 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800681 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100682 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100683 };
684
685 psa_invec in_vec[] = {
686 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
687 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000688 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100689 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000690 {.base = output, .len = output_size},
691 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100692
Antonio de Angelis4743e672019-04-11 11:38:48 +0100693#ifdef TFM_PSA_API
694 PSA_CONNECT(TFM_CRYPTO);
695#endif
696
697 status = API_DISPATCH(tfm_crypto_cipher_finish,
698 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100699
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000700 *output_length = out_vec[1].len;
701
Antonio de Angelis4743e672019-04-11 11:38:48 +0100702#ifdef TFM_PSA_API
703 PSA_CLOSE();
704#endif
705
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000706 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100707#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100708}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100709
Antonio de Angelis377a1552018-11-22 17:02:40 +0000710psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100711 psa_algorithm_t alg)
712{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100713#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
714 return PSA_ERROR_NOT_SUPPORTED;
715#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000716 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100717 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800718 .sfn_id = TFM_CRYPTO_HASH_SETUP_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100719 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100720 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000721 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100722
Antonio de Angelis4743e672019-04-11 11:38:48 +0100723 psa_invec in_vec[] = {
724 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
725 };
726 psa_outvec out_vec[] = {
727 {.base = &(operation->handle), .len = sizeof(uint32_t)},
728 };
729
730#ifdef TFM_PSA_API
731 PSA_CONNECT(TFM_CRYPTO);
732#endif
733
734 status = API_DISPATCH(tfm_crypto_hash_setup,
735 TFM_CRYPTO_HASH_SETUP);
736
737#ifdef TFM_PSA_API
738 PSA_CLOSE();
739#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100740
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000741 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100742#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100743}
744
745psa_status_t psa_hash_update(psa_hash_operation_t *operation,
746 const uint8_t *input,
747 size_t input_length)
748{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100749#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
750 return PSA_ERROR_NOT_SUPPORTED;
751#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000752 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100753 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800754 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100755 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100756 };
757
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000758 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100759 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000760 {.base = input, .len = input_length},
761 };
762 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100763 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000764 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100765
Antonio de Angelis4743e672019-04-11 11:38:48 +0100766#ifdef TFM_PSA_API
767 PSA_CONNECT(TFM_CRYPTO);
768#endif
769
770 status = API_DISPATCH(tfm_crypto_hash_update,
771 TFM_CRYPTO_HASH_UPDATE);
772
773#ifdef TFM_PSA_API
774 PSA_CLOSE();
775#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100776
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000777 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100778#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100779}
780
781psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
782 uint8_t *hash,
783 size_t hash_size,
784 size_t *hash_length)
785{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100786#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
787 return PSA_ERROR_NOT_SUPPORTED;
788#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000789 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100790 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800791 .sfn_id = TFM_CRYPTO_HASH_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100792 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100793 };
794
795 psa_invec in_vec[] = {
796 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
797 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000798 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100799 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000800 {.base = hash, .len = hash_size},
801 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100802
Antonio de Angelis4743e672019-04-11 11:38:48 +0100803#ifdef TFM_PSA_API
804 PSA_CONNECT(TFM_CRYPTO);
805#endif
806
807 status = API_DISPATCH(tfm_crypto_hash_finish,
808 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100809
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000810 *hash_length = out_vec[1].len;
811
Antonio de Angelis4743e672019-04-11 11:38:48 +0100812#ifdef TFM_PSA_API
813 PSA_CLOSE();
814#endif
815
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000816 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100817#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100818}
819
820psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
821 const uint8_t *hash,
822 size_t hash_length)
823{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100824#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
825 return PSA_ERROR_NOT_SUPPORTED;
826#else
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 = {
Edison Ai080b2e22019-04-17 16:27:21 +0800829 .sfn_id = TFM_CRYPTO_HASH_VERIFY_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100830 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100831 };
832
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000833 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100834 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000835 {.base = hash, .len = hash_length},
836 };
837 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100838 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000839 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100840
Antonio de Angelis4743e672019-04-11 11:38:48 +0100841#ifdef TFM_PSA_API
842 PSA_CONNECT(TFM_CRYPTO);
843#endif
844
845 status = API_DISPATCH(tfm_crypto_hash_verify,
846 TFM_CRYPTO_HASH_VERIFY);
847#ifdef TFM_PSA_API
848 PSA_CLOSE();
849#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100850
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000851 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100852#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100853}
854
855psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
856{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100857#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
858 return PSA_ERROR_NOT_SUPPORTED;
859#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000860 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100861 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800862 .sfn_id = TFM_CRYPTO_HASH_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100863 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000864 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100865
Antonio de Angelis4743e672019-04-11 11:38:48 +0100866 psa_invec in_vec[] = {
867 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
868 };
869 psa_outvec out_vec[] = {
870 {.base = &(operation->handle), .len = sizeof(uint32_t)},
871 };
872
873#ifdef TFM_PSA_API
874 PSA_CONNECT(TFM_CRYPTO);
875#endif
876
877 status = API_DISPATCH(tfm_crypto_hash_abort,
878 TFM_CRYPTO_HASH_ABORT);
879#ifdef TFM_PSA_API
880 PSA_CLOSE();
881#endif
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100882
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000883 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100884#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100885}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100886
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100887psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
888 psa_hash_operation_t *target_operation)
889{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100890#if (TFM_CRYPTO_HASH_MODULE_DISABLED != 0)
891 return PSA_ERROR_NOT_SUPPORTED;
892#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100893 psa_status_t status;
894 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800895 .sfn_id = TFM_CRYPTO_HASH_CLONE_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100896 .op_handle = source_operation->handle,
897 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100898
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100899 psa_invec in_vec[] = {
900 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
901 };
902 psa_outvec out_vec[] = {
903 {.base = target_operation, .len = sizeof(psa_hash_operation_t)},
904 };
905
906#ifdef TFM_PSA_API
907 PSA_CONNECT(TFM_CRYPTO);
908#endif
909
910 status = API_DISPATCH(tfm_crypto_hash_clone,
911 TFM_CRYPTO_HASH_CLONE);
912#ifdef TFM_PSA_API
913 PSA_CLOSE();
914#endif
915
916 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100917#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100918}
919
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100920psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100921 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100922 psa_algorithm_t alg)
923{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100924#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
925 return PSA_ERROR_NOT_SUPPORTED;
926#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000927 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100928 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800929 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100930 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100931 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100932 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000933 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100934
Antonio de Angelis4743e672019-04-11 11:38:48 +0100935 psa_invec in_vec[] = {
936 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
937 };
938 psa_outvec out_vec[] = {
939 {.base = &(operation->handle), .len = sizeof(uint32_t)},
940 };
941
942#ifdef TFM_PSA_API
943 PSA_CONNECT(TFM_CRYPTO);
944#endif
945
946 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
947 TFM_CRYPTO_MAC_SIGN_SETUP);
948#ifdef TFM_PSA_API
949 PSA_CLOSE();
950#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100951
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000952 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100953#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100954}
955
956psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100957 psa_key_handle_t handle,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100958 psa_algorithm_t alg)
959{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100960#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
961 return PSA_ERROR_NOT_SUPPORTED;
962#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000963 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100964 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800965 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100966 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100967 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100968 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000969 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100970
Antonio de Angelis4743e672019-04-11 11:38:48 +0100971 psa_invec in_vec[] = {
972 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
973 };
974 psa_outvec out_vec[] = {
975 {.base = &(operation->handle), .len = sizeof(uint32_t)},
976 };
977
978#ifdef TFM_PSA_API
979 PSA_CONNECT(TFM_CRYPTO);
980#endif
981
982 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
983 TFM_CRYPTO_MAC_VERIFY_SETUP);
984#ifdef TFM_PSA_API
985 PSA_CLOSE();
986#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100987
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000988 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100989#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100990}
991
992psa_status_t psa_mac_update(psa_mac_operation_t *operation,
993 const uint8_t *input,
994 size_t input_length)
995{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100996#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
997 return PSA_ERROR_NOT_SUPPORTED;
998#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000999 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001000 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001001 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001002 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001003 };
1004
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001005 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001006 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001007 {.base = input, .len = input_length},
1008 };
1009 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001010 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001011 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001012
Antonio de Angelis4743e672019-04-11 11:38:48 +01001013#ifdef TFM_PSA_API
1014 PSA_CONNECT(TFM_CRYPTO);
1015#endif
1016
1017 status = API_DISPATCH(tfm_crypto_mac_update,
1018 TFM_CRYPTO_MAC_UPDATE);
1019#ifdef TFM_PSA_API
1020 PSA_CLOSE();
1021#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001022
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001023 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001024#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001025}
1026
1027psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1028 uint8_t *mac,
1029 size_t mac_size,
1030 size_t *mac_length)
1031{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001032#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
1033 return PSA_ERROR_NOT_SUPPORTED;
1034#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001035 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001036 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001037 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001038 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001039 };
1040
1041 psa_invec in_vec[] = {
1042 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1043 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001044 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001045 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001046 {.base = mac, .len = mac_size},
1047 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001048
Antonio de Angelis4743e672019-04-11 11:38:48 +01001049#ifdef TFM_PSA_API
1050 PSA_CONNECT(TFM_CRYPTO);
1051#endif
1052
1053 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
1054 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001055
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001056 *mac_length = out_vec[1].len;
1057
Antonio de Angelis4743e672019-04-11 11:38:48 +01001058#ifdef TFM_PSA_API
1059 PSA_CLOSE();
1060#endif
1061
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001062 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001063#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001064}
1065
1066psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1067 const uint8_t *mac,
1068 size_t mac_length)
1069{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001070#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
1071 return PSA_ERROR_NOT_SUPPORTED;
1072#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001073 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001074 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001075 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001076 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001077 };
1078
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001079 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001080 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001081 {.base = mac, .len = mac_length},
1082 };
1083 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001084 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001085 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001086
Antonio de Angelis4743e672019-04-11 11:38:48 +01001087#ifdef TFM_PSA_API
1088 PSA_CONNECT(TFM_CRYPTO);
1089#endif
1090
1091 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
1092 TFM_CRYPTO_MAC_VERIFY_FINISH);
1093
1094#ifdef TFM_PSA_API
1095 PSA_CLOSE();
1096#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001097
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001098 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001099#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001100}
1101
1102psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
1103{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001104#if (TFM_CRYPTO_MAC_MODULE_DISABLED != 0)
1105 return PSA_ERROR_NOT_SUPPORTED;
1106#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001107 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001108 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001109 .sfn_id = TFM_CRYPTO_MAC_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001110 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001111 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001112
Antonio de Angelis4743e672019-04-11 11:38:48 +01001113 psa_invec in_vec[] = {
1114 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1115 };
1116 psa_outvec out_vec[] = {
1117 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1118 };
1119
1120#ifdef TFM_PSA_API
1121 PSA_CONNECT(TFM_CRYPTO);
1122#endif
1123
1124 status = API_DISPATCH(tfm_crypto_mac_abort,
1125 TFM_CRYPTO_MAC_ABORT);
1126#ifdef TFM_PSA_API
1127 PSA_CLOSE();
1128#endif
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001129
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001130 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001131#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001132}
Antonio de Angelis3a480992018-11-07 11:53:28 +00001133
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001134psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001135 psa_algorithm_t alg,
1136 const uint8_t *nonce,
1137 size_t nonce_length,
1138 const uint8_t *additional_data,
1139 size_t additional_data_length,
1140 const uint8_t *plaintext,
1141 size_t plaintext_length,
1142 uint8_t *ciphertext,
1143 size_t ciphertext_size,
1144 size_t *ciphertext_length)
1145{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001146#if (TFM_CRYPTO_AEAD_MODULE_DISABLED != 0)
1147 return PSA_ERROR_NOT_SUPPORTED;
1148#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001149 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001150 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001151 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001152 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001153 .alg = alg,
1154 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001155 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001156
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001157 /* Sanitize the optional input */
1158 if ((additional_data == NULL) && (additional_data_length != 0)) {
1159 return PSA_ERROR_INVALID_ARGUMENT;
1160 }
1161
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001162 size_t idx = 0;
1163 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001164 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001165 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001166 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001167 };
1168 psa_outvec out_vec[] = {
1169 {.base = ciphertext, .len = ciphertext_size},
1170 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001171
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001172 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1173 return PSA_ERROR_INVALID_ARGUMENT;
1174 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001175
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001176 if (nonce != NULL) {
1177 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001178 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001179 }
1180 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001181
Antonio de Angelis4743e672019-04-11 11:38:48 +01001182#ifdef TFM_PSA_API
1183 PSA_CONNECT(TFM_CRYPTO);
1184#endif
1185
1186#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001187 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001188 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001189 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001190 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001191 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001192 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001193#else
1194 status = API_DISPATCH(tfm_crypto_aead_encrypt,
1195 TFM_CRYPTO_AEAD_ENCRYPT);
1196#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001197
1198 *ciphertext_length = out_vec[0].len;
1199
Antonio de Angelis4743e672019-04-11 11:38:48 +01001200#ifdef TFM_PSA_API
1201 PSA_CLOSE();
1202#endif
1203
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001204 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001205#endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */
Antonio de Angelis3a480992018-11-07 11:53:28 +00001206}
1207
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001208psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis3a480992018-11-07 11:53:28 +00001209 psa_algorithm_t alg,
1210 const uint8_t *nonce,
1211 size_t nonce_length,
1212 const uint8_t *additional_data,
1213 size_t additional_data_length,
1214 const uint8_t *ciphertext,
1215 size_t ciphertext_length,
1216 uint8_t *plaintext,
1217 size_t plaintext_size,
1218 size_t *plaintext_length)
1219{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001220#if (TFM_CRYPTO_AEAD_MODULE_DISABLED != 0)
1221 return PSA_ERROR_NOT_SUPPORTED;
1222#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001223 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001224 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001225 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001226 .key_handle = handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +01001227 .alg = alg,
1228 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001229 };
Antonio de Angelis4743e672019-04-11 11:38:48 +01001230
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001231 /* Sanitize the optional input */
1232 if ((additional_data == NULL) && (additional_data_length != 0)) {
1233 return PSA_ERROR_INVALID_ARGUMENT;
1234 }
1235
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001236 size_t idx = 0;
1237 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001238 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001239 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +01001240 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001241 };
1242 psa_outvec out_vec[] = {
1243 {.base = plaintext, .len = plaintext_size},
1244 };
Antonio de Angelis3a480992018-11-07 11:53:28 +00001245
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001246 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
1247 return PSA_ERROR_INVALID_ARGUMENT;
1248 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001249
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001250 if (nonce != NULL) {
1251 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +01001252 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001253 }
1254 }
Antonio de Angelis3a480992018-11-07 11:53:28 +00001255
Antonio de Angelis4743e672019-04-11 11:38:48 +01001256#ifdef TFM_PSA_API
1257 PSA_CONNECT(TFM_CRYPTO);
1258#endif
1259
1260#ifdef TFM_PSA_API
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001261 size_t in_len = ARRAY_SIZE(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +01001262 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001263 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +01001264 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001265 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001266 out_vec, ARRAY_SIZE(out_vec));
Antonio de Angelis4743e672019-04-11 11:38:48 +01001267#else
1268 status = API_DISPATCH(tfm_crypto_aead_decrypt,
1269 TFM_CRYPTO_AEAD_DECRYPT);
1270#endif
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001271
1272 *plaintext_length = out_vec[0].len;
1273
Antonio de Angelis4743e672019-04-11 11:38:48 +01001274#ifdef TFM_PSA_API
1275 PSA_CLOSE();
1276#endif
1277
Antonio de Angelisab85ccd2019-03-25 15:14:29 +00001278 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001279#endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */
Antonio de Angelis3a480992018-11-07 11:53:28 +00001280}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001281
1282psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
1283 psa_algorithm_t alg,
1284 const uint8_t *hash,
1285 size_t hash_length,
1286 uint8_t *signature,
1287 size_t signature_size,
1288 size_t *signature_length)
1289{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001290#if (TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED != 0)
1291 return PSA_ERROR_NOT_SUPPORTED;
1292#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001293 psa_status_t status;
1294 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001295 .sfn_id = TFM_CRYPTO_ASYMMETRIC_SIGN_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001296 .key_handle = handle,
1297 .alg = alg,
1298 };
1299
1300 psa_invec in_vec[] = {
1301 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1302 {.base = hash, .len = hash_length},
1303 };
1304 psa_outvec out_vec[] = {
1305 {.base = signature, .len = signature_size},
1306 };
1307
1308#ifdef TFM_PSA_API
1309 PSA_CONNECT(TFM_CRYPTO);
1310#endif
1311
1312 status = API_DISPATCH(tfm_crypto_asymmetric_sign,
1313 TFM_CRYPTO_ASYMMETRIC_SIGN);
1314
1315 *signature_length = out_vec[0].len;
1316
1317#ifdef TFM_PSA_API
1318 PSA_CLOSE();
1319#endif
1320
1321 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001322#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001323}
1324
1325psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
1326 psa_algorithm_t alg,
1327 const uint8_t *hash,
1328 size_t hash_length,
1329 const uint8_t *signature,
1330 size_t signature_length)
1331{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001332#if (TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED != 0)
1333 return PSA_ERROR_NOT_SUPPORTED;
1334#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001335 psa_status_t status;
1336 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001337 .sfn_id = TFM_CRYPTO_ASYMMETRIC_VERIFY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001338 .key_handle = handle,
1339 .alg = alg
1340 };
1341
1342 psa_invec in_vec[] = {
1343 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1344 {.base = hash, .len = hash_length},
1345 {.base = signature, .len = signature_length}
1346 };
1347
1348#ifdef TFM_PSA_API
1349 PSA_CONNECT(TFM_CRYPTO);
1350#endif
1351
1352 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_asymmetric_verify,
1353 TFM_CRYPTO_ASYMMETRIC_VERIFY);
1354#ifdef TFM_PSA_API
1355 PSA_CLOSE();
1356#endif
1357
1358 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001359#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001360}
1361
1362psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
1363 psa_algorithm_t alg,
1364 const uint8_t *input,
1365 size_t input_length,
1366 const uint8_t *salt,
1367 size_t salt_length,
1368 uint8_t *output,
1369 size_t output_size,
1370 size_t *output_length)
1371{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001372#if (TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED != 0)
1373 return PSA_ERROR_NOT_SUPPORTED;
1374#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001375 psa_status_t status;
1376 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001377 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001378 .key_handle = handle,
1379 .alg = alg
1380 };
1381
1382 /* Sanitize the optional input */
1383 if ((salt == NULL) && (salt_length != 0)) {
1384 return PSA_ERROR_INVALID_ARGUMENT;
1385 }
1386
1387 psa_invec in_vec[] = {
1388 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1389 {.base = input, .len = input_length},
1390 {.base = salt, .len = salt_length}
1391 };
1392
1393 psa_outvec out_vec[] = {
1394 {.base = output, .len = output_size},
1395 };
1396
1397#ifdef TFM_PSA_API
1398 PSA_CONNECT(TFM_CRYPTO);
1399#endif
1400
1401#ifdef TFM_PSA_API
1402 size_t in_len = ARRAY_SIZE(in_vec);
1403 if (salt == NULL) {
1404 in_len--;
1405 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001406 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001407 out_vec, ARRAY_SIZE(out_vec));
1408#else
1409 status = API_DISPATCH(tfm_crypto_asymmetric_encrypt,
1410 TFM_CRYPTO_ASYMMETRIC_ENCRYPT);
1411#endif
1412
1413 *output_length = out_vec[0].len;
1414
1415#ifdef TFM_PSA_API
1416 PSA_CLOSE();
1417#endif
1418
1419 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001420#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001421}
1422
1423psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
1424 psa_algorithm_t alg,
1425 const uint8_t *input,
1426 size_t input_length,
1427 const uint8_t *salt,
1428 size_t salt_length,
1429 uint8_t *output,
1430 size_t output_size,
1431 size_t *output_length)
1432{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001433#if (TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED != 0)
1434 return PSA_ERROR_NOT_SUPPORTED;
1435#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001436 psa_status_t status;
1437 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001438 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001439 .key_handle = handle,
1440 .alg = alg
1441 };
1442
1443 /* Sanitize the optional input */
1444 if ((salt == NULL) && (salt_length != 0)) {
1445 return PSA_ERROR_INVALID_ARGUMENT;
1446 }
1447
1448 psa_invec in_vec[] = {
1449 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1450 {.base = input, .len = input_length},
1451 {.base = salt, .len = salt_length}
1452 };
1453
1454 psa_outvec out_vec[] = {
1455 {.base = output, .len = output_size},
1456 };
1457
1458#ifdef TFM_PSA_API
1459 PSA_CONNECT(TFM_CRYPTO);
1460#endif
1461
1462#ifdef TFM_PSA_API
1463 size_t in_len = ARRAY_SIZE(in_vec);
1464 if (salt == NULL) {
1465 in_len--;
1466 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001467 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001468 out_vec, ARRAY_SIZE(out_vec));
1469#else
1470 status = API_DISPATCH(tfm_crypto_asymmetric_decrypt,
1471 TFM_CRYPTO_ASYMMETRIC_DECRYPT);
1472#endif
1473
1474 *output_length = out_vec[0].len;
1475
1476#ifdef TFM_PSA_API
1477 PSA_CLOSE();
1478#endif
1479
1480 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001481#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001482}
1483
1484psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1485 size_t *capacity)
1486{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001487#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1488 return PSA_ERROR_NOT_SUPPORTED;
1489#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001490 psa_status_t status;
1491 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001492 .sfn_id = TFM_CRYPTO_GET_GENERATOR_CAPACITY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001493 .op_handle = generator->handle,
1494 };
1495
1496 psa_invec in_vec[] = {
1497 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1498 };
1499
1500 psa_outvec out_vec[] = {
1501 {.base = capacity, .len = sizeof(size_t)},
1502 };
1503
1504#ifdef TFM_PSA_API
1505 PSA_CONNECT(TFM_CRYPTO);
1506#endif
1507
1508 status = API_DISPATCH(tfm_crypto_get_generator_capacity,
1509 TFM_CRYPTO_GET_GENERATOR_CAPACITY);
1510#ifdef TFM_PSA_API
1511 PSA_CLOSE();
1512#endif
1513
1514 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001515#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001516}
1517
1518psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1519 uint8_t *output,
1520 size_t output_length)
1521{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001522#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1523 return PSA_ERROR_NOT_SUPPORTED;
1524#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001525 psa_status_t status;
1526 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001527 .sfn_id = TFM_CRYPTO_GENERATOR_READ_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001528 .op_handle = generator->handle,
1529 };
1530
1531 psa_invec in_vec[] = {
1532 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1533 };
1534
1535 psa_outvec out_vec[] = {
1536 {.base = output, .len = output_length},
1537 };
1538
1539#ifdef TFM_PSA_API
1540 PSA_CONNECT(TFM_CRYPTO);
1541#endif
1542
1543 status = API_DISPATCH(tfm_crypto_generator_read,
1544 TFM_CRYPTO_GENERATOR_READ);
1545#ifdef TFM_PSA_API
1546 PSA_CLOSE();
1547#endif
1548
1549 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001550#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001551}
1552
1553psa_status_t psa_generator_import_key(psa_key_handle_t handle,
1554 psa_key_type_t type,
1555 size_t bits,
1556 psa_crypto_generator_t *generator)
1557{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001558#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1559 return PSA_ERROR_NOT_SUPPORTED;
1560#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001561 psa_status_t status;
1562 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001563 .sfn_id = TFM_CRYPTO_GENERATOR_IMPORT_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001564 .key_handle = handle,
1565 .type = type,
1566 .op_handle = generator->handle,
1567 };
1568
1569 psa_invec in_vec[] = {
1570 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1571 {.base = &bits, .len = sizeof(size_t)},
1572 };
1573
1574#ifdef TFM_PSA_API
1575 PSA_CONNECT(TFM_CRYPTO);
1576#endif
1577
1578 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generator_import_key,
1579 TFM_CRYPTO_GENERATOR_IMPORT_KEY);
1580#ifdef TFM_PSA_API
1581 PSA_CLOSE();
1582#endif
1583
1584 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001585#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001586}
1587
1588psa_status_t psa_generator_abort(psa_crypto_generator_t *generator)
1589{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001590#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1591 return PSA_ERROR_NOT_SUPPORTED;
1592#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001593 psa_status_t status;
1594 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001595 .sfn_id = TFM_CRYPTO_GENERATOR_ABORT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001596 .op_handle = generator->handle,
1597 };
1598
1599 psa_invec in_vec[] = {
1600 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1601 };
1602
1603 psa_outvec out_vec[] = {
1604 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1605 };
1606
1607#ifdef TFM_PSA_API
1608 PSA_CONNECT(TFM_CRYPTO);
1609#endif
1610
1611 status = API_DISPATCH(tfm_crypto_generator_abort,
1612 TFM_CRYPTO_GENERATOR_ABORT);
1613#ifdef TFM_PSA_API
1614 PSA_CLOSE();
1615#endif
1616
1617 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001618#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001619}
1620
1621psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
1622 psa_key_handle_t handle,
1623 psa_algorithm_t alg,
1624 const uint8_t *salt,
1625 size_t salt_length,
1626 const uint8_t *label,
1627 size_t label_length,
1628 size_t capacity)
1629{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001630#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1631 return PSA_ERROR_NOT_SUPPORTED;
1632#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001633 psa_status_t status;
1634 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001635 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001636 .key_handle = handle,
1637 .alg = alg,
1638 .op_handle = generator->handle,
1639 .capacity = capacity,
1640 };
1641
1642 /* Sanitize the optional input */
1643 if ((salt == NULL) && (salt_length != 0)) {
1644 return PSA_ERROR_INVALID_ARGUMENT;
1645 }
1646
1647 if ((label == NULL) && (label_length != 0)) {
1648 return PSA_ERROR_INVALID_ARGUMENT;
1649 }
1650
1651 psa_invec in_vec[] = {
1652 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1653 {.base = salt, .len = salt_length},
1654 {.base = label, .len = label_length},
1655 };
1656
1657 psa_outvec out_vec[] = {
1658 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1659 };
1660
1661#ifdef TFM_PSA_API
1662 PSA_CONNECT(TFM_CRYPTO);
1663#endif
1664
1665#ifdef TFM_PSA_API
1666 size_t in_len = ARRAY_SIZE(in_vec);
1667 if (label == NULL) {
1668 in_len--;
1669 if (salt == NULL) {
1670 in_len--;
1671 }
1672 }
Summer Qin4b1d03b2019-07-02 14:56:08 +08001673 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001674 out_vec, ARRAY_SIZE(out_vec));
1675#else
1676 status = API_DISPATCH(tfm_crypto_key_derivation,
1677 TFM_CRYPTO_KEY_DERIVATION);
1678#endif
1679
1680#ifdef TFM_PSA_API
1681 PSA_CLOSE();
1682#endif
1683
1684 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001685#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001686}
1687
1688psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
1689 psa_key_handle_t private_key,
1690 const uint8_t *peer_key,
1691 size_t peer_key_length,
1692 psa_algorithm_t alg)
1693{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001694#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1695 return PSA_ERROR_NOT_SUPPORTED;
1696#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001697 psa_status_t status;
1698 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001699 .sfn_id = TFM_CRYPTO_KEY_AGREEMENT_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001700 .key_handle = private_key,
1701 .alg = alg,
1702 .op_handle = generator->handle,
1703 };
1704
1705 psa_invec in_vec[] = {
1706 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1707 {.base = peer_key, .len = peer_key_length},
1708 };
1709
1710 psa_outvec out_vec[] = {
1711 {.base = &(generator->handle), .len = sizeof(uint32_t)},
1712 };
1713
1714#ifdef TFM_PSA_API
1715 PSA_CONNECT(TFM_CRYPTO);
1716#endif
1717
1718 status = API_DISPATCH(tfm_crypto_key_agreement,
1719 TFM_CRYPTO_KEY_AGREEMENT);
1720
1721#ifdef TFM_PSA_API
1722 PSA_CLOSE();
1723#endif
1724
1725 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001726#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001727}
1728
1729psa_status_t psa_generate_random(uint8_t *output,
1730 size_t output_size)
1731{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001732#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1733 return PSA_ERROR_NOT_SUPPORTED;
1734#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001735 psa_status_t status;
1736 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001737 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001738 };
1739
1740 psa_invec in_vec[] = {
1741 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1742 };
1743
1744 psa_outvec out_vec[] = {
1745 {.base = output, .len = output_size},
1746 };
1747
1748 if (output_size == 0) {
1749 return PSA_SUCCESS;
1750 }
1751
1752#ifdef TFM_PSA_API
1753 PSA_CONNECT(TFM_CRYPTO);
1754#endif
1755
1756 status = API_DISPATCH(tfm_crypto_generate_random,
1757 TFM_CRYPTO_GENERATE_RANDOM);
1758
1759#ifdef TFM_PSA_API
1760 PSA_CLOSE();
1761#endif
1762
1763 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001764#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001765}
1766
1767psa_status_t psa_generate_key(psa_key_handle_t handle,
1768 psa_key_type_t type,
1769 size_t bits,
1770 const void *extra,
1771 size_t extra_size)
1772{
Antonio de Angelis7740b382019-07-16 10:59:25 +01001773#if (TFM_CRYPTO_GENERATOR_MODULE_DISABLED != 0)
1774 return PSA_ERROR_NOT_SUPPORTED;
1775#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001776 psa_status_t status;
1777 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001778 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001779 .key_handle = handle,
1780 .type = type,
1781 };
1782
1783 /* Sanitize the optional input */
1784 if ((extra == NULL) && (extra_size != 0)) {
1785 return PSA_ERROR_INVALID_ARGUMENT;
1786 }
1787
1788 psa_invec in_vec[] = {
1789 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1790 {.base = &bits, .len = sizeof(size_t)},
1791 {.base = extra, .len = extra_size},
1792 };
1793
1794#ifdef TFM_PSA_API
1795 PSA_CONNECT(TFM_CRYPTO);
1796#endif
1797
1798#ifdef TFM_PSA_API
1799 size_t in_len = ARRAY_SIZE(in_vec);
1800 if (extra == NULL) {
1801 in_len--;
1802 }
1803
Summer Qin4b1d03b2019-07-02 14:56:08 +08001804 status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len, NULL, 0);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001805#else
1806 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_generate_key,
1807 TFM_CRYPTO_GENERATE_KEY);
1808#endif
1809
1810#ifdef TFM_PSA_API
1811 PSA_CLOSE();
1812#endif
1813
1814 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +01001815#endif /* TFM_CRYPTO_GENERATOR_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001816}