blob: d771e23e1b2d5be74b0d6bd607e339eb5a493aae [file] [log] [blame]
Steven Cooremand13a70f2021-03-19 15:24:23 +01001/*
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 Rodgman16304472022-11-02 09:25:38 +000027#include "psa_crypto_cipher.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010028#include "psa_crypto_mac.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include <mbedtls/md.h>
Steven Cooremand13a70f2021-03-19 15:24:23 +010030
31#include <mbedtls/error.h>
32#include <string.h>
33
Ronald Cron0266cfe2021-03-13 18:50:11 +010034#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana6df6042021-04-29 19:32:25 +020035static psa_status_t psa_hmac_abort_internal(
36 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman82c66b62021-03-19 17:39:17 +010037{
38 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
39 return( psa_hash_abort( &hmac->hash_ctx ) );
40}
41
Steven Cooremana6df6042021-04-29 19:32:25 +020042static 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 Cooreman82c66b62021-03-19 17:39:17 +010047{
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 Peskinee7be73d2021-09-22 14:44:28 +020051 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +010052 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
101cleanup:
102 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
103
104 return( status );
105}
106
Steven Cooremana6df6042021-04-29 19:32:25 +0200107static 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 Cooreman4fdf0602021-03-22 12:21:10 +0100111{
112 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
113}
114
Steven Cooremana6df6042021-04-29 19:32:25 +0200115static psa_status_t psa_hmac_finish_internal(
116 mbedtls_psa_hmac_operation_t *hmac,
117 uint8_t *mac,
118 size_t mac_size )
Steven Cooreman82c66b62021-03-19 17:39:17 +0100119{
Ronald Cron69a63422021-10-18 09:47:58 +0200120 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman82c66b62021-03-19 17:39:17 +0100121 psa_algorithm_t hash_alg = hmac->alg;
122 size_t hash_size = 0;
Gilles Peskinee7be73d2021-09-22 14:44:28 +0200123 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +0100124 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
149exit:
150 mbedtls_platform_zeroize( tmp, hash_size );
151 return( status );
152}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100153#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100154
Ronald Cron0266cfe2021-03-13 18:50:11 +0100155#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100156static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
157 const psa_key_attributes_t *attributes,
Steven Cooremandcd08112021-05-06 18:00:37 +0200158 const uint8_t *key_buffer )
Steven Cooremane6804192021-03-19 18:28:56 +0100159{
160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman0c239652021-05-07 17:27:27 +0200161
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 Cooreman094a77e2021-05-06 17:58:36 +0200171 const mbedtls_cipher_info_t * cipher_info =
Steven Cooremane6804192021-03-19 18:28:56 +0100172 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 Cooreman094a77e2021-05-06 17:58:36 +0200178 if( cipher_info == NULL )
Steven Cooremane6804192021-03-19 18:28:56 +0100179 return( PSA_ERROR_NOT_SUPPORTED );
180
Steven Cooreman094a77e2021-05-06 17:58:36 +0200181 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooremane6804192021-03-19 18:28:56 +0100182 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 ) );
188exit:
189 return( mbedtls_to_psa_error( ret ) );
190}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100191#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100192
Ronald Cron0266cfe2021-03-13 18:50:11 +0100193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
194 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman02865f52021-05-07 15:55:27 +0200195
Steven Cooremane6804192021-03-19 18:28:56 +0100196/* 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. */
198static psa_status_t mac_init(
199 mbedtls_psa_mac_operation_t *operation,
200 psa_algorithm_t alg )
201{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100203
Steven Cooreman72f736a2021-05-07 14:14:37 +0200204 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100205
Ronald Cron0266cfe2021-03-13 18:50:11 +0100206#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200207 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100208 {
Steven Cooremane6804192021-03-19 18:28:56 +0100209 mbedtls_cipher_init( &operation->ctx.cmac );
210 status = PSA_SUCCESS;
211 }
212 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100213#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
214#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100215 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 Cron0266cfe2021-03-13 18:50:11 +0100222#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100223 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200224 (void) operation;
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200225 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100226 }
227
228 if( status != PSA_SUCCESS )
229 memset( operation, 0, sizeof( *operation ) );
230 return( status );
231}
232
Ronald Cron0266cfe2021-03-13 18:50:11 +0100233psa_status_t mbedtls_psa_mac_abort( mbedtls_psa_mac_operation_t *operation )
Steven Cooremane6804192021-03-19 18:28:56 +0100234{
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 Cron0266cfe2021-03-13 18:50:11 +0100243#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200244 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100245 {
246 mbedtls_cipher_free( &operation->ctx.cmac );
247 }
248 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100249#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
250#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100251 if( PSA_ALG_IS_HMAC( operation->alg ) )
252 {
253 psa_hmac_abort_internal( &operation->ctx.hmac );
254 }
255 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100256#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100257 {
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 Cooremane6804192021-03-19 18:28:56 +0100264
265 return( PSA_SUCCESS );
266
267bad_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 Cron0266cfe2021-03-13 18:50:11 +0100276static 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 Cooremane6804192021-03-19 18:28:56 +0100281{
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 Cooremane6804192021-03-19 18:28:56 +0100286 return( PSA_ERROR_BAD_STATE );
Steven Cooremane6804192021-03-19 18:28:56 +0100287
288 status = mac_init( operation, alg );
289 if( status != PSA_SUCCESS )
290 return( status );
Steven Cooremane6804192021-03-19 18:28:56 +0100291
Ronald Cron0266cfe2021-03-13 18:50:11 +0100292#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100293 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
294 {
Steven Cooremandcd08112021-05-06 18:00:37 +0200295 /* 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 Cooremane6804192021-03-19 18:28:56 +0100299 }
300 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100301#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
302#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100303 if( PSA_ALG_IS_HMAC( alg ) )
304 {
Steven Cooremane6804192021-03-19 18:28:56 +0100305 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 Cron0266cfe2021-03-13 18:50:11 +0100311#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100312 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200313 (void) attributes;
314 (void) key_buffer;
315 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100316 status = PSA_ERROR_NOT_SUPPORTED;
317 }
318
Steven Cooremana4638e72021-04-29 16:24:36 +0200319 if( status != PSA_SUCCESS )
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320 mbedtls_psa_mac_abort( operation );
Steven Cooremane6804192021-03-19 18:28:56 +0100321
322 return( status );
323}
324
Ronald Cron0266cfe2021-03-13 18:50:11 +0100325psa_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
336psa_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
347psa_status_t mbedtls_psa_mac_update(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100348 mbedtls_psa_mac_operation_t *operation,
349 const uint8_t *input,
350 size_t input_length )
351{
Steven Cooremana4638e72021-04-29 16:24:36 +0200352 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100353 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100354
Ronald Cron0266cfe2021-03-13 18:50:11 +0100355#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200356 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100357 {
358 return( mbedtls_to_psa_error(
359 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
360 input, input_length ) ) );
361 }
362 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100363#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
364#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100365 if( PSA_ALG_IS_HMAC( operation->alg ) )
366 {
Steven Cooremana6df6042021-04-29 19:32:25 +0200367 return( psa_hmac_update_internal( &operation->ctx.hmac,
368 input, input_length ) );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100369 }
370 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100371#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100372 {
373 /* This shouldn't happen if `operation` was initialized by
374 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200375 (void) input;
376 (void) input_length;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100377 return( PSA_ERROR_BAD_STATE );
378 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100379}
380
Ronald Cron0266cfe2021-03-13 18:50:11 +0100381static psa_status_t psa_mac_finish_internal(
382 mbedtls_psa_mac_operation_t *operation,
383 uint8_t *mac, size_t mac_size )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100384{
Ronald Cron0266cfe2021-03-13 18:50:11 +0100385#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200386 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100387 {
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 Cooreman72f736a2021-05-07 14:14:37 +0200391 memcpy( mac, tmp, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100392 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
393 return( mbedtls_to_psa_error( ret ) );
394 }
395 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100396#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
397#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana5b860a2021-03-19 19:04:39 +0100398 if( PSA_ALG_IS_HMAC( operation->alg ) )
399 {
400 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200401 mac, mac_size ) );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100402 }
403 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100404#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremana5b860a2021-03-19 19:04:39 +0100405 {
406 /* This shouldn't happen if `operation` was initialized by
407 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200408 (void) operation;
409 (void) mac;
410 (void) mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100411 return( PSA_ERROR_BAD_STATE );
412 }
413}
414
Ronald Cron0266cfe2021-03-13 18:50:11 +0100415psa_status_t mbedtls_psa_mac_sign_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100416 mbedtls_psa_mac_operation_t *operation,
417 uint8_t *mac,
418 size_t mac_size,
419 size_t *mac_length )
420{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100422
423 if( operation->alg == 0 )
424 return( PSA_ERROR_BAD_STATE );
425
Ronald Cron0266cfe2021-03-13 18:50:11 +0100426 status = psa_mac_finish_internal( operation, mac, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100427 if( status == PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200428 *mac_length = mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100429
430 return( status );
431}
432
Ronald Cron0266cfe2021-03-13 18:50:11 +0100433psa_status_t mbedtls_psa_mac_verify_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100434 mbedtls_psa_mac_operation_t *operation,
435 const uint8_t *mac,
436 size_t mac_length )
437{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100438 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100440
441 if( operation->alg == 0 )
442 return( PSA_ERROR_BAD_STATE );
443
Steven Cooreman72f736a2021-05-07 14:14:37 +0200444 /* Consistency check: requested MAC length fits our local buffer */
445 if( mac_length > sizeof( actual_mac ) )
446 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100447
Ronald Cron0266cfe2021-03-13 18:50:11 +0100448 status = psa_mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100449 if( status != PSA_SUCCESS )
450 goto cleanup;
451
Steven Cooreman36876a02021-04-29 16:37:59 +0200452 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100453 status = PSA_ERROR_INVALID_SIGNATURE;
454
455cleanup:
456 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
457
458 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100459}
Ronald Cron76be3e02021-06-17 17:34:43 +0200460
Ronald Cron0266cfe2021-03-13 18:50:11 +0100461psa_status_t mbedtls_psa_mac_compute(
Ronald Cron76be3e02021-06-17 17:34:43 +0200462 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 Cron0266cfe2021-03-13 18:50:11 +0100475 status = psa_mac_setup( &operation,
476 attributes, key_buffer, key_buffer_size,
477 alg );
Ronald Cron76be3e02021-06-17 17:34:43 +0200478 if( status != PSA_SUCCESS )
479 goto exit;
480
481 if( input_length > 0 )
482 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100483 status = mbedtls_psa_mac_update( &operation, input, input_length );
Ronald Cron76be3e02021-06-17 17:34:43 +0200484 if( status != PSA_SUCCESS )
485 goto exit;
486 }
487
Ronald Cron0266cfe2021-03-13 18:50:11 +0100488 status = psa_mac_finish_internal( &operation, mac, mac_size );
Ronald Cron76be3e02021-06-17 17:34:43 +0200489 if( status == PSA_SUCCESS )
490 *mac_length = mac_size;
491
492exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100493 mbedtls_psa_mac_abort( &operation );
Ronald Cron76be3e02021-06-17 17:34:43 +0200494
495 return( status );
496}
497
Ronald Cron0266cfe2021-03-13 18:50:11 +0100498#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100499
500#endif /* MBEDTLS_PSA_CRYPTO_C */