blob: 35808267d2a69c762a28339f5c470b005cfc9491 [file] [log] [blame]
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
Louis Mayencourt7a36f782018-09-24 14:00:57 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Fox0e54ebc2019-04-09 14:21:04 +01008#include <stddef.h>
9#include <stdint.h>
Louis Mayencourt7a36f782018-09-24 14:00:57 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010012
Jamie Fox0e54ebc2019-04-09 14:21:04 +010013#include "tfm_crypto_api.h"
14#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010015#include "tfm_crypto_private.h"
Louis Mayencourt7a36f782018-09-24 14:00:57 +010016
17/*!
18 * \defgroup public_psa Public functions, PSA
19 *
20 */
21
22/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000023psa_status_t tfm_crypto_mac_sign_setup(psa_invec in_vec[],
24 size_t in_len,
25 psa_outvec out_vec[],
26 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +010027{
Kevin Peng96f802e2019-12-26 16:10:25 +080028#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010029 return PSA_ERROR_NOT_SUPPORTED;
30#else
Antonio de Angelis4743e672019-04-11 11:38:48 +010031 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032 psa_mac_operation_t *operation = NULL;
33
Soby Mathewd8abdfd2020-10-14 10:28:01 +010034 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000035
Antonio de Angelis4743e672019-04-11 11:38:48 +010036 if ((out_vec[0].len != sizeof(uint32_t)) ||
37 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010038 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000039 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010040 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010041 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010042 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +000043 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +010044 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +000045 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000046
David Hu105b4872021-05-19 16:43:19 +080047 status = tfm_crypto_check_handle_owner(key_id);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010048 if (status != PSA_SUCCESS) {
49 return status;
50 }
51
Jamie Fox0e54ebc2019-04-09 14:21:04 +010052 /* Init the handle in the operation with the one passed from the iov */
53 *handle_out = iov->op_handle;
54
55 /* Allocate the operation context in the secure world */
56 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
57 &handle,
58 (void **)&operation);
59 if (status != PSA_SUCCESS) {
60 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010061 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +010062
63 *handle_out = handle;
64
Maulik Patel28659c42021-01-06 14:09:22 +000065 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
66 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +080067 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +000068 }
69
70 status = psa_mac_sign_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +010071 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +080072 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010073 }
74
David Hu7e2e5232021-04-21 16:52:07 +080075 return status;
76
77exit:
78 /* Release the operation context, ignore if the operation fails. */
79 (void)tfm_crypto_operation_release(handle_out);
80 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010081#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +010082}
83
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000084psa_status_t tfm_crypto_mac_verify_setup(psa_invec in_vec[],
85 size_t in_len,
86 psa_outvec out_vec[],
87 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +010088{
Kevin Peng96f802e2019-12-26 16:10:25 +080089#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010090 return PSA_ERROR_NOT_SUPPORTED;
91#else
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010093 psa_mac_operation_t *operation = NULL;
94
Soby Mathewd8abdfd2020-10-14 10:28:01 +010095 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000096
Antonio de Angelis4743e672019-04-11 11:38:48 +010097 if ((out_vec[0].len != sizeof(uint32_t)) ||
98 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010099 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000100 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100101 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100102 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100103 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +0000104 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100105 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000106 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000107
David Hu105b4872021-05-19 16:43:19 +0800108 status = tfm_crypto_check_handle_owner(key_id);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100109 if (status != PSA_SUCCESS) {
110 return status;
111 }
112
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100113 /* Init the handle in the operation with the one passed from the iov */
114 *handle_out = iov->op_handle;
115
116 /* Allocate the operation context in the secure world */
117 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
118 &handle,
119 (void **)&operation);
120 if (status != PSA_SUCCESS) {
121 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100122 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100123
124 *handle_out = handle;
125
Maulik Patel28659c42021-01-06 14:09:22 +0000126 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
127 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800128 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +0000129 }
130
131 status = psa_mac_verify_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100132 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800133 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100134 }
135
David Hu7e2e5232021-04-21 16:52:07 +0800136 return status;
137
138exit:
139 /* Release the operation context, ignore if the operation fails. */
140 (void)tfm_crypto_operation_release(handle_out);
141 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100142#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100143}
144
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000145psa_status_t tfm_crypto_mac_update(psa_invec in_vec[],
146 size_t in_len,
147 psa_outvec out_vec[],
148 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100149{
Kevin Peng96f802e2019-12-26 16:10:25 +0800150#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100151 return PSA_ERROR_NOT_SUPPORTED;
152#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000153 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100154 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100155
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100156 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100157
Antonio de Angelis4743e672019-04-11 11:38:48 +0100158 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
159 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100160 return PSA_ERROR_PROGRAMMER_ERROR;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100161 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100162 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100163 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100164 uint32_t *handle_out = out_vec[0].base;
165 const uint8_t *input = in_vec[1].base;
166 size_t input_length = in_vec[1].len;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100167
Antonio de Angelis4743e672019-04-11 11:38:48 +0100168 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100169 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000170
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100171 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000172 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100173 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100174 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000175 if (status != PSA_SUCCESS) {
176 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100177 }
178
David Hu7e2e5232021-04-21 16:52:07 +0800179 return psa_mac_update(operation, input, input_length);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100180#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100181}
182
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000183psa_status_t tfm_crypto_mac_sign_finish(psa_invec in_vec[],
184 size_t in_len,
185 psa_outvec out_vec[],
186 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100187{
Kevin Peng96f802e2019-12-26 16:10:25 +0800188#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100189 return PSA_ERROR_NOT_SUPPORTED;
190#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000191 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100192 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100193
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100194 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000195
Antonio de Angelis4743e672019-04-11 11:38:48 +0100196 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
197 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100198 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000199 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100200 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100201 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100202 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000203 uint8_t *mac = out_vec[1].base;
204 size_t mac_size = out_vec[1].len;
205
Antonio de Angelis4743e672019-04-11 11:38:48 +0100206 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100207 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100208
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000209 /* Initialise mac_length to zero */
210 out_vec[1].len = 0;
211
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100212 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000213 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100214 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100215 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000216 if (status != PSA_SUCCESS) {
217 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100218 }
219
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100220 status = psa_mac_sign_finish(operation, mac, mac_size, &out_vec[1].len);
David Hu7e2e5232021-04-21 16:52:07 +0800221 if (status == PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100222 /* Release the operation context, ignore if the operation fails. */
223 (void)tfm_crypto_operation_release(handle_out);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100224 }
225
Antonio de Angelis4743e672019-04-11 11:38:48 +0100226 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100227#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100228}
229
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000230psa_status_t tfm_crypto_mac_verify_finish(psa_invec in_vec[],
231 size_t in_len,
232 psa_outvec out_vec[],
233 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100234{
Kevin Peng96f802e2019-12-26 16:10:25 +0800235#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100236 return PSA_ERROR_NOT_SUPPORTED;
237#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000238 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100239 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100240
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100241 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000242
Antonio de Angelis4743e672019-04-11 11:38:48 +0100243 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
244 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100245 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000246 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100247 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100248 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100249 uint32_t *handle_out = out_vec[0].base;
250 const uint8_t *mac = in_vec[1].base;
251 size_t mac_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000252
Antonio de Angelis4743e672019-04-11 11:38:48 +0100253 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100254 *handle_out = iov->op_handle;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100255
256 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000257 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100258 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100259 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000260 if (status != PSA_SUCCESS) {
261 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100262 }
263
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100264 status = psa_mac_verify_finish(operation, mac, mac_length);
David Hu7e2e5232021-04-21 16:52:07 +0800265 if (status == PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100266 /* Release the operation context, ignore if the operation fails. */
267 (void)tfm_crypto_operation_release(handle_out);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100268 }
269
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100270 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100271#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100272}
273
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000274psa_status_t tfm_crypto_mac_abort(psa_invec in_vec[],
275 size_t in_len,
276 psa_outvec out_vec[],
277 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100278{
Kevin Peng96f802e2019-12-26 16:10:25 +0800279#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100280 return PSA_ERROR_NOT_SUPPORTED;
281#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000282 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100283 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100284
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100285 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100286
Antonio de Angelis4743e672019-04-11 11:38:48 +0100287 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
288 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100289 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000290 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100291 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100292 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100293 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000294
Antonio de Angelis4743e672019-04-11 11:38:48 +0100295 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100296 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000297
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100298 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000299 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100300 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100301 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000302 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100303 /* Operation does not exist, so abort has no effect */
304 return PSA_SUCCESS;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100305 }
306
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100307 status = psa_mac_abort(operation);
308
309 if (status != PSA_SUCCESS) {
310 /* Release the operation context, ignore if the operation fails. */
311 (void)tfm_crypto_operation_release(handle_out);
312 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100313 }
314
David Hu7e2e5232021-04-21 16:52:07 +0800315 return tfm_crypto_operation_release(handle_out);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100316#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100317}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100318
319psa_status_t tfm_crypto_mac_compute(psa_invec in_vec[],
320 size_t in_len,
321 psa_outvec out_vec[],
322 size_t out_len)
323{
Antonio de Angelis8f4db962021-07-05 13:58:43 +0200324#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100325 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis8f4db962021-07-05 13:58:43 +0200326#else
327
328 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1);
329
330 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
331 return PSA_ERROR_PROGRAMMER_ERROR;
332 }
333
334 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
335 psa_algorithm_t alg = iov->alg;
336 psa_key_id_t key_id = iov->key_id;
337 const uint8_t *input = in_vec[1].base;
338 size_t input_length = in_vec[1].len;
339 uint8_t *output = out_vec[0].base;
340 size_t output_size = out_vec[0].len;
341 psa_status_t status;
342 mbedtls_svc_key_id_t encoded_key;
343
344 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
345 if (status != PSA_SUCCESS) {
346 return status;
347 }
348
349 return psa_mac_compute(encoded_key, alg, input, input_length,
350 output, output_size, &out_vec[0].len);
351
352#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100353}
354
355psa_status_t tfm_crypto_mac_verify(psa_invec in_vec[],
356 size_t in_len,
357 psa_outvec out_vec[],
358 size_t out_len)
359{
Antonio de Angelis8f4db962021-07-05 13:58:43 +0200360#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100361 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis8f4db962021-07-05 13:58:43 +0200362#else
363
364 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0);
365
366 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
367 return PSA_ERROR_PROGRAMMER_ERROR;
368 }
369
370 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
371 psa_algorithm_t alg = iov->alg;
372 psa_key_id_t key_id = iov->key_id;
373 const uint8_t *input = in_vec[1].base;
374 size_t input_length = in_vec[1].len;
375 const uint8_t *mac = in_vec[2].base;
376 size_t mac_length = in_vec[2].len;
377 mbedtls_svc_key_id_t encoded_key;
378 psa_status_t status;
379
380 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
381 if (status != PSA_SUCCESS) {
382 return status;
383 }
384
385 return psa_mac_verify(encoded_key, alg,
386 input, input_length,
387 mac, mac_length);
388
389#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100390}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100391/*!@}*/