blob: eb0c3a52d8adfa01a191a08a1fdaaaf41084e799 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis377a1552018-11-22 17:02:40 +00002 * Copyright (c) 2018-2019, 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"
Antonio de Angelis8908f472018-08-31 15:44:25 +010020
21/*!
22 * \defgroup public_psa Public functions, PSA
23 *
24 */
25
26/*!@{*/
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010027psa_status_t tfm_crypto_cipher_generate_iv(psa_invec in_vec[],
28 size_t in_len,
29 psa_outvec out_vec[],
30 size_t out_len)
31{
Antonio de Angelis7740b382019-07-16 10:59:25 +010032#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
33 return PSA_ERROR_NOT_SUPPORTED;
34#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010035 psa_status_t status = PSA_SUCCESS;
36 psa_cipher_operation_t *operation = NULL;
37
38 if ((in_len != 1) || (out_len != 2)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080039 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010040 }
41
42 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
43 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080044 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010045 }
46
47 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
48 uint32_t handle = iov->op_handle;
49 uint32_t *handle_out = out_vec[0].base;
50 unsigned char *iv = out_vec[1].base;
51 size_t iv_size = out_vec[1].len;
52
53 /* Init the handle in the operation with the one passed from the iov */
54 *handle_out = iov->op_handle;
55
56 /* Look up the corresponding operation context */
57 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
58 handle,
59 (void **)&operation);
60 if (status != PSA_SUCCESS) {
61 return status;
62 }
63
64 *handle_out = handle;
65
66 status = psa_cipher_generate_iv(operation, iv, iv_size, &out_vec[1].len);
67 if (status != PSA_SUCCESS) {
68 /* Release the operation context, ignore if the operation fails. */
69 (void)tfm_crypto_operation_release(handle_out);
70 return status;
71 }
72
73 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010074#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010075}
76
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000077psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[],
78 size_t in_len,
79 psa_outvec out_vec[],
80 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +010081{
Antonio de Angelis7740b382019-07-16 10:59:25 +010082#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
83 return PSA_ERROR_NOT_SUPPORTED;
84#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010085 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010086 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +010087
Antonio de Angelis4743e672019-04-11 11:38:48 +010088 if ((in_len != 2) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080089 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +010090 }
91
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Jamie Fox0e54ebc2019-04-09 14:21:04 +010093 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080094 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000095 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010096 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010097 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010098 uint32_t *handle_out = out_vec[0].base;
99 const unsigned char *iv = in_vec[1].base;
100 size_t iv_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000101
Antonio de Angelis4743e672019-04-11 11:38:48 +0100102 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100103 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000104
Antonio de Angelis8908f472018-08-31 15:44:25 +0100105 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000106 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100107 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100108 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000109 if (status != PSA_SUCCESS) {
110 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100111 }
112
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100113 status = psa_cipher_set_iv(operation, iv, iv_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100114 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100115 /* Release the operation context, ignore if the operation fails. */
116 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000117 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100118 }
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100119
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000120 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100121#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122}
Antonio de Angelis4743e672019-04-11 11:38:48 +0100123
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000124psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[],
125 size_t in_len,
126 psa_outvec out_vec[],
127 size_t out_len)
128{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100129#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
130 return PSA_ERROR_NOT_SUPPORTED;
131#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100133 psa_cipher_operation_t *operation = NULL;
134
Antonio de Angelis4743e672019-04-11 11:38:48 +0100135 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800136 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000137 }
138
Antonio de Angelis4743e672019-04-11 11:38:48 +0100139 if ((out_vec[0].len != sizeof(uint32_t)) ||
140 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800141 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000142 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100143 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100144 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100145 uint32_t *handle_out = out_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100146 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100147 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000148
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100149 status = tfm_crypto_check_handle_owner(key_handle, NULL);
150 if (status != PSA_SUCCESS) {
151 return status;
152 }
153
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100154 /* Allocate the operation context in the secure world */
155 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
156 &handle,
157 (void **)&operation);
158 if (status != PSA_SUCCESS) {
159 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100160 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100161
162 *handle_out = handle;
163
164 status = psa_cipher_encrypt_setup(operation, key_handle, alg);
165 if (status != PSA_SUCCESS) {
166 /* Release the operation context, ignore if the operation fails. */
167 (void)tfm_crypto_operation_release(handle_out);
168 return status;
169 }
170
Antonio de Angelis4743e672019-04-11 11:38:48 +0100171 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100172#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100173}
174
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000175psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[],
176 size_t in_len,
177 psa_outvec out_vec[],
178 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100179{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100180#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
181 return PSA_ERROR_NOT_SUPPORTED;
182#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100183 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100184 psa_cipher_operation_t *operation = NULL;
185
Antonio de Angelis4743e672019-04-11 11:38:48 +0100186 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800187 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000188 }
189
Antonio de Angelis4743e672019-04-11 11:38:48 +0100190 if ((out_vec[0].len != sizeof(uint32_t)) ||
191 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800192 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000193 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100194 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100195 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100196 uint32_t *handle_out = out_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100197 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100198 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000199
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100200 status = tfm_crypto_check_handle_owner(key_handle, NULL);
201 if (status != PSA_SUCCESS) {
202 return status;
203 }
204
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100205 /* Allocate the operation context in the secure world */
206 status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
207 &handle,
208 (void **)&operation);
209 if (status != PSA_SUCCESS) {
210 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100211 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100212
213 *handle_out = handle;
214
215 status = psa_cipher_decrypt_setup(operation, key_handle, alg);
216 if (status != PSA_SUCCESS) {
217 /* Release the operation context, ignore if the operation fails. */
218 (void)tfm_crypto_operation_release(handle_out);
219 return status;
220 }
221
Antonio de Angelis4743e672019-04-11 11:38:48 +0100222 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100223#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100224}
225
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000226psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[],
227 size_t in_len,
228 psa_outvec out_vec[],
229 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100230{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100231#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
232 return PSA_ERROR_NOT_SUPPORTED;
233#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100234 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100235 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100236
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 if ((in_len != 2) || (out_len != 2)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800238 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100239 }
240
Antonio de Angelis4743e672019-04-11 11:38:48 +0100241 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
242 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800243 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000244 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100245 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100246 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100247 uint32_t *handle_out = out_vec[0].base;
248 const uint8_t *input = in_vec[1].base;
249 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000250 unsigned char *output = out_vec[1].base;
251 size_t output_size = out_vec[1].len;
252
Antonio de Angelis4743e672019-04-11 11:38:48 +0100253 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100254 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100255
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000256 /* Initialise the output_length to zero */
257 out_vec[1].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000258
Antonio de Angelis8908f472018-08-31 15:44:25 +0100259 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000260 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100261 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100262 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000263 if (status != PSA_SUCCESS) {
264 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100265 }
266
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100267 status = psa_cipher_update(operation, input, input_length,
268 output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100269 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100270 /* Release the operation context, ignore if the operation fails. */
271 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000272 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100273 }
274
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100275 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100276#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100277}
278
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000279psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[],
280 size_t in_len,
281 psa_outvec out_vec[],
282 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100283{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100284#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
285 return PSA_ERROR_NOT_SUPPORTED;
286#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100287 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100288 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100289
Antonio de Angelis4743e672019-04-11 11:38:48 +0100290 if ((in_len != 1) || (out_len != 2)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800291 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100292 }
293
Antonio de Angelis4743e672019-04-11 11:38:48 +0100294 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
295 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800296 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000297 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100298 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100299 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100300 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000301 unsigned char *output = out_vec[1].base;
302 size_t output_size = out_vec[1].len;
303
Antonio de Angelis4743e672019-04-11 11:38:48 +0100304 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100305 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100306
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000307 /* Initialise the output_length to zero */
308 out_vec[1].len = 0;
309
Antonio de Angelis8908f472018-08-31 15:44:25 +0100310 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000311 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100312 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100313 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000314 if (status != PSA_SUCCESS) {
315 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100316 }
317
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100318 status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100319 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100320 /* Release the operation context, ignore if the operation fails. */
321 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000322 return status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100323 }
324
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100325 status = tfm_crypto_operation_release(handle_out);
326
Antonio de Angelis4743e672019-04-11 11:38:48 +0100327 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100328#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100329}
330
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000331psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[],
332 size_t in_len,
333 psa_outvec out_vec[],
334 size_t out_len)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100335{
Antonio de Angelis7740b382019-07-16 10:59:25 +0100336#if (TFM_CRYPTO_CIPHER_MODULE_DISABLED != 0)
337 return PSA_ERROR_NOT_SUPPORTED;
338#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000339 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100340 psa_cipher_operation_t *operation = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100341
Antonio de Angelis4743e672019-04-11 11:38:48 +0100342 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800343 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100344 }
345
Antonio de Angelis4743e672019-04-11 11:38:48 +0100346 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
347 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800348 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000349 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100350 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100351 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100352 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000353
Antonio de Angelis4743e672019-04-11 11:38:48 +0100354 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100355 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000356
Antonio de Angelis8908f472018-08-31 15:44:25 +0100357 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000358 status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100359 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100360 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000361 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100362 /* Operation does not exist, so abort has no effect */
363 return PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100364 }
365
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100366 status = psa_cipher_abort(operation);
367
368 if (status != PSA_SUCCESS) {
369 /* Release the operation context, ignore if the operation fails. */
370 (void)tfm_crypto_operation_release(handle_out);
371 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100372 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100373
374 status = tfm_crypto_operation_release(handle_out);
375
Antonio de Angelis4743e672019-04-11 11:38:48 +0100376 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100377#endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100378}
379/*!@}*/