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