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