blob: 4c9ba2d285a959f92677bc8862685466344a962a [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
Jamie Fox0e54ebc2019-04-09 14:21:04 +010047 /* Init the handle in the operation with the one passed from the iov */
48 *handle_out = iov->op_handle;
49
50 /* Allocate the operation context in the secure world */
51 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
52 &handle,
53 (void **)&operation);
54 if (status != PSA_SUCCESS) {
55 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010056 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057
58 *handle_out = handle;
59
Maulik Patel28659c42021-01-06 14:09:22 +000060 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
61 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +080062 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +000063 }
64
65 status = psa_mac_sign_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +010066 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +080067 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010068 }
69
David Hu7e2e5232021-04-21 16:52:07 +080070 return status;
71
72exit:
73 /* Release the operation context, ignore if the operation fails. */
74 (void)tfm_crypto_operation_release(handle_out);
75 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +010076#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +010077}
78
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000079psa_status_t tfm_crypto_mac_verify_setup(psa_invec in_vec[],
80 size_t in_len,
81 psa_outvec out_vec[],
82 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +010083{
Kevin Peng96f802e2019-12-26 16:10:25 +080084#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010085 return PSA_ERROR_NOT_SUPPORTED;
86#else
Antonio de Angelis4743e672019-04-11 11:38:48 +010087 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010088 psa_mac_operation_t *operation = NULL;
89
Soby Mathewd8abdfd2020-10-14 10:28:01 +010090 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000091
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 if ((out_vec[0].len != sizeof(uint32_t)) ||
93 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010094 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000095 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010096 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010097 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010098 uint32_t *handle_out = out_vec[0].base;
Maulik Patel28659c42021-01-06 14:09:22 +000099 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100100 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000101 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000102
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100103 /* Init the handle in the operation with the one passed from the iov */
104 *handle_out = iov->op_handle;
105
106 /* Allocate the operation context in the secure world */
107 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
108 &handle,
109 (void **)&operation);
110 if (status != PSA_SUCCESS) {
111 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100112 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100113
114 *handle_out = handle;
115
Maulik Patel28659c42021-01-06 14:09:22 +0000116 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
117 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800118 goto exit;
Maulik Patel28659c42021-01-06 14:09:22 +0000119 }
120
121 status = psa_mac_verify_setup(operation, encoded_key, alg);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100122 if (status != PSA_SUCCESS) {
David Hu7e2e5232021-04-21 16:52:07 +0800123 goto exit;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100124 }
125
David Hu7e2e5232021-04-21 16:52:07 +0800126 return status;
127
128exit:
129 /* Release the operation context, ignore if the operation fails. */
130 (void)tfm_crypto_operation_release(handle_out);
131 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100132#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100133}
134
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000135psa_status_t tfm_crypto_mac_update(psa_invec in_vec[],
136 size_t in_len,
137 psa_outvec out_vec[],
138 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100139{
Kevin Peng96f802e2019-12-26 16:10:25 +0800140#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100141 return PSA_ERROR_NOT_SUPPORTED;
142#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000143 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100144 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100145
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100146 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100147
Antonio de Angelis4743e672019-04-11 11:38:48 +0100148 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
149 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100150 return PSA_ERROR_PROGRAMMER_ERROR;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100151 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100152 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100153 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100154 uint32_t *handle_out = out_vec[0].base;
155 const uint8_t *input = in_vec[1].base;
156 size_t input_length = in_vec[1].len;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100157
Antonio de Angelis4743e672019-04-11 11:38:48 +0100158 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100159 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000160
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100161 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000162 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100163 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100164 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000165 if (status != PSA_SUCCESS) {
166 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100167 }
168
David Hu7e2e5232021-04-21 16:52:07 +0800169 return psa_mac_update(operation, input, input_length);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100170#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100171}
172
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000173psa_status_t tfm_crypto_mac_sign_finish(psa_invec in_vec[],
174 size_t in_len,
175 psa_outvec out_vec[],
176 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100177{
Kevin Peng96f802e2019-12-26 16:10:25 +0800178#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100179 return PSA_ERROR_NOT_SUPPORTED;
180#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000181 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100182 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100183
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100184 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000185
Antonio de Angelis4743e672019-04-11 11:38:48 +0100186 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
187 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100188 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000189 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100190 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100191 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100192 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000193 uint8_t *mac = out_vec[1].base;
194 size_t mac_size = out_vec[1].len;
195
Antonio de Angelis4743e672019-04-11 11:38:48 +0100196 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100197 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100198
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000199 /* Initialise mac_length to zero */
200 out_vec[1].len = 0;
201
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100202 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000203 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100204 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100205 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000206 if (status != PSA_SUCCESS) {
207 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100208 }
209
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100210 status = psa_mac_sign_finish(operation, mac, mac_size, &out_vec[1].len);
David Hu7e2e5232021-04-21 16:52:07 +0800211 if (status == PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100212 /* Release the operation context, ignore if the operation fails. */
213 (void)tfm_crypto_operation_release(handle_out);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100214 }
215
Antonio de Angelis4743e672019-04-11 11:38:48 +0100216 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100217#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100218}
219
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000220psa_status_t tfm_crypto_mac_verify_finish(psa_invec in_vec[],
221 size_t in_len,
222 psa_outvec out_vec[],
223 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100224{
Kevin Peng96f802e2019-12-26 16:10:25 +0800225#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100226 return PSA_ERROR_NOT_SUPPORTED;
227#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000228 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100229 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100230
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100231 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000232
Antonio de Angelis4743e672019-04-11 11:38:48 +0100233 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
234 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100235 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000236 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100238 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100239 uint32_t *handle_out = out_vec[0].base;
240 const uint8_t *mac = in_vec[1].base;
241 size_t mac_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000242
Antonio de Angelis4743e672019-04-11 11:38:48 +0100243 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100244 *handle_out = iov->op_handle;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100245
246 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000247 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100248 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100249 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000250 if (status != PSA_SUCCESS) {
251 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100252 }
253
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100254 status = psa_mac_verify_finish(operation, mac, mac_length);
David Hu7e2e5232021-04-21 16:52:07 +0800255 if (status == PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100256 /* Release the operation context, ignore if the operation fails. */
257 (void)tfm_crypto_operation_release(handle_out);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100258 }
259
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100260 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100261#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100262}
263
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000264psa_status_t tfm_crypto_mac_abort(psa_invec in_vec[],
265 size_t in_len,
266 psa_outvec out_vec[],
267 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100268{
Kevin Peng96f802e2019-12-26 16:10:25 +0800269#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100270 return PSA_ERROR_NOT_SUPPORTED;
271#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000272 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100273 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100274
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100275 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100276
Antonio de Angelis4743e672019-04-11 11:38:48 +0100277 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
278 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100279 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000280 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100281 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100282 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100283 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000284
Antonio de Angelis4743e672019-04-11 11:38:48 +0100285 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100286 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000287
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100288 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000289 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100290 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100291 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000292 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100293 /* Operation does not exist, so abort has no effect */
294 return PSA_SUCCESS;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100295 }
296
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100297 status = psa_mac_abort(operation);
298
299 if (status != PSA_SUCCESS) {
300 /* Release the operation context, ignore if the operation fails. */
301 (void)tfm_crypto_operation_release(handle_out);
302 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100303 }
304
David Hu7e2e5232021-04-21 16:52:07 +0800305 return tfm_crypto_operation_release(handle_out);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100306#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100307}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100308
309psa_status_t tfm_crypto_mac_compute(psa_invec in_vec[],
310 size_t in_len,
311 psa_outvec out_vec[],
312 size_t out_len)
313{
Summer Qin045ec4a2021-07-07 14:28:04 +0800314#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100315 return PSA_ERROR_NOT_SUPPORTED;
Summer Qin045ec4a2021-07-07 14:28:04 +0800316#else
317 psa_status_t status = PSA_SUCCESS;
318
319 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
320
321 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
322 return PSA_ERROR_PROGRAMMER_ERROR;
323 }
324 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
325 psa_key_id_t key_id = iov->key_id;
326 psa_algorithm_t alg = iov->alg;
327 const uint8_t *input = in_vec[1].base;
328 size_t input_length = in_vec[1].len;
329 uint8_t *mac = out_vec[0].base;
330 size_t mac_size = out_vec[0].len;
331 mbedtls_svc_key_id_t encoded_key;
332
Summer Qin045ec4a2021-07-07 14:28:04 +0800333 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
334 if (status != PSA_SUCCESS) {
335 return status;
336 }
337
338 return psa_mac_compute(encoded_key, alg, input, input_length, mac, mac_size,
339 &out_vec[0].len);
340#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100341}
342
343psa_status_t tfm_crypto_mac_verify(psa_invec in_vec[],
344 size_t in_len,
345 psa_outvec out_vec[],
346 size_t out_len)
347{
Summer Qin045ec4a2021-07-07 14:28:04 +0800348#ifdef TFM_CRYPTO_MAC_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100349 return PSA_ERROR_NOT_SUPPORTED;
Summer Qin045ec4a2021-07-07 14:28:04 +0800350#else
351 psa_status_t status = PSA_SUCCESS;
352
353 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0);
354
355 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
356 return PSA_ERROR_PROGRAMMER_ERROR;
357 }
358 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
359 psa_key_id_t key_id = iov->key_id;
360 psa_algorithm_t alg = iov->alg;
361 const uint8_t *input = in_vec[1].base;
362 size_t input_length = in_vec[1].len;
363 const uint8_t *mac = in_vec[2].base;
364 size_t mac_length = in_vec[2].len;
365 mbedtls_svc_key_id_t encoded_key;
366
Summer Qin045ec4a2021-07-07 14:28:04 +0800367 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
368 if (status != PSA_SUCCESS) {
369 return status;
370 }
371
372 return psa_mac_verify(encoded_key, alg, input, input_length, mac,
373 mac_length);
374#endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100375}
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100376/*!@}*/