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" |
| 27 | #include "psa_crypto_mac.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 28 | #include <mbedtls/md.h> |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include <mbedtls/error.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | /* Use builtin defines specific to this compilation unit, since the test driver |
| 34 | * relies on the software driver. */ |
| 35 | #if( defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \ |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 36 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 37 | defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \ |
| 38 | defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) ) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 39 | #define BUILTIN_ALG_CMAC 1 |
| 40 | #endif |
| 41 | #if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 42 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 43 | defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \ |
| 44 | defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) ) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 45 | #define BUILTIN_ALG_HMAC 1 |
| 46 | #endif |
| 47 | |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 48 | #if defined(BUILTIN_ALG_HMAC) |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 49 | static psa_status_t psa_hmac_abort_internal( |
| 50 | mbedtls_psa_hmac_operation_t *hmac ) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 51 | { |
| 52 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
| 53 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 54 | } |
| 55 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 56 | static psa_status_t psa_hmac_setup_internal( |
| 57 | mbedtls_psa_hmac_operation_t *hmac, |
| 58 | const uint8_t *key, |
| 59 | size_t key_length, |
| 60 | psa_algorithm_t hash_alg ) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 61 | { |
| 62 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
| 63 | size_t i; |
| 64 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | 285f213 | 2021-09-22 14:44:28 +0200 | [diff] [blame] | 65 | size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg ); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 66 | psa_status_t status; |
| 67 | |
| 68 | hmac->alg = hash_alg; |
| 69 | |
| 70 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 71 | * overflow below. This should never trigger if the hash algorithm |
| 72 | * is implemented correctly. */ |
| 73 | /* The size checks against the ipad and opad buffers cannot be written |
| 74 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 75 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 76 | if( block_size > sizeof( ipad ) ) |
| 77 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 78 | if( block_size > sizeof( hmac->opad ) ) |
| 79 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 80 | if( block_size < hash_size ) |
| 81 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 82 | |
| 83 | if( key_length > block_size ) |
| 84 | { |
| 85 | status = psa_hash_compute( hash_alg, key, key_length, |
| 86 | ipad, sizeof( ipad ), &key_length ); |
| 87 | if( status != PSA_SUCCESS ) |
| 88 | goto cleanup; |
| 89 | } |
| 90 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 91 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 92 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 93 | * an invalid pointer which would make the behavior undefined. */ |
| 94 | else if( key_length != 0 ) |
| 95 | memcpy( ipad, key, key_length ); |
| 96 | |
| 97 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 98 | * to create the ipad value. */ |
| 99 | for( i = 0; i < key_length; i++ ) |
| 100 | ipad[i] ^= 0x36; |
| 101 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 102 | |
| 103 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 104 | * and filling the rest of opad with the requisite constant. */ |
| 105 | for( i = 0; i < key_length; i++ ) |
| 106 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 107 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
| 108 | |
| 109 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 110 | if( status != PSA_SUCCESS ) |
| 111 | goto cleanup; |
| 112 | |
| 113 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
| 114 | |
| 115 | cleanup: |
| 116 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
| 117 | |
| 118 | return( status ); |
| 119 | } |
| 120 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 121 | static psa_status_t psa_hmac_update_internal( |
| 122 | mbedtls_psa_hmac_operation_t *hmac, |
| 123 | const uint8_t *data, |
| 124 | size_t data_length ) |
Steven Cooreman | 76720f6 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 125 | { |
| 126 | return( psa_hash_update( &hmac->hash_ctx, data, data_length ) ); |
| 127 | } |
| 128 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 129 | static psa_status_t psa_hmac_finish_internal( |
| 130 | mbedtls_psa_hmac_operation_t *hmac, |
| 131 | uint8_t *mac, |
| 132 | size_t mac_size ) |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 133 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 134 | uint8_t tmp[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 135 | psa_algorithm_t hash_alg = hmac->alg; |
| 136 | size_t hash_size = 0; |
Gilles Peskine | 285f213 | 2021-09-22 14:44:28 +0200 | [diff] [blame] | 137 | size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg ); |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 138 | psa_status_t status; |
| 139 | |
| 140 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 141 | if( status != PSA_SUCCESS ) |
| 142 | return( status ); |
| 143 | /* From here on, tmp needs to be wiped. */ |
| 144 | |
| 145 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 146 | if( status != PSA_SUCCESS ) |
| 147 | goto exit; |
| 148 | |
| 149 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 150 | if( status != PSA_SUCCESS ) |
| 151 | goto exit; |
| 152 | |
| 153 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 154 | if( status != PSA_SUCCESS ) |
| 155 | goto exit; |
| 156 | |
| 157 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 158 | if( status != PSA_SUCCESS ) |
| 159 | goto exit; |
| 160 | |
| 161 | memcpy( mac, tmp, mac_size ); |
| 162 | |
| 163 | exit: |
| 164 | mbedtls_platform_zeroize( tmp, hash_size ); |
| 165 | return( status ); |
| 166 | } |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 167 | #endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 168 | |
| 169 | #if defined(BUILTIN_ALG_CMAC) |
| 170 | static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation, |
| 171 | const psa_key_attributes_t *attributes, |
Steven Cooreman | af81a71 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 172 | const uint8_t *key_buffer ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 173 | { |
| 174 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 63fa40e | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 175 | |
| 176 | #if defined(PSA_WANT_KEY_TYPE_DES) |
| 177 | /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept |
| 178 | * to do CMAC with pure DES, so return NOT_SUPPORTED here. */ |
| 179 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES && |
| 180 | ( psa_get_key_bits( attributes ) == 64 || |
| 181 | psa_get_key_bits( attributes ) == 128 ) ) |
| 182 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 183 | #endif |
| 184 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 185 | const mbedtls_cipher_info_t * cipher_info = |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 186 | mbedtls_cipher_info_from_psa( |
| 187 | PSA_ALG_CMAC, |
| 188 | psa_get_key_type( attributes ), |
| 189 | psa_get_key_bits( attributes ), |
| 190 | NULL ); |
| 191 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 192 | if( cipher_info == NULL ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 193 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 194 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 195 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 196 | if( ret != 0 ) |
| 197 | goto exit; |
| 198 | |
| 199 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 200 | key_buffer, |
| 201 | psa_get_key_bits( attributes ) ); |
| 202 | exit: |
| 203 | return( mbedtls_to_psa_error( ret ) ); |
| 204 | } |
| 205 | #endif /* BUILTIN_ALG_CMAC */ |
| 206 | |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 207 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 208 | * software driver or the test driver requires it. */ |
| 209 | #if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC) |
| 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, |
| 215 | psa_algorithm_t alg ) |
| 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 | |
| 221 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 222 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 223 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 224 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 225 | status = PSA_SUCCESS; |
| 226 | } |
| 227 | else |
| 228 | #endif /* BUILTIN_ALG_CMAC */ |
| 229 | #if defined(BUILTIN_ALG_HMAC) |
| 230 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 231 | { |
| 232 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 233 | operation->ctx.hmac.alg = 0; |
| 234 | status = PSA_SUCCESS; |
| 235 | } |
| 236 | else |
| 237 | #endif /* BUILTIN_ALG_HMAC */ |
| 238 | { |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame^] | 239 | (void) operation; |
Steven Cooreman | 6e6451e | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 240 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if( status != PSA_SUCCESS ) |
| 244 | memset( operation, 0, sizeof( *operation ) ); |
| 245 | return( status ); |
| 246 | } |
| 247 | |
| 248 | static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation ) |
| 249 | { |
| 250 | if( operation->alg == 0 ) |
| 251 | { |
| 252 | /* The object has (apparently) been initialized but it is not |
| 253 | * in use. It's ok to call abort on such an object, and there's |
| 254 | * nothing to do. */ |
| 255 | return( PSA_SUCCESS ); |
| 256 | } |
| 257 | else |
| 258 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 259 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 260 | { |
| 261 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 262 | } |
| 263 | else |
| 264 | #endif /* BUILTIN_ALG_CMAC */ |
| 265 | #if defined(BUILTIN_ALG_HMAC) |
| 266 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 267 | { |
| 268 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
| 269 | } |
| 270 | else |
| 271 | #endif /* BUILTIN_ALG_HMAC */ |
| 272 | { |
| 273 | /* Sanity check (shouldn't happen: operation->alg should |
| 274 | * always have been initialized to a valid value). */ |
| 275 | goto bad_state; |
| 276 | } |
| 277 | |
| 278 | operation->alg = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 279 | |
| 280 | return( PSA_SUCCESS ); |
| 281 | |
| 282 | bad_state: |
| 283 | /* If abort is called on an uninitialized object, we can't trust |
| 284 | * anything. Wipe the object in case it contains confidential data. |
| 285 | * This may result in a memory leak if a pointer gets overwritten, |
| 286 | * but it's too late to do anything about this. */ |
| 287 | memset( operation, 0, sizeof( *operation ) ); |
| 288 | return( PSA_ERROR_BAD_STATE ); |
| 289 | } |
| 290 | |
| 291 | static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation, |
| 292 | const psa_key_attributes_t *attributes, |
| 293 | const uint8_t *key_buffer, |
| 294 | size_t key_buffer_size, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 295 | psa_algorithm_t alg ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 296 | { |
| 297 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 298 | |
| 299 | /* A context must be freshly initialized before it can be set up. */ |
| 300 | if( operation->alg != 0 ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 301 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 302 | |
| 303 | status = mac_init( operation, alg ); |
| 304 | if( status != PSA_SUCCESS ) |
| 305 | return( status ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 306 | |
| 307 | #if defined(BUILTIN_ALG_CMAC) |
| 308 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
| 309 | { |
Steven Cooreman | af81a71 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 310 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 311 | * attributes, and previously validated by the core on key import. */ |
| 312 | (void) key_buffer_size; |
| 313 | status = cmac_setup( operation, attributes, key_buffer ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | #endif /* BUILTIN_ALG_CMAC */ |
| 317 | #if defined(BUILTIN_ALG_HMAC) |
| 318 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 319 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 320 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 321 | key_buffer, |
| 322 | key_buffer_size, |
| 323 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
| 324 | } |
| 325 | else |
| 326 | #endif /* BUILTIN_ALG_HMAC */ |
| 327 | { |
Steven Cooreman | 9621f44 | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 328 | (void) attributes; |
| 329 | (void) key_buffer; |
| 330 | (void) key_buffer_size; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 331 | status = PSA_ERROR_NOT_SUPPORTED; |
| 332 | } |
| 333 | |
Steven Cooreman | b4b9b28 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 334 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 335 | mac_abort( operation ); |
| 336 | |
| 337 | return( status ); |
| 338 | } |
| 339 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 340 | static psa_status_t mac_update( |
| 341 | mbedtls_psa_mac_operation_t *operation, |
| 342 | const uint8_t *input, |
| 343 | size_t input_length ) |
| 344 | { |
Steven Cooreman | b4b9b28 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 345 | if( operation->alg == 0 ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 346 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 347 | |
| 348 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 349 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 350 | { |
| 351 | return( mbedtls_to_psa_error( |
| 352 | mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 353 | input, input_length ) ) ); |
| 354 | } |
| 355 | else |
| 356 | #endif /* BUILTIN_ALG_CMAC */ |
| 357 | #if defined(BUILTIN_ALG_HMAC) |
| 358 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 359 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 360 | return( psa_hmac_update_internal( &operation->ctx.hmac, |
| 361 | input, input_length ) ); |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 362 | } |
| 363 | else |
| 364 | #endif /* BUILTIN_ALG_HMAC */ |
| 365 | { |
| 366 | /* This shouldn't happen if `operation` was initialized by |
| 367 | * a setup function. */ |
Steven Cooreman | 9621f44 | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 368 | (void) input; |
| 369 | (void) input_length; |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 370 | return( PSA_ERROR_BAD_STATE ); |
| 371 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 374 | static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation, |
| 375 | uint8_t *mac, |
| 376 | size_t mac_size ) |
| 377 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 378 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 379 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 380 | { |
| 381 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
| 382 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 383 | if( ret == 0 ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 384 | memcpy( mac, tmp, mac_size ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 385 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 386 | return( mbedtls_to_psa_error( ret ) ); |
| 387 | } |
| 388 | else |
| 389 | #endif /* BUILTIN_ALG_CMAC */ |
| 390 | #if defined(BUILTIN_ALG_HMAC) |
| 391 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 392 | { |
| 393 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 394 | mac, mac_size ) ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 395 | } |
| 396 | else |
| 397 | #endif /* BUILTIN_ALG_HMAC */ |
| 398 | { |
| 399 | /* This shouldn't happen if `operation` was initialized by |
| 400 | * a setup function. */ |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 401 | (void) operation; |
| 402 | (void) mac; |
| 403 | (void) mac_size; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 404 | return( PSA_ERROR_BAD_STATE ); |
| 405 | } |
| 406 | } |
| 407 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 408 | static psa_status_t mac_sign_finish( |
| 409 | mbedtls_psa_mac_operation_t *operation, |
| 410 | uint8_t *mac, |
| 411 | size_t mac_size, |
| 412 | size_t *mac_length ) |
| 413 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 415 | |
| 416 | if( operation->alg == 0 ) |
| 417 | return( PSA_ERROR_BAD_STATE ); |
| 418 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 419 | status = mac_finish_internal( operation, mac, mac_size ); |
| 420 | |
| 421 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 422 | *mac_length = mac_size; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 423 | |
| 424 | return( status ); |
| 425 | } |
| 426 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 427 | static psa_status_t mac_verify_finish( |
| 428 | mbedtls_psa_mac_operation_t *operation, |
| 429 | const uint8_t *mac, |
| 430 | size_t mac_length ) |
| 431 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 432 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 433 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 434 | |
| 435 | if( operation->alg == 0 ) |
| 436 | return( PSA_ERROR_BAD_STATE ); |
| 437 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 438 | /* Consistency check: requested MAC length fits our local buffer */ |
| 439 | if( mac_length > sizeof( actual_mac ) ) |
| 440 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 441 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 442 | status = mac_finish_internal( operation, actual_mac, mac_length ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 443 | if( status != PSA_SUCCESS ) |
| 444 | goto cleanup; |
| 445 | |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 446 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 447 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 448 | |
| 449 | cleanup: |
| 450 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
| 451 | |
| 452 | return( status ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 453 | } |
Ronald Cron | bfdfaa6 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 454 | |
| 455 | static psa_status_t mac_compute( |
| 456 | const psa_key_attributes_t *attributes, |
| 457 | const uint8_t *key_buffer, |
| 458 | size_t key_buffer_size, |
| 459 | psa_algorithm_t alg, |
| 460 | const uint8_t *input, |
| 461 | size_t input_length, |
| 462 | uint8_t *mac, |
| 463 | size_t mac_size, |
| 464 | size_t *mac_length ) |
| 465 | { |
| 466 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 467 | mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT; |
| 468 | |
| 469 | status = mac_setup( &operation, |
| 470 | attributes, key_buffer, key_buffer_size, |
| 471 | alg ); |
| 472 | if( status != PSA_SUCCESS ) |
| 473 | goto exit; |
| 474 | |
| 475 | if( input_length > 0 ) |
| 476 | { |
| 477 | status = mac_update( &operation, input, input_length ); |
| 478 | if( status != PSA_SUCCESS ) |
| 479 | goto exit; |
| 480 | } |
| 481 | |
| 482 | status = mac_finish_internal( &operation, mac, mac_size ); |
| 483 | if( status == PSA_SUCCESS ) |
| 484 | *mac_length = mac_size; |
| 485 | |
| 486 | exit: |
| 487 | mac_abort( &operation ); |
| 488 | |
| 489 | return( status ); |
| 490 | } |
| 491 | |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 492 | #endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 493 | |
| 494 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 495 | psa_status_t mbedtls_psa_mac_compute( |
| 496 | const psa_key_attributes_t *attributes, |
| 497 | const uint8_t *key_buffer, |
| 498 | size_t key_buffer_size, |
| 499 | psa_algorithm_t alg, |
| 500 | const uint8_t *input, |
| 501 | size_t input_length, |
| 502 | uint8_t *mac, |
| 503 | size_t mac_size, |
| 504 | size_t *mac_length ) |
| 505 | { |
| 506 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 507 | input, input_length, |
| 508 | mac, mac_size, mac_length ) ); |
| 509 | } |
| 510 | |
| 511 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 512 | mbedtls_psa_mac_operation_t *operation, |
| 513 | const psa_key_attributes_t *attributes, |
| 514 | const uint8_t *key_buffer, |
| 515 | size_t key_buffer_size, |
| 516 | psa_algorithm_t alg ) |
| 517 | { |
Steven Cooreman | bbb1952 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 518 | return( mac_setup( operation, attributes, |
| 519 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 523 | mbedtls_psa_mac_operation_t *operation, |
| 524 | const psa_key_attributes_t *attributes, |
| 525 | const uint8_t *key_buffer, |
| 526 | size_t key_buffer_size, |
| 527 | psa_algorithm_t alg ) |
| 528 | { |
Steven Cooreman | bbb1952 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 529 | return( mac_setup( operation, attributes, |
| 530 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | psa_status_t mbedtls_psa_mac_update( |
| 534 | mbedtls_psa_mac_operation_t *operation, |
| 535 | const uint8_t *input, |
| 536 | size_t input_length ) |
| 537 | { |
| 538 | return( mac_update( operation, input, input_length ) ); |
| 539 | } |
| 540 | |
| 541 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 542 | mbedtls_psa_mac_operation_t *operation, |
| 543 | uint8_t *mac, |
| 544 | size_t mac_size, |
| 545 | size_t *mac_length ) |
| 546 | { |
| 547 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 548 | } |
| 549 | |
| 550 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 551 | mbedtls_psa_mac_operation_t *operation, |
| 552 | const uint8_t *mac, |
| 553 | size_t mac_length ) |
| 554 | { |
| 555 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 556 | } |
| 557 | |
| 558 | psa_status_t mbedtls_psa_mac_abort( |
| 559 | mbedtls_psa_mac_operation_t *operation ) |
| 560 | { |
| 561 | return( mac_abort( operation ) ); |
| 562 | } |
| 563 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 564 | |
| 565 | /* |
| 566 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 567 | */ |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 568 | #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 569 | |
| 570 | static int is_mac_accelerated( psa_algorithm_t alg ) |
| 571 | { |
| 572 | #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 573 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 574 | return( 1 ); |
| 575 | #endif |
| 576 | |
| 577 | switch( PSA_ALG_FULL_LENGTH_MAC( alg ) ) |
| 578 | { |
| 579 | #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
| 580 | case PSA_ALG_CMAC: |
| 581 | return( 1 ); |
| 582 | #endif |
| 583 | default: |
| 584 | return( 0 ); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 589 | const psa_key_attributes_t *attributes, |
| 590 | const uint8_t *key_buffer, |
| 591 | size_t key_buffer_size, |
| 592 | psa_algorithm_t alg, |
| 593 | const uint8_t *input, |
| 594 | size_t input_length, |
| 595 | uint8_t *mac, |
| 596 | size_t mac_size, |
| 597 | size_t *mac_length ) |
| 598 | { |
| 599 | if( is_mac_accelerated( alg ) ) |
| 600 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 601 | input, input_length, |
| 602 | mac, mac_size, mac_length ) ); |
| 603 | else |
| 604 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 605 | } |
| 606 | |
| 607 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 608 | mbedtls_psa_mac_operation_t *operation, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 609 | const psa_key_attributes_t *attributes, |
| 610 | const uint8_t *key_buffer, |
| 611 | size_t key_buffer_size, |
| 612 | psa_algorithm_t alg ) |
| 613 | { |
| 614 | if( is_mac_accelerated( alg ) ) |
Steven Cooreman | bbb1952 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 615 | return( mac_setup( operation, attributes, |
| 616 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 617 | else |
| 618 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 619 | } |
| 620 | |
| 621 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 622 | mbedtls_psa_mac_operation_t *operation, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 623 | const psa_key_attributes_t *attributes, |
| 624 | const uint8_t *key_buffer, |
| 625 | size_t key_buffer_size, |
| 626 | psa_algorithm_t alg ) |
| 627 | { |
| 628 | if( is_mac_accelerated( alg ) ) |
Steven Cooreman | bbb1952 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 629 | return( mac_setup( operation, attributes, |
| 630 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 631 | else |
| 632 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 633 | } |
| 634 | |
| 635 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 636 | mbedtls_psa_mac_operation_t *operation, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 637 | const uint8_t *input, |
| 638 | size_t input_length ) |
| 639 | { |
| 640 | if( is_mac_accelerated( operation->alg ) ) |
| 641 | return( mac_update( operation, input, input_length ) ); |
| 642 | else |
| 643 | return( PSA_ERROR_BAD_STATE ); |
| 644 | } |
| 645 | |
| 646 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 647 | mbedtls_psa_mac_operation_t *operation, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 648 | uint8_t *mac, |
| 649 | size_t mac_size, |
| 650 | size_t *mac_length ) |
| 651 | { |
| 652 | if( is_mac_accelerated( operation->alg ) ) |
| 653 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 654 | else |
| 655 | return( PSA_ERROR_BAD_STATE ); |
| 656 | } |
| 657 | |
| 658 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 659 | mbedtls_psa_mac_operation_t *operation, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 660 | const uint8_t *mac, |
| 661 | size_t mac_length ) |
| 662 | { |
| 663 | if( is_mac_accelerated( operation->alg ) ) |
| 664 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 665 | else |
| 666 | return( PSA_ERROR_BAD_STATE ); |
| 667 | } |
| 668 | |
| 669 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
Ronald Cron | 0c677c2 | 2021-04-09 17:15:06 +0200 | [diff] [blame] | 670 | mbedtls_psa_mac_operation_t *operation ) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 671 | { |
| 672 | return( mac_abort( operation ) ); |
| 673 | } |
| 674 | |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 675 | #endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 676 | |
| 677 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |