blob: abaa259e2f7f1c6017404965c3eb204a3e893fd2 [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
Summer Qin4b1d03b2019-07-02 14:56:08 +080011/* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter
Antonio de Angelis4743e672019-04-11 11:38:48 +010012 * integrity checks but this will have to be revised
13 * when the full set of error codes mandated by PSA FF
14 * is available.
15 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010016#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010017
Jamie Fox0e54ebc2019-04-09 14:21:04 +010018#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010020#include "tfm_crypto_private.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010021
22/*!
23 * \defgroup public_psa Public functions, PSA
24 *
25 */
26
27/*!@{*/
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010028psa_status_t tfm_crypto_cipher_generate_iv(psa_invec in_vec[],
29 size_t in_len,
30 psa_outvec out_vec[],
31 size_t out_len)
32{
Kevin Peng96f802e2019-12-26 16:10:25 +080033#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010034 return PSA_ERROR_NOT_SUPPORTED;
35#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010036 psa_status_t status = PSA_SUCCESS;
37 psa_cipher_operation_t *operation = NULL;
38
Soby Mathewd8abdfd2020-10-14 10:28:01 +010039 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010040
41 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
42 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080043 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010044 }
45
46 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
47 uint32_t handle = iov->op_handle;
48 uint32_t *handle_out = out_vec[0].base;
49 unsigned char *iv = out_vec[1].base;
50 size_t iv_size = out_vec[1].len;
51
52 /* Init the handle in the operation with the one passed from the iov */
53 *handle_out = iov->op_handle;
54
55 /* Look up the corresponding operation context */
56 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
57 handle,
58 (void **)&operation);
59 if (status != PSA_SUCCESS) {
60 return status;
61 }
62
63 *handle_out = handle;
64
65 status = psa_cipher_generate_iv(operation, iv, iv_size, &out_vec[1].len);
66 if (status != PSA_SUCCESS) {
67 /* Release the operation context, ignore if the operation fails. */
68 (void)tfm_crypto_operation_release(handle_out);
69 return status;
70 }
71
72 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010073#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010074}
75
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000076psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[],
77 size_t in_len,
78 psa_outvec out_vec[],
79 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +010080{
Kevin Peng96f802e2019-12-26 16:10:25 +080081#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010082 return PSA_ERROR_NOT_SUPPORTED;
83#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010084 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010085 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010086
Soby Mathewd8abdfd2020-10-14 10:28:01 +010087 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +010088
Antonio de Angelis4743e672019-04-11 11:38:48 +010089 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Jamie Fox0e54ebc2019-04-09 14:21:04 +010090 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080091 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000092 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010093 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010094 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010095 uint32_t *handle_out = out_vec[0].base;
96 const unsigned char *iv = in_vec[1].base;
97 size_t iv_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000098
Antonio de Angelis4743e672019-04-11 11:38:48 +010099 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100100 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000101
Antonio de Angelis8908f472018-08-31 15:44:25 +0100102 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000103 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100104 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100105 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000106 if (status != PSA_SUCCESS) {
107 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100108 }
109
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100110 status = psa_cipher_set_iv(operation, iv, iv_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100111 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100112 /* Release the operation context, ignore if the operation fails. */
113 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000114 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100115 }
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100116
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000117 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100118#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000119}
Antonio de Angelis4743e672019-04-11 11:38:48 +0100120
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000121psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[],
122 size_t in_len,
123 psa_outvec out_vec[],
124 size_t out_len)
125{
Kevin Peng96f802e2019-12-26 16:10:25 +0800126#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100127 return PSA_ERROR_NOT_SUPPORTED;
128#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100130 psa_cipher_operation_t *operation = NULL;
131
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100132 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000133
Antonio de Angelis4743e672019-04-11 11:38:48 +0100134 if ((out_vec[0].len != sizeof(uint32_t)) ||
135 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800136 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000137 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100138 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100139 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100140 uint32_t *handle_out = out_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100141 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100142 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000143
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100144 status = tfm_crypto_check_handle_owner(key_handle, NULL);
145 if (status != PSA_SUCCESS) {
146 return status;
147 }
148
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100149 /* Allocate the operation context in the secure world */
150 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
151 &handle,
152 (void **)&operation);
153 if (status != PSA_SUCCESS) {
154 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100155 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100156
157 *handle_out = handle;
158
159 status = psa_cipher_encrypt_setup(operation, key_handle, alg);
160 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))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800185 return PSA_ERROR_CONNECTION_REFUSED;
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;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100190 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100191 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100193 status = tfm_crypto_check_handle_owner(key_handle, NULL);
194 if (status != PSA_SUCCESS) {
195 return status;
196 }
197
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100198 /* Allocate the operation context in the secure world */
199 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
200 &handle,
201 (void **)&operation);
202 if (status != PSA_SUCCESS) {
203 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100204 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100205
206 *handle_out = handle;
207
208 status = psa_cipher_decrypt_setup(operation, key_handle, alg);
209 if (status != PSA_SUCCESS) {
210 /* Release the operation context, ignore if the operation fails. */
211 (void)tfm_crypto_operation_release(handle_out);
212 return status;
213 }
214
Antonio de Angelis4743e672019-04-11 11:38:48 +0100215 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100216#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100217}
218
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000219psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[],
220 size_t in_len,
221 psa_outvec out_vec[],
222 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100223{
Kevin Peng96f802e2019-12-26 16:10:25 +0800224#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100225 return PSA_ERROR_NOT_SUPPORTED;
226#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100227 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100228 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100229
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100230 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100231
Antonio de Angelis4743e672019-04-11 11:38:48 +0100232 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
233 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800234 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000235 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100236
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100238 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100239 uint32_t *handle_out = out_vec[0].base;
240 const uint8_t *input = in_vec[1].base;
241 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000242 unsigned char *output = out_vec[1].base;
243 size_t output_size = out_vec[1].len;
244
Antonio de Angelis4743e672019-04-11 11:38:48 +0100245 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100246 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100247
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000248 /* Initialise the output_length to zero */
249 out_vec[1].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000250
Antonio de Angelis8908f472018-08-31 15:44:25 +0100251 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000252 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100253 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100254 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000255 if (status != PSA_SUCCESS) {
256 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100257 }
258
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100259 status = psa_cipher_update(operation, input, input_length,
260 output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100261 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100262 /* Release the operation context, ignore if the operation fails. */
263 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000264 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100265 }
266
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100267 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100268#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100269}
270
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000271psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[],
272 size_t in_len,
273 psa_outvec out_vec[],
274 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100275{
Kevin Peng96f802e2019-12-26 16:10:25 +0800276#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100277 return PSA_ERROR_NOT_SUPPORTED;
278#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100279 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100280 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100281
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100282 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100283
Antonio de Angelis4743e672019-04-11 11:38:48 +0100284 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
285 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800286 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000287 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100288 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100289 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100290 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000291 unsigned char *output = out_vec[1].base;
292 size_t output_size = out_vec[1].len;
293
Antonio de Angelis4743e672019-04-11 11:38:48 +0100294 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100295 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100296
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000297 /* Initialise the output_length to zero */
298 out_vec[1].len = 0;
299
Antonio de Angelis8908f472018-08-31 15:44:25 +0100300 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000301 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100302 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100303 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000304 if (status != PSA_SUCCESS) {
305 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100306 }
307
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100308 status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100309 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100310 /* Release the operation context, ignore if the operation fails. */
311 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000312 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100313 }
314
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100315 status = tfm_crypto_operation_release(handle_out);
316
Antonio de Angelis4743e672019-04-11 11:38:48 +0100317 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100318#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100319}
320
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000321psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[],
322 size_t in_len,
323 psa_outvec out_vec[],
324 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100325{
Kevin Peng96f802e2019-12-26 16:10:25 +0800326#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100327 return PSA_ERROR_NOT_SUPPORTED;
328#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000329 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100330 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100331
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100332 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100333
Antonio de Angelis4743e672019-04-11 11:38:48 +0100334 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
335 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800336 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000337 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100338 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100339 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100340 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000341
Antonio de Angelis4743e672019-04-11 11:38:48 +0100342 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100343 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000344
Antonio de Angelis8908f472018-08-31 15:44:25 +0100345 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000346 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100347 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100348 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000349 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100350 /* Operation does not exist, so abort has no effect */
351 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100352 }
353
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100354 status = psa_cipher_abort(operation);
355
356 if (status != PSA_SUCCESS) {
357 /* Release the operation context, ignore if the operation fails. */
358 (void)tfm_crypto_operation_release(handle_out);
359 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100360 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100361
362 status = tfm_crypto_operation_release(handle_out);
363
Antonio de Angelis4743e672019-04-11 11:38:48 +0100364 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100365#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100366}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100367
368psa_status_t tfm_crypto_cipher_encrypt(psa_invec in_vec[],
369 size_t in_len,
370 psa_outvec out_vec[],
371 size_t out_len)
372{
373 /* FixMe: To be implemented */
374 return PSA_ERROR_NOT_SUPPORTED;
375}
376
377psa_status_t tfm_crypto_cipher_decrypt(psa_invec in_vec[],
378 size_t in_len,
379 psa_outvec out_vec[],
380 size_t out_len)
381{
382 /* FixMe: To be implemented */
383 return PSA_ERROR_NOT_SUPPORTED;
384}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100385/*!@}*/