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) || \ |
| 36 | ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) ) |
| 37 | #define BUILTIN_ALG_CMAC 1 |
| 38 | #endif |
| 39 | #if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 40 | ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) ) |
| 41 | #define BUILTIN_ALG_HMAC 1 |
| 42 | #endif |
| 43 | |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 44 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST) |
| 45 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
| 46 | { |
| 47 | switch( alg ) |
| 48 | { |
| 49 | case PSA_ALG_MD2: |
| 50 | return( 16 ); |
| 51 | case PSA_ALG_MD4: |
| 52 | return( 64 ); |
| 53 | case PSA_ALG_MD5: |
| 54 | return( 64 ); |
| 55 | case PSA_ALG_RIPEMD160: |
| 56 | return( 64 ); |
| 57 | case PSA_ALG_SHA_1: |
| 58 | return( 64 ); |
| 59 | case PSA_ALG_SHA_224: |
| 60 | return( 64 ); |
| 61 | case PSA_ALG_SHA_256: |
| 62 | return( 64 ); |
| 63 | case PSA_ALG_SHA_384: |
| 64 | return( 128 ); |
| 65 | case PSA_ALG_SHA_512: |
| 66 | return( 128 ); |
| 67 | default: |
| 68 | return( 0 ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 73 | { |
| 74 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
| 75 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 76 | } |
| 77 | |
| 78 | psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 79 | const uint8_t *key, |
| 80 | size_t key_length, |
| 81 | psa_algorithm_t hash_alg ) |
| 82 | { |
| 83 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
| 84 | size_t i; |
| 85 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
| 86 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 87 | psa_status_t status; |
| 88 | |
| 89 | hmac->alg = hash_alg; |
| 90 | |
| 91 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 92 | * overflow below. This should never trigger if the hash algorithm |
| 93 | * is implemented correctly. */ |
| 94 | /* The size checks against the ipad and opad buffers cannot be written |
| 95 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 96 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 97 | if( block_size > sizeof( ipad ) ) |
| 98 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 99 | if( block_size > sizeof( hmac->opad ) ) |
| 100 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 101 | if( block_size < hash_size ) |
| 102 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 103 | |
| 104 | if( key_length > block_size ) |
| 105 | { |
| 106 | status = psa_hash_compute( hash_alg, key, key_length, |
| 107 | ipad, sizeof( ipad ), &key_length ); |
| 108 | if( status != PSA_SUCCESS ) |
| 109 | goto cleanup; |
| 110 | } |
| 111 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 112 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 113 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 114 | * an invalid pointer which would make the behavior undefined. */ |
| 115 | else if( key_length != 0 ) |
| 116 | memcpy( ipad, key, key_length ); |
| 117 | |
| 118 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 119 | * to create the ipad value. */ |
| 120 | for( i = 0; i < key_length; i++ ) |
| 121 | ipad[i] ^= 0x36; |
| 122 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 123 | |
| 124 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 125 | * and filling the rest of opad with the requisite constant. */ |
| 126 | for( i = 0; i < key_length; i++ ) |
| 127 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 128 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
| 129 | |
| 130 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 131 | if( status != PSA_SUCCESS ) |
| 132 | goto cleanup; |
| 133 | |
| 134 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
| 135 | |
| 136 | cleanup: |
| 137 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
| 138 | |
| 139 | return( status ); |
| 140 | } |
| 141 | |
| 142 | psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 143 | uint8_t *mac, |
| 144 | size_t mac_size ) |
| 145 | { |
| 146 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
| 147 | psa_algorithm_t hash_alg = hmac->alg; |
| 148 | size_t hash_size = 0; |
| 149 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 150 | psa_status_t status; |
| 151 | |
| 152 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 153 | if( status != PSA_SUCCESS ) |
| 154 | return( status ); |
| 155 | /* From here on, tmp needs to be wiped. */ |
| 156 | |
| 157 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 158 | if( status != PSA_SUCCESS ) |
| 159 | goto exit; |
| 160 | |
| 161 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 162 | if( status != PSA_SUCCESS ) |
| 163 | goto exit; |
| 164 | |
| 165 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 166 | if( status != PSA_SUCCESS ) |
| 167 | goto exit; |
| 168 | |
| 169 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 170 | if( status != PSA_SUCCESS ) |
| 171 | goto exit; |
| 172 | |
| 173 | memcpy( mac, tmp, mac_size ); |
| 174 | |
| 175 | exit: |
| 176 | mbedtls_platform_zeroize( tmp, hash_size ); |
| 177 | return( status ); |
| 178 | } |
| 179 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || PSA_CRYPTO_DRIVER_TEST */ |
| 180 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 181 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 182 | * software driver or the test driver requires it. */ |
| 183 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) || defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame^] | 184 | |
| 185 | #if defined(BUILTIN_ALG_CMAC) |
| 186 | static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation, |
| 187 | const psa_key_attributes_t *attributes, |
| 188 | const uint8_t *key_buffer, |
| 189 | size_t key_buffer_size ) |
| 190 | { |
| 191 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 192 | const mbedtls_cipher_info_t * cipher_info_tmp = |
| 193 | mbedtls_cipher_info_from_psa( |
| 194 | PSA_ALG_CMAC, |
| 195 | psa_get_key_type( attributes ), |
| 196 | psa_get_key_bits( attributes ), |
| 197 | NULL ); |
| 198 | |
| 199 | if( cipher_info_tmp == NULL ) |
| 200 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 201 | |
| 202 | if( key_buffer_size < PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) ) |
| 203 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 204 | |
| 205 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info_tmp ); |
| 206 | if( ret != 0 ) |
| 207 | goto exit; |
| 208 | |
| 209 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 210 | key_buffer, |
| 211 | psa_get_key_bits( attributes ) ); |
| 212 | exit: |
| 213 | return( mbedtls_to_psa_error( ret ) ); |
| 214 | } |
| 215 | #endif /* BUILTIN_ALG_CMAC */ |
| 216 | |
| 217 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 218 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
| 219 | static psa_status_t mac_init( |
| 220 | mbedtls_psa_mac_operation_t *operation, |
| 221 | psa_algorithm_t alg ) |
| 222 | { |
| 223 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 224 | |
| 225 | operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
| 226 | operation->key_set = 0; |
| 227 | operation->iv_set = 0; |
| 228 | operation->iv_required = 0; |
| 229 | operation->has_input = 0; |
| 230 | operation->is_sign = 0; |
| 231 | |
| 232 | #if defined(BUILTIN_ALG_CMAC) |
| 233 | if( operation->alg == PSA_ALG_CMAC ) |
| 234 | { |
| 235 | operation->iv_required = 0; |
| 236 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 237 | status = PSA_SUCCESS; |
| 238 | } |
| 239 | else |
| 240 | #endif /* BUILTIN_ALG_CMAC */ |
| 241 | #if defined(BUILTIN_ALG_HMAC) |
| 242 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 243 | { |
| 244 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 245 | operation->ctx.hmac.alg = 0; |
| 246 | status = PSA_SUCCESS; |
| 247 | } |
| 248 | else |
| 249 | #endif /* BUILTIN_ALG_HMAC */ |
| 250 | { |
| 251 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 252 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 253 | } |
| 254 | |
| 255 | if( status != PSA_SUCCESS ) |
| 256 | memset( operation, 0, sizeof( *operation ) ); |
| 257 | return( status ); |
| 258 | } |
| 259 | |
| 260 | static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation ) |
| 261 | { |
| 262 | if( operation->alg == 0 ) |
| 263 | { |
| 264 | /* The object has (apparently) been initialized but it is not |
| 265 | * in use. It's ok to call abort on such an object, and there's |
| 266 | * nothing to do. */ |
| 267 | return( PSA_SUCCESS ); |
| 268 | } |
| 269 | else |
| 270 | #if defined(BUILTIN_ALG_CMAC) |
| 271 | if( operation->alg == PSA_ALG_CMAC ) |
| 272 | { |
| 273 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 274 | } |
| 275 | else |
| 276 | #endif /* BUILTIN_ALG_CMAC */ |
| 277 | #if defined(BUILTIN_ALG_HMAC) |
| 278 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 279 | { |
| 280 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
| 281 | } |
| 282 | else |
| 283 | #endif /* BUILTIN_ALG_HMAC */ |
| 284 | { |
| 285 | /* Sanity check (shouldn't happen: operation->alg should |
| 286 | * always have been initialized to a valid value). */ |
| 287 | goto bad_state; |
| 288 | } |
| 289 | |
| 290 | operation->alg = 0; |
| 291 | operation->key_set = 0; |
| 292 | operation->iv_set = 0; |
| 293 | operation->iv_required = 0; |
| 294 | operation->has_input = 0; |
| 295 | operation->is_sign = 0; |
| 296 | |
| 297 | return( PSA_SUCCESS ); |
| 298 | |
| 299 | bad_state: |
| 300 | /* If abort is called on an uninitialized object, we can't trust |
| 301 | * anything. Wipe the object in case it contains confidential data. |
| 302 | * This may result in a memory leak if a pointer gets overwritten, |
| 303 | * but it's too late to do anything about this. */ |
| 304 | memset( operation, 0, sizeof( *operation ) ); |
| 305 | return( PSA_ERROR_BAD_STATE ); |
| 306 | } |
| 307 | |
| 308 | static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation, |
| 309 | const psa_key_attributes_t *attributes, |
| 310 | const uint8_t *key_buffer, |
| 311 | size_t key_buffer_size, |
| 312 | psa_algorithm_t alg, |
| 313 | int is_sign ) |
| 314 | { |
| 315 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 316 | |
| 317 | /* A context must be freshly initialized before it can be set up. */ |
| 318 | if( operation->alg != 0 ) |
| 319 | { |
| 320 | return( PSA_ERROR_BAD_STATE ); |
| 321 | } |
| 322 | |
| 323 | status = mac_init( operation, alg ); |
| 324 | if( status != PSA_SUCCESS ) |
| 325 | return( status ); |
| 326 | if( is_sign ) |
| 327 | operation->is_sign = 1; |
| 328 | |
| 329 | /* Get the output length for the algorithm and key combination. None of the |
| 330 | * currently supported algorithms have an output length dependent on actual |
| 331 | * key size, so setting it to a bogus value is currently OK. */ |
| 332 | operation->mac_size = |
| 333 | PSA_MAC_LENGTH( psa_get_key_type( attributes ), 0, alg ); |
| 334 | |
| 335 | #if defined(BUILTIN_ALG_CMAC) |
| 336 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
| 337 | { |
| 338 | status = cmac_setup( operation, attributes, |
| 339 | key_buffer, key_buffer_size ); |
| 340 | } |
| 341 | else |
| 342 | #endif /* BUILTIN_ALG_CMAC */ |
| 343 | #if defined(BUILTIN_ALG_HMAC) |
| 344 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 345 | { |
| 346 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 347 | if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 348 | { |
| 349 | status = PSA_ERROR_NOT_SUPPORTED; |
| 350 | goto exit; |
| 351 | } |
| 352 | |
| 353 | if( psa_get_key_type( attributes ) != PSA_KEY_TYPE_HMAC ) |
| 354 | { |
| 355 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 356 | goto exit; |
| 357 | } |
| 358 | |
| 359 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 360 | key_buffer, |
| 361 | key_buffer_size, |
| 362 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
| 363 | } |
| 364 | else |
| 365 | #endif /* BUILTIN_ALG_HMAC */ |
| 366 | { |
| 367 | status = PSA_ERROR_NOT_SUPPORTED; |
| 368 | } |
| 369 | |
| 370 | exit: |
| 371 | if( status == PSA_SUCCESS ) |
| 372 | operation->key_set = 1; |
| 373 | else |
| 374 | mac_abort( operation ); |
| 375 | |
| 376 | return( status ); |
| 377 | } |
| 378 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 379 | static psa_status_t mac_compute( |
| 380 | const psa_key_attributes_t *attributes, |
| 381 | const uint8_t *key_buffer, |
| 382 | size_t key_buffer_size, |
| 383 | psa_algorithm_t alg, |
| 384 | const uint8_t *input, |
| 385 | size_t input_length, |
| 386 | uint8_t *mac, |
| 387 | size_t mac_size, |
| 388 | size_t *mac_length ) |
| 389 | { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 390 | /* One-shot MAC has not been implemented in this PSA implementation yet. */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 391 | (void) attributes; |
| 392 | (void) key_buffer; |
| 393 | (void) key_buffer_size; |
| 394 | (void) alg; |
| 395 | (void) input; |
| 396 | (void) input_length; |
| 397 | (void) mac; |
| 398 | (void) mac_size; |
| 399 | (void) mac_length; |
| 400 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 401 | } |
| 402 | |
| 403 | static psa_status_t mac_sign_setup( |
| 404 | mbedtls_psa_mac_operation_t *operation, |
| 405 | const psa_key_attributes_t *attributes, |
| 406 | const uint8_t *key_buffer, |
| 407 | size_t key_buffer_size, |
| 408 | psa_algorithm_t alg ) |
| 409 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame^] | 410 | return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg, |
| 411 | 1 ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static psa_status_t mac_verify_setup( |
| 415 | mbedtls_psa_mac_operation_t *operation, |
| 416 | const psa_key_attributes_t *attributes, |
| 417 | const uint8_t *key_buffer, |
| 418 | size_t key_buffer_size, |
| 419 | psa_algorithm_t alg ) |
| 420 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame^] | 421 | return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg, |
| 422 | 0 ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static psa_status_t mac_update( |
| 426 | mbedtls_psa_mac_operation_t *operation, |
| 427 | const uint8_t *input, |
| 428 | size_t input_length ) |
| 429 | { |
| 430 | /* To be fleshed out in a subsequent commit */ |
| 431 | (void) operation; |
| 432 | (void) input; |
| 433 | (void) input_length; |
| 434 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 435 | } |
| 436 | |
| 437 | static psa_status_t mac_sign_finish( |
| 438 | mbedtls_psa_mac_operation_t *operation, |
| 439 | uint8_t *mac, |
| 440 | size_t mac_size, |
| 441 | size_t *mac_length ) |
| 442 | { |
| 443 | /* To be fleshed out in a subsequent commit */ |
| 444 | (void) operation; |
| 445 | (void) mac; |
| 446 | (void) mac_size; |
| 447 | (void) mac_length; |
| 448 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 449 | } |
| 450 | |
| 451 | static psa_status_t mac_verify_finish( |
| 452 | mbedtls_psa_mac_operation_t *operation, |
| 453 | const uint8_t *mac, |
| 454 | size_t mac_length ) |
| 455 | { |
| 456 | /* To be fleshed out in a subsequent commit */ |
| 457 | (void) operation; |
| 458 | (void) mac; |
| 459 | (void) mac_length; |
| 460 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 461 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 462 | #endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */ |
| 463 | |
| 464 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 465 | psa_status_t mbedtls_psa_mac_compute( |
| 466 | const psa_key_attributes_t *attributes, |
| 467 | const uint8_t *key_buffer, |
| 468 | size_t key_buffer_size, |
| 469 | psa_algorithm_t alg, |
| 470 | const uint8_t *input, |
| 471 | size_t input_length, |
| 472 | uint8_t *mac, |
| 473 | size_t mac_size, |
| 474 | size_t *mac_length ) |
| 475 | { |
| 476 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 477 | input, input_length, |
| 478 | mac, mac_size, mac_length ) ); |
| 479 | } |
| 480 | |
| 481 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 482 | mbedtls_psa_mac_operation_t *operation, |
| 483 | const psa_key_attributes_t *attributes, |
| 484 | const uint8_t *key_buffer, |
| 485 | size_t key_buffer_size, |
| 486 | psa_algorithm_t alg ) |
| 487 | { |
| 488 | return( mac_sign_setup( operation, attributes, |
| 489 | key_buffer, key_buffer_size, alg ) ); |
| 490 | } |
| 491 | |
| 492 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 493 | mbedtls_psa_mac_operation_t *operation, |
| 494 | const psa_key_attributes_t *attributes, |
| 495 | const uint8_t *key_buffer, |
| 496 | size_t key_buffer_size, |
| 497 | psa_algorithm_t alg ) |
| 498 | { |
| 499 | return( mac_verify_setup( operation, attributes, |
| 500 | key_buffer, key_buffer_size, alg ) ); |
| 501 | } |
| 502 | |
| 503 | psa_status_t mbedtls_psa_mac_update( |
| 504 | mbedtls_psa_mac_operation_t *operation, |
| 505 | const uint8_t *input, |
| 506 | size_t input_length ) |
| 507 | { |
| 508 | return( mac_update( operation, input, input_length ) ); |
| 509 | } |
| 510 | |
| 511 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 512 | mbedtls_psa_mac_operation_t *operation, |
| 513 | uint8_t *mac, |
| 514 | size_t mac_size, |
| 515 | size_t *mac_length ) |
| 516 | { |
| 517 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 518 | } |
| 519 | |
| 520 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 521 | mbedtls_psa_mac_operation_t *operation, |
| 522 | const uint8_t *mac, |
| 523 | size_t mac_length ) |
| 524 | { |
| 525 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 526 | } |
| 527 | |
| 528 | psa_status_t mbedtls_psa_mac_abort( |
| 529 | mbedtls_psa_mac_operation_t *operation ) |
| 530 | { |
| 531 | return( mac_abort( operation ) ); |
| 532 | } |
| 533 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 534 | |
| 535 | /* |
| 536 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 537 | */ |
| 538 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 539 | |
| 540 | static int is_mac_accelerated( psa_algorithm_t alg ) |
| 541 | { |
| 542 | #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 543 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 544 | return( 1 ); |
| 545 | #endif |
| 546 | |
| 547 | switch( PSA_ALG_FULL_LENGTH_MAC( alg ) ) |
| 548 | { |
| 549 | #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
| 550 | case PSA_ALG_CMAC: |
| 551 | return( 1 ); |
| 552 | #endif |
| 553 | default: |
| 554 | return( 0 ); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 559 | const psa_key_attributes_t *attributes, |
| 560 | const uint8_t *key_buffer, |
| 561 | size_t key_buffer_size, |
| 562 | psa_algorithm_t alg, |
| 563 | const uint8_t *input, |
| 564 | size_t input_length, |
| 565 | uint8_t *mac, |
| 566 | size_t mac_size, |
| 567 | size_t *mac_length ) |
| 568 | { |
| 569 | if( is_mac_accelerated( alg ) ) |
| 570 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 571 | input, input_length, |
| 572 | mac, mac_size, mac_length ) ); |
| 573 | else |
| 574 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 575 | } |
| 576 | |
| 577 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 578 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 579 | const psa_key_attributes_t *attributes, |
| 580 | const uint8_t *key_buffer, |
| 581 | size_t key_buffer_size, |
| 582 | psa_algorithm_t alg ) |
| 583 | { |
| 584 | if( is_mac_accelerated( alg ) ) |
| 585 | return( mac_sign_setup( operation, attributes, |
| 586 | key_buffer, key_buffer_size, alg ) ); |
| 587 | else |
| 588 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 589 | } |
| 590 | |
| 591 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
| 592 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 593 | const psa_key_attributes_t *attributes, |
| 594 | const uint8_t *key_buffer, |
| 595 | size_t key_buffer_size, |
| 596 | psa_algorithm_t alg ) |
| 597 | { |
| 598 | if( is_mac_accelerated( alg ) ) |
| 599 | return( mac_verify_setup( operation, attributes, |
| 600 | key_buffer, key_buffer_size, alg ) ); |
| 601 | else |
| 602 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 603 | } |
| 604 | |
| 605 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
| 606 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 607 | const uint8_t *input, |
| 608 | size_t input_length ) |
| 609 | { |
| 610 | if( is_mac_accelerated( operation->alg ) ) |
| 611 | return( mac_update( operation, input, input_length ) ); |
| 612 | else |
| 613 | return( PSA_ERROR_BAD_STATE ); |
| 614 | } |
| 615 | |
| 616 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 617 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 618 | uint8_t *mac, |
| 619 | size_t mac_size, |
| 620 | size_t *mac_length ) |
| 621 | { |
| 622 | if( is_mac_accelerated( operation->alg ) ) |
| 623 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 624 | else |
| 625 | return( PSA_ERROR_BAD_STATE ); |
| 626 | } |
| 627 | |
| 628 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 629 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 630 | const uint8_t *mac, |
| 631 | size_t mac_length ) |
| 632 | { |
| 633 | if( is_mac_accelerated( operation->alg ) ) |
| 634 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 635 | else |
| 636 | return( PSA_ERROR_BAD_STATE ); |
| 637 | } |
| 638 | |
| 639 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
| 640 | mbedtls_transparent_test_driver_mac_operation_t *operation ) |
| 641 | { |
| 642 | return( mac_abort( operation ) ); |
| 643 | } |
| 644 | |
| 645 | psa_status_t mbedtls_opaque_test_driver_mac_compute( |
| 646 | const psa_key_attributes_t *attributes, |
| 647 | const uint8_t *key_buffer, |
| 648 | size_t key_buffer_size, |
| 649 | psa_algorithm_t alg, |
| 650 | const uint8_t *input, |
| 651 | size_t input_length, |
| 652 | uint8_t *mac, |
| 653 | size_t mac_size, |
| 654 | size_t *mac_length ) |
| 655 | { |
| 656 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 657 | (void) attributes; |
| 658 | (void) key_buffer; |
| 659 | (void) key_buffer_size; |
| 660 | (void) alg; |
| 661 | (void) input; |
| 662 | (void) input_length; |
| 663 | (void) mac; |
| 664 | (void) mac_size; |
| 665 | (void) mac_length; |
| 666 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 667 | } |
| 668 | |
| 669 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 670 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 671 | const psa_key_attributes_t *attributes, |
| 672 | const uint8_t *key_buffer, |
| 673 | size_t key_buffer_size, |
| 674 | psa_algorithm_t alg ) |
| 675 | { |
| 676 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 677 | (void) operation; |
| 678 | (void) attributes; |
| 679 | (void) key_buffer; |
| 680 | (void) key_buffer_size; |
| 681 | (void) alg; |
| 682 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 683 | } |
| 684 | |
| 685 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 686 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 687 | const psa_key_attributes_t *attributes, |
| 688 | const uint8_t *key_buffer, |
| 689 | size_t key_buffer_size, |
| 690 | psa_algorithm_t alg ) |
| 691 | { |
| 692 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 693 | (void) operation; |
| 694 | (void) attributes; |
| 695 | (void) key_buffer; |
| 696 | (void) key_buffer_size; |
| 697 | (void) alg; |
| 698 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 699 | } |
| 700 | |
| 701 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 702 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 703 | const uint8_t *input, |
| 704 | size_t input_length ) |
| 705 | { |
| 706 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 707 | (void) operation; |
| 708 | (void) input; |
| 709 | (void) input_length; |
| 710 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 711 | } |
| 712 | |
| 713 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 714 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 715 | uint8_t *mac, |
| 716 | size_t mac_size, |
| 717 | size_t *mac_length ) |
| 718 | { |
| 719 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 720 | (void) operation; |
| 721 | (void) mac; |
| 722 | (void) mac_size; |
| 723 | (void) mac_length; |
| 724 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 725 | } |
| 726 | |
| 727 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 728 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 729 | const uint8_t *mac, |
| 730 | size_t mac_length ) |
| 731 | { |
| 732 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 733 | (void) operation; |
| 734 | (void) mac; |
| 735 | (void) mac_length; |
| 736 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 737 | } |
| 738 | |
| 739 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
| 740 | mbedtls_opaque_test_driver_mac_operation_t *operation ) |
| 741 | { |
| 742 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 743 | (void) operation; |
| 744 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 745 | } |
| 746 | |
| 747 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 748 | |
| 749 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |