blob: d59178ef3e3465e4590ee8ed241d499033048761 [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) || \
Ronald Cron2091eed2021-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 Cooreman896d51e2021-03-19 15:24:23 +010039#define BUILTIN_ALG_CMAC 1
40#endif
41#if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
Ronald Cron2091eed2021-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 Cooreman896d51e2021-03-19 15:24:23 +010045#define BUILTIN_ALG_HMAC 1
46#endif
47
Steven Cooremanaaf99442021-05-07 15:55:27 +020048#if defined(BUILTIN_ALG_HMAC)
Steven Cooreman22dea1d2021-04-29 19:32:25 +020049static psa_status_t psa_hmac_abort_internal(
50 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman32d56942021-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 Cooreman22dea1d2021-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 Cooreman32d56942021-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 Peskine285f2132021-09-22 14:44:28 +020065 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman32d56942021-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 Cooreman22dea1d2021-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 Cooreman76720f62021-03-22 12:21:10 +0100125{
126 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
127}
128
Steven Cooreman22dea1d2021-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 Cooreman32d56942021-03-19 17:39:17 +0100133{
Ronald Cron3a95d2b2021-10-18 09:47:58 +0200134 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman32d56942021-03-19 17:39:17 +0100135 psa_algorithm_t hash_alg = hmac->alg;
136 size_t hash_size = 0;
Gilles Peskine285f2132021-09-22 14:44:28 +0200137 size_t block_size = PSA_HASH_BLOCK_LENGTH( hash_alg );
Steven Cooreman32d56942021-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 Cooremanaaf99442021-05-07 15:55:27 +0200167#endif /* BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-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 Cooremanaf81a712021-05-06 18:00:37 +0200172 const uint8_t *key_buffer )
Steven Cooreman07897832021-03-19 18:28:56 +0100173{
174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman63fa40e2021-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 Cooreman9878a162021-05-06 17:58:36 +0200185 const mbedtls_cipher_info_t * cipher_info =
Steven Cooreman07897832021-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 Cooreman9878a162021-05-06 17:58:36 +0200192 if( cipher_info == NULL )
Steven Cooreman07897832021-03-19 18:28:56 +0100193 return( PSA_ERROR_NOT_SUPPORTED );
194
Steven Cooreman9878a162021-05-06 17:58:36 +0200195 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooreman07897832021-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 Cooremanaaf99442021-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 Cooreman07897832021-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 Cooreman6e6451e2021-04-29 16:21:24 +0200217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100218
Steven Cooreman15f0d922021-05-07 14:14:37 +0200219 operation->alg = alg;
Steven Cooreman07897832021-03-19 18:28:56 +0100220
221#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200222 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100223 {
Steven Cooreman07897832021-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 {
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200239 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100240 }
241
242 if( status != PSA_SUCCESS )
243 memset( operation, 0, sizeof( *operation ) );
244 return( status );
245}
246
247static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
248{
249 if( operation->alg == 0 )
250 {
251 /* The object has (apparently) been initialized but it is not
252 * in use. It's ok to call abort on such an object, and there's
253 * nothing to do. */
254 return( PSA_SUCCESS );
255 }
256 else
257#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200258 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100259 {
260 mbedtls_cipher_free( &operation->ctx.cmac );
261 }
262 else
263#endif /* BUILTIN_ALG_CMAC */
264#if defined(BUILTIN_ALG_HMAC)
265 if( PSA_ALG_IS_HMAC( operation->alg ) )
266 {
267 psa_hmac_abort_internal( &operation->ctx.hmac );
268 }
269 else
270#endif /* BUILTIN_ALG_HMAC */
271 {
272 /* Sanity check (shouldn't happen: operation->alg should
273 * always have been initialized to a valid value). */
274 goto bad_state;
275 }
276
277 operation->alg = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100278
279 return( PSA_SUCCESS );
280
281bad_state:
282 /* If abort is called on an uninitialized object, we can't trust
283 * anything. Wipe the object in case it contains confidential data.
284 * This may result in a memory leak if a pointer gets overwritten,
285 * but it's too late to do anything about this. */
286 memset( operation, 0, sizeof( *operation ) );
287 return( PSA_ERROR_BAD_STATE );
288}
289
290static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
291 const psa_key_attributes_t *attributes,
292 const uint8_t *key_buffer,
293 size_t key_buffer_size,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200294 psa_algorithm_t alg )
Steven Cooreman07897832021-03-19 18:28:56 +0100295{
296 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
297
298 /* A context must be freshly initialized before it can be set up. */
299 if( operation->alg != 0 )
Steven Cooreman07897832021-03-19 18:28:56 +0100300 return( PSA_ERROR_BAD_STATE );
Steven Cooreman07897832021-03-19 18:28:56 +0100301
302 status = mac_init( operation, alg );
303 if( status != PSA_SUCCESS )
304 return( status );
Steven Cooreman07897832021-03-19 18:28:56 +0100305
306#if defined(BUILTIN_ALG_CMAC)
307 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
308 {
Steven Cooremanaf81a712021-05-06 18:00:37 +0200309 /* Key buffer size for CMAC is dictated by the key bits set on the
310 * attributes, and previously validated by the core on key import. */
311 (void) key_buffer_size;
312 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooreman07897832021-03-19 18:28:56 +0100313 }
314 else
315#endif /* BUILTIN_ALG_CMAC */
316#if defined(BUILTIN_ALG_HMAC)
317 if( PSA_ALG_IS_HMAC( alg ) )
318 {
Steven Cooreman07897832021-03-19 18:28:56 +0100319 status = psa_hmac_setup_internal( &operation->ctx.hmac,
320 key_buffer,
321 key_buffer_size,
322 PSA_ALG_HMAC_GET_HASH( alg ) );
323 }
324 else
325#endif /* BUILTIN_ALG_HMAC */
326 {
Steven Cooreman9621f442021-05-10 09:47:05 +0200327 (void) attributes;
328 (void) key_buffer;
329 (void) key_buffer_size;
Steven Cooreman07897832021-03-19 18:28:56 +0100330 status = PSA_ERROR_NOT_SUPPORTED;
331 }
332
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200333 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +0100334 mac_abort( operation );
335
336 return( status );
337}
338
Steven Cooreman896d51e2021-03-19 15:24:23 +0100339static psa_status_t mac_update(
340 mbedtls_psa_mac_operation_t *operation,
341 const uint8_t *input,
342 size_t input_length )
343{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200344 if( operation->alg == 0 )
Steven Cooreman11743f92021-03-19 18:38:46 +0100345 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100346
347#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200348 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman11743f92021-03-19 18:38:46 +0100349 {
350 return( mbedtls_to_psa_error(
351 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
352 input, input_length ) ) );
353 }
354 else
355#endif /* BUILTIN_ALG_CMAC */
356#if defined(BUILTIN_ALG_HMAC)
357 if( PSA_ALG_IS_HMAC( operation->alg ) )
358 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200359 return( psa_hmac_update_internal( &operation->ctx.hmac,
360 input, input_length ) );
Steven Cooreman11743f92021-03-19 18:38:46 +0100361 }
362 else
363#endif /* BUILTIN_ALG_HMAC */
364 {
365 /* This shouldn't happen if `operation` was initialized by
366 * a setup function. */
Steven Cooreman9621f442021-05-10 09:47:05 +0200367 (void) input;
368 (void) input_length;
Steven Cooreman11743f92021-03-19 18:38:46 +0100369 return( PSA_ERROR_BAD_STATE );
370 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100371}
372
Steven Cooreman87885df2021-03-19 19:04:39 +0100373static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
374 uint8_t *mac,
375 size_t mac_size )
376{
Steven Cooreman87885df2021-03-19 19:04:39 +0100377#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200378 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman87885df2021-03-19 19:04:39 +0100379 {
380 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
381 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
382 if( ret == 0 )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200383 memcpy( mac, tmp, mac_size );
Steven Cooreman87885df2021-03-19 19:04:39 +0100384 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
385 return( mbedtls_to_psa_error( ret ) );
386 }
387 else
388#endif /* BUILTIN_ALG_CMAC */
389#if defined(BUILTIN_ALG_HMAC)
390 if( PSA_ALG_IS_HMAC( operation->alg ) )
391 {
392 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200393 mac, mac_size ) );
Steven Cooreman87885df2021-03-19 19:04:39 +0100394 }
395 else
396#endif /* BUILTIN_ALG_HMAC */
397 {
398 /* This shouldn't happen if `operation` was initialized by
399 * a setup function. */
Steven Cooreman15f0d922021-05-07 14:14:37 +0200400 (void) operation;
401 (void) mac;
402 (void) mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100403 return( PSA_ERROR_BAD_STATE );
404 }
405}
406
Steven Cooreman896d51e2021-03-19 15:24:23 +0100407static psa_status_t mac_sign_finish(
408 mbedtls_psa_mac_operation_t *operation,
409 uint8_t *mac,
410 size_t mac_size,
411 size_t *mac_length )
412{
Steven Cooreman9878a162021-05-06 17:58:36 +0200413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100414
415 if( operation->alg == 0 )
416 return( PSA_ERROR_BAD_STATE );
417
Steven Cooreman87885df2021-03-19 19:04:39 +0100418 status = mac_finish_internal( operation, mac, mac_size );
419
420 if( status == PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200421 *mac_length = mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100422
423 return( status );
424}
425
Steven Cooreman896d51e2021-03-19 15:24:23 +0100426static psa_status_t mac_verify_finish(
427 mbedtls_psa_mac_operation_t *operation,
428 const uint8_t *mac,
429 size_t mac_length )
430{
Steven Cooreman87885df2021-03-19 19:04:39 +0100431 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman9878a162021-05-06 17:58:36 +0200432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100433
434 if( operation->alg == 0 )
435 return( PSA_ERROR_BAD_STATE );
436
Steven Cooreman15f0d922021-05-07 14:14:37 +0200437 /* Consistency check: requested MAC length fits our local buffer */
438 if( mac_length > sizeof( actual_mac ) )
439 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman87885df2021-03-19 19:04:39 +0100440
Steven Cooreman15f0d922021-05-07 14:14:37 +0200441 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooreman87885df2021-03-19 19:04:39 +0100442 if( status != PSA_SUCCESS )
443 goto cleanup;
444
Steven Cooremana2a1b802021-04-29 16:37:59 +0200445 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100446 status = PSA_ERROR_INVALID_SIGNATURE;
447
448cleanup:
449 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
450
451 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100452}
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200453
454static psa_status_t mac_compute(
455 const psa_key_attributes_t *attributes,
456 const uint8_t *key_buffer,
457 size_t key_buffer_size,
458 psa_algorithm_t alg,
459 const uint8_t *input,
460 size_t input_length,
461 uint8_t *mac,
462 size_t mac_size,
463 size_t *mac_length )
464{
465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
466 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
467
468 status = mac_setup( &operation,
469 attributes, key_buffer, key_buffer_size,
470 alg );
471 if( status != PSA_SUCCESS )
472 goto exit;
473
474 if( input_length > 0 )
475 {
476 status = mac_update( &operation, input, input_length );
477 if( status != PSA_SUCCESS )
478 goto exit;
479 }
480
481 status = mac_finish_internal( &operation, mac, mac_size );
482 if( status == PSA_SUCCESS )
483 *mac_length = mac_size;
484
485exit:
486 mac_abort( &operation );
487
488 return( status );
489}
490
Steven Cooremanaaf99442021-05-07 15:55:27 +0200491#endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100492
493#if defined(MBEDTLS_PSA_BUILTIN_MAC)
494psa_status_t mbedtls_psa_mac_compute(
495 const psa_key_attributes_t *attributes,
496 const uint8_t *key_buffer,
497 size_t key_buffer_size,
498 psa_algorithm_t alg,
499 const uint8_t *input,
500 size_t input_length,
501 uint8_t *mac,
502 size_t mac_size,
503 size_t *mac_length )
504{
505 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
506 input, input_length,
507 mac, mac_size, mac_length ) );
508}
509
510psa_status_t mbedtls_psa_mac_sign_setup(
511 mbedtls_psa_mac_operation_t *operation,
512 const psa_key_attributes_t *attributes,
513 const uint8_t *key_buffer,
514 size_t key_buffer_size,
515 psa_algorithm_t alg )
516{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200517 return( mac_setup( operation, attributes,
518 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100519}
520
521psa_status_t mbedtls_psa_mac_verify_setup(
522 mbedtls_psa_mac_operation_t *operation,
523 const psa_key_attributes_t *attributes,
524 const uint8_t *key_buffer,
525 size_t key_buffer_size,
526 psa_algorithm_t alg )
527{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200528 return( mac_setup( operation, attributes,
529 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100530}
531
532psa_status_t mbedtls_psa_mac_update(
533 mbedtls_psa_mac_operation_t *operation,
534 const uint8_t *input,
535 size_t input_length )
536{
537 return( mac_update( operation, input, input_length ) );
538}
539
540psa_status_t mbedtls_psa_mac_sign_finish(
541 mbedtls_psa_mac_operation_t *operation,
542 uint8_t *mac,
543 size_t mac_size,
544 size_t *mac_length )
545{
546 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
547}
548
549psa_status_t mbedtls_psa_mac_verify_finish(
550 mbedtls_psa_mac_operation_t *operation,
551 const uint8_t *mac,
552 size_t mac_length )
553{
554 return( mac_verify_finish( operation, mac, mac_length ) );
555}
556
557psa_status_t mbedtls_psa_mac_abort(
558 mbedtls_psa_mac_operation_t *operation )
559{
560 return( mac_abort( operation ) );
561}
562#endif /* MBEDTLS_PSA_BUILTIN_MAC */
563
564 /*
565 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
566 */
Ronald Cron2091eed2021-04-09 11:09:54 +0200567#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Steven Cooreman896d51e2021-03-19 15:24:23 +0100568
569static int is_mac_accelerated( psa_algorithm_t alg )
570{
571#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
572 if( PSA_ALG_IS_HMAC( alg ) )
573 return( 1 );
574#endif
575
576 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
577 {
578#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
579 case PSA_ALG_CMAC:
580 return( 1 );
581#endif
582 default:
583 return( 0 );
584 }
585}
586
587psa_status_t mbedtls_transparent_test_driver_mac_compute(
588 const psa_key_attributes_t *attributes,
589 const uint8_t *key_buffer,
590 size_t key_buffer_size,
591 psa_algorithm_t alg,
592 const uint8_t *input,
593 size_t input_length,
594 uint8_t *mac,
595 size_t mac_size,
596 size_t *mac_length )
597{
598 if( is_mac_accelerated( alg ) )
599 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
600 input, input_length,
601 mac, mac_size, mac_length ) );
602 else
603 return( PSA_ERROR_NOT_SUPPORTED );
604}
605
606psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
Ronald Cron0c677c22021-04-09 17:15:06 +0200607 mbedtls_psa_mac_operation_t *operation,
Steven Cooreman896d51e2021-03-19 15:24:23 +0100608 const psa_key_attributes_t *attributes,
609 const uint8_t *key_buffer,
610 size_t key_buffer_size,
611 psa_algorithm_t alg )
612{
613 if( is_mac_accelerated( alg ) )
Steven Cooremanbbb19522021-05-11 11:10:34 +0200614 return( mac_setup( operation, attributes,
615 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100616 else
617 return( PSA_ERROR_NOT_SUPPORTED );
618}
619
620psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
Ronald Cron0c677c22021-04-09 17:15:06 +0200621 mbedtls_psa_mac_operation_t *operation,
Steven Cooreman896d51e2021-03-19 15:24:23 +0100622 const psa_key_attributes_t *attributes,
623 const uint8_t *key_buffer,
624 size_t key_buffer_size,
625 psa_algorithm_t alg )
626{
627 if( is_mac_accelerated( alg ) )
Steven Cooremanbbb19522021-05-11 11:10:34 +0200628 return( mac_setup( operation, attributes,
629 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100630 else
631 return( PSA_ERROR_NOT_SUPPORTED );
632}
633
634psa_status_t mbedtls_transparent_test_driver_mac_update(
Ronald Cron0c677c22021-04-09 17:15:06 +0200635 mbedtls_psa_mac_operation_t *operation,
Steven Cooreman896d51e2021-03-19 15:24:23 +0100636 const uint8_t *input,
637 size_t input_length )
638{
639 if( is_mac_accelerated( operation->alg ) )
640 return( mac_update( operation, input, input_length ) );
641 else
642 return( PSA_ERROR_BAD_STATE );
643}
644
645psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
Ronald Cron0c677c22021-04-09 17:15:06 +0200646 mbedtls_psa_mac_operation_t *operation,
Steven Cooreman896d51e2021-03-19 15:24:23 +0100647 uint8_t *mac,
648 size_t mac_size,
649 size_t *mac_length )
650{
651 if( is_mac_accelerated( operation->alg ) )
652 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
653 else
654 return( PSA_ERROR_BAD_STATE );
655}
656
657psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
Ronald Cron0c677c22021-04-09 17:15:06 +0200658 mbedtls_psa_mac_operation_t *operation,
Steven Cooreman896d51e2021-03-19 15:24:23 +0100659 const uint8_t *mac,
660 size_t mac_length )
661{
662 if( is_mac_accelerated( operation->alg ) )
663 return( mac_verify_finish( operation, mac, mac_length ) );
664 else
665 return( PSA_ERROR_BAD_STATE );
666}
667
668psa_status_t mbedtls_transparent_test_driver_mac_abort(
Ronald Cron0c677c22021-04-09 17:15:06 +0200669 mbedtls_psa_mac_operation_t *operation )
Steven Cooreman896d51e2021-03-19 15:24:23 +0100670{
671 return( mac_abort( operation ) );
672}
673
Ronald Cron2091eed2021-04-09 11:09:54 +0200674#endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100675
676#endif /* MBEDTLS_PSA_CRYPTO_C */