blob: a45d239aaabc8d7e1959dfa2e8040d89e99083f6 [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
Jamie Fox0e54ebc2019-04-09 14:21:04 +01008#include <stddef.h>
9#include <stdint.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010012
Jamie Fox0e54ebc2019-04-09 14:21:04 +010013#include "tfm_crypto_api.h"
14#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010015#include "tfm_crypto_private.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010016
17/*!
18 * \defgroup public_psa Public functions, PSA
19 *
20 */
21
22/*!@{*/
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010023psa_status_t tfm_crypto_cipher_generate_iv(psa_invec in_vec[],
24 size_t in_len,
25 psa_outvec out_vec[],
26 size_t out_len)
27{
Kevin Peng96f802e2019-12-26 16:10:25 +080028#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010029 return PSA_ERROR_NOT_SUPPORTED;
30#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010031 psa_status_t status = PSA_SUCCESS;
32 psa_cipher_operation_t *operation = NULL;
33
Soby Mathewd8abdfd2020-10-14 10:28:01 +010034 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010035
36 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
37 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010038 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010039 }
40
41 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
42 uint32_t handle = iov->op_handle;
43 uint32_t *handle_out = out_vec[0].base;
44 unsigned char *iv = out_vec[1].base;
45 size_t iv_size = out_vec[1].len;
46
47 /* Init the handle in the operation with the one passed from the iov */
48 *handle_out = iov->op_handle;
49
50 /* Look up the corresponding operation context */
51 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
52 handle,
53 (void **)&operation);
54 if (status != PSA_SUCCESS) {
55 return status;
56 }
57
58 *handle_out = handle;
59
David Hu7e2e5232021-04-21 16:52:07 +080060 return psa_cipher_generate_iv(operation, iv, iv_size, &out_vec[1].len);
Antonio de Angelis7740b382019-07-16 10:59:25 +010061#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010062}
63
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000064psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[],
65 size_t in_len,
66 psa_outvec out_vec[],
67 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +010068{
Kevin Peng96f802e2019-12-26 16:10:25 +080069#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010070 return PSA_ERROR_NOT_SUPPORTED;
71#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010072 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010073 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010074
Soby Mathewd8abdfd2020-10-14 10:28:01 +010075 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +010076
Antonio de Angelis4743e672019-04-11 11:38:48 +010077 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Jamie Fox0e54ebc2019-04-09 14:21:04 +010078 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010079 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000080 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010081 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010082 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010083 uint32_t *handle_out = out_vec[0].base;
84 const unsigned char *iv = in_vec[1].base;
85 size_t iv_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000086
Antonio de Angelis4743e672019-04-11 11:38:48 +010087 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010088 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000089
Antonio de Angelis8908f472018-08-31 15:44:25 +010090 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010093 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000094 if (status != PSA_SUCCESS) {
95 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +010096 }
97
David Hu7e2e5232021-04-21 16:52:07 +080098 return psa_cipher_set_iv(operation, iv, iv_length);
Antonio de Angelis7740b382019-07-16 10:59:25 +010099#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000100}
Antonio de Angelis4743e672019-04-11 11:38:48 +0100101
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000102psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[],
103 size_t in_len,
104 psa_outvec out_vec[],
105 size_t out_len)
106{
Kevin Peng96f802e2019-12-26 16:10:25 +0800107#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100108 return PSA_ERROR_NOT_SUPPORTED;
109#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100110 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100111 psa_cipher_operation_t *operation = NULL;
112
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100113 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114
Antonio de Angelis4743e672019-04-11 11:38:48 +0100115 if ((out_vec[0].len != sizeof(uint32_t)) ||
116 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100117 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000118 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100119 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100120 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100121 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000122 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100123 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000124 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000125
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100126 /* Allocate the operation context in the secure world */
127 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
128 &handle,
129 (void **)&operation);
130 if (status != PSA_SUCCESS) {
131 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100133 *handle_out = handle;
134
Maulik Patel28659c42021-01-06 14:09:22 +0000135 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
136 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800137 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +0000138 }
139
140 status = psa_cipher_encrypt_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100141 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800142 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100143 }
144
Antonio de Angelis4743e672019-04-11 11:38:48 +0100145 return status;
David Hu7e2e5232021-04-21 16:52:07 +0800146
147exit:
148 /* Release the operation context, ignore if the operation fails. */
149 (void)tfm_crypto_operation_release(handle_out);
150 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100151#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100152}
153
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000154psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[],
155 size_t in_len,
156 psa_outvec out_vec[],
157 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100158{
Kevin Peng96f802e2019-12-26 16:10:25 +0800159#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100160 return PSA_ERROR_NOT_SUPPORTED;
161#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100162 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100163 psa_cipher_operation_t *operation = NULL;
164
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100165 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000166
Antonio de Angelis4743e672019-04-11 11:38:48 +0100167 if ((out_vec[0].len != sizeof(uint32_t)) ||
168 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100169 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000170 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100171 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100172 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100173 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000174 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100175 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000176 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000177
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100178 /* Allocate the operation context in the secure world */
179 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
180 &handle,
181 (void **)&operation);
182 if (status != PSA_SUCCESS) {
183 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100184 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100185
186 *handle_out = handle;
Maulik Patel28659c42021-01-06 14:09:22 +0000187 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
188 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800189 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +0000190 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100191
Maulik Patel28659c42021-01-06 14:09:22 +0000192 status = psa_cipher_decrypt_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100193 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800194 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100195 }
196
Antonio de Angelis4743e672019-04-11 11:38:48 +0100197 return status;
David Hu7e2e5232021-04-21 16:52:07 +0800198
199exit:
200 /* Release the operation context, ignore if the operation fails. */
201 (void)tfm_crypto_operation_release(handle_out);
202 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100203#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100204}
205
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000206psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[],
207 size_t in_len,
208 psa_outvec out_vec[],
209 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100210{
Kevin Peng96f802e2019-12-26 16:10:25 +0800211#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100212 return PSA_ERROR_NOT_SUPPORTED;
213#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100214 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100215 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100216
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100217 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100218
Antonio de Angelis4743e672019-04-11 11:38:48 +0100219 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
220 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100221 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000222 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100223
Antonio de Angelis4743e672019-04-11 11:38:48 +0100224 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100225 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100226 uint32_t *handle_out = out_vec[0].base;
227 const uint8_t *input = in_vec[1].base;
228 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000229 unsigned char *output = out_vec[1].base;
230 size_t output_size = out_vec[1].len;
231
Antonio de Angelis4743e672019-04-11 11:38:48 +0100232 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100233 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100234
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000235 /* Initialise the output_length to zero */
236 out_vec[1].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000237
Antonio de Angelis8908f472018-08-31 15:44:25 +0100238 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000239 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100240 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100241 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000242 if (status != PSA_SUCCESS) {
243 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100244 }
245
David Hu7e2e5232021-04-21 16:52:07 +0800246 return psa_cipher_update(operation, input, input_length,
247 output, output_size, &out_vec[1].len);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100248#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100249}
250
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000251psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[],
252 size_t in_len,
253 psa_outvec out_vec[],
254 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100255{
Kevin Peng96f802e2019-12-26 16:10:25 +0800256#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100257 return PSA_ERROR_NOT_SUPPORTED;
258#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100259 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100260 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100261
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100262 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100263
Antonio de Angelis4743e672019-04-11 11:38:48 +0100264 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
265 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100266 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000267 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100268 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100269 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100270 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000271 unsigned char *output = out_vec[1].base;
272 size_t output_size = out_vec[1].len;
273
Antonio de Angelis4743e672019-04-11 11:38:48 +0100274 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100275 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100276
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000277 /* Initialise the output_length to zero */
278 out_vec[1].len = 0;
279
Antonio de Angelis8908f472018-08-31 15:44:25 +0100280 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000281 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100282 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100283 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000284 if (status != PSA_SUCCESS) {
285 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100286 }
287
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100288 status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len);
David Hu7e2e5232021-04-21 16:52:07 +0800289 if (status == PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100290 /* Release the operation context, ignore if the operation fails. */
291 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100292 }
293
Antonio de Angelis4743e672019-04-11 11:38:48 +0100294 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100295#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100296}
297
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000298psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[],
299 size_t in_len,
300 psa_outvec out_vec[],
301 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100302{
Kevin Peng96f802e2019-12-26 16:10:25 +0800303#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100304 return PSA_ERROR_NOT_SUPPORTED;
305#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000306 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100307 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100308
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100309 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100310
Antonio de Angelis4743e672019-04-11 11:38:48 +0100311 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
312 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100313 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000314 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100315 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100316 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100317 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000318
Antonio de Angelis4743e672019-04-11 11:38:48 +0100319 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100320 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000321
Antonio de Angelis8908f472018-08-31 15:44:25 +0100322 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000323 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100324 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100325 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000326 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100327 /* Operation does not exist, so abort has no effect */
328 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100329 }
330
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100331 status = psa_cipher_abort(operation);
332
333 if (status != PSA_SUCCESS) {
334 /* Release the operation context, ignore if the operation fails. */
335 (void)tfm_crypto_operation_release(handle_out);
336 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100337 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100338
David Hu7e2e5232021-04-21 16:52:07 +0800339 return tfm_crypto_operation_release(handle_out);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100340#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100341}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100342
343psa_status_t tfm_crypto_cipher_encrypt(psa_invec in_vec[],
344 size_t in_len,
345 psa_outvec out_vec[],
346 size_t out_len)
347{
Summer Qin045ec4a2021-07-07 14:28:04 +0800348#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100349 return PSA_ERROR_NOT_SUPPORTED;
Summer Qin045ec4a2021-07-07 14:28:04 +0800350#else
351 psa_status_t status = PSA_SUCCESS;
352
353 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
354
355 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
356 return PSA_ERROR_PROGRAMMER_ERROR;
357 }
358
359 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
360 psa_key_id_t key_id = iov->key_id;
361 psa_algorithm_t alg = iov->alg;
362 const uint8_t *input = in_vec[1].base;
363 size_t input_length = in_vec[1].len;
364 uint8_t *output = out_vec[0].base;
365 size_t output_size = out_vec[0].len;
366 mbedtls_svc_key_id_t encoded_key;
367
Summer Qin045ec4a2021-07-07 14:28:04 +0800368 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
369 if (status != PSA_SUCCESS) {
370 return status;
371 }
372
373 return psa_cipher_encrypt(encoded_key, alg, input, input_length, output,
374 output_size, &out_vec[0].len);
375#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100376}
377
378psa_status_t tfm_crypto_cipher_decrypt(psa_invec in_vec[],
379 size_t in_len,
380 psa_outvec out_vec[],
381 size_t out_len)
382{
Summer Qin045ec4a2021-07-07 14:28:04 +0800383#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100384 return PSA_ERROR_NOT_SUPPORTED;
Summer Qin045ec4a2021-07-07 14:28:04 +0800385#else
386 psa_status_t status = PSA_SUCCESS;
387
388 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
389
390 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
391 return PSA_ERROR_PROGRAMMER_ERROR;
392 }
393 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
394 psa_key_id_t key_id = iov->key_id;
395 psa_algorithm_t alg = iov->alg;
396 const uint8_t *input = in_vec[1].base;
397 size_t input_length = in_vec[1].len;
398 uint8_t *output = out_vec[0].base;
399 size_t output_size = out_vec[0].len;
400 mbedtls_svc_key_id_t encoded_key;
401
Summer Qin045ec4a2021-07-07 14:28:04 +0800402 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
403 if (status != PSA_SUCCESS) {
404 return status;
405 }
406
407 return psa_cipher_decrypt(encoded_key, alg, input, input_length, output,
408 output_size, &out_vec[0].len);
409#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100410}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100411/*!@}*/