2
* Functions to delegate cryptographic operations to an available
3
* and appropriate accelerator.
4
* Warning: This file will be auto-generated in the future.
6
/* Copyright The Mbed TLS Contributors
7
* SPDX-License-Identifier: Apache-2.0
9
* Licensed under the Apache License, Version 2.0 (the "License"); you may
10
* not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
#include "psa_crypto_core.h"
23
#include "psa_crypto_driver_wrappers.h"
24
#include "mbedtls/platform.h"
26
#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
28
/* Include test driver definition when running tests */
29
#if defined(PSA_CRYPTO_DRIVER_TEST)
30
#ifndef PSA_CRYPTO_DRIVER_PRESENT
31
#define PSA_CRYPTO_DRIVER_PRESENT
33
#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
34
#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
36
#include "test/drivers/test_driver.h"
37
#endif /* PSA_CRYPTO_DRIVER_TEST */
39
/* Repeat above block for each JSON-declared driver during autogeneration */
41
/* Auto-generated values depending on which drivers are registered. ID 0 is
42
* reserved for unallocated operations. */
43
#if defined(PSA_CRYPTO_DRIVER_TEST)
44
#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (1)
45
#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (2)
46
#endif /* PSA_CRYPTO_DRIVER_TEST */
47
#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
49
/* Support the 'old' SE interface when asked to */
50
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
51
/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
52
* SE driver is present, to avoid unused argument errors at compile time. */
53
#ifndef PSA_CRYPTO_DRIVER_PRESENT
54
#define PSA_CRYPTO_DRIVER_PRESENT
56
#include "psa_crypto_se.h"
59
/* Start delegation functions */
60
psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot,
65
size_t signature_size,
66
size_t *signature_length )
68
#if defined(PSA_CRYPTO_DRIVER_PRESENT)
69
/* Try dynamically-registered SE interface first */
70
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
71
const psa_drv_se_t *drv;
72
psa_drv_se_context_t *drv_context;
74
if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
76
if( drv->asymmetric == NULL ||
77
drv->asymmetric->p_sign == NULL )
79
/* Key is defined in SE, but we have no way to exercise it */
80
return( PSA_ERROR_NOT_SUPPORTED );
82
return( drv->asymmetric->p_sign( drv_context,
83
slot->data.se.slot_number,
86
signature, signature_size,
89
#endif /* PSA_CRYPTO_SE_C */
91
/* Then try accelerator API */
92
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
93
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
94
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
95
psa_key_attributes_t attributes = {
101
case PSA_KEY_LOCATION_LOCAL_STORAGE:
102
/* Key is stored in the slot in export representation, so
103
* cycle through all known transparent accelerators */
104
#if defined(PSA_CRYPTO_DRIVER_TEST)
105
status = test_transparent_signature_sign_hash( &attributes,
107
slot->data.key.bytes,
114
/* Declared with fallback == true */
115
if( status != PSA_ERROR_NOT_SUPPORTED )
117
#endif /* PSA_CRYPTO_DRIVER_TEST */
118
/* Fell through, meaning no accelerator supports this operation */
119
return( PSA_ERROR_NOT_SUPPORTED );
120
/* Add cases for opaque driver here */
121
#if defined(PSA_CRYPTO_DRIVER_TEST)
122
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
123
return( test_opaque_signature_sign_hash( &attributes,
125
slot->data.key.bytes,
131
signature_length ) );
132
#endif /* PSA_CRYPTO_DRIVER_TEST */
134
/* Key is declared with a lifetime not known to us */
137
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
138
return( PSA_ERROR_NOT_SUPPORTED );
139
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
140
#else /* PSA_CRYPTO_DRIVER_PRESENT */
146
(void)signature_size;
147
(void)signature_length;
149
return( PSA_ERROR_NOT_SUPPORTED );
150
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
153
psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot,
157
const uint8_t *signature,
158
size_t signature_length )
160
#if defined(PSA_CRYPTO_DRIVER_PRESENT)
161
/* Try dynamically-registered SE interface first */
162
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
163
const psa_drv_se_t *drv;
164
psa_drv_se_context_t *drv_context;
166
if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
168
if( drv->asymmetric == NULL ||
169
drv->asymmetric->p_verify == NULL )
171
/* Key is defined in SE, but we have no way to exercise it */
172
return( PSA_ERROR_NOT_SUPPORTED );
174
return( drv->asymmetric->p_verify( drv_context,
175
slot->data.se.slot_number,
178
signature, signature_length ) );
180
#endif /* PSA_CRYPTO_SE_C */
182
/* Then try accelerator API */
183
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
184
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
185
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
186
psa_key_attributes_t attributes = {
192
case PSA_KEY_LOCATION_LOCAL_STORAGE:
193
/* Key is stored in the slot in export representation, so
194
* cycle through all known transparent accelerators */
195
#if defined(PSA_CRYPTO_DRIVER_TEST)
196
status = test_transparent_signature_verify_hash( &attributes,
198
slot->data.key.bytes,
204
/* Declared with fallback == true */
205
if( status != PSA_ERROR_NOT_SUPPORTED )
207
#endif /* PSA_CRYPTO_DRIVER_TEST */
208
/* Fell through, meaning no accelerator supports this operation */
209
return( PSA_ERROR_NOT_SUPPORTED );
210
/* Add cases for opaque driver here */
211
#if defined(PSA_CRYPTO_DRIVER_TEST)
212
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
213
return( test_opaque_signature_verify_hash( &attributes,
215
slot->data.key.bytes,
220
signature_length ) );
221
#endif /* PSA_CRYPTO_DRIVER_TEST */
223
/* Key is declared with a lifetime not known to us */
226
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
227
return( PSA_ERROR_NOT_SUPPORTED );
228
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
229
#else /* PSA_CRYPTO_DRIVER_PRESENT */
235
(void)signature_length;
237
return( PSA_ERROR_NOT_SUPPORTED );
238
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
241
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
242
/** Calculate the size to allocate for buffering a key with given attributes.
244
* This function provides a way to get the expected size for storing a key with
245
* the given attributes. This will be the size of the export representation for
246
* cleartext keys, and a driver-defined size for keys stored by opaque drivers.
248
* \param[in] attributes The key attribute structure of the key to store.
249
* \param[out] expected_size On success, a byte size large enough to contain
252
* \retval #PSA_SUCCESS
253
* \retval #PSA_ERROR_NOT_SUPPORTED
255
static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes,
256
size_t *expected_size )
258
size_t buffer_size = 0;
259
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
260
psa_key_type_t key_type = attributes->core.type;
261
size_t key_bits = attributes->core.bits;
265
case PSA_KEY_LOCATION_LOCAL_STORAGE:
266
buffer_size = PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits );
268
if( buffer_size == 0 )
269
return( PSA_ERROR_NOT_SUPPORTED );
271
*expected_size = buffer_size;
272
return( PSA_SUCCESS );
274
#if defined(PSA_CRYPTO_DRIVER_TEST)
275
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
276
#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
277
*expected_size = test_size_function( key_type, key_bits );
278
return( PSA_SUCCESS );
279
#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
280
if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
282
int public_key_overhead = ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
283
PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits ) : 0 );
284
*expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
285
+ TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
286
+ public_key_overhead;
288
else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( attributes->core.type ) )
290
*expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
291
+ TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
293
else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
294
!PSA_KEY_TYPE_IS_PUBLIC_KEY ( attributes->core.type ) )
296
*expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
297
+ TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
298
* ( ( key_bits + 7 ) / 8 );
302
return( PSA_ERROR_NOT_SUPPORTED );
304
return( PSA_SUCCESS );
305
#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
306
#endif /* PSA_CRYPTO_DRIVER_TEST */
309
return( PSA_ERROR_NOT_SUPPORTED );
312
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
314
psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
315
psa_key_slot_t *slot )
317
#if defined(PSA_CRYPTO_DRIVER_PRESENT)
318
/* Try dynamically-registered SE interface first */
319
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
320
const psa_drv_se_t *drv;
321
psa_drv_se_context_t *drv_context;
323
if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
325
size_t pubkey_length = 0; /* We don't support this feature yet */
326
if( drv->key_management == NULL ||
327
drv->key_management->p_generate == NULL )
329
/* Key is defined as being in SE, but we have no way to generate it */
330
return( PSA_ERROR_NOT_SUPPORTED );
332
return( drv->key_management->p_generate(
334
slot->data.se.slot_number, attributes,
335
NULL, 0, &pubkey_length ) );
337
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
339
/* Then try accelerator API */
340
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
341
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
342
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
343
size_t export_size = 0;
345
status = get_expected_key_size( attributes, &export_size );
346
if( status != PSA_SUCCESS )
349
slot->data.key.data = mbedtls_calloc(1, export_size);
350
if( slot->data.key.data == NULL )
351
return( PSA_ERROR_INSUFFICIENT_MEMORY );
352
slot->data.key.bytes = export_size;
356
case PSA_KEY_LOCATION_LOCAL_STORAGE:
357
/* Key is stored in the slot in export representation, so
358
* cycle through all known transparent accelerators */
360
/* Transparent drivers are limited to generating asymmetric keys */
361
if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
363
status = PSA_ERROR_NOT_SUPPORTED;
366
#if defined(PSA_CRYPTO_DRIVER_TEST)
367
status = test_transparent_generate_key( attributes,
369
slot->data.key.bytes,
370
&slot->data.key.bytes );
371
/* Declared with fallback == true */
372
if( status != PSA_ERROR_NOT_SUPPORTED )
374
#endif /* PSA_CRYPTO_DRIVER_TEST */
375
/* Fell through, meaning no accelerator supports this operation */
376
status = PSA_ERROR_NOT_SUPPORTED;
378
/* Add cases for opaque driver here */
379
#if defined(PSA_CRYPTO_DRIVER_TEST)
380
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
381
status = test_opaque_generate_key( attributes,
383
slot->data.key.bytes,
384
&slot->data.key.bytes );
386
#endif /* PSA_CRYPTO_DRIVER_TEST */
388
/* Key is declared with a lifetime not known to us */
389
status = PSA_ERROR_INVALID_ARGUMENT;
393
if( status != PSA_SUCCESS )
395
/* free allocated buffer */
396
mbedtls_free( slot->data.key.data );
397
slot->data.key.data = NULL;
398
slot->data.key.bytes = 0;
402
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
403
return( PSA_ERROR_NOT_SUPPORTED );
404
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
405
#else /* PSA_CRYPTO_DRIVER_PRESENT */
409
return( PSA_ERROR_NOT_SUPPORTED );
410
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
413
psa_status_t psa_driver_wrapper_validate_key( const psa_key_attributes_t *attributes,
418
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
419
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
420
/* Try accelerators in turn */
421
#if defined(PSA_CRYPTO_DRIVER_TEST)
422
status = test_transparent_validate_key( attributes,
426
/* Declared with fallback == true */
427
if( status != PSA_ERROR_NOT_SUPPORTED )
429
#endif /* PSA_CRYPTO_DRIVER_TEST */
431
return( PSA_ERROR_NOT_SUPPORTED );
432
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
437
return( PSA_ERROR_NOT_SUPPORTED );
438
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
441
psa_status_t psa_driver_wrapper_export_public_key( const psa_key_slot_t *slot,
444
size_t *data_length )
446
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
447
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
448
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
449
psa_key_attributes_t attributes = {
455
case PSA_KEY_LOCATION_LOCAL_STORAGE:
456
/* Key is stored in the slot in export representation, so
457
* cycle through all known transparent accelerators */
458
#if defined(PSA_CRYPTO_DRIVER_TEST)
459
status = test_transparent_export_public_key( &attributes,
461
slot->data.key.bytes,
465
/* Declared with fallback == true */
466
if( status != PSA_ERROR_NOT_SUPPORTED )
468
#endif /* PSA_CRYPTO_DRIVER_TEST */
469
/* Fell through, meaning no accelerator supports this operation */
470
return( PSA_ERROR_NOT_SUPPORTED );
471
/* Add cases for opaque driver here */
472
#if defined(PSA_CRYPTO_DRIVER_TEST)
473
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
474
return( test_opaque_export_public_key( &attributes,
476
slot->data.key.bytes,
480
#endif /* PSA_CRYPTO_DRIVER_TEST */
482
/* Key is declared with a lifetime not known to us */
485
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
490
return( PSA_ERROR_NOT_SUPPORTED );
491
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
497
psa_status_t psa_driver_wrapper_cipher_encrypt(
498
psa_key_slot_t *slot,
500
const uint8_t *input,
504
size_t *output_length )
506
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
507
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
508
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
509
psa_key_attributes_t attributes = {
515
case PSA_KEY_LOCATION_LOCAL_STORAGE:
516
/* Key is stored in the slot in export representation, so
517
* cycle through all known transparent accelerators */
518
#if defined(PSA_CRYPTO_DRIVER_TEST)
519
status = test_transparent_cipher_encrypt( &attributes,
521
slot->data.key.bytes,
528
/* Declared with fallback == true */
529
if( status != PSA_ERROR_NOT_SUPPORTED )
531
#endif /* PSA_CRYPTO_DRIVER_TEST */
532
/* Fell through, meaning no accelerator supports this operation */
533
return( PSA_ERROR_NOT_SUPPORTED );
534
/* Add cases for opaque driver here */
535
#if defined(PSA_CRYPTO_DRIVER_TEST)
536
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
537
return( test_opaque_cipher_encrypt( &attributes,
539
slot->data.key.bytes,
546
#endif /* PSA_CRYPTO_DRIVER_TEST */
548
/* Key is declared with a lifetime not known to us */
551
#else /* PSA_CRYPTO_DRIVER_PRESENT */
558
(void) output_length;
560
return( PSA_ERROR_NOT_SUPPORTED );
561
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
564
psa_status_t psa_driver_wrapper_cipher_decrypt(
565
psa_key_slot_t *slot,
567
const uint8_t *input,
571
size_t *output_length )
573
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
574
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
575
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
576
psa_key_attributes_t attributes = {
582
case PSA_KEY_LOCATION_LOCAL_STORAGE:
583
/* Key is stored in the slot in export representation, so
584
* cycle through all known transparent accelerators */
585
#if defined(PSA_CRYPTO_DRIVER_TEST)
586
status = test_transparent_cipher_decrypt( &attributes,
588
slot->data.key.bytes,
595
/* Declared with fallback == true */
596
if( status != PSA_ERROR_NOT_SUPPORTED )
598
#endif /* PSA_CRYPTO_DRIVER_TEST */
599
/* Fell through, meaning no accelerator supports this operation */
600
return( PSA_ERROR_NOT_SUPPORTED );
601
/* Add cases for opaque driver here */
602
#if defined(PSA_CRYPTO_DRIVER_TEST)
603
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
604
return( test_opaque_cipher_decrypt( &attributes,
606
slot->data.key.bytes,
613
#endif /* PSA_CRYPTO_DRIVER_TEST */
615
/* Key is declared with a lifetime not known to us */
618
#else /* PSA_CRYPTO_DRIVER_PRESENT */
625
(void) output_length;
627
return( PSA_ERROR_NOT_SUPPORTED );
628
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
631
psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
632
psa_operation_driver_context_t *operation,
633
psa_key_slot_t *slot,
634
psa_algorithm_t alg )
636
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
637
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
638
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
639
psa_key_attributes_t attributes = {
645
case PSA_KEY_LOCATION_LOCAL_STORAGE:
646
/* Key is stored in the slot in export representation, so
647
* cycle through all known transparent accelerators */
648
#if defined(PSA_CRYPTO_DRIVER_TEST)
649
operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
650
if( operation->ctx == NULL )
651
return PSA_ERROR_INSUFFICIENT_MEMORY;
653
status = test_transparent_cipher_encrypt_setup( operation->ctx,
656
slot->data.key.bytes,
658
/* Declared with fallback == true */
659
if( status == PSA_SUCCESS )
660
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
663
mbedtls_platform_zeroize(
665
sizeof( test_transparent_cipher_operation_t ) );
666
mbedtls_free( operation->ctx );
667
operation->ctx = NULL;
671
#endif /* PSA_CRYPTO_DRIVER_TEST */
672
/* Fell through, meaning no accelerator supports this operation */
673
return( PSA_ERROR_NOT_SUPPORTED );
674
/* Add cases for opaque driver here */
675
#if defined(PSA_CRYPTO_DRIVER_TEST)
676
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
677
operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
678
if( operation->ctx == NULL )
679
return( PSA_ERROR_INSUFFICIENT_MEMORY );
681
status = test_opaque_cipher_encrypt_setup( operation->ctx,
684
slot->data.key.bytes,
686
if( status == PSA_SUCCESS )
687
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
690
mbedtls_platform_zeroize(
692
sizeof( test_opaque_cipher_operation_t ) );
693
mbedtls_free( operation->ctx );
694
operation->ctx = NULL;
698
#endif /* PSA_CRYPTO_DRIVER_TEST */
700
/* Key is declared with a lifetime not known to us */
701
return( PSA_ERROR_NOT_SUPPORTED );
703
#else /* PSA_CRYPTO_DRIVER_PRESENT */
708
return( PSA_ERROR_NOT_SUPPORTED );
709
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
712
psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
713
psa_operation_driver_context_t *operation,
714
psa_key_slot_t *slot,
715
psa_algorithm_t alg )
717
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
718
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
719
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
720
psa_key_attributes_t attributes = {
726
case PSA_KEY_LOCATION_LOCAL_STORAGE:
727
/* Key is stored in the slot in export representation, so
728
* cycle through all known transparent accelerators */
729
#if defined(PSA_CRYPTO_DRIVER_TEST)
730
operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
731
if( operation->ctx == NULL )
732
return( PSA_ERROR_INSUFFICIENT_MEMORY );
734
status = test_transparent_cipher_decrypt_setup( operation->ctx,
737
slot->data.key.bytes,
739
/* Declared with fallback == true */
740
if( status == PSA_SUCCESS )
741
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
744
mbedtls_platform_zeroize(
746
sizeof( test_transparent_cipher_operation_t ) );
747
mbedtls_free( operation->ctx );
748
operation->ctx = NULL;
752
#endif /* PSA_CRYPTO_DRIVER_TEST */
753
/* Fell through, meaning no accelerator supports this operation */
754
return( PSA_ERROR_NOT_SUPPORTED );
755
/* Add cases for opaque driver here */
756
#if defined(PSA_CRYPTO_DRIVER_TEST)
757
case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
758
operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
759
if( operation->ctx == NULL )
760
return PSA_ERROR_INSUFFICIENT_MEMORY;
762
status = test_opaque_cipher_decrypt_setup( operation->ctx,
765
slot->data.key.bytes,
767
if( status == PSA_SUCCESS )
768
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
771
mbedtls_platform_zeroize(
773
sizeof( test_opaque_cipher_operation_t ) );
774
mbedtls_free( operation->ctx );
775
operation->ctx = NULL;
779
#endif /* PSA_CRYPTO_DRIVER_TEST */
781
/* Key is declared with a lifetime not known to us */
782
return( PSA_ERROR_NOT_SUPPORTED );
784
#else /* PSA_CRYPTO_DRIVER_PRESENT */
789
return( PSA_ERROR_NOT_SUPPORTED );
790
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
793
psa_status_t psa_driver_wrapper_cipher_generate_iv(
794
psa_operation_driver_context_t *operation,
799
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
800
switch( operation->id )
802
#if defined(PSA_CRYPTO_DRIVER_TEST)
803
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
804
return( test_transparent_cipher_generate_iv( operation->ctx,
808
#endif /* PSA_CRYPTO_DRIVER_TEST */
809
#if defined(PSA_CRYPTO_DRIVER_TEST)
810
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
811
return( test_opaque_cipher_generate_iv( operation->ctx,
815
#endif /* PSA_CRYPTO_DRIVER_TEST */
817
/* Key is attached to a driver not known to us */
818
return( PSA_ERROR_BAD_STATE );
820
#else /* PSA_CRYPTO_DRIVER_PRESENT */
826
return( PSA_ERROR_NOT_SUPPORTED );
827
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
830
psa_status_t psa_driver_wrapper_cipher_set_iv(
831
psa_operation_driver_context_t *operation,
835
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
836
switch( operation->id )
838
#if defined(PSA_CRYPTO_DRIVER_TEST)
839
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
840
return( test_transparent_cipher_set_iv( operation->ctx,
843
#endif /* PSA_CRYPTO_DRIVER_TEST */
844
#if defined(PSA_CRYPTO_DRIVER_TEST)
845
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
846
return( test_opaque_cipher_set_iv( operation->ctx,
849
#endif /* PSA_CRYPTO_DRIVER_TEST */
851
/* Key is attached to a driver not known to us */
852
return( PSA_ERROR_BAD_STATE );
854
#else /* PSA_CRYPTO_DRIVER_PRESENT */
859
return( PSA_ERROR_NOT_SUPPORTED );
860
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
863
psa_status_t psa_driver_wrapper_cipher_update(
864
psa_operation_driver_context_t *operation,
865
const uint8_t *input,
869
size_t *output_length )
871
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
872
switch( operation->id )
874
#if defined(PSA_CRYPTO_DRIVER_TEST)
875
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
876
return( test_transparent_cipher_update( operation->ctx,
882
#endif /* PSA_CRYPTO_DRIVER_TEST */
883
#if defined(PSA_CRYPTO_DRIVER_TEST)
884
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
885
return( test_opaque_cipher_update( operation->ctx,
891
#endif /* PSA_CRYPTO_DRIVER_TEST */
893
/* Key is attached to a driver not known to us */
894
return( PSA_ERROR_BAD_STATE );
896
#else /* PSA_CRYPTO_DRIVER_PRESENT */
901
(void) output_length;
904
return( PSA_ERROR_NOT_SUPPORTED );
905
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
908
psa_status_t psa_driver_wrapper_cipher_finish(
909
psa_operation_driver_context_t *operation,
912
size_t *output_length )
914
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
915
switch( operation->id )
917
#if defined(PSA_CRYPTO_DRIVER_TEST)
918
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
919
return( test_transparent_cipher_finish( operation->ctx,
923
#endif /* PSA_CRYPTO_DRIVER_TEST */
924
#if defined(PSA_CRYPTO_DRIVER_TEST)
925
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
926
return( test_opaque_cipher_finish( operation->ctx,
930
#endif /* PSA_CRYPTO_DRIVER_TEST */
932
/* Key is attached to a driver not known to us */
933
return( PSA_ERROR_BAD_STATE );
935
#else /* PSA_CRYPTO_DRIVER_PRESENT */
939
(void) output_length;
941
return( PSA_ERROR_NOT_SUPPORTED );
942
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
945
psa_status_t psa_driver_wrapper_cipher_abort(
946
psa_operation_driver_context_t *operation )
948
#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
949
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
951
/* The object has (apparently) been initialized but it is not in use. It's
952
* ok to call abort on such an object, and there's nothing to do. */
953
if( operation->ctx == NULL && operation->id == 0 )
954
return( PSA_SUCCESS );
956
switch( operation->id )
958
#if defined(PSA_CRYPTO_DRIVER_TEST)
959
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
960
status = test_transparent_cipher_abort( operation->ctx );
961
mbedtls_platform_zeroize(
963
sizeof( test_transparent_cipher_operation_t ) );
964
mbedtls_free( operation->ctx );
965
operation->ctx = NULL;
969
#endif /* PSA_CRYPTO_DRIVER_TEST */
970
#if defined(PSA_CRYPTO_DRIVER_TEST)
971
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
972
status = test_opaque_cipher_abort( operation->ctx );
973
mbedtls_platform_zeroize(
975
sizeof( test_opaque_cipher_operation_t ) );
976
mbedtls_free( operation->ctx );
977
operation->ctx = NULL;
981
#endif /* PSA_CRYPTO_DRIVER_TEST */
983
/* Operation is attached to a driver not known to us */
984
return( PSA_ERROR_BAD_STATE );
986
#else /* PSA_CRYPTO_DRIVER_PRESENT */
989
return( PSA_ERROR_NOT_SUPPORTED );
990
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
993
/* End of automatically generated file. */