blob: 67842ac171ef8318ba933ea5056717c40d0cd4c5 [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,
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000781 .aead_in = {.nonce = {0}, .nonce_length = 0}
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 psa_invec in_vec[] = {
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000790 {.base = NULL, .len = 0},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000791 {.base = plaintext, .len = plaintext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +0100792 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000793 };
794 psa_outvec out_vec[] = {
795 {.base = ciphertext, .len = ciphertext_size},
796 };
Antonio de Angelis3a480992018-11-07 11:53:28 +0000797
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000798 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
799 return PSA_ERROR_INVALID_ARGUMENT;
800 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000801
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000802 if (nonce != NULL) {
Antonio de Angelisa80e7842021-11-25 13:16:02 +0000803 for (size_t idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100804 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000805 }
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000806 iov.aead_in.nonce_length = nonce_length;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000807 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000808
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000809 in_vec[0].base = &iov;
810 in_vec[0].len = sizeof(struct tfm_crypto_pack_iovec);
811
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800812 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100813 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100814 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100815 }
Summer Qinaee07882021-03-29 15:44:27 +0800816 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800817 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000818
819 *ciphertext_length = out_vec[0].len;
820
821 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +0000822}
823
Maulik Patel28659c42021-01-06 14:09:22 +0000824psa_status_t psa_aead_decrypt(psa_key_id_t key,
Antonio de Angelis3a480992018-11-07 11:53:28 +0000825 psa_algorithm_t alg,
826 const uint8_t *nonce,
827 size_t nonce_length,
828 const uint8_t *additional_data,
829 size_t additional_data_length,
830 const uint8_t *ciphertext,
831 size_t ciphertext_length,
832 uint8_t *plaintext,
833 size_t plaintext_size,
834 size_t *plaintext_length)
835{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000836 psa_status_t status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100837 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +0800838 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +0000839 .key_id = key,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100840 .alg = alg,
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000841 .aead_in = {.nonce = {0}, .nonce_length = 0}
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000842 };
Antonio de Angelis4743e672019-04-11 11:38:48 +0100843
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100844 /* Sanitize the optional input */
845 if ((additional_data == NULL) && (additional_data_length != 0)) {
846 return PSA_ERROR_INVALID_ARGUMENT;
847 }
848
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000849 psa_invec in_vec[] = {
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000850 {.base = NULL, .len = 0},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000851 {.base = ciphertext, .len = ciphertext_length},
Antonio de Angelis4743e672019-04-11 11:38:48 +0100852 {.base = additional_data, .len = additional_data_length},
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000853 };
854 psa_outvec out_vec[] = {
855 {.base = plaintext, .len = plaintext_size},
856 };
Antonio de Angelis3a480992018-11-07 11:53:28 +0000857
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000858 if (nonce_length > TFM_CRYPTO_MAX_NONCE_LENGTH) {
859 return PSA_ERROR_INVALID_ARGUMENT;
860 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000861
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000862 if (nonce != NULL) {
Antonio de Angelisa80e7842021-11-25 13:16:02 +0000863 for (size_t idx = 0; idx < nonce_length; idx++) {
Antonio de Angelis4743e672019-04-11 11:38:48 +0100864 iov.aead_in.nonce[idx] = nonce[idx];
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000865 }
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000866 iov.aead_in.nonce_length = nonce_length;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000867 }
Antonio de Angelis3a480992018-11-07 11:53:28 +0000868
Antonio de Angelis341f52a2021-12-02 15:49:44 +0000869 in_vec[0].base = &iov;
870 in_vec[0].len = sizeof(struct tfm_crypto_pack_iovec);
871
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800872 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100873 if (additional_data == NULL) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100874 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100875 }
Summer Qinaee07882021-03-29 15:44:27 +0800876 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +0800877 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000878
879 *plaintext_length = out_vec[0].len;
880
881 return status;
Antonio de Angelis3a480992018-11-07 11:53:28 +0000882}
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100883
Antonio de Angelis8d282482021-10-07 15:04:12 +0100884psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
885 psa_key_id_t key,
886 psa_algorithm_t alg)
887{
888 psa_status_t status;
889 struct tfm_crypto_pack_iovec iov = {
890 .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SETUP_SID,
891 .key_id = key,
892 .alg = alg,
893 .op_handle = operation->handle,
894 };
895
896 psa_invec in_vec[] = {
897 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}
898 };
899 psa_outvec out_vec[] = {
900 {.base = &(operation->handle), .len = sizeof(uint32_t)}
901 };
902
903 status = API_DISPATCH(tfm_crypto_aead_encrypt_setup,
904 TFM_CRYPTO_AEAD_ENCRYPT_SETUP);
905 return status;
906}
907
908psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
909 psa_key_id_t key,
910 psa_algorithm_t alg)
911{
912 psa_status_t status;
913 struct tfm_crypto_pack_iovec iov = {
914 .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SETUP_SID,
915 .key_id = key,
916 .alg = alg,
917 .op_handle = operation->handle,
918 };
919
920 psa_invec in_vec[] = {
921 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}
922 };
923 psa_outvec out_vec[] = {
924 {.base = &(operation->handle), .len = sizeof(uint32_t)}
925 };
926
927 status = API_DISPATCH(tfm_crypto_aead_decrypt_setup,
928 TFM_CRYPTO_AEAD_DECRYPT_SETUP);
929 return status;
930}
931
932psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
933 uint8_t *nonce,
934 size_t nonce_size,
935 size_t *nonce_length)
936{
937 psa_status_t status;
938 struct tfm_crypto_pack_iovec iov = {
939 .sfn_id = TFM_CRYPTO_AEAD_GENERATE_NONCE_SID,
940 .op_handle = operation->handle,
941 };
942
943 psa_invec in_vec[] = {
944 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
945 };
946 psa_outvec out_vec[] = {
947 {.base = &(operation->handle), .len = sizeof(uint32_t)},
948 {.base = nonce, .len = nonce_size}
949 };
950
951 status = API_DISPATCH(tfm_crypto_aead_generate_nonce,
952 TFM_CRYPTO_AEAD_GENERATE_NONCE);
953
954 *nonce_length = out_vec[1].len;
955 return status;
956}
957
958psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
959 const uint8_t *nonce,
960 size_t nonce_length)
961{
962 psa_status_t status;
963 struct tfm_crypto_pack_iovec iov = {
964 .sfn_id = TFM_CRYPTO_AEAD_SET_NONCE_SID,
965 .op_handle = operation->handle,
966 };
967
968 psa_invec in_vec[] = {
969 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
970 {.base = nonce, .len = nonce_length}
971 };
972 psa_outvec out_vec[] = {
973 {.base = &(operation->handle), .len = sizeof(uint32_t)}
974 };
975
976 status = API_DISPATCH(tfm_crypto_aead_set_nonce,
977 TFM_CRYPTO_AEAD_SET_NONCE);
978 return status;
979}
980
981psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
982 size_t ad_length,
983 size_t plaintext_length)
984{
985 psa_status_t status;
986 struct tfm_crypto_pack_iovec iov = {
987 .sfn_id = TFM_CRYPTO_AEAD_SET_LENGTHS_SID,
988 .ad_length = ad_length,
989 .plaintext_length = plaintext_length,
990 .op_handle = operation->handle,
991 };
992
993 psa_invec in_vec[] = {
994 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
995 };
996 psa_outvec out_vec[] = {
997 {.base = &(operation->handle), .len = sizeof(uint32_t)}
998 };
999
1000 status = API_DISPATCH(tfm_crypto_aead_set_lengths,
1001 TFM_CRYPTO_AEAD_SET_LENGTHS);
1002 return status;
1003}
1004
1005psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1006 const uint8_t *input,
1007 size_t input_length)
1008{
1009 psa_status_t status;
1010 struct tfm_crypto_pack_iovec iov = {
1011 .sfn_id = TFM_CRYPTO_AEAD_UPDATE_AD_SID,
1012 .op_handle = operation->handle,
1013 };
1014
1015 psa_invec in_vec[] = {
1016 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1017 {.base = input, .len = input_length}
1018 };
1019 psa_outvec out_vec[] = {
1020 {.base = &(operation->handle), .len = sizeof(uint32_t)}
1021 };
1022
1023 status = API_DISPATCH(tfm_crypto_aead_update_ad,
1024 TFM_CRYPTO_AEAD_UPDATE_AD);
1025 return status;
1026}
1027
1028psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1029 const uint8_t *input,
1030 size_t input_length,
1031 uint8_t *output,
1032 size_t output_size,
1033 size_t *output_length)
1034{
1035 psa_status_t status;
1036 struct tfm_crypto_pack_iovec iov = {
1037 .sfn_id = TFM_CRYPTO_AEAD_UPDATE_SID,
1038 .op_handle = operation->handle,
1039 };
1040
1041 psa_invec in_vec[] = {
1042 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1043 {.base = input, .len = input_length}
1044 };
1045 psa_outvec out_vec[] = {
1046 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1047 {.base = output, .len = output_size},
1048 };
1049
1050 status = API_DISPATCH(tfm_crypto_aead_update,
1051 TFM_CRYPTO_AEAD_UPDATE);
1052
1053 *output_length = out_vec[1].len;
1054 return status;
1055}
1056
1057psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1058 uint8_t *ciphertext,
1059 size_t ciphertext_size,
1060 size_t *ciphertext_length,
1061 uint8_t *tag,
1062 size_t tag_size,
1063 size_t *tag_length)
1064{
1065 psa_status_t status;
1066 struct tfm_crypto_pack_iovec iov = {
1067 .sfn_id = TFM_CRYPTO_AEAD_FINISH_SID,
1068 .op_handle = operation->handle,
1069 };
1070
1071 psa_invec in_vec[] = {
1072 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1073 };
1074 psa_outvec out_vec[] = {
1075 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1076 {.base = ciphertext, .len = ciphertext_size},
1077 {.base = tag, .len = tag_size},
1078 };
1079
1080 status = API_DISPATCH(tfm_crypto_aead_finish,
1081 TFM_CRYPTO_AEAD_FINISH);
1082
1083 *ciphertext_length = out_vec[1].len;
1084 *tag_length = out_vec[2].len;
1085 return status;
1086}
1087
1088psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1089 uint8_t *plaintext,
1090 size_t plaintext_size,
1091 size_t *plaintext_length,
1092 const uint8_t *tag,
1093 size_t tag_length)
1094{
1095 psa_status_t status;
1096 struct tfm_crypto_pack_iovec iov = {
1097 .sfn_id = TFM_CRYPTO_AEAD_VERIFY_SID,
1098 .op_handle = operation->handle,
1099 };
1100
1101 psa_invec in_vec[] = {
1102 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1103 {.base = tag, .len = tag_length}
1104 };
1105 psa_outvec out_vec[] = {
1106 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1107 {.base = plaintext, .len = plaintext_size},
1108 };
1109
1110 status = API_DISPATCH(tfm_crypto_aead_verify,
1111 TFM_CRYPTO_AEAD_VERIFY);
1112
1113 *plaintext_length = out_vec[1].len;
1114 return status;
1115}
1116
1117psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
1118{
1119 psa_status_t status;
1120 struct tfm_crypto_pack_iovec iov = {
1121 .sfn_id = TFM_CRYPTO_AEAD_ABORT_SID,
1122 .op_handle = operation->handle,
1123 };
1124
1125 psa_invec in_vec[] = {
1126 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1127 };
1128 psa_outvec out_vec[] = {
1129 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1130 };
1131
1132 status = API_DISPATCH(tfm_crypto_aead_abort,
1133 TFM_CRYPTO_AEAD_ABORT);
1134 return status;
1135}
1136
Summer Qinb9492d22021-06-22 18:00:54 +08001137psa_status_t psa_sign_message(psa_key_id_t key,
1138 psa_algorithm_t alg,
1139 const uint8_t *input,
1140 size_t input_length,
1141 uint8_t *signature,
1142 size_t signature_size,
1143 size_t *signature_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001144{
Summer Qinb9492d22021-06-22 18:00:54 +08001145 psa_status_t status;
1146 struct tfm_crypto_pack_iovec iov = {
1147 .sfn_id = TFM_CRYPTO_SIGN_MESSAGE_SID,
1148 .key_id = key,
1149 .alg = alg,
1150 };
1151
1152 psa_invec in_vec[] = {
1153 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1154 {.base = input, .len = input_length},
1155 };
1156 psa_outvec out_vec[] = {
1157 {.base = signature, .len = signature_size},
1158 };
1159
1160 status = API_DISPATCH(tfm_crypto_sign_message,
1161 TFM_CRYPTO_SIGN_MESSAGE);
1162
Antonio de Angelisf83a2082021-08-20 22:13:53 +01001163 *signature_length = out_vec[0].len;
Summer Qinb9492d22021-06-22 18:00:54 +08001164 return status;
1165}
1166
1167psa_status_t psa_verify_message(psa_key_id_t key,
1168 psa_algorithm_t alg,
1169 const uint8_t *input,
1170 size_t input_length,
1171 const uint8_t *signature,
1172 size_t signature_length)
1173{
1174 psa_status_t status;
1175 struct tfm_crypto_pack_iovec iov = {
1176 .sfn_id = TFM_CRYPTO_VERIFY_MESSAGE_SID,
1177 .key_id = key,
1178 .alg = alg
1179 };
1180
1181 psa_invec in_vec[] = {
1182 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1183 {.base = input, .len = input_length},
1184 {.base = signature, .len = signature_length}
1185 };
1186
1187 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_verify_message,
1188 TFM_CRYPTO_VERIFY_MESSAGE);
1189
1190 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001191}
1192
Maulik Patel28659c42021-01-06 14:09:22 +00001193psa_status_t psa_sign_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001194 psa_algorithm_t alg,
1195 const uint8_t *hash,
1196 size_t hash_length,
1197 uint8_t *signature,
1198 size_t signature_size,
1199 size_t *signature_length)
1200{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001201 psa_status_t status;
1202 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001203 .sfn_id = TFM_CRYPTO_SIGN_HASH_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001204 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001205 .alg = alg,
1206 };
1207
1208 psa_invec in_vec[] = {
1209 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1210 {.base = hash, .len = hash_length},
1211 };
1212 psa_outvec out_vec[] = {
1213 {.base = signature, .len = signature_size},
1214 };
1215
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001216 status = API_DISPATCH(tfm_crypto_sign_hash,
1217 TFM_CRYPTO_SIGN_HASH);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001218
1219 *signature_length = out_vec[0].len;
1220
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001221 return status;
1222}
1223
Maulik Patel28659c42021-01-06 14:09:22 +00001224psa_status_t psa_verify_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001225 psa_algorithm_t alg,
1226 const uint8_t *hash,
1227 size_t hash_length,
1228 const uint8_t *signature,
1229 size_t signature_length)
1230{
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001231 psa_status_t status;
1232 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001233 .sfn_id = TFM_CRYPTO_VERIFY_HASH_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001234 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001235 .alg = alg
1236 };
1237
1238 psa_invec in_vec[] = {
1239 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1240 {.base = hash, .len = hash_length},
1241 {.base = signature, .len = signature_length}
1242 };
1243
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001244 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_verify_hash,
1245 TFM_CRYPTO_VERIFY_HASH);
Kevin Peng9449a362019-07-29 16:05:42 +08001246
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001247 return status;
1248}
1249
Maulik Patel28659c42021-01-06 14:09:22 +00001250psa_status_t psa_asymmetric_encrypt(psa_key_id_t key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001251 psa_algorithm_t alg,
1252 const uint8_t *input,
1253 size_t input_length,
1254 const uint8_t *salt,
1255 size_t salt_length,
1256 uint8_t *output,
1257 size_t output_size,
1258 size_t *output_length)
1259{
1260 psa_status_t status;
1261 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001262 .sfn_id = TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001263 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001264 .alg = alg
1265 };
1266
1267 /* Sanitize the optional input */
1268 if ((salt == NULL) && (salt_length != 0)) {
1269 return PSA_ERROR_INVALID_ARGUMENT;
1270 }
1271
1272 psa_invec in_vec[] = {
1273 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1274 {.base = input, .len = input_length},
1275 {.base = salt, .len = salt_length}
1276 };
1277
1278 psa_outvec out_vec[] = {
1279 {.base = output, .len = output_size},
1280 };
1281
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001282 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001283 if (salt == NULL) {
1284 in_len--;
1285 }
Summer Qinaee07882021-03-29 15:44:27 +08001286 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001287 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001288
1289 *output_length = out_vec[0].len;
1290
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001291 return status;
1292}
1293
Maulik Patel28659c42021-01-06 14:09:22 +00001294psa_status_t psa_asymmetric_decrypt(psa_key_id_t key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001295 psa_algorithm_t alg,
1296 const uint8_t *input,
1297 size_t input_length,
1298 const uint8_t *salt,
1299 size_t salt_length,
1300 uint8_t *output,
1301 size_t output_size,
1302 size_t *output_length)
1303{
1304 psa_status_t status;
1305 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001306 .sfn_id = TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001307 .key_id = key,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001308 .alg = alg
1309 };
1310
1311 /* Sanitize the optional input */
1312 if ((salt == NULL) && (salt_length != 0)) {
1313 return PSA_ERROR_INVALID_ARGUMENT;
1314 }
1315
1316 psa_invec in_vec[] = {
1317 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1318 {.base = input, .len = input_length},
1319 {.base = salt, .len = salt_length}
1320 };
1321
1322 psa_outvec out_vec[] = {
1323 {.base = output, .len = output_size},
1324 };
1325
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001326 size_t in_len = IOVEC_LEN(in_vec);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001327 if (salt == NULL) {
1328 in_len--;
1329 }
Summer Qinaee07882021-03-29 15:44:27 +08001330 status = psa_call(TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec, in_len,
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08001331 out_vec, IOVEC_LEN(out_vec));
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001332
1333 *output_length = out_vec[0].len;
1334
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001335 return status;
1336}
1337
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001338psa_status_t psa_key_derivation_get_capacity(
1339 const psa_key_derivation_operation_t *operation,
1340 size_t *capacity)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001341{
1342 psa_status_t status;
1343 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001344 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_GET_CAPACITY_SID,
1345 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001346 };
1347
1348 psa_invec in_vec[] = {
1349 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1350 };
1351
1352 psa_outvec out_vec[] = {
1353 {.base = capacity, .len = sizeof(size_t)},
1354 };
1355
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001356 status = API_DISPATCH(tfm_crypto_key_derivation_get_capacity,
1357 TFM_CRYPTO_KEY_DERIVATION_GET_CAPACITY);
Kevin Peng9449a362019-07-29 16:05:42 +08001358
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001359 return status;
1360}
1361
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001362psa_status_t psa_key_derivation_output_bytes(
1363 psa_key_derivation_operation_t *operation,
1364 uint8_t *output,
1365 size_t output_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001366{
1367 psa_status_t status;
1368 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001369 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_OUTPUT_BYTES_SID,
1370 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001371 };
1372
1373 psa_invec in_vec[] = {
1374 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1375 };
1376
1377 psa_outvec out_vec[] = {
1378 {.base = output, .len = output_length},
1379 };
1380
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001381 status = API_DISPATCH(tfm_crypto_key_derivation_output_bytes,
1382 TFM_CRYPTO_KEY_DERIVATION_OUTPUT_BYTES);
Kevin Peng9449a362019-07-29 16:05:42 +08001383
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001384 return status;
1385}
1386
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001387psa_status_t psa_key_derivation_input_key(
1388 psa_key_derivation_operation_t *operation,
1389 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00001390 psa_key_id_t key)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001391{
1392 psa_status_t status;
1393 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001394 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_INPUT_KEY_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001395 .key_id = key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001396 .step = step,
1397 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001398 };
1399
1400 psa_invec in_vec[] = {
1401 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001402 };
1403
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001404 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_input_key,
1405 TFM_CRYPTO_KEY_DERIVATION_INPUT_KEY);
Kevin Peng9449a362019-07-29 16:05:42 +08001406
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001407 return status;
1408}
1409
Antonio de Angelis8d282482021-10-07 15:04:12 +01001410psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001411{
1412 psa_status_t status;
1413 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001414 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_ABORT_SID,
1415 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001416 };
1417
1418 psa_invec in_vec[] = {
1419 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1420 };
1421
1422 psa_outvec out_vec[] = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001423 {.base = &(operation->handle), .len = sizeof(uint32_t)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001424 };
1425
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001426 status = API_DISPATCH(tfm_crypto_key_derivation_abort,
1427 TFM_CRYPTO_KEY_DERIVATION_ABORT);
Kevin Peng9449a362019-07-29 16:05:42 +08001428
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001429 return status;
1430}
1431
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001432psa_status_t psa_key_derivation_key_agreement(
1433 psa_key_derivation_operation_t *operation,
1434 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00001435 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001436 const uint8_t *peer_key,
1437 size_t peer_key_length)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001438{
1439 psa_status_t status;
1440 struct tfm_crypto_pack_iovec iov = {
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001441 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_KEY_AGREEMENT_SID,
Maulik Patel28659c42021-01-06 14:09:22 +00001442 .key_id = private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001443 .step = step,
1444 .op_handle = operation->handle,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001445 };
1446
1447 psa_invec in_vec[] = {
1448 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1449 {.base = peer_key, .len = peer_key_length},
1450 };
1451
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001452 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_key_agreement,
1453 TFM_CRYPTO_KEY_DERIVATION_KEY_AGREEMENT);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001454
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001455 return status;
1456}
1457
1458psa_status_t psa_generate_random(uint8_t *output,
1459 size_t output_size)
1460{
1461 psa_status_t status;
1462 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001463 .sfn_id = TFM_CRYPTO_GENERATE_RANDOM_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001464 };
1465
1466 psa_invec in_vec[] = {
1467 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1468 };
1469
1470 psa_outvec out_vec[] = {
1471 {.base = output, .len = output_size},
1472 };
1473
1474 if (output_size == 0) {
1475 return PSA_SUCCESS;
1476 }
1477
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001478 status = API_DISPATCH(tfm_crypto_generate_random,
1479 TFM_CRYPTO_GENERATE_RANDOM);
1480
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001481 return status;
1482}
1483
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001484psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +00001485 psa_key_id_t *key)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001486{
1487 psa_status_t status;
1488 struct tfm_crypto_pack_iovec iov = {
Edison Ai080b2e22019-04-17 16:27:21 +08001489 .sfn_id = TFM_CRYPTO_GENERATE_KEY_SID,
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001490 };
1491
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001492 psa_invec in_vec[] = {
1493 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001494 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
1495 };
1496
1497 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +00001498 {.base = key, .len = sizeof(psa_key_id_t)},
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001499 };
1500
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001501 status = API_DISPATCH(tfm_crypto_generate_key,
1502 TFM_CRYPTO_GENERATE_KEY);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001503
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001504 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001505}
1506
Maulik Patel28659c42021-01-06 14:09:22 +00001507psa_status_t psa_mac_compute(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001508 psa_algorithm_t alg,
1509 const uint8_t *input,
1510 size_t input_length,
1511 uint8_t *mac,
1512 size_t mac_size,
1513 size_t *mac_length)
1514{
1515 psa_status_t status;
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001516 struct tfm_crypto_pack_iovec iov = {
1517 .sfn_id = TFM_CRYPTO_MAC_COMPUTE_SID,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001518 .key_id = key,
Summer Qin045ec4a2021-07-07 14:28:04 +08001519 .alg = alg,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001520 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001521
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001522 psa_invec in_vec[] = {
1523 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
Summer Qin045ec4a2021-07-07 14:28:04 +08001524 {.base = input, .len = input_length},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001525 };
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001526 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001527 {.base = mac, .len = mac_size},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001528 };
1529
1530 status = API_DISPATCH(tfm_crypto_mac_compute,
1531 TFM_CRYPTO_MAC_COMPUTE);
1532
1533 *mac_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001534 return status;
1535}
1536
Maulik Patel28659c42021-01-06 14:09:22 +00001537psa_status_t psa_mac_verify(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001538 psa_algorithm_t alg,
1539 const uint8_t *input,
1540 size_t input_length,
1541 const uint8_t *mac,
1542 const size_t mac_length)
1543{
1544 psa_status_t status;
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001545 struct tfm_crypto_pack_iovec iov = {
1546 .sfn_id = TFM_CRYPTO_MAC_VERIFY_SID,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001547 .key_id = key,
Summer Qin045ec4a2021-07-07 14:28:04 +08001548 .alg = alg,
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001549 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001550
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001551 psa_invec in_vec[] = {
1552 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1553 {.base = input, .len = input_length},
Summer Qin045ec4a2021-07-07 14:28:04 +08001554 {.base = mac, .len = mac_length},
Antonio de Angelis8f4db962021-07-05 13:58:43 +02001555 };
1556
1557 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_mac_verify,
1558 TFM_CRYPTO_MAC_VERIFY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001559
1560 return status;
1561}
1562
Maulik Patel28659c42021-01-06 14:09:22 +00001563psa_status_t psa_cipher_encrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001564 psa_algorithm_t alg,
1565 const uint8_t *input,
1566 size_t input_length,
1567 uint8_t *output,
1568 size_t output_size,
1569 size_t *output_length)
1570{
Antonio de Angelis609f0002021-07-06 16:51:28 +02001571#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
1572 return PSA_ERROR_NOT_SUPPORTED;
1573#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001574 psa_status_t status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001575 struct tfm_crypto_pack_iovec iov = {
1576 .sfn_id = TFM_CRYPTO_CIPHER_ENCRYPT_SID,
Summer Qin045ec4a2021-07-07 14:28:04 +08001577 .key_id = key,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001578 .alg = alg,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001579 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001580
Antonio de Angelis609f0002021-07-06 16:51:28 +02001581 psa_invec in_vec[] = {
1582 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1583 {.base = input, .len = input_length},
1584 };
Antonio de Angelis609f0002021-07-06 16:51:28 +02001585 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001586 {.base = output, .len = output_size}
Antonio de Angelis609f0002021-07-06 16:51:28 +02001587 };
1588
1589 status = API_DISPATCH(tfm_crypto_cipher_encrypt,
1590 TFM_CRYPTO_CIPHER_ENCRYPT);
1591
1592 *output_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001593 return status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001594#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001595}
1596
Maulik Patel28659c42021-01-06 14:09:22 +00001597psa_status_t psa_cipher_decrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001598 psa_algorithm_t alg,
1599 const uint8_t *input,
1600 size_t input_length,
1601 uint8_t *output,
1602 size_t output_size,
1603 size_t *output_length)
1604{
Antonio de Angelis609f0002021-07-06 16:51:28 +02001605#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
1606 return PSA_ERROR_NOT_SUPPORTED;
1607#else
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001608 psa_status_t status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001609 struct tfm_crypto_pack_iovec iov = {
1610 .sfn_id = TFM_CRYPTO_CIPHER_DECRYPT_SID,
Summer Qin045ec4a2021-07-07 14:28:04 +08001611 .key_id = key,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001612 .alg = alg,
Antonio de Angelis609f0002021-07-06 16:51:28 +02001613 };
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001614
Antonio de Angelis609f0002021-07-06 16:51:28 +02001615 psa_invec in_vec[] = {
1616 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1617 {.base = input, .len = input_length},
1618 };
Antonio de Angelis609f0002021-07-06 16:51:28 +02001619 psa_outvec out_vec[] = {
Summer Qin045ec4a2021-07-07 14:28:04 +08001620 {.base = output, .len = output_size}
Antonio de Angelis609f0002021-07-06 16:51:28 +02001621 };
1622
1623 status = API_DISPATCH(tfm_crypto_cipher_decrypt,
1624 TFM_CRYPTO_CIPHER_DECRYPT);
1625
1626 *output_length = out_vec[0].len;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001627 return status;
Antonio de Angelis609f0002021-07-06 16:51:28 +02001628#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001629}
1630
1631psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Maulik Patel28659c42021-01-06 14:09:22 +00001632 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001633 const uint8_t *peer_key,
1634 size_t peer_key_length,
1635 uint8_t *output,
1636 size_t output_size,
1637 size_t *output_length)
1638{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001639 psa_status_t status;
1640 struct tfm_crypto_pack_iovec iov = {
1641 .sfn_id = TFM_CRYPTO_RAW_KEY_AGREEMENT_SID,
1642 .alg = alg,
Maulik Patel28659c42021-01-06 14:09:22 +00001643 .key_id = private_key
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001644 };
1645
1646 psa_invec in_vec[] = {
1647 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1648 {.base = peer_key, .len = peer_key_length},
1649 };
1650
1651 psa_outvec out_vec[] = {
1652 {.base = output, .len = output_size},
1653 };
1654
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001655 status = API_DISPATCH(tfm_crypto_raw_key_agreement,
1656 TFM_CRYPTO_RAW_KEY_AGREEMENT);
1657
1658 *output_length = out_vec[0].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001659
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001660 return status;
1661}
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001662
1663psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
1664 psa_algorithm_t alg)
1665{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001666 psa_status_t status;
1667 struct tfm_crypto_pack_iovec iov = {
1668 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SETUP_SID,
1669 .alg = alg,
1670 .op_handle = operation->handle,
1671 };
1672
1673 psa_invec in_vec[] = {
1674 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1675 };
1676 psa_outvec out_vec[] = {
1677 {.base = &(operation->handle), .len = sizeof(uint32_t)},
1678 };
1679
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001680 status = API_DISPATCH(tfm_crypto_key_derivation_setup,
1681 TFM_CRYPTO_KEY_DERIVATION_SETUP);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001682 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001683}
1684
1685psa_status_t psa_key_derivation_set_capacity(
1686 psa_key_derivation_operation_t *operation,
1687 size_t capacity)
1688{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001689 psa_status_t status;
1690 struct tfm_crypto_pack_iovec iov = {
1691 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_SET_CAPACITY_SID,
1692 .capacity = capacity,
1693 .op_handle = operation->handle,
1694 };
1695
1696 psa_invec in_vec[] = {
1697 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1698 };
1699
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001700 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_set_capacity,
1701 TFM_CRYPTO_KEY_DERIVATION_SET_CAPACITY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001702 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001703}
1704
1705psa_status_t psa_key_derivation_input_bytes(
1706 psa_key_derivation_operation_t *operation,
1707 psa_key_derivation_step_t step,
1708 const uint8_t *data,
1709 size_t data_length)
1710{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001711 psa_status_t status;
1712 struct tfm_crypto_pack_iovec iov = {
1713 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_INPUT_BYTES_SID,
1714 .step = step,
1715 .op_handle = operation->handle,
1716 };
1717
1718 psa_invec in_vec[] = {
1719 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1720 {.base = data, .len = data_length},
1721 };
1722
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001723 status = API_DISPATCH_NO_OUTVEC(tfm_crypto_key_derivation_input_bytes,
1724 TFM_CRYPTO_KEY_DERIVATION_INPUT_BYTES);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001725 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001726}
1727
1728psa_status_t psa_key_derivation_output_key(
1729 const psa_key_attributes_t *attributes,
1730 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001731 psa_key_id_t *key)
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001732{
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001733 psa_status_t status;
1734 struct tfm_crypto_pack_iovec iov = {
1735 .sfn_id = TFM_CRYPTO_KEY_DERIVATION_OUTPUT_KEY_SID,
1736 .op_handle = operation->handle,
1737 };
1738
1739 psa_invec in_vec[] = {
1740 {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)},
1741 {.base = attributes, .len = sizeof(psa_key_attributes_t)},
1742 };
1743
1744 psa_outvec out_vec[] = {
Maulik Patel28659c42021-01-06 14:09:22 +00001745 {.base = key, .len = sizeof(psa_key_id_t)}
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001746 };
1747
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001748 status = API_DISPATCH(tfm_crypto_key_derivation_output_key,
1749 TFM_CRYPTO_KEY_DERIVATION_OUTPUT_KEY);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001750 return status;
1751}