blob: 6a706e87b6d5a85636c0eab5d5c823aadf9fdd54 [file] [log] [blame]
Antonio de Angelisa6f72162018-09-05 11:00:37 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Antonio de Angelisa6f72162018-09-05 11:00:37 +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>
Antonio de Angelisa6f72162018-09-05 11:00:37 +010010
Summer Qin4b1d03b2019-07-02 14:56:08 +080011/* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter
Antonio de Angelis4743e672019-04-11 11:38:48 +010012 * integrity checks but this will have to be revised
13 * when the full set of error codes mandated by PSA FF
14 * is available.
15 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010016#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010017
Jamie Fox0e54ebc2019-04-09 14:21:04 +010018#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010020#include "tfm_crypto_private.h"
Jamie Fox0ff04ba2019-02-05 14:19:07 +000021
Antonio de Angelisa6f72162018-09-05 11:00:37 +010022/*!
23 * \defgroup public_psa Public functions, PSA
24 *
25 */
26
27/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000028psa_status_t tfm_crypto_hash_setup(psa_invec in_vec[],
29 size_t in_len,
30 psa_outvec out_vec[],
31 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +010032{
Kevin Peng96f802e2019-12-26 16:10:25 +080033#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010034 return PSA_ERROR_NOT_SUPPORTED;
35#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010036 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010038
Soby Mathewd8abdfd2020-10-14 10:28:01 +010039 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisa6f72162018-09-05 11:00:37 +010040
Antonio de Angelis4743e672019-04-11 11:38:48 +010041 if ((out_vec[0].len != sizeof(uint32_t)) ||
42 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080043 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000044 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010045 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010046 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010047 uint32_t *handle_out = out_vec[0].base;
48 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000049
Antonio de Angelis4743e672019-04-11 11:38:48 +010050 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010051 *handle_out = iov->op_handle;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010052
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010053 /* Allocate the operation context in the secure world */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000054 status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +010055 &handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010056 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000057 if (status != PSA_SUCCESS) {
58 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010059 }
60
Antonio de Angelis4743e672019-04-11 11:38:48 +010061 *handle_out = handle;
62
Jamie Fox0e54ebc2019-04-09 14:21:04 +010063 status = psa_hash_setup(operation, alg);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010064 if (status != PSA_SUCCESS) {
Hugues de Valon8b442442019-02-19 14:30:52 +000065 /* Release the operation context, ignore if the operation fails. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010066 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000067 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010068 }
69
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000070 return PSA_SUCCESS;
Antonio de Angelis7740b382019-07-16 10:59:25 +010071#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +010072}
73
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000074psa_status_t tfm_crypto_hash_update(psa_invec in_vec[],
75 size_t in_len,
76 psa_outvec out_vec[],
77 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +010078{
Kevin Peng96f802e2019-12-26 16:10:25 +080079#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010080 return PSA_ERROR_NOT_SUPPORTED;
81#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010082 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010083 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010084
Soby Mathewd8abdfd2020-10-14 10:28:01 +010085 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelisa6f72162018-09-05 11:00:37 +010086
Antonio de Angelis4743e672019-04-11 11:38:48 +010087 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
88 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080089 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000090 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010091 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010092 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010093 uint32_t *handle_out = out_vec[0].base;
94 const uint8_t *input = in_vec[1].base;
95 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000096
Antonio de Angelis4743e672019-04-11 11:38:48 +010097 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010098 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000099
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100100 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000101 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100102 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100103 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000104 if (status != PSA_SUCCESS) {
105 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100106 }
107
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100108 status = psa_hash_update(operation, input, input_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100109 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100110 /* Release the operation context, ignore if the operation fails. */
111 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000112 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100113 }
114
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000115 return PSA_SUCCESS;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100116#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100117}
118
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000119psa_status_t tfm_crypto_hash_finish(psa_invec in_vec[],
120 size_t in_len,
121 psa_outvec out_vec[],
122 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100123{
Kevin Peng96f802e2019-12-26 16:10:25 +0800124#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100125 return PSA_ERROR_NOT_SUPPORTED;
126#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100127 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100128 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100129
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100130 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100131
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
133 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800134 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000135 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100136 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100137 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100138 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000139 uint8_t *hash = out_vec[1].base;
140 size_t hash_size = out_vec[1].len;
141
Antonio de Angelis4743e672019-04-11 11:38:48 +0100142 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100143 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100144
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000145 /* Initialise hash_length to zero */
146 out_vec[1].len = 0;
147
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100148 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000149 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100150 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100151 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000152 if (status != PSA_SUCCESS) {
153 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100154 }
155
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100156 status = psa_hash_finish(operation, hash, hash_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100157 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100158 /* Release the operation context, ignore if the operation fails. */
159 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000160 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100161 }
162
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100163 status = tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000164
165 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100166#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000167}
168
169psa_status_t tfm_crypto_hash_verify(psa_invec in_vec[],
170 size_t in_len,
171 psa_outvec out_vec[],
172 size_t out_len)
173{
Kevin Peng96f802e2019-12-26 16:10:25 +0800174#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100175 return PSA_ERROR_NOT_SUPPORTED;
176#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000177 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100178 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100179
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100180 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100181
Antonio de Angelis4743e672019-04-11 11:38:48 +0100182 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
183 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800184 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000185 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100186 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
187 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100188 uint32_t *handle_out = out_vec[0].base;
189 const uint8_t *hash = in_vec[1].base;
190 size_t hash_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000191
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100192 /* Init the handle in the operation with the one passed from the iov */
193 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000194
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100195 /* Look up the corresponding operation context */
196 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
197 handle,
198 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000199 if (status != PSA_SUCCESS) {
200 return status;
201 }
202
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100203 status = psa_hash_verify(operation, hash, hash_length);
204 if (status != PSA_SUCCESS) {
205 /* Release the operation context, ignore if the operation fails. */
206 (void)tfm_crypto_operation_release(handle_out);
207 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100208 }
209
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100210 status = tfm_crypto_operation_release(handle_out);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100211
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100212 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100213#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100214}
215
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000216psa_status_t tfm_crypto_hash_abort(psa_invec in_vec[],
217 size_t in_len,
218 psa_outvec out_vec[],
219 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100220{
Kevin Peng96f802e2019-12-26 16:10:25 +0800221#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100222 return PSA_ERROR_NOT_SUPPORTED;
223#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000224 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100225 psa_hash_operation_t *operation = NULL;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000226
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100227 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100228
Antonio de Angelis4743e672019-04-11 11:38:48 +0100229 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
230 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800231 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000232 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100233 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100234 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100235 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000236
Antonio de Angelis4743e672019-04-11 11:38:48 +0100237 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100238 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000239
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100240 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000241 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100242 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100243 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000244 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100245 /* Operation does not exist, so abort has no effect */
246 return PSA_SUCCESS;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100247 }
248
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100249 status = psa_hash_abort(operation);
250 if (status != PSA_SUCCESS) {
251 /* Release the operation context, ignore if the operation fails. */
252 (void)tfm_crypto_operation_release(handle_out);
253 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100254 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100255
256 status = tfm_crypto_operation_release(handle_out);
257
Antonio de Angelis4743e672019-04-11 11:38:48 +0100258 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100259#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100260}
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100261
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100262psa_status_t tfm_crypto_hash_clone(psa_invec in_vec[],
263 size_t in_len,
264 psa_outvec out_vec[],
265 size_t out_len)
266{
Kevin Peng96f802e2019-12-26 16:10:25 +0800267#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100268 return PSA_ERROR_NOT_SUPPORTED;
269#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100270 psa_status_t status = PSA_SUCCESS;
271 psa_hash_operation_t *source_operation = NULL;
272 psa_hash_operation_t *target_operation = NULL;
273
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100274 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100275
276 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
277 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800278 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100279 }
280 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
281 uint32_t source_handle = iov->op_handle;
282 uint32_t *target_handle = out_vec[0].base;
283
284 /* Look up the corresponding source operation context */
285 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
286 source_handle,
287 (void **)&source_operation);
288 if (status != PSA_SUCCESS) {
289 return status;
290 }
291
292 /* Allocate the target operation context in the secure world */
293 status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION,
294 target_handle,
295 (void **)&target_operation);
296 if (status != PSA_SUCCESS) {
297 return status;
298 }
299
300 status = psa_hash_clone(source_operation, target_operation);
301 if (status != PSA_SUCCESS) {
302 /* Release the target operation context, ignore if it fails. */
303 (void)tfm_crypto_operation_release(target_handle);
304 return status;
305 }
306
307 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100308#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100309}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100310
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100311psa_status_t tfm_crypto_hash_compute(psa_invec in_vec[],
312 size_t in_len,
313 psa_outvec out_vec[],
314 size_t out_len)
315{
Soby Mathew07ef6e42020-07-20 21:09:23 +0100316#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100317 return PSA_ERROR_NOT_SUPPORTED;
Soby Mathew07ef6e42020-07-20 21:09:23 +0100318#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100319
320 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1);
Soby Mathew07ef6e42020-07-20 21:09:23 +0100321
322 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
323 return PSA_ERROR_CONNECTION_REFUSED;
324 }
325
326 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
327 psa_algorithm_t alg = iov->alg;
328 const uint8_t *input = in_vec[1].base;
329 size_t input_length = in_vec[1].len;
330 uint8_t *hash = out_vec[0].base;
331 size_t hash_size = out_vec[0].len;
332
333 /* Initialize hash_length to zero */
334 out_vec[0].len = 0;
335 return psa_hash_compute(alg, input, input_length, hash, hash_size,
336 &out_vec[0].len);
337#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
338}
339
340psa_status_t tfm_crypto_hash_compare(psa_invec in_vec[],
341 size_t in_len,
342 psa_outvec out_vec[],
343 size_t out_len)
344{
345#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
346 return PSA_ERROR_NOT_SUPPORTED;
347#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100348
349 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0);
Soby Mathew07ef6e42020-07-20 21:09:23 +0100350
351 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
352 return PSA_ERROR_CONNECTION_REFUSED;
353 }
354
355 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
356 psa_algorithm_t alg = iov->alg;
357 const uint8_t *input = in_vec[1].base;
358 size_t input_length = in_vec[1].len;
359 const uint8_t *hash = in_vec[2].base;
360 size_t hash_length = in_vec[2].len;
361
362 return psa_hash_compare(alg, input, input_length, hash, hash_length);
363#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100364}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100365/*!@}*/