blob: 5e78d65a61113b69f11bee054c73f2bbf165e116 [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
Steven Cooreman02865f52021-05-07 15:55:27 +0200192/* Implement the PSA driver MAC interface on top of mbed TLS if either the
193 * software driver or the test driver requires it. */
Ronald Cron0266cfe2021-03-13 18:50:11 +0100194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
195 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman02865f52021-05-07 15:55:27 +0200196
Steven Cooremane6804192021-03-19 18:28:56 +0100197/* Initialize this driver's MAC operation structure. Once this function has been
198 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
199static psa_status_t mac_init(
200 mbedtls_psa_mac_operation_t *operation,
201 psa_algorithm_t alg )
202{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100204
Steven Cooreman72f736a2021-05-07 14:14:37 +0200205 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100206
Ronald Cron0266cfe2021-03-13 18:50:11 +0100207#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200208 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100209 {
Steven Cooremane6804192021-03-19 18:28:56 +0100210 mbedtls_cipher_init( &operation->ctx.cmac );
211 status = PSA_SUCCESS;
212 }
213 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100214#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
215#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100216 if( PSA_ALG_IS_HMAC( operation->alg ) )
217 {
218 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
219 operation->ctx.hmac.alg = 0;
220 status = PSA_SUCCESS;
221 }
222 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100223#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100224 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200225 (void) operation;
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200226 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100227 }
228
229 if( status != PSA_SUCCESS )
230 memset( operation, 0, sizeof( *operation ) );
231 return( status );
232}
233
Ronald Cron0266cfe2021-03-13 18:50:11 +0100234psa_status_t mbedtls_psa_mac_abort( mbedtls_psa_mac_operation_t *operation )
Steven Cooremane6804192021-03-19 18:28:56 +0100235{
236 if( operation->alg == 0 )
237 {
238 /* The object has (apparently) been initialized but it is not
239 * in use. It's ok to call abort on such an object, and there's
240 * nothing to do. */
241 return( PSA_SUCCESS );
242 }
243 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100244#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200245 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100246 {
247 mbedtls_cipher_free( &operation->ctx.cmac );
248 }
249 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100250#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
251#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100252 if( PSA_ALG_IS_HMAC( operation->alg ) )
253 {
254 psa_hmac_abort_internal( &operation->ctx.hmac );
255 }
256 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100257#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100258 {
259 /* Sanity check (shouldn't happen: operation->alg should
260 * always have been initialized to a valid value). */
261 goto bad_state;
262 }
263
264 operation->alg = 0;
Steven Cooremane6804192021-03-19 18:28:56 +0100265
266 return( PSA_SUCCESS );
267
268bad_state:
269 /* If abort is called on an uninitialized object, we can't trust
270 * anything. Wipe the object in case it contains confidential data.
271 * This may result in a memory leak if a pointer gets overwritten,
272 * but it's too late to do anything about this. */
273 memset( operation, 0, sizeof( *operation ) );
274 return( PSA_ERROR_BAD_STATE );
275}
276
Ronald Cron0266cfe2021-03-13 18:50:11 +0100277static psa_status_t psa_mac_setup( mbedtls_psa_mac_operation_t *operation,
278 const psa_key_attributes_t *attributes,
279 const uint8_t *key_buffer,
280 size_t key_buffer_size,
281 psa_algorithm_t alg )
Steven Cooremane6804192021-03-19 18:28:56 +0100282{
283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
284
285 /* A context must be freshly initialized before it can be set up. */
286 if( operation->alg != 0 )
Steven Cooremane6804192021-03-19 18:28:56 +0100287 return( PSA_ERROR_BAD_STATE );
Steven Cooremane6804192021-03-19 18:28:56 +0100288
289 status = mac_init( operation, alg );
290 if( status != PSA_SUCCESS )
291 return( status );
Steven Cooremane6804192021-03-19 18:28:56 +0100292
Ronald Cron0266cfe2021-03-13 18:50:11 +0100293#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100294 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
295 {
Steven Cooremandcd08112021-05-06 18:00:37 +0200296 /* Key buffer size for CMAC is dictated by the key bits set on the
297 * attributes, and previously validated by the core on key import. */
298 (void) key_buffer_size;
299 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooremane6804192021-03-19 18:28:56 +0100300 }
301 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100302#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremane6804192021-03-19 18:28:56 +0100304 if( PSA_ALG_IS_HMAC( alg ) )
305 {
Steven Cooremane6804192021-03-19 18:28:56 +0100306 status = psa_hmac_setup_internal( &operation->ctx.hmac,
307 key_buffer,
308 key_buffer_size,
309 PSA_ALG_HMAC_GET_HASH( alg ) );
310 }
311 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100312#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100313 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200314 (void) attributes;
315 (void) key_buffer;
316 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100317 status = PSA_ERROR_NOT_SUPPORTED;
318 }
319
Steven Cooremana4638e72021-04-29 16:24:36 +0200320 if( status != PSA_SUCCESS )
Ronald Cron0266cfe2021-03-13 18:50:11 +0100321 mbedtls_psa_mac_abort( operation );
Steven Cooremane6804192021-03-19 18:28:56 +0100322
323 return( status );
324}
325
Ronald Cron0266cfe2021-03-13 18:50:11 +0100326psa_status_t mbedtls_psa_mac_sign_setup(
327 mbedtls_psa_mac_operation_t *operation,
328 const psa_key_attributes_t *attributes,
329 const uint8_t *key_buffer,
330 size_t key_buffer_size,
331 psa_algorithm_t alg )
332{
333 return( psa_mac_setup( operation, attributes,
334 key_buffer, key_buffer_size, alg ) );
335}
336
337psa_status_t mbedtls_psa_mac_verify_setup(
338 mbedtls_psa_mac_operation_t *operation,
339 const psa_key_attributes_t *attributes,
340 const uint8_t *key_buffer,
341 size_t key_buffer_size,
342 psa_algorithm_t alg )
343{
344 return( psa_mac_setup( operation, attributes,
345 key_buffer, key_buffer_size, alg ) );
346}
347
348psa_status_t mbedtls_psa_mac_update(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100349 mbedtls_psa_mac_operation_t *operation,
350 const uint8_t *input,
351 size_t input_length )
352{
Steven Cooremana4638e72021-04-29 16:24:36 +0200353 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100354 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100355
Ronald Cron0266cfe2021-03-13 18:50:11 +0100356#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200357 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100358 {
359 return( mbedtls_to_psa_error(
360 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
361 input, input_length ) ) );
362 }
363 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100364#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
365#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100366 if( PSA_ALG_IS_HMAC( operation->alg ) )
367 {
Steven Cooremana6df6042021-04-29 19:32:25 +0200368 return( psa_hmac_update_internal( &operation->ctx.hmac,
369 input, input_length ) );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100370 }
371 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100372#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100373 {
374 /* This shouldn't happen if `operation` was initialized by
375 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200376 (void) input;
377 (void) input_length;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100378 return( PSA_ERROR_BAD_STATE );
379 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100380}
381
Ronald Cron0266cfe2021-03-13 18:50:11 +0100382static psa_status_t psa_mac_finish_internal(
383 mbedtls_psa_mac_operation_t *operation,
384 uint8_t *mac, size_t mac_size )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100385{
Ronald Cron0266cfe2021-03-13 18:50:11 +0100386#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200387 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100388 {
389 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
390 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
391 if( ret == 0 )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200392 memcpy( mac, tmp, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100393 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
394 return( mbedtls_to_psa_error( ret ) );
395 }
396 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100397#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
398#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana5b860a2021-03-19 19:04:39 +0100399 if( PSA_ALG_IS_HMAC( operation->alg ) )
400 {
401 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200402 mac, mac_size ) );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100403 }
404 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100405#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremana5b860a2021-03-19 19:04:39 +0100406 {
407 /* This shouldn't happen if `operation` was initialized by
408 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200409 (void) operation;
410 (void) mac;
411 (void) mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100412 return( PSA_ERROR_BAD_STATE );
413 }
414}
415
Ronald Cron0266cfe2021-03-13 18:50:11 +0100416psa_status_t mbedtls_psa_mac_sign_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100417 mbedtls_psa_mac_operation_t *operation,
418 uint8_t *mac,
419 size_t mac_size,
420 size_t *mac_length )
421{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100423
424 if( operation->alg == 0 )
425 return( PSA_ERROR_BAD_STATE );
426
Ronald Cron0266cfe2021-03-13 18:50:11 +0100427 status = psa_mac_finish_internal( operation, mac, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100428 if( status == PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200429 *mac_length = mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100430
431 return( status );
432}
433
Ronald Cron0266cfe2021-03-13 18:50:11 +0100434psa_status_t mbedtls_psa_mac_verify_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100435 mbedtls_psa_mac_operation_t *operation,
436 const uint8_t *mac,
437 size_t mac_length )
438{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100439 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100441
442 if( operation->alg == 0 )
443 return( PSA_ERROR_BAD_STATE );
444
Steven Cooreman72f736a2021-05-07 14:14:37 +0200445 /* Consistency check: requested MAC length fits our local buffer */
446 if( mac_length > sizeof( actual_mac ) )
447 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100448
Ronald Cron0266cfe2021-03-13 18:50:11 +0100449 status = psa_mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100450 if( status != PSA_SUCCESS )
451 goto cleanup;
452
Steven Cooreman36876a02021-04-29 16:37:59 +0200453 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100454 status = PSA_ERROR_INVALID_SIGNATURE;
455
456cleanup:
457 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
458
459 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100460}
Ronald Cron76be3e02021-06-17 17:34:43 +0200461
Ronald Cron0266cfe2021-03-13 18:50:11 +0100462psa_status_t mbedtls_psa_mac_compute(
Ronald Cron76be3e02021-06-17 17:34:43 +0200463 const psa_key_attributes_t *attributes,
464 const uint8_t *key_buffer,
465 size_t key_buffer_size,
466 psa_algorithm_t alg,
467 const uint8_t *input,
468 size_t input_length,
469 uint8_t *mac,
470 size_t mac_size,
471 size_t *mac_length )
472{
473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
474 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
475
Ronald Cron0266cfe2021-03-13 18:50:11 +0100476 status = psa_mac_setup( &operation,
477 attributes, key_buffer, key_buffer_size,
478 alg );
Ronald Cron76be3e02021-06-17 17:34:43 +0200479 if( status != PSA_SUCCESS )
480 goto exit;
481
482 if( input_length > 0 )
483 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100484 status = mbedtls_psa_mac_update( &operation, input, input_length );
Ronald Cron76be3e02021-06-17 17:34:43 +0200485 if( status != PSA_SUCCESS )
486 goto exit;
487 }
488
Ronald Cron0266cfe2021-03-13 18:50:11 +0100489 status = psa_mac_finish_internal( &operation, mac, mac_size );
Ronald Cron76be3e02021-06-17 17:34:43 +0200490 if( status == PSA_SUCCESS )
491 *mac_length = mac_size;
492
493exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100494 mbedtls_psa_mac_abort( &operation );
Ronald Cron76be3e02021-06-17 17:34:43 +0200495
496 return( status );
497}
498
Ronald Cron0266cfe2021-03-13 18:50:11 +0100499#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100500
501#endif /* MBEDTLS_PSA_CRYPTO_C */