blob: 70ce182c6f04d21e0fde44a81ec1d5b71642eb2f [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis202425a2022-04-06 11:13:15 +01002 * Copyright (c) 2018-2022, 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"
Antonio de Angelis8908f472018-08-31 15:44:25 +010015
16/*!
Antonio de Angelis202425a2022-04-06 11:13:15 +010017 * \addtogroup tfm_crypto_api_shim_layer
Antonio de Angelis8908f472018-08-31 15:44:25 +010018 *
19 */
20
21/*!@{*/
Antonio de Angelis202425a2022-04-06 11:13:15 +010022psa_status_t tfm_crypto_cipher_interface(psa_invec in_vec[],
23 psa_outvec out_vec[],
24 mbedtls_svc_key_id_t *encoded_key)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010025{
Kevin Peng96f802e2019-12-26 16:10:25 +080026#ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010027 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis202425a2022-04-06 11:13:15 +010028#endif
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010029 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis202425a2022-04-06 11:13:15 +010030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010031 psa_cipher_operation_t *operation = NULL;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032 uint32_t handle = iov->op_handle;
Antonio de Angelis202425a2022-04-06 11:13:15 +010033 uint32_t *handle_out = NULL;
34 enum tfm_crypto_function_type function_type =
35 TFM_CRYPTO_GET_FUNCTION_TYPE(iov->function_id);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000036
Antonio de Angelis202425a2022-04-06 11:13:15 +010037 if (function_type != TFM_CRYPTO_FUNCTION_TYPE_NON_MULTIPART) {
38 handle_out = (out_vec && out_vec[0].base != NULL) ?
39 out_vec[0].base : &handle;
40 *handle_out = handle;
41 status = tfm_crypto_operation_handling(TFM_CRYPTO_CIPHER_OPERATION,
42 function_type,
43 handle_out,
44 (void **)&operation);
45 if (status != PSA_SUCCESS) {
46 return (iov->function_id == TFM_CRYPTO_CIPHER_ABORT_SID) ?
47 PSA_SUCCESS : status;
48 }
Antonio de Angelis8908f472018-08-31 15:44:25 +010049 }
50
Antonio de Angelis202425a2022-04-06 11:13:15 +010051 switch (iov->function_id) {
52 case TFM_CRYPTO_CIPHER_GENERATE_IV_SID:
53 {
54 unsigned char *iv = out_vec[0].base;
55 size_t iv_size = out_vec[0].len;
Antonio de Angelis4743e672019-04-11 11:38:48 +010056
Antonio de Angelis202425a2022-04-06 11:13:15 +010057 status = psa_cipher_generate_iv(operation,
58 iv, iv_size, &out_vec[0].len);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000059 }
Antonio de Angelis202425a2022-04-06 11:13:15 +010060 break;
61 case TFM_CRYPTO_CIPHER_SET_IV_SID:
62 {
63 const unsigned char *iv = in_vec[1].base;
64 size_t iv_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000065
Antonio de Angelis202425a2022-04-06 11:13:15 +010066 status = psa_cipher_set_iv(operation, iv, iv_length);
Antonio de Angelis4743e672019-04-11 11:38:48 +010067 }
Antonio de Angelis202425a2022-04-06 11:13:15 +010068 break;
69 case TFM_CRYPTO_CIPHER_ENCRYPT_SETUP_SID:
70 {
71 status = psa_cipher_encrypt_setup(operation, *encoded_key, iov->alg);
72 if (status != PSA_SUCCESS) {
73 goto release_operation_and_return;
74 }
Maulik Patel28659c42021-01-06 14:09:22 +000075 }
Antonio de Angelis202425a2022-04-06 11:13:15 +010076 break;
77 case TFM_CRYPTO_CIPHER_DECRYPT_SETUP_SID:
78 {
79 status = psa_cipher_decrypt_setup(operation, *encoded_key, iov->alg);
80 if (status != PSA_SUCCESS) {
81 goto release_operation_and_return;
82 }
83 }
84 break;
85 case TFM_CRYPTO_CIPHER_UPDATE_SID:
86 {
87 const uint8_t *input = in_vec[1].base;
88 size_t input_length = in_vec[1].len;
89 unsigned char *output = out_vec[0].base;
90 size_t output_size = out_vec[0].len;
91 /* Initialise the output_length to zero */
92 out_vec[0].len = 0;
Maulik Patel28659c42021-01-06 14:09:22 +000093
Antonio de Angelis202425a2022-04-06 11:13:15 +010094 status = psa_cipher_update(operation, input, input_length,
95 output, output_size, &out_vec[0].len);
96 }
97 break;
98 case TFM_CRYPTO_CIPHER_FINISH_SID:
99 {
100 uint8_t *output = out_vec[1].base;
101 size_t output_size = out_vec[1].len;
102 /* Initialise the output_length to zero */
103 out_vec[1].len = 0;
104
105 status = psa_cipher_finish(operation,
106 output, output_size, &out_vec[1].len);
107 if (status == PSA_SUCCESS) {
108 /* In case of success automatically release the operation */
109 goto release_operation_and_return;
110 }
111 }
112 break;
113 case TFM_CRYPTO_CIPHER_ABORT_SID:
114 {
115 status = psa_cipher_abort(operation);
116
117 if (status != PSA_SUCCESS) {
118 goto release_operation_and_return;
119 } else {
120 status = tfm_crypto_operation_release(handle_out);
121 }
122 }
123 break;
124 case TFM_CRYPTO_CIPHER_ENCRYPT_SID:
125 {
126 const uint8_t *input = in_vec[1].base;
127 size_t input_length = in_vec[1].len;
128 uint8_t *output = out_vec[0].base;
129 size_t output_size = out_vec[0].len;
130
131 status = psa_cipher_encrypt(*encoded_key, iov->alg, input, input_length,
132 output, output_size, &out_vec[0].len);
133 }
134 break;
135 case TFM_CRYPTO_CIPHER_DECRYPT_SID:
136 {
137 const uint8_t *input = in_vec[1].base;
138 size_t input_length = in_vec[1].len;
139 uint8_t *output = out_vec[0].base;
140 size_t output_size = out_vec[0].len;
141
142 status = psa_cipher_decrypt(*encoded_key, iov->alg, input, input_length,
143 output, output_size, &out_vec[0].len);
144 }
145 break;
146 default:
147 status = PSA_ERROR_NOT_SUPPORTED;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100148 }
149
Antonio de Angelis4743e672019-04-11 11:38:48 +0100150 return status;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100151release_operation_and_return:
David Hu7e2e5232021-04-21 16:52:07 +0800152 /* Release the operation context, ignore if the operation fails. */
153 (void)tfm_crypto_operation_release(handle_out);
154 return status;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100155}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100156/*!@}*/