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