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 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 33 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 34 | static psa_status_t psa_hmac_abort_internal( |
| 35 | mbedtls_psa_hmac_operation_t *hmac ) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 36 | { |
| 37 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
| 38 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 39 | } |
| 40 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 41 | static psa_status_t psa_hmac_setup_internal( |
| 42 | mbedtls_psa_hmac_operation_t *hmac, |
| 43 | const uint8_t *key, |
| 44 | size_t key_length, |
| 45 | psa_algorithm_t hash_alg ) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 46 | { |
| 47 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
| 48 | size_t i; |
| 49 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Gilles Peskine | e7be73d | 2021-09-22 14:44:28 +0200 | [diff] [blame] | 50 | size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg ); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 51 | psa_status_t status; |
| 52 | |
| 53 | hmac->alg = hash_alg; |
| 54 | |
| 55 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 56 | * overflow below. This should never trigger if the hash algorithm |
| 57 | * is implemented correctly. */ |
| 58 | /* The size checks against the ipad and opad buffers cannot be written |
| 59 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 60 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 61 | if( block_size > sizeof( ipad ) ) |
| 62 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 63 | if( block_size > sizeof( hmac->opad ) ) |
| 64 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 65 | if( block_size < hash_size ) |
| 66 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 67 | |
| 68 | if( key_length > block_size ) |
| 69 | { |
| 70 | status = psa_hash_compute( hash_alg, key, key_length, |
| 71 | ipad, sizeof( ipad ), &key_length ); |
| 72 | if( status != PSA_SUCCESS ) |
| 73 | goto cleanup; |
| 74 | } |
| 75 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 76 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 77 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 78 | * an invalid pointer which would make the behavior undefined. */ |
| 79 | else if( key_length != 0 ) |
| 80 | memcpy( ipad, key, key_length ); |
| 81 | |
| 82 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 83 | * to create the ipad value. */ |
| 84 | for( i = 0; i < key_length; i++ ) |
| 85 | ipad[i] ^= 0x36; |
| 86 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 87 | |
| 88 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 89 | * and filling the rest of opad with the requisite constant. */ |
| 90 | for( i = 0; i < key_length; i++ ) |
| 91 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 92 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
| 93 | |
| 94 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 95 | if( status != PSA_SUCCESS ) |
| 96 | goto cleanup; |
| 97 | |
| 98 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
| 99 | |
| 100 | cleanup: |
| 101 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
| 102 | |
| 103 | return( status ); |
| 104 | } |
| 105 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 106 | static psa_status_t psa_hmac_update_internal( |
| 107 | mbedtls_psa_hmac_operation_t *hmac, |
| 108 | const uint8_t *data, |
| 109 | size_t data_length ) |
Steven Cooreman | 4fdf060 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 110 | { |
| 111 | return( psa_hash_update( &hmac->hash_ctx, data, data_length ) ); |
| 112 | } |
| 113 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 114 | static psa_status_t psa_hmac_finish_internal( |
| 115 | mbedtls_psa_hmac_operation_t *hmac, |
| 116 | uint8_t *mac, |
| 117 | size_t mac_size ) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 118 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 119 | uint8_t tmp[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 120 | psa_algorithm_t hash_alg = hmac->alg; |
| 121 | size_t hash_size = 0; |
Gilles Peskine | e7be73d | 2021-09-22 14:44:28 +0200 | [diff] [blame] | 122 | size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg ); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 123 | psa_status_t status; |
| 124 | |
| 125 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 126 | if( status != PSA_SUCCESS ) |
| 127 | return( status ); |
| 128 | /* From here on, tmp needs to be wiped. */ |
| 129 | |
| 130 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 131 | if( status != PSA_SUCCESS ) |
| 132 | goto exit; |
| 133 | |
| 134 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 135 | if( status != PSA_SUCCESS ) |
| 136 | goto exit; |
| 137 | |
| 138 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 139 | if( status != PSA_SUCCESS ) |
| 140 | goto exit; |
| 141 | |
| 142 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 143 | if( status != PSA_SUCCESS ) |
| 144 | goto exit; |
| 145 | |
| 146 | memcpy( mac, tmp, mac_size ); |
| 147 | |
| 148 | exit: |
| 149 | mbedtls_platform_zeroize( tmp, hash_size ); |
| 150 | return( status ); |
| 151 | } |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 152 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 153 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 154 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 155 | static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation, |
| 156 | const psa_key_attributes_t *attributes, |
Steven Cooreman | dcd0811 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 157 | const uint8_t *key_buffer ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 158 | { |
| 159 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 0c23965 | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 160 | |
| 161 | #if defined(PSA_WANT_KEY_TYPE_DES) |
| 162 | /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept |
| 163 | * to do CMAC with pure DES, so return NOT_SUPPORTED here. */ |
| 164 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES && |
| 165 | ( psa_get_key_bits( attributes ) == 64 || |
| 166 | psa_get_key_bits( attributes ) == 128 ) ) |
| 167 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 168 | #endif |
| 169 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 170 | const mbedtls_cipher_info_t * cipher_info = |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 171 | mbedtls_cipher_info_from_psa( |
| 172 | PSA_ALG_CMAC, |
| 173 | psa_get_key_type( attributes ), |
| 174 | psa_get_key_bits( attributes ), |
| 175 | NULL ); |
| 176 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 177 | if( cipher_info == NULL ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 178 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 179 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 180 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 181 | if( ret != 0 ) |
| 182 | goto exit; |
| 183 | |
| 184 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
| 185 | key_buffer, |
| 186 | psa_get_key_bits( attributes ) ); |
| 187 | exit: |
| 188 | return( mbedtls_to_psa_error( ret ) ); |
| 189 | } |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 190 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 191 | |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 192 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 193 | * software driver or the test driver requires it. */ |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 195 | defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 196 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 197 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 198 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
| 199 | static psa_status_t mac_init( |
| 200 | mbedtls_psa_mac_operation_t *operation, |
| 201 | psa_algorithm_t alg ) |
| 202 | { |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 203 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 204 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 205 | operation->alg = alg; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 206 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 207 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 208 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 209 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 210 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 211 | status = PSA_SUCCESS; |
| 212 | } |
| 213 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 214 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 215 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 216 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 217 | { |
| 218 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 219 | operation->ctx.hmac.alg = 0; |
| 220 | status = PSA_SUCCESS; |
| 221 | } |
| 222 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 223 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 224 | { |
Ronald Cron | 7a55deb | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 225 | (void) operation; |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 226 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if( status != PSA_SUCCESS ) |
| 230 | memset( operation, 0, sizeof( *operation ) ); |
| 231 | return( status ); |
| 232 | } |
| 233 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 234 | psa_status_t mbedtls_psa_mac_abort( mbedtls_psa_mac_operation_t *operation ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 235 | { |
| 236 | if( operation->alg == 0 ) |
| 237 | { |
| 238 | /* The object has (apparently) been initialized but it is not |
| 239 | * in use. It's ok to call abort on such an object, and there's |
| 240 | * nothing to do. */ |
| 241 | return( PSA_SUCCESS ); |
| 242 | } |
| 243 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 244 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 245 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 246 | { |
| 247 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 248 | } |
| 249 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 250 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 251 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 252 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 253 | { |
| 254 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
| 255 | } |
| 256 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 257 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 258 | { |
| 259 | /* Sanity check (shouldn't happen: operation->alg should |
| 260 | * always have been initialized to a valid value). */ |
| 261 | goto bad_state; |
| 262 | } |
| 263 | |
| 264 | operation->alg = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 265 | |
| 266 | return( PSA_SUCCESS ); |
| 267 | |
| 268 | bad_state: |
| 269 | /* If abort is called on an uninitialized object, we can't trust |
| 270 | * anything. Wipe the object in case it contains confidential data. |
| 271 | * This may result in a memory leak if a pointer gets overwritten, |
| 272 | * but it's too late to do anything about this. */ |
| 273 | memset( operation, 0, sizeof( *operation ) ); |
| 274 | return( PSA_ERROR_BAD_STATE ); |
| 275 | } |
| 276 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 277 | static psa_status_t psa_mac_setup( mbedtls_psa_mac_operation_t *operation, |
| 278 | const psa_key_attributes_t *attributes, |
| 279 | const uint8_t *key_buffer, |
| 280 | size_t key_buffer_size, |
| 281 | psa_algorithm_t alg ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 282 | { |
| 283 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 284 | |
| 285 | /* A context must be freshly initialized before it can be set up. */ |
| 286 | if( operation->alg != 0 ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 287 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 288 | |
| 289 | status = mac_init( operation, alg ); |
| 290 | if( status != PSA_SUCCESS ) |
| 291 | return( status ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 292 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 293 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 294 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
| 295 | { |
Steven Cooreman | dcd0811 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 296 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 297 | * attributes, and previously validated by the core on key import. */ |
| 298 | (void) key_buffer_size; |
| 299 | status = cmac_setup( operation, attributes, key_buffer ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 300 | } |
| 301 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 302 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 303 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 304 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 305 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 306 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 307 | key_buffer, |
| 308 | key_buffer_size, |
| 309 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
| 310 | } |
| 311 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 312 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 313 | { |
Steven Cooreman | b29902a | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 314 | (void) attributes; |
| 315 | (void) key_buffer; |
| 316 | (void) key_buffer_size; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 317 | status = PSA_ERROR_NOT_SUPPORTED; |
| 318 | } |
| 319 | |
Steven Cooreman | a4638e7 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 320 | if( status != PSA_SUCCESS ) |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 321 | mbedtls_psa_mac_abort( operation ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 322 | |
| 323 | return( status ); |
| 324 | } |
| 325 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 326 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 327 | mbedtls_psa_mac_operation_t *operation, |
| 328 | const psa_key_attributes_t *attributes, |
| 329 | const uint8_t *key_buffer, |
| 330 | size_t key_buffer_size, |
| 331 | psa_algorithm_t alg ) |
| 332 | { |
| 333 | return( psa_mac_setup( operation, attributes, |
| 334 | key_buffer, key_buffer_size, alg ) ); |
| 335 | } |
| 336 | |
| 337 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 338 | mbedtls_psa_mac_operation_t *operation, |
| 339 | const psa_key_attributes_t *attributes, |
| 340 | const uint8_t *key_buffer, |
| 341 | size_t key_buffer_size, |
| 342 | psa_algorithm_t alg ) |
| 343 | { |
| 344 | return( psa_mac_setup( operation, attributes, |
| 345 | key_buffer, key_buffer_size, alg ) ); |
| 346 | } |
| 347 | |
| 348 | psa_status_t mbedtls_psa_mac_update( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 349 | mbedtls_psa_mac_operation_t *operation, |
| 350 | const uint8_t *input, |
| 351 | size_t input_length ) |
| 352 | { |
Steven Cooreman | a4638e7 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 353 | if( operation->alg == 0 ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 354 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 355 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 357 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 358 | { |
| 359 | return( mbedtls_to_psa_error( |
| 360 | mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 361 | input, input_length ) ) ); |
| 362 | } |
| 363 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 364 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 365 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 366 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 367 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 368 | return( psa_hmac_update_internal( &operation->ctx.hmac, |
| 369 | input, input_length ) ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 370 | } |
| 371 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 372 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 373 | { |
| 374 | /* This shouldn't happen if `operation` was initialized by |
| 375 | * a setup function. */ |
Steven Cooreman | b29902a | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 376 | (void) input; |
| 377 | (void) input_length; |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 378 | return( PSA_ERROR_BAD_STATE ); |
| 379 | } |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 382 | static psa_status_t psa_mac_finish_internal( |
| 383 | mbedtls_psa_mac_operation_t *operation, |
| 384 | uint8_t *mac, size_t mac_size ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 385 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 386 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 387 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 388 | { |
| 389 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
| 390 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 391 | if( ret == 0 ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 392 | memcpy( mac, tmp, mac_size ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 393 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 394 | return( mbedtls_to_psa_error( ret ) ); |
| 395 | } |
| 396 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 399 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 400 | { |
| 401 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 402 | mac, mac_size ) ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 403 | } |
| 404 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 405 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 406 | { |
| 407 | /* This shouldn't happen if `operation` was initialized by |
| 408 | * a setup function. */ |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 409 | (void) operation; |
| 410 | (void) mac; |
| 411 | (void) mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 412 | return( PSA_ERROR_BAD_STATE ); |
| 413 | } |
| 414 | } |
| 415 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 416 | psa_status_t mbedtls_psa_mac_sign_finish( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 417 | mbedtls_psa_mac_operation_t *operation, |
| 418 | uint8_t *mac, |
| 419 | size_t mac_size, |
| 420 | size_t *mac_length ) |
| 421 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 422 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 423 | |
| 424 | if( operation->alg == 0 ) |
| 425 | return( PSA_ERROR_BAD_STATE ); |
| 426 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 427 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 428 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 429 | *mac_length = mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 430 | |
| 431 | return( status ); |
| 432 | } |
| 433 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 434 | psa_status_t mbedtls_psa_mac_verify_finish( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 435 | mbedtls_psa_mac_operation_t *operation, |
| 436 | const uint8_t *mac, |
| 437 | size_t mac_length ) |
| 438 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 439 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 440 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 441 | |
| 442 | if( operation->alg == 0 ) |
| 443 | return( PSA_ERROR_BAD_STATE ); |
| 444 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 445 | /* Consistency check: requested MAC length fits our local buffer */ |
| 446 | if( mac_length > sizeof( actual_mac ) ) |
| 447 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 448 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 449 | status = psa_mac_finish_internal( operation, actual_mac, mac_length ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 450 | if( status != PSA_SUCCESS ) |
| 451 | goto cleanup; |
| 452 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 453 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 454 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 455 | |
| 456 | cleanup: |
| 457 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
| 458 | |
| 459 | return( status ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 460 | } |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 461 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 462 | psa_status_t mbedtls_psa_mac_compute( |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 463 | const psa_key_attributes_t *attributes, |
| 464 | const uint8_t *key_buffer, |
| 465 | size_t key_buffer_size, |
| 466 | psa_algorithm_t alg, |
| 467 | const uint8_t *input, |
| 468 | size_t input_length, |
| 469 | uint8_t *mac, |
| 470 | size_t mac_size, |
| 471 | size_t *mac_length ) |
| 472 | { |
| 473 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 474 | mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT; |
| 475 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 476 | status = psa_mac_setup( &operation, |
| 477 | attributes, key_buffer, key_buffer_size, |
| 478 | alg ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 479 | if( status != PSA_SUCCESS ) |
| 480 | goto exit; |
| 481 | |
| 482 | if( input_length > 0 ) |
| 483 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 484 | status = mbedtls_psa_mac_update( &operation, input, input_length ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 485 | if( status != PSA_SUCCESS ) |
| 486 | goto exit; |
| 487 | } |
| 488 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 489 | status = psa_mac_finish_internal( &operation, mac, mac_size ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 490 | if( status == PSA_SUCCESS ) |
| 491 | *mac_length = mac_size; |
| 492 | |
| 493 | exit: |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 494 | mbedtls_psa_mac_abort( &operation ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 495 | |
| 496 | return( status ); |
| 497 | } |
| 498 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 499 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 500 | |
| 501 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |