Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +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> |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +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" |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 16 | |
| 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_mac_sign_setup(psa_invec in_vec[], |
| 24 | size_t in_len, |
| 25 | psa_outvec out_vec[], |
| 26 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 27 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 28 | #ifdef TFM_CRYPTO_MAC_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 | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 31 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 32 | psa_mac_operation_t *operation = NULL; |
| 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 | ab85ccd | 2019-03-25 15:14:29 +0000 | [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; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 43 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 44 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 45 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 46 | |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 47 | status = tfm_crypto_check_handle_owner(key_id); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 48 | if (status != PSA_SUCCESS) { |
| 49 | return status; |
| 50 | } |
| 51 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 52 | /* 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 61 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 62 | |
| 63 | *handle_out = handle; |
| 64 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 65 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 66 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 67 | goto exit; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | status = psa_mac_sign_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 71 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 72 | goto exit; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 73 | } |
| 74 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 75 | return status; |
| 76 | |
| 77 | exit: |
| 78 | /* Release the operation context, ignore if the operation fails. */ |
| 79 | (void)tfm_crypto_operation_release(handle_out); |
| 80 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 81 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 84 | psa_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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 88 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 89 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 90 | return PSA_ERROR_NOT_SUPPORTED; |
| 91 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 93 | psa_mac_operation_t *operation = NULL; |
| 94 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 95 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 96 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 97 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 98 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 99 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 100 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 101 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 102 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 103 | uint32_t *handle_out = out_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 104 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 105 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 106 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 107 | |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 108 | status = tfm_crypto_check_handle_owner(key_id); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 109 | if (status != PSA_SUCCESS) { |
| 110 | return status; |
| 111 | } |
| 112 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 113 | /* 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 Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 122 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 123 | |
| 124 | *handle_out = handle; |
| 125 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 126 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 127 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 128 | goto exit; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | status = psa_mac_verify_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 132 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 133 | goto exit; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 134 | } |
| 135 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 136 | return status; |
| 137 | |
| 138 | exit: |
| 139 | /* Release the operation context, ignore if the operation fails. */ |
| 140 | (void)tfm_crypto_operation_release(handle_out); |
| 141 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 142 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 145 | psa_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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 149 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 150 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 151 | return PSA_ERROR_NOT_SUPPORTED; |
| 152 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 153 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 154 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 155 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 156 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 157 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 158 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 159 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 160 | return PSA_ERROR_PROGRAMMER_ERROR; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 161 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 162 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 163 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 164 | 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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 167 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 168 | /* 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] | 169 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 170 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 171 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 172 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 173 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 174 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 175 | if (status != PSA_SUCCESS) { |
| 176 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 177 | } |
| 178 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 179 | return psa_mac_update(operation, input, input_length); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 180 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 183 | psa_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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 187 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 188 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 189 | return PSA_ERROR_NOT_SUPPORTED; |
| 190 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 191 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 192 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 193 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 194 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 2); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 195 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 196 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 197 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 198 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 199 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 200 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 201 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 202 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 203 | uint8_t *mac = out_vec[1].base; |
| 204 | size_t mac_size = out_vec[1].len; |
| 205 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 206 | /* 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] | 207 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 208 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 209 | /* Initialise mac_length to zero */ |
| 210 | out_vec[1].len = 0; |
| 211 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 212 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 213 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 214 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 215 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 216 | if (status != PSA_SUCCESS) { |
| 217 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 220 | status = psa_mac_sign_finish(operation, mac, mac_size, &out_vec[1].len); |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 221 | if (status == PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 222 | /* Release the operation context, ignore if the operation fails. */ |
| 223 | (void)tfm_crypto_operation_release(handle_out); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 226 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 227 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 230 | psa_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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 234 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 235 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 236 | return PSA_ERROR_NOT_SUPPORTED; |
| 237 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 238 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 239 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 240 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 241 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 242 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 243 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 244 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 245 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 246 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 247 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 248 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 249 | 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 Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 252 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 253 | /* 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] | 254 | *handle_out = iov->op_handle; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 255 | |
| 256 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 257 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 258 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 259 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 260 | if (status != PSA_SUCCESS) { |
| 261 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 264 | status = psa_mac_verify_finish(operation, mac, mac_length); |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 265 | if (status == PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 266 | /* Release the operation context, ignore if the operation fails. */ |
| 267 | (void)tfm_crypto_operation_release(handle_out); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 270 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 271 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 274 | psa_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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 278 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 279 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 280 | return PSA_ERROR_NOT_SUPPORTED; |
| 281 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 282 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 283 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 284 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 285 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 286 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 287 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 288 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 289 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 290 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 291 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 292 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 293 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 294 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 295 | /* 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] | 296 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 297 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 298 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 299 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 300 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 301 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 302 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 303 | /* Operation does not exist, so abort has no effect */ |
| 304 | return PSA_SUCCESS; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 307 | 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 Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 313 | } |
| 314 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 315 | return tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 316 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 317 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 318 | |
| 319 | psa_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 Angelis | 8f4db96 | 2021-07-05 13:58:43 +0200 | [diff] [blame] | 324 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 325 | return PSA_ERROR_NOT_SUPPORTED; |
Antonio de Angelis | 8f4db96 | 2021-07-05 13:58:43 +0200 | [diff] [blame] | 326 | #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 Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | psa_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 Angelis | 8f4db96 | 2021-07-05 13:58:43 +0200 | [diff] [blame] | 360 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 361 | return PSA_ERROR_NOT_SUPPORTED; |
Antonio de Angelis | 8f4db96 | 2021-07-05 13:58:43 +0200 | [diff] [blame] | 362 | #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 Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 390 | } |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 391 | /*!@}*/ |