Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA MAC layer on top of Mbed TLS software crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
Dave Rodgman | 8e322b1 | 2022-11-02 09:25:38 +0000 | [diff] [blame] | 27 | #include "psa_crypto_cipher.h" |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 28 | #include "psa_crypto_mac.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 29 | #include <mbedtls/md.h> |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <mbedtls/error.h> |
| 32 | #include <string.h> |
| 33 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 35 | static psa_status_t psa_hmac_abort_internal( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 36 | mbedtls_psa_hmac_operation_t *hmac) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 37 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 38 | mbedtls_platform_zeroize(hmac->opad, sizeof(hmac->opad)); |
| 39 | return psa_hash_abort(&hmac->hash_ctx); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 42 | static psa_status_t psa_hmac_setup_internal( |
| 43 | mbedtls_psa_hmac_operation_t *hmac, |
| 44 | const uint8_t *key, |
| 45 | size_t key_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 46 | psa_algorithm_t hash_alg) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 47 | { |
| 48 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
| 49 | size_t i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 50 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 51 | size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 52 | psa_status_t status; |
| 53 | |
| 54 | hmac->alg = hash_alg; |
| 55 | |
| 56 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 57 | * overflow below. This should never trigger if the hash algorithm |
| 58 | * is implemented correctly. */ |
| 59 | /* The size checks against the ipad and opad buffers cannot be written |
| 60 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 61 | * because that triggers -Wlogical-op on GCC 7.3. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 62 | if (block_size > sizeof(ipad)) { |
| 63 | return PSA_ERROR_NOT_SUPPORTED; |
| 64 | } |
| 65 | if (block_size > sizeof(hmac->opad)) { |
| 66 | return PSA_ERROR_NOT_SUPPORTED; |
| 67 | } |
| 68 | if (block_size < hash_size) { |
| 69 | return PSA_ERROR_NOT_SUPPORTED; |
| 70 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 71 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 72 | if (key_length > block_size) { |
| 73 | status = psa_hash_compute(hash_alg, key, key_length, |
| 74 | ipad, sizeof(ipad), &key_length); |
| 75 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 76 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 77 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 78 | } |
| 79 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 80 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 81 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 82 | * an invalid pointer which would make the behavior undefined. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 83 | else if (key_length != 0) { |
| 84 | memcpy(ipad, key, key_length); |
| 85 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 86 | |
| 87 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 88 | * to create the ipad value. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 89 | for (i = 0; i < key_length; i++) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 90 | ipad[i] ^= 0x36; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 91 | } |
| 92 | memset(ipad + key_length, 0x36, block_size - key_length); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 93 | |
| 94 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 95 | * and filling the rest of opad with the requisite constant. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 96 | for (i = 0; i < key_length; i++) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 97 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 98 | } |
| 99 | memset(hmac->opad + key_length, 0x5C, block_size - key_length); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 101 | status = psa_hash_setup(&hmac->hash_ctx, hash_alg); |
| 102 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 103 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 104 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 106 | status = psa_hash_update(&hmac->hash_ctx, ipad, block_size); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 107 | |
| 108 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 109 | mbedtls_platform_zeroize(ipad, sizeof(ipad)); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 111 | return status; |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 114 | static psa_status_t psa_hmac_update_internal( |
| 115 | mbedtls_psa_hmac_operation_t *hmac, |
| 116 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 117 | size_t data_length) |
Steven Cooreman | 76720f6 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 118 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 119 | return psa_hash_update(&hmac->hash_ctx, data, data_length); |
Steven Cooreman | 76720f6 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 122 | static psa_status_t psa_hmac_finish_internal( |
| 123 | mbedtls_psa_hmac_operation_t *hmac, |
| 124 | uint8_t *mac, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 125 | size_t mac_size) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 126 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 127 | uint8_t tmp[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 128 | psa_algorithm_t hash_alg = hmac->alg; |
| 129 | size_t hash_size = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 130 | size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 131 | psa_status_t status; |
| 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 133 | status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size); |
| 134 | if (status != PSA_SUCCESS) { |
| 135 | return status; |
| 136 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 137 | /* From here on, tmp needs to be wiped. */ |
| 138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 139 | status = psa_hash_setup(&hmac->hash_ctx, hash_alg); |
| 140 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 141 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 142 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 144 | status = psa_hash_update(&hmac->hash_ctx, hmac->opad, block_size); |
| 145 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 146 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 147 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 149 | status = psa_hash_update(&hmac->hash_ctx, tmp, hash_size); |
| 150 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 151 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 152 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 153 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 154 | status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size); |
| 155 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 156 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 157 | } |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 159 | memcpy(mac, tmp, mac_size); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 160 | |
| 161 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 162 | mbedtls_platform_zeroize(tmp, hash_size); |
| 163 | return status; |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 164 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 165 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 166 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 167 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 168 | static psa_status_t cmac_setup(mbedtls_psa_mac_operation_t *operation, |
| 169 | const psa_key_attributes_t *attributes, |
| 170 | const uint8_t *key_buffer) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 171 | { |
| 172 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 63fa40e | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 173 | |
| 174 | #if defined(PSA_WANT_KEY_TYPE_DES) |
| 175 | /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept |
| 176 | * to do CMAC with pure DES, so return NOT_SUPPORTED here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 177 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_DES && |
| 178 | (psa_get_key_bits(attributes) == 64 || |
| 179 | psa_get_key_bits(attributes) == 128)) { |
| 180 | return PSA_ERROR_NOT_SUPPORTED; |
| 181 | } |
Steven Cooreman | 63fa40e | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 182 | #endif |
| 183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 184 | const mbedtls_cipher_info_t *cipher_info = |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 185 | mbedtls_cipher_info_from_psa( |
| 186 | PSA_ALG_CMAC, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 187 | psa_get_key_type(attributes), |
| 188 | psa_get_key_bits(attributes), |
| 189 | NULL); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 191 | if (cipher_info == NULL) { |
| 192 | return PSA_ERROR_NOT_SUPPORTED; |
| 193 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 195 | ret = mbedtls_cipher_setup(&operation->ctx.cmac, cipher_info); |
| 196 | if (ret != 0) { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 197 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 198 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 200 | ret = mbedtls_cipher_cmac_starts(&operation->ctx.cmac, |
| 201 | key_buffer, |
| 202 | psa_get_key_bits(attributes)); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 203 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 204 | return mbedtls_to_psa_error(ret); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 205 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 206 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 207 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 208 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 209 | defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 210 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 211 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 212 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
| 213 | static psa_status_t mac_init( |
| 214 | mbedtls_psa_mac_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 215 | psa_algorithm_t alg) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 216 | { |
Steven Cooreman | 6e6451e | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 217 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 218 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 219 | operation->alg = alg; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 220 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 221 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 222 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 223 | mbedtls_cipher_init(&operation->ctx.cmac); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 224 | status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 225 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 226 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 227 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 228 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 229 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 230 | operation->ctx.hmac.alg = 0; |
| 231 | status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 232 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 233 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 234 | { |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 235 | (void) operation; |
Steven Cooreman | 6e6451e | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 236 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 239 | if (status != PSA_SUCCESS) { |
| 240 | memset(operation, 0, sizeof(*operation)); |
| 241 | } |
| 242 | return status; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 245 | psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 246 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 247 | if (operation->alg == 0) { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 248 | /* The object has (apparently) been initialized but it is not |
| 249 | * in use. It's ok to call abort on such an object, and there's |
| 250 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 251 | return PSA_SUCCESS; |
| 252 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 253 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 254 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 255 | mbedtls_cipher_free(&operation->ctx.cmac); |
| 256 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 257 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 258 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 259 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 260 | psa_hmac_abort_internal(&operation->ctx.hmac); |
| 261 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 262 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 263 | { |
| 264 | /* Sanity check (shouldn't happen: operation->alg should |
| 265 | * always have been initialized to a valid value). */ |
| 266 | goto bad_state; |
| 267 | } |
| 268 | |
| 269 | operation->alg = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 271 | return PSA_SUCCESS; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 272 | |
| 273 | bad_state: |
| 274 | /* If abort is called on an uninitialized object, we can't trust |
| 275 | * anything. Wipe the object in case it contains confidential data. |
| 276 | * This may result in a memory leak if a pointer gets overwritten, |
| 277 | * but it's too late to do anything about this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 278 | memset(operation, 0, sizeof(*operation)); |
| 279 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 282 | static psa_status_t psa_mac_setup(mbedtls_psa_mac_operation_t *operation, |
| 283 | const psa_key_attributes_t *attributes, |
| 284 | const uint8_t *key_buffer, |
| 285 | size_t key_buffer_size, |
| 286 | psa_algorithm_t alg) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 287 | { |
| 288 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 289 | |
| 290 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 291 | if (operation->alg != 0) { |
| 292 | return PSA_ERROR_BAD_STATE; |
| 293 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 295 | status = mac_init(operation, alg); |
| 296 | if (status != PSA_SUCCESS) { |
| 297 | return status; |
| 298 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 299 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 300 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 301 | if (PSA_ALG_FULL_LENGTH_MAC(alg) == PSA_ALG_CMAC) { |
Steven Cooreman | af81a71 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 302 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 303 | * attributes, and previously validated by the core on key import. */ |
| 304 | (void) key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 305 | status = cmac_setup(operation, attributes, key_buffer); |
| 306 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 307 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 308 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 309 | if (PSA_ALG_IS_HMAC(alg)) { |
| 310 | status = psa_hmac_setup_internal(&operation->ctx.hmac, |
| 311 | key_buffer, |
| 312 | key_buffer_size, |
| 313 | PSA_ALG_HMAC_GET_HASH(alg)); |
| 314 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 315 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 316 | { |
Steven Cooreman | 9621f44 | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 317 | (void) attributes; |
| 318 | (void) key_buffer; |
| 319 | (void) key_buffer_size; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 320 | status = PSA_ERROR_NOT_SUPPORTED; |
| 321 | } |
| 322 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 323 | if (status != PSA_SUCCESS) { |
| 324 | mbedtls_psa_mac_abort(operation); |
| 325 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 326 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 327 | return status; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 330 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 331 | mbedtls_psa_mac_operation_t *operation, |
| 332 | const psa_key_attributes_t *attributes, |
| 333 | const uint8_t *key_buffer, |
| 334 | size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 335 | psa_algorithm_t alg) |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 336 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 337 | return psa_mac_setup(operation, attributes, |
| 338 | key_buffer, key_buffer_size, alg); |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 342 | mbedtls_psa_mac_operation_t *operation, |
| 343 | const psa_key_attributes_t *attributes, |
| 344 | const uint8_t *key_buffer, |
| 345 | size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 346 | psa_algorithm_t alg) |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 347 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 348 | return psa_mac_setup(operation, attributes, |
| 349 | key_buffer, key_buffer_size, alg); |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | psa_status_t mbedtls_psa_mac_update( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 353 | mbedtls_psa_mac_operation_t *operation, |
| 354 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 355 | size_t input_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 356 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 357 | if (operation->alg == 0) { |
| 358 | return PSA_ERROR_BAD_STATE; |
| 359 | } |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 360 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 361 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 362 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 363 | return mbedtls_to_psa_error( |
| 364 | mbedtls_cipher_cmac_update(&operation->ctx.cmac, |
| 365 | input, input_length)); |
| 366 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 367 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 368 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 369 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 370 | return psa_hmac_update_internal(&operation->ctx.hmac, |
| 371 | input, input_length); |
| 372 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 373 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 374 | { |
| 375 | /* This shouldn't happen if `operation` was initialized by |
| 376 | * a setup function. */ |
Steven Cooreman | 9621f44 | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 377 | (void) input; |
| 378 | (void) input_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 379 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 380 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 383 | static psa_status_t psa_mac_finish_internal( |
| 384 | mbedtls_psa_mac_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 385 | uint8_t *mac, size_t mac_size) |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 386 | { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 387 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 388 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 389 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 390 | int ret = mbedtls_cipher_cmac_finish(&operation->ctx.cmac, tmp); |
| 391 | if (ret == 0) { |
| 392 | memcpy(mac, tmp, mac_size); |
| 393 | } |
| 394 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 395 | return mbedtls_to_psa_error(ret); |
| 396 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 399 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 400 | return psa_hmac_finish_internal(&operation->ctx.hmac, |
| 401 | mac, mac_size); |
| 402 | } else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 403 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 404 | { |
| 405 | /* This shouldn't happen if `operation` was initialized by |
| 406 | * a setup function. */ |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 407 | (void) operation; |
| 408 | (void) mac; |
| 409 | (void) mac_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 410 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 414 | psa_status_t mbedtls_psa_mac_sign_finish( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 415 | mbedtls_psa_mac_operation_t *operation, |
| 416 | uint8_t *mac, |
| 417 | size_t mac_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 418 | size_t *mac_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 419 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 420 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 421 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 422 | if (operation->alg == 0) { |
| 423 | return PSA_ERROR_BAD_STATE; |
| 424 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 426 | status = psa_mac_finish_internal(operation, mac, mac_size); |
| 427 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 428 | *mac_length = mac_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 429 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 431 | return status; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 434 | psa_status_t mbedtls_psa_mac_verify_finish( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 435 | mbedtls_psa_mac_operation_t *operation, |
| 436 | const uint8_t *mac, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 437 | size_t mac_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 438 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 439 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 440 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 441 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 442 | if (operation->alg == 0) { |
| 443 | return PSA_ERROR_BAD_STATE; |
| 444 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 445 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 446 | /* Consistency check: requested MAC length fits our local buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 447 | if (mac_length > sizeof(actual_mac)) { |
| 448 | return PSA_ERROR_INVALID_ARGUMENT; |
| 449 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 450 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 451 | status = psa_mac_finish_internal(operation, actual_mac, mac_length); |
| 452 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 453 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 454 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 456 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, mac_length) != 0) { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 457 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 458 | } |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 459 | |
| 460 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 461 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 463 | return status; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 464 | } |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 465 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 466 | psa_status_t mbedtls_psa_mac_compute( |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 467 | const psa_key_attributes_t *attributes, |
| 468 | const uint8_t *key_buffer, |
| 469 | size_t key_buffer_size, |
| 470 | psa_algorithm_t alg, |
| 471 | const uint8_t *input, |
| 472 | size_t input_length, |
| 473 | uint8_t *mac, |
| 474 | size_t mac_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 475 | size_t *mac_length) |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 476 | { |
| 477 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 478 | mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT; |
| 479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 480 | status = psa_mac_setup(&operation, |
| 481 | attributes, key_buffer, key_buffer_size, |
| 482 | alg); |
| 483 | if (status != PSA_SUCCESS) { |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 484 | goto exit; |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 487 | if (input_length > 0) { |
| 488 | status = mbedtls_psa_mac_update(&operation, input, input_length); |
| 489 | if (status != PSA_SUCCESS) { |
| 490 | goto exit; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | status = psa_mac_finish_internal(&operation, mac, mac_size); |
| 495 | if (status == PSA_SUCCESS) { |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 496 | *mac_length = mac_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 497 | } |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 498 | |
| 499 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 500 | mbedtls_psa_mac_abort(&operation); |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 501 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 502 | return status; |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 505 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 506 | |
| 507 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |