Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 12 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 13 | #include "tfm_crypto_api.h" |
| 14 | #include "tfm_crypto_defs.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 15 | #include "tfm_crypto_private.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 16 | |
| 17 | /*! |
| 18 | * \defgroup public_psa Public functions, PSA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /*!@{*/ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 23 | psa_status_t tfm_crypto_cipher_generate_iv(psa_invec in_vec[], |
| 24 | size_t in_len, |
| 25 | psa_outvec out_vec[], |
| 26 | size_t out_len) |
| 27 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 28 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 29 | return PSA_ERROR_NOT_SUPPORTED; |
| 30 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 31 | psa_status_t status = PSA_SUCCESS; |
| 32 | psa_cipher_operation_t *operation = NULL; |
| 33 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 34 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 35 | |
| 36 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 37 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 38 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 42 | uint32_t handle = iov->op_handle; |
| 43 | uint32_t *handle_out = out_vec[0].base; |
| 44 | unsigned char *iv = out_vec[1].base; |
| 45 | size_t iv_size = out_vec[1].len; |
| 46 | |
| 47 | /* Init the handle in the operation with the one passed from the iov */ |
| 48 | *handle_out = iov->op_handle; |
| 49 | |
| 50 | /* Look up the corresponding operation context */ |
| 51 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
| 52 | handle, |
| 53 | (void **)&operation); |
| 54 | if (status != PSA_SUCCESS) { |
| 55 | return status; |
| 56 | } |
| 57 | |
| 58 | *handle_out = handle; |
| 59 | |
| 60 | status = psa_cipher_generate_iv(operation, iv, iv_size, &out_vec[1].len); |
| 61 | if (status != PSA_SUCCESS) { |
| 62 | /* Release the operation context, ignore if the operation fails. */ |
| 63 | (void)tfm_crypto_operation_release(handle_out); |
| 64 | return status; |
| 65 | } |
| 66 | |
| 67 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 68 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 71 | psa_status_t tfm_crypto_cipher_set_iv(psa_invec in_vec[], |
| 72 | size_t in_len, |
| 73 | psa_outvec out_vec[], |
| 74 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 75 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 76 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 77 | return PSA_ERROR_NOT_SUPPORTED; |
| 78 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 79 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 80 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 81 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 82 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 83 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 84 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 85 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 86 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 87 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 88 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 89 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 90 | uint32_t *handle_out = out_vec[0].base; |
| 91 | const unsigned char *iv = in_vec[1].base; |
| 92 | size_t iv_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 93 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 94 | /* 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] | 95 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 96 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 97 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 98 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 99 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 100 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 101 | if (status != PSA_SUCCESS) { |
| 102 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 105 | status = psa_cipher_set_iv(operation, iv, iv_length); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 106 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 107 | /* Release the operation context, ignore if the operation fails. */ |
| 108 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 109 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 110 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 111 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 112 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 113 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 114 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 115 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 116 | psa_status_t tfm_crypto_cipher_encrypt_setup(psa_invec in_vec[], |
| 117 | size_t in_len, |
| 118 | psa_outvec out_vec[], |
| 119 | size_t out_len) |
| 120 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 121 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 122 | return PSA_ERROR_NOT_SUPPORTED; |
| 123 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 124 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 125 | psa_cipher_operation_t *operation = NULL; |
| 126 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 127 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 128 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 129 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 130 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 131 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 132 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 133 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 134 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 135 | uint32_t *handle_out = out_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 136 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 137 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 138 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 139 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 140 | status = tfm_crypto_check_handle_owner(key_id, NULL); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 141 | if (status != PSA_SUCCESS) { |
| 142 | return status; |
| 143 | } |
| 144 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 145 | /* Allocate the operation context in the secure world */ |
| 146 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION, |
| 147 | &handle, |
| 148 | (void **)&operation); |
| 149 | if (status != PSA_SUCCESS) { |
| 150 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 151 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 152 | *handle_out = handle; |
| 153 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 154 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 155 | if (status != PSA_SUCCESS) { |
| 156 | return status; |
| 157 | } |
| 158 | |
| 159 | status = psa_cipher_encrypt_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 160 | if (status != PSA_SUCCESS) { |
| 161 | /* Release the operation context, ignore if the operation fails. */ |
| 162 | (void)tfm_crypto_operation_release(handle_out); |
| 163 | return status; |
| 164 | } |
| 165 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 166 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 167 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 170 | psa_status_t tfm_crypto_cipher_decrypt_setup(psa_invec in_vec[], |
| 171 | size_t in_len, |
| 172 | psa_outvec out_vec[], |
| 173 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 174 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 175 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 176 | return PSA_ERROR_NOT_SUPPORTED; |
| 177 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 178 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 179 | psa_cipher_operation_t *operation = NULL; |
| 180 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 181 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 182 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 183 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 184 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 185 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 186 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 187 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 188 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 189 | uint32_t *handle_out = out_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 190 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 191 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 192 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 193 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 194 | status = tfm_crypto_check_handle_owner(key_id, NULL); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 195 | if (status != PSA_SUCCESS) { |
| 196 | return status; |
| 197 | } |
| 198 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 199 | /* Allocate the operation context in the secure world */ |
| 200 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_CIPHER_OPERATION, |
| 201 | &handle, |
| 202 | (void **)&operation); |
| 203 | if (status != PSA_SUCCESS) { |
| 204 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 205 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 206 | |
| 207 | *handle_out = handle; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 208 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 209 | if (status != PSA_SUCCESS) { |
| 210 | return status; |
| 211 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 212 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 213 | status = psa_cipher_decrypt_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 214 | if (status != PSA_SUCCESS) { |
| 215 | /* Release the operation context, ignore if the operation fails. */ |
| 216 | (void)tfm_crypto_operation_release(handle_out); |
| 217 | return status; |
| 218 | } |
| 219 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 220 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 221 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 224 | psa_status_t tfm_crypto_cipher_update(psa_invec in_vec[], |
| 225 | size_t in_len, |
| 226 | psa_outvec out_vec[], |
| 227 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 228 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 229 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 230 | return PSA_ERROR_NOT_SUPPORTED; |
| 231 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 232 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 233 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 234 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 235 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 2); |
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_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 238 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 239 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 240 | } |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 241 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 242 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 243 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 244 | uint32_t *handle_out = out_vec[0].base; |
| 245 | const uint8_t *input = in_vec[1].base; |
| 246 | size_t input_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 247 | unsigned char *output = out_vec[1].base; |
| 248 | size_t output_size = out_vec[1].len; |
| 249 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 250 | /* 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] | 251 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 252 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 253 | /* Initialise the output_length to zero */ |
| 254 | out_vec[1].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 255 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 256 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 257 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 258 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 259 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 260 | if (status != PSA_SUCCESS) { |
| 261 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 264 | status = psa_cipher_update(operation, input, input_length, |
| 265 | output, output_size, &out_vec[1].len); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 266 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 267 | /* Release the operation context, ignore if the operation fails. */ |
| 268 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 269 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 272 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 273 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 276 | psa_status_t tfm_crypto_cipher_finish(psa_invec in_vec[], |
| 277 | size_t in_len, |
| 278 | psa_outvec out_vec[], |
| 279 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 280 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 281 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 282 | return PSA_ERROR_NOT_SUPPORTED; |
| 283 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 284 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 285 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 286 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 287 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 288 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 289 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 290 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 291 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 292 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 293 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 294 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 295 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 296 | unsigned char *output = out_vec[1].base; |
| 297 | size_t output_size = out_vec[1].len; |
| 298 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 299 | /* 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] | 300 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 301 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 302 | /* Initialise the output_length to zero */ |
| 303 | out_vec[1].len = 0; |
| 304 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 305 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 306 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 307 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 308 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 309 | if (status != PSA_SUCCESS) { |
| 310 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 313 | 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] | 314 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 315 | /* Release the operation context, ignore if the operation fails. */ |
| 316 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 317 | return status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 320 | status = tfm_crypto_operation_release(handle_out); |
| 321 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 322 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 323 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 326 | psa_status_t tfm_crypto_cipher_abort(psa_invec in_vec[], |
| 327 | size_t in_len, |
| 328 | psa_outvec out_vec[], |
| 329 | size_t out_len) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 330 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 331 | #ifdef TFM_CRYPTO_CIPHER_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 332 | return PSA_ERROR_NOT_SUPPORTED; |
| 333 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 334 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 335 | psa_cipher_operation_t *operation = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 336 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 337 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 338 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 339 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 340 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 341 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 342 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 343 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 344 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 345 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 346 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 347 | /* 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] | 348 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 349 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 350 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 351 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_CIPHER_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 352 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 353 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 354 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 355 | /* Operation does not exist, so abort has no effect */ |
| 356 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 359 | status = psa_cipher_abort(operation); |
| 360 | |
| 361 | if (status != PSA_SUCCESS) { |
| 362 | /* Release the operation context, ignore if the operation fails. */ |
| 363 | (void)tfm_crypto_operation_release(handle_out); |
| 364 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 365 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 366 | |
| 367 | status = tfm_crypto_operation_release(handle_out); |
| 368 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 369 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 370 | #endif /* TFM_CRYPTO_CIPHER_MODULE_DISABLED */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 371 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 372 | |
| 373 | psa_status_t tfm_crypto_cipher_encrypt(psa_invec in_vec[], |
| 374 | size_t in_len, |
| 375 | psa_outvec out_vec[], |
| 376 | size_t out_len) |
| 377 | { |
| 378 | /* FixMe: To be implemented */ |
| 379 | return PSA_ERROR_NOT_SUPPORTED; |
| 380 | } |
| 381 | |
| 382 | psa_status_t tfm_crypto_cipher_decrypt(psa_invec in_vec[], |
| 383 | size_t in_len, |
| 384 | psa_outvec out_vec[], |
| 385 | size_t out_len) |
| 386 | { |
| 387 | /* FixMe: To be implemented */ |
| 388 | return PSA_ERROR_NOT_SUPPORTED; |
| 389 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 390 | /*!@}*/ |