blob: dcf065a672651e55c7ecd7317ec1cc7a893f491d [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"
27#include "psa_crypto_mac.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010028#include <mbedtls/md.h>
Steven Cooremand13a70f2021-03-19 15:24:23 +010029
30#include <mbedtls/error.h>
31#include <string.h>
32
Ronald Cron0266cfe2021-03-13 18:50:11 +010033#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana6df6042021-04-29 19:32:25 +020034static psa_status_t psa_hmac_abort_internal(
35 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman82c66b62021-03-19 17:39:17 +010036{
37 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
38 return( psa_hash_abort( &hmac->hash_ctx ) );
39}
40
Steven Cooremana6df6042021-04-29 19:32:25 +020041static 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 Cooreman82c66b62021-03-19 17:39:17 +010046{
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 Peskinee7be73d2021-09-22 14:44:28 +020050 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +010051 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
100cleanup:
101 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
102
103 return( status );
104}
105
Steven Cooremana6df6042021-04-29 19:32:25 +0200106static 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 Cooreman4fdf0602021-03-22 12:21:10 +0100110{
111 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
112}
113
Steven Cooremana6df6042021-04-29 19:32:25 +0200114static psa_status_t psa_hmac_finish_internal(
115 mbedtls_psa_hmac_operation_t *hmac,
116 uint8_t *mac,
117 size_t mac_size )
Steven Cooreman82c66b62021-03-19 17:39:17 +0100118{
Ronald Cron69a63422021-10-18 09:47:58 +0200119 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman82c66b62021-03-19 17:39:17 +0100120 psa_algorithm_t hash_alg = hmac->alg;
121 size_t hash_size = 0;
Gilles Peskinee7be73d2021-09-22 14:44:28 +0200122 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +0100123 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
148exit:
149 mbedtls_platform_zeroize( tmp, hash_size );
150 return( status );
151}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100152#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100153
Ronald Cron0266cfe2021-03-13 18:50:11 +0100154#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100155static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
156 const psa_key_attributes_t *attributes,
Steven Cooremandcd08112021-05-06 18:00:37 +0200157 const uint8_t *key_buffer )
Steven Cooremane6804192021-03-19 18:28:56 +0100158{
159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman0c239652021-05-07 17:27:27 +0200160
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 Cooreman094a77e2021-05-06 17:58:36 +0200170 const mbedtls_cipher_info_t * cipher_info =
Steven Cooremane6804192021-03-19 18:28:56 +0100171 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 Cooreman094a77e2021-05-06 17:58:36 +0200177 if( cipher_info == NULL )
Steven Cooremane6804192021-03-19 18:28:56 +0100178 return( PSA_ERROR_NOT_SUPPORTED );
179
Steven Cooreman094a77e2021-05-06 17:58:36 +0200180 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooremane6804192021-03-19 18:28:56 +0100181 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 ) );
187exit:
188 return( mbedtls_to_psa_error( ret ) );
189}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100190#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100191
Ronald Cron0266cfe2021-03-13 18:50:11 +0100192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
193 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman02865f52021-05-07 15:55:27 +0200194
Steven Cooremane6804192021-03-19 18:28:56 +0100195/* 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. */
197static psa_status_t mac_init(
198 mbedtls_psa_mac_operation_t *operation,
199 psa_algorithm_t alg )
200{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100202
Steven Cooreman72f736a2021-05-07 14:14:37 +0200203 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100204
Ronald Cron0266cfe2021-03-13 18:50:11 +0100205#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200206 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100207 {
Steven Cooremane6804192021-03-19 18:28:56 +0100208 mbedtls_cipher_init( &operation->ctx.cmac );
209 status = PSA_SUCCESS;
210 }
211 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100212#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
213#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100214 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 Cron0266cfe2021-03-13 18:50:11 +0100221#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100222 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200223 (void) operation;
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200224 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100225 }
226
227 if( status != PSA_SUCCESS )
228 memset( operation, 0, sizeof( *operation ) );
229 return( status );
230}
231
Ronald Cron0266cfe2021-03-13 18:50:11 +0100232psa_status_t mbedtls_psa_mac_abort( mbedtls_psa_mac_operation_t *operation )
Steven Cooremane6804192021-03-19 18:28:56 +0100233{
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 Cron0266cfe2021-03-13 18:50:11 +0100242#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200243 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100244 {
245 mbedtls_cipher_free( &operation->ctx.cmac );
246 }
247 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100248#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
249#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100250 if( PSA_ALG_IS_HMAC( operation->alg ) )
251 {
252 psa_hmac_abort_internal( &operation->ctx.hmac );
253 }
254 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100255#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100256 {
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 Cooremane6804192021-03-19 18:28:56 +0100263
264 return( PSA_SUCCESS );
265
266bad_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 Cron0266cfe2021-03-13 18:50:11 +0100275static 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 Cooremane6804192021-03-19 18:28:56 +0100280{
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 Cooremane6804192021-03-19 18:28:56 +0100285 return( PSA_ERROR_BAD_STATE );
Steven Cooremane6804192021-03-19 18:28:56 +0100286
287 status = mac_init( operation, alg );
288 if( status != PSA_SUCCESS )
289 return( status );
Steven Cooremane6804192021-03-19 18:28:56 +0100290
Ronald Cron0266cfe2021-03-13 18:50:11 +0100291#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100292 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
293 {
Steven Cooremandcd08112021-05-06 18:00:37 +0200294 /* 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 Cooremane6804192021-03-19 18:28:56 +0100298 }
299 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100300#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
301#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100302 if( PSA_ALG_IS_HMAC( alg ) )
303 {
Steven Cooremane6804192021-03-19 18:28:56 +0100304 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 Cron0266cfe2021-03-13 18:50:11 +0100310#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100311 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200312 (void) attributes;
313 (void) key_buffer;
314 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100315 status = PSA_ERROR_NOT_SUPPORTED;
316 }
317
Steven Cooremana4638e72021-04-29 16:24:36 +0200318 if( status != PSA_SUCCESS )
Ronald Cron0266cfe2021-03-13 18:50:11 +0100319 mbedtls_psa_mac_abort( operation );
Steven Cooremane6804192021-03-19 18:28:56 +0100320
321 return( status );
322}
323
Ronald Cron0266cfe2021-03-13 18:50:11 +0100324psa_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
335psa_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
346psa_status_t mbedtls_psa_mac_update(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100347 mbedtls_psa_mac_operation_t *operation,
348 const uint8_t *input,
349 size_t input_length )
350{
Steven Cooremana4638e72021-04-29 16:24:36 +0200351 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100352 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100353
Ronald Cron0266cfe2021-03-13 18:50:11 +0100354#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200355 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100356 {
357 return( mbedtls_to_psa_error(
358 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
359 input, input_length ) ) );
360 }
361 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100362#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
363#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100364 if( PSA_ALG_IS_HMAC( operation->alg ) )
365 {
Steven Cooremana6df6042021-04-29 19:32:25 +0200366 return( psa_hmac_update_internal( &operation->ctx.hmac,
367 input, input_length ) );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100368 }
369 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100370#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100371 {
372 /* This shouldn't happen if `operation` was initialized by
373 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200374 (void) input;
375 (void) input_length;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100376 return( PSA_ERROR_BAD_STATE );
377 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100378}
379
Ronald Cron0266cfe2021-03-13 18:50:11 +0100380static psa_status_t psa_mac_finish_internal(
381 mbedtls_psa_mac_operation_t *operation,
382 uint8_t *mac, size_t mac_size )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100383{
Ronald Cron0266cfe2021-03-13 18:50:11 +0100384#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200385 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100386 {
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 Cooreman72f736a2021-05-07 14:14:37 +0200390 memcpy( mac, tmp, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100391 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
392 return( mbedtls_to_psa_error( ret ) );
393 }
394 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100395#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana5b860a2021-03-19 19:04:39 +0100397 if( PSA_ALG_IS_HMAC( operation->alg ) )
398 {
399 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200400 mac, mac_size ) );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100401 }
402 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100403#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremana5b860a2021-03-19 19:04:39 +0100404 {
405 /* This shouldn't happen if `operation` was initialized by
406 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200407 (void) operation;
408 (void) mac;
409 (void) mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100410 return( PSA_ERROR_BAD_STATE );
411 }
412}
413
Ronald Cron0266cfe2021-03-13 18:50:11 +0100414psa_status_t mbedtls_psa_mac_sign_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100415 mbedtls_psa_mac_operation_t *operation,
416 uint8_t *mac,
417 size_t mac_size,
418 size_t *mac_length )
419{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100421
422 if( operation->alg == 0 )
423 return( PSA_ERROR_BAD_STATE );
424
Ronald Cron0266cfe2021-03-13 18:50:11 +0100425 status = psa_mac_finish_internal( operation, mac, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100426 if( status == PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200427 *mac_length = mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100428
429 return( status );
430}
431
Ronald Cron0266cfe2021-03-13 18:50:11 +0100432psa_status_t mbedtls_psa_mac_verify_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100433 mbedtls_psa_mac_operation_t *operation,
434 const uint8_t *mac,
435 size_t mac_length )
436{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100437 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100439
440 if( operation->alg == 0 )
441 return( PSA_ERROR_BAD_STATE );
442
Steven Cooreman72f736a2021-05-07 14:14:37 +0200443 /* Consistency check: requested MAC length fits our local buffer */
444 if( mac_length > sizeof( actual_mac ) )
445 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100446
Ronald Cron0266cfe2021-03-13 18:50:11 +0100447 status = psa_mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100448 if( status != PSA_SUCCESS )
449 goto cleanup;
450
Steven Cooreman36876a02021-04-29 16:37:59 +0200451 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100452 status = PSA_ERROR_INVALID_SIGNATURE;
453
454cleanup:
455 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
456
457 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100458}
Ronald Cron76be3e02021-06-17 17:34:43 +0200459
Ronald Cron0266cfe2021-03-13 18:50:11 +0100460psa_status_t mbedtls_psa_mac_compute(
Ronald Cron76be3e02021-06-17 17:34:43 +0200461 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 Cron0266cfe2021-03-13 18:50:11 +0100474 status = psa_mac_setup( &operation,
475 attributes, key_buffer, key_buffer_size,
476 alg );
Ronald Cron76be3e02021-06-17 17:34:43 +0200477 if( status != PSA_SUCCESS )
478 goto exit;
479
480 if( input_length > 0 )
481 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100482 status = mbedtls_psa_mac_update( &operation, input, input_length );
Ronald Cron76be3e02021-06-17 17:34:43 +0200483 if( status != PSA_SUCCESS )
484 goto exit;
485 }
486
Ronald Cron0266cfe2021-03-13 18:50:11 +0100487 status = psa_mac_finish_internal( &operation, mac, mac_size );
Ronald Cron76be3e02021-06-17 17:34:43 +0200488 if( status == PSA_SUCCESS )
489 *mac_length = mac_size;
490
491exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100492 mbedtls_psa_mac_abort( &operation );
Ronald Cron76be3e02021-06-17 17:34:43 +0200493
494 return( status );
495}
496
Ronald Cron0266cfe2021-03-13 18:50:11 +0100497#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100498
499#endif /* MBEDTLS_PSA_CRYPTO_C */