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