blob: 6e47f61b160cd2ef9b0af8e5dbdc692862230778 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, 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
60 status = psa_cipher_generate_iv(operation, iv, iv_size, &out_vec[1].len);
61 if (status != PSA_SUCCESS) {
62 /* Release the operation context, ignore if the operation fails. */
63 (void)tfm_crypto_operation_release(handle_out);
64 return status;
65 }
66
67 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010068#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010069}
70
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000071psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[],
72 size_t in_len,
73 psa_outvec out_vec[],
74 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +010075{
Kevin Peng96f802e2019-12-26 16:10:25 +080076#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010077 return PSA_ERROR_NOT_SUPPORTED;
78#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010079 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010080 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010081
Soby Mathewd8abdfd2020-10-14 10:28:01 +010082 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +010083
Antonio de Angelis4743e672019-04-11 11:38:48 +010084 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Jamie Fox0e54ebc2019-04-09 14:21:04 +010085 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010086 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000087 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010088 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010089 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010090 uint32_t *handle_out = out_vec[0].base;
91 const unsigned char *iv = in_vec[1].base;
92 size_t iv_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000093
Antonio de Angelis4743e672019-04-11 11:38:48 +010094 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010095 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000096
Antonio de Angelis8908f472018-08-31 15:44:25 +010097 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000098 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +010099 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100100 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000101 if (status != PSA_SUCCESS) {
102 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100103 }
104
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100105 status = psa_cipher_set_iv(operation, iv, iv_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100106 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100107 /* Release the operation context, ignore if the operation fails. */
108 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000109 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100110 }
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100111
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000112 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100113#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114}
Antonio de Angelis4743e672019-04-11 11:38:48 +0100115
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000116psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[],
117 size_t in_len,
118 psa_outvec out_vec[],
119 size_t out_len)
120{
Kevin Peng96f802e2019-12-26 16:10:25 +0800121#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100122 return PSA_ERROR_NOT_SUPPORTED;
123#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100124 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100125 psa_cipher_operation_t *operation = NULL;
126
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100127 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000128
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 if ((out_vec[0].len != sizeof(uint32_t)) ||
130 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100131 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000132 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100133 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100134 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100135 uint32_t *handle_out = out_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100136 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100137 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000138
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100139 status = tfm_crypto_check_handle_owner(key_handle, NULL);
140 if (status != PSA_SUCCESS) {
141 return status;
142 }
143
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100144 /* Allocate the operation context in the secure world */
145 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
146 &handle,
147 (void **)&operation);
148 if (status != PSA_SUCCESS) {
149 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100150 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100151
152 *handle_out = handle;
153
154 status = psa_cipher_encrypt_setup(operation, key_handle, alg);
155 if (status != PSA_SUCCESS) {
156 /* Release the operation context, ignore if the operation fails. */
157 (void)tfm_crypto_operation_release(handle_out);
158 return status;
159 }
160
Antonio de Angelis4743e672019-04-11 11:38:48 +0100161 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100162#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100163}
164
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000165psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[],
166 size_t in_len,
167 psa_outvec out_vec[],
168 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100169{
Kevin Peng96f802e2019-12-26 16:10:25 +0800170#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100171 return PSA_ERROR_NOT_SUPPORTED;
172#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100173 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100174 psa_cipher_operation_t *operation = NULL;
175
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100176 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000177
Antonio de Angelis4743e672019-04-11 11:38:48 +0100178 if ((out_vec[0].len != sizeof(uint32_t)) ||
179 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100180 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000181 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100182 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100183 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100184 uint32_t *handle_out = out_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100185 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100186 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000187
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100188 status = tfm_crypto_check_handle_owner(key_handle, NULL);
189 if (status != PSA_SUCCESS) {
190 return status;
191 }
192
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100193 /* Allocate the operation context in the secure world */
194 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
195 &handle,
196 (void **)&operation);
197 if (status != PSA_SUCCESS) {
198 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100199 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100200
201 *handle_out = handle;
202
203 status = psa_cipher_decrypt_setup(operation, key_handle, alg);
204 if (status != PSA_SUCCESS) {
205 /* Release the operation context, ignore if the operation fails. */
206 (void)tfm_crypto_operation_release(handle_out);
207 return status;
208 }
209
Antonio de Angelis4743e672019-04-11 11:38:48 +0100210 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100211#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100212}
213
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000214psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[],
215 size_t in_len,
216 psa_outvec out_vec[],
217 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100218{
Kevin Peng96f802e2019-12-26 16:10:25 +0800219#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100220 return PSA_ERROR_NOT_SUPPORTED;
221#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100222 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100223 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100224
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100225 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100226
Antonio de Angelis4743e672019-04-11 11:38:48 +0100227 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
228 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100229 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000230 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100231
Antonio de Angelis4743e672019-04-11 11:38:48 +0100232 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100233 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100234 uint32_t *handle_out = out_vec[0].base;
235 const uint8_t *input = in_vec[1].base;
236 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000237 unsigned char *output = out_vec[1].base;
238 size_t output_size = out_vec[1].len;
239
Antonio de Angelis4743e672019-04-11 11:38:48 +0100240 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100241 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100242
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000243 /* Initialise the output_length to zero */
244 out_vec[1].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000245
Antonio de Angelis8908f472018-08-31 15:44:25 +0100246 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000247 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100248 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100249 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000250 if (status != PSA_SUCCESS) {
251 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100252 }
253
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100254 status = psa_cipher_update(operation, input, input_length,
255 output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100256 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100257 /* Release the operation context, ignore if the operation fails. */
258 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000259 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100260 }
261
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100262 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100263#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100264}
265
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000266psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[],
267 size_t in_len,
268 psa_outvec out_vec[],
269 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100270{
Kevin Peng96f802e2019-12-26 16:10:25 +0800271#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100272 return PSA_ERROR_NOT_SUPPORTED;
273#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100274 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100275 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100276
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100277 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100278
Antonio de Angelis4743e672019-04-11 11:38:48 +0100279 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
280 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100281 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000282 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100283 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100284 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100285 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000286 unsigned char *output = out_vec[1].base;
287 size_t output_size = out_vec[1].len;
288
Antonio de Angelis4743e672019-04-11 11:38:48 +0100289 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100290 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100291
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000292 /* Initialise the output_length to zero */
293 out_vec[1].len = 0;
294
Antonio de Angelis8908f472018-08-31 15:44:25 +0100295 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000296 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100297 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100298 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000299 if (status != PSA_SUCCESS) {
300 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100301 }
302
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100303 status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100304 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100305 /* Release the operation context, ignore if the operation fails. */
306 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000307 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100308 }
309
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100310 status = tfm_crypto_operation_release(handle_out);
311
Antonio de Angelis4743e672019-04-11 11:38:48 +0100312 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100313#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100314}
315
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000316psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[],
317 size_t in_len,
318 psa_outvec out_vec[],
319 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100320{
Kevin Peng96f802e2019-12-26 16:10:25 +0800321#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100322 return PSA_ERROR_NOT_SUPPORTED;
323#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000324 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100325 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100326
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100327 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100328
Antonio de Angelis4743e672019-04-11 11:38:48 +0100329 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
330 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100331 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000332 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100333 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100334 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100335 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000336
Antonio de Angelis4743e672019-04-11 11:38:48 +0100337 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100338 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000339
Antonio de Angelis8908f472018-08-31 15:44:25 +0100340 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000341 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100342 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100343 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000344 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100345 /* Operation does not exist, so abort has no effect */
346 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100347 }
348
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100349 status = psa_cipher_abort(operation);
350
351 if (status != PSA_SUCCESS) {
352 /* Release the operation context, ignore if the operation fails. */
353 (void)tfm_crypto_operation_release(handle_out);
354 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100355 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100356
357 status = tfm_crypto_operation_release(handle_out);
358
Antonio de Angelis4743e672019-04-11 11:38:48 +0100359 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100360#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100361}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100362
363psa_status_t tfm_crypto_cipher_encrypt(psa_invec in_vec[],
364 size_t in_len,
365 psa_outvec out_vec[],
366 size_t out_len)
367{
368 /* FixMe: To be implemented */
369 return PSA_ERROR_NOT_SUPPORTED;
370}
371
372psa_status_t tfm_crypto_cipher_decrypt(psa_invec in_vec[],
373 size_t in_len,
374 psa_outvec out_vec[],
375 size_t out_len)
376{
377 /* FixMe: To be implemented */
378 return PSA_ERROR_NOT_SUPPORTED;
379}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100380/*!@}*/