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 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 192 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 193 | defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 194 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 195 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 196 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
| 197 | static psa_status_t mac_init( |
| 198 | mbedtls_psa_mac_operation_t *operation, |
| 199 | psa_algorithm_t alg ) |
| 200 | { |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 202 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 203 | operation->alg = alg; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 204 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 205 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 206 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 207 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 208 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 209 | status = PSA_SUCCESS; |
| 210 | } |
| 211 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 212 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 213 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 214 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 215 | { |
| 216 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 217 | operation->ctx.hmac.alg = 0; |
| 218 | status = PSA_SUCCESS; |
| 219 | } |
| 220 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 221 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 222 | { |
Ronald Cron | 7a55deb | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 223 | (void) operation; |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 224 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if( status != PSA_SUCCESS ) |
| 228 | memset( operation, 0, sizeof( *operation ) ); |
| 229 | return( status ); |
| 230 | } |
| 231 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 232 | 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] | 233 | { |
| 234 | if( operation->alg == 0 ) |
| 235 | { |
| 236 | /* The object has (apparently) been initialized but it is not |
| 237 | * in use. It's ok to call abort on such an object, and there's |
| 238 | * nothing to do. */ |
| 239 | return( PSA_SUCCESS ); |
| 240 | } |
| 241 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 242 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 243 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 244 | { |
| 245 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 246 | } |
| 247 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 248 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 249 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 250 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 251 | { |
| 252 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
| 253 | } |
| 254 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 255 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 256 | { |
| 257 | /* Sanity check (shouldn't happen: operation->alg should |
| 258 | * always have been initialized to a valid value). */ |
| 259 | goto bad_state; |
| 260 | } |
| 261 | |
| 262 | operation->alg = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 263 | |
| 264 | return( PSA_SUCCESS ); |
| 265 | |
| 266 | bad_state: |
| 267 | /* If abort is called on an uninitialized object, we can't trust |
| 268 | * anything. Wipe the object in case it contains confidential data. |
| 269 | * This may result in a memory leak if a pointer gets overwritten, |
| 270 | * but it's too late to do anything about this. */ |
| 271 | memset( operation, 0, sizeof( *operation ) ); |
| 272 | return( PSA_ERROR_BAD_STATE ); |
| 273 | } |
| 274 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 275 | static psa_status_t psa_mac_setup( mbedtls_psa_mac_operation_t *operation, |
| 276 | const psa_key_attributes_t *attributes, |
| 277 | const uint8_t *key_buffer, |
| 278 | size_t key_buffer_size, |
| 279 | psa_algorithm_t alg ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 280 | { |
| 281 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 282 | |
| 283 | /* A context must be freshly initialized before it can be set up. */ |
| 284 | if( operation->alg != 0 ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 285 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 286 | |
| 287 | status = mac_init( operation, alg ); |
| 288 | if( status != PSA_SUCCESS ) |
| 289 | return( status ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 290 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 291 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 292 | if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC ) |
| 293 | { |
Steven Cooreman | dcd0811 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 294 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 295 | * attributes, and previously validated by the core on key import. */ |
| 296 | (void) key_buffer_size; |
| 297 | status = cmac_setup( operation, attributes, key_buffer ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 298 | } |
| 299 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 300 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 301 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 302 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 303 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 304 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
| 305 | key_buffer, |
| 306 | key_buffer_size, |
| 307 | PSA_ALG_HMAC_GET_HASH( alg ) ); |
| 308 | } |
| 309 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 310 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 311 | { |
Steven Cooreman | b29902a | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 312 | (void) attributes; |
| 313 | (void) key_buffer; |
| 314 | (void) key_buffer_size; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 315 | status = PSA_ERROR_NOT_SUPPORTED; |
| 316 | } |
| 317 | |
Steven Cooreman | a4638e7 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 318 | if( status != PSA_SUCCESS ) |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 319 | mbedtls_psa_mac_abort( operation ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 320 | |
| 321 | return( status ); |
| 322 | } |
| 323 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 324 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 325 | mbedtls_psa_mac_operation_t *operation, |
| 326 | const psa_key_attributes_t *attributes, |
| 327 | const uint8_t *key_buffer, |
| 328 | size_t key_buffer_size, |
| 329 | psa_algorithm_t alg ) |
| 330 | { |
| 331 | return( psa_mac_setup( operation, attributes, |
| 332 | key_buffer, key_buffer_size, alg ) ); |
| 333 | } |
| 334 | |
| 335 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 336 | mbedtls_psa_mac_operation_t *operation, |
| 337 | const psa_key_attributes_t *attributes, |
| 338 | const uint8_t *key_buffer, |
| 339 | size_t key_buffer_size, |
| 340 | psa_algorithm_t alg ) |
| 341 | { |
| 342 | return( psa_mac_setup( operation, attributes, |
| 343 | key_buffer, key_buffer_size, alg ) ); |
| 344 | } |
| 345 | |
| 346 | psa_status_t mbedtls_psa_mac_update( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 347 | mbedtls_psa_mac_operation_t *operation, |
| 348 | const uint8_t *input, |
| 349 | size_t input_length ) |
| 350 | { |
Steven Cooreman | a4638e7 | 2021-04-29 16:24:36 +0200 | [diff] [blame] | 351 | if( operation->alg == 0 ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 352 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 353 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 354 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 355 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 356 | { |
| 357 | return( mbedtls_to_psa_error( |
| 358 | mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 359 | input, input_length ) ) ); |
| 360 | } |
| 361 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 362 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 363 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 364 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 365 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 366 | return( psa_hmac_update_internal( &operation->ctx.hmac, |
| 367 | input, input_length ) ); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 368 | } |
| 369 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 370 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 371 | { |
| 372 | /* This shouldn't happen if `operation` was initialized by |
| 373 | * a setup function. */ |
Steven Cooreman | b29902a | 2021-05-10 09:47:05 +0200 | [diff] [blame] | 374 | (void) input; |
| 375 | (void) input_length; |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 376 | return( PSA_ERROR_BAD_STATE ); |
| 377 | } |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 380 | static psa_status_t psa_mac_finish_internal( |
| 381 | mbedtls_psa_mac_operation_t *operation, |
| 382 | uint8_t *mac, size_t mac_size ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 383 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 384 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 385 | if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 386 | { |
| 387 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
| 388 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 389 | if( ret == 0 ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 390 | memcpy( mac, tmp, mac_size ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 391 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 392 | return( mbedtls_to_psa_error( ret ) ); |
| 393 | } |
| 394 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 395 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
| 396 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 397 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 398 | { |
| 399 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 400 | mac, mac_size ) ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 401 | } |
| 402 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 403 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 404 | { |
| 405 | /* This shouldn't happen if `operation` was initialized by |
| 406 | * a setup function. */ |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 407 | (void) operation; |
| 408 | (void) mac; |
| 409 | (void) mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 410 | return( PSA_ERROR_BAD_STATE ); |
| 411 | } |
| 412 | } |
| 413 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 414 | psa_status_t mbedtls_psa_mac_sign_finish( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 415 | mbedtls_psa_mac_operation_t *operation, |
| 416 | uint8_t *mac, |
| 417 | size_t mac_size, |
| 418 | size_t *mac_length ) |
| 419 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 420 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 421 | |
| 422 | if( operation->alg == 0 ) |
| 423 | return( PSA_ERROR_BAD_STATE ); |
| 424 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 425 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 426 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 427 | *mac_length = mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 428 | |
| 429 | return( status ); |
| 430 | } |
| 431 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 432 | psa_status_t mbedtls_psa_mac_verify_finish( |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 433 | mbedtls_psa_mac_operation_t *operation, |
| 434 | const uint8_t *mac, |
| 435 | size_t mac_length ) |
| 436 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 437 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 438 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 439 | |
| 440 | if( operation->alg == 0 ) |
| 441 | return( PSA_ERROR_BAD_STATE ); |
| 442 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 443 | /* Consistency check: requested MAC length fits our local buffer */ |
| 444 | if( mac_length > sizeof( actual_mac ) ) |
| 445 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 446 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 447 | status = psa_mac_finish_internal( operation, actual_mac, mac_length ); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 448 | if( status != PSA_SUCCESS ) |
| 449 | goto cleanup; |
| 450 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 451 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 452 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 453 | |
| 454 | cleanup: |
| 455 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
| 456 | |
| 457 | return( status ); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 458 | } |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 459 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 460 | psa_status_t mbedtls_psa_mac_compute( |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 461 | const psa_key_attributes_t *attributes, |
| 462 | const uint8_t *key_buffer, |
| 463 | size_t key_buffer_size, |
| 464 | psa_algorithm_t alg, |
| 465 | const uint8_t *input, |
| 466 | size_t input_length, |
| 467 | uint8_t *mac, |
| 468 | size_t mac_size, |
| 469 | size_t *mac_length ) |
| 470 | { |
| 471 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 472 | mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT; |
| 473 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 474 | status = psa_mac_setup( &operation, |
| 475 | attributes, key_buffer, key_buffer_size, |
| 476 | alg ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 477 | if( status != PSA_SUCCESS ) |
| 478 | goto exit; |
| 479 | |
| 480 | if( input_length > 0 ) |
| 481 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 482 | status = mbedtls_psa_mac_update( &operation, input, input_length ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 483 | if( status != PSA_SUCCESS ) |
| 484 | goto exit; |
| 485 | } |
| 486 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 487 | status = psa_mac_finish_internal( &operation, mac, mac_size ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 488 | if( status == PSA_SUCCESS ) |
| 489 | *mac_length = mac_size; |
| 490 | |
| 491 | exit: |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 492 | mbedtls_psa_mac_abort( &operation ); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 493 | |
| 494 | return( status ); |
| 495 | } |
| 496 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 497 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 498 | |
| 499 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |