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