blob: 03849dfd9710c689a69bcb044b2b1468c191132c [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
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;
Maulik Patel28659c42021-01-06 14:09:22 +0000136 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100137 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000138 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000139
Maulik Patel28659c42021-01-06 14:09:22 +0000140 status = tfm_crypto_check_handle_owner(key_id, NULL);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100141 if (status != PSA_SUCCESS) {
142 return status;
143 }
144
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100145 /* Allocate the operation context in the secure world */
146 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
147 &handle,
148 (void **)&operation);
149 if (status != PSA_SUCCESS) {
150 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100151 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100152 *handle_out = handle;
153
Maulik Patel28659c42021-01-06 14:09:22 +0000154 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
155 if (status != PSA_SUCCESS) {
156 return status;
157 }
158
159 status = psa_cipher_encrypt_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100160 if (status != PSA_SUCCESS) {
161 /* Release the operation context, ignore if the operation fails. */
162 (void)tfm_crypto_operation_release(handle_out);
163 return status;
164 }
165
Antonio de Angelis4743e672019-04-11 11:38:48 +0100166 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100167#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100168}
169
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000170psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[],
171 size_t in_len,
172 psa_outvec out_vec[],
173 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174{
Kevin Peng96f802e2019-12-26 16:10:25 +0800175#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100176 return PSA_ERROR_NOT_SUPPORTED;
177#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100178 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100179 psa_cipher_operation_t *operation = NULL;
180
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100181 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000182
Antonio de Angelis4743e672019-04-11 11:38:48 +0100183 if ((out_vec[0].len != sizeof(uint32_t)) ||
184 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100185 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000186 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100187 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100188 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100189 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000190 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100191 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000192 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000193
Maulik Patel28659c42021-01-06 14:09:22 +0000194 status = tfm_crypto_check_handle_owner(key_id, NULL);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100195 if (status != PSA_SUCCESS) {
196 return status;
197 }
198
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100199 /* Allocate the operation context in the secure world */
200 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
201 &handle,
202 (void **)&operation);
203 if (status != PSA_SUCCESS) {
204 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100205 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100206
207 *handle_out = handle;
Maulik Patel28659c42021-01-06 14:09:22 +0000208 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
209 if (status != PSA_SUCCESS) {
210 return status;
211 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100212
Maulik Patel28659c42021-01-06 14:09:22 +0000213 status = psa_cipher_decrypt_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100214 if (status != PSA_SUCCESS) {
215 /* Release the operation context, ignore if the operation fails. */
216 (void)tfm_crypto_operation_release(handle_out);
217 return status;
218 }
219
Antonio de Angelis4743e672019-04-11 11:38:48 +0100220 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100221#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100222}
223
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[],
225 size_t in_len,
226 psa_outvec out_vec[],
227 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100228{
Kevin Peng96f802e2019-12-26 16:10:25 +0800229#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100230 return PSA_ERROR_NOT_SUPPORTED;
231#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100232 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100233 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100234
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100235 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100236
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
238 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100239 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000240 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100241
Antonio de Angelis4743e672019-04-11 11:38:48 +0100242 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100243 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100244 uint32_t *handle_out = out_vec[0].base;
245 const uint8_t *input = in_vec[1].base;
246 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000247 unsigned char *output = out_vec[1].base;
248 size_t output_size = out_vec[1].len;
249
Antonio de Angelis4743e672019-04-11 11:38:48 +0100250 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100251 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100252
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000253 /* Initialise the output_length to zero */
254 out_vec[1].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000255
Antonio de Angelis8908f472018-08-31 15:44:25 +0100256 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000257 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100258 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100259 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000260 if (status != PSA_SUCCESS) {
261 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100262 }
263
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100264 status = psa_cipher_update(operation, input, input_length,
265 output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100266 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100267 /* Release the operation context, ignore if the operation fails. */
268 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000269 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100270 }
271
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100272 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100273#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100274}
275
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000276psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[],
277 size_t in_len,
278 psa_outvec out_vec[],
279 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100280{
Kevin Peng96f802e2019-12-26 16:10:25 +0800281#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100282 return PSA_ERROR_NOT_SUPPORTED;
283#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100284 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100285 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100286
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100287 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100288
Antonio de Angelis4743e672019-04-11 11:38:48 +0100289 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
290 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100291 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000292 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100293 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100294 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100295 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000296 unsigned char *output = out_vec[1].base;
297 size_t output_size = out_vec[1].len;
298
Antonio de Angelis4743e672019-04-11 11:38:48 +0100299 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100300 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100301
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000302 /* Initialise the output_length to zero */
303 out_vec[1].len = 0;
304
Antonio de Angelis8908f472018-08-31 15:44:25 +0100305 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000306 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100307 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100308 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000309 if (status != PSA_SUCCESS) {
310 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100311 }
312
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100313 status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100314 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100315 /* Release the operation context, ignore if the operation fails. */
316 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000317 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100318 }
319
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100320 status = tfm_crypto_operation_release(handle_out);
321
Antonio de Angelis4743e672019-04-11 11:38:48 +0100322 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100323#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100324}
325
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000326psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[],
327 size_t in_len,
328 psa_outvec out_vec[],
329 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100330{
Kevin Peng96f802e2019-12-26 16:10:25 +0800331#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100332 return PSA_ERROR_NOT_SUPPORTED;
333#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000334 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100335 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100336
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100337 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100338
Antonio de Angelis4743e672019-04-11 11:38:48 +0100339 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
340 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100341 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000342 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100343 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100344 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100345 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000346
Antonio de Angelis4743e672019-04-11 11:38:48 +0100347 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100348 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000349
Antonio de Angelis8908f472018-08-31 15:44:25 +0100350 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000351 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100352 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100353 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000354 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100355 /* Operation does not exist, so abort has no effect */
356 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100357 }
358
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100359 status = psa_cipher_abort(operation);
360
361 if (status != PSA_SUCCESS) {
362 /* Release the operation context, ignore if the operation fails. */
363 (void)tfm_crypto_operation_release(handle_out);
364 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100365 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100366
367 status = tfm_crypto_operation_release(handle_out);
368
Antonio de Angelis4743e672019-04-11 11:38:48 +0100369 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100370#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100371}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100372
373psa_status_t tfm_crypto_cipher_encrypt(psa_invec in_vec[],
374 size_t in_len,
375 psa_outvec out_vec[],
376 size_t out_len)
377{
378 /* FixMe: To be implemented */
379 return PSA_ERROR_NOT_SUPPORTED;
380}
381
382psa_status_t tfm_crypto_cipher_decrypt(psa_invec in_vec[],
383 size_t in_len,
384 psa_outvec out_vec[],
385 size_t out_len)
386{
387 /* FixMe: To be implemented */
388 return PSA_ERROR_NOT_SUPPORTED;
389}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100390/*!@}*/