blob: 32ab88535d171f2da90298fdb577113ee6064482 [file] [log] [blame]
Steven Cooreman896d51e2021-03-19 15:24:23 +01001/*
2 * PSA MAC layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_mac.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010028#include <mbedtls/md.h>
Steven Cooreman896d51e2021-03-19 15:24:23 +010029
30#include <mbedtls/error.h>
31#include <string.h>
32
33/* Use builtin defines specific to this compilation unit, since the test driver
34 * relies on the software driver. */
35#if( defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
36 ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) )
37#define BUILTIN_ALG_CMAC 1
38#endif
39#if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
40 ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) )
41#define BUILTIN_ALG_HMAC 1
42#endif
43
Steven Cooreman32d56942021-03-19 17:39:17 +010044#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
45static size_t psa_get_hash_block_size( psa_algorithm_t alg )
46{
47 switch( alg )
48 {
49 case PSA_ALG_MD2:
50 return( 16 );
51 case PSA_ALG_MD4:
52 return( 64 );
53 case PSA_ALG_MD5:
54 return( 64 );
55 case PSA_ALG_RIPEMD160:
56 return( 64 );
57 case PSA_ALG_SHA_1:
58 return( 64 );
59 case PSA_ALG_SHA_224:
60 return( 64 );
61 case PSA_ALG_SHA_256:
62 return( 64 );
63 case PSA_ALG_SHA_384:
64 return( 128 );
65 case PSA_ALG_SHA_512:
66 return( 128 );
67 default:
68 return( 0 );
69 }
70}
71
Steven Cooreman22dea1d2021-04-29 19:32:25 +020072static psa_status_t psa_hmac_abort_internal(
73 mbedtls_psa_hmac_operation_t *hmac )
Steven Cooreman32d56942021-03-19 17:39:17 +010074{
75 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
76 return( psa_hash_abort( &hmac->hash_ctx ) );
77}
78
Steven Cooreman22dea1d2021-04-29 19:32:25 +020079static psa_status_t psa_hmac_setup_internal(
80 mbedtls_psa_hmac_operation_t *hmac,
81 const uint8_t *key,
82 size_t key_length,
83 psa_algorithm_t hash_alg )
Steven Cooreman32d56942021-03-19 17:39:17 +010084{
85 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
86 size_t i;
87 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
88 size_t block_size = psa_get_hash_block_size( hash_alg );
89 psa_status_t status;
90
91 hmac->alg = hash_alg;
92
93 /* Sanity checks on block_size, to guarantee that there won't be a buffer
94 * overflow below. This should never trigger if the hash algorithm
95 * is implemented correctly. */
96 /* The size checks against the ipad and opad buffers cannot be written
97 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
98 * because that triggers -Wlogical-op on GCC 7.3. */
99 if( block_size > sizeof( ipad ) )
100 return( PSA_ERROR_NOT_SUPPORTED );
101 if( block_size > sizeof( hmac->opad ) )
102 return( PSA_ERROR_NOT_SUPPORTED );
103 if( block_size < hash_size )
104 return( PSA_ERROR_NOT_SUPPORTED );
105
106 if( key_length > block_size )
107 {
108 status = psa_hash_compute( hash_alg, key, key_length,
109 ipad, sizeof( ipad ), &key_length );
110 if( status != PSA_SUCCESS )
111 goto cleanup;
112 }
113 /* A 0-length key is not commonly used in HMAC when used as a MAC,
114 * but it is permitted. It is common when HMAC is used in HKDF, for
115 * example. Don't call `memcpy` in the 0-length because `key` could be
116 * an invalid pointer which would make the behavior undefined. */
117 else if( key_length != 0 )
118 memcpy( ipad, key, key_length );
119
120 /* ipad contains the key followed by garbage. Xor and fill with 0x36
121 * to create the ipad value. */
122 for( i = 0; i < key_length; i++ )
123 ipad[i] ^= 0x36;
124 memset( ipad + key_length, 0x36, block_size - key_length );
125
126 /* Copy the key material from ipad to opad, flipping the requisite bits,
127 * and filling the rest of opad with the requisite constant. */
128 for( i = 0; i < key_length; i++ )
129 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
130 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
131
132 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
133 if( status != PSA_SUCCESS )
134 goto cleanup;
135
136 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
137
138cleanup:
139 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
140
141 return( status );
142}
143
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200144static psa_status_t psa_hmac_update_internal(
145 mbedtls_psa_hmac_operation_t *hmac,
146 const uint8_t *data,
147 size_t data_length )
Steven Cooreman76720f62021-03-22 12:21:10 +0100148{
149 return( psa_hash_update( &hmac->hash_ctx, data, data_length ) );
150}
151
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200152static psa_status_t psa_hmac_finish_internal(
153 mbedtls_psa_hmac_operation_t *hmac,
154 uint8_t *mac,
155 size_t mac_size )
Steven Cooreman32d56942021-03-19 17:39:17 +0100156{
157 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
158 psa_algorithm_t hash_alg = hmac->alg;
159 size_t hash_size = 0;
160 size_t block_size = psa_get_hash_block_size( hash_alg );
161 psa_status_t status;
162
163 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
164 if( status != PSA_SUCCESS )
165 return( status );
166 /* From here on, tmp needs to be wiped. */
167
168 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
169 if( status != PSA_SUCCESS )
170 goto exit;
171
172 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
173 if( status != PSA_SUCCESS )
174 goto exit;
175
176 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
177 if( status != PSA_SUCCESS )
178 goto exit;
179
180 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
181 if( status != PSA_SUCCESS )
182 goto exit;
183
184 memcpy( mac, tmp, mac_size );
185
186exit:
187 mbedtls_platform_zeroize( tmp, hash_size );
188 return( status );
189}
190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || PSA_CRYPTO_DRIVER_TEST */
191
Steven Cooreman896d51e2021-03-19 15:24:23 +0100192/* Implement the PSA driver MAC interface on top of mbed TLS if either the
193 * software driver or the test driver requires it. */
194#if defined(MBEDTLS_PSA_BUILTIN_MAC) || defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman07897832021-03-19 18:28:56 +0100195
196#if defined(BUILTIN_ALG_CMAC)
197static psa_status_t cmac_setup( mbedtls_psa_mac_operation_t *operation,
198 const psa_key_attributes_t *attributes,
Steven Cooremanaf81a712021-05-06 18:00:37 +0200199 const uint8_t *key_buffer )
Steven Cooreman07897832021-03-19 18:28:56 +0100200{
201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman9878a162021-05-06 17:58:36 +0200202 const mbedtls_cipher_info_t * cipher_info =
Steven Cooreman07897832021-03-19 18:28:56 +0100203 mbedtls_cipher_info_from_psa(
204 PSA_ALG_CMAC,
205 psa_get_key_type( attributes ),
206 psa_get_key_bits( attributes ),
207 NULL );
208
Steven Cooreman9878a162021-05-06 17:58:36 +0200209 if( cipher_info == NULL )
Steven Cooreman07897832021-03-19 18:28:56 +0100210 return( PSA_ERROR_NOT_SUPPORTED );
211
Steven Cooreman9878a162021-05-06 17:58:36 +0200212 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
Steven Cooreman07897832021-03-19 18:28:56 +0100213 if( ret != 0 )
214 goto exit;
215
216 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
217 key_buffer,
218 psa_get_key_bits( attributes ) );
219exit:
220 return( mbedtls_to_psa_error( ret ) );
221}
222#endif /* BUILTIN_ALG_CMAC */
223
224/* Initialize this driver's MAC operation structure. Once this function has been
225 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
226static psa_status_t mac_init(
227 mbedtls_psa_mac_operation_t *operation,
228 psa_algorithm_t alg )
229{
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100231
Steven Cooreman15f0d922021-05-07 14:14:37 +0200232 operation->alg = alg;
Steven Cooreman07897832021-03-19 18:28:56 +0100233
234#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200235 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100236 {
Steven Cooreman07897832021-03-19 18:28:56 +0100237 mbedtls_cipher_init( &operation->ctx.cmac );
238 status = PSA_SUCCESS;
239 }
240 else
241#endif /* BUILTIN_ALG_CMAC */
242#if defined(BUILTIN_ALG_HMAC)
243 if( PSA_ALG_IS_HMAC( operation->alg ) )
244 {
245 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
246 operation->ctx.hmac.alg = 0;
247 status = PSA_SUCCESS;
248 }
249 else
250#endif /* BUILTIN_ALG_HMAC */
251 {
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200252 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100253 }
254
255 if( status != PSA_SUCCESS )
256 memset( operation, 0, sizeof( *operation ) );
257 return( status );
258}
259
260static psa_status_t mac_abort( mbedtls_psa_mac_operation_t *operation )
261{
262 if( operation->alg == 0 )
263 {
264 /* The object has (apparently) been initialized but it is not
265 * in use. It's ok to call abort on such an object, and there's
266 * nothing to do. */
267 return( PSA_SUCCESS );
268 }
269 else
270#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200271 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman07897832021-03-19 18:28:56 +0100272 {
273 mbedtls_cipher_free( &operation->ctx.cmac );
274 }
275 else
276#endif /* BUILTIN_ALG_CMAC */
277#if defined(BUILTIN_ALG_HMAC)
278 if( PSA_ALG_IS_HMAC( operation->alg ) )
279 {
280 psa_hmac_abort_internal( &operation->ctx.hmac );
281 }
282 else
283#endif /* BUILTIN_ALG_HMAC */
284 {
285 /* Sanity check (shouldn't happen: operation->alg should
286 * always have been initialized to a valid value). */
287 goto bad_state;
288 }
289
290 operation->alg = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100291
292 return( PSA_SUCCESS );
293
294bad_state:
295 /* If abort is called on an uninitialized object, we can't trust
296 * anything. Wipe the object in case it contains confidential data.
297 * This may result in a memory leak if a pointer gets overwritten,
298 * but it's too late to do anything about this. */
299 memset( operation, 0, sizeof( *operation ) );
300 return( PSA_ERROR_BAD_STATE );
301}
302
303static psa_status_t mac_setup( mbedtls_psa_mac_operation_t *operation,
304 const psa_key_attributes_t *attributes,
305 const uint8_t *key_buffer,
306 size_t key_buffer_size,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200307 psa_algorithm_t alg )
Steven Cooreman07897832021-03-19 18:28:56 +0100308{
309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
310
311 /* A context must be freshly initialized before it can be set up. */
312 if( operation->alg != 0 )
Steven Cooreman07897832021-03-19 18:28:56 +0100313 return( PSA_ERROR_BAD_STATE );
Steven Cooreman07897832021-03-19 18:28:56 +0100314
315 status = mac_init( operation, alg );
316 if( status != PSA_SUCCESS )
317 return( status );
Steven Cooreman07897832021-03-19 18:28:56 +0100318
319#if defined(BUILTIN_ALG_CMAC)
320 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
321 {
Steven Cooremanaf81a712021-05-06 18:00:37 +0200322 /* Key buffer size for CMAC is dictated by the key bits set on the
323 * attributes, and previously validated by the core on key import. */
324 (void) key_buffer_size;
325 status = cmac_setup( operation, attributes, key_buffer );
Steven Cooreman07897832021-03-19 18:28:56 +0100326 }
327 else
328#endif /* BUILTIN_ALG_CMAC */
329#if defined(BUILTIN_ALG_HMAC)
330 if( PSA_ALG_IS_HMAC( alg ) )
331 {
Steven Cooreman07897832021-03-19 18:28:56 +0100332 status = psa_hmac_setup_internal( &operation->ctx.hmac,
333 key_buffer,
334 key_buffer_size,
335 PSA_ALG_HMAC_GET_HASH( alg ) );
336 }
337 else
338#endif /* BUILTIN_ALG_HMAC */
339 {
340 status = PSA_ERROR_NOT_SUPPORTED;
341 }
342
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200343 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +0100344 mac_abort( operation );
345
346 return( status );
347}
348
Steven Cooreman896d51e2021-03-19 15:24:23 +0100349static psa_status_t mac_compute(
350 const psa_key_attributes_t *attributes,
351 const uint8_t *key_buffer,
352 size_t key_buffer_size,
353 psa_algorithm_t alg,
354 const uint8_t *input,
355 size_t input_length,
356 uint8_t *mac,
357 size_t mac_size,
358 size_t *mac_length )
359{
Steven Cooreman32d56942021-03-19 17:39:17 +0100360 /* One-shot MAC has not been implemented in this PSA implementation yet. */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100361 (void) attributes;
362 (void) key_buffer;
363 (void) key_buffer_size;
364 (void) alg;
365 (void) input;
366 (void) input_length;
367 (void) mac;
368 (void) mac_size;
369 (void) mac_length;
370 return( PSA_ERROR_NOT_SUPPORTED );
371}
372
373static psa_status_t mac_sign_setup(
374 mbedtls_psa_mac_operation_t *operation,
375 const psa_key_attributes_t *attributes,
376 const uint8_t *key_buffer,
377 size_t key_buffer_size,
378 psa_algorithm_t alg )
379{
Steven Cooreman15f0d922021-05-07 14:14:37 +0200380 return( mac_setup( operation,
381 attributes, key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100382}
383
384static psa_status_t mac_verify_setup(
385 mbedtls_psa_mac_operation_t *operation,
386 const psa_key_attributes_t *attributes,
387 const uint8_t *key_buffer,
388 size_t key_buffer_size,
389 psa_algorithm_t alg )
390{
Steven Cooreman15f0d922021-05-07 14:14:37 +0200391 return( mac_setup( operation,
392 attributes, key_buffer, key_buffer_size, alg ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100393}
394
395static psa_status_t mac_update(
396 mbedtls_psa_mac_operation_t *operation,
397 const uint8_t *input,
398 size_t input_length )
399{
Steven Cooremanb4b9b282021-04-29 16:24:36 +0200400 if( operation->alg == 0 )
Steven Cooreman11743f92021-03-19 18:38:46 +0100401 return( PSA_ERROR_BAD_STATE );
Steven Cooreman11743f92021-03-19 18:38:46 +0100402
403#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200404 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman11743f92021-03-19 18:38:46 +0100405 {
406 return( mbedtls_to_psa_error(
407 mbedtls_cipher_cmac_update( &operation->ctx.cmac,
408 input, input_length ) ) );
409 }
410 else
411#endif /* BUILTIN_ALG_CMAC */
412#if defined(BUILTIN_ALG_HMAC)
413 if( PSA_ALG_IS_HMAC( operation->alg ) )
414 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200415 return( psa_hmac_update_internal( &operation->ctx.hmac,
416 input, input_length ) );
Steven Cooreman11743f92021-03-19 18:38:46 +0100417 }
418 else
419#endif /* BUILTIN_ALG_HMAC */
420 {
421 /* This shouldn't happen if `operation` was initialized by
422 * a setup function. */
423 return( PSA_ERROR_BAD_STATE );
424 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100425}
426
Steven Cooreman87885df2021-03-19 19:04:39 +0100427static psa_status_t mac_finish_internal( mbedtls_psa_mac_operation_t *operation,
428 uint8_t *mac,
429 size_t mac_size )
430{
Steven Cooreman87885df2021-03-19 19:04:39 +0100431#if defined(BUILTIN_ALG_CMAC)
Steven Cooreman15f0d922021-05-07 14:14:37 +0200432 if( PSA_ALG_FULL_LENGTH_MAC( operation->alg ) == PSA_ALG_CMAC )
Steven Cooreman87885df2021-03-19 19:04:39 +0100433 {
434 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
435 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
436 if( ret == 0 )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200437 memcpy( mac, tmp, mac_size );
Steven Cooreman87885df2021-03-19 19:04:39 +0100438 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
439 return( mbedtls_to_psa_error( ret ) );
440 }
441 else
442#endif /* BUILTIN_ALG_CMAC */
443#if defined(BUILTIN_ALG_HMAC)
444 if( PSA_ALG_IS_HMAC( operation->alg ) )
445 {
446 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Steven Cooreman15f0d922021-05-07 14:14:37 +0200447 mac, mac_size ) );
Steven Cooreman87885df2021-03-19 19:04:39 +0100448 }
449 else
450#endif /* BUILTIN_ALG_HMAC */
451 {
452 /* This shouldn't happen if `operation` was initialized by
453 * a setup function. */
Steven Cooreman15f0d922021-05-07 14:14:37 +0200454 (void) operation;
455 (void) mac;
456 (void) mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100457 return( PSA_ERROR_BAD_STATE );
458 }
459}
460
Steven Cooreman896d51e2021-03-19 15:24:23 +0100461static psa_status_t mac_sign_finish(
462 mbedtls_psa_mac_operation_t *operation,
463 uint8_t *mac,
464 size_t mac_size,
465 size_t *mac_length )
466{
Steven Cooreman9878a162021-05-06 17:58:36 +0200467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100468
469 if( operation->alg == 0 )
470 return( PSA_ERROR_BAD_STATE );
471
Steven Cooreman87885df2021-03-19 19:04:39 +0100472 status = mac_finish_internal( operation, mac, mac_size );
473
474 if( status == PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +0200475 *mac_length = mac_size;
Steven Cooreman87885df2021-03-19 19:04:39 +0100476
477 return( status );
478}
479
Steven Cooreman896d51e2021-03-19 15:24:23 +0100480static psa_status_t mac_verify_finish(
481 mbedtls_psa_mac_operation_t *operation,
482 const uint8_t *mac,
483 size_t mac_length )
484{
Steven Cooreman87885df2021-03-19 19:04:39 +0100485 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman9878a162021-05-06 17:58:36 +0200486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100487
488 if( operation->alg == 0 )
489 return( PSA_ERROR_BAD_STATE );
490
Steven Cooreman15f0d922021-05-07 14:14:37 +0200491 /* Consistency check: requested MAC length fits our local buffer */
492 if( mac_length > sizeof( actual_mac ) )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman87885df2021-03-19 19:04:39 +0100494
Steven Cooreman15f0d922021-05-07 14:14:37 +0200495 status = mac_finish_internal( operation, actual_mac, mac_length );
Steven Cooreman87885df2021-03-19 19:04:39 +0100496 if( status != PSA_SUCCESS )
497 goto cleanup;
498
Steven Cooremana2a1b802021-04-29 16:37:59 +0200499 if( mbedtls_psa_safer_memcmp( mac, actual_mac, mac_length ) != 0 )
Steven Cooreman87885df2021-03-19 19:04:39 +0100500 status = PSA_ERROR_INVALID_SIGNATURE;
501
502cleanup:
503 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
504
505 return( status );
Steven Cooreman896d51e2021-03-19 15:24:23 +0100506}
Steven Cooreman896d51e2021-03-19 15:24:23 +0100507#endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */
508
509#if defined(MBEDTLS_PSA_BUILTIN_MAC)
510psa_status_t mbedtls_psa_mac_compute(
511 const psa_key_attributes_t *attributes,
512 const uint8_t *key_buffer,
513 size_t key_buffer_size,
514 psa_algorithm_t alg,
515 const uint8_t *input,
516 size_t input_length,
517 uint8_t *mac,
518 size_t mac_size,
519 size_t *mac_length )
520{
521 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
522 input, input_length,
523 mac, mac_size, mac_length ) );
524}
525
526psa_status_t mbedtls_psa_mac_sign_setup(
527 mbedtls_psa_mac_operation_t *operation,
528 const psa_key_attributes_t *attributes,
529 const uint8_t *key_buffer,
530 size_t key_buffer_size,
531 psa_algorithm_t alg )
532{
533 return( mac_sign_setup( operation, attributes,
534 key_buffer, key_buffer_size, alg ) );
535}
536
537psa_status_t mbedtls_psa_mac_verify_setup(
538 mbedtls_psa_mac_operation_t *operation,
539 const psa_key_attributes_t *attributes,
540 const uint8_t *key_buffer,
541 size_t key_buffer_size,
542 psa_algorithm_t alg )
543{
544 return( mac_verify_setup( operation, attributes,
545 key_buffer, key_buffer_size, alg ) );
546}
547
548psa_status_t mbedtls_psa_mac_update(
549 mbedtls_psa_mac_operation_t *operation,
550 const uint8_t *input,
551 size_t input_length )
552{
553 return( mac_update( operation, input, input_length ) );
554}
555
556psa_status_t mbedtls_psa_mac_sign_finish(
557 mbedtls_psa_mac_operation_t *operation,
558 uint8_t *mac,
559 size_t mac_size,
560 size_t *mac_length )
561{
562 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
563}
564
565psa_status_t mbedtls_psa_mac_verify_finish(
566 mbedtls_psa_mac_operation_t *operation,
567 const uint8_t *mac,
568 size_t mac_length )
569{
570 return( mac_verify_finish( operation, mac, mac_length ) );
571}
572
573psa_status_t mbedtls_psa_mac_abort(
574 mbedtls_psa_mac_operation_t *operation )
575{
576 return( mac_abort( operation ) );
577}
578#endif /* MBEDTLS_PSA_BUILTIN_MAC */
579
580 /*
581 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
582 */
583#if defined(PSA_CRYPTO_DRIVER_TEST)
584
585static int is_mac_accelerated( psa_algorithm_t alg )
586{
587#if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC)
588 if( PSA_ALG_IS_HMAC( alg ) )
589 return( 1 );
590#endif
591
592 switch( PSA_ALG_FULL_LENGTH_MAC( alg ) )
593 {
594#if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC)
595 case PSA_ALG_CMAC:
596 return( 1 );
597#endif
598 default:
599 return( 0 );
600 }
601}
602
603psa_status_t mbedtls_transparent_test_driver_mac_compute(
604 const psa_key_attributes_t *attributes,
605 const uint8_t *key_buffer,
606 size_t key_buffer_size,
607 psa_algorithm_t alg,
608 const uint8_t *input,
609 size_t input_length,
610 uint8_t *mac,
611 size_t mac_size,
612 size_t *mac_length )
613{
614 if( is_mac_accelerated( alg ) )
615 return( mac_compute( attributes, key_buffer, key_buffer_size, alg,
616 input, input_length,
617 mac, mac_size, mac_length ) );
618 else
619 return( PSA_ERROR_NOT_SUPPORTED );
620}
621
622psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
623 mbedtls_transparent_test_driver_mac_operation_t *operation,
624 const psa_key_attributes_t *attributes,
625 const uint8_t *key_buffer,
626 size_t key_buffer_size,
627 psa_algorithm_t alg )
628{
629 if( is_mac_accelerated( alg ) )
630 return( mac_sign_setup( operation, attributes,
631 key_buffer, key_buffer_size, alg ) );
632 else
633 return( PSA_ERROR_NOT_SUPPORTED );
634}
635
636psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
637 mbedtls_transparent_test_driver_mac_operation_t *operation,
638 const psa_key_attributes_t *attributes,
639 const uint8_t *key_buffer,
640 size_t key_buffer_size,
641 psa_algorithm_t alg )
642{
643 if( is_mac_accelerated( alg ) )
644 return( mac_verify_setup( operation, attributes,
645 key_buffer, key_buffer_size, alg ) );
646 else
647 return( PSA_ERROR_NOT_SUPPORTED );
648}
649
650psa_status_t mbedtls_transparent_test_driver_mac_update(
651 mbedtls_transparent_test_driver_mac_operation_t *operation,
652 const uint8_t *input,
653 size_t input_length )
654{
655 if( is_mac_accelerated( operation->alg ) )
656 return( mac_update( operation, input, input_length ) );
657 else
658 return( PSA_ERROR_BAD_STATE );
659}
660
661psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
662 mbedtls_transparent_test_driver_mac_operation_t *operation,
663 uint8_t *mac,
664 size_t mac_size,
665 size_t *mac_length )
666{
667 if( is_mac_accelerated( operation->alg ) )
668 return( mac_sign_finish( operation, mac, mac_size, mac_length ) );
669 else
670 return( PSA_ERROR_BAD_STATE );
671}
672
673psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
674 mbedtls_transparent_test_driver_mac_operation_t *operation,
675 const uint8_t *mac,
676 size_t mac_length )
677{
678 if( is_mac_accelerated( operation->alg ) )
679 return( mac_verify_finish( operation, mac, mac_length ) );
680 else
681 return( PSA_ERROR_BAD_STATE );
682}
683
684psa_status_t mbedtls_transparent_test_driver_mac_abort(
685 mbedtls_transparent_test_driver_mac_operation_t *operation )
686{
687 return( mac_abort( operation ) );
688}
689
690psa_status_t mbedtls_opaque_test_driver_mac_compute(
691 const psa_key_attributes_t *attributes,
692 const uint8_t *key_buffer,
693 size_t key_buffer_size,
694 psa_algorithm_t alg,
695 const uint8_t *input,
696 size_t input_length,
697 uint8_t *mac,
698 size_t mac_size,
699 size_t *mac_length )
700{
701 /* Opaque driver testing is not implemented yet through this mechanism. */
702 (void) attributes;
703 (void) key_buffer;
704 (void) key_buffer_size;
705 (void) alg;
706 (void) input;
707 (void) input_length;
708 (void) mac;
709 (void) mac_size;
710 (void) mac_length;
711 return( PSA_ERROR_NOT_SUPPORTED );
712}
713
714psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
715 mbedtls_opaque_test_driver_mac_operation_t *operation,
716 const psa_key_attributes_t *attributes,
717 const uint8_t *key_buffer,
718 size_t key_buffer_size,
719 psa_algorithm_t alg )
720{
721 /* Opaque driver testing is not implemented yet through this mechanism. */
722 (void) operation;
723 (void) attributes;
724 (void) key_buffer;
725 (void) key_buffer_size;
726 (void) alg;
727 return( PSA_ERROR_NOT_SUPPORTED );
728}
729
730psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
731 mbedtls_opaque_test_driver_mac_operation_t *operation,
732 const psa_key_attributes_t *attributes,
733 const uint8_t *key_buffer,
734 size_t key_buffer_size,
735 psa_algorithm_t alg )
736{
737 /* Opaque driver testing is not implemented yet through this mechanism. */
738 (void) operation;
739 (void) attributes;
740 (void) key_buffer;
741 (void) key_buffer_size;
742 (void) alg;
743 return( PSA_ERROR_NOT_SUPPORTED );
744}
745
746psa_status_t mbedtls_opaque_test_driver_mac_update(
747 mbedtls_opaque_test_driver_mac_operation_t *operation,
748 const uint8_t *input,
749 size_t input_length )
750{
751 /* Opaque driver testing is not implemented yet through this mechanism. */
752 (void) operation;
753 (void) input;
754 (void) input_length;
755 return( PSA_ERROR_NOT_SUPPORTED );
756}
757
758psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
759 mbedtls_opaque_test_driver_mac_operation_t *operation,
760 uint8_t *mac,
761 size_t mac_size,
762 size_t *mac_length )
763{
764 /* Opaque driver testing is not implemented yet through this mechanism. */
765 (void) operation;
766 (void) mac;
767 (void) mac_size;
768 (void) mac_length;
769 return( PSA_ERROR_NOT_SUPPORTED );
770}
771
772psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
773 mbedtls_opaque_test_driver_mac_operation_t *operation,
774 const uint8_t *mac,
775 size_t mac_length )
776{
777 /* Opaque driver testing is not implemented yet through this mechanism. */
778 (void) operation;
779 (void) mac;
780 (void) mac_length;
781 return( PSA_ERROR_NOT_SUPPORTED );
782}
783
784psa_status_t mbedtls_opaque_test_driver_mac_abort(
785 mbedtls_opaque_test_driver_mac_operation_t *operation )
786{
787 /* Opaque driver testing is not implemented yet through this mechanism. */
788 (void) operation;
789 return( PSA_ERROR_NOT_SUPPORTED );
790}
791
792#endif /* PSA_CRYPTO_DRIVER_TEST */
793
794#endif /* MBEDTLS_PSA_CRYPTO_C */