1/*
2 *  Functions to delegate cryptographic operations to an available
3 *  and appropriate accelerator.
4 *  Warning: This file is now auto-generated.
5 */
6/*  Copyright The Mbed TLS Contributors
7 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8 */
9
10
11/* BEGIN-common headers */
12#include "common.h"
13#include "psa_crypto_aead.h"
14#include "psa_crypto_cipher.h"
15#include "psa_crypto_core.h"
16#include "psa_crypto_driver_wrappers_no_static.h"
17#include "psa_crypto_hash.h"
18#include "psa_crypto_mac.h"
19#include "psa_crypto_pake.h"
20#include "psa_crypto_rsa.h"
21
22#include "mbedtls/platform.h"
23#include "mbedtls/constant_time.h"
24/* END-common headers */
25
26#if defined(MBEDTLS_PSA_CRYPTO_C)
27
28/* BEGIN-driver headers */
29{% for driver in drivers -%}
30/* Headers for {{driver.prefix}} {{driver.type}} driver */
31{% if driver['mbedtls/h_condition'] is defined -%}
32#if {{ driver['mbedtls/h_condition'] }}
33{% endif -%}
34{% for header in driver.headers -%}
35#include "{{ header }}"
36{% endfor %}
37{% if driver['mbedtls/h_condition'] is defined -%}
38#endif
39{% endif -%}
40{% endfor %}
41/* END-driver headers */
42
43/* Auto-generated values depending on which drivers are registered.
44 * ID 0 is reserved for unallocated operations.
45 * ID 1 is reserved for the Mbed TLS software driver. */
46/* BEGIN-driver id definition */
47#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
48{% for driver in drivers -%}
49#define {{(driver.prefix + "_" + driver.type + "_driver_id").upper()}} ({{ loop.index + 1 }})
50{% endfor %}
51/* END-driver id */
52
53/* BEGIN-Common Macro definitions */
54{% macro entry_point_name(capability, entry_point, driver) -%}
55    {% if capability.name is defined and entry_point in capability.names.keys() -%}
56    {{ capability.names[entry_point]}}
57    {% else -%}
58    {{driver.prefix}}_{{driver.type}}_{{entry_point}}
59    {% endif -%}
60{% endmacro %}
61/* END-Common Macro definitions */
62
63/* Support the 'old' SE interface when asked to */
64#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
65/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
66 * SE driver is present, to avoid unused argument errors at compile time. */
67#ifndef PSA_CRYPTO_DRIVER_PRESENT
68#define PSA_CRYPTO_DRIVER_PRESENT
69#endif
70#include "psa_crypto_se.h"
71#endif
72
73static inline psa_status_t psa_driver_wrapper_init( void )
74{
75    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
76
77#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
78    status = psa_init_all_se_drivers( );
79    if( status != PSA_SUCCESS )
80        return( status );
81#endif
82
83#if defined(PSA_CRYPTO_DRIVER_TEST)
84    status = mbedtls_test_transparent_init( );
85    if( status != PSA_SUCCESS )
86        return( status );
87
88    status = mbedtls_test_opaque_init( );
89    if( status != PSA_SUCCESS )
90        return( status );
91#endif
92
93    (void) status;
94    return( PSA_SUCCESS );
95}
96
97static inline void psa_driver_wrapper_free( void )
98{
99#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
100    /* Unregister all secure element drivers, so that we restart from
101     * a pristine state. */
102    psa_unregister_all_se_drivers( );
103#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
104
105#if defined(PSA_CRYPTO_DRIVER_TEST)
106    mbedtls_test_transparent_free( );
107    mbedtls_test_opaque_free( );
108#endif
109}
110
111/* Start delegation functions */
112static inline psa_status_t psa_driver_wrapper_sign_message(
113    const psa_key_attributes_t *attributes,
114    const uint8_t *key_buffer,
115    size_t key_buffer_size,
116    psa_algorithm_t alg,
117    const uint8_t *input,
118    size_t input_length,
119    uint8_t *signature,
120    size_t signature_size,
121    size_t *signature_length )
122{
123    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
124    psa_key_location_t location =
125        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
126
127    switch( location )
128    {
129        case PSA_KEY_LOCATION_LOCAL_STORAGE:
130            /* Key is stored in the slot in export representation, so
131             * cycle through all known transparent accelerators */
132#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
133#if defined(PSA_CRYPTO_DRIVER_TEST)
134            status = mbedtls_test_transparent_signature_sign_message(
135                        attributes,
136                        key_buffer,
137                        key_buffer_size,
138                        alg,
139                        input,
140                        input_length,
141                        signature,
142                        signature_size,
143                        signature_length );
144            /* Declared with fallback == true */
145            if( status != PSA_ERROR_NOT_SUPPORTED )
146                return( status );
147#endif /* PSA_CRYPTO_DRIVER_TEST */
148#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
149            break;
150
151        /* Add cases for opaque driver here */
152#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
153#if defined(PSA_CRYPTO_DRIVER_TEST)
154        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
155            status = mbedtls_test_opaque_signature_sign_message(
156                        attributes,
157                        key_buffer,
158                        key_buffer_size,
159                        alg,
160                        input,
161                        input_length,
162                        signature,
163                        signature_size,
164                        signature_length );
165            if( status != PSA_ERROR_NOT_SUPPORTED )
166                return( status );
167            break;
168#endif /* PSA_CRYPTO_DRIVER_TEST */
169#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
170        default:
171            /* Key is declared with a lifetime not known to us */
172            (void)status;
173            break;
174    }
175
176    return( psa_sign_message_builtin( attributes,
177                                      key_buffer,
178                                      key_buffer_size,
179                                      alg,
180                                      input,
181                                      input_length,
182                                      signature,
183                                      signature_size,
184                                      signature_length ) );
185}
186
187static inline psa_status_t psa_driver_wrapper_verify_message(
188    const psa_key_attributes_t *attributes,
189    const uint8_t *key_buffer,
190    size_t key_buffer_size,
191    psa_algorithm_t alg,
192    const uint8_t *input,
193    size_t input_length,
194    const uint8_t *signature,
195    size_t signature_length )
196{
197    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
198    psa_key_location_t location =
199        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
200
201    switch( location )
202    {
203        case PSA_KEY_LOCATION_LOCAL_STORAGE:
204            /* Key is stored in the slot in export representation, so
205             * cycle through all known transparent accelerators */
206#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
207#if defined(PSA_CRYPTO_DRIVER_TEST)
208            status = mbedtls_test_transparent_signature_verify_message(
209                        attributes,
210                        key_buffer,
211                        key_buffer_size,
212                        alg,
213                        input,
214                        input_length,
215                        signature,
216                        signature_length );
217            /* Declared with fallback == true */
218            if( status != PSA_ERROR_NOT_SUPPORTED )
219                return( status );
220#endif /* PSA_CRYPTO_DRIVER_TEST */
221#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
222            break;
223
224        /* Add cases for opaque driver here */
225#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
226#if defined(PSA_CRYPTO_DRIVER_TEST)
227        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
228            return( mbedtls_test_opaque_signature_verify_message(
229                        attributes,
230                        key_buffer,
231                        key_buffer_size,
232                        alg,
233                        input,
234                        input_length,
235                        signature,
236                        signature_length ) );
237            if( status != PSA_ERROR_NOT_SUPPORTED )
238                return( status );
239            break;
240#endif /* PSA_CRYPTO_DRIVER_TEST */
241#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
242        default:
243            /* Key is declared with a lifetime not known to us */
244            (void)status;
245            break;
246    }
247
248    return( psa_verify_message_builtin( attributes,
249                                        key_buffer,
250                                        key_buffer_size,
251                                        alg,
252                                        input,
253                                        input_length,
254                                        signature,
255                                        signature_length ) );
256}
257
258static inline psa_status_t psa_driver_wrapper_sign_hash(
259    const psa_key_attributes_t *attributes,
260    const uint8_t *key_buffer, size_t key_buffer_size,
261    psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
262    uint8_t *signature, size_t signature_size, size_t *signature_length )
263{
264    /* Try dynamically-registered SE interface first */
265#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
266    const psa_drv_se_t *drv;
267    psa_drv_se_context_t *drv_context;
268
269    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
270    {
271        if( drv->asymmetric == NULL ||
272            drv->asymmetric->p_sign == NULL )
273        {
274            /* Key is defined in SE, but we have no way to exercise it */
275            return( PSA_ERROR_NOT_SUPPORTED );
276        }
277        return( drv->asymmetric->p_sign(
278                    drv_context, *( (psa_key_slot_number_t *)key_buffer ),
279                    alg, hash, hash_length,
280                    signature, signature_size, signature_length ) );
281    }
282#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
283
284    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
285    psa_key_location_t location =
286        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
287
288    switch( location )
289    {
290        case PSA_KEY_LOCATION_LOCAL_STORAGE:
291            /* Key is stored in the slot in export representation, so
292             * cycle through all known transparent accelerators */
293#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
294#if defined(PSA_CRYPTO_DRIVER_TEST)
295            status = mbedtls_test_transparent_signature_sign_hash( attributes,
296                                                           key_buffer,
297                                                           key_buffer_size,
298                                                           alg,
299                                                           hash,
300                                                           hash_length,
301                                                           signature,
302                                                           signature_size,
303                                                           signature_length );
304            /* Declared with fallback == true */
305            if( status != PSA_ERROR_NOT_SUPPORTED )
306                return( status );
307#endif /* PSA_CRYPTO_DRIVER_TEST */
308#if defined (MBEDTLS_PSA_P256M_DRIVER_ENABLED)
309            if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
310                PSA_ALG_IS_ECDSA(alg) &&
311                !PSA_ALG_ECDSA_IS_DETERMINISTIC( alg ) &&
312                PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
313                psa_get_key_bits(attributes) == 256 )
314            {
315                status = p256_transparent_sign_hash( attributes,
316                                                     key_buffer,
317                                                     key_buffer_size,
318                                                     alg,
319                                                     hash,
320                                                     hash_length,
321                                                     signature,
322                                                     signature_size,
323                                                     signature_length );
324                if( status != PSA_ERROR_NOT_SUPPORTED )
325                return( status );
326            }
327#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
328#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
329            /* Fell through, meaning no accelerator supports this operation */
330            return( psa_sign_hash_builtin( attributes,
331                                           key_buffer,
332                                           key_buffer_size,
333                                           alg,
334                                           hash,
335                                           hash_length,
336                                           signature,
337                                           signature_size,
338                                           signature_length ) );
339
340        /* Add cases for opaque driver here */
341#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
342#if defined(PSA_CRYPTO_DRIVER_TEST)
343        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
344            return( mbedtls_test_opaque_signature_sign_hash( attributes,
345                                                             key_buffer,
346                                                             key_buffer_size,
347                                                             alg,
348                                                             hash,
349                                                             hash_length,
350                                                             signature,
351                                                             signature_size,
352                                                             signature_length ) );
353#endif /* PSA_CRYPTO_DRIVER_TEST */
354#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
355        default:
356            /* Key is declared with a lifetime not known to us */
357            (void)status;
358            return( PSA_ERROR_INVALID_ARGUMENT );
359    }
360}
361
362static inline psa_status_t psa_driver_wrapper_verify_hash(
363    const psa_key_attributes_t *attributes,
364    const uint8_t *key_buffer, size_t key_buffer_size,
365    psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
366    const uint8_t *signature, size_t signature_length )
367{
368    /* Try dynamically-registered SE interface first */
369#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
370    const psa_drv_se_t *drv;
371    psa_drv_se_context_t *drv_context;
372
373    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
374    {
375        if( drv->asymmetric == NULL ||
376            drv->asymmetric->p_verify == NULL )
377        {
378            /* Key is defined in SE, but we have no way to exercise it */
379            return( PSA_ERROR_NOT_SUPPORTED );
380        }
381        return( drv->asymmetric->p_verify(
382                    drv_context, *( (psa_key_slot_number_t *)key_buffer ),
383                    alg, hash, hash_length,
384                    signature, signature_length ) );
385    }
386#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
387
388    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
389    psa_key_location_t location =
390        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
391
392    switch( location )
393    {
394        case PSA_KEY_LOCATION_LOCAL_STORAGE:
395            /* Key is stored in the slot in export representation, so
396             * cycle through all known transparent accelerators */
397#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
398#if defined(PSA_CRYPTO_DRIVER_TEST)
399            status = mbedtls_test_transparent_signature_verify_hash(
400                         attributes,
401                         key_buffer,
402                         key_buffer_size,
403                         alg,
404                         hash,
405                         hash_length,
406                         signature,
407                         signature_length );
408            /* Declared with fallback == true */
409            if( status != PSA_ERROR_NOT_SUPPORTED )
410                return( status );
411#endif /* PSA_CRYPTO_DRIVER_TEST */
412#if defined (MBEDTLS_PSA_P256M_DRIVER_ENABLED)
413            if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
414                PSA_ALG_IS_ECDSA(alg) &&
415                !PSA_ALG_ECDSA_IS_DETERMINISTIC( alg ) &&
416                PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
417                psa_get_key_bits(attributes) == 256 )
418            {
419                status = p256_transparent_verify_hash( attributes,
420                                                       key_buffer,
421                                                       key_buffer_size,
422                                                       alg,
423                                                       hash,
424                                                       hash_length,
425                                                       signature,
426                                                       signature_length );
427                if( status != PSA_ERROR_NOT_SUPPORTED )
428                return( status );
429            }
430#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
431#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
432
433            return( psa_verify_hash_builtin( attributes,
434                                             key_buffer,
435                                             key_buffer_size,
436                                             alg,
437                                             hash,
438                                             hash_length,
439                                             signature,
440                                             signature_length ) );
441
442        /* Add cases for opaque driver here */
443#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
444#if defined(PSA_CRYPTO_DRIVER_TEST)
445        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
446            return( mbedtls_test_opaque_signature_verify_hash( attributes,
447                                                               key_buffer,
448                                                               key_buffer_size,
449                                                               alg,
450                                                               hash,
451                                                               hash_length,
452                                                               signature,
453                                                               signature_length ) );
454#endif /* PSA_CRYPTO_DRIVER_TEST */
455#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
456        default:
457            /* Key is declared with a lifetime not known to us */
458            (void)status;
459            return( PSA_ERROR_INVALID_ARGUMENT );
460    }
461}
462
463static inline uint32_t psa_driver_wrapper_sign_hash_get_num_ops(
464    psa_sign_hash_interruptible_operation_t *operation )
465{
466    switch( operation->id )
467    {
468        /* If uninitialised, return 0, as no work can have been done. */
469        case 0:
470            return 0;
471
472        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
473            return(mbedtls_psa_sign_hash_get_num_ops(&operation->ctx.mbedtls_ctx));
474
475#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
476#if defined(PSA_CRYPTO_DRIVER_TEST)
477            /* Add test driver tests here */
478
479#endif /* PSA_CRYPTO_DRIVER_TEST */
480#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
481    }
482
483    /* Can't happen (see discussion in #8271) */
484    return 0;
485}
486
487static inline uint32_t psa_driver_wrapper_verify_hash_get_num_ops(
488    psa_verify_hash_interruptible_operation_t *operation )
489{
490    switch( operation->id )
491    {
492        /* If uninitialised, return 0, as no work can have been done. */
493        case 0:
494            return 0;
495
496        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
497            return (mbedtls_psa_verify_hash_get_num_ops(&operation->ctx.mbedtls_ctx));
498
499#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
500#if defined(PSA_CRYPTO_DRIVER_TEST)
501            /* Add test driver tests here */
502
503#endif /* PSA_CRYPTO_DRIVER_TEST */
504#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
505
506    }
507
508    /* Can't happen (see discussion in #8271) */
509    return 0;
510}
511
512static inline psa_status_t psa_driver_wrapper_sign_hash_start(
513    psa_sign_hash_interruptible_operation_t *operation,
514    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
515    size_t key_buffer_size, psa_algorithm_t alg,
516    const uint8_t *hash, size_t hash_length )
517{
518    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
519    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
520                                                    psa_get_key_lifetime(attributes) );
521
522    switch( location )
523    {
524        case PSA_KEY_LOCATION_LOCAL_STORAGE:
525            /* Key is stored in the slot in export representation, so
526             * cycle through all known transparent accelerators */
527
528#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
529#if defined(PSA_CRYPTO_DRIVER_TEST)
530
531            /* Add test driver tests here */
532
533            /* Declared with fallback == true */
534
535#endif /* PSA_CRYPTO_DRIVER_TEST */
536#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
537
538            /* Fell through, meaning no accelerator supports this operation */
539            operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
540            status = mbedtls_psa_sign_hash_start( &operation->ctx.mbedtls_ctx,
541                                                  attributes,
542                                                  key_buffer, key_buffer_size,
543                                                  alg, hash, hash_length );
544            break;
545
546            /* Add cases for opaque driver here */
547
548        default:
549            /* Key is declared with a lifetime not known to us */
550            status = PSA_ERROR_INVALID_ARGUMENT;
551            break;
552    }
553
554    return( status );
555}
556
557static inline psa_status_t psa_driver_wrapper_sign_hash_complete(
558    psa_sign_hash_interruptible_operation_t *operation,
559    uint8_t *signature, size_t signature_size,
560    size_t *signature_length )
561{
562    switch( operation->id )
563    {
564        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
565            return( mbedtls_psa_sign_hash_complete( &operation->ctx.mbedtls_ctx,
566                                                    signature, signature_size,
567                                                    signature_length ) );
568
569#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
570#if defined(PSA_CRYPTO_DRIVER_TEST)
571            /* Add test driver tests here */
572
573#endif /* PSA_CRYPTO_DRIVER_TEST */
574#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
575    }
576
577    ( void ) signature;
578    ( void ) signature_size;
579    ( void ) signature_length;
580
581    return( PSA_ERROR_INVALID_ARGUMENT );
582}
583
584static inline psa_status_t psa_driver_wrapper_sign_hash_abort(
585    psa_sign_hash_interruptible_operation_t *operation )
586{
587    switch( operation->id )
588    {
589        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
590            return( mbedtls_psa_sign_hash_abort( &operation->ctx.mbedtls_ctx ) );
591
592#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
593#if defined(PSA_CRYPTO_DRIVER_TEST)
594            /* Add test driver tests here */
595
596#endif /* PSA_CRYPTO_DRIVER_TEST */
597#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
598    }
599
600    return( PSA_ERROR_INVALID_ARGUMENT );
601}
602
603static inline psa_status_t psa_driver_wrapper_verify_hash_start(
604    psa_verify_hash_interruptible_operation_t *operation,
605    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
606    size_t key_buffer_size, psa_algorithm_t alg,
607    const uint8_t *hash, size_t hash_length,
608    const uint8_t *signature, size_t signature_length )
609{
610    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
611    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
612                                                    psa_get_key_lifetime(attributes) );
613
614    switch( location )
615    {
616        case PSA_KEY_LOCATION_LOCAL_STORAGE:
617            /* Key is stored in the slot in export representation, so
618             * cycle through all known transparent accelerators */
619
620#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
621#if defined(PSA_CRYPTO_DRIVER_TEST)
622
623            /* Add test driver tests here */
624
625            /* Declared with fallback == true */
626
627#endif /* PSA_CRYPTO_DRIVER_TEST */
628#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
629
630            /* Fell through, meaning no accelerator supports this operation */
631            operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
632            status = mbedtls_psa_verify_hash_start( &operation->ctx.mbedtls_ctx,
633                                                    attributes,
634                                                    key_buffer, key_buffer_size,
635                                                    alg, hash, hash_length,
636                                                    signature, signature_length );
637            break;
638
639            /* Add cases for opaque driver here */
640
641        default:
642            /* Key is declared with a lifetime not known to us */
643            status = PSA_ERROR_INVALID_ARGUMENT;
644            break;
645    }
646
647    return( status );
648}
649
650static inline psa_status_t psa_driver_wrapper_verify_hash_complete(
651    psa_verify_hash_interruptible_operation_t *operation )
652{
653    switch( operation->id )
654    {
655        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
656            return( mbedtls_psa_verify_hash_complete(
657                                                     &operation->ctx.mbedtls_ctx
658                                                     ) );
659
660#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
661#if defined(PSA_CRYPTO_DRIVER_TEST)
662            /* Add test driver tests here */
663
664#endif /* PSA_CRYPTO_DRIVER_TEST */
665#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
666    }
667
668    return( PSA_ERROR_INVALID_ARGUMENT );
669}
670
671static inline psa_status_t psa_driver_wrapper_verify_hash_abort(
672    psa_verify_hash_interruptible_operation_t *operation )
673{
674    switch( operation->id )
675    {
676        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
677            return( mbedtls_psa_verify_hash_abort( &operation->ctx.mbedtls_ctx
678                                                 ) );
679
680#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
681#if defined(PSA_CRYPTO_DRIVER_TEST)
682            /* Add test driver tests here */
683
684#endif /* PSA_CRYPTO_DRIVER_TEST */
685#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
686    }
687
688    return( PSA_ERROR_INVALID_ARGUMENT );
689}
690
691/** Calculate the key buffer size required to store the key material of a key
692 *  associated with an opaque driver from input key data.
693 *
694 * \param[in] attributes        The key attributes
695 * \param[in] data              The input key data.
696 * \param[in] data_length       The input data length.
697 * \param[out] key_buffer_size  Minimum buffer size to contain the key material.
698 *
699 * \retval #PSA_SUCCESS \emptydescription
700 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
701 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
702 */
703static inline psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
704    const psa_key_attributes_t *attributes,
705    const uint8_t *data,
706    size_t data_length,
707    size_t *key_buffer_size )
708{
709    psa_key_location_t location =
710        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
711    psa_key_type_t key_type = psa_get_key_type(attributes);
712
713    *key_buffer_size = 0;
714    switch( location )
715    {
716#if defined(PSA_CRYPTO_DRIVER_TEST)
717        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
718            *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
719                                     PSA_BYTES_TO_BITS( data_length ) );
720            return( ( *key_buffer_size != 0 ) ?
721                    PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
722#endif /* PSA_CRYPTO_DRIVER_TEST */
723
724        default:
725            (void)key_type;
726            (void)data;
727            (void)data_length;
728            return( PSA_ERROR_INVALID_ARGUMENT );
729    }
730}
731
732static inline psa_status_t psa_driver_wrapper_generate_key(
733    const psa_key_attributes_t *attributes,
734    const psa_key_production_parameters_t *params, size_t params_data_length,
735    uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
736{
737    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
738    psa_key_location_t location =
739        PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(attributes));
740
741#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
742    int is_default_production =
743        psa_key_production_parameters_are_default(params, params_data_length);
744    if( location != PSA_KEY_LOCATION_LOCAL_STORAGE && !is_default_production )
745    {
746        /* We don't support passing custom production parameters
747         * to drivers yet. */
748        return PSA_ERROR_NOT_SUPPORTED;
749    }
750#else
751    int is_default_production = 1;
752    (void) is_default_production;
753#endif
754
755    /* Try dynamically-registered SE interface first */
756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
757    const psa_drv_se_t *drv;
758    psa_drv_se_context_t *drv_context;
759
760    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
761    {
762        size_t pubkey_length = 0; /* We don't support this feature yet */
763        if( drv->key_management == NULL ||
764            drv->key_management->p_generate == NULL )
765        {
766            /* Key is defined as being in SE, but we have no way to generate it */
767            return( PSA_ERROR_NOT_SUPPORTED );
768        }
769        return( drv->key_management->p_generate(
770            drv_context,
771            *( (psa_key_slot_number_t *)key_buffer ),
772            attributes, NULL, 0, &pubkey_length ) );
773    }
774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
775
776    switch( location )
777    {
778        case PSA_KEY_LOCATION_LOCAL_STORAGE:
779#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
780            /* Transparent drivers are limited to generating asymmetric keys. */
781            /* We don't support passing custom production parameters
782             * to drivers yet. */
783            if( PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type(attributes) ) &&
784                is_default_production )
785            {
786            /* Cycle through all known transparent accelerators */
787#if defined(PSA_CRYPTO_DRIVER_TEST)
788                status = mbedtls_test_transparent_generate_key(
789                    attributes, key_buffer, key_buffer_size,
790                    key_buffer_length );
791                /* Declared with fallback == true */
792                if( status != PSA_ERROR_NOT_SUPPORTED )
793                    break;
794#endif /* PSA_CRYPTO_DRIVER_TEST */
795#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
796                if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
797                    psa_get_key_type(attributes) == PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) &&
798                    psa_get_key_bits(attributes) == 256 )
799                {
800                    status = p256_transparent_generate_key( attributes,
801                                                            key_buffer,
802                                                            key_buffer_size,
803                                                            key_buffer_length );
804                    if( status != PSA_ERROR_NOT_SUPPORTED )
805                        break;
806                }
807
808#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
809            }
810#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
811
812            /* Software fallback */
813            status = psa_generate_key_internal(
814                attributes, params, params_data_length,
815                key_buffer, key_buffer_size, key_buffer_length );
816            break;
817
818        /* Add cases for opaque driver here */
819#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
820#if defined(PSA_CRYPTO_DRIVER_TEST)
821        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
822            status = mbedtls_test_opaque_generate_key(
823                attributes, key_buffer, key_buffer_size, key_buffer_length );
824            break;
825#endif /* PSA_CRYPTO_DRIVER_TEST */
826#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
827
828        default:
829            /* Key is declared with a lifetime not known to us */
830            status = PSA_ERROR_INVALID_ARGUMENT;
831            break;
832    }
833
834    return( status );
835}
836
837static inline psa_status_t psa_driver_wrapper_import_key(
838    const psa_key_attributes_t *attributes,
839    const uint8_t *data,
840    size_t data_length,
841    uint8_t *key_buffer,
842    size_t key_buffer_size,
843    size_t *key_buffer_length,
844    size_t *bits )
845{
846{% with entry_point = "import_key" -%}
847{% macro entry_point_param(driver) -%}
848attributes,
849data,
850data_length,
851key_buffer,
852key_buffer_size,
853key_buffer_length,
854bits
855{% endmacro %}
856    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
857    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
858                                      psa_get_key_lifetime( attributes ) );
859
860    /* Try dynamically-registered SE interface first */
861#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
862    const psa_drv_se_t *drv;
863    psa_drv_se_context_t *drv_context;
864
865    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
866    {
867        if( drv->key_management == NULL ||
868            drv->key_management->p_import == NULL )
869            return( PSA_ERROR_NOT_SUPPORTED );
870
871        /* The driver should set the number of key bits, however in
872         * case it doesn't, we initialize bits to an invalid value. */
873        *bits = PSA_MAX_KEY_BITS + 1;
874        status = drv->key_management->p_import(
875            drv_context,
876            *( (psa_key_slot_number_t *)key_buffer ),
877            attributes, data, data_length, bits );
878
879        if( status != PSA_SUCCESS )
880            return( status );
881
882        if( (*bits) > PSA_MAX_KEY_BITS )
883            return( PSA_ERROR_NOT_SUPPORTED );
884
885        return( PSA_SUCCESS );
886    }
887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
888
889    switch( location )
890    {
891        case PSA_KEY_LOCATION_LOCAL_STORAGE:
892            /* Key is stored in the slot in export representation, so
893             * cycle through all known transparent accelerators */
894#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
895{% with nest_indent=12 %}
896{% include "OS-template-transparent.jinja" -%}
897{% endwith -%}
898#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
899
900            /* Fell through, meaning no accelerator supports this operation */
901            return( psa_import_key_into_slot( attributes,
902                                              data, data_length,
903                                              key_buffer, key_buffer_size,
904                                              key_buffer_length, bits ) );
905        /* Add cases for opaque driver here */
906#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
907{% with nest_indent=8 %}
908{% include "OS-template-opaque.jinja" -%}
909{% endwith -%}
910#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
911        default:
912            (void)status;
913            return( PSA_ERROR_INVALID_ARGUMENT );
914    }
915{% endwith %}
916}
917
918static inline psa_status_t psa_driver_wrapper_export_key(
919    const psa_key_attributes_t *attributes,
920    const uint8_t *key_buffer, size_t key_buffer_size,
921    uint8_t *data, size_t data_size, size_t *data_length )
922
923{
924{% with entry_point = "export_key" -%}
925{% macro entry_point_param(driver) -%}
926attributes,
927key_buffer,
928key_buffer_size,
929data,
930data_size,
931data_length
932{% endmacro %}
933    psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
934    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
935                                      psa_get_key_lifetime( attributes ) );
936
937    /* Try dynamically-registered SE interface first */
938#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
939    const psa_drv_se_t *drv;
940    psa_drv_se_context_t *drv_context;
941
942    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
943    {
944        if( ( drv->key_management == NULL   ) ||
945            ( drv->key_management->p_export == NULL ) )
946        {
947            return( PSA_ERROR_NOT_SUPPORTED );
948        }
949
950        return( drv->key_management->p_export(
951                     drv_context,
952                     *( (psa_key_slot_number_t *)key_buffer ),
953                     data, data_size, data_length ) );
954    }
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
956
957    switch( location )
958    {
959        case PSA_KEY_LOCATION_LOCAL_STORAGE:
960            return( psa_export_key_internal( attributes,
961                                             key_buffer,
962                                             key_buffer_size,
963                                             data,
964                                             data_size,
965                                             data_length ) );
966
967        /* Add cases for opaque driver here */
968#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
969{% with nest_indent=8 %}
970{% include "OS-template-opaque.jinja" -%}
971{% endwith -%}
972#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
973        default:
974            /* Key is declared with a lifetime not known to us */
975            return( status );
976    }
977{% endwith %}
978}
979
980static inline psa_status_t psa_driver_wrapper_copy_key(
981    psa_key_attributes_t *attributes,
982    const uint8_t *source_key, size_t source_key_length,
983    uint8_t *target_key_buffer, size_t target_key_buffer_size,
984    size_t *target_key_buffer_length )
985{
986{% with entry_point = "copy_key" -%}
987{% macro entry_point_param(driver) -%}
988attributes,
989source_key,
990source_key_length,
991target_key_buffer,
992target_key_buffer_size,
993target_key_buffer_length
994{% endmacro %}
995    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
996    psa_key_location_t location =
997        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
998
999#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1000    const psa_drv_se_t *drv;
1001    psa_drv_se_context_t *drv_context;
1002
1003    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
1004    {
1005        /* Copying to a secure element is not implemented yet. */
1006        return( PSA_ERROR_NOT_SUPPORTED );
1007    }
1008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1009
1010    switch( location )
1011    {
1012#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1013{% with nest_indent=8 %}
1014{% include "OS-template-opaque.jinja" -%}
1015{% endwith -%}
1016#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1017        default:
1018            (void)source_key;
1019            (void)source_key_length;
1020            (void)target_key_buffer;
1021            (void)target_key_buffer_size;
1022            (void)target_key_buffer_length;
1023            status = PSA_ERROR_INVALID_ARGUMENT;
1024    }
1025    return( status );
1026{% endwith %}
1027}
1028
1029/*
1030 * Cipher functions
1031 */
1032static inline psa_status_t psa_driver_wrapper_cipher_encrypt(
1033    const psa_key_attributes_t *attributes,
1034    const uint8_t *key_buffer,
1035    size_t key_buffer_size,
1036    psa_algorithm_t alg,
1037    const uint8_t *iv,
1038    size_t iv_length,
1039    const uint8_t *input,
1040    size_t input_length,
1041    uint8_t *output,
1042    size_t output_size,
1043    size_t *output_length )
1044{
1045    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1046    psa_key_location_t location =
1047        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1048
1049    switch( location )
1050    {
1051        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1052            /* Key is stored in the slot in export representation, so
1053             * cycle through all known transparent accelerators */
1054#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1055#if defined(PSA_CRYPTO_DRIVER_TEST)
1056            status = mbedtls_test_transparent_cipher_encrypt( attributes,
1057                                                              key_buffer,
1058                                                              key_buffer_size,
1059                                                              alg,
1060                                                              iv,
1061                                                              iv_length,
1062                                                              input,
1063                                                              input_length,
1064                                                              output,
1065                                                              output_size,
1066                                                              output_length );
1067            /* Declared with fallback == true */
1068            if( status != PSA_ERROR_NOT_SUPPORTED )
1069                return( status );
1070#endif /* PSA_CRYPTO_DRIVER_TEST */
1071#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1072
1073#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1074            return( mbedtls_psa_cipher_encrypt( attributes,
1075                                                key_buffer,
1076                                                key_buffer_size,
1077                                                alg,
1078                                                iv,
1079                                                iv_length,
1080                                                input,
1081                                                input_length,
1082                                                output,
1083                                                output_size,
1084                                                output_length ) );
1085#else
1086            return( PSA_ERROR_NOT_SUPPORTED );
1087#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1088
1089        /* Add cases for opaque driver here */
1090#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1091#if defined(PSA_CRYPTO_DRIVER_TEST)
1092        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
1093            return( mbedtls_test_opaque_cipher_encrypt( attributes,
1094                                                        key_buffer,
1095                                                        key_buffer_size,
1096                                                        alg,
1097                                                        iv,
1098                                                        iv_length,
1099                                                        input,
1100                                                        input_length,
1101                                                        output,
1102                                                        output_size,
1103                                                        output_length ) );
1104#endif /* PSA_CRYPTO_DRIVER_TEST */
1105#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1106
1107        default:
1108            /* Key is declared with a lifetime not known to us */
1109            (void)status;
1110            (void)key_buffer;
1111            (void)key_buffer_size;
1112            (void)alg;
1113            (void)iv;
1114            (void)iv_length;
1115            (void)input;
1116            (void)input_length;
1117            (void)output;
1118            (void)output_size;
1119            (void)output_length;
1120            return( PSA_ERROR_INVALID_ARGUMENT );
1121    }
1122}
1123
1124static inline psa_status_t psa_driver_wrapper_cipher_decrypt(
1125    const psa_key_attributes_t *attributes,
1126    const uint8_t *key_buffer,
1127    size_t key_buffer_size,
1128    psa_algorithm_t alg,
1129    const uint8_t *input,
1130    size_t input_length,
1131    uint8_t *output,
1132    size_t output_size,
1133    size_t *output_length )
1134{
1135    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1136    psa_key_location_t location =
1137        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1138
1139    switch( location )
1140    {
1141        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1142            /* Key is stored in the slot in export representation, so
1143             * cycle through all known transparent accelerators */
1144#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1145#if defined(PSA_CRYPTO_DRIVER_TEST)
1146            status = mbedtls_test_transparent_cipher_decrypt( attributes,
1147                                                              key_buffer,
1148                                                              key_buffer_size,
1149                                                              alg,
1150                                                              input,
1151                                                              input_length,
1152                                                              output,
1153                                                              output_size,
1154                                                              output_length );
1155            /* Declared with fallback == true */
1156            if( status != PSA_ERROR_NOT_SUPPORTED )
1157                return( status );
1158#endif /* PSA_CRYPTO_DRIVER_TEST */
1159#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1160
1161#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1162            return( mbedtls_psa_cipher_decrypt( attributes,
1163                                                key_buffer,
1164                                                key_buffer_size,
1165                                                alg,
1166                                                input,
1167                                                input_length,
1168                                                output,
1169                                                output_size,
1170                                                output_length ) );
1171#else
1172            return( PSA_ERROR_NOT_SUPPORTED );
1173#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1174
1175        /* Add cases for opaque driver here */
1176#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1177#if defined(PSA_CRYPTO_DRIVER_TEST)
1178        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
1179            return( mbedtls_test_opaque_cipher_decrypt( attributes,
1180                                                        key_buffer,
1181                                                        key_buffer_size,
1182                                                        alg,
1183                                                        input,
1184                                                        input_length,
1185                                                        output,
1186                                                        output_size,
1187                                                        output_length ) );
1188#endif /* PSA_CRYPTO_DRIVER_TEST */
1189#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1190
1191        default:
1192            /* Key is declared with a lifetime not known to us */
1193            (void)status;
1194            (void)key_buffer;
1195            (void)key_buffer_size;
1196            (void)alg;
1197            (void)input;
1198            (void)input_length;
1199            (void)output;
1200            (void)output_size;
1201            (void)output_length;
1202            return( PSA_ERROR_INVALID_ARGUMENT );
1203    }
1204}
1205
1206static inline psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
1207    psa_cipher_operation_t *operation,
1208    const psa_key_attributes_t *attributes,
1209    const uint8_t *key_buffer, size_t key_buffer_size,
1210    psa_algorithm_t alg )
1211{
1212    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1213    psa_key_location_t location =
1214        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1215
1216    switch( location )
1217    {
1218        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1219            /* Key is stored in the slot in export representation, so
1220             * cycle through all known transparent accelerators */
1221#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1222#if defined(PSA_CRYPTO_DRIVER_TEST)
1223            status = mbedtls_test_transparent_cipher_encrypt_setup(
1224                &operation->ctx.transparent_test_driver_ctx,
1225                attributes,
1226                key_buffer,
1227                key_buffer_size,
1228                alg );
1229            /* Declared with fallback == true */
1230            if( status == PSA_SUCCESS )
1231                operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1232
1233            if( status != PSA_ERROR_NOT_SUPPORTED )
1234                return( status );
1235#endif /* PSA_CRYPTO_DRIVER_TEST */
1236#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1237#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1238            /* Fell through, meaning no accelerator supports this operation */
1239            status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
1240                                                       attributes,
1241                                                       key_buffer,
1242                                                       key_buffer_size,
1243                                                       alg );
1244            if( status == PSA_SUCCESS )
1245                operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1246
1247            if( status != PSA_ERROR_NOT_SUPPORTED )
1248                return( status );
1249#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1250            return( PSA_ERROR_NOT_SUPPORTED );
1251
1252        /* Add cases for opaque driver here */
1253#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1254#if defined(PSA_CRYPTO_DRIVER_TEST)
1255        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
1256            status = mbedtls_test_opaque_cipher_encrypt_setup(
1257                &operation->ctx.opaque_test_driver_ctx,
1258                attributes,
1259                key_buffer, key_buffer_size,
1260                alg );
1261
1262            if( status == PSA_SUCCESS )
1263                operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
1264
1265            return( status );
1266#endif /* PSA_CRYPTO_DRIVER_TEST */
1267#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1268        default:
1269            /* Key is declared with a lifetime not known to us */
1270            (void)status;
1271            (void)operation;
1272            (void)key_buffer;
1273            (void)key_buffer_size;
1274            (void)alg;
1275            return( PSA_ERROR_INVALID_ARGUMENT );
1276    }
1277}
1278
1279static inline psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
1280    psa_cipher_operation_t *operation,
1281    const psa_key_attributes_t *attributes,
1282    const uint8_t *key_buffer, size_t key_buffer_size,
1283    psa_algorithm_t alg )
1284{
1285    psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
1286    psa_key_location_t location =
1287        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1288
1289    switch( location )
1290    {
1291        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1292            /* Key is stored in the slot in export representation, so
1293             * cycle through all known transparent accelerators */
1294#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1295#if defined(PSA_CRYPTO_DRIVER_TEST)
1296            status = mbedtls_test_transparent_cipher_decrypt_setup(
1297                &operation->ctx.transparent_test_driver_ctx,
1298                attributes,
1299                key_buffer,
1300                key_buffer_size,
1301                alg );
1302            /* Declared with fallback == true */
1303            if( status == PSA_SUCCESS )
1304                operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1305
1306            if( status != PSA_ERROR_NOT_SUPPORTED )
1307                return( status );
1308#endif /* PSA_CRYPTO_DRIVER_TEST */
1309#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1310#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1311            /* Fell through, meaning no accelerator supports this operation */
1312            status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
1313                                                       attributes,
1314                                                       key_buffer,
1315                                                       key_buffer_size,
1316                                                       alg );
1317            if( status == PSA_SUCCESS )
1318                operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1319
1320            return( status );
1321#else /* MBEDTLS_PSA_BUILTIN_CIPHER */
1322            return( PSA_ERROR_NOT_SUPPORTED );
1323#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1324
1325        /* Add cases for opaque driver here */
1326#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1327#if defined(PSA_CRYPTO_DRIVER_TEST)
1328        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
1329            status = mbedtls_test_opaque_cipher_decrypt_setup(
1330                         &operation->ctx.opaque_test_driver_ctx,
1331                         attributes,
1332                         key_buffer, key_buffer_size,
1333                         alg );
1334
1335            if( status == PSA_SUCCESS )
1336                operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
1337
1338            return( status );
1339#endif /* PSA_CRYPTO_DRIVER_TEST */
1340#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1341        default:
1342            /* Key is declared with a lifetime not known to us */
1343            (void)status;
1344            (void)operation;
1345            (void)key_buffer;
1346            (void)key_buffer_size;
1347            (void)alg;
1348            return( PSA_ERROR_INVALID_ARGUMENT );
1349    }
1350}
1351
1352static inline psa_status_t psa_driver_wrapper_cipher_set_iv(
1353    psa_cipher_operation_t *operation,
1354    const uint8_t *iv,
1355    size_t iv_length )
1356{
1357    switch( operation->id )
1358    {
1359#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1360        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1361            return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
1362                                               iv,
1363                                               iv_length ) );
1364#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1365
1366#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1367#if defined(PSA_CRYPTO_DRIVER_TEST)
1368        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1369            return( mbedtls_test_transparent_cipher_set_iv(
1370                        &operation->ctx.transparent_test_driver_ctx,
1371                        iv, iv_length ) );
1372
1373        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
1374            return( mbedtls_test_opaque_cipher_set_iv(
1375                        &operation->ctx.opaque_test_driver_ctx,
1376                        iv, iv_length ) );
1377#endif /* PSA_CRYPTO_DRIVER_TEST */
1378#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1379    }
1380
1381    (void)iv;
1382    (void)iv_length;
1383
1384    return( PSA_ERROR_INVALID_ARGUMENT );
1385}
1386
1387static inline psa_status_t psa_driver_wrapper_cipher_update(
1388    psa_cipher_operation_t *operation,
1389    const uint8_t *input,
1390    size_t input_length,
1391    uint8_t *output,
1392    size_t output_size,
1393    size_t *output_length )
1394{
1395    switch( operation->id )
1396    {
1397#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1398        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1399            return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
1400                                               input,
1401                                               input_length,
1402                                               output,
1403                                               output_size,
1404                                               output_length ) );
1405#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1406
1407#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1408#if defined(PSA_CRYPTO_DRIVER_TEST)
1409        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1410            return( mbedtls_test_transparent_cipher_update(
1411                        &operation->ctx.transparent_test_driver_ctx,
1412                        input, input_length,
1413                        output, output_size, output_length ) );
1414
1415        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
1416            return( mbedtls_test_opaque_cipher_update(
1417                        &operation->ctx.opaque_test_driver_ctx,
1418                        input, input_length,
1419                        output, output_size, output_length ) );
1420#endif /* PSA_CRYPTO_DRIVER_TEST */
1421#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1422    }
1423
1424    (void)input;
1425    (void)input_length;
1426    (void)output;
1427    (void)output_size;
1428    (void)output_length;
1429
1430    return( PSA_ERROR_INVALID_ARGUMENT );
1431}
1432
1433static inline psa_status_t psa_driver_wrapper_cipher_finish(
1434    psa_cipher_operation_t *operation,
1435    uint8_t *output,
1436    size_t output_size,
1437    size_t *output_length )
1438{
1439    switch( operation->id )
1440    {
1441#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1442        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1443            return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
1444                                               output,
1445                                               output_size,
1446                                               output_length ) );
1447#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1448
1449#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1450#if defined(PSA_CRYPTO_DRIVER_TEST)
1451        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1452            return( mbedtls_test_transparent_cipher_finish(
1453                        &operation->ctx.transparent_test_driver_ctx,
1454                        output, output_size, output_length ) );
1455
1456        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
1457            return( mbedtls_test_opaque_cipher_finish(
1458                        &operation->ctx.opaque_test_driver_ctx,
1459                        output, output_size, output_length ) );
1460#endif /* PSA_CRYPTO_DRIVER_TEST */
1461#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1462    }
1463
1464    (void)output;
1465    (void)output_size;
1466    (void)output_length;
1467
1468    return( PSA_ERROR_INVALID_ARGUMENT );
1469}
1470
1471static inline psa_status_t psa_driver_wrapper_cipher_abort(
1472    psa_cipher_operation_t *operation )
1473{
1474    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1475
1476    switch( operation->id )
1477    {
1478#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
1479        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1480            return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
1481#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1482
1483#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1484#if defined(PSA_CRYPTO_DRIVER_TEST)
1485        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1486            status = mbedtls_test_transparent_cipher_abort(
1487                         &operation->ctx.transparent_test_driver_ctx );
1488            mbedtls_platform_zeroize(
1489                &operation->ctx.transparent_test_driver_ctx,
1490                sizeof( operation->ctx.transparent_test_driver_ctx ) );
1491            return( status );
1492
1493        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
1494            status = mbedtls_test_opaque_cipher_abort(
1495                         &operation->ctx.opaque_test_driver_ctx );
1496            mbedtls_platform_zeroize(
1497                &operation->ctx.opaque_test_driver_ctx,
1498                sizeof( operation->ctx.opaque_test_driver_ctx ) );
1499            return( status );
1500#endif /* PSA_CRYPTO_DRIVER_TEST */
1501#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1502    }
1503
1504    (void)status;
1505    return( PSA_ERROR_INVALID_ARGUMENT );
1506}
1507
1508/*
1509 * Hashing functions
1510 */
1511static inline psa_status_t psa_driver_wrapper_hash_compute(
1512    psa_algorithm_t alg,
1513    const uint8_t *input,
1514    size_t input_length,
1515    uint8_t *hash,
1516    size_t hash_size,
1517    size_t *hash_length)
1518{
1519    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1520
1521    /* Try accelerators first */
1522#if defined(PSA_CRYPTO_DRIVER_TEST)
1523    status = mbedtls_test_transparent_hash_compute(
1524                alg, input, input_length, hash, hash_size, hash_length );
1525    if( status != PSA_ERROR_NOT_SUPPORTED )
1526        return( status );
1527#endif
1528
1529    /* If software fallback is compiled in, try fallback */
1530#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1531    status = mbedtls_psa_hash_compute( alg, input, input_length,
1532                                       hash, hash_size, hash_length );
1533    if( status != PSA_ERROR_NOT_SUPPORTED )
1534        return( status );
1535#endif
1536    (void) status;
1537    (void) alg;
1538    (void) input;
1539    (void) input_length;
1540    (void) hash;
1541    (void) hash_size;
1542    (void) hash_length;
1543
1544    return( PSA_ERROR_NOT_SUPPORTED );
1545}
1546
1547static inline psa_status_t psa_driver_wrapper_hash_setup(
1548    psa_hash_operation_t *operation,
1549    psa_algorithm_t alg )
1550{
1551    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1552
1553    /* Try setup on accelerators first */
1554#if defined(PSA_CRYPTO_DRIVER_TEST)
1555    status = mbedtls_test_transparent_hash_setup(
1556                &operation->ctx.test_driver_ctx, alg );
1557    if( status == PSA_SUCCESS )
1558        operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1559
1560    if( status != PSA_ERROR_NOT_SUPPORTED )
1561        return( status );
1562#endif
1563
1564    /* If software fallback is compiled in, try fallback */
1565#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1566    status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
1567    if( status == PSA_SUCCESS )
1568        operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1569
1570    if( status != PSA_ERROR_NOT_SUPPORTED )
1571        return( status );
1572#endif
1573    /* Nothing left to try if we fall through here */
1574    (void) status;
1575    (void) operation;
1576    (void) alg;
1577    return( PSA_ERROR_NOT_SUPPORTED );
1578}
1579
1580static inline psa_status_t psa_driver_wrapper_hash_clone(
1581    const psa_hash_operation_t *source_operation,
1582    psa_hash_operation_t *target_operation )
1583{
1584    switch( source_operation->id )
1585    {
1586#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1587        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1588            target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1589            return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1590                                            &target_operation->ctx.mbedtls_ctx ) );
1591#endif
1592#if defined(PSA_CRYPTO_DRIVER_TEST)
1593        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1594            target_operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1595            return( mbedtls_test_transparent_hash_clone(
1596                        &source_operation->ctx.test_driver_ctx,
1597                        &target_operation->ctx.test_driver_ctx ) );
1598#endif
1599        default:
1600            (void) target_operation;
1601            return( PSA_ERROR_BAD_STATE );
1602    }
1603}
1604
1605static inline psa_status_t psa_driver_wrapper_hash_update(
1606    psa_hash_operation_t *operation,
1607    const uint8_t *input,
1608    size_t input_length )
1609{
1610    switch( operation->id )
1611    {
1612#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1613        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1614            return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1615                                             input, input_length ) );
1616#endif
1617#if defined(PSA_CRYPTO_DRIVER_TEST)
1618        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1619            return( mbedtls_test_transparent_hash_update(
1620                        &operation->ctx.test_driver_ctx,
1621                        input, input_length ) );
1622#endif
1623        default:
1624            (void) input;
1625            (void) input_length;
1626            return( PSA_ERROR_BAD_STATE );
1627    }
1628}
1629
1630static inline psa_status_t psa_driver_wrapper_hash_finish(
1631    psa_hash_operation_t *operation,
1632    uint8_t *hash,
1633    size_t hash_size,
1634    size_t *hash_length )
1635{
1636    switch( operation->id )
1637    {
1638#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1639        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1640            return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1641                                             hash, hash_size, hash_length ) );
1642#endif
1643#if defined(PSA_CRYPTO_DRIVER_TEST)
1644        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1645            return( mbedtls_test_transparent_hash_finish(
1646                        &operation->ctx.test_driver_ctx,
1647                        hash, hash_size, hash_length ) );
1648#endif
1649        default:
1650            (void) hash;
1651            (void) hash_size;
1652            (void) hash_length;
1653            return( PSA_ERROR_BAD_STATE );
1654    }
1655}
1656
1657static inline psa_status_t psa_driver_wrapper_hash_abort(
1658    psa_hash_operation_t *operation )
1659{
1660    switch( operation->id )
1661    {
1662#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1663        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1664            return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1665#endif
1666#if defined(PSA_CRYPTO_DRIVER_TEST)
1667        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1668            return( mbedtls_test_transparent_hash_abort(
1669                        &operation->ctx.test_driver_ctx ) );
1670#endif
1671        default:
1672            return( PSA_ERROR_BAD_STATE );
1673    }
1674}
1675
1676static inline psa_status_t psa_driver_wrapper_aead_encrypt(
1677    const psa_key_attributes_t *attributes,
1678    const uint8_t *key_buffer, size_t key_buffer_size,
1679    psa_algorithm_t alg,
1680    const uint8_t *nonce, size_t nonce_length,
1681    const uint8_t *additional_data, size_t additional_data_length,
1682    const uint8_t *plaintext, size_t plaintext_length,
1683    uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1684{
1685    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1686    psa_key_location_t location =
1687        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1688
1689    switch( location )
1690    {
1691        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1692            /* Key is stored in the slot in export representation, so
1693             * cycle through all known transparent accelerators */
1694
1695#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1696#if defined(PSA_CRYPTO_DRIVER_TEST)
1697            status = mbedtls_test_transparent_aead_encrypt(
1698                         attributes, key_buffer, key_buffer_size,
1699                         alg,
1700                         nonce, nonce_length,
1701                         additional_data, additional_data_length,
1702                         plaintext, plaintext_length,
1703                         ciphertext, ciphertext_size, ciphertext_length );
1704            /* Declared with fallback == true */
1705            if( status != PSA_ERROR_NOT_SUPPORTED )
1706                return( status );
1707#endif /* PSA_CRYPTO_DRIVER_TEST */
1708#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1709
1710            /* Fell through, meaning no accelerator supports this operation */
1711            return( mbedtls_psa_aead_encrypt(
1712                        attributes, key_buffer, key_buffer_size,
1713                        alg,
1714                        nonce, nonce_length,
1715                        additional_data, additional_data_length,
1716                        plaintext, plaintext_length,
1717                        ciphertext, ciphertext_size, ciphertext_length ) );
1718
1719        /* Add cases for opaque driver here */
1720
1721        default:
1722            /* Key is declared with a lifetime not known to us */
1723            (void)status;
1724            return( PSA_ERROR_INVALID_ARGUMENT );
1725    }
1726}
1727
1728static inline psa_status_t psa_driver_wrapper_aead_decrypt(
1729    const psa_key_attributes_t *attributes,
1730    const uint8_t *key_buffer, size_t key_buffer_size,
1731    psa_algorithm_t alg,
1732    const uint8_t *nonce, size_t nonce_length,
1733    const uint8_t *additional_data, size_t additional_data_length,
1734    const uint8_t *ciphertext, size_t ciphertext_length,
1735    uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1736{
1737    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1738    psa_key_location_t location =
1739        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1740
1741    switch( location )
1742    {
1743        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1744            /* Key is stored in the slot in export representation, so
1745             * cycle through all known transparent accelerators */
1746
1747#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1748#if defined(PSA_CRYPTO_DRIVER_TEST)
1749            status = mbedtls_test_transparent_aead_decrypt(
1750                        attributes, key_buffer, key_buffer_size,
1751                        alg,
1752                        nonce, nonce_length,
1753                        additional_data, additional_data_length,
1754                        ciphertext, ciphertext_length,
1755                        plaintext, plaintext_size, plaintext_length );
1756            /* Declared with fallback == true */
1757            if( status != PSA_ERROR_NOT_SUPPORTED )
1758                return( status );
1759#endif /* PSA_CRYPTO_DRIVER_TEST */
1760#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1761
1762            /* Fell through, meaning no accelerator supports this operation */
1763            return( mbedtls_psa_aead_decrypt(
1764                        attributes, key_buffer, key_buffer_size,
1765                        alg,
1766                        nonce, nonce_length,
1767                        additional_data, additional_data_length,
1768                        ciphertext, ciphertext_length,
1769                        plaintext, plaintext_size, plaintext_length ) );
1770
1771        /* Add cases for opaque driver here */
1772
1773        default:
1774            /* Key is declared with a lifetime not known to us */
1775            (void)status;
1776            return( PSA_ERROR_INVALID_ARGUMENT );
1777    }
1778}
1779
1780static inline psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1781   psa_aead_operation_t *operation,
1782   const psa_key_attributes_t *attributes,
1783   const uint8_t *key_buffer, size_t key_buffer_size,
1784   psa_algorithm_t alg )
1785{
1786    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1787    psa_key_location_t location =
1788        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1789
1790    switch( location )
1791    {
1792        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1793            /* Key is stored in the slot in export representation, so
1794             * cycle through all known transparent accelerators */
1795
1796#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1797#if defined(PSA_CRYPTO_DRIVER_TEST)
1798            operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1799            status = mbedtls_test_transparent_aead_encrypt_setup(
1800                        &operation->ctx.transparent_test_driver_ctx,
1801                        attributes, key_buffer, key_buffer_size,
1802                        alg );
1803
1804            /* Declared with fallback == true */
1805            if( status != PSA_ERROR_NOT_SUPPORTED )
1806                return( status );
1807#endif /* PSA_CRYPTO_DRIVER_TEST */
1808#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1809
1810            /* Fell through, meaning no accelerator supports this operation */
1811            operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1812            status = mbedtls_psa_aead_encrypt_setup(
1813                        &operation->ctx.mbedtls_ctx, attributes,
1814                        key_buffer, key_buffer_size,
1815                        alg );
1816
1817            return( status );
1818
1819        /* Add cases for opaque driver here */
1820
1821        default:
1822            /* Key is declared with a lifetime not known to us */
1823            (void)status;
1824            return( PSA_ERROR_INVALID_ARGUMENT );
1825    }
1826}
1827
1828static inline psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1829   psa_aead_operation_t *operation,
1830   const psa_key_attributes_t *attributes,
1831   const uint8_t *key_buffer, size_t key_buffer_size,
1832   psa_algorithm_t alg )
1833{
1834    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1835    psa_key_location_t location =
1836        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
1837
1838    switch( location )
1839    {
1840        case PSA_KEY_LOCATION_LOCAL_STORAGE:
1841            /* Key is stored in the slot in export representation, so
1842             * cycle through all known transparent accelerators */
1843
1844#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1845#if defined(PSA_CRYPTO_DRIVER_TEST)
1846            operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
1847            status = mbedtls_test_transparent_aead_decrypt_setup(
1848                        &operation->ctx.transparent_test_driver_ctx,
1849                        attributes,
1850                        key_buffer, key_buffer_size,
1851                        alg );
1852
1853            /* Declared with fallback == true */
1854            if( status != PSA_ERROR_NOT_SUPPORTED )
1855                return( status );
1856#endif /* PSA_CRYPTO_DRIVER_TEST */
1857#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1858
1859            /* Fell through, meaning no accelerator supports this operation */
1860            operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1861            status = mbedtls_psa_aead_decrypt_setup(
1862                        &operation->ctx.mbedtls_ctx,
1863                        attributes,
1864                        key_buffer, key_buffer_size,
1865                        alg );
1866
1867            return( status );
1868
1869        /* Add cases for opaque driver here */
1870
1871        default:
1872            /* Key is declared with a lifetime not known to us */
1873            (void)status;
1874            return( PSA_ERROR_INVALID_ARGUMENT );
1875    }
1876}
1877
1878static inline psa_status_t psa_driver_wrapper_aead_set_nonce(
1879   psa_aead_operation_t *operation,
1880   const uint8_t *nonce,
1881   size_t nonce_length )
1882{
1883    switch( operation->id )
1884    {
1885#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
1886        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1887            return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1888                                                nonce,
1889                                                nonce_length ) );
1890
1891#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
1892
1893#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1894#if defined(PSA_CRYPTO_DRIVER_TEST)
1895        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1896            return( mbedtls_test_transparent_aead_set_nonce(
1897                         &operation->ctx.transparent_test_driver_ctx,
1898                         nonce, nonce_length ) );
1899
1900        /* Add cases for opaque driver here */
1901
1902#endif /* PSA_CRYPTO_DRIVER_TEST */
1903#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1904    }
1905
1906    (void)nonce;
1907    (void)nonce_length;
1908
1909    return( PSA_ERROR_INVALID_ARGUMENT );
1910}
1911
1912static inline psa_status_t psa_driver_wrapper_aead_set_lengths(
1913   psa_aead_operation_t *operation,
1914   size_t ad_length,
1915   size_t plaintext_length )
1916{
1917    switch( operation->id )
1918    {
1919#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
1920        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1921            return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1922                                                  ad_length,
1923                                                  plaintext_length ) );
1924
1925#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
1926
1927#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1928#if defined(PSA_CRYPTO_DRIVER_TEST)
1929        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1930            return( mbedtls_test_transparent_aead_set_lengths(
1931                        &operation->ctx.transparent_test_driver_ctx,
1932                        ad_length, plaintext_length ) );
1933
1934        /* Add cases for opaque driver here */
1935
1936#endif /* PSA_CRYPTO_DRIVER_TEST */
1937#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1938    }
1939
1940    (void)ad_length;
1941    (void)plaintext_length;
1942
1943    return( PSA_ERROR_INVALID_ARGUMENT );
1944}
1945
1946static inline psa_status_t psa_driver_wrapper_aead_update_ad(
1947   psa_aead_operation_t *operation,
1948   const uint8_t *input,
1949   size_t input_length )
1950{
1951    switch( operation->id )
1952    {
1953#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
1954        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1955            return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1956                                                input,
1957                                                input_length ) );
1958
1959#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
1960
1961#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1962#if defined(PSA_CRYPTO_DRIVER_TEST)
1963        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
1964            return( mbedtls_test_transparent_aead_update_ad(
1965                        &operation->ctx.transparent_test_driver_ctx,
1966                        input, input_length ) );
1967
1968        /* Add cases for opaque driver here */
1969
1970#endif /* PSA_CRYPTO_DRIVER_TEST */
1971#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1972    }
1973
1974    (void)input;
1975    (void)input_length;
1976
1977    return( PSA_ERROR_INVALID_ARGUMENT );
1978}
1979
1980static inline psa_status_t psa_driver_wrapper_aead_update(
1981   psa_aead_operation_t *operation,
1982   const uint8_t *input,
1983   size_t input_length,
1984   uint8_t *output,
1985   size_t output_size,
1986   size_t *output_length )
1987{
1988    switch( operation->id )
1989    {
1990#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
1991        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1992            return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1993                                             input, input_length,
1994                                             output, output_size,
1995                                             output_length ) );
1996
1997#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
1998
1999#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2000#if defined(PSA_CRYPTO_DRIVER_TEST)
2001        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2002            return( mbedtls_test_transparent_aead_update(
2003                        &operation->ctx.transparent_test_driver_ctx,
2004                        input, input_length, output, output_size,
2005                        output_length ) );
2006
2007        /* Add cases for opaque driver here */
2008
2009#endif /* PSA_CRYPTO_DRIVER_TEST */
2010#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2011    }
2012
2013    (void)input;
2014    (void)input_length;
2015    (void)output;
2016    (void)output_size;
2017    (void)output_length;
2018
2019    return( PSA_ERROR_INVALID_ARGUMENT );
2020}
2021
2022static inline psa_status_t psa_driver_wrapper_aead_finish(
2023   psa_aead_operation_t *operation,
2024   uint8_t *ciphertext,
2025   size_t ciphertext_size,
2026   size_t *ciphertext_length,
2027   uint8_t *tag,
2028   size_t tag_size,
2029   size_t *tag_length )
2030{
2031    switch( operation->id )
2032    {
2033#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
2034        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2035            return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
2036                                             ciphertext,
2037                                             ciphertext_size,
2038                                             ciphertext_length, tag,
2039                                             tag_size, tag_length ) );
2040
2041#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
2042
2043#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2044#if defined(PSA_CRYPTO_DRIVER_TEST)
2045        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2046            return( mbedtls_test_transparent_aead_finish(
2047                        &operation->ctx.transparent_test_driver_ctx,
2048                        ciphertext, ciphertext_size,
2049                        ciphertext_length, tag, tag_size, tag_length ) );
2050
2051        /* Add cases for opaque driver here */
2052
2053#endif /* PSA_CRYPTO_DRIVER_TEST */
2054#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2055    }
2056
2057    (void)ciphertext;
2058    (void)ciphertext_size;
2059    (void)ciphertext_length;
2060    (void)tag;
2061    (void)tag_size;
2062    (void)tag_length;
2063
2064    return( PSA_ERROR_INVALID_ARGUMENT );
2065}
2066
2067static inline psa_status_t psa_driver_wrapper_aead_verify(
2068   psa_aead_operation_t *operation,
2069   uint8_t *plaintext,
2070   size_t plaintext_size,
2071   size_t *plaintext_length,
2072   const uint8_t *tag,
2073   size_t tag_length )
2074{
2075    switch( operation->id )
2076    {
2077#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
2078        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2079            {
2080                psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2081                uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
2082                size_t check_tag_length;
2083
2084                status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
2085                                                  plaintext,
2086                                                  plaintext_size,
2087                                                  plaintext_length,
2088                                                  check_tag,
2089                                                  sizeof( check_tag ),
2090                                                  &check_tag_length );
2091
2092                if( status == PSA_SUCCESS )
2093                {
2094                    if( tag_length != check_tag_length ||
2095                        mbedtls_ct_memcmp( tag, check_tag, tag_length )
2096                        != 0 )
2097                        status = PSA_ERROR_INVALID_SIGNATURE;
2098                }
2099
2100                mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
2101
2102                return( status );
2103            }
2104
2105#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
2106
2107#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2108#if defined(PSA_CRYPTO_DRIVER_TEST)
2109        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2110            return( mbedtls_test_transparent_aead_verify(
2111                        &operation->ctx.transparent_test_driver_ctx,
2112                        plaintext, plaintext_size,
2113                        plaintext_length, tag, tag_length ) );
2114
2115        /* Add cases for opaque driver here */
2116
2117#endif /* PSA_CRYPTO_DRIVER_TEST */
2118#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2119    }
2120
2121    (void)plaintext;
2122    (void)plaintext_size;
2123    (void)plaintext_length;
2124    (void)tag;
2125    (void)tag_length;
2126
2127    return( PSA_ERROR_INVALID_ARGUMENT );
2128}
2129
2130static inline psa_status_t psa_driver_wrapper_aead_abort(
2131   psa_aead_operation_t *operation )
2132{
2133    switch( operation->id )
2134    {
2135#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
2136        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2137            return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
2138
2139#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
2140
2141#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2142#if defined(PSA_CRYPTO_DRIVER_TEST)
2143        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2144            return( mbedtls_test_transparent_aead_abort(
2145               &operation->ctx.transparent_test_driver_ctx ) );
2146
2147        /* Add cases for opaque driver here */
2148
2149#endif /* PSA_CRYPTO_DRIVER_TEST */
2150#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2151    }
2152
2153    return( PSA_ERROR_INVALID_ARGUMENT );
2154}
2155
2156/*
2157 * MAC functions
2158 */
2159static inline psa_status_t psa_driver_wrapper_mac_compute(
2160    const psa_key_attributes_t *attributes,
2161    const uint8_t *key_buffer,
2162    size_t key_buffer_size,
2163    psa_algorithm_t alg,
2164    const uint8_t *input,
2165    size_t input_length,
2166    uint8_t *mac,
2167    size_t mac_size,
2168    size_t *mac_length )
2169{
2170    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2171    psa_key_location_t location =
2172        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2173
2174    switch( location )
2175    {
2176        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2177            /* Key is stored in the slot in export representation, so
2178             * cycle through all known transparent accelerators */
2179#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2180#if defined(PSA_CRYPTO_DRIVER_TEST)
2181            status = mbedtls_test_transparent_mac_compute(
2182                attributes, key_buffer, key_buffer_size, alg,
2183                input, input_length,
2184                mac, mac_size, mac_length );
2185            /* Declared with fallback == true */
2186            if( status != PSA_ERROR_NOT_SUPPORTED )
2187                return( status );
2188#endif /* PSA_CRYPTO_DRIVER_TEST */
2189#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2190#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2191            /* Fell through, meaning no accelerator supports this operation */
2192            status = mbedtls_psa_mac_compute(
2193                attributes, key_buffer, key_buffer_size, alg,
2194                input, input_length,
2195                mac, mac_size, mac_length );
2196            if( status != PSA_ERROR_NOT_SUPPORTED )
2197                return( status );
2198#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2199            return( PSA_ERROR_NOT_SUPPORTED );
2200
2201        /* Add cases for opaque driver here */
2202#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2203#if defined(PSA_CRYPTO_DRIVER_TEST)
2204        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2205            status = mbedtls_test_opaque_mac_compute(
2206                attributes, key_buffer, key_buffer_size, alg,
2207                input, input_length,
2208                mac, mac_size, mac_length );
2209            return( status );
2210#endif /* PSA_CRYPTO_DRIVER_TEST */
2211#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2212        default:
2213            /* Key is declared with a lifetime not known to us */
2214            (void) key_buffer;
2215            (void) key_buffer_size;
2216            (void) alg;
2217            (void) input;
2218            (void) input_length;
2219            (void) mac;
2220            (void) mac_size;
2221            (void) mac_length;
2222            (void) status;
2223            return( PSA_ERROR_INVALID_ARGUMENT );
2224    }
2225}
2226
2227static inline psa_status_t psa_driver_wrapper_mac_sign_setup(
2228    psa_mac_operation_t *operation,
2229    const psa_key_attributes_t *attributes,
2230    const uint8_t *key_buffer,
2231    size_t key_buffer_size,
2232    psa_algorithm_t alg )
2233{
2234    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2235    psa_key_location_t location =
2236        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2237
2238    switch( location )
2239    {
2240        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2241            /* Key is stored in the slot in export representation, so
2242             * cycle through all known transparent accelerators */
2243#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2244#if defined(PSA_CRYPTO_DRIVER_TEST)
2245            status = mbedtls_test_transparent_mac_sign_setup(
2246                &operation->ctx.transparent_test_driver_ctx,
2247                attributes,
2248                key_buffer, key_buffer_size,
2249                alg );
2250            /* Declared with fallback == true */
2251            if( status == PSA_SUCCESS )
2252                operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
2253
2254            if( status != PSA_ERROR_NOT_SUPPORTED )
2255                return( status );
2256#endif /* PSA_CRYPTO_DRIVER_TEST */
2257#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2258#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2259            /* Fell through, meaning no accelerator supports this operation */
2260            status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
2261                                                 attributes,
2262                                                 key_buffer, key_buffer_size,
2263                                                 alg );
2264            if( status == PSA_SUCCESS )
2265                operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2266
2267            if( status != PSA_ERROR_NOT_SUPPORTED )
2268                return( status );
2269#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2270            return( PSA_ERROR_NOT_SUPPORTED );
2271
2272        /* Add cases for opaque driver here */
2273#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2274#if defined(PSA_CRYPTO_DRIVER_TEST)
2275        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2276            status = mbedtls_test_opaque_mac_sign_setup(
2277                &operation->ctx.opaque_test_driver_ctx,
2278                attributes,
2279                key_buffer, key_buffer_size,
2280                alg );
2281
2282            if( status == PSA_SUCCESS )
2283                operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
2284
2285            return( status );
2286#endif /* PSA_CRYPTO_DRIVER_TEST */
2287#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2288        default:
2289            /* Key is declared with a lifetime not known to us */
2290            (void) status;
2291            (void) operation;
2292            (void) key_buffer;
2293            (void) key_buffer_size;
2294            (void) alg;
2295            return( PSA_ERROR_INVALID_ARGUMENT );
2296    }
2297}
2298
2299static inline psa_status_t psa_driver_wrapper_mac_verify_setup(
2300    psa_mac_operation_t *operation,
2301    const psa_key_attributes_t *attributes,
2302    const uint8_t *key_buffer,
2303    size_t key_buffer_size,
2304    psa_algorithm_t alg )
2305{
2306    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2307    psa_key_location_t location =
2308        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2309
2310    switch( location )
2311    {
2312        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2313            /* Key is stored in the slot in export representation, so
2314             * cycle through all known transparent accelerators */
2315#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2316#if defined(PSA_CRYPTO_DRIVER_TEST)
2317            status = mbedtls_test_transparent_mac_verify_setup(
2318                &operation->ctx.transparent_test_driver_ctx,
2319                attributes,
2320                key_buffer, key_buffer_size,
2321                alg );
2322            /* Declared with fallback == true */
2323            if( status == PSA_SUCCESS )
2324                operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
2325
2326            if( status != PSA_ERROR_NOT_SUPPORTED )
2327                return( status );
2328#endif /* PSA_CRYPTO_DRIVER_TEST */
2329#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2330#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2331            /* Fell through, meaning no accelerator supports this operation */
2332            status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
2333                                                   attributes,
2334                                                   key_buffer, key_buffer_size,
2335                                                   alg );
2336            if( status == PSA_SUCCESS )
2337                operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2338
2339            if( status != PSA_ERROR_NOT_SUPPORTED )
2340                return( status );
2341#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2342            return( PSA_ERROR_NOT_SUPPORTED );
2343
2344        /* Add cases for opaque driver here */
2345#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2346#if defined(PSA_CRYPTO_DRIVER_TEST)
2347        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2348            status = mbedtls_test_opaque_mac_verify_setup(
2349                &operation->ctx.opaque_test_driver_ctx,
2350                attributes,
2351                key_buffer, key_buffer_size,
2352                alg );
2353
2354            if( status == PSA_SUCCESS )
2355                operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
2356
2357            return( status );
2358#endif /* PSA_CRYPTO_DRIVER_TEST */
2359#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2360        default:
2361            /* Key is declared with a lifetime not known to us */
2362            (void) status;
2363            (void) operation;
2364            (void) key_buffer;
2365            (void) key_buffer_size;
2366            (void) alg;
2367            return( PSA_ERROR_INVALID_ARGUMENT );
2368    }
2369}
2370
2371static inline psa_status_t psa_driver_wrapper_mac_update(
2372    psa_mac_operation_t *operation,
2373    const uint8_t *input,
2374    size_t input_length )
2375{
2376    switch( operation->id )
2377    {
2378#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2379        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2380            return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
2381                                            input, input_length ) );
2382#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2383
2384#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2385#if defined(PSA_CRYPTO_DRIVER_TEST)
2386        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2387            return( mbedtls_test_transparent_mac_update(
2388                        &operation->ctx.transparent_test_driver_ctx,
2389                        input, input_length ) );
2390
2391        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
2392            return( mbedtls_test_opaque_mac_update(
2393                        &operation->ctx.opaque_test_driver_ctx,
2394                        input, input_length ) );
2395#endif /* PSA_CRYPTO_DRIVER_TEST */
2396#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2397        default:
2398            (void) input;
2399            (void) input_length;
2400            return( PSA_ERROR_INVALID_ARGUMENT );
2401    }
2402}
2403
2404static inline psa_status_t psa_driver_wrapper_mac_sign_finish(
2405    psa_mac_operation_t *operation,
2406    uint8_t *mac,
2407    size_t mac_size,
2408    size_t *mac_length )
2409{
2410    switch( operation->id )
2411    {
2412#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2413        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2414            return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
2415                                                 mac, mac_size, mac_length ) );
2416#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2417
2418#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2419#if defined(PSA_CRYPTO_DRIVER_TEST)
2420        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2421            return( mbedtls_test_transparent_mac_sign_finish(
2422                        &operation->ctx.transparent_test_driver_ctx,
2423                        mac, mac_size, mac_length ) );
2424
2425        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
2426            return( mbedtls_test_opaque_mac_sign_finish(
2427                        &operation->ctx.opaque_test_driver_ctx,
2428                        mac, mac_size, mac_length ) );
2429#endif /* PSA_CRYPTO_DRIVER_TEST */
2430#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2431        default:
2432            (void) mac;
2433            (void) mac_size;
2434            (void) mac_length;
2435            return( PSA_ERROR_INVALID_ARGUMENT );
2436    }
2437}
2438
2439static inline psa_status_t psa_driver_wrapper_mac_verify_finish(
2440    psa_mac_operation_t *operation,
2441    const uint8_t *mac,
2442    size_t mac_length )
2443{
2444    switch( operation->id )
2445    {
2446#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2447        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2448            return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
2449                                                   mac, mac_length ) );
2450#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2451
2452#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2453#if defined(PSA_CRYPTO_DRIVER_TEST)
2454        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2455            return( mbedtls_test_transparent_mac_verify_finish(
2456                        &operation->ctx.transparent_test_driver_ctx,
2457                        mac, mac_length ) );
2458
2459        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
2460            return( mbedtls_test_opaque_mac_verify_finish(
2461                        &operation->ctx.opaque_test_driver_ctx,
2462                        mac, mac_length ) );
2463#endif /* PSA_CRYPTO_DRIVER_TEST */
2464#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2465        default:
2466            (void) mac;
2467            (void) mac_length;
2468            return( PSA_ERROR_INVALID_ARGUMENT );
2469    }
2470}
2471
2472static inline psa_status_t psa_driver_wrapper_mac_abort(
2473    psa_mac_operation_t *operation )
2474{
2475    switch( operation->id )
2476    {
2477#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2478        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2479            return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
2480#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2481
2482#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2483#if defined(PSA_CRYPTO_DRIVER_TEST)
2484        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2485            return( mbedtls_test_transparent_mac_abort(
2486                        &operation->ctx.transparent_test_driver_ctx ) );
2487        case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
2488            return( mbedtls_test_opaque_mac_abort(
2489                        &operation->ctx.opaque_test_driver_ctx ) );
2490#endif /* PSA_CRYPTO_DRIVER_TEST */
2491#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2492        default:
2493            return( PSA_ERROR_INVALID_ARGUMENT );
2494    }
2495}
2496
2497/*
2498 * Asymmetric cryptography
2499 */
2500static inline psa_status_t psa_driver_wrapper_asymmetric_encrypt(
2501    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
2502    size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
2503    size_t input_length, const uint8_t *salt, size_t salt_length,
2504    uint8_t *output, size_t output_size, size_t *output_length )
2505{
2506    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2507    psa_key_location_t location =
2508        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2509
2510    switch( location )
2511    {
2512        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2513            /* Key is stored in the slot in export representation, so
2514             * cycle through all known transparent accelerators */
2515#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2516#if defined(PSA_CRYPTO_DRIVER_TEST)
2517            status = mbedtls_test_transparent_asymmetric_encrypt( attributes,
2518                        key_buffer, key_buffer_size, alg, input, input_length,
2519                        salt, salt_length, output, output_size,
2520                        output_length );
2521            /* Declared with fallback == true */
2522            if( status != PSA_ERROR_NOT_SUPPORTED )
2523                return( status );
2524#endif /* PSA_CRYPTO_DRIVER_TEST */
2525#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2526            return( mbedtls_psa_asymmetric_encrypt( attributes,
2527                        key_buffer, key_buffer_size, alg, input, input_length,
2528                        salt, salt_length, output, output_size, output_length )
2529                  );
2530        /* Add cases for opaque driver here */
2531#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2532#if defined(PSA_CRYPTO_DRIVER_TEST)
2533        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2534            return( mbedtls_test_opaque_asymmetric_encrypt( attributes,
2535                        key_buffer, key_buffer_size, alg, input, input_length,
2536                        salt, salt_length, output, output_size, output_length )
2537                  );
2538#endif /* PSA_CRYPTO_DRIVER_TEST */
2539#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2540
2541        default:
2542            /* Key is declared with a lifetime not known to us */
2543            (void)status;
2544            (void)key_buffer;
2545            (void)key_buffer_size;
2546            (void)alg;
2547            (void)input;
2548            (void)input_length;
2549            (void)salt;
2550            (void)salt_length;
2551            (void)output;
2552            (void)output_size;
2553            (void)output_length;
2554            return( PSA_ERROR_INVALID_ARGUMENT );
2555    }
2556}
2557
2558static inline psa_status_t psa_driver_wrapper_asymmetric_decrypt(
2559    const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
2560    size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
2561    size_t input_length, const uint8_t *salt, size_t salt_length,
2562    uint8_t *output, size_t output_size, size_t *output_length )
2563{
2564    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2565    psa_key_location_t location =
2566        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2567
2568    switch( location )
2569    {
2570        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2571            /* Key is stored in the slot in export representation, so
2572             * cycle through all known transparent accelerators */
2573#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2574#if defined(PSA_CRYPTO_DRIVER_TEST)
2575            status = mbedtls_test_transparent_asymmetric_decrypt( attributes,
2576                        key_buffer, key_buffer_size, alg, input, input_length,
2577                        salt, salt_length, output, output_size,
2578                        output_length );
2579            /* Declared with fallback == true */
2580            if( status != PSA_ERROR_NOT_SUPPORTED )
2581                return( status );
2582#endif /* PSA_CRYPTO_DRIVER_TEST */
2583#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2584            return( mbedtls_psa_asymmetric_decrypt( attributes,
2585                        key_buffer, key_buffer_size, alg,input, input_length,
2586                        salt, salt_length, output, output_size,
2587                        output_length ) );
2588        /* Add cases for opaque driver here */
2589#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2590#if defined(PSA_CRYPTO_DRIVER_TEST)
2591        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2592            return( mbedtls_test_opaque_asymmetric_decrypt( attributes,
2593                        key_buffer, key_buffer_size, alg, input, input_length,
2594                        salt, salt_length, output, output_size,
2595                        output_length ) );
2596#endif /* PSA_CRYPTO_DRIVER_TEST */
2597#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2598
2599        default:
2600            /* Key is declared with a lifetime not known to us */
2601            (void)status;
2602            (void)key_buffer;
2603            (void)key_buffer_size;
2604            (void)alg;
2605            (void)input;
2606            (void)input_length;
2607            (void)salt;
2608            (void)salt_length;
2609            (void)output;
2610            (void)output_size;
2611            (void)output_length;
2612            return( PSA_ERROR_INVALID_ARGUMENT );
2613    }
2614}
2615
2616static inline psa_status_t psa_driver_wrapper_key_agreement(
2617    const psa_key_attributes_t *attributes,
2618    const uint8_t *key_buffer,
2619    size_t key_buffer_size,
2620    psa_algorithm_t alg,
2621    const uint8_t *peer_key,
2622    size_t peer_key_length,
2623    uint8_t *shared_secret,
2624    size_t shared_secret_size,
2625    size_t *shared_secret_length
2626 )
2627{
2628    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2629    psa_key_location_t location =
2630        PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
2631
2632    switch( location )
2633    {
2634        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2635            /* Key is stored in the slot in export representation, so
2636             * cycle through all known transparent accelerators */
2637#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2638#if defined(PSA_CRYPTO_DRIVER_TEST)
2639            status =
2640                mbedtls_test_transparent_key_agreement( attributes,
2641                        key_buffer, key_buffer_size, alg, peer_key,
2642                        peer_key_length, shared_secret, shared_secret_size,
2643                        shared_secret_length );
2644            if( status != PSA_ERROR_NOT_SUPPORTED )
2645                return( status );
2646#endif /* PSA_CRYPTO_DRIVER_TEST */
2647#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
2648            if( PSA_KEY_TYPE_IS_ECC( psa_get_key_type(attributes) ) &&
2649                PSA_ALG_IS_ECDH(alg) &&
2650                PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(attributes)) == PSA_ECC_FAMILY_SECP_R1 &&
2651                psa_get_key_bits(attributes) == 256 )
2652            {
2653                status = p256_transparent_key_agreement( attributes,
2654                                                         key_buffer,
2655                                                         key_buffer_size,
2656                                                         alg,
2657                                                         peer_key,
2658                                                         peer_key_length,
2659                                                         shared_secret,
2660                                                         shared_secret_size,
2661                                                         shared_secret_length );
2662                if( status != PSA_ERROR_NOT_SUPPORTED)
2663                    return( status );
2664            }
2665#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
2666#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2667
2668            /* Software Fallback */
2669            status = psa_key_agreement_raw_builtin( attributes,
2670                                                    key_buffer,
2671                                                    key_buffer_size,
2672                                                    alg,
2673                                                    peer_key,
2674                                                    peer_key_length,
2675                                                    shared_secret,
2676                                                    shared_secret_size,
2677                                                    shared_secret_length );
2678            return( status );
2679#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2680#if defined(PSA_CRYPTO_DRIVER_TEST)
2681        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
2682            return( mbedtls_test_opaque_key_agreement( attributes,
2683                        key_buffer, key_buffer_size, alg, peer_key,
2684                        peer_key_length, shared_secret, shared_secret_size,
2685                        shared_secret_length ) );
2686#endif /* PSA_CRYPTO_DRIVER_TEST */
2687#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2688
2689        default:
2690            (void) attributes;
2691            (void) key_buffer;
2692            (void) key_buffer_size;
2693            (void) peer_key;
2694            (void) peer_key_length;
2695            (void) shared_secret;
2696            (void) shared_secret_size;
2697            (void) shared_secret_length;
2698            return( PSA_ERROR_NOT_SUPPORTED );
2699
2700    }
2701}
2702
2703static inline psa_status_t psa_driver_wrapper_pake_setup(
2704    psa_pake_operation_t *operation,
2705    const psa_crypto_driver_pake_inputs_t *inputs )
2706{
2707    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2708
2709    psa_key_location_t location =
2710            PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( &inputs->attributes ) );
2711
2712    switch( location )
2713    {
2714        case PSA_KEY_LOCATION_LOCAL_STORAGE:
2715            /* Key is stored in the slot in export representation, so
2716             * cycle through all known transparent accelerators */
2717            status = PSA_ERROR_NOT_SUPPORTED;
2718#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2719#if defined(PSA_CRYPTO_DRIVER_TEST)
2720            status = mbedtls_test_transparent_pake_setup(
2721                        &operation->data.ctx.transparent_test_driver_ctx,
2722                        inputs );
2723            if( status == PSA_SUCCESS )
2724                operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
2725            /* Declared with fallback == true */
2726            if( status != PSA_ERROR_NOT_SUPPORTED )
2727                return( status );
2728#endif /* PSA_CRYPTO_DRIVER_TEST */
2729#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2730#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
2731            status = mbedtls_psa_pake_setup( &operation->data.ctx.mbedtls_ctx,
2732                        inputs );
2733            if( status == PSA_SUCCESS )
2734                operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2735#endif
2736            return status;
2737        /* Add cases for opaque driver here */
2738        default:
2739            /* Key is declared with a lifetime not known to us */
2740            (void)operation;
2741            return( PSA_ERROR_INVALID_ARGUMENT );
2742    }
2743}
2744
2745static inline psa_status_t psa_driver_wrapper_pake_output(
2746    psa_pake_operation_t *operation,
2747    psa_crypto_driver_pake_step_t step,
2748    uint8_t *output,
2749    size_t output_size,
2750    size_t *output_length )
2751{
2752    switch( operation->id )
2753    {
2754#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
2755        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2756            return( mbedtls_psa_pake_output( &operation->data.ctx.mbedtls_ctx, step,
2757                                             output, output_size, output_length ) );
2758#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
2759
2760#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2761#if defined(PSA_CRYPTO_DRIVER_TEST)
2762        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2763            return( mbedtls_test_transparent_pake_output(
2764                        &operation->data.ctx.transparent_test_driver_ctx,
2765                        step, output, output_size, output_length ) );
2766#endif /* PSA_CRYPTO_DRIVER_TEST */
2767#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2768        default:
2769            (void) step;
2770            (void) output;
2771            (void) output_size;
2772            (void) output_length;
2773            return( PSA_ERROR_INVALID_ARGUMENT );
2774    }
2775}
2776
2777static inline psa_status_t psa_driver_wrapper_pake_input(
2778    psa_pake_operation_t *operation,
2779    psa_crypto_driver_pake_step_t step,
2780    const uint8_t *input,
2781    size_t input_length )
2782{
2783    switch( operation->id )
2784    {
2785#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
2786        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2787            return( mbedtls_psa_pake_input( &operation->data.ctx.mbedtls_ctx,
2788                                            step, input,
2789                                            input_length ) );
2790#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
2791
2792#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2793#if defined(PSA_CRYPTO_DRIVER_TEST)
2794        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2795            return( mbedtls_test_transparent_pake_input(
2796                        &operation->data.ctx.transparent_test_driver_ctx,
2797                        step,
2798                        input, input_length ) );
2799#endif /* PSA_CRYPTO_DRIVER_TEST */
2800#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2801        default:
2802            (void) step;
2803            (void) input;
2804            (void) input_length;
2805            return( PSA_ERROR_INVALID_ARGUMENT );
2806    }
2807}
2808
2809static inline psa_status_t psa_driver_wrapper_pake_get_implicit_key(
2810    psa_pake_operation_t *operation,
2811    uint8_t *output, size_t output_size,
2812    size_t *output_length )
2813{
2814    switch( operation->id )
2815    {
2816#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
2817        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2818            return( mbedtls_psa_pake_get_implicit_key( &operation->data.ctx.mbedtls_ctx,
2819                                                       output, output_size, output_length ) );
2820#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
2821
2822#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2823#if defined(PSA_CRYPTO_DRIVER_TEST)
2824        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2825            return( mbedtls_test_transparent_pake_get_implicit_key(
2826                        &operation->data.ctx.transparent_test_driver_ctx,
2827                        output, output_size, output_length ) );
2828#endif /* PSA_CRYPTO_DRIVER_TEST */
2829#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2830        default:
2831            (void) output;
2832            (void) output_size;
2833            (void) output_length;
2834            return( PSA_ERROR_INVALID_ARGUMENT );
2835    }
2836}
2837
2838static inline psa_status_t psa_driver_wrapper_pake_abort(
2839    psa_pake_operation_t * operation )
2840{
2841    switch( operation->id )
2842    {
2843#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
2844        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
2845            return( mbedtls_psa_pake_abort( &operation->data.ctx.mbedtls_ctx ) );
2846#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
2847
2848#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2849#if defined(PSA_CRYPTO_DRIVER_TEST)
2850        case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
2851            return( mbedtls_test_transparent_pake_abort(
2852                        &operation->data.ctx.transparent_test_driver_ctx ) );
2853#endif /* PSA_CRYPTO_DRIVER_TEST */
2854#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2855        default:
2856            return( PSA_ERROR_INVALID_ARGUMENT );
2857    }
2858}
2859
2860#endif /* MBEDTLS_PSA_CRYPTO_C */
2861