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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 47 | /* Init the handle in the operation with the one passed from the iov */ |
| 48 | *handle_out = iov->op_handle; |
| 49 | |
| 50 | /* Allocate the operation context in the secure world */ |
| 51 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION, |
| 52 | &handle, |
| 53 | (void **)&operation); |
| 54 | if (status != PSA_SUCCESS) { |
| 55 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 56 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 57 | |
| 58 | *handle_out = handle; |
| 59 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 60 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 61 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 62 | goto exit; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | status = psa_mac_sign_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 66 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 67 | goto exit; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 68 | } |
| 69 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 70 | return status; |
| 71 | |
| 72 | exit: |
| 73 | /* Release the operation context, ignore if the operation fails. */ |
| 74 | (void)tfm_crypto_operation_release(handle_out); |
| 75 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 76 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 79 | psa_status_t tfm_crypto_mac_verify_setup(psa_invec in_vec[], |
| 80 | size_t in_len, |
| 81 | psa_outvec out_vec[], |
| 82 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 83 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 84 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 85 | return PSA_ERROR_NOT_SUPPORTED; |
| 86 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 87 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 88 | psa_mac_operation_t *operation = NULL; |
| 89 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 90 | 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] | 91 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | if ((out_vec[0].len != sizeof(uint32_t)) || |
| 93 | (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 94 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 95 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 96 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 97 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 98 | uint32_t *handle_out = out_vec[0].base; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 99 | psa_key_id_t key_id = iov->key_id; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 100 | psa_algorithm_t alg = iov->alg; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 101 | mbedtls_svc_key_id_t encoded_key; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 102 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 103 | /* Init the handle in the operation with the one passed from the iov */ |
| 104 | *handle_out = iov->op_handle; |
| 105 | |
| 106 | /* Allocate the operation context in the secure world */ |
| 107 | status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION, |
| 108 | &handle, |
| 109 | (void **)&operation); |
| 110 | if (status != PSA_SUCCESS) { |
| 111 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 112 | } |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 113 | |
| 114 | *handle_out = handle; |
| 115 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 116 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 117 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 118 | goto exit; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | status = psa_mac_verify_setup(operation, encoded_key, alg); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 122 | if (status != PSA_SUCCESS) { |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 123 | goto exit; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 124 | } |
| 125 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 126 | return status; |
| 127 | |
| 128 | exit: |
| 129 | /* Release the operation context, ignore if the operation fails. */ |
| 130 | (void)tfm_crypto_operation_release(handle_out); |
| 131 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 132 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 135 | psa_status_t tfm_crypto_mac_update(psa_invec in_vec[], |
| 136 | size_t in_len, |
| 137 | psa_outvec out_vec[], |
| 138 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 139 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 140 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 141 | return PSA_ERROR_NOT_SUPPORTED; |
| 142 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 143 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 144 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 145 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 146 | 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] | 147 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 148 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 149 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 150 | return PSA_ERROR_PROGRAMMER_ERROR; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 151 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 152 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 153 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 154 | uint32_t *handle_out = out_vec[0].base; |
| 155 | const uint8_t *input = in_vec[1].base; |
| 156 | size_t input_length = in_vec[1].len; |
Louis 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 | /* 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] | 159 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 160 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 161 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 162 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 163 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 164 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 165 | if (status != PSA_SUCCESS) { |
| 166 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 167 | } |
| 168 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 169 | return psa_mac_update(operation, input, input_length); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 170 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 173 | psa_status_t tfm_crypto_mac_sign_finish(psa_invec in_vec[], |
| 174 | size_t in_len, |
| 175 | psa_outvec out_vec[], |
| 176 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 177 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 178 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 179 | return PSA_ERROR_NOT_SUPPORTED; |
| 180 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 181 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 182 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 183 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 184 | 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] | 185 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 186 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 187 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 188 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 189 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 190 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 191 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 192 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 193 | uint8_t *mac = out_vec[1].base; |
| 194 | size_t mac_size = out_vec[1].len; |
| 195 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 196 | /* 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] | 197 | *handle_out = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 198 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 199 | /* Initialise mac_length to zero */ |
| 200 | out_vec[1].len = 0; |
| 201 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 202 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 203 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 204 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 205 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 206 | if (status != PSA_SUCCESS) { |
| 207 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 210 | 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] | 211 | if (status == PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 212 | /* Release the operation context, ignore if the operation fails. */ |
| 213 | (void)tfm_crypto_operation_release(handle_out); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 216 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 217 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 220 | psa_status_t tfm_crypto_mac_verify_finish(psa_invec in_vec[], |
| 221 | size_t in_len, |
| 222 | psa_outvec out_vec[], |
| 223 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 224 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 225 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 226 | return PSA_ERROR_NOT_SUPPORTED; |
| 227 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 228 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 229 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 230 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 231 | 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] | 232 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 233 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 234 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 235 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 236 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 237 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 238 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 239 | uint32_t *handle_out = out_vec[0].base; |
| 240 | const uint8_t *mac = in_vec[1].base; |
| 241 | size_t mac_length = in_vec[1].len; |
Antonio de 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 | /* 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] | 244 | *handle_out = iov->op_handle; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 245 | |
| 246 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 247 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 248 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 249 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 250 | if (status != PSA_SUCCESS) { |
| 251 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 254 | status = psa_mac_verify_finish(operation, mac, mac_length); |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 255 | if (status == PSA_SUCCESS) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 256 | /* Release the operation context, ignore if the operation fails. */ |
| 257 | (void)tfm_crypto_operation_release(handle_out); |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 260 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 261 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 264 | psa_status_t tfm_crypto_mac_abort(psa_invec in_vec[], |
| 265 | size_t in_len, |
| 266 | psa_outvec out_vec[], |
| 267 | size_t out_len) |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 268 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 269 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 270 | return PSA_ERROR_NOT_SUPPORTED; |
| 271 | #else |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 272 | psa_status_t status = PSA_SUCCESS; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 273 | psa_mac_operation_t *operation = NULL; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 274 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 275 | 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] | 276 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 277 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) || |
| 278 | (out_vec[0].len != sizeof(uint32_t))) { |
Soby Mathew | c6e8936 | 2020-10-19 16:55:16 +0100 | [diff] [blame] | 279 | return PSA_ERROR_PROGRAMMER_ERROR; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 280 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 281 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 282 | uint32_t handle = iov->op_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 283 | uint32_t *handle_out = out_vec[0].base; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 284 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 285 | /* 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] | 286 | *handle_out = iov->op_handle; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 287 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 288 | /* Look up the corresponding operation context */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 289 | status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 290 | handle, |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 291 | (void **)&operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 292 | if (status != PSA_SUCCESS) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 293 | /* Operation does not exist, so abort has no effect */ |
| 294 | return PSA_SUCCESS; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 297 | status = psa_mac_abort(operation); |
| 298 | |
| 299 | if (status != PSA_SUCCESS) { |
| 300 | /* Release the operation context, ignore if the operation fails. */ |
| 301 | (void)tfm_crypto_operation_release(handle_out); |
| 302 | return status; |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 303 | } |
| 304 | |
David Hu | 7e2e523 | 2021-04-21 16:52:07 +0800 | [diff] [blame] | 305 | return tfm_crypto_operation_release(handle_out); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 306 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 307 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 308 | |
| 309 | psa_status_t tfm_crypto_mac_compute(psa_invec in_vec[], |
| 310 | size_t in_len, |
| 311 | psa_outvec out_vec[], |
| 312 | size_t out_len) |
| 313 | { |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 314 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 315 | return PSA_ERROR_NOT_SUPPORTED; |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 316 | #else |
| 317 | psa_status_t status = PSA_SUCCESS; |
| 318 | |
| 319 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 1, 1); |
| 320 | |
| 321 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
| 322 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 323 | } |
| 324 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 325 | psa_key_id_t key_id = iov->key_id; |
| 326 | psa_algorithm_t alg = iov->alg; |
| 327 | const uint8_t *input = in_vec[1].base; |
| 328 | size_t input_length = in_vec[1].len; |
| 329 | uint8_t *mac = out_vec[0].base; |
| 330 | size_t mac_size = out_vec[0].len; |
| 331 | mbedtls_svc_key_id_t encoded_key; |
| 332 | |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 333 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 334 | if (status != PSA_SUCCESS) { |
| 335 | return status; |
| 336 | } |
| 337 | |
| 338 | return psa_mac_compute(encoded_key, alg, input, input_length, mac, mac_size, |
| 339 | &out_vec[0].len); |
| 340 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | psa_status_t tfm_crypto_mac_verify(psa_invec in_vec[], |
| 344 | size_t in_len, |
| 345 | psa_outvec out_vec[], |
| 346 | size_t out_len) |
| 347 | { |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 348 | #ifdef TFM_CRYPTO_MAC_MODULE_DISABLED |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 349 | return PSA_ERROR_NOT_SUPPORTED; |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 350 | #else |
| 351 | psa_status_t status = PSA_SUCCESS; |
| 352 | |
| 353 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0); |
| 354 | |
| 355 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
| 356 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 357 | } |
| 358 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 359 | psa_key_id_t key_id = iov->key_id; |
| 360 | psa_algorithm_t alg = iov->alg; |
| 361 | const uint8_t *input = in_vec[1].base; |
| 362 | size_t input_length = in_vec[1].len; |
| 363 | const uint8_t *mac = in_vec[2].base; |
| 364 | size_t mac_length = in_vec[2].len; |
| 365 | mbedtls_svc_key_id_t encoded_key; |
| 366 | |
Summer Qin | 045ec4a | 2021-07-07 14:28:04 +0800 | [diff] [blame] | 367 | status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key); |
| 368 | if (status != PSA_SUCCESS) { |
| 369 | return status; |
| 370 | } |
| 371 | |
| 372 | return psa_mac_verify(encoded_key, alg, input, input_length, mac, |
| 373 | mac_length); |
| 374 | #endif /* TFM_CRYPTO_MAC_MODULE_DISABLED */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 375 | } |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 376 | /*!@}*/ |