blob: 421d12e033f0cfeeeda988d187017c2de1234500 [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) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020036 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
37 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
38 defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) )
Steven Cooremand13a70f2021-03-19 15:24:23 +010039#define BUILTIN_ALG_CMAC 1
40#endif
41#if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020042 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
43 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
44 defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) )
Steven Cooremand13a70f2021-03-19 15:24:23 +010045#define BUILTIN_ALG_HMAC 1
46#endif
47
Steven Cooreman02865f52021-05-07 15:55:27 +020048#if defined(BUILTIN_ALG_HMAC)
Steven Cooremana6df6042021-04-29 19:32:25 +020049static psa_status_t psa_hmac_abort_internal(
50 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman82c66b62021-03-19 17:39:17 +010051{
52 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
53 return( psa_hash_abort( &hmac->hash_ctx ) );
54}
55
Steven Cooremana6df6042021-04-29 19:32:25 +020056static psa_status_t psa_hmac_setup_internal(
57 mbedtls_psa_hmac_operation_t *hmac,
58 const uint8_t *key,
59 size_t key_length,
60 psa_algorithm_t hash_alg )
Steven Cooreman82c66b62021-03-19 17:39:17 +010061{
62 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
63 size_t i;
64 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinee7be73d2021-09-22 14:44:28 +020065 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +010066 psa_status_t status;
67
68 hmac->alg = hash_alg;
69
70 /* Sanity checks on block_size, to guarantee that there won't be a buffer
71 * overflow below. This should never trigger if the hash algorithm
72 * is implemented correctly. */
73 /* The size checks against the ipad and opad buffers cannot be written
74 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
75 * because that triggers -Wlogical-op on GCC 7.3. */
76 if( block_size > sizeof( ipad ) )
77 return( PSA_ERROR_NOT_SUPPORTED );
78 if( block_size > sizeof( hmac->opad ) )
79 return( PSA_ERROR_NOT_SUPPORTED );
80 if( block_size < hash_size )
81 return( PSA_ERROR_NOT_SUPPORTED );
82
83 if( key_length > block_size )
84 {
85 status = psa_hash_compute( hash_alg, key, key_length,
86 ipad, sizeof( ipad ), &key_length );
87 if( status != PSA_SUCCESS )
88 goto cleanup;
89 }
90 /* A 0-length key is not commonly used in HMAC when used as a MAC,
91 * but it is permitted. It is common when HMAC is used in HKDF, for
92 * example. Don't call `memcpy` in the 0-length because `key` could be
93 * an invalid pointer which would make the behavior undefined. */
94 else if( key_length != 0 )
95 memcpy( ipad, key, key_length );
96
97 /* ipad contains the key followed by garbage. Xor and fill with 0x36
98 * to create the ipad value. */
99 for( i = 0; i < key_length; i++ )
100 ipad[i] ^= 0x36;
101 memset( ipad + key_length, 0x36, block_size - key_length );
102
103 /* Copy the key material from ipad to opad, flipping the requisite bits,
104 * and filling the rest of opad with the requisite constant. */
105 for( i = 0; i < key_length; i++ )
106 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
107 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
108
109 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
110 if( status != PSA_SUCCESS )
111 goto cleanup;
112
113 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
114
115cleanup:
116 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
117
118 return( status );
119}
120
Steven Cooremana6df6042021-04-29 19:32:25 +0200121static psa_status_t psa_hmac_update_internal(
122 mbedtls_psa_hmac_operation_t *hmac,
123 const uint8_t *data,
124 size_t data_length )
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100125{
126 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
127}
128
Steven Cooremana6df6042021-04-29 19:32:25 +0200129static psa_status_t psa_hmac_finish_internal(
130 mbedtls_psa_hmac_operation_t *hmac,
131 uint8_t *mac,
132 size_t mac_size )
Steven Cooreman82c66b62021-03-19 17:39:17 +0100133{
Ronald Cron69a63422021-10-18 09:47:58 +0200134 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman82c66b62021-03-19 17:39:17 +0100135 psa_algorithm_t hash_alg = hmac->alg;
136 size_t hash_size = 0;
Gilles Peskinee7be73d2021-09-22 14:44:28 +0200137 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman82c66b62021-03-19 17:39:17 +0100138 psa_status_t status;
139
140 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
141 if( status != PSA_SUCCESS )
142 return( status );
143 /* From here on, tmp needs to be wiped. */
144
145 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
146 if( status != PSA_SUCCESS )
147 goto exit;
148
149 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
150 if( status != PSA_SUCCESS )
151 goto exit;
152
153 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
154 if( status != PSA_SUCCESS )
155 goto exit;
156
157 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
158 if( status != PSA_SUCCESS )
159 goto exit;
160
161 memcpy( mac, tmp, mac_size );
162
163exit:
164 mbedtls_platform_zeroize( tmp, hash_size );
165 return( status );
166}
Steven Cooreman02865f52021-05-07 15:55:27 +0200167#endif /* BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100168
169#if defined(BUILTIN_ALG_CMAC)
170static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
171 const psa_key_attributes_t *attributes,
Steven Cooremandcd08112021-05-06 18:00:37 +0200172 const uint8_t *key_buffer )
Steven Cooremane6804192021-03-19 18:28:56 +0100173{
174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman0c239652021-05-07 17:27:27 +0200175
176#if defined(PSA_WANT_KEY_TYPE_DES)
177 /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
178 * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
179 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_DES &&
180 ( psa_get_key_bits( attributes ) == 64 ||
181 psa_get_key_bits( attributes ) == 128 ) )
182 return( PSA_ERROR_NOT_SUPPORTED );
183#endif
184
Steven Cooreman094a77e2021-05-06 17:58:36 +0200185 const mbedtls_cipher_info_t * cipher_info =
Steven Cooremane6804192021-03-19 18:28:56 +0100186 mbedtls_cipher_info_from_psa(
187 PSA_ALG_CMAC,
188 psa_get_key_type( attributes ),
189 psa_get_key_bits( attributes ),
190 NULL );
191
Steven Cooreman094a77e2021-05-06 17:58:36 +0200192 if( cipher_info == NULL )
Steven Cooremane6804192021-03-19 18:28:56 +0100193 return( PSA_ERROR_NOT_SUPPORTED );
194
Steven Cooreman094a77e2021-05-06 17:58:36 +0200195 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooremane6804192021-03-19 18:28:56 +0100196 if( ret != 0 )
197 goto exit;
198
199 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
200 key_buffer,
201 psa_get_key_bits( attributes ) );
202exit:
203 return( mbedtls_to_psa_error( ret ) );
204}
205#endif /* BUILTIN_ALG_CMAC */
206
Steven Cooreman02865f52021-05-07 15:55:27 +0200207/* Implement the PSA driver MAC interface on top of mbed TLS if either the
208 * software driver or the test driver requires it. */
209#if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC)
210
Steven Cooremane6804192021-03-19 18:28:56 +0100211/* Initialize this driver's MAC operation structure. Once this function has been
212 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
213static psa_status_t mac_init(
214 mbedtls_psa_mac_operation_t *operation,
215 psa_algorithm_t alg )
216{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100218
Steven Cooreman72f736a2021-05-07 14:14:37 +0200219 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100220
221#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200222 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100223 {
Steven Cooremane6804192021-03-19 18:28:56 +0100224 mbedtls_cipher_init( &operation->ctx.cmac );
225 status = PSA_SUCCESS;
226 }
227 else
228#endif /* BUILTIN_ALG_CMAC */
229#if defined(BUILTIN_ALG_HMAC)
230 if( PSA_ALG_IS_HMAC( operation->alg ) )
231 {
232 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
233 operation->ctx.hmac.alg = 0;
234 status = PSA_SUCCESS;
235 }
236 else
237#endif /* BUILTIN_ALG_HMAC */
238 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200239 (void) operation;
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200240 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100241 }
242
243 if( status != PSA_SUCCESS )
244 memset( operation, 0, sizeof( *operation ) );
245 return( status );
246}
247
248static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
249{
250 if( operation->alg == 0 )
251 {
252 /* The object has (apparently) been initialized but it is not
253 * in use. It's ok to call abort on such an object, and there's
254 * nothing to do. */
255 return( PSA_SUCCESS );
256 }
257 else
258#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200259 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremane6804192021-03-19 18:28:56 +0100260 {
261 mbedtls_cipher_free( &operation->ctx.cmac );
262 }
263 else
264#endif /* BUILTIN_ALG_CMAC */
265#if defined(BUILTIN_ALG_HMAC)
266 if( PSA_ALG_IS_HMAC( operation->alg ) )
267 {
268 psa_hmac_abort_internal( &operation->ctx.hmac );
269 }
270 else
271#endif /* BUILTIN_ALG_HMAC */
272 {
273 /* Sanity check (shouldn't happen: operation->alg should
274 * always have been initialized to a valid value). */
275 goto bad_state;
276 }
277
278 operation->alg = 0;
Steven Cooremane6804192021-03-19 18:28:56 +0100279
280 return( PSA_SUCCESS );
281
282bad_state:
283 /* If abort is called on an uninitialized object, we can't trust
284 * anything. Wipe the object in case it contains confidential data.
285 * This may result in a memory leak if a pointer gets overwritten,
286 * but it's too late to do anything about this. */
287 memset( operation, 0, sizeof( *operation ) );
288 return( PSA_ERROR_BAD_STATE );
289}
290
291static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
292 const psa_key_attributes_t *attributes,
293 const uint8_t *key_buffer,
294 size_t key_buffer_size,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200295 psa_algorithm_t alg )
Steven Cooremane6804192021-03-19 18:28:56 +0100296{
297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
298
299 /* A context must be freshly initialized before it can be set up. */
300 if( operation->alg != 0 )
Steven Cooremane6804192021-03-19 18:28:56 +0100301 return( PSA_ERROR_BAD_STATE );
Steven Cooremane6804192021-03-19 18:28:56 +0100302
303 status = mac_init( operation, alg );
304 if( status != PSA_SUCCESS )
305 return( status );
Steven Cooremane6804192021-03-19 18:28:56 +0100306
307#if defined(BUILTIN_ALG_CMAC)
308 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
309 {
Steven Cooremandcd08112021-05-06 18:00:37 +0200310 /* Key buffer size for CMAC is dictated by the key bits set on the
311 * attributes, and previously validated by the core on key import. */
312 (void) key_buffer_size;
313 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooremane6804192021-03-19 18:28:56 +0100314 }
315 else
316#endif /* BUILTIN_ALG_CMAC */
317#if defined(BUILTIN_ALG_HMAC)
318 if( PSA_ALG_IS_HMAC( alg ) )
319 {
Steven Cooremane6804192021-03-19 18:28:56 +0100320 status = psa_hmac_setup_internal( &operation->ctx.hmac,
321 key_buffer,
322 key_buffer_size,
323 PSA_ALG_HMAC_GET_HASH( alg ) );
324 }
325 else
326#endif /* BUILTIN_ALG_HMAC */
327 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200328 (void) attributes;
329 (void) key_buffer;
330 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100331 status = PSA_ERROR_NOT_SUPPORTED;
332 }
333
Steven Cooremana4638e72021-04-29 16:24:36 +0200334 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +0100335 mac_abort( operation );
336
337 return( status );
338}
339
Steven Cooremand13a70f2021-03-19 15:24:23 +0100340static psa_status_t mac_update(
341 mbedtls_psa_mac_operation_t *operation,
342 const uint8_t *input,
343 size_t input_length )
344{
Steven Cooremana4638e72021-04-29 16:24:36 +0200345 if( operation->alg == 0 )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100346 return( PSA_ERROR_BAD_STATE );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100347
348#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200349 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100350 {
351 return( mbedtls_to_psa_error(
352 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
353 input, input_length ) ) );
354 }
355 else
356#endif /* BUILTIN_ALG_CMAC */
357#if defined(BUILTIN_ALG_HMAC)
358 if( PSA_ALG_IS_HMAC( operation->alg ) )
359 {
Steven Cooremana6df6042021-04-29 19:32:25 +0200360 return( psa_hmac_update_internal( &operation->ctx.hmac,
361 input, input_length ) );
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100362 }
363 else
364#endif /* BUILTIN_ALG_HMAC */
365 {
366 /* This shouldn't happen if `operation` was initialized by
367 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200368 (void) input;
369 (void) input_length;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100370 return( PSA_ERROR_BAD_STATE );
371 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100372}
373
Steven Cooremana5b860a2021-03-19 19:04:39 +0100374static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
375 uint8_t *mac,
376 size_t mac_size )
377{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100378#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman72f736a2021-05-07 14:14:37 +0200379 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100380 {
381 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
382 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
383 if( ret == 0 )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200384 memcpy( mac, tmp, mac_size );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100385 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
386 return( mbedtls_to_psa_error( ret ) );
387 }
388 else
389#endif /* BUILTIN_ALG_CMAC */
390#if defined(BUILTIN_ALG_HMAC)
391 if( PSA_ALG_IS_HMAC( operation->alg ) )
392 {
393 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman72f736a2021-05-07 14:14:37 +0200394 mac, mac_size ) );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100395 }
396 else
397#endif /* BUILTIN_ALG_HMAC */
398 {
399 /* This shouldn't happen if `operation` was initialized by
400 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200401 (void) operation;
402 (void) mac;
403 (void) mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100404 return( PSA_ERROR_BAD_STATE );
405 }
406}
407
Steven Cooremand13a70f2021-03-19 15:24:23 +0100408static psa_status_t mac_sign_finish(
409 mbedtls_psa_mac_operation_t *operation,
410 uint8_t *mac,
411 size_t mac_size,
412 size_t *mac_length )
413{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100415
416 if( operation->alg == 0 )
417 return( PSA_ERROR_BAD_STATE );
418
Steven Cooremana5b860a2021-03-19 19:04:39 +0100419 status = mac_finish_internal( operation, mac, mac_size );
420
421 if( status == PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +0200422 *mac_length = mac_size;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100423
424 return( status );
425}
426
Steven Cooremand13a70f2021-03-19 15:24:23 +0100427static psa_status_t mac_verify_finish(
428 mbedtls_psa_mac_operation_t *operation,
429 const uint8_t *mac,
430 size_t mac_length )
431{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100432 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100434
435 if( operation->alg == 0 )
436 return( PSA_ERROR_BAD_STATE );
437
Steven Cooreman72f736a2021-05-07 14:14:37 +0200438 /* Consistency check: requested MAC length fits our local buffer */
439 if( mac_length > sizeof( actual_mac ) )
440 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100441
Steven Cooreman72f736a2021-05-07 14:14:37 +0200442 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooremana5b860a2021-03-19 19:04:39 +0100443 if( status != PSA_SUCCESS )
444 goto cleanup;
445
Steven Cooreman36876a02021-04-29 16:37:59 +0200446 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooremana5b860a2021-03-19 19:04:39 +0100447 status = PSA_ERROR_INVALID_SIGNATURE;
448
449cleanup:
450 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
451
452 return( status );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100453}
Ronald Cron76be3e02021-06-17 17:34:43 +0200454
455static psa_status_t mac_compute(
456 const psa_key_attributes_t *attributes,
457 const uint8_t *key_buffer,
458 size_t key_buffer_size,
459 psa_algorithm_t alg,
460 const uint8_t *input,
461 size_t input_length,
462 uint8_t *mac,
463 size_t mac_size,
464 size_t *mac_length )
465{
466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
467 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
468
469 status = mac_setup( &operation,
470 attributes, key_buffer, key_buffer_size,
471 alg );
472 if( status != PSA_SUCCESS )
473 goto exit;
474
475 if( input_length > 0 )
476 {
477 status = mac_update( &operation, input, input_length );
478 if( status != PSA_SUCCESS )
479 goto exit;
480 }
481
482 status = mac_finish_internal( &operation, mac, mac_size );
483 if( status == PSA_SUCCESS )
484 *mac_length = mac_size;
485
486exit:
487 mac_abort( &operation );
488
489 return( status );
490}
491
Steven Cooreman02865f52021-05-07 15:55:27 +0200492#endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100493
494#if defined(MBEDTLS_PSA_BUILTIN_MAC)
495psa_status_t mbedtls_psa_mac_compute(
496 const psa_key_attributes_t *attributes,
497 const uint8_t *key_buffer,
498 size_t key_buffer_size,
499 psa_algorithm_t alg,
500 const uint8_t *input,
501 size_t input_length,
502 uint8_t *mac,
503 size_t mac_size,
504 size_t *mac_length )
505{
506 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
507 input, input_length,
508 mac, mac_size, mac_length ) );
509}
510
511psa_status_t mbedtls_psa_mac_sign_setup(
512 mbedtls_psa_mac_operation_t *operation,
513 const psa_key_attributes_t *attributes,
514 const uint8_t *key_buffer,
515 size_t key_buffer_size,
516 psa_algorithm_t alg )
517{
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200518 return( mac_setup( operation, attributes,
519 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100520}
521
522psa_status_t mbedtls_psa_mac_verify_setup(
523 mbedtls_psa_mac_operation_t *operation,
524 const psa_key_attributes_t *attributes,
525 const uint8_t *key_buffer,
526 size_t key_buffer_size,
527 psa_algorithm_t alg )
528{
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200529 return( mac_setup( operation, attributes,
530 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100531}
532
533psa_status_t mbedtls_psa_mac_update(
534 mbedtls_psa_mac_operation_t *operation,
535 const uint8_t *input,
536 size_t input_length )
537{
538 return( mac_update( operation, input, input_length ) );
539}
540
541psa_status_t mbedtls_psa_mac_sign_finish(
542 mbedtls_psa_mac_operation_t *operation,
543 uint8_t *mac,
544 size_t mac_size,
545 size_t *mac_length )
546{
547 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
548}
549
550psa_status_t mbedtls_psa_mac_verify_finish(
551 mbedtls_psa_mac_operation_t *operation,
552 const uint8_t *mac,
553 size_t mac_length )
554{
555 return( mac_verify_finish( operation, mac, mac_length ) );
556}
557
558psa_status_t mbedtls_psa_mac_abort(
559 mbedtls_psa_mac_operation_t *operation )
560{
561 return( mac_abort( operation ) );
562}
563#endif /* MBEDTLS_PSA_BUILTIN_MAC */
564
565 /*
566 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
567 */
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200568#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Steven Cooremand13a70f2021-03-19 15:24:23 +0100569
570static int is_mac_accelerated( psa_algorithm_t alg )
571{
572#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
573 if( PSA_ALG_IS_HMAC( alg ) )
574 return( 1 );
575#endif
576
577 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
578 {
579#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
580 case PSA_ALG_CMAC:
581 return( 1 );
582#endif
583 default:
584 return( 0 );
585 }
586}
587
588psa_status_t mbedtls_transparent_test_driver_mac_compute(
589 const psa_key_attributes_t *attributes,
590 const uint8_t *key_buffer,
591 size_t key_buffer_size,
592 psa_algorithm_t alg,
593 const uint8_t *input,
594 size_t input_length,
595 uint8_t *mac,
596 size_t mac_size,
597 size_t *mac_length )
598{
599 if( is_mac_accelerated( alg ) )
600 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
601 input, input_length,
602 mac, mac_size, mac_length ) );
603 else
604 return( PSA_ERROR_NOT_SUPPORTED );
605}
606
607psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
Ronald Cron56f78972021-04-09 17:15:06 +0200608 mbedtls_psa_mac_operation_t *operation,
Steven Cooremand13a70f2021-03-19 15:24:23 +0100609 const psa_key_attributes_t *attributes,
610 const uint8_t *key_buffer,
611 size_t key_buffer_size,
612 psa_algorithm_t alg )
613{
614 if( is_mac_accelerated( alg ) )
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200615 return( mac_setup( operation, attributes,
616 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100617 else
618 return( PSA_ERROR_NOT_SUPPORTED );
619}
620
621psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
Ronald Cron56f78972021-04-09 17:15:06 +0200622 mbedtls_psa_mac_operation_t *operation,
Steven Cooremand13a70f2021-03-19 15:24:23 +0100623 const psa_key_attributes_t *attributes,
624 const uint8_t *key_buffer,
625 size_t key_buffer_size,
626 psa_algorithm_t alg )
627{
628 if( is_mac_accelerated( alg ) )
Steven Cooreman9e15fb72021-05-11 11:10:34 +0200629 return( mac_setup( operation, attributes,
630 key_buffer, key_buffer_size, alg ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +0100631 else
632 return( PSA_ERROR_NOT_SUPPORTED );
633}
634
635psa_status_t mbedtls_transparent_test_driver_mac_update(
Ronald Cron56f78972021-04-09 17:15:06 +0200636 mbedtls_psa_mac_operation_t *operation,
Steven Cooremand13a70f2021-03-19 15:24:23 +0100637 const uint8_t *input,
638 size_t input_length )
639{
640 if( is_mac_accelerated( operation->alg ) )
641 return( mac_update( operation, input, input_length ) );
642 else
643 return( PSA_ERROR_BAD_STATE );
644}
645
646psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
Ronald Cron56f78972021-04-09 17:15:06 +0200647 mbedtls_psa_mac_operation_t *operation,
Steven Cooremand13a70f2021-03-19 15:24:23 +0100648 uint8_t *mac,
649 size_t mac_size,
650 size_t *mac_length )
651{
652 if( is_mac_accelerated( operation->alg ) )
653 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
654 else
655 return( PSA_ERROR_BAD_STATE );
656}
657
658psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
Ronald Cron56f78972021-04-09 17:15:06 +0200659 mbedtls_psa_mac_operation_t *operation,
Steven Cooremand13a70f2021-03-19 15:24:23 +0100660 const uint8_t *mac,
661 size_t mac_length )
662{
663 if( is_mac_accelerated( operation->alg ) )
664 return( mac_verify_finish( operation, mac, mac_length ) );
665 else
666 return( PSA_ERROR_BAD_STATE );
667}
668
669psa_status_t mbedtls_transparent_test_driver_mac_abort(
Ronald Cron56f78972021-04-09 17:15:06 +0200670 mbedtls_psa_mac_operation_t *operation )
Steven Cooremand13a70f2021-03-19 15:24:23 +0100671{
672 return( mac_abort( operation ) );
673}
674
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200675#endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100676
677#endif /* MBEDTLS_PSA_CRYPTO_C */