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