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