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_compute( |
| 359 | const psa_key_attributes_t *attributes, |
| 360 | const uint8_t *key_buffer, |
| 361 | size_t key_buffer_size, |
| 362 | psa_algorithm_t alg, |
| 363 | const uint8_t *input, |
| 364 | size_t input_length, |
| 365 | uint8_t *mac, |
| 366 | size_t mac_size, |
| 367 | size_t *mac_length ) |
| 368 | { |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 369 | /* One-shot MAC has not been implemented in this PSA implementation yet. */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 370 | (void) attributes; |
| 371 | (void) key_buffer; |
| 372 | (void) key_buffer_size; |
| 373 | (void) alg; |
| 374 | (void) input; |
| 375 | (void) input_length; |
| 376 | (void) mac; |
| 377 | (void) mac_size; |
| 378 | (void) mac_length; |
| 379 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 380 | } |
| 381 | |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 382 | static psa_status_t mac_update( |
| 383 | mbedtls_psa_mac_operation_t *operation, |
| 384 | const uint8_t *input, |
| 385 | size_t input_length ) |
| 386 | { |
Steven Cooreman | a4638e7 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 387 | if( operation->alg == 0 ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 388 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 389 | |
| 390 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 391 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 392 | { |
| 393 | return( mbedtls_to_psa_error( |
| 394 | mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 395 | input, input_length ) ) ); |
| 396 | } |
| 397 | else |
| 398 | #endif /* BUILTIN_ALG_CMAC */ |
| 399 | #if defined(BUILTIN_ALG_HMAC) |
| 400 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 401 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 402 | return( psa_hmac_update_internal( &operation->ctx.hmac, |
| 403 | input, input_length ) ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | #endif /* BUILTIN_ALG_HMAC */ |
| 407 | { |
| 408 | /* This shouldn't happen if `operation` was initialized by |
| 409 | * a setup function. */ |
Steven Cooreman | b29902a | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 410 | (void) input; |
| 411 | (void) input_length; |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 412 | return( PSA_ERROR_BAD_STATE ); |
| 413 | } |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 416 | static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation, |
| 417 | uint8_t *mac, |
| 418 | size_t mac_size ) |
| 419 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 420 | #if defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 421 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 422 | { |
| 423 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
| 424 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 425 | if( ret == 0 ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 426 | memcpy( mac, tmp, mac_size ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 427 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 428 | return( mbedtls_to_psa_error( ret ) ); |
| 429 | } |
| 430 | else |
| 431 | #endif /* BUILTIN_ALG_CMAC */ |
| 432 | #if defined(BUILTIN_ALG_HMAC) |
| 433 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 434 | { |
| 435 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 436 | mac, mac_size ) ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | #endif /* BUILTIN_ALG_HMAC */ |
| 440 | { |
| 441 | /* This shouldn't happen if `operation` was initialized by |
| 442 | * a setup function. */ |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 443 | (void) operation; |
| 444 | (void) mac; |
| 445 | (void) mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 446 | return( PSA_ERROR_BAD_STATE ); |
| 447 | } |
| 448 | } |
| 449 | |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 450 | static psa_status_t mac_sign_finish( |
| 451 | mbedtls_psa_mac_operation_t *operation, |
| 452 | uint8_t *mac, |
| 453 | size_t mac_size, |
| 454 | size_t *mac_length ) |
| 455 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 456 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 457 | |
| 458 | if( operation->alg == 0 ) |
| 459 | return( PSA_ERROR_BAD_STATE ); |
| 460 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 461 | status = mac_finish_internal( operation, mac, mac_size ); |
| 462 | |
| 463 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 464 | *mac_length = mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 465 | |
| 466 | return( status ); |
| 467 | } |
| 468 | |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 469 | static psa_status_t mac_verify_finish( |
| 470 | mbedtls_psa_mac_operation_t *operation, |
| 471 | const uint8_t *mac, |
| 472 | size_t mac_length ) |
| 473 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 474 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 475 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 476 | |
| 477 | if( operation->alg == 0 ) |
| 478 | return( PSA_ERROR_BAD_STATE ); |
| 479 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 480 | /* Consistency check: requested MAC length fits our local buffer */ |
| 481 | if( mac_length > sizeof( actual_mac ) ) |
| 482 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 483 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 484 | status = mac_finish_internal( operation, actual_mac, mac_length ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 485 | if( status != PSA_SUCCESS ) |
| 486 | goto cleanup; |
| 487 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 488 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 489 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 490 | |
| 491 | cleanup: |
| 492 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
| 493 | |
| 494 | return( status ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 495 | } |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 496 | #endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 497 | |
| 498 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 499 | psa_status_t mbedtls_psa_mac_compute( |
| 500 | const psa_key_attributes_t *attributes, |
| 501 | const uint8_t *key_buffer, |
| 502 | size_t key_buffer_size, |
| 503 | psa_algorithm_t alg, |
| 504 | const uint8_t *input, |
| 505 | size_t input_length, |
| 506 | uint8_t *mac, |
| 507 | size_t mac_size, |
| 508 | size_t *mac_length ) |
| 509 | { |
| 510 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 511 | input, input_length, |
| 512 | mac, mac_size, mac_length ) ); |
| 513 | } |
| 514 | |
| 515 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 516 | mbedtls_psa_mac_operation_t *operation, |
| 517 | const psa_key_attributes_t *attributes, |
| 518 | const uint8_t *key_buffer, |
| 519 | size_t key_buffer_size, |
| 520 | psa_algorithm_t alg ) |
| 521 | { |
Steven Cooreman | 9e15fb7 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 522 | return( mac_setup( operation, attributes, |
| 523 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | psa_status_t mbedtls_psa_mac_verify_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 | { |
Steven Cooreman | 9e15fb7 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 533 | return( mac_setup( operation, attributes, |
| 534 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | psa_status_t mbedtls_psa_mac_update( |
| 538 | mbedtls_psa_mac_operation_t *operation, |
| 539 | const uint8_t *input, |
| 540 | size_t input_length ) |
| 541 | { |
| 542 | return( mac_update( operation, input, input_length ) ); |
| 543 | } |
| 544 | |
| 545 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 546 | mbedtls_psa_mac_operation_t *operation, |
| 547 | uint8_t *mac, |
| 548 | size_t mac_size, |
| 549 | size_t *mac_length ) |
| 550 | { |
| 551 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 552 | } |
| 553 | |
| 554 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 555 | mbedtls_psa_mac_operation_t *operation, |
| 556 | const uint8_t *mac, |
| 557 | size_t mac_length ) |
| 558 | { |
| 559 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 560 | } |
| 561 | |
| 562 | psa_status_t mbedtls_psa_mac_abort( |
| 563 | mbedtls_psa_mac_operation_t *operation ) |
| 564 | { |
| 565 | return( mac_abort( operation ) ); |
| 566 | } |
| 567 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 568 | |
| 569 | /* |
| 570 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 571 | */ |
| 572 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 573 | |
| 574 | static int is_mac_accelerated( psa_algorithm_t alg ) |
| 575 | { |
| 576 | #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 577 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 578 | return( 1 ); |
| 579 | #endif |
| 580 | |
| 581 | switch( PSA_ALG_FULL_LENGTH_MAC( alg ) ) |
| 582 | { |
| 583 | #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
| 584 | case PSA_ALG_CMAC: |
| 585 | return( 1 ); |
| 586 | #endif |
| 587 | default: |
| 588 | return( 0 ); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 593 | const psa_key_attributes_t *attributes, |
| 594 | const uint8_t *key_buffer, |
| 595 | size_t key_buffer_size, |
| 596 | psa_algorithm_t alg, |
| 597 | const uint8_t *input, |
| 598 | size_t input_length, |
| 599 | uint8_t *mac, |
| 600 | size_t mac_size, |
| 601 | size_t *mac_length ) |
| 602 | { |
| 603 | if( is_mac_accelerated( alg ) ) |
| 604 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 605 | input, input_length, |
| 606 | mac, mac_size, mac_length ) ); |
| 607 | else |
| 608 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 609 | } |
| 610 | |
| 611 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 612 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 613 | const psa_key_attributes_t *attributes, |
| 614 | const uint8_t *key_buffer, |
| 615 | size_t key_buffer_size, |
| 616 | psa_algorithm_t alg ) |
| 617 | { |
| 618 | if( is_mac_accelerated( alg ) ) |
Steven Cooreman | 9e15fb7 | 2021-05-11 11:10:34 +0200 | [diff] [blame] | 619 | return( mac_setup( operation, attributes, |
| 620 | key_buffer, key_buffer_size, alg ) ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 621 | else |
| 622 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 623 | } |
| 624 | |
| 625 | psa_status_t mbedtls_transparent_test_driver_mac_verify_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_update( |
| 640 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 641 | const uint8_t *input, |
| 642 | size_t input_length ) |
| 643 | { |
| 644 | if( is_mac_accelerated( operation->alg ) ) |
| 645 | return( mac_update( operation, input, input_length ) ); |
| 646 | else |
| 647 | return( PSA_ERROR_BAD_STATE ); |
| 648 | } |
| 649 | |
| 650 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 651 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 652 | uint8_t *mac, |
| 653 | size_t mac_size, |
| 654 | size_t *mac_length ) |
| 655 | { |
| 656 | if( is_mac_accelerated( operation->alg ) ) |
| 657 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 658 | else |
| 659 | return( PSA_ERROR_BAD_STATE ); |
| 660 | } |
| 661 | |
| 662 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 663 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 664 | const uint8_t *mac, |
| 665 | size_t mac_length ) |
| 666 | { |
| 667 | if( is_mac_accelerated( operation->alg ) ) |
| 668 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 669 | else |
| 670 | return( PSA_ERROR_BAD_STATE ); |
| 671 | } |
| 672 | |
| 673 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
| 674 | mbedtls_transparent_test_driver_mac_operation_t *operation ) |
| 675 | { |
| 676 | return( mac_abort( operation ) ); |
| 677 | } |
| 678 | |
| 679 | psa_status_t mbedtls_opaque_test_driver_mac_compute( |
| 680 | const psa_key_attributes_t *attributes, |
| 681 | const uint8_t *key_buffer, |
| 682 | size_t key_buffer_size, |
| 683 | psa_algorithm_t alg, |
| 684 | const uint8_t *input, |
| 685 | size_t input_length, |
| 686 | uint8_t *mac, |
| 687 | size_t mac_size, |
| 688 | size_t *mac_length ) |
| 689 | { |
| 690 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 691 | (void) attributes; |
| 692 | (void) key_buffer; |
| 693 | (void) key_buffer_size; |
| 694 | (void) alg; |
| 695 | (void) input; |
| 696 | (void) input_length; |
| 697 | (void) mac; |
| 698 | (void) mac_size; |
| 699 | (void) mac_length; |
| 700 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 701 | } |
| 702 | |
| 703 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 704 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 705 | const psa_key_attributes_t *attributes, |
| 706 | const uint8_t *key_buffer, |
| 707 | size_t key_buffer_size, |
| 708 | psa_algorithm_t alg ) |
| 709 | { |
| 710 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 711 | (void) operation; |
| 712 | (void) attributes; |
| 713 | (void) key_buffer; |
| 714 | (void) key_buffer_size; |
| 715 | (void) alg; |
| 716 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 717 | } |
| 718 | |
| 719 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 720 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 721 | const psa_key_attributes_t *attributes, |
| 722 | const uint8_t *key_buffer, |
| 723 | size_t key_buffer_size, |
| 724 | psa_algorithm_t alg ) |
| 725 | { |
| 726 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 727 | (void) operation; |
| 728 | (void) attributes; |
| 729 | (void) key_buffer; |
| 730 | (void) key_buffer_size; |
| 731 | (void) alg; |
| 732 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 733 | } |
| 734 | |
| 735 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 736 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 737 | const uint8_t *input, |
| 738 | size_t input_length ) |
| 739 | { |
| 740 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 741 | (void) operation; |
| 742 | (void) input; |
| 743 | (void) input_length; |
| 744 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 745 | } |
| 746 | |
| 747 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 748 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 749 | uint8_t *mac, |
| 750 | size_t mac_size, |
| 751 | size_t *mac_length ) |
| 752 | { |
| 753 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 754 | (void) operation; |
| 755 | (void) mac; |
| 756 | (void) mac_size; |
| 757 | (void) mac_length; |
| 758 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 759 | } |
| 760 | |
| 761 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 762 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 763 | const uint8_t *mac, |
| 764 | size_t mac_length ) |
| 765 | { |
| 766 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 767 | (void) operation; |
| 768 | (void) mac; |
| 769 | (void) mac_length; |
| 770 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 771 | } |
| 772 | |
| 773 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
| 774 | mbedtls_opaque_test_driver_mac_operation_t *operation ) |
| 775 | { |
| 776 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 777 | (void) operation; |
| 778 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 779 | } |
| 780 | |
| 781 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 782 | |
| 783 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |