blob: 2c079d434fb7c57c957f810eecda5b10916a0d81 [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
33/* Use builtin defines specific to this compilation unit, since the test driver
34 * relies on the software driver. */
35#if( defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
36 ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) )
37#define BUILTIN_ALG_CMAC 1
38#endif
39#if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
40 ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) )
41#define BUILTIN_ALG_HMAC 1
42#endif
43
Steven Cooreman02865f52021-05-07 15:55:27 +020044#if defined(BUILTIN_ALG_HMAC)
Steven Cooreman82c66b62021-03-19 17:39:17 +010045static size_t psa_get_hash_block_size( psa_algorithm_t alg )
46{
47 switch( alg )
48 {
Steven Cooreman82c66b62021-03-19 17:39:17 +010049 case PSA_ALG_MD5:
50 return( 64 );
51 case PSA_ALG_RIPEMD160:
52 return( 64 );
53 case PSA_ALG_SHA_1:
54 return( 64 );
55 case PSA_ALG_SHA_224:
56 return( 64 );
57 case PSA_ALG_SHA_256:
58 return( 64 );
59 case PSA_ALG_SHA_384:
60 return( 128 );
61 case PSA_ALG_SHA_512:
62 return( 128 );
63 default:
64 return( 0 );
65 }
66}
67
Steven Cooremana6df6042021-04-29 19:32:25 +020068static psa_status_t psa_hmac_abort_internal(
69 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman82c66b62021-03-19 17:39:17 +010070{
71 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
72 return( psa_hash_abort( &hmac->hash_ctx ) );
73}
74
Steven Cooremana6df6042021-04-29 19:32:25 +020075static psa_status_t psa_hmac_setup_internal(
76 mbedtls_psa_hmac_operation_t *hmac,
77 const uint8_t *key,
78 size_t key_length,
79 psa_algorithm_t hash_alg )
Steven Cooreman82c66b62021-03-19 17:39:17 +010080{
81 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
82 size_t i;
83 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
84 size_t block_size = psa_get_hash_block_size( hash_alg );
85 psa_status_t status;
86
87 hmac->alg = hash_alg;
88
89 /* Sanity checks on block_size, to guarantee that there won't be a buffer
90 * overflow below. This should never trigger if the hash algorithm
91 * is implemented correctly. */
92 /* The size checks against the ipad and opad buffers cannot be written
93 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
94 * because that triggers -Wlogical-op on GCC 7.3. */
95 if( block_size > sizeof( ipad ) )
96 return( PSA_ERROR_NOT_SUPPORTED );
97 if( block_size > sizeof( hmac->opad ) )
98 return( PSA_ERROR_NOT_SUPPORTED );
99 if( block_size < hash_size )
100 return( PSA_ERROR_NOT_SUPPORTED );
101
102 if( key_length > block_size )
103 {
104 status = psa_hash_compute( hash_alg, key, key_length,
105 ipad, sizeof( ipad ), &key_length );
106 if( status != PSA_SUCCESS )
107 goto cleanup;
108 }
109 /* A 0-length key is not commonly used in HMAC when used as a MAC,
110 * but it is permitted. It is common when HMAC is used in HKDF, for
111 * example. Don't call `memcpy` in the 0-length because `key` could be
112 * an invalid pointer which would make the behavior undefined. */
113 else if( key_length != 0 )
114 memcpy( ipad, key, key_length );
115
116 /* ipad contains the key followed by garbage. Xor and fill with 0x36
117 * to create the ipad value. */
118 for( i = 0; i < key_length; i++ )
119 ipad[i] ^= 0x36;
120 memset( ipad + key_length, 0x36, block_size - key_length );
121
122 /* Copy the key material from ipad to opad, flipping the requisite bits,
123 * and filling the rest of opad with the requisite constant. */
124 for( i = 0; i < key_length; i++ )
125 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
126 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
127
128 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
129 if( status != PSA_SUCCESS )
130 goto cleanup;
131
132 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
133
134cleanup:
135 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
136
137 return( status );
138}
139
Steven Cooremana6df6042021-04-29 19:32:25 +0200140static psa_status_t psa_hmac_update_internal(
141 mbedtls_psa_hmac_operation_t *hmac,
142 const uint8_t *data,
143 size_t data_length )
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100144{
145 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
146}
147
Steven Cooremana6df6042021-04-29 19:32:25 +0200148static psa_status_t psa_hmac_finish_internal(
149 mbedtls_psa_hmac_operation_t *hmac,
150 uint8_t *mac,
151 size_t mac_size )
Steven Cooreman82c66b62021-03-19 17:39:17 +0100152{
153 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
154 psa_algorithm_t hash_alg = hmac->alg;
155 size_t hash_size = 0;
156 size_t block_size = psa_get_hash_block_size( hash_alg );
157 psa_status_t status;
158
159 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
160 if( status != PSA_SUCCESS )
161 return( status );
162 /* From here on, tmp needs to be wiped. */
163
164 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
165 if( status != PSA_SUCCESS )
166 goto exit;
167
168 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
169 if( status != PSA_SUCCESS )
170 goto exit;
171
172 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
173 if( status != PSA_SUCCESS )
174 goto exit;
175
176 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
177 if( status != PSA_SUCCESS )
178 goto exit;
179
180 memcpy( mac, tmp, mac_size );
181
182exit:
183 mbedtls_platform_zeroize( tmp, hash_size );
184 return( status );
185}
Steven Cooreman02865f52021-05-07 15:55:27 +0200186#endif /* BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100187
188#if defined(BUILTIN_ALG_CMAC)
189static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
190 const psa_key_attributes_t *attributes,
Steven Cooremandcd08112021-05-06 18:00:37 +0200191 const uint8_t *key_buffer )
Steven Cooremane6804192021-03-19 18:28:56 +0100192{
193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman0c239652021-05-07 17:27:27 +0200194
195#if defined(PSA_WANT_KEY_TYPE_DES)
196 /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
197 * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
198 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES &&
199 ( psa_get_key_bits( attributes ) == 64 ||
200 psa_get_key_bits( attributes ) == 128 ) )
201 return( PSA_ERROR_NOT_SUPPORTED );
202#endif
203
Steven Cooreman094a77e2021-05-06 17:58:36 +0200204 const mbedtls_cipher_info_t * cipher_info =
Steven Cooremane6804192021-03-19 18:28:56 +0100205 mbedtls_cipher_info_from_psa(
206 PSA_ALG_CMAC,
207 psa_get_key_type( attributes ),
208 psa_get_key_bits( attributes ),
209 NULL );
210
Steven Cooreman094a77e2021-05-06 17:58:36 +0200211 if( cipher_info == NULL )
Steven Cooremane6804192021-03-19 18:28:56 +0100212 return( PSA_ERROR_NOT_SUPPORTED );
213
Steven Cooreman094a77e2021-05-06 17:58:36 +0200214 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooremane6804192021-03-19 18:28:56 +0100215 if( ret != 0 )
216 goto exit;
217
218 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
219 key_buffer,
220 psa_get_key_bits( attributes ) );
221exit:
222 return( mbedtls_to_psa_error( ret ) );
223}
224#endif /* BUILTIN_ALG_CMAC */
225
Steven Cooreman02865f52021-05-07 15:55:27 +0200226/* Implement the PSA driver MAC interface on top of mbed TLS if either the
227 * software driver or the test driver requires it. */
228#if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC)
229
Steven Cooremane6804192021-03-19 18:28:56 +0100230/* Initialize this driver's MAC operation structure. Once this function has been
231 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
232static psa_status_t mac_init(
233 mbedtls_psa_mac_operation_t *operation,
234 psa_algorithm_t alg )
235{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100237
Steven Cooreman72f736a2021-05-07 14:14:37 +0200238 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100239
240#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200241 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100242 {
Steven Cooremane6804192021-03-19 18:28:56 +0100243 mbedtls_cipher_init( &operation->ctx.cmac );
244 status = PSA_SUCCESS;
245 }
246 else
247#endif /* BUILTIN_ALG_CMAC */
248#if defined(BUILTIN_ALG_HMAC)
249 if( PSA_ALG_IS_HMAC( operation->alg ) )
250 {
251 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
252 operation->ctx.hmac.alg = 0;
253 status = PSA_SUCCESS;
254 }
255 else
256#endif /* BUILTIN_ALG_HMAC */
257 {
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200258 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100259 }
260
261 if( status != PSA_SUCCESS )
262 memset( operation, 0, sizeof( *operation ) );
263 return( status );
264}
265
266static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
267{
268 if( operation->alg == 0 )
269 {
270 /* The object has (apparently) been initialized but it is not
271 * in use. It's ok to call abort on such an object, and there's
272 * nothing to do. */
273 return( PSA_SUCCESS );
274 }
275 else
276#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200277 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100278 {
279 mbedtls_cipher_free( &operation->ctx.cmac );
280 }
281 else
282#endif /* BUILTIN_ALG_CMAC */
283#if defined(BUILTIN_ALG_HMAC)
284 if( PSA_ALG_IS_HMAC( operation->alg ) )
285 {
286 psa_hmac_abort_internal( &operation->ctx.hmac );
287 }
288 else
289#endif /* BUILTIN_ALG_HMAC */
290 {
291 /* Sanity check (shouldn't happen: operation->alg should
292 * always have been initialized to a valid value). */
293 goto bad_state;
294 }
295
296 operation->alg = 0;
Steven Cooremane6804192021-03-19 18:28:56 +0100297
298 return( PSA_SUCCESS );
299
300bad_state:
301 /* If abort is called on an uninitialized object, we can't trust
302 * anything. Wipe the object in case it contains confidential data.
303 * This may result in a memory leak if a pointer gets overwritten,
304 * but it's too late to do anything about this. */
305 memset( operation, 0, sizeof( *operation ) );
306 return( PSA_ERROR_BAD_STATE );
307}
308
309static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
310 const psa_key_attributes_t *attributes,
311 const uint8_t *key_buffer,
312 size_t key_buffer_size,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200313 psa_algorithm_t alg )
Steven Cooremane6804192021-03-19 18:28:56 +0100314{
315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
316
317 /* A context must be freshly initialized before it can be set up. */
318 if( operation->alg != 0 )
Steven Cooremane6804192021-03-19 18:28:56 +0100319 return( PSA_ERROR_BAD_STATE );
Steven Cooremane6804192021-03-19 18:28:56 +0100320
321 status = mac_init( operation, alg );
322 if( status != PSA_SUCCESS )
323 return( status );
Steven Cooremane6804192021-03-19 18:28:56 +0100324
325#if defined(BUILTIN_ALG_CMAC)
326 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
327 {
Steven Cooremandcd08112021-05-06 18:00:37 +0200328 /* Key buffer size for CMAC is dictated by the key bits set on the
329 * attributes, and previously validated by the core on key import. */
330 (void) key_buffer_size;
331 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooremane6804192021-03-19 18:28:56 +0100332 }
333 else
334#endif /* BUILTIN_ALG_CMAC */
335#if defined(BUILTIN_ALG_HMAC)
336 if( PSA_ALG_IS_HMAC( alg ) )
337 {
Steven Cooremane6804192021-03-19 18:28:56 +0100338 status = psa_hmac_setup_internal( &operation->ctx.hmac,
339 key_buffer,
340 key_buffer_size,
341 PSA_ALG_HMAC_GET_HASH( alg ) );
342 }
343 else
344#endif /* BUILTIN_ALG_HMAC */
345 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200346 (void) attributes;
347 (void) key_buffer;
348 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100349 status = PSA_ERROR_NOT_SUPPORTED;
350 }
351
Steven Cooremana4638e72021-04-29 16:24:36 +0200352 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +0100353 mac_abort( operation );
354
355 return( status );
356}
357
Steven Cooremand13a70f2021-03-19 15:24:23 +0100358static psa_status_t mac_update(
359 mbedtls_psa_mac_operation_t *operation,
360 const uint8_t *input,
361 size_t input_length )
362{
Steven Cooremana4638e72021-04-29 16:24:36 +0200363 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100364 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100365
366#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200367 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100368 {
369 return( mbedtls_to_psa_error(
370 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
371 input, input_length ) ) );
372 }
373 else
374#endif /* BUILTIN_ALG_CMAC */
375#if defined(BUILTIN_ALG_HMAC)
376 if( PSA_ALG_IS_HMAC( operation->alg ) )
377 {
Steven Cooremana6df6042021-04-29 19:32:25 +0200378 return( psa_hmac_update_internal( &operation->ctx.hmac,
379 input, input_length ) );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100380 }
381 else
382#endif /* BUILTIN_ALG_HMAC */
383 {
384 /* This shouldn't happen if `operation` was initialized by
385 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200386 (void) input;
387 (void) input_length;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100388 return( PSA_ERROR_BAD_STATE );
389 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100390}
391
Steven Cooremana5b860a2021-03-19 19:04:39 +0100392static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
393 uint8_t *mac,
394 size_t mac_size )
395{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100396#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200397 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100398 {
399 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
400 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
401 if( ret == 0 )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200402 memcpy( mac, tmp, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100403 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
404 return( mbedtls_to_psa_error( ret ) );
405 }
406 else
407#endif /* BUILTIN_ALG_CMAC */
408#if defined(BUILTIN_ALG_HMAC)
409 if( PSA_ALG_IS_HMAC( operation->alg ) )
410 {
411 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200412 mac, mac_size ) );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100413 }
414 else
415#endif /* BUILTIN_ALG_HMAC */
416 {
417 /* This shouldn't happen if `operation` was initialized by
418 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200419 (void) operation;
420 (void) mac;
421 (void) mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100422 return( PSA_ERROR_BAD_STATE );
423 }
424}
425
Steven Cooremand13a70f2021-03-19 15:24:23 +0100426static psa_status_t mac_sign_finish(
427 mbedtls_psa_mac_operation_t *operation,
428 uint8_t *mac,
429 size_t mac_size,
430 size_t *mac_length )
431{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100433
434 if( operation->alg == 0 )
435 return( PSA_ERROR_BAD_STATE );
436
Steven Cooremana5b860a2021-03-19 19:04:39 +0100437 status = mac_finish_internal( operation, mac, mac_size );
438
439 if( status == PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200440 *mac_length = mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100441
442 return( status );
443}
444
Steven Cooremand13a70f2021-03-19 15:24:23 +0100445static psa_status_t mac_verify_finish(
446 mbedtls_psa_mac_operation_t *operation,
447 const uint8_t *mac,
448 size_t mac_length )
449{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100450 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100452
453 if( operation->alg == 0 )
454 return( PSA_ERROR_BAD_STATE );
455
Steven Cooreman72f736a2021-05-07 14:14:37 +0200456 /* Consistency check: requested MAC length fits our local buffer */
457 if( mac_length > sizeof( actual_mac ) )
458 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100459
Steven Cooreman72f736a2021-05-07 14:14:37 +0200460 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100461 if( status != PSA_SUCCESS )
462 goto cleanup;
463
Steven Cooreman36876a02021-04-29 16:37:59 +0200464 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100465 status = PSA_ERROR_INVALID_SIGNATURE;
466
467cleanup:
468 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
469
470 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100471}
Ronald Cron76be3e02021-06-17 17:34:43 +0200472
473static psa_status_t mac_compute(
474 const psa_key_attributes_t *attributes,
475 const uint8_t *key_buffer,
476 size_t key_buffer_size,
477 psa_algorithm_t alg,
478 const uint8_t *input,
479 size_t input_length,
480 uint8_t *mac,
481 size_t mac_size,
482 size_t *mac_length )
483{
484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
485 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
486
487 status = mac_setup( &operation,
488 attributes, key_buffer, key_buffer_size,
489 alg );
490 if( status != PSA_SUCCESS )
491 goto exit;
492
493 if( input_length > 0 )
494 {
495 status = mac_update( &operation, input, input_length );
496 if( status != PSA_SUCCESS )
497 goto exit;
498 }
499
500 status = mac_finish_internal( &operation, mac, mac_size );
501 if( status == PSA_SUCCESS )
502 *mac_length = mac_size;
503
504exit:
505 mac_abort( &operation );
506
507 return( status );
508}
509
Steven Cooreman02865f52021-05-07 15:55:27 +0200510#endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100511
512#if defined(MBEDTLS_PSA_BUILTIN_MAC)
513psa_status_t mbedtls_psa_mac_compute(
514 const psa_key_attributes_t *attributes,
515 const uint8_t *key_buffer,
516 size_t key_buffer_size,
517 psa_algorithm_t alg,
518 const uint8_t *input,
519 size_t input_length,
520 uint8_t *mac,
521 size_t mac_size,
522 size_t *mac_length )
523{
524 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
525 input, input_length,
526 mac, mac_size, mac_length ) );
527}
528
529psa_status_t mbedtls_psa_mac_sign_setup(
530 mbedtls_psa_mac_operation_t *operation,
531 const psa_key_attributes_t *attributes,
532 const uint8_t *key_buffer,
533 size_t key_buffer_size,
534 psa_algorithm_t alg )
535{
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200536 return( mac_setup( operation, attributes,
537 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100538}
539
540psa_status_t mbedtls_psa_mac_verify_setup(
541 mbedtls_psa_mac_operation_t *operation,
542 const psa_key_attributes_t *attributes,
543 const uint8_t *key_buffer,
544 size_t key_buffer_size,
545 psa_algorithm_t alg )
546{
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200547 return( mac_setup( operation, attributes,
548 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100549}
550
551psa_status_t mbedtls_psa_mac_update(
552 mbedtls_psa_mac_operation_t *operation,
553 const uint8_t *input,
554 size_t input_length )
555{
556 return( mac_update( operation, input, input_length ) );
557}
558
559psa_status_t mbedtls_psa_mac_sign_finish(
560 mbedtls_psa_mac_operation_t *operation,
561 uint8_t *mac,
562 size_t mac_size,
563 size_t *mac_length )
564{
565 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
566}
567
568psa_status_t mbedtls_psa_mac_verify_finish(
569 mbedtls_psa_mac_operation_t *operation,
570 const uint8_t *mac,
571 size_t mac_length )
572{
573 return( mac_verify_finish( operation, mac, mac_length ) );
574}
575
576psa_status_t mbedtls_psa_mac_abort(
577 mbedtls_psa_mac_operation_t *operation )
578{
579 return( mac_abort( operation ) );
580}
581#endif /* MBEDTLS_PSA_BUILTIN_MAC */
582
583 /*
584 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
585 */
586#if defined(PSA_CRYPTO_DRIVER_TEST)
587
588static int is_mac_accelerated( psa_algorithm_t alg )
589{
590#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
591 if( PSA_ALG_IS_HMAC( alg ) )
592 return( 1 );
593#endif
594
595 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
596 {
597#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
598 case PSA_ALG_CMAC:
599 return( 1 );
600#endif
601 default:
602 return( 0 );
603 }
604}
605
606psa_status_t mbedtls_transparent_test_driver_mac_compute(
607 const psa_key_attributes_t *attributes,
608 const uint8_t *key_buffer,
609 size_t key_buffer_size,
610 psa_algorithm_t alg,
611 const uint8_t *input,
612 size_t input_length,
613 uint8_t *mac,
614 size_t mac_size,
615 size_t *mac_length )
616{
617 if( is_mac_accelerated( alg ) )
618 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
619 input, input_length,
620 mac, mac_size, mac_length ) );
621 else
622 return( PSA_ERROR_NOT_SUPPORTED );
623}
624
625psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
626 mbedtls_transparent_test_driver_mac_operation_t *operation,
627 const psa_key_attributes_t *attributes,
628 const uint8_t *key_buffer,
629 size_t key_buffer_size,
630 psa_algorithm_t alg )
631{
632 if( is_mac_accelerated( alg ) )
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200633 return( mac_setup( operation, attributes,
634 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100635 else
636 return( PSA_ERROR_NOT_SUPPORTED );
637}
638
639psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
640 mbedtls_transparent_test_driver_mac_operation_t *operation,
641 const psa_key_attributes_t *attributes,
642 const uint8_t *key_buffer,
643 size_t key_buffer_size,
644 psa_algorithm_t alg )
645{
646 if( is_mac_accelerated( alg ) )
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200647 return( mac_setup( operation, attributes,
648 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100649 else
650 return( PSA_ERROR_NOT_SUPPORTED );
651}
652
653psa_status_t mbedtls_transparent_test_driver_mac_update(
654 mbedtls_transparent_test_driver_mac_operation_t *operation,
655 const uint8_t *input,
656 size_t input_length )
657{
658 if( is_mac_accelerated( operation->alg ) )
659 return( mac_update( operation, input, input_length ) );
660 else
661 return( PSA_ERROR_BAD_STATE );
662}
663
664psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
665 mbedtls_transparent_test_driver_mac_operation_t *operation,
666 uint8_t *mac,
667 size_t mac_size,
668 size_t *mac_length )
669{
670 if( is_mac_accelerated( operation->alg ) )
671 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
672 else
673 return( PSA_ERROR_BAD_STATE );
674}
675
676psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
677 mbedtls_transparent_test_driver_mac_operation_t *operation,
678 const uint8_t *mac,
679 size_t mac_length )
680{
681 if( is_mac_accelerated( operation->alg ) )
682 return( mac_verify_finish( operation, mac, mac_length ) );
683 else
684 return( PSA_ERROR_BAD_STATE );
685}
686
687psa_status_t mbedtls_transparent_test_driver_mac_abort(
688 mbedtls_transparent_test_driver_mac_operation_t *operation )
689{
690 return( mac_abort( operation ) );
691}
692
693psa_status_t mbedtls_opaque_test_driver_mac_compute(
694 const psa_key_attributes_t *attributes,
695 const uint8_t *key_buffer,
696 size_t key_buffer_size,
697 psa_algorithm_t alg,
698 const uint8_t *input,
699 size_t input_length,
700 uint8_t *mac,
701 size_t mac_size,
702 size_t *mac_length )
703{
704 /* Opaque driver testing is not implemented yet through this mechanism. */
705 (void) attributes;
706 (void) key_buffer;
707 (void) key_buffer_size;
708 (void) alg;
709 (void) input;
710 (void) input_length;
711 (void) mac;
712 (void) mac_size;
713 (void) mac_length;
714 return( PSA_ERROR_NOT_SUPPORTED );
715}
716
717psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
718 mbedtls_opaque_test_driver_mac_operation_t *operation,
719 const psa_key_attributes_t *attributes,
720 const uint8_t *key_buffer,
721 size_t key_buffer_size,
722 psa_algorithm_t alg )
723{
724 /* Opaque driver testing is not implemented yet through this mechanism. */
725 (void) operation;
726 (void) attributes;
727 (void) key_buffer;
728 (void) key_buffer_size;
729 (void) alg;
730 return( PSA_ERROR_NOT_SUPPORTED );
731}
732
733psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
734 mbedtls_opaque_test_driver_mac_operation_t *operation,
735 const psa_key_attributes_t *attributes,
736 const uint8_t *key_buffer,
737 size_t key_buffer_size,
738 psa_algorithm_t alg )
739{
740 /* Opaque driver testing is not implemented yet through this mechanism. */
741 (void) operation;
742 (void) attributes;
743 (void) key_buffer;
744 (void) key_buffer_size;
745 (void) alg;
746 return( PSA_ERROR_NOT_SUPPORTED );
747}
748
749psa_status_t mbedtls_opaque_test_driver_mac_update(
750 mbedtls_opaque_test_driver_mac_operation_t *operation,
751 const uint8_t *input,
752 size_t input_length )
753{
754 /* Opaque driver testing is not implemented yet through this mechanism. */
755 (void) operation;
756 (void) input;
757 (void) input_length;
758 return( PSA_ERROR_NOT_SUPPORTED );
759}
760
761psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
762 mbedtls_opaque_test_driver_mac_operation_t *operation,
763 uint8_t *mac,
764 size_t mac_size,
765 size_t *mac_length )
766{
767 /* Opaque driver testing is not implemented yet through this mechanism. */
768 (void) operation;
769 (void) mac;
770 (void) mac_size;
771 (void) mac_length;
772 return( PSA_ERROR_NOT_SUPPORTED );
773}
774
775psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
776 mbedtls_opaque_test_driver_mac_operation_t *operation,
777 const uint8_t *mac,
778 size_t mac_length )
779{
780 /* Opaque driver testing is not implemented yet through this mechanism. */
781 (void) operation;
782 (void) mac;
783 (void) mac_length;
784 return( PSA_ERROR_NOT_SUPPORTED );
785}
786
787psa_status_t mbedtls_opaque_test_driver_mac_abort(
788 mbedtls_opaque_test_driver_mac_operation_t *operation )
789{
790 /* Opaque driver testing is not implemented yet through this mechanism. */
791 (void) operation;
792 return( PSA_ERROR_NOT_SUPPORTED );
793}
794
795#endif /* PSA_CRYPTO_DRIVER_TEST */
796
797#endif /* MBEDTLS_PSA_CRYPTO_C */