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