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/* END-common headers */
24
25#if defined(MBEDTLS_PSA_CRYPTO_C)
26
27/* BEGIN-driver headers */
28/* Headers for mbedtls_test opaque driver */
29#if defined(PSA_CRYPTO_DRIVER_TEST)
30#include "test/drivers/test_driver.h"
31
32#endif
33/* Headers for mbedtls_test transparent driver */
34#if defined(PSA_CRYPTO_DRIVER_TEST)
35#include "test/drivers/test_driver.h"
36
37#endif
38/* Headers for p256 transparent driver */
39#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
40#include "../3rdparty/p256-m/p256-m_driver_entrypoints.h"
41
42#endif
43
44/* END-driver headers */
45
46/* Auto-generated values depending on which drivers are registered.
47 * ID 0 is reserved for unallocated operations.
48 * ID 1 is reserved for the Mbed TLS software driver. */
49/* BEGIN-driver id definition */
50#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
51#define MBEDTLS_TEST_OPAQUE_DRIVER_ID (2)
52#define MBEDTLS_TEST_TRANSPARENT_DRIVER_ID (3)
53#define P256_TRANSPARENT_DRIVER_ID (4)
54
55/* END-driver id */
56
57/* BEGIN-Common Macro definitions */
58
59/* END-Common Macro definitions */
60
61/* Support the 'old' SE interface when asked to */
62#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
63/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
64 * SE driver is present, to avoid unused argument errors at compile time. */
65#ifndef PSA_CRYPTO_DRIVER_PRESENT
66#define PSA_CRYPTO_DRIVER_PRESENT
67#endif
68#include "psa_crypto_se.h"
69#endif
70
71/** Get the key buffer size required to store the key material of a key
72 *  associated with an opaque driver.
73 *
74 * \param[in] attributes  The key attributes.
75 * \param[out] key_buffer_size  Minimum buffer size to contain the key material
76 *
77 * \retval #PSA_SUCCESS
78 *         The minimum size for a buffer to contain the key material has been
79 *         returned successfully.
80 * \retval #PSA_ERROR_NOT_SUPPORTED
81 *         The type and/or the size in bits of the key or the combination of
82 *         the two is not supported.
83 * \retval #PSA_ERROR_INVALID_ARGUMENT
84 *         The key is declared with a lifetime not known to us.
85 */
86psa_status_t psa_driver_wrapper_get_key_buffer_size(
87    const psa_key_attributes_t *attributes,
88    size_t *key_buffer_size )
89{
90    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
91    psa_key_type_t key_type = psa_get_key_type(attributes);
92    size_t key_bits = psa_get_key_bits(attributes);
93
94    *key_buffer_size = 0;
95    switch( location )
96    {
97#if defined(PSA_CRYPTO_DRIVER_TEST)
98        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
99#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
100            /* Emulate property 'builtin_key_size' */
101            if( psa_key_id_is_builtin(
102                    MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
103                        psa_get_key_id( attributes ) ) ) )
104            {
105                *key_buffer_size = sizeof( psa_drv_slot_number_t );
106                return( PSA_SUCCESS );
107            }
108#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
109            *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
110                                                                  key_bits );
111            return( ( *key_buffer_size != 0 ) ?
112                    PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
113#endif /* PSA_CRYPTO_DRIVER_TEST */
114
115        default:
116            (void)key_type;
117            (void)key_bits;
118            return( PSA_ERROR_INVALID_ARGUMENT );
119    }
120}
121
122psa_status_t psa_driver_wrapper_export_public_key(
123    const psa_key_attributes_t *attributes,
124    const uint8_t *key_buffer, size_t key_buffer_size,
125    uint8_t *data, size_t data_size, size_t *data_length )
126
127{
128
129    psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
130    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
131                                      psa_get_key_lifetime( attributes ) );
132
133    /* Try dynamically-registered SE interface first */
134#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
135    const psa_drv_se_t *drv;
136    psa_drv_se_context_t *drv_context;
137
138    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
139    {
140        if( ( drv->key_management == NULL ) ||
141            ( drv->key_management->p_export_public == NULL ) )
142        {
143            return( PSA_ERROR_NOT_SUPPORTED );
144        }
145
146        return( drv->key_management->p_export_public(
147                    drv_context,
148                    *( (psa_key_slot_number_t *)key_buffer ),
149                    data, data_size, data_length ) );
150    }
151#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
152
153    switch( location )
154    {
155        case PSA_KEY_LOCATION_LOCAL_STORAGE:
156            /* Key is stored in the slot in export representation, so
157             * cycle through all known transparent accelerators */
158#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
159
160#if (defined(PSA_CRYPTO_DRIVER_TEST) )
161            status = mbedtls_test_transparent_export_public_key
162                (attributes,
163                                key_buffer,
164                                key_buffer_size,
165                                data,
166                                data_size,
167                                data_length
168            );
169
170            if( status != PSA_ERROR_NOT_SUPPORTED )
171                return( status );
172#endif
173
174#if (defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED) )
175            status = p256_transparent_export_public_key
176                (attributes,
177                                key_buffer,
178                                key_buffer_size,
179                                data,
180                                data_size,
181                                data_length
182            );
183
184            if( status != PSA_ERROR_NOT_SUPPORTED )
185                return( status );
186#endif
187
188
189#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
190            /* Fell through, meaning no accelerator supports this operation */
191            return( psa_export_public_key_internal( attributes,
192                                                    key_buffer,
193                                                    key_buffer_size,
194                                                    data,
195                                                    data_size,
196                                                    data_length ) );
197
198        /* Add cases for opaque driver here */
199#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
200
201#if (defined(PSA_CRYPTO_DRIVER_TEST) )
202        case 0x7fffff:
203            return( mbedtls_test_opaque_export_public_key
204            (attributes,
205                            key_buffer,
206                            key_buffer_size,
207                            data,
208                            data_size,
209                            data_length
210        ));
211#endif
212
213
214#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
215        default:
216            /* Key is declared with a lifetime not known to us */
217            return( status );
218    }
219
220}
221
222psa_status_t psa_driver_wrapper_get_builtin_key(
223    psa_drv_slot_number_t slot_number,
224    psa_key_attributes_t *attributes,
225    uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
226{
227
228    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
229    switch( location )
230    {
231#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
232
233#if (defined(PSA_CRYPTO_DRIVER_TEST) )
234        case 0x7fffff:
235            return( mbedtls_test_opaque_get_builtin_key
236            (slot_number,
237                            attributes,
238                            key_buffer,
239                            key_buffer_size,
240                            key_buffer_length
241        ));
242#endif
243
244
245#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
246        default:
247            (void) slot_number;
248            (void) key_buffer;
249            (void) key_buffer_size;
250            (void) key_buffer_length;
251            return( PSA_ERROR_DOES_NOT_EXIST );
252    }
253
254}
255
256#endif /* MBEDTLS_PSA_CRYPTO_C */
257