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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 11 | /* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 12 | * 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 Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 16 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 17 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 18 | #include "tfm_crypto_api.h" |
| 19 | #include "tfm_crypto_defs.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 20 | |
| 21 | /*! |
| 22 | * \defgroup public_psa Public functions, PSA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /*!@{*/ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 27 | psa_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 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 32 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 33 | return PSA_ERROR_NOT_SUPPORTED; |
| 34 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 35 | psa_status_t status = PSA_SUCCESS; |
| 36 | psa_cipher_operation_t *operation = NULL; |
| 37 | |
| 38 | if ((in_len != 1) || (out_len != 2)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 39 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 43 | (out_vec[0].len != sizeof(uint32_t))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 44 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 45 | } |
| 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 Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 74 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 77 | psa_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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 81 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 82 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 83 | return PSA_ERROR_NOT_SUPPORTED; |
| 84 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 85 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 86 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 87 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 88 | if ((in_len != 2) || (out_len != 1)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 89 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 93 | (out_vec[0].len != sizeof(uint32_t))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 94 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 95 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 96 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 97 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 98 | 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 Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 101 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 102 | /* Init the handle in the operation with the one passed from the iov */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 103 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 104 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 105 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 106 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 107 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 108 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 109 | if (status != PSA_SUCCESS) { |
| 110 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 113 | status = psa_cipher_set_iv(operation, iv, iv_length); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 114 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 115 | /* Release the operation context, ignore if the operation fails. */ |
| 116 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 117 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 118 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 119 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 120 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 121 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 122 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 123 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 124 | psa_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 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 129 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 130 | return PSA_ERROR_NOT_SUPPORTED; |
| 131 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 132 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 133 | psa_cipher_operation_t *operation = NULL; |
| 134 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 135 | if ((in_len != 1) || (out_len != 1)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 136 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 139 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 140 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 141 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 142 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 143 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 144 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 145 | uint32_t *handle_out = out_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 146 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 147 | psa_algorithm_t alg = iov->alg; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 148 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 149 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 150 | if (status != PSA_SUCCESS) { |
| 151 | return status; |
| 152 | } |
| 153 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 154 | /* 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 160 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 161 | |
| 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 171 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 172 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 175 | psa_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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 179 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 180 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 181 | return PSA_ERROR_NOT_SUPPORTED; |
| 182 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 183 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 184 | psa_cipher_operation_t *operation = NULL; |
| 185 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 186 | if ((in_len != 1) || (out_len != 1)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 187 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 190 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 191 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 192 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 193 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 194 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 195 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 196 | uint32_t *handle_out = out_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 197 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 198 | psa_algorithm_t alg = iov->alg; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 199 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 200 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 201 | if (status != PSA_SUCCESS) { |
| 202 | return status; |
| 203 | } |
| 204 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 205 | /* 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 211 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 212 | |
| 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 222 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 223 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 226 | psa_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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 230 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 231 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 232 | return PSA_ERROR_NOT_SUPPORTED; |
| 233 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 234 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 235 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 236 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 237 | if ((in_len != 2) || (out_len != 2)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 238 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 241 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 242 | (out_vec[0].len != sizeof(uint32_t))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 243 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 244 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 245 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 246 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 247 | 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 Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 250 | unsigned char *output = out_vec[1].base; |
| 251 | size_t output_size = out_vec[1].len; |
| 252 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 253 | /* Init the handle in the operation with the one passed from the iov */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 254 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 255 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 256 | /* Initialise the output_length to zero */ |
| 257 | out_vec[1].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 258 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 259 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 260 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 261 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 262 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 263 | if (status != PSA_SUCCESS) { |
| 264 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 267 | status = psa_cipher_update(operation, input, input_length, |
| 268 | output, output_size, &out_vec[1].len); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 269 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 270 | /* Release the operation context, ignore if the operation fails. */ |
| 271 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 272 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 275 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 276 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 279 | psa_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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 283 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 284 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 285 | return PSA_ERROR_NOT_SUPPORTED; |
| 286 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 287 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 288 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 289 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 290 | if ((in_len != 1) || (out_len != 2)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 291 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 294 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 295 | (out_vec[0].len != sizeof(uint32_t))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 296 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 297 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 298 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 299 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 300 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 301 | unsigned char *output = out_vec[1].base; |
| 302 | size_t output_size = out_vec[1].len; |
| 303 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 304 | /* Init the handle in the operation with the one passed from the iov */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 305 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 306 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 307 | /* Initialise the output_length to zero */ |
| 308 | out_vec[1].len = 0; |
| 309 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 310 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 311 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 312 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 313 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 314 | if (status != PSA_SUCCESS) { |
| 315 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 318 | status = psa_cipher_finish(operation, output, output_size, &out_vec[1].len); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 319 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 320 | /* Release the operation context, ignore if the operation fails. */ |
| 321 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 322 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 325 | status = tfm_crypto_operation_release(handle_out); |
| 326 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 327 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 328 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 331 | psa_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 Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 335 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 336 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 337 | return PSA_ERROR_NOT_SUPPORTED; |
| 338 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 339 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 340 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 341 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 342 | if ((in_len != 1) || (out_len != 1)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 343 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 346 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 347 | (out_vec[0].len != sizeof(uint32_t))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 348 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 349 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 350 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 351 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 352 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 353 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 354 | /* Init the handle in the operation with the one passed from the iov */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 355 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 356 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 357 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 358 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 359 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 360 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 361 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 362 | /* Operation does not exist, so abort has no effect */ |
| 363 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 366 | 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 372 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 373 | |
| 374 | status = tfm_crypto_operation_release(handle_out); |
| 375 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 376 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 377 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 378 | } |
| 379 | /*!@}*/ |