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 | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 44 | #if defined(BUILTIN_ALG_HMAC) |
Steven Cooreman | 32d5694 | 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 | 22dea1d | 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 | 32d5694 | 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 | 22dea1d | 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 | 32d5694 | 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 | 22dea1d | 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 | 76720f6 | 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 | 22dea1d | 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 | 32d5694 | 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 | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 190 | #endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | 0789783 | 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 | af81a71 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 195 | const uint8_t *key_buffer ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 196 | { |
| 197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 63fa40e | 2021-05-07 17:27:27 +0200 | [diff] [blame^] | 198 | |
| 199 | #if defined(PSA_WANT_KEY_TYPE_DES) |
| 200 | /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept |
| 201 | * to do CMAC with pure DES, so return NOT_SUPPORTED here. */ |
| 202 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES && |
| 203 | ( psa_get_key_bits( attributes ) == 64 || |
| 204 | psa_get_key_bits( attributes ) == 128 ) ) |
| 205 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 206 | #endif |
| 207 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 208 | const mbedtls_cipher_info_t * cipher_info = |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 209 | mbedtls_cipher_info_from_psa( |
| 210 | PSA_ALG_CMAC, |
| 211 | psa_get_key_type( attributes ), |
| 212 | psa_get_key_bits( attributes ), |
| 213 | NULL ); |
| 214 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 215 | if( cipher_info == NULL ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 216 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 217 | |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 218 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 219 | if( ret != 0 ) |
| 220 | goto exit; |
| 221 | |
| 222 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 223 | key_buffer, |
| 224 | psa_get_key_bits( attributes ) ); |
| 225 | exit: |
| 226 | return( mbedtls_to_psa_error( ret ) ); |
| 227 | } |
| 228 | #endif /* BUILTIN_ALG_CMAC */ |
| 229 | |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 230 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 231 | * software driver or the test driver requires it. */ |
| 232 | #if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC) |
| 233 | |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 234 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 235 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
| 236 | static psa_status_t mac_init( |
| 237 | mbedtls_psa_mac_operation_t *operation, |
| 238 | psa_algorithm_t alg ) |
| 239 | { |
Steven Cooreman | 6e6451e | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 240 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 241 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 242 | operation->alg = alg; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 243 | |
| 244 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 245 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 246 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 247 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 248 | status = PSA_SUCCESS; |
| 249 | } |
| 250 | else |
| 251 | #endif /* BUILTIN_ALG_CMAC */ |
| 252 | #if defined(BUILTIN_ALG_HMAC) |
| 253 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 254 | { |
| 255 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 256 | operation->ctx.hmac.alg = 0; |
| 257 | status = PSA_SUCCESS; |
| 258 | } |
| 259 | else |
| 260 | #endif /* BUILTIN_ALG_HMAC */ |
| 261 | { |
Steven Cooreman | 6e6451e | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 262 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if( status != PSA_SUCCESS ) |
| 266 | memset( operation, 0, sizeof( *operation ) ); |
| 267 | return( status ); |
| 268 | } |
| 269 | |
| 270 | static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation ) |
| 271 | { |
| 272 | if( operation->alg == 0 ) |
| 273 | { |
| 274 | /* The object has (apparently) been initialized but it is not |
| 275 | * in use. It's ok to call abort on such an object, and there's |
| 276 | * nothing to do. */ |
| 277 | return( PSA_SUCCESS ); |
| 278 | } |
| 279 | else |
| 280 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 281 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 282 | { |
| 283 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 284 | } |
| 285 | else |
| 286 | #endif /* BUILTIN_ALG_CMAC */ |
| 287 | #if defined(BUILTIN_ALG_HMAC) |
| 288 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 289 | { |
| 290 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
| 291 | } |
| 292 | else |
| 293 | #endif /* BUILTIN_ALG_HMAC */ |
| 294 | { |
| 295 | /* Sanity check (shouldn't happen: operation->alg should |
| 296 | * always have been initialized to a valid value). */ |
| 297 | goto bad_state; |
| 298 | } |
| 299 | |
| 300 | operation->alg = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 301 | |
| 302 | return( PSA_SUCCESS ); |
| 303 | |
| 304 | bad_state: |
| 305 | /* If abort is called on an uninitialized object, we can't trust |
| 306 | * anything. Wipe the object in case it contains confidential data. |
| 307 | * This may result in a memory leak if a pointer gets overwritten, |
| 308 | * but it's too late to do anything about this. */ |
| 309 | memset( operation, 0, sizeof( *operation ) ); |
| 310 | return( PSA_ERROR_BAD_STATE ); |
| 311 | } |
| 312 | |
| 313 | static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation, |
| 314 | const psa_key_attributes_t *attributes, |
| 315 | const uint8_t *key_buffer, |
| 316 | size_t key_buffer_size, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 317 | psa_algorithm_t alg ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 318 | { |
| 319 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 320 | |
| 321 | /* A context must be freshly initialized before it can be set up. */ |
| 322 | if( operation->alg != 0 ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 323 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 324 | |
| 325 | status = mac_init( operation, alg ); |
| 326 | if( status != PSA_SUCCESS ) |
| 327 | return( status ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 328 | |
| 329 | #if defined(BUILTIN_ALG_CMAC) |
| 330 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
| 331 | { |
Steven Cooreman | af81a71 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 332 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 333 | * attributes, and previously validated by the core on key import. */ |
| 334 | (void) key_buffer_size; |
| 335 | status = cmac_setup( operation, attributes, key_buffer ); |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 336 | } |
| 337 | else |
| 338 | #endif /* BUILTIN_ALG_CMAC */ |
| 339 | #if defined(BUILTIN_ALG_HMAC) |
| 340 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 341 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 342 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 343 | key_buffer, |
| 344 | key_buffer_size, |
| 345 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
| 346 | } |
| 347 | else |
| 348 | #endif /* BUILTIN_ALG_HMAC */ |
| 349 | { |
| 350 | status = PSA_ERROR_NOT_SUPPORTED; |
| 351 | } |
| 352 | |
Steven Cooreman | b4b9b28 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 353 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 354 | mac_abort( operation ); |
| 355 | |
| 356 | return( status ); |
| 357 | } |
| 358 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 359 | static psa_status_t mac_compute( |
| 360 | const psa_key_attributes_t *attributes, |
| 361 | const uint8_t *key_buffer, |
| 362 | size_t key_buffer_size, |
| 363 | psa_algorithm_t alg, |
| 364 | const uint8_t *input, |
| 365 | size_t input_length, |
| 366 | uint8_t *mac, |
| 367 | size_t mac_size, |
| 368 | size_t *mac_length ) |
| 369 | { |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 370 | /* One-shot MAC has not been implemented in this PSA implementation yet. */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 371 | (void) attributes; |
| 372 | (void) key_buffer; |
| 373 | (void) key_buffer_size; |
| 374 | (void) alg; |
| 375 | (void) input; |
| 376 | (void) input_length; |
| 377 | (void) mac; |
| 378 | (void) mac_size; |
| 379 | (void) mac_length; |
| 380 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 381 | } |
| 382 | |
| 383 | static psa_status_t mac_sign_setup( |
| 384 | mbedtls_psa_mac_operation_t *operation, |
| 385 | const psa_key_attributes_t *attributes, |
| 386 | const uint8_t *key_buffer, |
| 387 | size_t key_buffer_size, |
| 388 | psa_algorithm_t alg ) |
| 389 | { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 390 | return( mac_setup( operation, |
| 391 | attributes, key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static psa_status_t mac_verify_setup( |
| 395 | mbedtls_psa_mac_operation_t *operation, |
| 396 | const psa_key_attributes_t *attributes, |
| 397 | const uint8_t *key_buffer, |
| 398 | size_t key_buffer_size, |
| 399 | psa_algorithm_t alg ) |
| 400 | { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 401 | return( mac_setup( operation, |
| 402 | attributes, key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static psa_status_t mac_update( |
| 406 | mbedtls_psa_mac_operation_t *operation, |
| 407 | const uint8_t *input, |
| 408 | size_t input_length ) |
| 409 | { |
Steven Cooreman | b4b9b28 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 410 | if( operation->alg == 0 ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 411 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 412 | |
| 413 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 414 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 415 | { |
| 416 | return( mbedtls_to_psa_error( |
| 417 | mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 418 | input, input_length ) ) ); |
| 419 | } |
| 420 | else |
| 421 | #endif /* BUILTIN_ALG_CMAC */ |
| 422 | #if defined(BUILTIN_ALG_HMAC) |
| 423 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 424 | { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 425 | return( psa_hmac_update_internal( &operation->ctx.hmac, |
| 426 | input, input_length ) ); |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 427 | } |
| 428 | else |
| 429 | #endif /* BUILTIN_ALG_HMAC */ |
| 430 | { |
| 431 | /* This shouldn't happen if `operation` was initialized by |
| 432 | * a setup function. */ |
| 433 | return( PSA_ERROR_BAD_STATE ); |
| 434 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 437 | static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation, |
| 438 | uint8_t *mac, |
| 439 | size_t mac_size ) |
| 440 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 441 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 442 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 443 | { |
| 444 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
| 445 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 446 | if( ret == 0 ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 447 | memcpy( mac, tmp, mac_size ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 448 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 449 | return( mbedtls_to_psa_error( ret ) ); |
| 450 | } |
| 451 | else |
| 452 | #endif /* BUILTIN_ALG_CMAC */ |
| 453 | #if defined(BUILTIN_ALG_HMAC) |
| 454 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 455 | { |
| 456 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 457 | mac, mac_size ) ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 458 | } |
| 459 | else |
| 460 | #endif /* BUILTIN_ALG_HMAC */ |
| 461 | { |
| 462 | /* This shouldn't happen if `operation` was initialized by |
| 463 | * a setup function. */ |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 464 | (void) operation; |
| 465 | (void) mac; |
| 466 | (void) mac_size; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 467 | return( PSA_ERROR_BAD_STATE ); |
| 468 | } |
| 469 | } |
| 470 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 471 | static psa_status_t mac_sign_finish( |
| 472 | mbedtls_psa_mac_operation_t *operation, |
| 473 | uint8_t *mac, |
| 474 | size_t mac_size, |
| 475 | size_t *mac_length ) |
| 476 | { |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 477 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 478 | |
| 479 | if( operation->alg == 0 ) |
| 480 | return( PSA_ERROR_BAD_STATE ); |
| 481 | |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 482 | status = mac_finish_internal( operation, mac, mac_size ); |
| 483 | |
| 484 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 485 | *mac_length = mac_size; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 486 | |
| 487 | return( status ); |
| 488 | } |
| 489 | |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 490 | static psa_status_t mac_verify_finish( |
| 491 | mbedtls_psa_mac_operation_t *operation, |
| 492 | const uint8_t *mac, |
| 493 | size_t mac_length ) |
| 494 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 495 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 496 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 497 | |
| 498 | if( operation->alg == 0 ) |
| 499 | return( PSA_ERROR_BAD_STATE ); |
| 500 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 501 | /* Consistency check: requested MAC length fits our local buffer */ |
| 502 | if( mac_length > sizeof( actual_mac ) ) |
| 503 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 504 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 505 | status = mac_finish_internal( operation, actual_mac, mac_length ); |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 506 | if( status != PSA_SUCCESS ) |
| 507 | goto cleanup; |
| 508 | |
Steven Cooreman | a2a1b80 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 509 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 510 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 511 | |
| 512 | cleanup: |
| 513 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
| 514 | |
| 515 | return( status ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 516 | } |
Steven Cooreman | aaf9944 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 517 | #endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 518 | |
| 519 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 520 | psa_status_t mbedtls_psa_mac_compute( |
| 521 | const psa_key_attributes_t *attributes, |
| 522 | const uint8_t *key_buffer, |
| 523 | size_t key_buffer_size, |
| 524 | psa_algorithm_t alg, |
| 525 | const uint8_t *input, |
| 526 | size_t input_length, |
| 527 | uint8_t *mac, |
| 528 | size_t mac_size, |
| 529 | size_t *mac_length ) |
| 530 | { |
| 531 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 532 | input, input_length, |
| 533 | mac, mac_size, mac_length ) ); |
| 534 | } |
| 535 | |
| 536 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 537 | mbedtls_psa_mac_operation_t *operation, |
| 538 | const psa_key_attributes_t *attributes, |
| 539 | const uint8_t *key_buffer, |
| 540 | size_t key_buffer_size, |
| 541 | psa_algorithm_t alg ) |
| 542 | { |
| 543 | return( mac_sign_setup( operation, attributes, |
| 544 | key_buffer, key_buffer_size, alg ) ); |
| 545 | } |
| 546 | |
| 547 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 548 | mbedtls_psa_mac_operation_t *operation, |
| 549 | const psa_key_attributes_t *attributes, |
| 550 | const uint8_t *key_buffer, |
| 551 | size_t key_buffer_size, |
| 552 | psa_algorithm_t alg ) |
| 553 | { |
| 554 | return( mac_verify_setup( operation, attributes, |
| 555 | key_buffer, key_buffer_size, alg ) ); |
| 556 | } |
| 557 | |
| 558 | psa_status_t mbedtls_psa_mac_update( |
| 559 | mbedtls_psa_mac_operation_t *operation, |
| 560 | const uint8_t *input, |
| 561 | size_t input_length ) |
| 562 | { |
| 563 | return( mac_update( operation, input, input_length ) ); |
| 564 | } |
| 565 | |
| 566 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 567 | mbedtls_psa_mac_operation_t *operation, |
| 568 | uint8_t *mac, |
| 569 | size_t mac_size, |
| 570 | size_t *mac_length ) |
| 571 | { |
| 572 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 573 | } |
| 574 | |
| 575 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 576 | mbedtls_psa_mac_operation_t *operation, |
| 577 | const uint8_t *mac, |
| 578 | size_t mac_length ) |
| 579 | { |
| 580 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 581 | } |
| 582 | |
| 583 | psa_status_t mbedtls_psa_mac_abort( |
| 584 | mbedtls_psa_mac_operation_t *operation ) |
| 585 | { |
| 586 | return( mac_abort( operation ) ); |
| 587 | } |
| 588 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 589 | |
| 590 | /* |
| 591 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 592 | */ |
| 593 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 594 | |
| 595 | static int is_mac_accelerated( psa_algorithm_t alg ) |
| 596 | { |
| 597 | #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 598 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 599 | return( 1 ); |
| 600 | #endif |
| 601 | |
| 602 | switch( PSA_ALG_FULL_LENGTH_MAC( alg ) ) |
| 603 | { |
| 604 | #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
| 605 | case PSA_ALG_CMAC: |
| 606 | return( 1 ); |
| 607 | #endif |
| 608 | default: |
| 609 | return( 0 ); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 614 | const psa_key_attributes_t *attributes, |
| 615 | const uint8_t *key_buffer, |
| 616 | size_t key_buffer_size, |
| 617 | psa_algorithm_t alg, |
| 618 | const uint8_t *input, |
| 619 | size_t input_length, |
| 620 | uint8_t *mac, |
| 621 | size_t mac_size, |
| 622 | size_t *mac_length ) |
| 623 | { |
| 624 | if( is_mac_accelerated( alg ) ) |
| 625 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 626 | input, input_length, |
| 627 | mac, mac_size, mac_length ) ); |
| 628 | else |
| 629 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 630 | } |
| 631 | |
| 632 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 633 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 634 | const psa_key_attributes_t *attributes, |
| 635 | const uint8_t *key_buffer, |
| 636 | size_t key_buffer_size, |
| 637 | psa_algorithm_t alg ) |
| 638 | { |
| 639 | if( is_mac_accelerated( alg ) ) |
| 640 | return( mac_sign_setup( operation, attributes, |
| 641 | key_buffer, key_buffer_size, alg ) ); |
| 642 | else |
| 643 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 644 | } |
| 645 | |
| 646 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
| 647 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 648 | const psa_key_attributes_t *attributes, |
| 649 | const uint8_t *key_buffer, |
| 650 | size_t key_buffer_size, |
| 651 | psa_algorithm_t alg ) |
| 652 | { |
| 653 | if( is_mac_accelerated( alg ) ) |
| 654 | return( mac_verify_setup( operation, attributes, |
| 655 | key_buffer, key_buffer_size, alg ) ); |
| 656 | else |
| 657 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 658 | } |
| 659 | |
| 660 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
| 661 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 662 | const uint8_t *input, |
| 663 | size_t input_length ) |
| 664 | { |
| 665 | if( is_mac_accelerated( operation->alg ) ) |
| 666 | return( mac_update( operation, input, input_length ) ); |
| 667 | else |
| 668 | return( PSA_ERROR_BAD_STATE ); |
| 669 | } |
| 670 | |
| 671 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 672 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 673 | uint8_t *mac, |
| 674 | size_t mac_size, |
| 675 | size_t *mac_length ) |
| 676 | { |
| 677 | if( is_mac_accelerated( operation->alg ) ) |
| 678 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 679 | else |
| 680 | return( PSA_ERROR_BAD_STATE ); |
| 681 | } |
| 682 | |
| 683 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 684 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 685 | const uint8_t *mac, |
| 686 | size_t mac_length ) |
| 687 | { |
| 688 | if( is_mac_accelerated( operation->alg ) ) |
| 689 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 690 | else |
| 691 | return( PSA_ERROR_BAD_STATE ); |
| 692 | } |
| 693 | |
| 694 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
| 695 | mbedtls_transparent_test_driver_mac_operation_t *operation ) |
| 696 | { |
| 697 | return( mac_abort( operation ) ); |
| 698 | } |
| 699 | |
| 700 | psa_status_t mbedtls_opaque_test_driver_mac_compute( |
| 701 | const psa_key_attributes_t *attributes, |
| 702 | const uint8_t *key_buffer, |
| 703 | size_t key_buffer_size, |
| 704 | psa_algorithm_t alg, |
| 705 | const uint8_t *input, |
| 706 | size_t input_length, |
| 707 | uint8_t *mac, |
| 708 | size_t mac_size, |
| 709 | size_t *mac_length ) |
| 710 | { |
| 711 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 712 | (void) attributes; |
| 713 | (void) key_buffer; |
| 714 | (void) key_buffer_size; |
| 715 | (void) alg; |
| 716 | (void) input; |
| 717 | (void) input_length; |
| 718 | (void) mac; |
| 719 | (void) mac_size; |
| 720 | (void) mac_length; |
| 721 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 722 | } |
| 723 | |
| 724 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 725 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 726 | const psa_key_attributes_t *attributes, |
| 727 | const uint8_t *key_buffer, |
| 728 | size_t key_buffer_size, |
| 729 | psa_algorithm_t alg ) |
| 730 | { |
| 731 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 732 | (void) operation; |
| 733 | (void) attributes; |
| 734 | (void) key_buffer; |
| 735 | (void) key_buffer_size; |
| 736 | (void) alg; |
| 737 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 738 | } |
| 739 | |
| 740 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 741 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 742 | const psa_key_attributes_t *attributes, |
| 743 | const uint8_t *key_buffer, |
| 744 | size_t key_buffer_size, |
| 745 | psa_algorithm_t alg ) |
| 746 | { |
| 747 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 748 | (void) operation; |
| 749 | (void) attributes; |
| 750 | (void) key_buffer; |
| 751 | (void) key_buffer_size; |
| 752 | (void) alg; |
| 753 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 754 | } |
| 755 | |
| 756 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 757 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 758 | const uint8_t *input, |
| 759 | size_t input_length ) |
| 760 | { |
| 761 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 762 | (void) operation; |
| 763 | (void) input; |
| 764 | (void) input_length; |
| 765 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 766 | } |
| 767 | |
| 768 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 769 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 770 | uint8_t *mac, |
| 771 | size_t mac_size, |
| 772 | size_t *mac_length ) |
| 773 | { |
| 774 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 775 | (void) operation; |
| 776 | (void) mac; |
| 777 | (void) mac_size; |
| 778 | (void) mac_length; |
| 779 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 780 | } |
| 781 | |
| 782 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 783 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 784 | const uint8_t *mac, |
| 785 | size_t mac_length ) |
| 786 | { |
| 787 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 788 | (void) operation; |
| 789 | (void) mac; |
| 790 | (void) mac_length; |
| 791 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 792 | } |
| 793 | |
| 794 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
| 795 | mbedtls_opaque_test_driver_mac_operation_t *operation ) |
| 796 | { |
| 797 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 798 | (void) operation; |
| 799 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 800 | } |
| 801 | |
| 802 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 803 | |
| 804 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |