blob: 854aee4f0b1b0dc39c4af24a2a5cce421434034e [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 Cooreman82c66b62021-03-19 17:39:17 +010044#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
45static size_t psa_get_hash_block_size( psa_algorithm_t alg )
46{
47 switch( alg )
48 {
49 case PSA_ALG_MD2:
50 return( 16 );
51 case PSA_ALG_MD4:
52 return( 64 );
53 case PSA_ALG_MD5:
54 return( 64 );
55 case PSA_ALG_RIPEMD160:
56 return( 64 );
57 case PSA_ALG_SHA_1:
58 return( 64 );
59 case PSA_ALG_SHA_224:
60 return( 64 );
61 case PSA_ALG_SHA_256:
62 return( 64 );
63 case PSA_ALG_SHA_384:
64 return( 128 );
65 case PSA_ALG_SHA_512:
66 return( 128 );
67 default:
68 return( 0 );
69 }
70}
71
Steven Cooremand1955af2021-03-22 14:49:06 +010072psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data_t *hmac )
Steven Cooreman82c66b62021-03-19 17:39:17 +010073{
74 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
75 return( psa_hash_abort( &hmac->hash_ctx ) );
76}
77
Steven Cooremand1955af2021-03-22 14:49:06 +010078psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman82c66b62021-03-19 17:39:17 +010079 const uint8_t *key,
80 size_t key_length,
81 psa_algorithm_t hash_alg )
82{
83 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
84 size_t i;
85 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
86 size_t block_size = psa_get_hash_block_size( hash_alg );
87 psa_status_t status;
88
89 hmac->alg = hash_alg;
90
91 /* Sanity checks on block_size, to guarantee that there won't be a buffer
92 * overflow below. This should never trigger if the hash algorithm
93 * is implemented correctly. */
94 /* The size checks against the ipad and opad buffers cannot be written
95 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
96 * because that triggers -Wlogical-op on GCC 7.3. */
97 if( block_size > sizeof( ipad ) )
98 return( PSA_ERROR_NOT_SUPPORTED );
99 if( block_size > sizeof( hmac->opad ) )
100 return( PSA_ERROR_NOT_SUPPORTED );
101 if( block_size < hash_size )
102 return( PSA_ERROR_NOT_SUPPORTED );
103
104 if( key_length > block_size )
105 {
106 status = psa_hash_compute( hash_alg, key, key_length,
107 ipad, sizeof( ipad ), &key_length );
108 if( status != PSA_SUCCESS )
109 goto cleanup;
110 }
111 /* A 0-length key is not commonly used in HMAC when used as a MAC,
112 * but it is permitted. It is common when HMAC is used in HKDF, for
113 * example. Don't call `memcpy` in the 0-length because `key` could be
114 * an invalid pointer which would make the behavior undefined. */
115 else if( key_length != 0 )
116 memcpy( ipad, key, key_length );
117
118 /* ipad contains the key followed by garbage. Xor and fill with 0x36
119 * to create the ipad value. */
120 for( i = 0; i < key_length; i++ )
121 ipad[i] ^= 0x36;
122 memset( ipad + key_length, 0x36, block_size - key_length );
123
124 /* Copy the key material from ipad to opad, flipping the requisite bits,
125 * and filling the rest of opad with the requisite constant. */
126 for( i = 0; i < key_length; i++ )
127 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
128 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
129
130 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
131 if( status != PSA_SUCCESS )
132 goto cleanup;
133
134 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
135
136cleanup:
137 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
138
139 return( status );
140}
141
Steven Cooremand1955af2021-03-22 14:49:06 +0100142psa_status_t psa_hmac_update_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100143 const uint8_t *data,
144 size_t data_length )
145{
146 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
147}
148
Steven Cooremand1955af2021-03-22 14:49:06 +0100149psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman82c66b62021-03-19 17:39:17 +0100150 uint8_t *mac,
151 size_t mac_size )
152{
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 Cooreman4fdf0602021-03-22 12:21:10 +0100186
Steven Cooremand1955af2021-03-22 14:49:06 +0100187psa_status_t psa_hmac_clone_internal( const psa_hmac_internal_data_t *source,
188 psa_hmac_internal_data_t *destination )
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100189{
190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
191
192 destination->alg = source->alg;
193 destination->hash_ctx = psa_hash_operation_init();
194 status = psa_hash_clone( &source->hash_ctx, &destination->hash_ctx );
195 memcpy( destination->opad, source->opad, sizeof( destination->opad ) );
196
197 if( status != PSA_SUCCESS )
198 memset( destination, 0, sizeof( *destination ) );
199
200 return( status );
201}
Steven Cooreman82c66b62021-03-19 17:39:17 +0100202#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || PSA_CRYPTO_DRIVER_TEST */
203
Steven Cooremand13a70f2021-03-19 15:24:23 +0100204/* Implement the PSA driver MAC interface on top of mbed TLS if either the
205 * software driver or the test driver requires it. */
206#if defined(MBEDTLS_PSA_BUILTIN_MAC) || defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremane6804192021-03-19 18:28:56 +0100207
208#if defined(BUILTIN_ALG_CMAC)
209static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
210 const psa_key_attributes_t *attributes,
211 const uint8_t *key_buffer,
212 size_t key_buffer_size )
213{
214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
215 const mbedtls_cipher_info_t * cipher_info_tmp =
216 mbedtls_cipher_info_from_psa(
217 PSA_ALG_CMAC,
218 psa_get_key_type( attributes ),
219 psa_get_key_bits( attributes ),
220 NULL );
221
222 if( cipher_info_tmp == NULL )
223 return( PSA_ERROR_NOT_SUPPORTED );
224
225 if( key_buffer_size < PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) )
226 return( PSA_ERROR_INVALID_ARGUMENT );
227
228 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info_tmp );
229 if( ret != 0 )
230 goto exit;
231
232 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
233 key_buffer,
234 psa_get_key_bits( attributes ) );
235exit:
236 return( mbedtls_to_psa_error( ret ) );
237}
238#endif /* BUILTIN_ALG_CMAC */
239
240/* Initialize this driver's MAC operation structure. Once this function has been
241 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
242static psa_status_t mac_init(
243 mbedtls_psa_mac_operation_t *operation,
244 psa_algorithm_t alg )
245{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100247
248 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Steven Cooremane6804192021-03-19 18:28:56 +0100249 operation->has_input = 0;
250 operation->is_sign = 0;
251
252#if defined(BUILTIN_ALG_CMAC)
253 if( operation->alg == PSA_ALG_CMAC )
254 {
Steven Cooremane6804192021-03-19 18:28:56 +0100255 mbedtls_cipher_init( &operation->ctx.cmac );
256 status = PSA_SUCCESS;
257 }
258 else
259#endif /* BUILTIN_ALG_CMAC */
260#if defined(BUILTIN_ALG_HMAC)
261 if( PSA_ALG_IS_HMAC( operation->alg ) )
262 {
263 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
264 operation->ctx.hmac.alg = 0;
265 status = PSA_SUCCESS;
266 }
267 else
268#endif /* BUILTIN_ALG_HMAC */
269 {
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200270 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100271 }
272
273 if( status != PSA_SUCCESS )
274 memset( operation, 0, sizeof( *operation ) );
275 return( status );
276}
277
278static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
279{
280 if( operation->alg == 0 )
281 {
282 /* The object has (apparently) been initialized but it is not
283 * in use. It's ok to call abort on such an object, and there's
284 * nothing to do. */
285 return( PSA_SUCCESS );
286 }
287 else
288#if defined(BUILTIN_ALG_CMAC)
289 if( operation->alg == PSA_ALG_CMAC )
290 {
291 mbedtls_cipher_free( &operation->ctx.cmac );
292 }
293 else
294#endif /* BUILTIN_ALG_CMAC */
295#if defined(BUILTIN_ALG_HMAC)
296 if( PSA_ALG_IS_HMAC( operation->alg ) )
297 {
298 psa_hmac_abort_internal( &operation->ctx.hmac );
299 }
300 else
301#endif /* BUILTIN_ALG_HMAC */
302 {
303 /* Sanity check (shouldn't happen: operation->alg should
304 * always have been initialized to a valid value). */
305 goto bad_state;
306 }
307
308 operation->alg = 0;
Steven Cooremane6804192021-03-19 18:28:56 +0100309 operation->has_input = 0;
310 operation->is_sign = 0;
311
312 return( PSA_SUCCESS );
313
314bad_state:
315 /* If abort is called on an uninitialized object, we can't trust
316 * anything. Wipe the object in case it contains confidential data.
317 * This may result in a memory leak if a pointer gets overwritten,
318 * but it's too late to do anything about this. */
319 memset( operation, 0, sizeof( *operation ) );
320 return( PSA_ERROR_BAD_STATE );
321}
322
323static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
324 const psa_key_attributes_t *attributes,
325 const uint8_t *key_buffer,
326 size_t key_buffer_size,
327 psa_algorithm_t alg,
328 int is_sign )
329{
330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
331
332 /* A context must be freshly initialized before it can be set up. */
333 if( operation->alg != 0 )
334 {
335 return( PSA_ERROR_BAD_STATE );
336 }
337
338 status = mac_init( operation, alg );
339 if( status != PSA_SUCCESS )
340 return( status );
341 if( is_sign )
342 operation->is_sign = 1;
343
344 /* Get the output length for the algorithm and key combination. None of the
345 * currently supported algorithms have an output length dependent on actual
346 * key size, so setting it to a bogus value is currently OK. */
347 operation->mac_size =
348 PSA_MAC_LENGTH( psa_get_key_type( attributes ), 0, alg );
349
350#if defined(BUILTIN_ALG_CMAC)
351 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
352 {
353 status = cmac_setup( operation, attributes,
354 key_buffer, key_buffer_size );
355 }
356 else
357#endif /* BUILTIN_ALG_CMAC */
358#if defined(BUILTIN_ALG_HMAC)
359 if( PSA_ALG_IS_HMAC( alg ) )
360 {
361 /* Sanity check. This shouldn't fail on a valid configuration. */
362 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
363 {
364 status = PSA_ERROR_NOT_SUPPORTED;
365 goto exit;
366 }
367
368 if( psa_get_key_type( attributes ) != PSA_KEY_TYPE_HMAC )
369 {
370 status = PSA_ERROR_INVALID_ARGUMENT;
371 goto exit;
372 }
373
374 status = psa_hmac_setup_internal( &operation->ctx.hmac,
375 key_buffer,
376 key_buffer_size,
377 PSA_ALG_HMAC_GET_HASH( alg ) );
378 }
379 else
380#endif /* BUILTIN_ALG_HMAC */
381 {
382 status = PSA_ERROR_NOT_SUPPORTED;
383 }
384
385exit:
Steven Cooremana4638e72021-04-29 16:24:36 +0200386 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +0100387 mac_abort( operation );
388
389 return( status );
390}
391
Steven Cooremand13a70f2021-03-19 15:24:23 +0100392static psa_status_t mac_compute(
393 const psa_key_attributes_t *attributes,
394 const uint8_t *key_buffer,
395 size_t key_buffer_size,
396 psa_algorithm_t alg,
397 const uint8_t *input,
398 size_t input_length,
399 uint8_t *mac,
400 size_t mac_size,
401 size_t *mac_length )
402{
Steven Cooreman82c66b62021-03-19 17:39:17 +0100403 /* One-shot MAC has not been implemented in this PSA implementation yet. */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100404 (void) attributes;
405 (void) key_buffer;
406 (void) key_buffer_size;
407 (void) alg;
408 (void) input;
409 (void) input_length;
410 (void) mac;
411 (void) mac_size;
412 (void) mac_length;
413 return( PSA_ERROR_NOT_SUPPORTED );
414}
415
416static psa_status_t mac_sign_setup(
417 mbedtls_psa_mac_operation_t *operation,
418 const psa_key_attributes_t *attributes,
419 const uint8_t *key_buffer,
420 size_t key_buffer_size,
421 psa_algorithm_t alg )
422{
Steven Cooremane6804192021-03-19 18:28:56 +0100423 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
424 1 ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100425}
426
427static psa_status_t mac_verify_setup(
428 mbedtls_psa_mac_operation_t *operation,
429 const psa_key_attributes_t *attributes,
430 const uint8_t *key_buffer,
431 size_t key_buffer_size,
432 psa_algorithm_t alg )
433{
Steven Cooremane6804192021-03-19 18:28:56 +0100434 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
435 0 ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100436}
437
438static psa_status_t mac_update(
439 mbedtls_psa_mac_operation_t *operation,
440 const uint8_t *input,
441 size_t input_length )
442{
Steven Cooremana4638e72021-04-29 16:24:36 +0200443 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100444 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100445 operation->has_input = 1;
446
447#if defined(BUILTIN_ALG_CMAC)
448 if( operation->alg == PSA_ALG_CMAC )
449 {
450 return( mbedtls_to_psa_error(
451 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
452 input, input_length ) ) );
453 }
454 else
455#endif /* BUILTIN_ALG_CMAC */
456#if defined(BUILTIN_ALG_HMAC)
457 if( PSA_ALG_IS_HMAC( operation->alg ) )
458 {
459 return( psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
460 input_length ) );
461 }
462 else
463#endif /* BUILTIN_ALG_HMAC */
464 {
465 /* This shouldn't happen if `operation` was initialized by
466 * a setup function. */
467 return( PSA_ERROR_BAD_STATE );
468 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100469}
470
Steven Cooremana5b860a2021-03-19 19:04:39 +0100471static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
472 uint8_t *mac,
473 size_t mac_size )
474{
Steven Cooremana4638e72021-04-29 16:24:36 +0200475 if( operation->alg == 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100476 return( PSA_ERROR_BAD_STATE );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100477 if( mac_size < operation->mac_size )
478 return( PSA_ERROR_BUFFER_TOO_SMALL );
479
480#if defined(BUILTIN_ALG_CMAC)
481 if( operation->alg == PSA_ALG_CMAC )
482 {
483 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
484 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
485 if( ret == 0 )
486 memcpy( mac, tmp, operation->mac_size );
487 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
488 return( mbedtls_to_psa_error( ret ) );
489 }
490 else
491#endif /* BUILTIN_ALG_CMAC */
492#if defined(BUILTIN_ALG_HMAC)
493 if( PSA_ALG_IS_HMAC( operation->alg ) )
494 {
495 return( psa_hmac_finish_internal( &operation->ctx.hmac,
496 mac, operation->mac_size ) );
497 }
498 else
499#endif /* BUILTIN_ALG_HMAC */
500 {
501 /* This shouldn't happen if `operation` was initialized by
502 * a setup function. */
503 return( PSA_ERROR_BAD_STATE );
504 }
505}
506
Steven Cooremand13a70f2021-03-19 15:24:23 +0100507static psa_status_t mac_sign_finish(
508 mbedtls_psa_mac_operation_t *operation,
509 uint8_t *mac,
510 size_t mac_size,
511 size_t *mac_length )
512{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100513 psa_status_t status;
514
515 if( operation->alg == 0 )
516 return( PSA_ERROR_BAD_STATE );
517
518 if( ! operation->is_sign )
519 return( PSA_ERROR_BAD_STATE );
520
521 status = mac_finish_internal( operation, mac, mac_size );
522
523 if( status == PSA_SUCCESS )
524 *mac_length = operation->mac_size;
525
526 return( status );
527}
528
Steven Cooremand13a70f2021-03-19 15:24:23 +0100529static psa_status_t mac_verify_finish(
530 mbedtls_psa_mac_operation_t *operation,
531 const uint8_t *mac,
532 size_t mac_length )
533{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100534 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
535 psa_status_t status;
536
537 if( operation->alg == 0 )
538 return( PSA_ERROR_BAD_STATE );
539
540 if( operation->is_sign )
541 return( PSA_ERROR_BAD_STATE );
542
543 if( operation->mac_size != mac_length )
544 {
545 status = PSA_ERROR_INVALID_SIGNATURE;
546 goto cleanup;
547 }
548
549 status = mac_finish_internal( operation, actual_mac, sizeof( actual_mac ) );
550 if( status != PSA_SUCCESS )
551 goto cleanup;
552
Steven Cooreman36876a02021-04-29 16:37:59 +0200553 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100554 status = PSA_ERROR_INVALID_SIGNATURE;
555
556cleanup:
557 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
558
559 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100560}
Steven Cooremand13a70f2021-03-19 15:24:23 +0100561#endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */
562
563#if defined(MBEDTLS_PSA_BUILTIN_MAC)
564psa_status_t mbedtls_psa_mac_compute(
565 const psa_key_attributes_t *attributes,
566 const uint8_t *key_buffer,
567 size_t key_buffer_size,
568 psa_algorithm_t alg,
569 const uint8_t *input,
570 size_t input_length,
571 uint8_t *mac,
572 size_t mac_size,
573 size_t *mac_length )
574{
575 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
576 input, input_length,
577 mac, mac_size, mac_length ) );
578}
579
580psa_status_t mbedtls_psa_mac_sign_setup(
581 mbedtls_psa_mac_operation_t *operation,
582 const psa_key_attributes_t *attributes,
583 const uint8_t *key_buffer,
584 size_t key_buffer_size,
585 psa_algorithm_t alg )
586{
587 return( mac_sign_setup( operation, attributes,
588 key_buffer, key_buffer_size, alg ) );
589}
590
591psa_status_t mbedtls_psa_mac_verify_setup(
592 mbedtls_psa_mac_operation_t *operation,
593 const psa_key_attributes_t *attributes,
594 const uint8_t *key_buffer,
595 size_t key_buffer_size,
596 psa_algorithm_t alg )
597{
598 return( mac_verify_setup( operation, attributes,
599 key_buffer, key_buffer_size, alg ) );
600}
601
602psa_status_t mbedtls_psa_mac_update(
603 mbedtls_psa_mac_operation_t *operation,
604 const uint8_t *input,
605 size_t input_length )
606{
607 return( mac_update( operation, input, input_length ) );
608}
609
610psa_status_t mbedtls_psa_mac_sign_finish(
611 mbedtls_psa_mac_operation_t *operation,
612 uint8_t *mac,
613 size_t mac_size,
614 size_t *mac_length )
615{
616 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
617}
618
619psa_status_t mbedtls_psa_mac_verify_finish(
620 mbedtls_psa_mac_operation_t *operation,
621 const uint8_t *mac,
622 size_t mac_length )
623{
624 return( mac_verify_finish( operation, mac, mac_length ) );
625}
626
627psa_status_t mbedtls_psa_mac_abort(
628 mbedtls_psa_mac_operation_t *operation )
629{
630 return( mac_abort( operation ) );
631}
632#endif /* MBEDTLS_PSA_BUILTIN_MAC */
633
634 /*
635 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
636 */
637#if defined(PSA_CRYPTO_DRIVER_TEST)
638
639static int is_mac_accelerated( psa_algorithm_t alg )
640{
641#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
642 if( PSA_ALG_IS_HMAC( alg ) )
643 return( 1 );
644#endif
645
646 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
647 {
648#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
649 case PSA_ALG_CMAC:
650 return( 1 );
651#endif
652 default:
653 return( 0 );
654 }
655}
656
657psa_status_t mbedtls_transparent_test_driver_mac_compute(
658 const psa_key_attributes_t *attributes,
659 const uint8_t *key_buffer,
660 size_t key_buffer_size,
661 psa_algorithm_t alg,
662 const uint8_t *input,
663 size_t input_length,
664 uint8_t *mac,
665 size_t mac_size,
666 size_t *mac_length )
667{
668 if( is_mac_accelerated( alg ) )
669 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
670 input, input_length,
671 mac, mac_size, mac_length ) );
672 else
673 return( PSA_ERROR_NOT_SUPPORTED );
674}
675
676psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
677 mbedtls_transparent_test_driver_mac_operation_t *operation,
678 const psa_key_attributes_t *attributes,
679 const uint8_t *key_buffer,
680 size_t key_buffer_size,
681 psa_algorithm_t alg )
682{
683 if( is_mac_accelerated( alg ) )
684 return( mac_sign_setup( operation, attributes,
685 key_buffer, key_buffer_size, alg ) );
686 else
687 return( PSA_ERROR_NOT_SUPPORTED );
688}
689
690psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
691 mbedtls_transparent_test_driver_mac_operation_t *operation,
692 const psa_key_attributes_t *attributes,
693 const uint8_t *key_buffer,
694 size_t key_buffer_size,
695 psa_algorithm_t alg )
696{
697 if( is_mac_accelerated( alg ) )
698 return( mac_verify_setup( operation, attributes,
699 key_buffer, key_buffer_size, alg ) );
700 else
701 return( PSA_ERROR_NOT_SUPPORTED );
702}
703
704psa_status_t mbedtls_transparent_test_driver_mac_update(
705 mbedtls_transparent_test_driver_mac_operation_t *operation,
706 const uint8_t *input,
707 size_t input_length )
708{
709 if( is_mac_accelerated( operation->alg ) )
710 return( mac_update( operation, input, input_length ) );
711 else
712 return( PSA_ERROR_BAD_STATE );
713}
714
715psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
716 mbedtls_transparent_test_driver_mac_operation_t *operation,
717 uint8_t *mac,
718 size_t mac_size,
719 size_t *mac_length )
720{
721 if( is_mac_accelerated( operation->alg ) )
722 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
723 else
724 return( PSA_ERROR_BAD_STATE );
725}
726
727psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
728 mbedtls_transparent_test_driver_mac_operation_t *operation,
729 const uint8_t *mac,
730 size_t mac_length )
731{
732 if( is_mac_accelerated( operation->alg ) )
733 return( mac_verify_finish( operation, mac, mac_length ) );
734 else
735 return( PSA_ERROR_BAD_STATE );
736}
737
738psa_status_t mbedtls_transparent_test_driver_mac_abort(
739 mbedtls_transparent_test_driver_mac_operation_t *operation )
740{
741 return( mac_abort( operation ) );
742}
743
744psa_status_t mbedtls_opaque_test_driver_mac_compute(
745 const psa_key_attributes_t *attributes,
746 const uint8_t *key_buffer,
747 size_t key_buffer_size,
748 psa_algorithm_t alg,
749 const uint8_t *input,
750 size_t input_length,
751 uint8_t *mac,
752 size_t mac_size,
753 size_t *mac_length )
754{
755 /* Opaque driver testing is not implemented yet through this mechanism. */
756 (void) attributes;
757 (void) key_buffer;
758 (void) key_buffer_size;
759 (void) alg;
760 (void) input;
761 (void) input_length;
762 (void) mac;
763 (void) mac_size;
764 (void) mac_length;
765 return( PSA_ERROR_NOT_SUPPORTED );
766}
767
768psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
769 mbedtls_opaque_test_driver_mac_operation_t *operation,
770 const psa_key_attributes_t *attributes,
771 const uint8_t *key_buffer,
772 size_t key_buffer_size,
773 psa_algorithm_t alg )
774{
775 /* Opaque driver testing is not implemented yet through this mechanism. */
776 (void) operation;
777 (void) attributes;
778 (void) key_buffer;
779 (void) key_buffer_size;
780 (void) alg;
781 return( PSA_ERROR_NOT_SUPPORTED );
782}
783
784psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
785 mbedtls_opaque_test_driver_mac_operation_t *operation,
786 const psa_key_attributes_t *attributes,
787 const uint8_t *key_buffer,
788 size_t key_buffer_size,
789 psa_algorithm_t alg )
790{
791 /* Opaque driver testing is not implemented yet through this mechanism. */
792 (void) operation;
793 (void) attributes;
794 (void) key_buffer;
795 (void) key_buffer_size;
796 (void) alg;
797 return( PSA_ERROR_NOT_SUPPORTED );
798}
799
800psa_status_t mbedtls_opaque_test_driver_mac_update(
801 mbedtls_opaque_test_driver_mac_operation_t *operation,
802 const uint8_t *input,
803 size_t input_length )
804{
805 /* Opaque driver testing is not implemented yet through this mechanism. */
806 (void) operation;
807 (void) input;
808 (void) input_length;
809 return( PSA_ERROR_NOT_SUPPORTED );
810}
811
812psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
813 mbedtls_opaque_test_driver_mac_operation_t *operation,
814 uint8_t *mac,
815 size_t mac_size,
816 size_t *mac_length )
817{
818 /* Opaque driver testing is not implemented yet through this mechanism. */
819 (void) operation;
820 (void) mac;
821 (void) mac_size;
822 (void) mac_length;
823 return( PSA_ERROR_NOT_SUPPORTED );
824}
825
826psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
827 mbedtls_opaque_test_driver_mac_operation_t *operation,
828 const uint8_t *mac,
829 size_t mac_length )
830{
831 /* Opaque driver testing is not implemented yet through this mechanism. */
832 (void) operation;
833 (void) mac;
834 (void) mac_length;
835 return( PSA_ERROR_NOT_SUPPORTED );
836}
837
838psa_status_t mbedtls_opaque_test_driver_mac_abort(
839 mbedtls_opaque_test_driver_mac_operation_t *operation )
840{
841 /* Opaque driver testing is not implemented yet through this mechanism. */
842 (void) operation;
843 return( PSA_ERROR_NOT_SUPPORTED );
844}
845
846#endif /* PSA_CRYPTO_DRIVER_TEST */
847
848#endif /* MBEDTLS_PSA_CRYPTO_C */