blob: 84048994df4c6fc275a1b327bb6943de7af18c31 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, 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_crypto_defs.h"
Jamie Foxcc31d402019-01-28 17:13:52 +00009#include "psa/crypto.h"
Antonio de Angelis05b24192019-07-04 15:28:46 +010010#include "tfm_ns_interface.h"
Edison Aicc4c6162019-06-21 13:52:49 +080011#include "psa_manifest/sid.h"
Kevin Peng9449a362019-07-29 16:05:42 +080012#include "psa/client.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000013
Jamie Fox0e54ebc2019-04-09 14:21:04 +010014#define API_DISPATCH(sfn_name, sfn_id) \
Summer Qinaee07882021-03-29 15:44:27 +080015 psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, \
Xinyu Zhangade2e0a2021-03-18 16:20:54 +080016 in_vec, IOVEC_LEN(in_vec), \
17 out_vec, IOVEC_LEN(out_vec))
Antonio de Angelis4743e672019-04-11 11:38:48 +010018
Jamie Fox0e54ebc2019-04-09 14:21:04 +010019#define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id) \
Summer Qinaee07882021-03-29 15:44:27 +080020 psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, \
Xinyu Zhangade2e0a2021-03-18 16:20:54 +080021 in_vec, IOVEC_LEN(in_vec), \
Antonio de Angelis4743e672019-04-11 11:38:48 +010022 (psa_outvec *)NULL, 0)
Antonio de Angelis8908f472018-08-31 15:44:25 +010023
24psa_status_t psa_crypto_init(void)
25{
26 /* Service init is performed during TFM boot up,
27 * so application level initialisation is empty
28 */
29 return PSA_SUCCESS;
30}
31
Antonio de Angelis04debbd2019-10-14 12:12:52 +010032psa_status_t psa_open_key(psa_key_id_t id,
Maulik Patel28659c42021-01-06 14:09:22 +000033 psa_key_id_t *key)
Jamie Fox0e54ebc2019-04-09 14:21:04 +010034{
Jamie Foxdadb4e82019-09-03 17:59:41 +010035 psa_status_t status;
36 const struct tfm_crypto_pack_iovec iov = {
37 .sfn_id = TFM_CRYPTO_OPEN_KEY_SID,
Jamie Foxdadb4e82019-09-03 17:59:41 +010038 };
39 psa_invec in_vec[] = {
40 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
41 {.base = &id, .len = sizeof(psa_key_id_t)},
42 };
43 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +000044 {.base = key, .len = sizeof(psa_key_id_t)},
Jamie Foxdadb4e82019-09-03 17:59:41 +010045 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +010046
Jamie Foxdadb4e82019-09-03 17:59:41 +010047 status = API_DISPATCH(tfm_crypto_open_key,
48 TFM_CRYPTO_OPEN_KEY);
49
Jamie Foxdadb4e82019-09-03 17:59:41 +010050 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010051}
52
Maulik Patel28659c42021-01-06 14:09:22 +000053psa_status_t psa_close_key(psa_key_id_t key)
Jamie Fox0e54ebc2019-04-09 14:21:04 +010054{
Jamie Foxdadb4e82019-09-03 17:59:41 +010055 psa_status_t status;
56 const struct tfm_crypto_pack_iovec iov = {
57 .sfn_id = TFM_CRYPTO_CLOSE_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +000058 .key_id = key,
Jamie Foxdadb4e82019-09-03 17:59:41 +010059 };
60 psa_invec in_vec[] = {
61 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
62 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +010063
Jamie Foxdadb4e82019-09-03 17:59:41 +010064 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_close_key,
65 TFM_CRYPTO_CLOSE_KEY);;
66
Jamie Foxdadb4e82019-09-03 17:59:41 +010067 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010068}
69
Antonio de Angelis04debbd2019-10-14 12:12:52 +010070psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Antonio de Angelis8908f472018-08-31 15:44:25 +010071 const uint8_t *data,
Antonio de Angelis04debbd2019-10-14 12:12:52 +010072 size_t data_length,
Maulik Patel28659c42021-01-06 14:09:22 +000073 psa_key_id_t *key)
Antonio de Angelis8908f472018-08-31 15:44:25 +010074{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000075 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010076 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080077 .sfn_id = TFM_CRYPTO_IMPORT_KEY_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +010078 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000079 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +010080 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis04debbd2019-10-14 12:12:52 +010081 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000082 {.base = data, .len = data_length}
83 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +010084 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +000085 {.base = key, .len = sizeof(psa_key_id_t)}
Antonio de Angelis04debbd2019-10-14 12:12:52 +010086 };
Antonio de Angelis8908f472018-08-31 15:44:25 +010087
Antonio de Angelis04debbd2019-10-14 12:12:52 +010088 status = API_DISPATCH(tfm_crypto_import_key,
89 TFM_CRYPTO_IMPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +010090
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +010092}
93
Maulik Patel28659c42021-01-06 14:09:22 +000094psa_status_t psa_destroy_key(psa_key_id_t key)
Antonio de Angelis8908f472018-08-31 15:44:25 +010095{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000096 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010097 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +080098 .sfn_id = TFM_CRYPTO_DESTROY_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +000099 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100100 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000101 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100102 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000103 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100104
Antonio de Angelis4743e672019-04-11 11:38:48 +0100105 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_destroy_key,
106 TFM_CRYPTO_DESTROY_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100107
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000108 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100109}
110
Maulik Patel28659c42021-01-06 14:09:22 +0000111psa_status_t psa_get_key_attributes(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100112 psa_key_attributes_t *attributes)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100113{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100115 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100116 .sfn_id = TFM_CRYPTO_GET_KEY_ATTRIBUTES_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000117 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100118 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000119 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100120 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000121 };
122 psa_outvec out_vec[] = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100123 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000124 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100125
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100126 status = API_DISPATCH(tfm_crypto_get_key_attributes,
127 TFM_CRYPTO_GET_KEY_ATTRIBUTES);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000128 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100129}
130
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100131void psa_reset_key_attributes(psa_key_attributes_t *attributes)
132{
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100133 struct tfm_crypto_pack_iovec iov = {
134 .sfn_id = TFM_CRYPTO_RESET_KEY_ATTRIBUTES_SID,
135 };
136 psa_invec in_vec[] = {
137 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
138 };
139 psa_outvec out_vec[] = {
140 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
141 };
142
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100143 (void)API_DISPATCH(tfm_crypto_reset_key_attributes,
Summer Qinaee07882021-03-29 15:44:27 +0800144 TFM_CRYPTO_RESET_KEY_ATTRIBUTES);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100145 return;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100146}
147
Maulik Patel28659c42021-01-06 14:09:22 +0000148psa_status_t psa_export_key(psa_key_id_t key,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100149 uint8_t *data,
150 size_t data_size,
151 size_t *data_length)
152{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000153 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100154 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800155 .sfn_id = TFM_CRYPTO_EXPORT_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000156 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100157 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000158 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100159 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000160 };
161 psa_outvec out_vec[] = {
162 {.base = data, .len = data_size}
163 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100164
Antonio de Angelis4743e672019-04-11 11:38:48 +0100165 status = API_DISPATCH(tfm_crypto_export_key,
166 TFM_CRYPTO_EXPORT_KEY);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100167
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000168 *data_length = out_vec[0].len;
169
170 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100171}
172
Maulik Patel28659c42021-01-06 14:09:22 +0000173psa_status_t psa_export_public_key(psa_key_id_t key,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174 uint8_t *data,
175 size_t data_size,
176 size_t *data_length)
177{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100178 psa_status_t status;
179 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800180 .sfn_id = TFM_CRYPTO_EXPORT_PUBLIC_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000181 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100182 };
Hugues de Valon8b442442019-02-19 14:30:52 +0000183
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100184 psa_invec in_vec[] = {
185 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
186 };
187 psa_outvec out_vec[] = {
188 {.base = data, .len = data_size}
189 };
190
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100191 status = API_DISPATCH(tfm_crypto_export_public_key,
192 TFM_CRYPTO_EXPORT_PUBLIC_KEY);
193
194 *data_length = out_vec[0].len;
195
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100196 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100197}
198
Maulik Patel28659c42021-01-06 14:09:22 +0000199psa_status_t psa_purge_key(psa_key_id_t key)
200{
201 psa_status_t status;
202 struct tfm_crypto_pack_iovec iov = {
203 .sfn_id = TFM_CRYPTO_PURGE_KEY_SID,
204 .key_id = key,
205 };
206 psa_invec in_vec[] = {
207 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
208 };
209
Maulik Patel28659c42021-01-06 14:09:22 +0000210 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_purge_key,
211 TFM_CRYPTO_PURGE_KEY);
Maulik Patel28659c42021-01-06 14:09:22 +0000212 return status;
213}
214
215psa_status_t psa_copy_key(psa_key_id_t source_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100216 const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000217 psa_key_id_t *target_key)
Jamie Foxefd82732018-11-26 10:34:32 +0000218{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100219 psa_status_t status;
220 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800221 .sfn_id = TFM_CRYPTO_COPY_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000222 .key_id = source_key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100223 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100225 psa_invec in_vec[] = {
226 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100227 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100228 };
229
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000230 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +0000231 {.base = target_key, .len = sizeof(psa_key_id_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000232 };
Jamie Foxefd82732018-11-26 10:34:32 +0000233
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100234 status = API_DISPATCH(tfm_crypto_copy_key,
235 TFM_CRYPTO_COPY_KEY);
Kevin Peng9449a362019-07-29 16:05:42 +0800236
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000237 return status;
Jamie Foxefd82732018-11-26 10:34:32 +0000238}
239
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100240psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
241 unsigned char *iv,
242 size_t iv_size,
243 size_t *iv_length)
244{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100245 psa_status_t status;
246 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800247 .sfn_id = TFM_CRYPTO_CIPHER_GENERATE_IV_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100248 .op_handle = operation->handle,
249 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100250
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100251 psa_invec in_vec[] = {
252 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
253 };
254 psa_outvec out_vec[] = {
255 {.base = &(operation->handle), .len = sizeof(uint32_t)},
256 {.base = iv, .len = iv_size},
257 };
258
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100259 status = API_DISPATCH(tfm_crypto_cipher_generate_iv,
260 TFM_CRYPTO_CIPHER_GENERATE_IV);
261
262 *iv_length = out_vec[1].len;
263
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100264 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100265}
266
Antonio de Angelis377a1552018-11-22 17:02:40 +0000267psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
268 const unsigned char *iv,
269 size_t iv_length)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100270{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000271 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100272 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800273 .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100274 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100275 };
276
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000277 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100278 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000279 {.base = iv, .len = iv_length},
280 };
281 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100282 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000283 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100284
Antonio de Angelis4743e672019-04-11 11:38:48 +0100285 status = API_DISPATCH(tfm_crypto_cipher_set_iv,
286 TFM_CRYPTO_CIPHER_SET_IV);
Kevin Peng9449a362019-07-29 16:05:42 +0800287
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000288 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100289}
290
Antonio de Angelis377a1552018-11-22 17:02:40 +0000291psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +0000292 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000293 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100294{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000295 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100296 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800297 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000298 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100299 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100300 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000301 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100302
Antonio de Angelis4743e672019-04-11 11:38:48 +0100303 psa_invec in_vec[] = {
304 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
305 };
306 psa_outvec out_vec[] = {
307 {.base = &(operation->handle), .len = sizeof(uint32_t)},
308 };
309
Antonio de Angelis4743e672019-04-11 11:38:48 +0100310 status = API_DISPATCH(tfm_crypto_cipher_encrypt_setup,
311 TFM_CRYPTO_CIPHER_ENCRYPT_SETUP);
Kevin Peng9449a362019-07-29 16:05:42 +0800312
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000313 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100314}
315
Antonio de Angelis377a1552018-11-22 17:02:40 +0000316psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +0000317 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000318 psa_algorithm_t alg)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100319{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000320 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100321 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800322 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000323 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100324 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100325 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000326 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100327
Antonio de Angelis4743e672019-04-11 11:38:48 +0100328 psa_invec in_vec[] = {
329 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
330 };
331 psa_outvec out_vec[] = {
332 {.base = &(operation->handle), .len = sizeof(uint32_t)},
333 };
334
Antonio de Angelis4743e672019-04-11 11:38:48 +0100335 status = API_DISPATCH(tfm_crypto_cipher_decrypt_setup,
336 TFM_CRYPTO_CIPHER_DECRYPT_SETUP);
Kevin Peng9449a362019-07-29 16:05:42 +0800337
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000338 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100339}
340
341psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
342 const uint8_t *input,
343 size_t input_length,
344 unsigned char *output,
345 size_t output_size,
346 size_t *output_length)
347{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000348 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100349 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800350 .sfn_id = TFM_CRYPTO_CIPHER_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100351 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100352 };
353
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000354 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100355 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000356 {.base = input, .len = input_length},
357 };
358 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100359 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000360 {.base = output, .len = output_size}
361 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100362
Antonio de Angelis4743e672019-04-11 11:38:48 +0100363 status = API_DISPATCH(tfm_crypto_cipher_update,
364 TFM_CRYPTO_CIPHER_UPDATE);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100365
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000366 *output_length = out_vec[1].len;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100367
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000368 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100369}
370
371psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
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_CIPHER_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100376 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000377 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100378
Antonio de Angelis4743e672019-04-11 11:38:48 +0100379 psa_invec in_vec[] = {
380 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
381 };
382 psa_outvec out_vec[] = {
383 {.base = &(operation->handle), .len = sizeof(uint32_t)},
384 };
385
Antonio de Angelis4743e672019-04-11 11:38:48 +0100386 status = API_DISPATCH(tfm_crypto_cipher_abort,
387 TFM_CRYPTO_CIPHER_ABORT);
Kevin Peng9449a362019-07-29 16:05:42 +0800388
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000389 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100390}
391
392psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
393 uint8_t *output,
394 size_t output_size,
395 size_t *output_length)
396{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000397 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100398 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800399 .sfn_id = TFM_CRYPTO_CIPHER_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100400 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100401 };
402
403 psa_invec in_vec[] = {
404 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
405 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000406 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100407 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000408 {.base = output, .len = output_size},
409 };
Antonio de Angelis8908f472018-08-31 15:44:25 +0100410
Antonio de Angelis4743e672019-04-11 11:38:48 +0100411 status = API_DISPATCH(tfm_crypto_cipher_finish,
412 TFM_CRYPTO_CIPHER_FINISH);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100413
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000414 *output_length = out_vec[1].len;
415
416 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100417}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100418
Antonio de Angelis377a1552018-11-22 17:02:40 +0000419psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100420 psa_algorithm_t alg)
421{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000422 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100423 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800424 .sfn_id = TFM_CRYPTO_HASH_SETUP_SID,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100425 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100426 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000427 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100428
Antonio de Angelis4743e672019-04-11 11:38:48 +0100429 psa_invec in_vec[] = {
430 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
431 };
432 psa_outvec out_vec[] = {
433 {.base = &(operation->handle), .len = sizeof(uint32_t)},
434 };
435
Antonio de Angelis4743e672019-04-11 11:38:48 +0100436 status = API_DISPATCH(tfm_crypto_hash_setup,
437 TFM_CRYPTO_HASH_SETUP);
438
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000439 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100440}
441
442psa_status_t psa_hash_update(psa_hash_operation_t *operation,
443 const uint8_t *input,
444 size_t input_length)
445{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000446 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100447 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800448 .sfn_id = TFM_CRYPTO_HASH_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100449 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100450 };
451
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000452 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100453 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000454 {.base = input, .len = input_length},
455 };
456 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100457 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000458 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100459
Antonio de Angelis4743e672019-04-11 11:38:48 +0100460 status = API_DISPATCH(tfm_crypto_hash_update,
461 TFM_CRYPTO_HASH_UPDATE);
462
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000463 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100464}
465
466psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
467 uint8_t *hash,
468 size_t hash_size,
469 size_t *hash_length)
470{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000471 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100472 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800473 .sfn_id = TFM_CRYPTO_HASH_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100474 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100475 };
476
477 psa_invec in_vec[] = {
478 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
479 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000480 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100481 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000482 {.base = hash, .len = hash_size},
483 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100484
Antonio de Angelis4743e672019-04-11 11:38:48 +0100485 status = API_DISPATCH(tfm_crypto_hash_finish,
486 TFM_CRYPTO_HASH_FINISH);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100487
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000488 *hash_length = out_vec[1].len;
489
490 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100491}
492
493psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
494 const uint8_t *hash,
495 size_t hash_length)
496{
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_HASH_VERIFY_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 = hash, .len = hash_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 Angelisa6f72162018-09-05 11:00:37 +0100510
Antonio de Angelis4743e672019-04-11 11:38:48 +0100511 status = API_DISPATCH(tfm_crypto_hash_verify,
512 TFM_CRYPTO_HASH_VERIFY);
Kevin Peng9449a362019-07-29 16:05:42 +0800513
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000514 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100515}
516
517psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
518{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000519 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100520 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800521 .sfn_id = TFM_CRYPTO_HASH_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100522 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000523 };
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100524
Antonio de Angelis4743e672019-04-11 11:38:48 +0100525 psa_invec in_vec[] = {
526 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
527 };
528 psa_outvec out_vec[] = {
529 {.base = &(operation->handle), .len = sizeof(uint32_t)},
530 };
531
Antonio de Angelis4743e672019-04-11 11:38:48 +0100532 status = API_DISPATCH(tfm_crypto_hash_abort,
533 TFM_CRYPTO_HASH_ABORT);
Kevin Peng9449a362019-07-29 16:05:42 +0800534
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000535 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100536}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100537
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100538psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
539 psa_hash_operation_t *target_operation)
540{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100541 psa_status_t status;
542 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800543 .sfn_id = TFM_CRYPTO_HASH_CLONE_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100544 .op_handle = source_operation->handle,
545 };
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100546
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100547 psa_invec in_vec[] = {
548 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
549 };
550 psa_outvec out_vec[] = {
551 {.base = target_operation, .len = sizeof(psa_hash_operation_t)},
552 };
553
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100554 if (target_operation && (target_operation->handle != 0)) {
555 return PSA_ERROR_BAD_STATE;
556 }
557
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100558 status = API_DISPATCH(tfm_crypto_hash_clone,
559 TFM_CRYPTO_HASH_CLONE);
Kevin Peng9449a362019-07-29 16:05:42 +0800560
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100561 return status;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100562}
563
Soby Mathew07ef6e42020-07-20 21:09:23 +0100564psa_status_t psa_hash_compute(psa_algorithm_t alg,
565 const uint8_t *input,
566 size_t input_length,
567 uint8_t *hash,
568 size_t hash_size,
569 size_t *hash_length)
570{
Soby Mathew07ef6e42020-07-20 21:09:23 +0100571 psa_status_t status;
572 struct tfm_crypto_pack_iovec iov = {
573 .sfn_id = TFM_CRYPTO_HASH_COMPUTE_SID,
574 .alg = alg,
575 };
576
577 psa_invec in_vec[] = {
578 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
579 {.base = input, .len = input_length},
580 };
581
582 psa_outvec out_vec[] = {
583 {.base = hash, .len = hash_size}
584 };
585
Soby Mathew07ef6e42020-07-20 21:09:23 +0100586 status = API_DISPATCH(tfm_crypto_hash_compute,
587 TFM_CRYPTO_HASH_COMPUTE);
588
589 *hash_length = out_vec[0].len;
590
Soby Mathew07ef6e42020-07-20 21:09:23 +0100591 return status;
Soby Mathew07ef6e42020-07-20 21:09:23 +0100592}
593
594psa_status_t psa_hash_compare(psa_algorithm_t alg,
595 const uint8_t *input,
596 size_t input_length,
597 const uint8_t *hash,
598 size_t hash_length)
599{
Soby Mathew07ef6e42020-07-20 21:09:23 +0100600 psa_status_t status;
601 struct tfm_crypto_pack_iovec iov = {
602 .sfn_id = TFM_CRYPTO_HASH_COMPARE_SID,
603 .alg = alg,
604 };
605
606 psa_invec in_vec[] = {
607 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
608 {.base = input, .len = input_length},
609 {.base = hash, .len = hash_length},
610 };
611
Soby Mathew07ef6e42020-07-20 21:09:23 +0100612 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_hash_compare,
613 TFM_CRYPTO_HASH_COMPARE);
614
Soby Mathew07ef6e42020-07-20 21:09:23 +0100615 return status;
Soby Mathew07ef6e42020-07-20 21:09:23 +0100616}
617
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100618psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +0000619 psa_key_id_t key,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100620 psa_algorithm_t alg)
621{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000622 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100623 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800624 .sfn_id = TFM_CRYPTO_MAC_SIGN_SETUP_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000625 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100626 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100627 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000628 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100629
Antonio de Angelis4743e672019-04-11 11:38:48 +0100630 psa_invec in_vec[] = {
631 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
632 };
633 psa_outvec out_vec[] = {
634 {.base = &(operation->handle), .len = sizeof(uint32_t)},
635 };
636
Antonio de Angelis4743e672019-04-11 11:38:48 +0100637 status = API_DISPATCH(tfm_crypto_mac_sign_setup,
638 TFM_CRYPTO_MAC_SIGN_SETUP);
Kevin Peng9449a362019-07-29 16:05:42 +0800639
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000640 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100641}
642
643psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +0000644 psa_key_id_t key,
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100645 psa_algorithm_t alg)
646{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000647 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100648 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800649 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SETUP_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000650 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100651 .alg = alg,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100652 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000653 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100654
Antonio de Angelis4743e672019-04-11 11:38:48 +0100655 psa_invec in_vec[] = {
656 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
657 };
658 psa_outvec out_vec[] = {
659 {.base = &(operation->handle), .len = sizeof(uint32_t)},
660 };
661
Antonio de Angelis4743e672019-04-11 11:38:48 +0100662 status = API_DISPATCH(tfm_crypto_mac_verify_setup,
663 TFM_CRYPTO_MAC_VERIFY_SETUP);
Kevin Peng9449a362019-07-29 16:05:42 +0800664
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000665 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100666}
667
668psa_status_t psa_mac_update(psa_mac_operation_t *operation,
669 const uint8_t *input,
670 size_t input_length)
671{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000672 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100673 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800674 .sfn_id = TFM_CRYPTO_MAC_UPDATE_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100675 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100676 };
677
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000678 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100679 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000680 {.base = input, .len = input_length},
681 };
682 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100683 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000684 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100685
Antonio de Angelis4743e672019-04-11 11:38:48 +0100686 status = API_DISPATCH(tfm_crypto_mac_update,
687 TFM_CRYPTO_MAC_UPDATE);
Kevin Peng9449a362019-07-29 16:05:42 +0800688
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000689 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100690}
691
692psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
693 uint8_t *mac,
694 size_t mac_size,
695 size_t *mac_length)
696{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000697 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100698 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800699 .sfn_id = TFM_CRYPTO_MAC_SIGN_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100700 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100701 };
702
703 psa_invec in_vec[] = {
704 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
705 };
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000706 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100707 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000708 {.base = mac, .len = mac_size},
709 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100710
Antonio de Angelis4743e672019-04-11 11:38:48 +0100711 status = API_DISPATCH(tfm_crypto_mac_sign_finish,
712 TFM_CRYPTO_MAC_SIGN_FINISH);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100713
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000714 *mac_length = out_vec[1].len;
715
716 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100717}
718
719psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
720 const uint8_t *mac,
721 size_t mac_length)
722{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000723 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100724 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800725 .sfn_id = TFM_CRYPTO_MAC_VERIFY_FINISH_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100726 .op_handle = operation->handle,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100727 };
728
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000729 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100730 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000731 {.base = mac, .len = mac_length},
732 };
733 psa_outvec out_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100734 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000735 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100736
Antonio de Angelis4743e672019-04-11 11:38:48 +0100737 status = API_DISPATCH(tfm_crypto_mac_verify_finish,
738 TFM_CRYPTO_MAC_VERIFY_FINISH);
739
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000740 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100741}
742
743psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
744{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000745 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100746 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800747 .sfn_id = TFM_CRYPTO_MAC_ABORT_SID,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100748 .op_handle = operation->handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000749 };
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100750
Antonio de Angelis4743e672019-04-11 11:38:48 +0100751 psa_invec in_vec[] = {
752 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
753 };
754 psa_outvec out_vec[] = {
755 {.base = &(operation->handle), .len = sizeof(uint32_t)},
756 };
757
Antonio de Angelis4743e672019-04-11 11:38:48 +0100758 status = API_DISPATCH(tfm_crypto_mac_abort,
759 TFM_CRYPTO_MAC_ABORT);
Kevin Peng9449a362019-07-29 16:05:42 +0800760
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000761 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100762}
Antonio de Angelis3a480992018-11-07 11:53:28 +0000763
Maulik Patel28659c42021-01-06 14:09:22 +0000764psa_status_t psa_aead_encrypt(psa_key_id_t key,
Antonio de Angelis3a480992018-11-07 11:53:28 +0000765 psa_algorithm_t alg,
766 const uint8_t *nonce,
767 size_t nonce_length,
768 const uint8_t *additional_data,
769 size_t additional_data_length,
770 const uint8_t *plaintext,
771 size_t plaintext_length,
772 uint8_t *ciphertext,
773 size_t ciphertext_size,
774 size_t *ciphertext_length)
775{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000776 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100777 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800778 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000779 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100780 .alg = alg,
781 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000782 };
Antonio de Angelis4743e672019-04-11 11:38:48 +0100783
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100784 /* Sanitize the optional input */
785 if ((additional_data == NULL) && (additional_data_length != 0)) {
786 return PSA_ERROR_INVALID_ARGUMENT;
787 }
788
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000789 size_t idx = 0;
790 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100791 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000792 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +0100793 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000794 };
795 psa_outvec out_vec[] = {
796 {.base = ciphertext, .len = ciphertext_size},
797 };
Antonio de Angelis3a480992018-11-07 11:53:28 +0000798
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000799 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
800 return PSA_ERROR_INVALID_ARGUMENT;
801 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000802
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000803 if (nonce != NULL) {
804 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100805 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000806 }
807 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000808
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800809 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100810 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100811 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100812 }
Summer Qinaee07882021-03-29 15:44:27 +0800813 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800814 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000815
816 *ciphertext_length = out_vec[0].len;
817
818 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +0000819}
820
Maulik Patel28659c42021-01-06 14:09:22 +0000821psa_status_t psa_aead_decrypt(psa_key_id_t key,
Antonio de Angelis3a480992018-11-07 11:53:28 +0000822 psa_algorithm_t alg,
823 const uint8_t *nonce,
824 size_t nonce_length,
825 const uint8_t *additional_data,
826 size_t additional_data_length,
827 const uint8_t *ciphertext,
828 size_t ciphertext_length,
829 uint8_t *plaintext,
830 size_t plaintext_size,
831 size_t *plaintext_length)
832{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000833 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100834 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800835 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000836 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100837 .alg = alg,
838 .aead_in = {.nonce = {0}, .nonce_length = nonce_length}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000839 };
Antonio de Angelis4743e672019-04-11 11:38:48 +0100840
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100841 /* Sanitize the optional input */
842 if ((additional_data == NULL) && (additional_data_length != 0)) {
843 return PSA_ERROR_INVALID_ARGUMENT;
844 }
845
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000846 size_t idx = 0;
847 psa_invec in_vec[] = {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100848 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000849 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +0100850 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000851 };
852 psa_outvec out_vec[] = {
853 {.base = plaintext, .len = plaintext_size},
854 };
Antonio de Angelis3a480992018-11-07 11:53:28 +0000855
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000856 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
857 return PSA_ERROR_INVALID_ARGUMENT;
858 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000859
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000860 if (nonce != NULL) {
861 for (idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100862 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000863 }
864 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000865
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800866 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100867 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100868 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100869 }
Summer Qinaee07882021-03-29 15:44:27 +0800870 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800871 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000872
873 *plaintext_length = out_vec[0].len;
874
875 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +0000876}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100877
Antonio de Angelis8d282482021-10-07 15:04:12 +0100878psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
879 psa_key_id_t key,
880 psa_algorithm_t alg)
881{
882 psa_status_t status;
883 struct tfm_crypto_pack_iovec iov = {
884 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SETUP_SID,
885 .key_id = key,
886 .alg = alg,
887 .op_handle = operation->handle,
888 };
889
890 psa_invec in_vec[] = {
891 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}
892 };
893 psa_outvec out_vec[] = {
894 {.base = &(operation->handle), .len = sizeof(uint32_t)}
895 };
896
897 status = API_DISPATCH(tfm_crypto_aead_encrypt_setup,
898 TFM_CRYPTO_AEAD_ENCRYPT_SETUP);
899 return status;
900}
901
902psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
903 psa_key_id_t key,
904 psa_algorithm_t alg)
905{
906 psa_status_t status;
907 struct tfm_crypto_pack_iovec iov = {
908 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SETUP_SID,
909 .key_id = key,
910 .alg = alg,
911 .op_handle = operation->handle,
912 };
913
914 psa_invec in_vec[] = {
915 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}
916 };
917 psa_outvec out_vec[] = {
918 {.base = &(operation->handle), .len = sizeof(uint32_t)}
919 };
920
921 status = API_DISPATCH(tfm_crypto_aead_decrypt_setup,
922 TFM_CRYPTO_AEAD_DECRYPT_SETUP);
923 return status;
924}
925
926psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
927 uint8_t *nonce,
928 size_t nonce_size,
929 size_t *nonce_length)
930{
931 psa_status_t status;
932 struct tfm_crypto_pack_iovec iov = {
933 .sfn_id = TFM_CRYPTO_AEAD_GENERATE_NONCE_SID,
934 .op_handle = operation->handle,
935 };
936
937 psa_invec in_vec[] = {
938 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
939 };
940 psa_outvec out_vec[] = {
941 {.base = &(operation->handle), .len = sizeof(uint32_t)},
942 {.base = nonce, .len = nonce_size}
943 };
944
945 status = API_DISPATCH(tfm_crypto_aead_generate_nonce,
946 TFM_CRYPTO_AEAD_GENERATE_NONCE);
947
948 *nonce_length = out_vec[1].len;
949 return status;
950}
951
952psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
953 const uint8_t *nonce,
954 size_t nonce_length)
955{
956 psa_status_t status;
957 struct tfm_crypto_pack_iovec iov = {
958 .sfn_id = TFM_CRYPTO_AEAD_SET_NONCE_SID,
959 .op_handle = operation->handle,
960 };
961
962 psa_invec in_vec[] = {
963 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
964 {.base = nonce, .len = nonce_length}
965 };
966 psa_outvec out_vec[] = {
967 {.base = &(operation->handle), .len = sizeof(uint32_t)}
968 };
969
970 status = API_DISPATCH(tfm_crypto_aead_set_nonce,
971 TFM_CRYPTO_AEAD_SET_NONCE);
972 return status;
973}
974
975psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
976 size_t ad_length,
977 size_t plaintext_length)
978{
979 psa_status_t status;
980 struct tfm_crypto_pack_iovec iov = {
981 .sfn_id = TFM_CRYPTO_AEAD_SET_LENGTHS_SID,
982 .ad_length = ad_length,
983 .plaintext_length = plaintext_length,
984 .op_handle = operation->handle,
985 };
986
987 psa_invec in_vec[] = {
988 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
989 };
990 psa_outvec out_vec[] = {
991 {.base = &(operation->handle), .len = sizeof(uint32_t)}
992 };
993
994 status = API_DISPATCH(tfm_crypto_aead_set_lengths,
995 TFM_CRYPTO_AEAD_SET_LENGTHS);
996 return status;
997}
998
999psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1000 const uint8_t *input,
1001 size_t input_length)
1002{
1003 psa_status_t status;
1004 struct tfm_crypto_pack_iovec iov = {
1005 .sfn_id = TFM_CRYPTO_AEAD_UPDATE_AD_SID,
1006 .op_handle = operation->handle,
1007 };
1008
1009 psa_invec in_vec[] = {
1010 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1011 {.base = input, .len = input_length}
1012 };
1013 psa_outvec out_vec[] = {
1014 {.base = &(operation->handle), .len = sizeof(uint32_t)}
1015 };
1016
1017 status = API_DISPATCH(tfm_crypto_aead_update_ad,
1018 TFM_CRYPTO_AEAD_UPDATE_AD);
1019 return status;
1020}
1021
1022psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1023 const uint8_t *input,
1024 size_t input_length,
1025 uint8_t *output,
1026 size_t output_size,
1027 size_t *output_length)
1028{
1029 psa_status_t status;
1030 struct tfm_crypto_pack_iovec iov = {
1031 .sfn_id = TFM_CRYPTO_AEAD_UPDATE_SID,
1032 .op_handle = operation->handle,
1033 };
1034
1035 psa_invec in_vec[] = {
1036 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1037 {.base = input, .len = input_length}
1038 };
1039 psa_outvec out_vec[] = {
1040 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1041 {.base = output, .len = output_size},
1042 };
1043
1044 status = API_DISPATCH(tfm_crypto_aead_update,
1045 TFM_CRYPTO_AEAD_UPDATE);
1046
1047 *output_length = out_vec[1].len;
1048 return status;
1049}
1050
1051psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1052 uint8_t *ciphertext,
1053 size_t ciphertext_size,
1054 size_t *ciphertext_length,
1055 uint8_t *tag,
1056 size_t tag_size,
1057 size_t *tag_length)
1058{
1059 psa_status_t status;
1060 struct tfm_crypto_pack_iovec iov = {
1061 .sfn_id = TFM_CRYPTO_AEAD_FINISH_SID,
1062 .op_handle = operation->handle,
1063 };
1064
1065 psa_invec in_vec[] = {
1066 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1067 };
1068 psa_outvec out_vec[] = {
1069 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1070 {.base = ciphertext, .len = ciphertext_size},
1071 {.base = tag, .len = tag_size},
1072 };
1073
1074 status = API_DISPATCH(tfm_crypto_aead_finish,
1075 TFM_CRYPTO_AEAD_FINISH);
1076
1077 *ciphertext_length = out_vec[1].len;
1078 *tag_length = out_vec[2].len;
1079 return status;
1080}
1081
1082psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1083 uint8_t *plaintext,
1084 size_t plaintext_size,
1085 size_t *plaintext_length,
1086 const uint8_t *tag,
1087 size_t tag_length)
1088{
1089 psa_status_t status;
1090 struct tfm_crypto_pack_iovec iov = {
1091 .sfn_id = TFM_CRYPTO_AEAD_VERIFY_SID,
1092 .op_handle = operation->handle,
1093 };
1094
1095 psa_invec in_vec[] = {
1096 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1097 {.base = tag, .len = tag_length}
1098 };
1099 psa_outvec out_vec[] = {
1100 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1101 {.base = plaintext, .len = plaintext_size},
1102 };
1103
1104 status = API_DISPATCH(tfm_crypto_aead_verify,
1105 TFM_CRYPTO_AEAD_VERIFY);
1106
1107 *plaintext_length = out_vec[1].len;
1108 return status;
1109}
1110
1111psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
1112{
1113 psa_status_t status;
1114 struct tfm_crypto_pack_iovec iov = {
1115 .sfn_id = TFM_CRYPTO_AEAD_ABORT_SID,
1116 .op_handle = operation->handle,
1117 };
1118
1119 psa_invec in_vec[] = {
1120 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1121 };
1122 psa_outvec out_vec[] = {
1123 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1124 };
1125
1126 status = API_DISPATCH(tfm_crypto_aead_abort,
1127 TFM_CRYPTO_AEAD_ABORT);
1128 return status;
1129}
1130
Summer Qinb9492d22021-06-22 18:00:54 +08001131psa_status_t psa_sign_message(psa_key_id_t key,
1132 psa_algorithm_t alg,
1133 const uint8_t *input,
1134 size_t input_length,
1135 uint8_t *signature,
1136 size_t signature_size,
1137 size_t *signature_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001138{
Summer Qinb9492d22021-06-22 18:00:54 +08001139 psa_status_t status;
1140 struct tfm_crypto_pack_iovec iov = {
1141 .sfn_id = TFM_CRYPTO_SIGN_MESSAGE_SID,
1142 .key_id = key,
1143 .alg = alg,
1144 };
1145
1146 psa_invec in_vec[] = {
1147 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1148 {.base = input, .len = input_length},
1149 };
1150 psa_outvec out_vec[] = {
1151 {.base = signature, .len = signature_size},
1152 };
1153
1154 status = API_DISPATCH(tfm_crypto_sign_message,
1155 TFM_CRYPTO_SIGN_MESSAGE);
1156
Antonio de Angelisf83a2082021-08-20 22:13:53 +01001157 *signature_length = out_vec[0].len;
Summer Qinb9492d22021-06-22 18:00:54 +08001158 return status;
1159}
1160
1161psa_status_t psa_verify_message(psa_key_id_t key,
1162 psa_algorithm_t alg,
1163 const uint8_t *input,
1164 size_t input_length,
1165 const uint8_t *signature,
1166 size_t signature_length)
1167{
1168 psa_status_t status;
1169 struct tfm_crypto_pack_iovec iov = {
1170 .sfn_id = TFM_CRYPTO_VERIFY_MESSAGE_SID,
1171 .key_id = key,
1172 .alg = alg
1173 };
1174
1175 psa_invec in_vec[] = {
1176 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1177 {.base = input, .len = input_length},
1178 {.base = signature, .len = signature_length}
1179 };
1180
1181 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_verify_message,
1182 TFM_CRYPTO_VERIFY_MESSAGE);
1183
1184 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001185}
1186
Maulik Patel28659c42021-01-06 14:09:22 +00001187psa_status_t psa_sign_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001188 psa_algorithm_t alg,
1189 const uint8_t *hash,
1190 size_t hash_length,
1191 uint8_t *signature,
1192 size_t signature_size,
1193 size_t *signature_length)
1194{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001195 psa_status_t status;
1196 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001197 .sfn_id = TFM_CRYPTO_SIGN_HASH_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001198 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001199 .alg = alg,
1200 };
1201
1202 psa_invec in_vec[] = {
1203 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1204 {.base = hash, .len = hash_length},
1205 };
1206 psa_outvec out_vec[] = {
1207 {.base = signature, .len = signature_size},
1208 };
1209
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001210 status = API_DISPATCH(tfm_crypto_sign_hash,
1211 TFM_CRYPTO_SIGN_HASH);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001212
1213 *signature_length = out_vec[0].len;
1214
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001215 return status;
1216}
1217
Maulik Patel28659c42021-01-06 14:09:22 +00001218psa_status_t psa_verify_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001219 psa_algorithm_t alg,
1220 const uint8_t *hash,
1221 size_t hash_length,
1222 const uint8_t *signature,
1223 size_t signature_length)
1224{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001225 psa_status_t status;
1226 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001227 .sfn_id = TFM_CRYPTO_VERIFY_HASH_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001228 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001229 .alg = alg
1230 };
1231
1232 psa_invec in_vec[] = {
1233 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1234 {.base = hash, .len = hash_length},
1235 {.base = signature, .len = signature_length}
1236 };
1237
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001238 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_verify_hash,
1239 TFM_CRYPTO_VERIFY_HASH);
Kevin Peng9449a362019-07-29 16:05:42 +08001240
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001241 return status;
1242}
1243
Maulik Patel28659c42021-01-06 14:09:22 +00001244psa_status_t psa_asymmetric_encrypt(psa_key_id_t key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001245 psa_algorithm_t alg,
1246 const uint8_t *input,
1247 size_t input_length,
1248 const uint8_t *salt,
1249 size_t salt_length,
1250 uint8_t *output,
1251 size_t output_size,
1252 size_t *output_length)
1253{
1254 psa_status_t status;
1255 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001256 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001257 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001258 .alg = alg
1259 };
1260
1261 /* Sanitize the optional input */
1262 if ((salt == NULL) && (salt_length != 0)) {
1263 return PSA_ERROR_INVALID_ARGUMENT;
1264 }
1265
1266 psa_invec in_vec[] = {
1267 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1268 {.base = input, .len = input_length},
1269 {.base = salt, .len = salt_length}
1270 };
1271
1272 psa_outvec out_vec[] = {
1273 {.base = output, .len = output_size},
1274 };
1275
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001276 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001277 if (salt == NULL) {
1278 in_len--;
1279 }
Summer Qinaee07882021-03-29 15:44:27 +08001280 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001281 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001282
1283 *output_length = out_vec[0].len;
1284
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001285 return status;
1286}
1287
Maulik Patel28659c42021-01-06 14:09:22 +00001288psa_status_t psa_asymmetric_decrypt(psa_key_id_t key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001289 psa_algorithm_t alg,
1290 const uint8_t *input,
1291 size_t input_length,
1292 const uint8_t *salt,
1293 size_t salt_length,
1294 uint8_t *output,
1295 size_t output_size,
1296 size_t *output_length)
1297{
1298 psa_status_t status;
1299 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001300 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001301 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001302 .alg = alg
1303 };
1304
1305 /* Sanitize the optional input */
1306 if ((salt == NULL) && (salt_length != 0)) {
1307 return PSA_ERROR_INVALID_ARGUMENT;
1308 }
1309
1310 psa_invec in_vec[] = {
1311 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1312 {.base = input, .len = input_length},
1313 {.base = salt, .len = salt_length}
1314 };
1315
1316 psa_outvec out_vec[] = {
1317 {.base = output, .len = output_size},
1318 };
1319
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001320 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001321 if (salt == NULL) {
1322 in_len--;
1323 }
Summer Qinaee07882021-03-29 15:44:27 +08001324 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001325 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001326
1327 *output_length = out_vec[0].len;
1328
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001329 return status;
1330}
1331
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001332psa_status_t psa_key_derivation_get_capacity(
1333 const psa_key_derivation_operation_t *operation,
1334 size_t *capacity)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001335{
1336 psa_status_t status;
1337 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001338 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_GET_CAPACITY_SID,
1339 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001340 };
1341
1342 psa_invec in_vec[] = {
1343 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1344 };
1345
1346 psa_outvec out_vec[] = {
1347 {.base = capacity, .len = sizeof(size_t)},
1348 };
1349
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001350 status = API_DISPATCH(tfm_crypto_key_derivation_get_capacity,
1351 TFM_CRYPTO_KEY_DERIVATION_GET_CAPACITY);
Kevin Peng9449a362019-07-29 16:05:42 +08001352
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001353 return status;
1354}
1355
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001356psa_status_t psa_key_derivation_output_bytes(
1357 psa_key_derivation_operation_t *operation,
1358 uint8_t *output,
1359 size_t output_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001360{
1361 psa_status_t status;
1362 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001363 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_OUTPUT_BYTES_SID,
1364 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001365 };
1366
1367 psa_invec in_vec[] = {
1368 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1369 };
1370
1371 psa_outvec out_vec[] = {
1372 {.base = output, .len = output_length},
1373 };
1374
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001375 status = API_DISPATCH(tfm_crypto_key_derivation_output_bytes,
1376 TFM_CRYPTO_KEY_DERIVATION_OUTPUT_BYTES);
Kevin Peng9449a362019-07-29 16:05:42 +08001377
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001378 return status;
1379}
1380
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001381psa_status_t psa_key_derivation_input_key(
1382 psa_key_derivation_operation_t *operation,
1383 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00001384 psa_key_id_t key)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001385{
1386 psa_status_t status;
1387 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001388 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_INPUT_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001389 .key_id = key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001390 .step = step,
1391 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001392 };
1393
1394 psa_invec in_vec[] = {
1395 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001396 };
1397
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001398 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_input_key,
1399 TFM_CRYPTO_KEY_DERIVATION_INPUT_KEY);
Kevin Peng9449a362019-07-29 16:05:42 +08001400
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001401 return status;
1402}
1403
Antonio de Angelis8d282482021-10-07 15:04:12 +01001404psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001405{
1406 psa_status_t status;
1407 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001408 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_ABORT_SID,
1409 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001410 };
1411
1412 psa_invec in_vec[] = {
1413 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1414 };
1415
1416 psa_outvec out_vec[] = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001417 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001418 };
1419
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001420 status = API_DISPATCH(tfm_crypto_key_derivation_abort,
1421 TFM_CRYPTO_KEY_DERIVATION_ABORT);
Kevin Peng9449a362019-07-29 16:05:42 +08001422
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001423 return status;
1424}
1425
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001426psa_status_t psa_key_derivation_key_agreement(
1427 psa_key_derivation_operation_t *operation,
1428 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00001429 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001430 const uint8_t *peer_key,
1431 size_t peer_key_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001432{
1433 psa_status_t status;
1434 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001435 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_KEY_AGREEMENT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001436 .key_id = private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001437 .step = step,
1438 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001439 };
1440
1441 psa_invec in_vec[] = {
1442 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1443 {.base = peer_key, .len = peer_key_length},
1444 };
1445
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001446 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_key_agreement,
1447 TFM_CRYPTO_KEY_DERIVATION_KEY_AGREEMENT);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001448
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001449 return status;
1450}
1451
1452psa_status_t psa_generate_random(uint8_t *output,
1453 size_t output_size)
1454{
1455 psa_status_t status;
1456 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001457 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001458 };
1459
1460 psa_invec in_vec[] = {
1461 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1462 };
1463
1464 psa_outvec out_vec[] = {
1465 {.base = output, .len = output_size},
1466 };
1467
1468 if (output_size == 0) {
1469 return PSA_SUCCESS;
1470 }
1471
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001472 status = API_DISPATCH(tfm_crypto_generate_random,
1473 TFM_CRYPTO_GENERATE_RANDOM);
1474
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001475 return status;
1476}
1477
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001478psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +00001479 psa_key_id_t *key)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001480{
1481 psa_status_t status;
1482 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001483 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001484 };
1485
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001486 psa_invec in_vec[] = {
1487 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001488 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
1489 };
1490
1491 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +00001492 {.base = key, .len = sizeof(psa_key_id_t)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001493 };
1494
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001495 status = API_DISPATCH(tfm_crypto_generate_key,
1496 TFM_CRYPTO_GENERATE_KEY);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001497
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001498 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001499}
1500
Maulik Patel28659c42021-01-06 14:09:22 +00001501psa_status_t psa_mac_compute(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001502 psa_algorithm_t alg,
1503 const uint8_t *input,
1504 size_t input_length,
1505 uint8_t *mac,
1506 size_t mac_size,
1507 size_t *mac_length)
1508{
1509 psa_status_t status;
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001510 struct tfm_crypto_pack_iovec iov = {
1511 .sfn_id = TFM_CRYPTO_MAC_COMPUTE_SID,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001512 .key_id = key,
Summer Qin045ec4a2021-07-07 14:28:04 +08001513 .alg = alg,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001514 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001515
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001516 psa_invec in_vec[] = {
1517 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Summer Qin045ec4a2021-07-07 14:28:04 +08001518 {.base = input, .len = input_length},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001519 };
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001520 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001521 {.base = mac, .len = mac_size},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001522 };
1523
1524 status = API_DISPATCH(tfm_crypto_mac_compute,
1525 TFM_CRYPTO_MAC_COMPUTE);
1526
1527 *mac_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001528 return status;
1529}
1530
Maulik Patel28659c42021-01-06 14:09:22 +00001531psa_status_t psa_mac_verify(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001532 psa_algorithm_t alg,
1533 const uint8_t *input,
1534 size_t input_length,
1535 const uint8_t *mac,
1536 const size_t mac_length)
1537{
1538 psa_status_t status;
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001539 struct tfm_crypto_pack_iovec iov = {
1540 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SID,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001541 .key_id = key,
Summer Qin045ec4a2021-07-07 14:28:04 +08001542 .alg = alg,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001543 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001544
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001545 psa_invec in_vec[] = {
1546 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1547 {.base = input, .len = input_length},
Summer Qin045ec4a2021-07-07 14:28:04 +08001548 {.base = mac, .len = mac_length},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001549 };
1550
1551 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_mac_verify,
1552 TFM_CRYPTO_MAC_VERIFY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001553
1554 return status;
1555}
1556
Maulik Patel28659c42021-01-06 14:09:22 +00001557psa_status_t psa_cipher_encrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001558 psa_algorithm_t alg,
1559 const uint8_t *input,
1560 size_t input_length,
1561 uint8_t *output,
1562 size_t output_size,
1563 size_t *output_length)
1564{
Antonio de Angelis609f0002021-07-06 16:51:28 +02001565#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
1566 return PSA_ERROR_NOT_SUPPORTED;
1567#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001568 psa_status_t status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001569 struct tfm_crypto_pack_iovec iov = {
1570 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SID,
Summer Qin045ec4a2021-07-07 14:28:04 +08001571 .key_id = key,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001572 .alg = alg,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001573 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001574
Antonio de Angelis609f0002021-07-06 16:51:28 +02001575 psa_invec in_vec[] = {
1576 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1577 {.base = input, .len = input_length},
1578 };
Antonio de Angelis609f0002021-07-06 16:51:28 +02001579 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001580 {.base = output, .len = output_size}
Antonio de Angelis609f0002021-07-06 16:51:28 +02001581 };
1582
1583 status = API_DISPATCH(tfm_crypto_cipher_encrypt,
1584 TFM_CRYPTO_CIPHER_ENCRYPT);
1585
1586 *output_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001587 return status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001588#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001589}
1590
Maulik Patel28659c42021-01-06 14:09:22 +00001591psa_status_t psa_cipher_decrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001592 psa_algorithm_t alg,
1593 const uint8_t *input,
1594 size_t input_length,
1595 uint8_t *output,
1596 size_t output_size,
1597 size_t *output_length)
1598{
Antonio de Angelis609f0002021-07-06 16:51:28 +02001599#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
1600 return PSA_ERROR_NOT_SUPPORTED;
1601#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001602 psa_status_t status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001603 struct tfm_crypto_pack_iovec iov = {
1604 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SID,
Summer Qin045ec4a2021-07-07 14:28:04 +08001605 .key_id = key,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001606 .alg = alg,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001607 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001608
Antonio de Angelis609f0002021-07-06 16:51:28 +02001609 psa_invec in_vec[] = {
1610 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1611 {.base = input, .len = input_length},
1612 };
Antonio de Angelis609f0002021-07-06 16:51:28 +02001613 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001614 {.base = output, .len = output_size}
Antonio de Angelis609f0002021-07-06 16:51:28 +02001615 };
1616
1617 status = API_DISPATCH(tfm_crypto_cipher_decrypt,
1618 TFM_CRYPTO_CIPHER_DECRYPT);
1619
1620 *output_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001621 return status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001622#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001623}
1624
1625psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Maulik Patel28659c42021-01-06 14:09:22 +00001626 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001627 const uint8_t *peer_key,
1628 size_t peer_key_length,
1629 uint8_t *output,
1630 size_t output_size,
1631 size_t *output_length)
1632{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001633 psa_status_t status;
1634 struct tfm_crypto_pack_iovec iov = {
1635 .sfn_id = TFM_CRYPTO_RAW_KEY_AGREEMENT_SID,
1636 .alg = alg,
Maulik Patel28659c42021-01-06 14:09:22 +00001637 .key_id = private_key
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001638 };
1639
1640 psa_invec in_vec[] = {
1641 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1642 {.base = peer_key, .len = peer_key_length},
1643 };
1644
1645 psa_outvec out_vec[] = {
1646 {.base = output, .len = output_size},
1647 };
1648
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001649 status = API_DISPATCH(tfm_crypto_raw_key_agreement,
1650 TFM_CRYPTO_RAW_KEY_AGREEMENT);
1651
1652 *output_length = out_vec[0].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001653
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001654 return status;
1655}
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001656
1657psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
1658 psa_algorithm_t alg)
1659{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001660 psa_status_t status;
1661 struct tfm_crypto_pack_iovec iov = {
1662 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SETUP_SID,
1663 .alg = alg,
1664 .op_handle = operation->handle,
1665 };
1666
1667 psa_invec in_vec[] = {
1668 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1669 };
1670 psa_outvec out_vec[] = {
1671 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1672 };
1673
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001674 status = API_DISPATCH(tfm_crypto_key_derivation_setup,
1675 TFM_CRYPTO_KEY_DERIVATION_SETUP);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001676 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001677}
1678
1679psa_status_t psa_key_derivation_set_capacity(
1680 psa_key_derivation_operation_t *operation,
1681 size_t capacity)
1682{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001683 psa_status_t status;
1684 struct tfm_crypto_pack_iovec iov = {
1685 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SET_CAPACITY_SID,
1686 .capacity = capacity,
1687 .op_handle = operation->handle,
1688 };
1689
1690 psa_invec in_vec[] = {
1691 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1692 };
1693
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001694 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_set_capacity,
1695 TFM_CRYPTO_KEY_DERIVATION_SET_CAPACITY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001696 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001697}
1698
1699psa_status_t psa_key_derivation_input_bytes(
1700 psa_key_derivation_operation_t *operation,
1701 psa_key_derivation_step_t step,
1702 const uint8_t *data,
1703 size_t data_length)
1704{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001705 psa_status_t status;
1706 struct tfm_crypto_pack_iovec iov = {
1707 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_INPUT_BYTES_SID,
1708 .step = step,
1709 .op_handle = operation->handle,
1710 };
1711
1712 psa_invec in_vec[] = {
1713 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1714 {.base = data, .len = data_length},
1715 };
1716
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001717 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_input_bytes,
1718 TFM_CRYPTO_KEY_DERIVATION_INPUT_BYTES);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001719 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001720}
1721
1722psa_status_t psa_key_derivation_output_key(
1723 const psa_key_attributes_t *attributes,
1724 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001725 psa_key_id_t *key)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001726{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001727 psa_status_t status;
1728 struct tfm_crypto_pack_iovec iov = {
1729 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_OUTPUT_KEY_SID,
1730 .op_handle = operation->handle,
1731 };
1732
1733 psa_invec in_vec[] = {
1734 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1735 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
1736 };
1737
1738 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +00001739 {.base = key, .len = sizeof(psa_key_id_t)}
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001740 };
1741
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001742 status = API_DISPATCH(tfm_crypto_key_derivation_output_key,
1743 TFM_CRYPTO_KEY_DERIVATION_OUTPUT_KEY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001744 return status;
1745}