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