blob: 20c56a0214dee42c7fe7005540fa1390255812a1 [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_compute(
363 const psa_key_attributes_t *attributes,
364 const uint8_t *key_buffer,
365 size_t key_buffer_size,
366 psa_algorithm_t alg,
367 const uint8_t *input,
368 size_t input_length,
369 uint8_t *mac,
370 size_t mac_size,
371 size_t *mac_length )
372{
Steven Cooreman32d56942021-03-19 17:39:17 +0100373 /* One-shot MAC has not been implemented in this PSA implementation yet. */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100374 (void) attributes;
375 (void) key_buffer;
376 (void) key_buffer_size;
377 (void) alg;
378 (void) input;
379 (void) input_length;
380 (void) mac;
381 (void) mac_size;
382 (void) mac_length;
383 return( PSA_ERROR_NOT_SUPPORTED );
384}
385
Steven Cooreman896d51e2021-03-19 15:24:23 +0100386static psa_status_t mac_update(
387 mbedtls_psa_mac_operation_t *operation,
388 const uint8_t *input,
389 size_t input_length )
390{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200391 if( operation->alg == 0 )
Steven Cooreman11743f92021-03-19 18:38:46 +0100392 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100393
394#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200395 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman11743f92021-03-19 18:38:46 +0100396 {
397 return( mbedtls_to_psa_error(
398 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
399 input, input_length ) ) );
400 }
401 else
402#endif /* BUILTIN_ALG_CMAC */
403#if defined(BUILTIN_ALG_HMAC)
404 if( PSA_ALG_IS_HMAC( operation->alg ) )
405 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200406 return( psa_hmac_update_internal( &operation->ctx.hmac,
407 input, input_length ) );
Steven Cooreman11743f92021-03-19 18:38:46 +0100408 }
409 else
410#endif /* BUILTIN_ALG_HMAC */
411 {
412 /* This shouldn't happen if `operation` was initialized by
413 * a setup function. */
Steven Cooreman9621f442021-05-10 09:47:05 +0200414 (void) input;
415 (void) input_length;
Steven Cooreman11743f92021-03-19 18:38:46 +0100416 return( PSA_ERROR_BAD_STATE );
417 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100418}
419
Steven Cooreman87885df2021-03-19 19:04:39 +0100420static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
421 uint8_t *mac,
422 size_t mac_size )
423{
Steven Cooreman87885df2021-03-19 19:04:39 +0100424#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200425 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman87885df2021-03-19 19:04:39 +0100426 {
427 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
428 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
429 if( ret == 0 )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200430 memcpy( mac, tmp, mac_size );
Steven Cooreman87885df2021-03-19 19:04:39 +0100431 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
432 return( mbedtls_to_psa_error( ret ) );
433 }
434 else
435#endif /* BUILTIN_ALG_CMAC */
436#if defined(BUILTIN_ALG_HMAC)
437 if( PSA_ALG_IS_HMAC( operation->alg ) )
438 {
439 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200440 mac, mac_size ) );
Steven Cooreman87885df2021-03-19 19:04:39 +0100441 }
442 else
443#endif /* BUILTIN_ALG_HMAC */
444 {
445 /* This shouldn't happen if `operation` was initialized by
446 * a setup function. */
Steven Cooreman15f0d922021-05-07 14:14:37 +0200447 (void) operation;
448 (void) mac;
449 (void) mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100450 return( PSA_ERROR_BAD_STATE );
451 }
452}
453
Steven Cooreman896d51e2021-03-19 15:24:23 +0100454static psa_status_t mac_sign_finish(
455 mbedtls_psa_mac_operation_t *operation,
456 uint8_t *mac,
457 size_t mac_size,
458 size_t *mac_length )
459{
Steven Cooreman9878a162021-05-06 17:58:36 +0200460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100461
462 if( operation->alg == 0 )
463 return( PSA_ERROR_BAD_STATE );
464
Steven Cooreman87885df2021-03-19 19:04:39 +0100465 status = mac_finish_internal( operation, mac, mac_size );
466
467 if( status == PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200468 *mac_length = mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100469
470 return( status );
471}
472
Steven Cooreman896d51e2021-03-19 15:24:23 +0100473static psa_status_t mac_verify_finish(
474 mbedtls_psa_mac_operation_t *operation,
475 const uint8_t *mac,
476 size_t mac_length )
477{
Steven Cooreman87885df2021-03-19 19:04:39 +0100478 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman9878a162021-05-06 17:58:36 +0200479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100480
481 if( operation->alg == 0 )
482 return( PSA_ERROR_BAD_STATE );
483
Steven Cooreman15f0d922021-05-07 14:14:37 +0200484 /* Consistency check: requested MAC length fits our local buffer */
485 if( mac_length > sizeof( actual_mac ) )
486 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman87885df2021-03-19 19:04:39 +0100487
Steven Cooreman15f0d922021-05-07 14:14:37 +0200488 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooreman87885df2021-03-19 19:04:39 +0100489 if( status != PSA_SUCCESS )
490 goto cleanup;
491
Steven Cooremana2a1b802021-04-29 16:37:59 +0200492 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100493 status = PSA_ERROR_INVALID_SIGNATURE;
494
495cleanup:
496 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
497
498 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100499}
Steven Cooremanaaf99442021-05-07 15:55:27 +0200500#endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100501
502#if defined(MBEDTLS_PSA_BUILTIN_MAC)
503psa_status_t mbedtls_psa_mac_compute(
504 const psa_key_attributes_t *attributes,
505 const uint8_t *key_buffer,
506 size_t key_buffer_size,
507 psa_algorithm_t alg,
508 const uint8_t *input,
509 size_t input_length,
510 uint8_t *mac,
511 size_t mac_size,
512 size_t *mac_length )
513{
514 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
515 input, input_length,
516 mac, mac_size, mac_length ) );
517}
518
519psa_status_t mbedtls_psa_mac_sign_setup(
520 mbedtls_psa_mac_operation_t *operation,
521 const psa_key_attributes_t *attributes,
522 const uint8_t *key_buffer,
523 size_t key_buffer_size,
524 psa_algorithm_t alg )
525{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200526 return( mac_setup( operation, attributes,
527 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100528}
529
530psa_status_t mbedtls_psa_mac_verify_setup(
531 mbedtls_psa_mac_operation_t *operation,
532 const psa_key_attributes_t *attributes,
533 const uint8_t *key_buffer,
534 size_t key_buffer_size,
535 psa_algorithm_t alg )
536{
Steven Cooremanbbb19522021-05-11 11:10:34 +0200537 return( mac_setup( operation, attributes,
538 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100539}
540
541psa_status_t mbedtls_psa_mac_update(
542 mbedtls_psa_mac_operation_t *operation,
543 const uint8_t *input,
544 size_t input_length )
545{
546 return( mac_update( operation, input, input_length ) );
547}
548
549psa_status_t mbedtls_psa_mac_sign_finish(
550 mbedtls_psa_mac_operation_t *operation,
551 uint8_t *mac,
552 size_t mac_size,
553 size_t *mac_length )
554{
555 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
556}
557
558psa_status_t mbedtls_psa_mac_verify_finish(
559 mbedtls_psa_mac_operation_t *operation,
560 const uint8_t *mac,
561 size_t mac_length )
562{
563 return( mac_verify_finish( operation, mac, mac_length ) );
564}
565
566psa_status_t mbedtls_psa_mac_abort(
567 mbedtls_psa_mac_operation_t *operation )
568{
569 return( mac_abort( operation ) );
570}
571#endif /* MBEDTLS_PSA_BUILTIN_MAC */
572
573 /*
574 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
575 */
576#if defined(PSA_CRYPTO_DRIVER_TEST)
577
578static int is_mac_accelerated( psa_algorithm_t alg )
579{
580#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
581 if( PSA_ALG_IS_HMAC( alg ) )
582 return( 1 );
583#endif
584
585 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
586 {
587#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
588 case PSA_ALG_CMAC:
589 return( 1 );
590#endif
591 default:
592 return( 0 );
593 }
594}
595
596psa_status_t mbedtls_transparent_test_driver_mac_compute(
597 const psa_key_attributes_t *attributes,
598 const uint8_t *key_buffer,
599 size_t key_buffer_size,
600 psa_algorithm_t alg,
601 const uint8_t *input,
602 size_t input_length,
603 uint8_t *mac,
604 size_t mac_size,
605 size_t *mac_length )
606{
607 if( is_mac_accelerated( alg ) )
608 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
609 input, input_length,
610 mac, mac_size, mac_length ) );
611 else
612 return( PSA_ERROR_NOT_SUPPORTED );
613}
614
615psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
616 mbedtls_transparent_test_driver_mac_operation_t *operation,
617 const psa_key_attributes_t *attributes,
618 const uint8_t *key_buffer,
619 size_t key_buffer_size,
620 psa_algorithm_t alg )
621{
622 if( is_mac_accelerated( alg ) )
Steven Cooremanbbb19522021-05-11 11:10:34 +0200623 return( mac_setup( operation, attributes,
624 key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100625 else
626 return( PSA_ERROR_NOT_SUPPORTED );
627}
628
629psa_status_t mbedtls_transparent_test_driver_mac_verify_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_update(
644 mbedtls_transparent_test_driver_mac_operation_t *operation,
645 const uint8_t *input,
646 size_t input_length )
647{
648 if( is_mac_accelerated( operation->alg ) )
649 return( mac_update( operation, input, input_length ) );
650 else
651 return( PSA_ERROR_BAD_STATE );
652}
653
654psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
655 mbedtls_transparent_test_driver_mac_operation_t *operation,
656 uint8_t *mac,
657 size_t mac_size,
658 size_t *mac_length )
659{
660 if( is_mac_accelerated( operation->alg ) )
661 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
662 else
663 return( PSA_ERROR_BAD_STATE );
664}
665
666psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
667 mbedtls_transparent_test_driver_mac_operation_t *operation,
668 const uint8_t *mac,
669 size_t mac_length )
670{
671 if( is_mac_accelerated( operation->alg ) )
672 return( mac_verify_finish( operation, mac, mac_length ) );
673 else
674 return( PSA_ERROR_BAD_STATE );
675}
676
677psa_status_t mbedtls_transparent_test_driver_mac_abort(
678 mbedtls_transparent_test_driver_mac_operation_t *operation )
679{
680 return( mac_abort( operation ) );
681}
682
683psa_status_t mbedtls_opaque_test_driver_mac_compute(
684 const psa_key_attributes_t *attributes,
685 const uint8_t *key_buffer,
686 size_t key_buffer_size,
687 psa_algorithm_t alg,
688 const uint8_t *input,
689 size_t input_length,
690 uint8_t *mac,
691 size_t mac_size,
692 size_t *mac_length )
693{
694 /* Opaque driver testing is not implemented yet through this mechanism. */
695 (void) attributes;
696 (void) key_buffer;
697 (void) key_buffer_size;
698 (void) alg;
699 (void) input;
700 (void) input_length;
701 (void) mac;
702 (void) mac_size;
703 (void) mac_length;
704 return( PSA_ERROR_NOT_SUPPORTED );
705}
706
707psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
708 mbedtls_opaque_test_driver_mac_operation_t *operation,
709 const psa_key_attributes_t *attributes,
710 const uint8_t *key_buffer,
711 size_t key_buffer_size,
712 psa_algorithm_t alg )
713{
714 /* Opaque driver testing is not implemented yet through this mechanism. */
715 (void) operation;
716 (void) attributes;
717 (void) key_buffer;
718 (void) key_buffer_size;
719 (void) alg;
720 return( PSA_ERROR_NOT_SUPPORTED );
721}
722
723psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
724 mbedtls_opaque_test_driver_mac_operation_t *operation,
725 const psa_key_attributes_t *attributes,
726 const uint8_t *key_buffer,
727 size_t key_buffer_size,
728 psa_algorithm_t alg )
729{
730 /* Opaque driver testing is not implemented yet through this mechanism. */
731 (void) operation;
732 (void) attributes;
733 (void) key_buffer;
734 (void) key_buffer_size;
735 (void) alg;
736 return( PSA_ERROR_NOT_SUPPORTED );
737}
738
739psa_status_t mbedtls_opaque_test_driver_mac_update(
740 mbedtls_opaque_test_driver_mac_operation_t *operation,
741 const uint8_t *input,
742 size_t input_length )
743{
744 /* Opaque driver testing is not implemented yet through this mechanism. */
745 (void) operation;
746 (void) input;
747 (void) input_length;
748 return( PSA_ERROR_NOT_SUPPORTED );
749}
750
751psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
752 mbedtls_opaque_test_driver_mac_operation_t *operation,
753 uint8_t *mac,
754 size_t mac_size,
755 size_t *mac_length )
756{
757 /* Opaque driver testing is not implemented yet through this mechanism. */
758 (void) operation;
759 (void) mac;
760 (void) mac_size;
761 (void) mac_length;
762 return( PSA_ERROR_NOT_SUPPORTED );
763}
764
765psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
766 mbedtls_opaque_test_driver_mac_operation_t *operation,
767 const uint8_t *mac,
768 size_t mac_length )
769{
770 /* Opaque driver testing is not implemented yet through this mechanism. */
771 (void) operation;
772 (void) mac;
773 (void) mac_length;
774 return( PSA_ERROR_NOT_SUPPORTED );
775}
776
777psa_status_t mbedtls_opaque_test_driver_mac_abort(
778 mbedtls_opaque_test_driver_mac_operation_t *operation )
779{
780 /* Opaque driver testing is not implemented yet through this mechanism. */
781 (void) operation;
782 return( PSA_ERROR_NOT_SUPPORTED );
783}
784
785#endif /* PSA_CRYPTO_DRIVER_TEST */
786
787#endif /* MBEDTLS_PSA_CRYPTO_C */