Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +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 | a6f7216 | 2018-09-05 11:00:37 +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" |
Jamie Fox | 0ff04ba | 2019-02-05 14:19:07 +0000 | [diff] [blame] | 16 | |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 17 | /*! |
| 18 | * \defgroup public_psa Public functions, PSA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 23 | psa_status_t tfm_crypto_hash_setup(psa_invec in_vec[], |
| 24 | size_t in_len, |
| 25 | psa_outvec out_vec[], |
| 26 | size_t out_len) |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 27 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 28 | #ifdef TFM_CRYPTO_HASH_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 | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 31 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 32 | psa_hash_operation_t *operation = NULL; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 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, 1); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 35 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 36 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 37 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 38 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 39 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 40 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 41 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 42 | uint32_t *handle_out = out_vec[0].base; |
| 43 | psa_algorithm_t alg = iov->alg; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 44 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 45 | /* 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] | 46 | *handle_out = iov->op_handle; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 47 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 48 | /* Allocate the operation context in the secure world */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 49 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 50 | &handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 51 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 52 | if (status != PSA_SUCCESS) { |
| 53 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 56 | *handle_out = handle; |
| 57 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 58 | status = psa_hash_setup(operation, alg); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 59 | if (status != PSA_SUCCESS) { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame] | 60 | /* Release the operation context, ignore if the operation fails. */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 61 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 62 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 65 | return PSA_SUCCESS; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 66 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 69 | psa_status_t tfm_crypto_hash_update(psa_invec in_vec[], |
| 70 | size_t in_len, |
| 71 | psa_outvec out_vec[], |
| 72 | size_t out_len) |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 73 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 74 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 75 | return PSA_ERROR_NOT_SUPPORTED; |
| 76 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 77 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 78 | psa_hash_operation_t *operation = NULL; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 79 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 80 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 81 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 82 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 83 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 84 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 85 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 86 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 87 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 88 | uint32_t *handle_out = out_vec[0].base; |
| 89 | const uint8_t *input = in_vec[1].base; |
| 90 | size_t input_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 91 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | /* 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] | 93 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 94 | |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 95 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 96 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 97 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 98 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 99 | if (status != PSA_SUCCESS) { |
| 100 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 103 | status = psa_hash_update(operation, input, input_length); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 104 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 105 | /* Release the operation context, ignore if the operation fails. */ |
| 106 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 107 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 110 | return PSA_SUCCESS; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 111 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 114 | psa_status_t tfm_crypto_hash_finish(psa_invec in_vec[], |
| 115 | size_t in_len, |
| 116 | psa_outvec out_vec[], |
| 117 | size_t out_len) |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 118 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 119 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 120 | return PSA_ERROR_NOT_SUPPORTED; |
| 121 | #else |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 122 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 123 | psa_hash_operation_t *operation = NULL; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 124 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 125 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 126 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 127 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 128 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 129 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 130 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 131 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 132 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 133 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 134 | uint8_t *hash = out_vec[1].base; |
| 135 | size_t hash_size = out_vec[1].len; |
| 136 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 137 | /* 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] | 138 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 139 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 140 | /* Initialise hash_length to zero */ |
| 141 | out_vec[1].len = 0; |
| 142 | |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 143 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 144 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 145 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 146 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 147 | if (status != PSA_SUCCESS) { |
| 148 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 151 | status = psa_hash_finish(operation, hash, hash_size, &out_vec[1].len); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 152 | if (status != PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 153 | /* Release the operation context, ignore if the operation fails. */ |
| 154 | (void)tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 155 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 158 | status = tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 159 | |
| 160 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 161 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | psa_status_t tfm_crypto_hash_verify(psa_invec in_vec[], |
| 165 | size_t in_len, |
| 166 | psa_outvec out_vec[], |
| 167 | size_t out_len) |
| 168 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 169 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 170 | return PSA_ERROR_NOT_SUPPORTED; |
| 171 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 172 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 173 | psa_hash_operation_t *operation = NULL; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 174 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 175 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 176 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 177 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 178 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 179 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 180 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 181 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 182 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 183 | uint32_t *handle_out = out_vec[0].base; |
| 184 | const uint8_t *hash = in_vec[1].base; |
| 185 | size_t hash_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 186 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 187 | /* Init the handle in the operation with the one passed from the iov */ |
| 188 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 189 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 190 | /* Look up the corresponding operation context */ |
| 191 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION, |
| 192 | handle, |
| 193 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 194 | if (status != PSA_SUCCESS) { |
| 195 | return status; |
| 196 | } |
| 197 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 198 | status = psa_hash_verify(operation, hash, hash_length); |
| 199 | if (status != PSA_SUCCESS) { |
| 200 | /* Release the operation context, ignore if the operation fails. */ |
| 201 | (void)tfm_crypto_operation_release(handle_out); |
| 202 | return status; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 205 | status = tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 206 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 207 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 208 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 211 | psa_status_t tfm_crypto_hash_abort(psa_invec in_vec[], |
| 212 | size_t in_len, |
| 213 | psa_outvec out_vec[], |
| 214 | size_t out_len) |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 215 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 216 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 217 | return PSA_ERROR_NOT_SUPPORTED; |
| 218 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 219 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 220 | psa_hash_operation_t *operation = NULL; |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 221 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 222 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 223 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 224 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 225 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 226 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 227 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 228 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 229 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 230 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 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 | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 234 | |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 235 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 236 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 237 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 238 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 239 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 240 | /* Operation does not exist, so abort has no effect */ |
| 241 | return PSA_SUCCESS; |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 244 | status = psa_hash_abort(operation); |
| 245 | if (status != PSA_SUCCESS) { |
| 246 | /* Release the operation context, ignore if the operation fails. */ |
| 247 | (void)tfm_crypto_operation_release(handle_out); |
| 248 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 249 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 250 | |
| 251 | status = tfm_crypto_operation_release(handle_out); |
| 252 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 253 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 254 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 255 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 256 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 257 | psa_status_t tfm_crypto_hash_clone(psa_invec in_vec[], |
| 258 | size_t in_len, |
| 259 | psa_outvec out_vec[], |
| 260 | size_t out_len) |
| 261 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 262 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 263 | return PSA_ERROR_NOT_SUPPORTED; |
| 264 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 265 | psa_status_t status = PSA_SUCCESS; |
| 266 | psa_hash_operation_t *source_operation = NULL; |
| 267 | psa_hash_operation_t *target_operation = NULL; |
| 268 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 269 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 270 | |
| 271 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 272 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 273 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 274 | } |
| 275 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 276 | uint32_t source_handle = iov->op_handle; |
| 277 | uint32_t *target_handle = out_vec[0].base; |
| 278 | |
| 279 | /* Look up the corresponding source operation context */ |
| 280 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_HASH_OPERATION, |
| 281 | source_handle, |
| 282 | (void **)&source_operation); |
| 283 | if (status != PSA_SUCCESS) { |
| 284 | return status; |
| 285 | } |
| 286 | |
| 287 | /* Allocate the target operation context in the secure world */ |
| 288 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_HASH_OPERATION, |
| 289 | target_handle, |
| 290 | (void **)&target_operation); |
| 291 | if (status != PSA_SUCCESS) { |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | status = psa_hash_clone(source_operation, target_operation); |
| 296 | if (status != PSA_SUCCESS) { |
| 297 | /* Release the target operation context, ignore if it fails. */ |
| 298 | (void)tfm_crypto_operation_release(target_handle); |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 303 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 304 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 305 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 306 | psa_status_t tfm_crypto_hash_compute(psa_invec in_vec[], |
| 307 | size_t in_len, |
| 308 | psa_outvec out_vec[], |
| 309 | size_t out_len) |
| 310 | { |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 311 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 312 | return PSA_ERROR_NOT_SUPPORTED; |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 313 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 314 | |
| 315 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1); |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 316 | |
| 317 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 318 | return PSA_ERROR_PROGRAMMER_ERROR; |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 322 | psa_algorithm_t alg = iov->alg; |
| 323 | const uint8_t *input = in_vec[1].base; |
| 324 | size_t input_length = in_vec[1].len; |
| 325 | uint8_t *hash = out_vec[0].base; |
| 326 | size_t hash_size = out_vec[0].len; |
| 327 | |
| 328 | /* Initialize hash_length to zero */ |
| 329 | out_vec[0].len = 0; |
| 330 | return psa_hash_compute(alg, input, input_length, hash, hash_size, |
| 331 | &out_vec[0].len); |
| 332 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
| 333 | } |
| 334 | |
| 335 | psa_status_t tfm_crypto_hash_compare(psa_invec in_vec[], |
| 336 | size_t in_len, |
| 337 | psa_outvec out_vec[], |
| 338 | size_t out_len) |
| 339 | { |
| 340 | #ifdef TFM_CRYPTO_HASH_MODULE_DISABLED |
| 341 | return PSA_ERROR_NOT_SUPPORTED; |
| 342 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 343 | |
| 344 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0); |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 345 | |
| 346 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 347 | return PSA_ERROR_PROGRAMMER_ERROR; |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 351 | psa_algorithm_t alg = iov->alg; |
| 352 | const uint8_t *input = in_vec[1].base; |
| 353 | size_t input_length = in_vec[1].len; |
| 354 | const uint8_t *hash = in_vec[2].base; |
| 355 | size_t hash_length = in_vec[2].len; |
| 356 | |
| 357 | return psa_hash_compare(alg, input, input_length, hash, hash_length); |
| 358 | #endif /* TFM_CRYPTO_HASH_MODULE_DISABLED */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 359 | } |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 360 | /*!@}*/ |