blob: a88f4a832a3c37710b39350d70a74b75d097462f [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
8#include <limits.h>
9
10#include "tfm_crypto_defs.h"
11
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010012#include "crypto_engine.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010013
14#include "psa_crypto.h"
15
Antonio de Angelis377a1552018-11-22 17:02:40 +000016#include "tfm_crypto_struct.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010017
18#include "tfm_crypto_api.h"
19#include "crypto_utils.h"
20
Jamie Fox82b87ca2018-12-11 16:41:11 +000021/**
22 * \def CRYPTO_CIPHER_MAX_KEY_LENGTH
23 *
24 * \brief Specifies the maximum key length supported by the
25 * Cipher operations in this implementation
26 */
27#ifndef CRYPTO_CIPHER_MAX_KEY_LENGTH
28#define CRYPTO_CIPHER_MAX_KEY_LENGTH (32)
29#endif
30
Antonio de Angelis8908f472018-08-31 15:44:25 +010031static enum tfm_crypto_err_t tfm_crypto_cipher_setup(
Antonio de Angelis377a1552018-11-22 17:02:40 +000032 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +010033 psa_key_slot_t key,
34 psa_algorithm_t alg,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010035 enum engine_cipher_mode_t c_mode)
Antonio de Angelis8908f472018-08-31 15:44:25 +010036{
Jamie Fox82b87ca2018-12-11 16:41:11 +000037 uint8_t key_data[CRYPTO_CIPHER_MAX_KEY_LENGTH];
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010038 size_t key_size;
39 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
40 psa_status_t status = PSA_SUCCESS;
41 enum tfm_crypto_err_t err;
Antonio de Angelis377a1552018-11-22 17:02:40 +000042 struct tfm_cipher_operation_s *ctx = NULL;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010043 struct cipher_engine_info engine_info;
Jamie Foxefd82732018-11-26 10:34:32 +000044 psa_key_usage_t usage;
Antonio de Angelis8908f472018-08-31 15:44:25 +010045
46 /* Validate pointers */
Antonio de Angelis377a1552018-11-22 17:02:40 +000047 err = tfm_crypto_memory_check(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +010048 sizeof(psa_cipher_operation_t),
49 TFM_MEMORY_ACCESS_RW);
50 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
51 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
52 }
53
54 if (!PSA_ALG_IS_CIPHER(alg)) {
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010055 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +010056 }
57
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010058 /* Access the key module to retrieve key related information */
Antonio de Angelis8908f472018-08-31 15:44:25 +010059 err = tfm_crypto_get_key_information(key, &key_type, &key_size);
60 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
Jamie Fox82b87ca2018-12-11 16:41:11 +000061 return err;
62 }
63
64 /* Check if it's a raw data key type */
65 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
66 return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED;
67 }
68
69 /* Check compatibility between key and algorithm */
70 if ((key_type == PSA_KEY_TYPE_ARC4) && (alg != PSA_ALG_ARC4)) {
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010071 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +010072 }
73
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010074 /* Setup the algorithm on the crypto engine */
75 status = tfm_crypto_engine_cipher_setup(alg,
76 (const psa_key_type_t)key_type,
77 (const uint32_t)key_size,
78 c_mode,
79 &engine_info);
80 if (status != PSA_SUCCESS) {
81 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +010082 }
83
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010084 /* Allocate the operation context in the secure world */
Antonio de Angelis377a1552018-11-22 17:02:40 +000085 err = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION,
86 &(operation->handle));
Antonio de Angelis8908f472018-08-31 15:44:25 +010087 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
88 return err;
89 }
90
Antonio de Angelis8908f472018-08-31 15:44:25 +010091 /* Look up the corresponding operation context */
92 err = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis377a1552018-11-22 17:02:40 +000093 operation->handle,
94 (void **)&ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +010095 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010096 /* Release the operation context */
97 tfm_crypto_operation_release(&(operation->handle));
Antonio de Angelis8908f472018-08-31 15:44:25 +010098 return err;
99 }
100
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100101 /* Set the proper cipher mode (encrypt/decrypt) in the operation context */
102 ctx->cipher_mode = (uint8_t) c_mode;
103
Antonio de Angelis8908f472018-08-31 15:44:25 +0100104 /* Bind the algorithm to the cipher operation */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000105 ctx->alg = alg;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100106
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100107 /* Start the crypto engine */
108 status = tfm_crypto_engine_cipher_start(&(ctx->engine_ctx), &engine_info);
109 if (status != PSA_SUCCESS) {
Antonio de Angelis8908f472018-08-31 15:44:25 +0100110 /* Release the operation context */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000111 tfm_crypto_operation_release(&(operation->handle));
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100112 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100113 }
114
Jamie Foxefd82732018-11-26 10:34:32 +0000115 /* Set the key usage based on the cipher mode */
116 usage = (c_mode == ENGINE_CIPHER_MODE_DECRYPT) ? PSA_KEY_USAGE_DECRYPT
117 : PSA_KEY_USAGE_ENCRYPT;
118
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100119 /* Access the crypto service key module to retrieve key data */
Jamie Foxefd82732018-11-26 10:34:32 +0000120 err = tfm_crypto_get_key(key,
121 usage,
122 alg,
123 key_data,
Jamie Fox82b87ca2018-12-11 16:41:11 +0000124 CRYPTO_CIPHER_MAX_KEY_LENGTH,
Jamie Foxefd82732018-11-26 10:34:32 +0000125 &key_size);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100126 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
127 /* Release the operation context */
128 tfm_crypto_operation_release(&(operation->handle));
129 return err;
130 }
131
132 /* Set the key on the engine */
133 status = tfm_crypto_engine_cipher_set_key(&(ctx->engine_ctx),
134 key_data,
135 key_size,
136 &engine_info);
137 if (status != PSA_SUCCESS) {
138 /* Release the operation context */
139 tfm_crypto_operation_release(&(operation->handle));
140 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
141 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100142
143 /* Bind the key to the cipher operation */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000144 ctx->key = key;
145 ctx->key_set = 1;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100146
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100147 /* Set padding mode on engine in case of CBC */
Antonio de Angelis8908f472018-08-31 15:44:25 +0100148 if ((alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK) == PSA_ALG_CBC_BASE) {
149
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100150 status = tfm_crypto_engine_cipher_set_padding_mode(&(ctx->engine_ctx),
151 &engine_info);
152 if (status != PSA_SUCCESS) {
Antonio de Angelis8908f472018-08-31 15:44:25 +0100153 /* Release the operation context */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000154 tfm_crypto_operation_release(&(operation->handle));
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100155 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100156 }
157 }
158
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100159 /* FIXME: Check based on the algorithm, if we need to have an IV */
160 ctx->iv_required = 1;
161 ctx->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type);
162
Antonio de Angelis8908f472018-08-31 15:44:25 +0100163 return TFM_CRYPTO_ERR_PSA_SUCCESS;
164}
165
166/*!
167 * \defgroup public_psa Public functions, PSA
168 *
169 */
170
171/*!@{*/
Antonio de Angelis377a1552018-11-22 17:02:40 +0000172enum tfm_crypto_err_t tfm_crypto_cipher_set_iv(
173 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174 const unsigned char *iv,
175 size_t iv_length)
176{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100177 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100178 enum tfm_crypto_err_t err;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000179 struct tfm_cipher_operation_s *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100180
181 /* Validate pointers */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000182 err = tfm_crypto_memory_check(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100183 sizeof(psa_cipher_operation_t),
184 TFM_MEMORY_ACCESS_RW);
185 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
186 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
187 }
188 err = tfm_crypto_memory_check((void *)iv, iv_length, TFM_MEMORY_ACCESS_RO);
189 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
190 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
191 }
192
193 /* Look up the corresponding operation context */
194 err = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000195 operation->handle,
196 (void **)&ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100197 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
198 return err;
199 }
200
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100201 if ((iv_length != ctx->block_size) || (iv_length > TFM_CIPHER_IV_MAX_SIZE)){
202 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100203 }
204
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100205 if ((ctx->iv_set == 1) || (ctx->iv_required == 0)) {
206 return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100207 }
208
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100209 /* Set the IV on the crypto engine */
210 status = tfm_crypto_engine_cipher_set_iv(&(ctx->engine_ctx),
211 iv,
212 iv_length);
213 if (status != PSA_SUCCESS) {
214 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100215 }
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100216
Antonio de Angelis377a1552018-11-22 17:02:40 +0000217 ctx->iv_set = 1;
218 ctx->iv_size = iv_length;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100219
Antonio de Angelis8908f472018-08-31 15:44:25 +0100220 return TFM_CRYPTO_ERR_PSA_SUCCESS;
221}
222
Antonio de Angelis377a1552018-11-22 17:02:40 +0000223enum tfm_crypto_err_t tfm_crypto_cipher_encrypt_setup(
224 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100225 psa_key_slot_t key,
226 psa_algorithm_t alg)
227{
Antonio de Angelis377a1552018-11-22 17:02:40 +0000228 return tfm_crypto_cipher_setup(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100229 key,
230 alg,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100231 ENGINE_CIPHER_MODE_ENCRYPT);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100232}
233
Antonio de Angelis377a1552018-11-22 17:02:40 +0000234enum tfm_crypto_err_t tfm_crypto_cipher_decrypt_setup(
235 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100236 psa_key_slot_t key,
237 psa_algorithm_t alg)
238{
Antonio de Angelis377a1552018-11-22 17:02:40 +0000239 return tfm_crypto_cipher_setup(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100240 key,
241 alg,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100242 ENGINE_CIPHER_MODE_DECRYPT);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100243}
244
245enum tfm_crypto_err_t tfm_crypto_cipher_update(
Antonio de Angelis377a1552018-11-22 17:02:40 +0000246 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100247 const uint8_t *input,
248 size_t input_length,
249 unsigned char *output,
250 size_t output_size,
251 size_t *output_length)
252{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100253 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100254 enum tfm_crypto_err_t err;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000255 struct tfm_cipher_operation_s *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100256
257 /* Validate pointers */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000258 err = tfm_crypto_memory_check(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100259 sizeof(psa_cipher_operation_t),
260 TFM_MEMORY_ACCESS_RW);
261 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
262 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
263 }
264 err = tfm_crypto_memory_check((void *)input,
265 input_length,
266 TFM_MEMORY_ACCESS_RO);
267 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
268 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
269 }
270 err = tfm_crypto_memory_check(output,
271 output_size,
272 TFM_MEMORY_ACCESS_RW);
273 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
274 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
275 }
276 err = tfm_crypto_memory_check(output_length,
277 sizeof(size_t),
278 TFM_MEMORY_ACCESS_RW);
279 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
280 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
281 }
282
Jamie Fox82b87ca2018-12-11 16:41:11 +0000283 /* Initialise the output length to zero */
284 *output_length = 0;
285
Antonio de Angelis8908f472018-08-31 15:44:25 +0100286 /* Look up the corresponding operation context */
287 err = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000288 operation->handle,
289 (void **)&ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100290 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
291 return err;
292 }
293
294 /* If the IV is required and it's not been set yet */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000295 if ((ctx->iv_required == 1) && (ctx->iv_set == 0)) {
Antonio de Angelis8908f472018-08-31 15:44:25 +0100296
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100297 if (ctx->cipher_mode != ENGINE_CIPHER_MODE_DECRYPT) {
Antonio de Angelis8908f472018-08-31 15:44:25 +0100298 return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE;
299 }
300
301 /* This call is used to set the IV on the object */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000302 err = tfm_crypto_cipher_set_iv(operation, input, input_length);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100303
Antonio de Angelis8908f472018-08-31 15:44:25 +0100304 return err;
305 }
306
307 /* If the key is not set, setup phase has not been completed */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000308 if (ctx->key_set == 0) {
Antonio de Angelis8908f472018-08-31 15:44:25 +0100309 return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE;
310 }
311
Jamie Fox82b87ca2018-12-11 16:41:11 +0000312 /* FIXME: The implementation currently expects to work only on blocks
313 * of input data whose length is equal to the block size
314 */
315 if (input_length > output_size) {
316 return TFM_CRYPTO_ERR_PSA_ERROR_BUFFER_TOO_SMALL;
317 }
318
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100319 /* Update the cipher output with the input chunk on the engine */
320 status = tfm_crypto_engine_cipher_update(&(ctx->engine_ctx),
321 input,
322 input_length,
323 output,
324 (uint32_t *)output_length);
325 if (status != PSA_SUCCESS) {
326 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100327 }
328
Antonio de Angelis8908f472018-08-31 15:44:25 +0100329 return TFM_CRYPTO_ERR_PSA_SUCCESS;
330}
331
332enum tfm_crypto_err_t tfm_crypto_cipher_finish(
Antonio de Angelis377a1552018-11-22 17:02:40 +0000333 psa_cipher_operation_t *operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100334 uint8_t *output,
335 size_t output_size,
336 size_t *output_length)
337{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100338 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100339 enum tfm_crypto_err_t err;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000340 struct tfm_cipher_operation_s *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100341
342 /* Validate pointers */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000343 err = tfm_crypto_memory_check(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100344 sizeof(psa_cipher_operation_t),
345 TFM_MEMORY_ACCESS_RW);
346 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
347 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
348 }
349 err = tfm_crypto_memory_check(output,
350 output_size,
351 TFM_MEMORY_ACCESS_RW);
352 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
353 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
354 }
355 err = tfm_crypto_memory_check(output_length,
356 sizeof(size_t),
357 TFM_MEMORY_ACCESS_RW);
358 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
359 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
360 }
361
Antonio de Angelis8908f472018-08-31 15:44:25 +0100362 /* Look up the corresponding operation context */
363 err = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000364 operation->handle,
365 (void **)&ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100366 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
367 return err;
368 }
369
Jamie Fox82b87ca2018-12-11 16:41:11 +0000370 /* Check that the output buffer is large enough for up to one block size of
371 * output data.
372 */
373 if (output_size < ctx->block_size) {
374 return TFM_CRYPTO_ERR_PSA_ERROR_BUFFER_TOO_SMALL;
375 }
376
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100377 /* Finalise the operation on the crypto engine */
378 status = tfm_crypto_engine_cipher_finish(&(ctx->engine_ctx),
379 output,
380 (uint32_t *)output_length);
381 if (status != PSA_SUCCESS) {
382 *output_length = 0;
383 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100384 }
385
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100386 /* Release resources on the crypto engine */
387 status = tfm_crypto_engine_cipher_release(&(ctx->engine_ctx));
388 if (status != PSA_SUCCESS) {
389 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
390 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100391
392 /* Release the operation context */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000393 err = tfm_crypto_operation_release(&(operation->handle));
Antonio de Angelis8908f472018-08-31 15:44:25 +0100394 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
395 return err;
396 }
397
398 return TFM_CRYPTO_ERR_PSA_SUCCESS;
399}
400
Antonio de Angelis377a1552018-11-22 17:02:40 +0000401enum tfm_crypto_err_t tfm_crypto_cipher_abort(psa_cipher_operation_t *operation)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100402{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100403 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100404 enum tfm_crypto_err_t err;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000405 struct tfm_cipher_operation_s *ctx = NULL;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100406
407 /* Validate pointers */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000408 err = tfm_crypto_memory_check(operation,
Antonio de Angelis8908f472018-08-31 15:44:25 +0100409 sizeof(psa_cipher_operation_t),
410 TFM_MEMORY_ACCESS_RW);
411 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
412 return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT;
413 }
414
415 /* Look up the corresponding operation context */
416 err = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION,
Antonio de Angelis377a1552018-11-22 17:02:40 +0000417 operation->handle,
418 (void **)&ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100419 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
420 return err;
421 }
422
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100423 /* Release resources on the engine */
424 status = tfm_crypto_engine_cipher_release(&(ctx->engine_ctx));
425 if (status != PSA_SUCCESS) {
426 return PSA_STATUS_TO_TFM_CRYPTO_ERR(status);
427 }
Antonio de Angelis8908f472018-08-31 15:44:25 +0100428
429 /* Release the operation context */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000430 err = tfm_crypto_operation_release(&(operation->handle));
Antonio de Angelis8908f472018-08-31 15:44:25 +0100431 if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) {
432 return err;
433 }
434
435 return TFM_CRYPTO_ERR_PSA_SUCCESS;
436}
437/*!@}*/