blob: 0189cded8a72577946d2bb4c331b14bfb568b572 [file] [log] [blame]
Steven Cooreman896d51e2021-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 Cooreman32d56942021-03-19 17:39:17 +010028#include <mbedtls/md.h>
Steven Cooreman896d51e2021-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 Cooreman32d56942021-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 Cooremanf64b25e2021-03-22 14:49:06 +010072psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data_t *hmac )
Steven Cooreman32d56942021-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 Cooremanf64b25e2021-03-22 14:49:06 +010078psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman32d56942021-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 Cooremanf64b25e2021-03-22 14:49:06 +0100142psa_status_t psa_hmac_update_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman76720f62021-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 Cooremanf64b25e2021-03-22 14:49:06 +0100149psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data_t *hmac,
Steven Cooreman32d56942021-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 Cooreman76720f62021-03-22 12:21:10 +0100186
Steven Cooremanf64b25e2021-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 Cooreman76720f62021-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 Cooreman32d56942021-03-19 17:39:17 +0100202#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || PSA_CRYPTO_DRIVER_TEST */
203
Steven Cooreman896d51e2021-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 Cooreman07897832021-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{
246 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
247
248 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
249 operation->key_set = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100250 operation->has_input = 0;
251 operation->is_sign = 0;
252
253#if defined(BUILTIN_ALG_CMAC)
254 if( operation->alg == PSA_ALG_CMAC )
255 {
Steven Cooreman07897832021-03-19 18:28:56 +0100256 mbedtls_cipher_init( &operation->ctx.cmac );
257 status = PSA_SUCCESS;
258 }
259 else
260#endif /* BUILTIN_ALG_CMAC */
261#if defined(BUILTIN_ALG_HMAC)
262 if( PSA_ALG_IS_HMAC( operation->alg ) )
263 {
264 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
265 operation->ctx.hmac.alg = 0;
266 status = PSA_SUCCESS;
267 }
268 else
269#endif /* BUILTIN_ALG_HMAC */
270 {
271 if( ! PSA_ALG_IS_MAC( alg ) )
272 status = PSA_ERROR_INVALID_ARGUMENT;
273 }
274
275 if( status != PSA_SUCCESS )
276 memset( operation, 0, sizeof( *operation ) );
277 return( status );
278}
279
280static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
281{
282 if( operation->alg == 0 )
283 {
284 /* The object has (apparently) been initialized but it is not
285 * in use. It's ok to call abort on such an object, and there's
286 * nothing to do. */
287 return( PSA_SUCCESS );
288 }
289 else
290#if defined(BUILTIN_ALG_CMAC)
291 if( operation->alg == PSA_ALG_CMAC )
292 {
293 mbedtls_cipher_free( &operation->ctx.cmac );
294 }
295 else
296#endif /* BUILTIN_ALG_CMAC */
297#if defined(BUILTIN_ALG_HMAC)
298 if( PSA_ALG_IS_HMAC( operation->alg ) )
299 {
300 psa_hmac_abort_internal( &operation->ctx.hmac );
301 }
302 else
303#endif /* BUILTIN_ALG_HMAC */
304 {
305 /* Sanity check (shouldn't happen: operation->alg should
306 * always have been initialized to a valid value). */
307 goto bad_state;
308 }
309
310 operation->alg = 0;
311 operation->key_set = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100312 operation->has_input = 0;
313 operation->is_sign = 0;
314
315 return( PSA_SUCCESS );
316
317bad_state:
318 /* If abort is called on an uninitialized object, we can't trust
319 * anything. Wipe the object in case it contains confidential data.
320 * This may result in a memory leak if a pointer gets overwritten,
321 * but it's too late to do anything about this. */
322 memset( operation, 0, sizeof( *operation ) );
323 return( PSA_ERROR_BAD_STATE );
324}
325
326static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
327 const psa_key_attributes_t *attributes,
328 const uint8_t *key_buffer,
329 size_t key_buffer_size,
330 psa_algorithm_t alg,
331 int is_sign )
332{
333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
334
335 /* A context must be freshly initialized before it can be set up. */
336 if( operation->alg != 0 )
337 {
338 return( PSA_ERROR_BAD_STATE );
339 }
340
341 status = mac_init( operation, alg );
342 if( status != PSA_SUCCESS )
343 return( status );
344 if( is_sign )
345 operation->is_sign = 1;
346
347 /* Get the output length for the algorithm and key combination. None of the
348 * currently supported algorithms have an output length dependent on actual
349 * key size, so setting it to a bogus value is currently OK. */
350 operation->mac_size =
351 PSA_MAC_LENGTH( psa_get_key_type( attributes ), 0, alg );
352
353#if defined(BUILTIN_ALG_CMAC)
354 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
355 {
356 status = cmac_setup( operation, attributes,
357 key_buffer, key_buffer_size );
358 }
359 else
360#endif /* BUILTIN_ALG_CMAC */
361#if defined(BUILTIN_ALG_HMAC)
362 if( PSA_ALG_IS_HMAC( alg ) )
363 {
364 /* Sanity check. This shouldn't fail on a valid configuration. */
365 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
366 {
367 status = PSA_ERROR_NOT_SUPPORTED;
368 goto exit;
369 }
370
371 if( psa_get_key_type( attributes ) != PSA_KEY_TYPE_HMAC )
372 {
373 status = PSA_ERROR_INVALID_ARGUMENT;
374 goto exit;
375 }
376
377 status = psa_hmac_setup_internal( &operation->ctx.hmac,
378 key_buffer,
379 key_buffer_size,
380 PSA_ALG_HMAC_GET_HASH( alg ) );
381 }
382 else
383#endif /* BUILTIN_ALG_HMAC */
384 {
385 status = PSA_ERROR_NOT_SUPPORTED;
386 }
387
388exit:
389 if( status == PSA_SUCCESS )
390 operation->key_set = 1;
391 else
392 mac_abort( operation );
393
394 return( status );
395}
396
Steven Cooreman896d51e2021-03-19 15:24:23 +0100397static psa_status_t mac_compute(
398 const psa_key_attributes_t *attributes,
399 const uint8_t *key_buffer,
400 size_t key_buffer_size,
401 psa_algorithm_t alg,
402 const uint8_t *input,
403 size_t input_length,
404 uint8_t *mac,
405 size_t mac_size,
406 size_t *mac_length )
407{
Steven Cooreman32d56942021-03-19 17:39:17 +0100408 /* One-shot MAC has not been implemented in this PSA implementation yet. */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100409 (void) attributes;
410 (void) key_buffer;
411 (void) key_buffer_size;
412 (void) alg;
413 (void) input;
414 (void) input_length;
415 (void) mac;
416 (void) mac_size;
417 (void) mac_length;
418 return( PSA_ERROR_NOT_SUPPORTED );
419}
420
421static psa_status_t mac_sign_setup(
422 mbedtls_psa_mac_operation_t *operation,
423 const psa_key_attributes_t *attributes,
424 const uint8_t *key_buffer,
425 size_t key_buffer_size,
426 psa_algorithm_t alg )
427{
Steven Cooreman07897832021-03-19 18:28:56 +0100428 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
429 1 ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100430}
431
432static psa_status_t mac_verify_setup(
433 mbedtls_psa_mac_operation_t *operation,
434 const psa_key_attributes_t *attributes,
435 const uint8_t *key_buffer,
436 size_t key_buffer_size,
437 psa_algorithm_t alg )
438{
Steven Cooreman07897832021-03-19 18:28:56 +0100439 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
440 0 ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100441}
442
443static psa_status_t mac_update(
444 mbedtls_psa_mac_operation_t *operation,
445 const uint8_t *input,
446 size_t input_length )
447{
Steven Cooreman11743f92021-03-19 18:38:46 +0100448 if( ! operation->key_set )
449 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100450 operation->has_input = 1;
451
452#if defined(BUILTIN_ALG_CMAC)
453 if( operation->alg == PSA_ALG_CMAC )
454 {
455 return( mbedtls_to_psa_error(
456 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
457 input, input_length ) ) );
458 }
459 else
460#endif /* BUILTIN_ALG_CMAC */
461#if defined(BUILTIN_ALG_HMAC)
462 if( PSA_ALG_IS_HMAC( operation->alg ) )
463 {
464 return( psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
465 input_length ) );
466 }
467 else
468#endif /* BUILTIN_ALG_HMAC */
469 {
470 /* This shouldn't happen if `operation` was initialized by
471 * a setup function. */
472 return( PSA_ERROR_BAD_STATE );
473 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100474}
475
Steven Cooreman87885df2021-03-19 19:04:39 +0100476static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
477 uint8_t *mac,
478 size_t mac_size )
479{
480 if( ! operation->key_set )
481 return( PSA_ERROR_BAD_STATE );
Steven Cooreman87885df2021-03-19 19:04:39 +0100482
483 if( mac_size < operation->mac_size )
484 return( PSA_ERROR_BUFFER_TOO_SMALL );
485
486#if defined(BUILTIN_ALG_CMAC)
487 if( operation->alg == PSA_ALG_CMAC )
488 {
489 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
490 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
491 if( ret == 0 )
492 memcpy( mac, tmp, operation->mac_size );
493 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
494 return( mbedtls_to_psa_error( ret ) );
495 }
496 else
497#endif /* BUILTIN_ALG_CMAC */
498#if defined(BUILTIN_ALG_HMAC)
499 if( PSA_ALG_IS_HMAC( operation->alg ) )
500 {
501 return( psa_hmac_finish_internal( &operation->ctx.hmac,
502 mac, operation->mac_size ) );
503 }
504 else
505#endif /* BUILTIN_ALG_HMAC */
506 {
507 /* This shouldn't happen if `operation` was initialized by
508 * a setup function. */
509 return( PSA_ERROR_BAD_STATE );
510 }
511}
512
Steven Cooreman896d51e2021-03-19 15:24:23 +0100513static psa_status_t mac_sign_finish(
514 mbedtls_psa_mac_operation_t *operation,
515 uint8_t *mac,
516 size_t mac_size,
517 size_t *mac_length )
518{
Steven Cooreman87885df2021-03-19 19:04:39 +0100519 psa_status_t status;
520
521 if( operation->alg == 0 )
522 return( PSA_ERROR_BAD_STATE );
523
524 if( ! operation->is_sign )
525 return( PSA_ERROR_BAD_STATE );
526
527 status = mac_finish_internal( operation, mac, mac_size );
528
529 if( status == PSA_SUCCESS )
530 *mac_length = operation->mac_size;
531
532 return( status );
533}
534
535/* constant-time buffer comparison */
536static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
537{
538 size_t i;
539 unsigned char diff = 0;
540
541 for( i = 0; i < n; i++ )
542 diff |= a[i] ^ b[i];
543
544 return( diff );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100545}
546
547static psa_status_t mac_verify_finish(
548 mbedtls_psa_mac_operation_t *operation,
549 const uint8_t *mac,
550 size_t mac_length )
551{
Steven Cooreman87885df2021-03-19 19:04:39 +0100552 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
553 psa_status_t status;
554
555 if( operation->alg == 0 )
556 return( PSA_ERROR_BAD_STATE );
557
558 if( operation->is_sign )
559 return( PSA_ERROR_BAD_STATE );
560
561 if( operation->mac_size != mac_length )
562 {
563 status = PSA_ERROR_INVALID_SIGNATURE;
564 goto cleanup;
565 }
566
567 status = mac_finish_internal( operation, actual_mac, sizeof( actual_mac ) );
568 if( status != PSA_SUCCESS )
569 goto cleanup;
570
571 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
572 status = PSA_ERROR_INVALID_SIGNATURE;
573
574cleanup:
575 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
576
577 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100578}
Steven Cooreman896d51e2021-03-19 15:24:23 +0100579#endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */
580
581#if defined(MBEDTLS_PSA_BUILTIN_MAC)
582psa_status_t mbedtls_psa_mac_compute(
583 const psa_key_attributes_t *attributes,
584 const uint8_t *key_buffer,
585 size_t key_buffer_size,
586 psa_algorithm_t alg,
587 const uint8_t *input,
588 size_t input_length,
589 uint8_t *mac,
590 size_t mac_size,
591 size_t *mac_length )
592{
593 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
594 input, input_length,
595 mac, mac_size, mac_length ) );
596}
597
598psa_status_t mbedtls_psa_mac_sign_setup(
599 mbedtls_psa_mac_operation_t *operation,
600 const psa_key_attributes_t *attributes,
601 const uint8_t *key_buffer,
602 size_t key_buffer_size,
603 psa_algorithm_t alg )
604{
605 return( mac_sign_setup( operation, attributes,
606 key_buffer, key_buffer_size, alg ) );
607}
608
609psa_status_t mbedtls_psa_mac_verify_setup(
610 mbedtls_psa_mac_operation_t *operation,
611 const psa_key_attributes_t *attributes,
612 const uint8_t *key_buffer,
613 size_t key_buffer_size,
614 psa_algorithm_t alg )
615{
616 return( mac_verify_setup( operation, attributes,
617 key_buffer, key_buffer_size, alg ) );
618}
619
620psa_status_t mbedtls_psa_mac_update(
621 mbedtls_psa_mac_operation_t *operation,
622 const uint8_t *input,
623 size_t input_length )
624{
625 return( mac_update( operation, input, input_length ) );
626}
627
628psa_status_t mbedtls_psa_mac_sign_finish(
629 mbedtls_psa_mac_operation_t *operation,
630 uint8_t *mac,
631 size_t mac_size,
632 size_t *mac_length )
633{
634 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
635}
636
637psa_status_t mbedtls_psa_mac_verify_finish(
638 mbedtls_psa_mac_operation_t *operation,
639 const uint8_t *mac,
640 size_t mac_length )
641{
642 return( mac_verify_finish( operation, mac, mac_length ) );
643}
644
645psa_status_t mbedtls_psa_mac_abort(
646 mbedtls_psa_mac_operation_t *operation )
647{
648 return( mac_abort( operation ) );
649}
650#endif /* MBEDTLS_PSA_BUILTIN_MAC */
651
652 /*
653 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
654 */
655#if defined(PSA_CRYPTO_DRIVER_TEST)
656
657static int is_mac_accelerated( psa_algorithm_t alg )
658{
659#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
660 if( PSA_ALG_IS_HMAC( alg ) )
661 return( 1 );
662#endif
663
664 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
665 {
666#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
667 case PSA_ALG_CMAC:
668 return( 1 );
669#endif
670 default:
671 return( 0 );
672 }
673}
674
675psa_status_t mbedtls_transparent_test_driver_mac_compute(
676 const psa_key_attributes_t *attributes,
677 const uint8_t *key_buffer,
678 size_t key_buffer_size,
679 psa_algorithm_t alg,
680 const uint8_t *input,
681 size_t input_length,
682 uint8_t *mac,
683 size_t mac_size,
684 size_t *mac_length )
685{
686 if( is_mac_accelerated( alg ) )
687 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
688 input, input_length,
689 mac, mac_size, mac_length ) );
690 else
691 return( PSA_ERROR_NOT_SUPPORTED );
692}
693
694psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
695 mbedtls_transparent_test_driver_mac_operation_t *operation,
696 const psa_key_attributes_t *attributes,
697 const uint8_t *key_buffer,
698 size_t key_buffer_size,
699 psa_algorithm_t alg )
700{
701 if( is_mac_accelerated( alg ) )
702 return( mac_sign_setup( operation, attributes,
703 key_buffer, key_buffer_size, alg ) );
704 else
705 return( PSA_ERROR_NOT_SUPPORTED );
706}
707
708psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
709 mbedtls_transparent_test_driver_mac_operation_t *operation,
710 const psa_key_attributes_t *attributes,
711 const uint8_t *key_buffer,
712 size_t key_buffer_size,
713 psa_algorithm_t alg )
714{
715 if( is_mac_accelerated( alg ) )
716 return( mac_verify_setup( operation, attributes,
717 key_buffer, key_buffer_size, alg ) );
718 else
719 return( PSA_ERROR_NOT_SUPPORTED );
720}
721
722psa_status_t mbedtls_transparent_test_driver_mac_update(
723 mbedtls_transparent_test_driver_mac_operation_t *operation,
724 const uint8_t *input,
725 size_t input_length )
726{
727 if( is_mac_accelerated( operation->alg ) )
728 return( mac_update( operation, input, input_length ) );
729 else
730 return( PSA_ERROR_BAD_STATE );
731}
732
733psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
734 mbedtls_transparent_test_driver_mac_operation_t *operation,
735 uint8_t *mac,
736 size_t mac_size,
737 size_t *mac_length )
738{
739 if( is_mac_accelerated( operation->alg ) )
740 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
741 else
742 return( PSA_ERROR_BAD_STATE );
743}
744
745psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
746 mbedtls_transparent_test_driver_mac_operation_t *operation,
747 const uint8_t *mac,
748 size_t mac_length )
749{
750 if( is_mac_accelerated( operation->alg ) )
751 return( mac_verify_finish( operation, mac, mac_length ) );
752 else
753 return( PSA_ERROR_BAD_STATE );
754}
755
756psa_status_t mbedtls_transparent_test_driver_mac_abort(
757 mbedtls_transparent_test_driver_mac_operation_t *operation )
758{
759 return( mac_abort( operation ) );
760}
761
762psa_status_t mbedtls_opaque_test_driver_mac_compute(
763 const psa_key_attributes_t *attributes,
764 const uint8_t *key_buffer,
765 size_t key_buffer_size,
766 psa_algorithm_t alg,
767 const uint8_t *input,
768 size_t input_length,
769 uint8_t *mac,
770 size_t mac_size,
771 size_t *mac_length )
772{
773 /* Opaque driver testing is not implemented yet through this mechanism. */
774 (void) attributes;
775 (void) key_buffer;
776 (void) key_buffer_size;
777 (void) alg;
778 (void) input;
779 (void) input_length;
780 (void) mac;
781 (void) mac_size;
782 (void) mac_length;
783 return( PSA_ERROR_NOT_SUPPORTED );
784}
785
786psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
787 mbedtls_opaque_test_driver_mac_operation_t *operation,
788 const psa_key_attributes_t *attributes,
789 const uint8_t *key_buffer,
790 size_t key_buffer_size,
791 psa_algorithm_t alg )
792{
793 /* Opaque driver testing is not implemented yet through this mechanism. */
794 (void) operation;
795 (void) attributes;
796 (void) key_buffer;
797 (void) key_buffer_size;
798 (void) alg;
799 return( PSA_ERROR_NOT_SUPPORTED );
800}
801
802psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
803 mbedtls_opaque_test_driver_mac_operation_t *operation,
804 const psa_key_attributes_t *attributes,
805 const uint8_t *key_buffer,
806 size_t key_buffer_size,
807 psa_algorithm_t alg )
808{
809 /* Opaque driver testing is not implemented yet through this mechanism. */
810 (void) operation;
811 (void) attributes;
812 (void) key_buffer;
813 (void) key_buffer_size;
814 (void) alg;
815 return( PSA_ERROR_NOT_SUPPORTED );
816}
817
818psa_status_t mbedtls_opaque_test_driver_mac_update(
819 mbedtls_opaque_test_driver_mac_operation_t *operation,
820 const uint8_t *input,
821 size_t input_length )
822{
823 /* Opaque driver testing is not implemented yet through this mechanism. */
824 (void) operation;
825 (void) input;
826 (void) input_length;
827 return( PSA_ERROR_NOT_SUPPORTED );
828}
829
830psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
831 mbedtls_opaque_test_driver_mac_operation_t *operation,
832 uint8_t *mac,
833 size_t mac_size,
834 size_t *mac_length )
835{
836 /* Opaque driver testing is not implemented yet through this mechanism. */
837 (void) operation;
838 (void) mac;
839 (void) mac_size;
840 (void) mac_length;
841 return( PSA_ERROR_NOT_SUPPORTED );
842}
843
844psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
845 mbedtls_opaque_test_driver_mac_operation_t *operation,
846 const uint8_t *mac,
847 size_t mac_length )
848{
849 /* Opaque driver testing is not implemented yet through this mechanism. */
850 (void) operation;
851 (void) mac;
852 (void) mac_length;
853 return( PSA_ERROR_NOT_SUPPORTED );
854}
855
856psa_status_t mbedtls_opaque_test_driver_mac_abort(
857 mbedtls_opaque_test_driver_mac_operation_t *operation )
858{
859 /* Opaque driver testing is not implemented yet through this mechanism. */
860 (void) operation;
861 return( PSA_ERROR_NOT_SUPPORTED );
862}
863
864#endif /* PSA_CRYPTO_DRIVER_TEST */
865
866#endif /* MBEDTLS_PSA_CRYPTO_C */