blob: ca40b03bf7e03140fb1e75e61b3c053a6a8ffb45 [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 Cooreman22dea1d2021-04-29 19:32:25 +020072static psa_status_t psa_hmac_abort_internal(
73 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman32d56942021-03-19 17:39:17 +010074{
75 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
76 return( psa_hash_abort( &hmac->hash_ctx ) );
77}
78
Steven Cooreman22dea1d2021-04-29 19:32:25 +020079static psa_status_t psa_hmac_setup_internal(
80 mbedtls_psa_hmac_operation_t *hmac,
81 const uint8_t *key,
82 size_t key_length,
83 psa_algorithm_t hash_alg )
Steven Cooreman32d56942021-03-19 17:39:17 +010084{
85 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
86 size_t i;
87 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
88 size_t block_size = psa_get_hash_block_size( hash_alg );
89 psa_status_t status;
90
91 hmac->alg = hash_alg;
92
93 /* Sanity checks on block_size, to guarantee that there won't be a buffer
94 * overflow below. This should never trigger if the hash algorithm
95 * is implemented correctly. */
96 /* The size checks against the ipad and opad buffers cannot be written
97 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
98 * because that triggers -Wlogical-op on GCC 7.3. */
99 if( block_size > sizeof( ipad ) )
100 return( PSA_ERROR_NOT_SUPPORTED );
101 if( block_size > sizeof( hmac->opad ) )
102 return( PSA_ERROR_NOT_SUPPORTED );
103 if( block_size < hash_size )
104 return( PSA_ERROR_NOT_SUPPORTED );
105
106 if( key_length > block_size )
107 {
108 status = psa_hash_compute( hash_alg, key, key_length,
109 ipad, sizeof( ipad ), &key_length );
110 if( status != PSA_SUCCESS )
111 goto cleanup;
112 }
113 /* A 0-length key is not commonly used in HMAC when used as a MAC,
114 * but it is permitted. It is common when HMAC is used in HKDF, for
115 * example. Don't call `memcpy` in the 0-length because `key` could be
116 * an invalid pointer which would make the behavior undefined. */
117 else if( key_length != 0 )
118 memcpy( ipad, key, key_length );
119
120 /* ipad contains the key followed by garbage. Xor and fill with 0x36
121 * to create the ipad value. */
122 for( i = 0; i < key_length; i++ )
123 ipad[i] ^= 0x36;
124 memset( ipad + key_length, 0x36, block_size - key_length );
125
126 /* Copy the key material from ipad to opad, flipping the requisite bits,
127 * and filling the rest of opad with the requisite constant. */
128 for( i = 0; i < key_length; i++ )
129 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
130 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
131
132 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
133 if( status != PSA_SUCCESS )
134 goto cleanup;
135
136 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
137
138cleanup:
139 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
140
141 return( status );
142}
143
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200144static psa_status_t psa_hmac_update_internal(
145 mbedtls_psa_hmac_operation_t *hmac,
146 const uint8_t *data,
147 size_t data_length )
Steven Cooreman76720f62021-03-22 12:21:10 +0100148{
149 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
150}
151
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200152static psa_status_t psa_hmac_finish_internal(
153 mbedtls_psa_hmac_operation_t *hmac,
154 uint8_t *mac,
155 size_t mac_size )
Steven Cooreman32d56942021-03-19 17:39:17 +0100156{
157 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
158 psa_algorithm_t hash_alg = hmac->alg;
159 size_t hash_size = 0;
160 size_t block_size = psa_get_hash_block_size( hash_alg );
161 psa_status_t status;
162
163 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
164 if( status != PSA_SUCCESS )
165 return( status );
166 /* From here on, tmp needs to be wiped. */
167
168 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
169 if( status != PSA_SUCCESS )
170 goto exit;
171
172 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
173 if( status != PSA_SUCCESS )
174 goto exit;
175
176 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
177 if( status != PSA_SUCCESS )
178 goto exit;
179
180 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
181 if( status != PSA_SUCCESS )
182 goto exit;
183
184 memcpy( mac, tmp, mac_size );
185
186exit:
187 mbedtls_platform_zeroize( tmp, hash_size );
188 return( status );
189}
190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || PSA_CRYPTO_DRIVER_TEST */
191
Steven Cooreman896d51e2021-03-19 15:24:23 +0100192/* Implement the PSA driver MAC interface on top of mbed TLS if either the
193 * software driver or the test driver requires it. */
194#if defined(MBEDTLS_PSA_BUILTIN_MAC) || defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman07897832021-03-19 18:28:56 +0100195
196#if defined(BUILTIN_ALG_CMAC)
197static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
198 const psa_key_attributes_t *attributes,
199 const uint8_t *key_buffer,
200 size_t key_buffer_size )
201{
202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
203 const mbedtls_cipher_info_t * cipher_info_tmp =
204 mbedtls_cipher_info_from_psa(
205 PSA_ALG_CMAC,
206 psa_get_key_type( attributes ),
207 psa_get_key_bits( attributes ),
208 NULL );
209
210 if( cipher_info_tmp == NULL )
211 return( PSA_ERROR_NOT_SUPPORTED );
212
213 if( key_buffer_size < PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) )
214 return( PSA_ERROR_INVALID_ARGUMENT );
215
216 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info_tmp );
217 if( ret != 0 )
218 goto exit;
219
220 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
221 key_buffer,
222 psa_get_key_bits( attributes ) );
223exit:
224 return( mbedtls_to_psa_error( ret ) );
225}
226#endif /* BUILTIN_ALG_CMAC */
227
228/* Initialize this driver's MAC operation structure. Once this function has been
229 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
230static psa_status_t mac_init(
231 mbedtls_psa_mac_operation_t *operation,
232 psa_algorithm_t alg )
233{
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100235
236 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Steven Cooreman07897832021-03-19 18:28:56 +0100237 operation->has_input = 0;
238 operation->is_sign = 0;
239
240#if defined(BUILTIN_ALG_CMAC)
241 if( operation->alg == PSA_ALG_CMAC )
242 {
Steven Cooreman07897832021-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 Cooreman6e6451e2021-04-29 16:21:24 +0200258 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman07897832021-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)
277 if( operation->alg == PSA_ALG_CMAC )
278 {
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 Cooreman07897832021-03-19 18:28:56 +0100297 operation->has_input = 0;
298 operation->is_sign = 0;
299
300 return( PSA_SUCCESS );
301
302bad_state:
303 /* If abort is called on an uninitialized object, we can't trust
304 * anything. Wipe the object in case it contains confidential data.
305 * This may result in a memory leak if a pointer gets overwritten,
306 * but it's too late to do anything about this. */
307 memset( operation, 0, sizeof( *operation ) );
308 return( PSA_ERROR_BAD_STATE );
309}
310
311static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
312 const psa_key_attributes_t *attributes,
313 const uint8_t *key_buffer,
314 size_t key_buffer_size,
315 psa_algorithm_t alg,
316 int is_sign )
317{
318 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
319
320 /* A context must be freshly initialized before it can be set up. */
321 if( operation->alg != 0 )
322 {
323 return( PSA_ERROR_BAD_STATE );
324 }
325
326 status = mac_init( operation, alg );
327 if( status != PSA_SUCCESS )
328 return( status );
329 if( is_sign )
330 operation->is_sign = 1;
331
332 /* Get the output length for the algorithm and key combination. None of the
333 * currently supported algorithms have an output length dependent on actual
334 * key size, so setting it to a bogus value is currently OK. */
335 operation->mac_size =
336 PSA_MAC_LENGTH( psa_get_key_type( attributes ), 0, alg );
337
338#if defined(BUILTIN_ALG_CMAC)
339 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
340 {
341 status = cmac_setup( operation, attributes,
342 key_buffer, key_buffer_size );
343 }
344 else
345#endif /* BUILTIN_ALG_CMAC */
346#if defined(BUILTIN_ALG_HMAC)
347 if( PSA_ALG_IS_HMAC( alg ) )
348 {
349 /* Sanity check. This shouldn't fail on a valid configuration. */
350 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
351 {
352 status = PSA_ERROR_NOT_SUPPORTED;
353 goto exit;
354 }
355
356 if( psa_get_key_type( attributes ) != PSA_KEY_TYPE_HMAC )
357 {
358 status = PSA_ERROR_INVALID_ARGUMENT;
359 goto exit;
360 }
361
362 status = psa_hmac_setup_internal( &operation->ctx.hmac,
363 key_buffer,
364 key_buffer_size,
365 PSA_ALG_HMAC_GET_HASH( alg ) );
366 }
367 else
368#endif /* BUILTIN_ALG_HMAC */
369 {
370 status = PSA_ERROR_NOT_SUPPORTED;
371 }
372
373exit:
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200374 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +0100375 mac_abort( operation );
376
377 return( status );
378}
379
Steven Cooreman896d51e2021-03-19 15:24:23 +0100380static psa_status_t mac_compute(
381 const psa_key_attributes_t *attributes,
382 const uint8_t *key_buffer,
383 size_t key_buffer_size,
384 psa_algorithm_t alg,
385 const uint8_t *input,
386 size_t input_length,
387 uint8_t *mac,
388 size_t mac_size,
389 size_t *mac_length )
390{
Steven Cooreman32d56942021-03-19 17:39:17 +0100391 /* One-shot MAC has not been implemented in this PSA implementation yet. */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100392 (void) attributes;
393 (void) key_buffer;
394 (void) key_buffer_size;
395 (void) alg;
396 (void) input;
397 (void) input_length;
398 (void) mac;
399 (void) mac_size;
400 (void) mac_length;
401 return( PSA_ERROR_NOT_SUPPORTED );
402}
403
404static psa_status_t mac_sign_setup(
405 mbedtls_psa_mac_operation_t *operation,
406 const psa_key_attributes_t *attributes,
407 const uint8_t *key_buffer,
408 size_t key_buffer_size,
409 psa_algorithm_t alg )
410{
Steven Cooreman07897832021-03-19 18:28:56 +0100411 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
412 1 ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100413}
414
415static psa_status_t mac_verify_setup(
416 mbedtls_psa_mac_operation_t *operation,
417 const psa_key_attributes_t *attributes,
418 const uint8_t *key_buffer,
419 size_t key_buffer_size,
420 psa_algorithm_t alg )
421{
Steven Cooreman07897832021-03-19 18:28:56 +0100422 return( mac_setup( operation, attributes, key_buffer, key_buffer_size, alg,
423 0 ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100424}
425
426static psa_status_t mac_update(
427 mbedtls_psa_mac_operation_t *operation,
428 const uint8_t *input,
429 size_t input_length )
430{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200431 if( operation->alg == 0 )
Steven Cooreman11743f92021-03-19 18:38:46 +0100432 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100433 operation->has_input = 1;
434
435#if defined(BUILTIN_ALG_CMAC)
436 if( operation->alg == PSA_ALG_CMAC )
437 {
438 return( mbedtls_to_psa_error(
439 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
440 input, input_length ) ) );
441 }
442 else
443#endif /* BUILTIN_ALG_CMAC */
444#if defined(BUILTIN_ALG_HMAC)
445 if( PSA_ALG_IS_HMAC( operation->alg ) )
446 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200447 return( psa_hmac_update_internal( &operation->ctx.hmac,
448 input, input_length ) );
Steven Cooreman11743f92021-03-19 18:38:46 +0100449 }
450 else
451#endif /* BUILTIN_ALG_HMAC */
452 {
453 /* This shouldn't happen if `operation` was initialized by
454 * a setup function. */
455 return( PSA_ERROR_BAD_STATE );
456 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100457}
458
Steven Cooreman87885df2021-03-19 19:04:39 +0100459static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
460 uint8_t *mac,
461 size_t mac_size )
462{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200463 if( operation->alg == 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100464 return( PSA_ERROR_BAD_STATE );
Steven Cooreman87885df2021-03-19 19:04:39 +0100465 if( mac_size < operation->mac_size )
466 return( PSA_ERROR_BUFFER_TOO_SMALL );
467
468#if defined(BUILTIN_ALG_CMAC)
469 if( operation->alg == PSA_ALG_CMAC )
470 {
471 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
472 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
473 if( ret == 0 )
474 memcpy( mac, tmp, operation->mac_size );
475 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
476 return( mbedtls_to_psa_error( ret ) );
477 }
478 else
479#endif /* BUILTIN_ALG_CMAC */
480#if defined(BUILTIN_ALG_HMAC)
481 if( PSA_ALG_IS_HMAC( operation->alg ) )
482 {
483 return( psa_hmac_finish_internal( &operation->ctx.hmac,
484 mac, operation->mac_size ) );
485 }
486 else
487#endif /* BUILTIN_ALG_HMAC */
488 {
489 /* This shouldn't happen if `operation` was initialized by
490 * a setup function. */
491 return( PSA_ERROR_BAD_STATE );
492 }
493}
494
Steven Cooreman896d51e2021-03-19 15:24:23 +0100495static psa_status_t mac_sign_finish(
496 mbedtls_psa_mac_operation_t *operation,
497 uint8_t *mac,
498 size_t mac_size,
499 size_t *mac_length )
500{
Steven Cooreman87885df2021-03-19 19:04:39 +0100501 psa_status_t status;
502
503 if( operation->alg == 0 )
504 return( PSA_ERROR_BAD_STATE );
505
506 if( ! operation->is_sign )
507 return( PSA_ERROR_BAD_STATE );
508
509 status = mac_finish_internal( operation, mac, mac_size );
510
511 if( status == PSA_SUCCESS )
512 *mac_length = operation->mac_size;
513
514 return( status );
515}
516
Steven Cooreman896d51e2021-03-19 15:24:23 +0100517static psa_status_t mac_verify_finish(
518 mbedtls_psa_mac_operation_t *operation,
519 const uint8_t *mac,
520 size_t mac_length )
521{
Steven Cooreman87885df2021-03-19 19:04:39 +0100522 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
523 psa_status_t status;
524
525 if( operation->alg == 0 )
526 return( PSA_ERROR_BAD_STATE );
527
528 if( operation->is_sign )
529 return( PSA_ERROR_BAD_STATE );
530
531 if( operation->mac_size != mac_length )
532 {
533 status = PSA_ERROR_INVALID_SIGNATURE;
534 goto cleanup;
535 }
536
537 status = mac_finish_internal( operation, actual_mac, sizeof( actual_mac ) );
538 if( status != PSA_SUCCESS )
539 goto cleanup;
540
Steven Cooremana2a1b802021-04-29 16:37:59 +0200541 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100542 status = PSA_ERROR_INVALID_SIGNATURE;
543
544cleanup:
545 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
546
547 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100548}
Steven Cooreman896d51e2021-03-19 15:24:23 +0100549#endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */
550
551#if defined(MBEDTLS_PSA_BUILTIN_MAC)
552psa_status_t mbedtls_psa_mac_compute(
553 const psa_key_attributes_t *attributes,
554 const uint8_t *key_buffer,
555 size_t key_buffer_size,
556 psa_algorithm_t alg,
557 const uint8_t *input,
558 size_t input_length,
559 uint8_t *mac,
560 size_t mac_size,
561 size_t *mac_length )
562{
563 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
564 input, input_length,
565 mac, mac_size, mac_length ) );
566}
567
568psa_status_t mbedtls_psa_mac_sign_setup(
569 mbedtls_psa_mac_operation_t *operation,
570 const psa_key_attributes_t *attributes,
571 const uint8_t *key_buffer,
572 size_t key_buffer_size,
573 psa_algorithm_t alg )
574{
575 return( mac_sign_setup( operation, attributes,
576 key_buffer, key_buffer_size, alg ) );
577}
578
579psa_status_t mbedtls_psa_mac_verify_setup(
580 mbedtls_psa_mac_operation_t *operation,
581 const psa_key_attributes_t *attributes,
582 const uint8_t *key_buffer,
583 size_t key_buffer_size,
584 psa_algorithm_t alg )
585{
586 return( mac_verify_setup( operation, attributes,
587 key_buffer, key_buffer_size, alg ) );
588}
589
590psa_status_t mbedtls_psa_mac_update(
591 mbedtls_psa_mac_operation_t *operation,
592 const uint8_t *input,
593 size_t input_length )
594{
595 return( mac_update( operation, input, input_length ) );
596}
597
598psa_status_t mbedtls_psa_mac_sign_finish(
599 mbedtls_psa_mac_operation_t *operation,
600 uint8_t *mac,
601 size_t mac_size,
602 size_t *mac_length )
603{
604 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
605}
606
607psa_status_t mbedtls_psa_mac_verify_finish(
608 mbedtls_psa_mac_operation_t *operation,
609 const uint8_t *mac,
610 size_t mac_length )
611{
612 return( mac_verify_finish( operation, mac, mac_length ) );
613}
614
615psa_status_t mbedtls_psa_mac_abort(
616 mbedtls_psa_mac_operation_t *operation )
617{
618 return( mac_abort( operation ) );
619}
620#endif /* MBEDTLS_PSA_BUILTIN_MAC */
621
622 /*
623 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
624 */
625#if defined(PSA_CRYPTO_DRIVER_TEST)
626
627static int is_mac_accelerated( psa_algorithm_t alg )
628{
629#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
630 if( PSA_ALG_IS_HMAC( alg ) )
631 return( 1 );
632#endif
633
634 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
635 {
636#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
637 case PSA_ALG_CMAC:
638 return( 1 );
639#endif
640 default:
641 return( 0 );
642 }
643}
644
645psa_status_t mbedtls_transparent_test_driver_mac_compute(
646 const psa_key_attributes_t *attributes,
647 const uint8_t *key_buffer,
648 size_t key_buffer_size,
649 psa_algorithm_t alg,
650 const uint8_t *input,
651 size_t input_length,
652 uint8_t *mac,
653 size_t mac_size,
654 size_t *mac_length )
655{
656 if( is_mac_accelerated( alg ) )
657 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
658 input, input_length,
659 mac, mac_size, mac_length ) );
660 else
661 return( PSA_ERROR_NOT_SUPPORTED );
662}
663
664psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
665 mbedtls_transparent_test_driver_mac_operation_t *operation,
666 const psa_key_attributes_t *attributes,
667 const uint8_t *key_buffer,
668 size_t key_buffer_size,
669 psa_algorithm_t alg )
670{
671 if( is_mac_accelerated( alg ) )
672 return( mac_sign_setup( operation, attributes,
673 key_buffer, key_buffer_size, alg ) );
674 else
675 return( PSA_ERROR_NOT_SUPPORTED );
676}
677
678psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
679 mbedtls_transparent_test_driver_mac_operation_t *operation,
680 const psa_key_attributes_t *attributes,
681 const uint8_t *key_buffer,
682 size_t key_buffer_size,
683 psa_algorithm_t alg )
684{
685 if( is_mac_accelerated( alg ) )
686 return( mac_verify_setup( operation, attributes,
687 key_buffer, key_buffer_size, alg ) );
688 else
689 return( PSA_ERROR_NOT_SUPPORTED );
690}
691
692psa_status_t mbedtls_transparent_test_driver_mac_update(
693 mbedtls_transparent_test_driver_mac_operation_t *operation,
694 const uint8_t *input,
695 size_t input_length )
696{
697 if( is_mac_accelerated( operation->alg ) )
698 return( mac_update( operation, input, input_length ) );
699 else
700 return( PSA_ERROR_BAD_STATE );
701}
702
703psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
704 mbedtls_transparent_test_driver_mac_operation_t *operation,
705 uint8_t *mac,
706 size_t mac_size,
707 size_t *mac_length )
708{
709 if( is_mac_accelerated( operation->alg ) )
710 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
711 else
712 return( PSA_ERROR_BAD_STATE );
713}
714
715psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
716 mbedtls_transparent_test_driver_mac_operation_t *operation,
717 const uint8_t *mac,
718 size_t mac_length )
719{
720 if( is_mac_accelerated( operation->alg ) )
721 return( mac_verify_finish( operation, mac, mac_length ) );
722 else
723 return( PSA_ERROR_BAD_STATE );
724}
725
726psa_status_t mbedtls_transparent_test_driver_mac_abort(
727 mbedtls_transparent_test_driver_mac_operation_t *operation )
728{
729 return( mac_abort( operation ) );
730}
731
732psa_status_t mbedtls_opaque_test_driver_mac_compute(
733 const psa_key_attributes_t *attributes,
734 const uint8_t *key_buffer,
735 size_t key_buffer_size,
736 psa_algorithm_t alg,
737 const uint8_t *input,
738 size_t input_length,
739 uint8_t *mac,
740 size_t mac_size,
741 size_t *mac_length )
742{
743 /* Opaque driver testing is not implemented yet through this mechanism. */
744 (void) attributes;
745 (void) key_buffer;
746 (void) key_buffer_size;
747 (void) alg;
748 (void) input;
749 (void) input_length;
750 (void) mac;
751 (void) mac_size;
752 (void) mac_length;
753 return( PSA_ERROR_NOT_SUPPORTED );
754}
755
756psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
757 mbedtls_opaque_test_driver_mac_operation_t *operation,
758 const psa_key_attributes_t *attributes,
759 const uint8_t *key_buffer,
760 size_t key_buffer_size,
761 psa_algorithm_t alg )
762{
763 /* Opaque driver testing is not implemented yet through this mechanism. */
764 (void) operation;
765 (void) attributes;
766 (void) key_buffer;
767 (void) key_buffer_size;
768 (void) alg;
769 return( PSA_ERROR_NOT_SUPPORTED );
770}
771
772psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
773 mbedtls_opaque_test_driver_mac_operation_t *operation,
774 const psa_key_attributes_t *attributes,
775 const uint8_t *key_buffer,
776 size_t key_buffer_size,
777 psa_algorithm_t alg )
778{
779 /* Opaque driver testing is not implemented yet through this mechanism. */
780 (void) operation;
781 (void) attributes;
782 (void) key_buffer;
783 (void) key_buffer_size;
784 (void) alg;
785 return( PSA_ERROR_NOT_SUPPORTED );
786}
787
788psa_status_t mbedtls_opaque_test_driver_mac_update(
789 mbedtls_opaque_test_driver_mac_operation_t *operation,
790 const uint8_t *input,
791 size_t input_length )
792{
793 /* Opaque driver testing is not implemented yet through this mechanism. */
794 (void) operation;
795 (void) input;
796 (void) input_length;
797 return( PSA_ERROR_NOT_SUPPORTED );
798}
799
800psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
801 mbedtls_opaque_test_driver_mac_operation_t *operation,
802 uint8_t *mac,
803 size_t mac_size,
804 size_t *mac_length )
805{
806 /* Opaque driver testing is not implemented yet through this mechanism. */
807 (void) operation;
808 (void) mac;
809 (void) mac_size;
810 (void) mac_length;
811 return( PSA_ERROR_NOT_SUPPORTED );
812}
813
814psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
815 mbedtls_opaque_test_driver_mac_operation_t *operation,
816 const uint8_t *mac,
817 size_t mac_length )
818{
819 /* Opaque driver testing is not implemented yet through this mechanism. */
820 (void) operation;
821 (void) mac;
822 (void) mac_length;
823 return( PSA_ERROR_NOT_SUPPORTED );
824}
825
826psa_status_t mbedtls_opaque_test_driver_mac_abort(
827 mbedtls_opaque_test_driver_mac_operation_t *operation )
828{
829 /* Opaque driver testing is not implemented yet through this mechanism. */
830 (void) operation;
831 return( PSA_ERROR_NOT_SUPPORTED );
832}
833
834#endif /* PSA_CRYPTO_DRIVER_TEST */
835
836#endif /* MBEDTLS_PSA_CRYPTO_C */