Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 8 | #include "tfm_crypto_api.h" |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 9 | #include "crypto_engine.h" |
| 10 | #include "tfm_crypto_struct.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 11 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 12 | /* FixMe: Use PSA_CONNECTION_REFUSED when performing parameter |
| 13 | * integrity checks but this will have to be revised |
| 14 | * when the full set of error codes mandated by PSA FF |
| 15 | * is available. |
| 16 | */ |
| 17 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 18 | /** |
| 19 | * \def CRYPTO_CIPHER_MAX_KEY_LENGTH |
| 20 | * |
| 21 | * \brief Specifies the maximum key length supported by the |
| 22 | * Cipher operations in this implementation |
| 23 | */ |
| 24 | #ifndef CRYPTO_CIPHER_MAX_KEY_LENGTH |
| 25 | #define CRYPTO_CIPHER_MAX_KEY_LENGTH (32) |
| 26 | #endif |
| 27 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 28 | static psa_status_t _psa_get_key_information(psa_key_slot_t key, |
| 29 | psa_key_type_t *type, |
| 30 | size_t *bits) |
| 31 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 32 | psa_status_t status; |
| 33 | struct tfm_crypto_pack_iovec iov = { |
| 34 | .sfn_id = TFM_CRYPTO_GET_KEY_INFORMATION_SFID, |
| 35 | .key = key, |
| 36 | }; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 37 | psa_invec in_vec[] = { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 38 | {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 39 | }; |
| 40 | psa_outvec out_vec[] = { |
| 41 | {.base = type, .len = sizeof(psa_key_type_t)}, |
| 42 | {.base = bits, .len = sizeof(size_t)} |
| 43 | }; |
| 44 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 45 | status = tfm_crypto_get_key_information( |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 46 | in_vec, sizeof(in_vec)/sizeof(in_vec[0]), |
| 47 | out_vec, sizeof(out_vec)/sizeof(out_vec[0])); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 48 | |
| 49 | return status; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 52 | /** |
| 53 | * \brief Release all resources associated with a cipher operation. |
| 54 | * |
| 55 | * \param[in] operation Frontend cipher operation context |
| 56 | * \param[in] ctx Backend cipher operation context |
| 57 | * |
| 58 | * \return Return values as described in \ref tfm_crypto_err_t |
| 59 | */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 60 | static psa_status_t tfm_crypto_cipher_release( |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 61 | uint32_t *handle, |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 62 | struct tfm_cipher_operation_s *ctx) |
| 63 | { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 64 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 65 | |
| 66 | /* Release resources in the engine */ |
| 67 | status = tfm_crypto_engine_cipher_release(&(ctx->engine_ctx)); |
| 68 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 69 | return status; |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Release the operation context */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 73 | return tfm_crypto_operation_release(handle); |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 76 | static psa_status_t tfm_crypto_cipher_setup(uint32_t *handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 77 | psa_key_slot_t key, |
| 78 | psa_algorithm_t alg, |
| 79 | enum engine_cipher_mode_t c_mode) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 80 | { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 81 | uint8_t key_data[CRYPTO_CIPHER_MAX_KEY_LENGTH]; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 82 | size_t key_size; |
| 83 | psa_key_type_t key_type = PSA_KEY_TYPE_NONE; |
| 84 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 85 | struct tfm_cipher_operation_s *ctx = NULL; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 86 | struct cipher_engine_info engine_info; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 87 | psa_key_usage_t usage; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 88 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 89 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 90 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 93 | /* Access the key module to retrieve key related information */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 94 | status = _psa_get_key_information(key, &key_type, &key_size); |
| 95 | if (status != PSA_SUCCESS) { |
| 96 | return status; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Check if it's a raw data key type */ |
| 100 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 101 | return PSA_ERROR_NOT_PERMITTED; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Check compatibility between key and algorithm */ |
| 105 | if ((key_type == PSA_KEY_TYPE_ARC4) && (alg != PSA_ALG_ARC4)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 106 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 109 | /* Setup the algorithm on the crypto engine */ |
| 110 | status = tfm_crypto_engine_cipher_setup(alg, |
| 111 | (const psa_key_type_t)key_type, |
| 112 | (const uint32_t)key_size, |
| 113 | c_mode, |
| 114 | &engine_info); |
| 115 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 116 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 119 | /* Allocate the operation context in the secure world */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 120 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 121 | handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 122 | (void **)&ctx); |
| 123 | if (status != PSA_SUCCESS) { |
| 124 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 127 | /* Set the proper cipher mode (encrypt/decrypt) in the operation context */ |
| 128 | ctx->cipher_mode = (uint8_t) c_mode; |
| 129 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 130 | /* Bind the algorithm to the cipher operation */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 131 | ctx->alg = alg; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 132 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 133 | /* Start the crypto engine */ |
| 134 | status = tfm_crypto_engine_cipher_start(&(ctx->engine_ctx), &engine_info); |
| 135 | if (status != PSA_SUCCESS) { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 136 | /* Release the operation context, ignore if this operation fails. */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 137 | (void)tfm_crypto_cipher_release(handle, ctx); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 138 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 141 | /* Set the key usage based on the cipher mode */ |
| 142 | usage = (c_mode == ENGINE_CIPHER_MODE_DECRYPT) ? PSA_KEY_USAGE_DECRYPT |
| 143 | : PSA_KEY_USAGE_ENCRYPT; |
| 144 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 145 | /* Access the crypto service key module to retrieve key data */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 146 | status = tfm_crypto_get_key(key, |
| 147 | usage, |
| 148 | alg, |
| 149 | key_data, |
| 150 | CRYPTO_CIPHER_MAX_KEY_LENGTH, |
| 151 | &key_size); |
| 152 | if (status != PSA_SUCCESS) { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 153 | /* Release the operation context, ignore if this operation fails. */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 154 | (void)tfm_crypto_cipher_release(handle, ctx); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 155 | return status; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* Set the key on the engine */ |
| 159 | status = tfm_crypto_engine_cipher_set_key(&(ctx->engine_ctx), |
| 160 | key_data, |
| 161 | key_size, |
| 162 | &engine_info); |
| 163 | if (status != PSA_SUCCESS) { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 164 | /* Release the operation context, ignore if this operation fails. */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 165 | (void)tfm_crypto_cipher_release(handle, ctx); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 166 | return status; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 167 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 168 | |
| 169 | /* Bind the key to the cipher operation */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 170 | ctx->key = key; |
| 171 | ctx->key_set = 1; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 172 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 173 | /* Set padding mode on engine in case of CBC */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 174 | if ((alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK) == PSA_ALG_CBC_BASE) { |
| 175 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 176 | status = tfm_crypto_engine_cipher_set_padding_mode(&(ctx->engine_ctx), |
| 177 | &engine_info); |
| 178 | if (status != PSA_SUCCESS) { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 179 | /* Release the operation context, ignore if this operation fails. */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 180 | (void)tfm_crypto_cipher_release(handle, ctx); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 181 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 185 | /* FIXME: Check based on the algorithm, if we need to have an IV */ |
| 186 | ctx->iv_required = 1; |
| 187 | ctx->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); |
| 188 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 189 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /*! |
| 193 | * \defgroup public_psa Public functions, PSA |
| 194 | * |
| 195 | */ |
| 196 | |
| 197 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 198 | psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[], |
| 199 | size_t in_len, |
| 200 | psa_outvec out_vec[], |
| 201 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 202 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 203 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 204 | struct tfm_cipher_operation_s *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 205 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 206 | if ((in_len != 2) || (out_len != 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 207 | return PSA_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 210 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 211 | (out_vec[0].len != sizeof(uint32_t))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 212 | return PSA_CONNECTION_REFUSED; |
| 213 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 214 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 215 | uint32_t handle = iov->handle; |
| 216 | uint32_t *handle_out = out_vec[0].base; |
| 217 | const unsigned char *iv = in_vec[1].base; |
| 218 | size_t iv_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 219 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 220 | /* Init the handle in the operation with the one passed from the iov */ |
| 221 | *handle_out = iov->handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 222 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 223 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 224 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 225 | handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 226 | (void **)&ctx); |
| 227 | if (status != PSA_SUCCESS) { |
| 228 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 231 | if ((iv_length != ctx->block_size) || (iv_length > TFM_CIPHER_IV_MAX_SIZE)){ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 232 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 233 | *handle_out = handle; |
| 234 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 235 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 238 | if ((ctx->iv_set == 1) || (ctx->iv_required == 0)) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 239 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 240 | *handle_out = handle; |
| 241 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 242 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 245 | /* Set the IV on the crypto engine */ |
| 246 | status = tfm_crypto_engine_cipher_set_iv(&(ctx->engine_ctx), |
| 247 | iv, |
| 248 | iv_length); |
| 249 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 250 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 251 | *handle_out = handle; |
| 252 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 253 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 254 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 255 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 256 | ctx->iv_set = 1; |
| 257 | ctx->iv_size = iv_length; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 258 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 259 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 262 | static psa_status_t _psa_cipher_set_iv(uint32_t *handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 263 | const unsigned char *iv, |
| 264 | size_t iv_length) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 265 | { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 266 | psa_status_t status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 267 | struct tfm_crypto_pack_iovec iov = { |
| 268 | .sfn_id = TFM_CRYPTO_CIPHER_SET_IV_SFID, |
| 269 | .handle = *handle, |
| 270 | }; |
| 271 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 272 | psa_invec in_vec[] = { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 273 | {.base = &iov, .len = sizeof(struct tfm_crypto_pack_iovec)}, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 274 | {.base = iv, .len = iv_length}, |
| 275 | }; |
| 276 | psa_outvec out_vec[] = { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 277 | {.base = handle, .len = sizeof(uint32_t)}, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | status = tfm_crypto_cipher_set_iv(in_vec, sizeof(in_vec)/sizeof(in_vec[0]), |
| 281 | out_vec, sizeof(out_vec)/sizeof(out_vec[0])); |
| 282 | return status; |
| 283 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 284 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 285 | psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[], |
| 286 | size_t in_len, |
| 287 | psa_outvec out_vec[], |
| 288 | size_t out_len) |
| 289 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 290 | psa_status_t status = PSA_SUCCESS; |
| 291 | if ((in_len != 1) || (out_len != 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 292 | return PSA_CONNECTION_REFUSED; |
| 293 | } |
| 294 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 295 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 296 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 297 | return PSA_CONNECTION_REFUSED; |
| 298 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 299 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 300 | uint32_t handle = iov->handle; |
| 301 | uint32_t *handle_out = out_vec[0].base; |
| 302 | psa_key_slot_t key = iov->key; |
| 303 | psa_algorithm_t alg = iov->alg; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 304 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 305 | status = tfm_crypto_cipher_setup(&handle, |
| 306 | key, |
| 307 | alg, |
| 308 | ENGINE_CIPHER_MODE_ENCRYPT); |
| 309 | if (status == PSA_SUCCESS) { |
| 310 | *handle_out = handle; |
| 311 | } else { |
| 312 | *handle_out = iov->handle; |
| 313 | } |
| 314 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 317 | psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[], |
| 318 | size_t in_len, |
| 319 | psa_outvec out_vec[], |
| 320 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 321 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 322 | psa_status_t status = PSA_SUCCESS; |
| 323 | if ((in_len != 1) || (out_len != 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 324 | return PSA_CONNECTION_REFUSED; |
| 325 | } |
| 326 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 327 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 328 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 329 | return PSA_CONNECTION_REFUSED; |
| 330 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 331 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 332 | uint32_t handle = iov->handle; |
| 333 | uint32_t *handle_out = out_vec[0].base; |
| 334 | psa_key_slot_t key = iov->key; |
| 335 | psa_algorithm_t alg = iov->alg; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 336 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 337 | status = tfm_crypto_cipher_setup(&handle, |
| 338 | key, |
| 339 | alg, |
| 340 | ENGINE_CIPHER_MODE_DECRYPT); |
| 341 | if (status == PSA_SUCCESS) { |
| 342 | *handle_out = handle; |
| 343 | } |
| 344 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 347 | psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[], |
| 348 | size_t in_len, |
| 349 | psa_outvec out_vec[], |
| 350 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 351 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 352 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 353 | struct tfm_cipher_operation_s *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 354 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 355 | if ((in_len != 2) || (out_len != 2)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 356 | return PSA_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 359 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 360 | (out_vec[0].len != sizeof(uint32_t))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 361 | return PSA_CONNECTION_REFUSED; |
| 362 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 363 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 364 | uint32_t handle = iov->handle; |
| 365 | uint32_t *handle_out = out_vec[0].base; |
| 366 | const uint8_t *input = in_vec[1].base; |
| 367 | size_t input_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 368 | unsigned char *output = out_vec[1].base; |
| 369 | size_t output_size = out_vec[1].len; |
| 370 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 371 | /* Init the handle in the operation with the one passed from the iov */ |
| 372 | *handle_out = iov->handle; |
| 373 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 374 | /* Initialise the output_length to zero */ |
| 375 | out_vec[1].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 376 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 377 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 378 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 379 | handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 380 | (void **)&ctx); |
| 381 | if (status != PSA_SUCCESS) { |
| 382 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* If the IV is required and it's not been set yet */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 386 | if ((ctx->iv_required == 1) && (ctx->iv_set == 0)) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 387 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 388 | if (ctx->cipher_mode != ENGINE_CIPHER_MODE_DECRYPT) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 389 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 390 | *handle_out = handle; |
| 391 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 392 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* This call is used to set the IV on the object */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 396 | return _psa_cipher_set_iv(handle_out, input, input_length); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* If the key is not set, setup phase has not been completed */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 400 | if (ctx->key_set == 0) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 401 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 402 | *handle_out = handle; |
| 403 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 404 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 407 | /* FIXME: The implementation currently expects to work only on blocks |
| 408 | * of input data whose length is equal to the block size |
| 409 | */ |
| 410 | if (input_length > output_size) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 411 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 412 | *handle_out = handle; |
| 413 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 414 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 417 | /* Update the cipher output with the input chunk on the engine */ |
| 418 | status = tfm_crypto_engine_cipher_update(&(ctx->engine_ctx), |
| 419 | input, |
| 420 | input_length, |
| 421 | output, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 422 | (uint32_t *)&(out_vec[1].len)); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 423 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 424 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 425 | *handle_out = handle; |
| 426 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 427 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 430 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 433 | psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[], |
| 434 | size_t in_len, |
| 435 | psa_outvec out_vec[], |
| 436 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 437 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 438 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 439 | struct tfm_cipher_operation_s *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 440 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 441 | if ((in_len != 1) || (out_len != 2)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 442 | return PSA_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 445 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 446 | (out_vec[0].len != sizeof(uint32_t))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 447 | return PSA_CONNECTION_REFUSED; |
| 448 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 449 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 450 | uint32_t handle = iov->handle; |
| 451 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 452 | unsigned char *output = out_vec[1].base; |
| 453 | size_t output_size = out_vec[1].len; |
| 454 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 455 | /* Init the handle in the operation with the one passed from the iov */ |
| 456 | *handle_out = iov->handle; |
| 457 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 458 | /* Initialise the output_length to zero */ |
| 459 | out_vec[1].len = 0; |
| 460 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 461 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 462 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 463 | handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 464 | (void **)&ctx); |
| 465 | if (status != PSA_SUCCESS) { |
| 466 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 469 | /* Check that the output buffer is large enough for up to one block size of |
| 470 | * output data. |
| 471 | */ |
| 472 | if (output_size < ctx->block_size) { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 473 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 474 | *handle_out = handle; |
| 475 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 476 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 479 | /* Finalise the operation on the crypto engine */ |
| 480 | status = tfm_crypto_engine_cipher_finish(&(ctx->engine_ctx), |
| 481 | output, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 482 | (uint32_t *)&(out_vec[1].len)); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 483 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 484 | out_vec[1].len = 0; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 485 | if (tfm_crypto_cipher_release(&handle, ctx) == PSA_SUCCESS) { |
| 486 | *handle_out = handle; |
| 487 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 488 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 491 | status = tfm_crypto_cipher_release(&handle, ctx); |
| 492 | if (status == PSA_SUCCESS) { |
| 493 | *handle_out = handle; |
| 494 | } |
| 495 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 498 | psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[], |
| 499 | size_t in_len, |
| 500 | psa_outvec out_vec[], |
| 501 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 502 | { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 503 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 504 | struct tfm_cipher_operation_s *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 505 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 506 | if ((in_len != 1) || (out_len != 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 507 | return PSA_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 510 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 511 | (out_vec[0].len != sizeof(uint32_t))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 512 | return PSA_CONNECTION_REFUSED; |
| 513 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 514 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 515 | uint32_t handle = iov->handle; |
| 516 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 517 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 518 | /* Init the handle in the operation with the one passed from the iov */ |
| 519 | *handle_out = iov->handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 520 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 521 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 522 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 523 | handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 524 | (void **)&ctx); |
| 525 | if (status != PSA_SUCCESS) { |
| 526 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame^] | 529 | status = tfm_crypto_cipher_release(&handle, ctx); |
| 530 | if (status == PSA_SUCCESS) { |
| 531 | *handle_out = handle; |
| 532 | } |
| 533 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 534 | } |
| 535 | /*!@}*/ |