blob: 135fa352da7d9ceed1efe85d1316f5c93702c340 [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 Cooremanaaf99442021-05-07 15:55:27 +020044#if defined(BUILTIN_ALG_HMAC)
Steven Cooreman32d56942021-03-19 17:39:17 +010045static 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}
Steven Cooremanaaf99442021-05-07 15:55:27 +0200190#endif /* BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100191
192#if defined(BUILTIN_ALG_CMAC)
193static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
194 const psa_key_attributes_t *attributes,
Steven Cooremanaf81a712021-05-06 18:00:37 +0200195 const uint8_t *key_buffer )
Steven Cooreman07897832021-03-19 18:28:56 +0100196{
197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman63fa40e2021-05-07 17:27:27 +0200198
199#if defined(PSA_WANT_KEY_TYPE_DES)
200 /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
201 * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
202 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES &&
203 ( psa_get_key_bits( attributes ) == 64 ||
204 psa_get_key_bits( attributes ) == 128 ) )
205 return( PSA_ERROR_NOT_SUPPORTED );
206#endif
207
Steven Cooreman9878a162021-05-06 17:58:36 +0200208 const mbedtls_cipher_info_t * cipher_info =
Steven Cooreman07897832021-03-19 18:28:56 +0100209 mbedtls_cipher_info_from_psa(
210 PSA_ALG_CMAC,
211 psa_get_key_type( attributes ),
212 psa_get_key_bits( attributes ),
213 NULL );
214
Steven Cooreman9878a162021-05-06 17:58:36 +0200215 if( cipher_info == NULL )
Steven Cooreman07897832021-03-19 18:28:56 +0100216 return( PSA_ERROR_NOT_SUPPORTED );
217
Steven Cooreman9878a162021-05-06 17:58:36 +0200218 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooreman07897832021-03-19 18:28:56 +0100219 if( ret != 0 )
220 goto exit;
221
222 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
223 key_buffer,
224 psa_get_key_bits( attributes ) );
225exit:
226 return( mbedtls_to_psa_error( ret ) );
227}
228#endif /* BUILTIN_ALG_CMAC */
229
Steven Cooremanaaf99442021-05-07 15:55:27 +0200230/* Implement the PSA driver MAC interface on top of mbed TLS if either the
231 * software driver or the test driver requires it. */
232#if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC)
233
Steven Cooreman07897832021-03-19 18:28:56 +0100234/* Initialize this driver's MAC operation structure. Once this function has been
235 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
236static psa_status_t mac_init(
237 mbedtls_psa_mac_operation_t *operation,
238 psa_algorithm_t alg )
239{
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100241
Steven Cooreman15f0d922021-05-07 14:14:37 +0200242 operation->alg = alg;
Steven Cooreman07897832021-03-19 18:28:56 +0100243
244#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200245 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100246 {
Steven Cooreman07897832021-03-19 18:28:56 +0100247 mbedtls_cipher_init( &operation->ctx.cmac );
248 status = PSA_SUCCESS;
249 }
250 else
251#endif /* BUILTIN_ALG_CMAC */
252#if defined(BUILTIN_ALG_HMAC)
253 if( PSA_ALG_IS_HMAC( operation->alg ) )
254 {
255 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
256 operation->ctx.hmac.alg = 0;
257 status = PSA_SUCCESS;
258 }
259 else
260#endif /* BUILTIN_ALG_HMAC */
261 {
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200262 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100263 }
264
265 if( status != PSA_SUCCESS )
266 memset( operation, 0, sizeof( *operation ) );
267 return( status );
268}
269
270static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
271{
272 if( operation->alg == 0 )
273 {
274 /* The object has (apparently) been initialized but it is not
275 * in use. It's ok to call abort on such an object, and there's
276 * nothing to do. */
277 return( PSA_SUCCESS );
278 }
279 else
280#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200281 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100282 {
283 mbedtls_cipher_free( &operation->ctx.cmac );
284 }
285 else
286#endif /* BUILTIN_ALG_CMAC */
287#if defined(BUILTIN_ALG_HMAC)
288 if( PSA_ALG_IS_HMAC( operation->alg ) )
289 {
290 psa_hmac_abort_internal( &operation->ctx.hmac );
291 }
292 else
293#endif /* BUILTIN_ALG_HMAC */
294 {
295 /* Sanity check (shouldn't happen: operation->alg should
296 * always have been initialized to a valid value). */
297 goto bad_state;
298 }
299
300 operation->alg = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100301
302 return( PSA_SUCCESS );
303
304bad_state:
305 /* If abort is called on an uninitialized object, we can't trust
306 * anything. Wipe the object in case it contains confidential data.
307 * This may result in a memory leak if a pointer gets overwritten,
308 * but it's too late to do anything about this. */
309 memset( operation, 0, sizeof( *operation ) );
310 return( PSA_ERROR_BAD_STATE );
311}
312
313static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
314 const psa_key_attributes_t *attributes,
315 const uint8_t *key_buffer,
316 size_t key_buffer_size,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200317 psa_algorithm_t alg )
Steven Cooreman07897832021-03-19 18:28:56 +0100318{
319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
320
321 /* A context must be freshly initialized before it can be set up. */
322 if( operation->alg != 0 )
Steven Cooreman07897832021-03-19 18:28:56 +0100323 return( PSA_ERROR_BAD_STATE );
Steven Cooreman07897832021-03-19 18:28:56 +0100324
325 status = mac_init( operation, alg );
326 if( status != PSA_SUCCESS )
327 return( status );
Steven Cooreman07897832021-03-19 18:28:56 +0100328
329#if defined(BUILTIN_ALG_CMAC)
330 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
331 {
Steven Cooremanaf81a712021-05-06 18:00:37 +0200332 /* Key buffer size for CMAC is dictated by the key bits set on the
333 * attributes, and previously validated by the core on key import. */
334 (void) key_buffer_size;
335 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooreman07897832021-03-19 18:28:56 +0100336 }
337 else
338#endif /* BUILTIN_ALG_CMAC */
339#if defined(BUILTIN_ALG_HMAC)
340 if( PSA_ALG_IS_HMAC( alg ) )
341 {
Steven Cooreman07897832021-03-19 18:28:56 +0100342 status = psa_hmac_setup_internal( &operation->ctx.hmac,
343 key_buffer,
344 key_buffer_size,
345 PSA_ALG_HMAC_GET_HASH( alg ) );
346 }
347 else
348#endif /* BUILTIN_ALG_HMAC */
349 {
Steven Cooreman9621f442021-05-10 09:47:05 +0200350 (void) attributes;
351 (void) key_buffer;
352 (void) key_buffer_size;
Steven Cooreman07897832021-03-19 18:28:56 +0100353 status = PSA_ERROR_NOT_SUPPORTED;
354 }
355
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200356 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +0100357 mac_abort( operation );
358
359 return( status );
360}
361
Steven Cooreman896d51e2021-03-19 15:24:23 +0100362static psa_status_t mac_update(
363 mbedtls_psa_mac_operation_t *operation,
364 const uint8_t *input,
365 size_t input_length )
366{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200367 if( operation->alg == 0 )
Steven Cooreman11743f92021-03-19 18:38:46 +0100368 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100369
370#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200371 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman11743f92021-03-19 18:38:46 +0100372 {
373 return( mbedtls_to_psa_error(
374 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
375 input, input_length ) ) );
376 }
377 else
378#endif /* BUILTIN_ALG_CMAC */
379#if defined(BUILTIN_ALG_HMAC)
380 if( PSA_ALG_IS_HMAC( operation->alg ) )
381 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200382 return( psa_hmac_update_internal( &operation->ctx.hmac,
383 input, input_length ) );
Steven Cooreman11743f92021-03-19 18:38:46 +0100384 }
385 else
386#endif /* BUILTIN_ALG_HMAC */
387 {
388 /* This shouldn't happen if `operation` was initialized by
389 * a setup function. */
Steven Cooreman9621f442021-05-10 09:47:05 +0200390 (void) input;
391 (void) input_length;
Steven Cooreman11743f92021-03-19 18:38:46 +0100392 return( PSA_ERROR_BAD_STATE );
393 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100394}
395
Steven Cooreman87885df2021-03-19 19:04:39 +0100396static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
397 uint8_t *mac,
398 size_t mac_size )
399{
Steven Cooreman87885df2021-03-19 19:04:39 +0100400#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200401 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman87885df2021-03-19 19:04:39 +0100402 {
403 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
404 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
405 if( ret == 0 )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200406 memcpy( mac, tmp, mac_size );
Steven Cooreman87885df2021-03-19 19:04:39 +0100407 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
408 return( mbedtls_to_psa_error( ret ) );
409 }
410 else
411#endif /* BUILTIN_ALG_CMAC */
412#if defined(BUILTIN_ALG_HMAC)
413 if( PSA_ALG_IS_HMAC( operation->alg ) )
414 {
415 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200416 mac, mac_size ) );
Steven Cooreman87885df2021-03-19 19:04:39 +0100417 }
418 else
419#endif /* BUILTIN_ALG_HMAC */
420 {
421 /* This shouldn't happen if `operation` was initialized by
422 * a setup function. */
Steven Cooreman15f0d922021-05-07 14:14:37 +0200423 (void) operation;
424 (void) mac;
425 (void) mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100426 return( PSA_ERROR_BAD_STATE );
427 }
428}
429
Steven Cooreman896d51e2021-03-19 15:24:23 +0100430static psa_status_t mac_sign_finish(
431 mbedtls_psa_mac_operation_t *operation,
432 uint8_t *mac,
433 size_t mac_size,
434 size_t *mac_length )
435{
Steven Cooreman9878a162021-05-06 17:58:36 +0200436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100437
438 if( operation->alg == 0 )
439 return( PSA_ERROR_BAD_STATE );
440
Steven Cooreman87885df2021-03-19 19:04:39 +0100441 status = mac_finish_internal( operation, mac, mac_size );
442
443 if( status == PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200444 *mac_length = mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100445
446 return( status );
447}
448
Steven Cooreman896d51e2021-03-19 15:24:23 +0100449static psa_status_t mac_verify_finish(
450 mbedtls_psa_mac_operation_t *operation,
451 const uint8_t *mac,
452 size_t mac_length )
453{
Steven Cooreman87885df2021-03-19 19:04:39 +0100454 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman9878a162021-05-06 17:58:36 +0200455 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100456
457 if( operation->alg == 0 )
458 return( PSA_ERROR_BAD_STATE );
459
Steven Cooreman15f0d922021-05-07 14:14:37 +0200460 /* Consistency check: requested MAC length fits our local buffer */
461 if( mac_length > sizeof( actual_mac ) )
462 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman87885df2021-03-19 19:04:39 +0100463
Steven Cooreman15f0d922021-05-07 14:14:37 +0200464 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooreman87885df2021-03-19 19:04:39 +0100465 if( status != PSA_SUCCESS )
466 goto cleanup;
467
Steven Cooremana2a1b802021-04-29 16:37:59 +0200468 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100469 status = PSA_ERROR_INVALID_SIGNATURE;
470
471cleanup:
472 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
473
474 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100475}
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200476
477static psa_status_t mac_compute(
478 const psa_key_attributes_t *attributes,
479 const uint8_t *key_buffer,
480 size_t key_buffer_size,
481 psa_algorithm_t alg,
482 const uint8_t *input,
483 size_t input_length,
484 uint8_t *mac,
485 size_t mac_size,
486 size_t *mac_length )
487{
488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
489 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
490
491 status = mac_setup( &operation,
492 attributes, key_buffer, key_buffer_size,
493 alg );
494 if( status != PSA_SUCCESS )
495 goto exit;
496
497 if( input_length > 0 )
498 {
499 status = mac_update( &operation, input, input_length );
500 if( status != PSA_SUCCESS )
501 goto exit;
502 }
503
504 status = mac_finish_internal( &operation, mac, mac_size );
505 if( status == PSA_SUCCESS )
506 *mac_length = mac_size;
507
508exit:
509 mac_abort( &operation );
510
511 return( status );
512}
513
Steven Cooremanaaf99442021-05-07 15:55:27 +0200514#endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100515
516#if defined(MBEDTLS_PSA_BUILTIN_MAC)
517psa_status_t mbedtls_psa_mac_compute(
518 const psa_key_attributes_t *attributes,
519 const uint8_t *key_buffer,
520 size_t key_buffer_size,
521 psa_algorithm_t alg,
522 const uint8_t *input,
523 size_t input_length,
524 uint8_t *mac,
525 size_t mac_size,
526 size_t *mac_length )
527{
528 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
529 input, input_length,
530 mac, mac_size, mac_length ) );
531}
532
533psa_status_t mbedtls_psa_mac_sign_setup(
534 mbedtls_psa_mac_operation_t *operation,
535 const psa_key_attributes_t *attributes,
536 const uint8_t *key_buffer,
537 size_t key_buffer_size,
538 psa_algorithm_t alg )
539{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200540 return( mac_setup( operation, attributes,
541 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100542}
543
544psa_status_t mbedtls_psa_mac_verify_setup(
545 mbedtls_psa_mac_operation_t *operation,
546 const psa_key_attributes_t *attributes,
547 const uint8_t *key_buffer,
548 size_t key_buffer_size,
549 psa_algorithm_t alg )
550{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200551 return( mac_setup( operation, attributes,
552 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100553}
554
555psa_status_t mbedtls_psa_mac_update(
556 mbedtls_psa_mac_operation_t *operation,
557 const uint8_t *input,
558 size_t input_length )
559{
560 return( mac_update( operation, input, input_length ) );
561}
562
563psa_status_t mbedtls_psa_mac_sign_finish(
564 mbedtls_psa_mac_operation_t *operation,
565 uint8_t *mac,
566 size_t mac_size,
567 size_t *mac_length )
568{
569 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
570}
571
572psa_status_t mbedtls_psa_mac_verify_finish(
573 mbedtls_psa_mac_operation_t *operation,
574 const uint8_t *mac,
575 size_t mac_length )
576{
577 return( mac_verify_finish( operation, mac, mac_length ) );
578}
579
580psa_status_t mbedtls_psa_mac_abort(
581 mbedtls_psa_mac_operation_t *operation )
582{
583 return( mac_abort( operation ) );
584}
585#endif /* MBEDTLS_PSA_BUILTIN_MAC */
586
587 /*
588 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
589 */
590#if defined(PSA_CRYPTO_DRIVER_TEST)
591
592static int is_mac_accelerated( psa_algorithm_t alg )
593{
594#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
595 if( PSA_ALG_IS_HMAC( alg ) )
596 return( 1 );
597#endif
598
599 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
600 {
601#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
602 case PSA_ALG_CMAC:
603 return( 1 );
604#endif
605 default:
606 return( 0 );
607 }
608}
609
610psa_status_t mbedtls_transparent_test_driver_mac_compute(
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 const uint8_t *input,
616 size_t input_length,
617 uint8_t *mac,
618 size_t mac_size,
619 size_t *mac_length )
620{
621 if( is_mac_accelerated( alg ) )
622 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
623 input, input_length,
624 mac, mac_size, mac_length ) );
625 else
626 return( PSA_ERROR_NOT_SUPPORTED );
627}
628
629psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
630 mbedtls_transparent_test_driver_mac_operation_t *operation,
631 const psa_key_attributes_t *attributes,
632 const uint8_t *key_buffer,
633 size_t key_buffer_size,
634 psa_algorithm_t alg )
635{
636 if( is_mac_accelerated( alg ) )
Steven Cooremanbbb19522021-05-11 11:10:34 +0200637 return( mac_setup( operation, attributes,
638 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100639 else
640 return( PSA_ERROR_NOT_SUPPORTED );
641}
642
643psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
644 mbedtls_transparent_test_driver_mac_operation_t *operation,
645 const psa_key_attributes_t *attributes,
646 const uint8_t *key_buffer,
647 size_t key_buffer_size,
648 psa_algorithm_t alg )
649{
650 if( is_mac_accelerated( alg ) )
Steven Cooremanbbb19522021-05-11 11:10:34 +0200651 return( mac_setup( operation, attributes,
652 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100653 else
654 return( PSA_ERROR_NOT_SUPPORTED );
655}
656
657psa_status_t mbedtls_transparent_test_driver_mac_update(
658 mbedtls_transparent_test_driver_mac_operation_t *operation,
659 const uint8_t *input,
660 size_t input_length )
661{
662 if( is_mac_accelerated( operation->alg ) )
663 return( mac_update( operation, input, input_length ) );
664 else
665 return( PSA_ERROR_BAD_STATE );
666}
667
668psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
669 mbedtls_transparent_test_driver_mac_operation_t *operation,
670 uint8_t *mac,
671 size_t mac_size,
672 size_t *mac_length )
673{
674 if( is_mac_accelerated( operation->alg ) )
675 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
676 else
677 return( PSA_ERROR_BAD_STATE );
678}
679
680psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
681 mbedtls_transparent_test_driver_mac_operation_t *operation,
682 const uint8_t *mac,
683 size_t mac_length )
684{
685 if( is_mac_accelerated( operation->alg ) )
686 return( mac_verify_finish( operation, mac, mac_length ) );
687 else
688 return( PSA_ERROR_BAD_STATE );
689}
690
691psa_status_t mbedtls_transparent_test_driver_mac_abort(
692 mbedtls_transparent_test_driver_mac_operation_t *operation )
693{
694 return( mac_abort( operation ) );
695}
696
697psa_status_t mbedtls_opaque_test_driver_mac_compute(
698 const psa_key_attributes_t *attributes,
699 const uint8_t *key_buffer,
700 size_t key_buffer_size,
701 psa_algorithm_t alg,
702 const uint8_t *input,
703 size_t input_length,
704 uint8_t *mac,
705 size_t mac_size,
706 size_t *mac_length )
707{
708 /* Opaque driver testing is not implemented yet through this mechanism. */
709 (void) attributes;
710 (void) key_buffer;
711 (void) key_buffer_size;
712 (void) alg;
713 (void) input;
714 (void) input_length;
715 (void) mac;
716 (void) mac_size;
717 (void) mac_length;
718 return( PSA_ERROR_NOT_SUPPORTED );
719}
720
721psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
722 mbedtls_opaque_test_driver_mac_operation_t *operation,
723 const psa_key_attributes_t *attributes,
724 const uint8_t *key_buffer,
725 size_t key_buffer_size,
726 psa_algorithm_t alg )
727{
728 /* Opaque driver testing is not implemented yet through this mechanism. */
729 (void) operation;
730 (void) attributes;
731 (void) key_buffer;
732 (void) key_buffer_size;
733 (void) alg;
734 return( PSA_ERROR_NOT_SUPPORTED );
735}
736
737psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
738 mbedtls_opaque_test_driver_mac_operation_t *operation,
739 const psa_key_attributes_t *attributes,
740 const uint8_t *key_buffer,
741 size_t key_buffer_size,
742 psa_algorithm_t alg )
743{
744 /* Opaque driver testing is not implemented yet through this mechanism. */
745 (void) operation;
746 (void) attributes;
747 (void) key_buffer;
748 (void) key_buffer_size;
749 (void) alg;
750 return( PSA_ERROR_NOT_SUPPORTED );
751}
752
753psa_status_t mbedtls_opaque_test_driver_mac_update(
754 mbedtls_opaque_test_driver_mac_operation_t *operation,
755 const uint8_t *input,
756 size_t input_length )
757{
758 /* Opaque driver testing is not implemented yet through this mechanism. */
759 (void) operation;
760 (void) input;
761 (void) input_length;
762 return( PSA_ERROR_NOT_SUPPORTED );
763}
764
765psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
766 mbedtls_opaque_test_driver_mac_operation_t *operation,
767 uint8_t *mac,
768 size_t mac_size,
769 size_t *mac_length )
770{
771 /* Opaque driver testing is not implemented yet through this mechanism. */
772 (void) operation;
773 (void) mac;
774 (void) mac_size;
775 (void) mac_length;
776 return( PSA_ERROR_NOT_SUPPORTED );
777}
778
779psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
780 mbedtls_opaque_test_driver_mac_operation_t *operation,
781 const uint8_t *mac,
782 size_t mac_length )
783{
784 /* Opaque driver testing is not implemented yet through this mechanism. */
785 (void) operation;
786 (void) mac;
787 (void) mac_length;
788 return( PSA_ERROR_NOT_SUPPORTED );
789}
790
791psa_status_t mbedtls_opaque_test_driver_mac_abort(
792 mbedtls_opaque_test_driver_mac_operation_t *operation )
793{
794 /* Opaque driver testing is not implemented yet through this mechanism. */
795 (void) operation;
796 return( PSA_ERROR_NOT_SUPPORTED );
797}
798
799#endif /* PSA_CRYPTO_DRIVER_TEST */
800
801#endif /* MBEDTLS_PSA_CRYPTO_C */