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