blob: 9d5ae46e62498bc80a54969fee3136d670f76842 [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"
Jamie Fox0ff04ba2019-02-05 14:19:07 +000020
Antonio de Angelisa6f72162018-09-05 11:00:37 +010021/*!
22 * \defgroup public_psa Public functions, PSA
23 *
24 */
25
26/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000027psa_status_t tfm_crypto_hash_setup(psa_invec in_vec[],
28 size_t in_len,
29 psa_outvec out_vec[],
30 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +010031{
Kevin Peng96f802e2019-12-26 16:10:25 +080032#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010033 return PSA_ERROR_NOT_SUPPORTED;
34#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010035 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010036 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010037
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000038 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080039 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010040 }
41
Antonio de Angelis4743e672019-04-11 11:38:48 +010042 if ((out_vec[0].len != sizeof(uint32_t)) ||
43 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080044 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000045 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010046 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010047 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010048 uint32_t *handle_out = out_vec[0].base;
49 psa_algorithm_t alg = iov->alg;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000050
Antonio de Angelis4743e672019-04-11 11:38:48 +010051 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010052 *handle_out = iov->op_handle;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010053
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010054 /* Allocate the operation context in the secure world */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000055 status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +010056 &handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000058 if (status != PSA_SUCCESS) {
59 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010060 }
61
Antonio de Angelis4743e672019-04-11 11:38:48 +010062 *handle_out = handle;
63
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064 status = psa_hash_setup(operation, alg);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010065 if (status != PSA_SUCCESS) {
Hugues de Valon8b442442019-02-19 14:30:52 +000066 /* Release the operation context, ignore if the operation fails. */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010067 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000068 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010069 }
70
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000071 return PSA_SUCCESS;
Antonio de Angelis7740b382019-07-16 10:59:25 +010072#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +010073}
74
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000075psa_status_t tfm_crypto_hash_update(psa_invec in_vec[],
76 size_t in_len,
77 psa_outvec out_vec[],
78 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +010079{
Kevin Peng96f802e2019-12-26 16:10:25 +080080#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010081 return PSA_ERROR_NOT_SUPPORTED;
82#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010083 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010084 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010085
Antonio de Angelis4743e672019-04-11 11:38:48 +010086 if ((in_len != 2) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080087 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisa6f72162018-09-05 11:00:37 +010088 }
89
Antonio de Angelis4743e672019-04-11 11:38:48 +010090 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
91 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080092 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000093 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010094 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010095 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010096 uint32_t *handle_out = out_vec[0].base;
97 const uint8_t *input = in_vec[1].base;
98 size_t input_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000099
Antonio de Angelis4743e672019-04-11 11:38:48 +0100100 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100101 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000102
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100103 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000104 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100105 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100106 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000107 if (status != PSA_SUCCESS) {
108 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100109 }
110
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100111 status = psa_hash_update(operation, input, input_length);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100112 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100113 /* Release the operation context, ignore if the operation fails. */
114 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000115 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100116 }
117
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000118 return PSA_SUCCESS;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100119#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100120}
121
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122psa_status_t tfm_crypto_hash_finish(psa_invec in_vec[],
123 size_t in_len,
124 psa_outvec out_vec[],
125 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100126{
Kevin Peng96f802e2019-12-26 16:10:25 +0800127#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100128 return PSA_ERROR_NOT_SUPPORTED;
129#else
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100130 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100131 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100132
Antonio de Angelis4743e672019-04-11 11:38:48 +0100133 if ((in_len != 1) || (out_len != 2)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800134 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100135 }
136
Antonio de Angelis4743e672019-04-11 11:38:48 +0100137 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
138 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800139 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000140 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100141 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100142 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100143 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000144 uint8_t *hash = out_vec[1].base;
145 size_t hash_size = out_vec[1].len;
146
Antonio de Angelis4743e672019-04-11 11:38:48 +0100147 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100148 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100149
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000150 /* Initialise hash_length to zero */
151 out_vec[1].len = 0;
152
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100153 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000154 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100155 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100156 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000157 if (status != PSA_SUCCESS) {
158 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100159 }
160
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100161 status = psa_hash_finish(operation, hash, hash_size, &out_vec[1].len);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100162 if (status != PSA_SUCCESS) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100163 /* Release the operation context, ignore if the operation fails. */
164 (void)tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000165 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100166 }
167
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100168 status = tfm_crypto_operation_release(handle_out);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000169
170 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100171#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000172}
173
174psa_status_t tfm_crypto_hash_verify(psa_invec in_vec[],
175 size_t in_len,
176 psa_outvec out_vec[],
177 size_t out_len)
178{
Kevin Peng96f802e2019-12-26 16:10:25 +0800179#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100180 return PSA_ERROR_NOT_SUPPORTED;
181#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000182 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100183 psa_hash_operation_t *operation = NULL;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100184
Antonio de Angelis4743e672019-04-11 11:38:48 +0100185 if ((in_len != 2) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800186 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100187 }
188
Antonio de Angelis4743e672019-04-11 11:38:48 +0100189 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
190 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800191 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100193 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
194 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100195 uint32_t *handle_out = out_vec[0].base;
196 const uint8_t *hash = in_vec[1].base;
197 size_t hash_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000198
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100199 /* Init the handle in the operation with the one passed from the iov */
200 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000201
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100202 /* Look up the corresponding operation context */
203 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
204 handle,
205 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000206 if (status != PSA_SUCCESS) {
207 return status;
208 }
209
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100210 status = psa_hash_verify(operation, hash, hash_length);
211 if (status != PSA_SUCCESS) {
212 /* Release the operation context, ignore if the operation fails. */
213 (void)tfm_crypto_operation_release(handle_out);
214 return status;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100215 }
216
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100217 status = tfm_crypto_operation_release(handle_out);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100218
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100219 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100220#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100221}
222
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000223psa_status_t tfm_crypto_hash_abort(psa_invec in_vec[],
224 size_t in_len,
225 psa_outvec out_vec[],
226 size_t out_len)
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100227{
Kevin Peng96f802e2019-12-26 16:10:25 +0800228#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100229 return PSA_ERROR_NOT_SUPPORTED;
230#else
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000231 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100232 psa_hash_operation_t *operation = NULL;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000233
Antonio de Angelis4743e672019-04-11 11:38:48 +0100234 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800235 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis377a1552018-11-22 17:02:40 +0000236 }
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100237
Antonio de Angelis4743e672019-04-11 11:38:48 +0100238 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
239 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800240 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000241 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100242 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100243 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100244 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000245
Antonio de Angelis4743e672019-04-11 11:38:48 +0100246 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100247 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000248
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100249 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000250 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100251 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100252 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000253 if (status != PSA_SUCCESS) {
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100254 /* Operation does not exist, so abort has no effect */
255 return PSA_SUCCESS;
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100256 }
257
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100258 status = psa_hash_abort(operation);
259 if (status != PSA_SUCCESS) {
260 /* Release the operation context, ignore if the operation fails. */
261 (void)tfm_crypto_operation_release(handle_out);
262 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100263 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100264
265 status = tfm_crypto_operation_release(handle_out);
266
Antonio de Angelis4743e672019-04-11 11:38:48 +0100267 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100268#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100269}
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100270
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100271psa_status_t tfm_crypto_hash_clone(psa_invec in_vec[],
272 size_t in_len,
273 psa_outvec out_vec[],
274 size_t out_len)
275{
Kevin Peng96f802e2019-12-26 16:10:25 +0800276#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100277 return PSA_ERROR_NOT_SUPPORTED;
278#else
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100279 psa_status_t status = PSA_SUCCESS;
280 psa_hash_operation_t *source_operation = NULL;
281 psa_hash_operation_t *target_operation = NULL;
282
283 if ((in_len != 1) || (out_len != 1)) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800284 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100285 }
286
287 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
288 (out_vec[0].len != sizeof(uint32_t))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800289 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100290 }
291 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
292 uint32_t source_handle = iov->op_handle;
293 uint32_t *target_handle = out_vec[0].base;
294
295 /* Look up the corresponding source operation context */
296 status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION,
297 source_handle,
298 (void **)&source_operation);
299 if (status != PSA_SUCCESS) {
300 return status;
301 }
302
303 /* Allocate the target operation context in the secure world */
304 status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION,
305 target_handle,
306 (void **)&target_operation);
307 if (status != PSA_SUCCESS) {
308 return status;
309 }
310
311 status = psa_hash_clone(source_operation, target_operation);
312 if (status != PSA_SUCCESS) {
313 /* Release the target operation context, ignore if it fails. */
314 (void)tfm_crypto_operation_release(target_handle);
315 return status;
316 }
317
318 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100319#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100320}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100321
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100322psa_status_t tfm_crypto_hash_compute(psa_invec in_vec[],
323 size_t in_len,
324 psa_outvec out_vec[],
325 size_t out_len)
326{
Soby Mathew07ef6e42020-07-20 21:09:23 +0100327#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100328 return PSA_ERROR_NOT_SUPPORTED;
Soby Mathew07ef6e42020-07-20 21:09:23 +0100329#else
330 if ((in_len != 2) || (out_len != 1)) {
331 return PSA_ERROR_CONNECTION_REFUSED;
332 }
333
334 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
335 return PSA_ERROR_CONNECTION_REFUSED;
336 }
337
338 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
339 psa_algorithm_t alg = iov->alg;
340 const uint8_t *input = in_vec[1].base;
341 size_t input_length = in_vec[1].len;
342 uint8_t *hash = out_vec[0].base;
343 size_t hash_size = out_vec[0].len;
344
345 /* Initialize hash_length to zero */
346 out_vec[0].len = 0;
347 return psa_hash_compute(alg, input, input_length, hash, hash_size,
348 &out_vec[0].len);
349#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
350}
351
352psa_status_t tfm_crypto_hash_compare(psa_invec in_vec[],
353 size_t in_len,
354 psa_outvec out_vec[],
355 size_t out_len)
356{
357#ifdef TFM_CRYPTO_HASH_MODULE_DISABLED
358 return PSA_ERROR_NOT_SUPPORTED;
359#else
360 if (in_len != 3) {
361 return PSA_ERROR_CONNECTION_REFUSED;
362 }
363
364 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
365 return PSA_ERROR_CONNECTION_REFUSED;
366 }
367
368 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
369 psa_algorithm_t alg = iov->alg;
370 const uint8_t *input = in_vec[1].base;
371 size_t input_length = in_vec[1].len;
372 const uint8_t *hash = in_vec[2].base;
373 size_t hash_length = in_vec[2].len;
374
375 return psa_hash_compare(alg, input, input_length, hash, hash_length);
376#endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100377}
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100378/*!@}*/