Coverage Report

Created: 2026-07-25 06:10

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