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