1 /*
2  *  PSA crypto support for secure element drivers
3  */
4 /*
5  *  Copyright The Mbed TLS Contributors
6  *  SPDX-License-Identifier: Apache-2.0
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
9  *  not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include "common.h"
22 
23 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
24 
25 #include <assert.h>
26 #include <stdint.h>
27 #include <string.h>
28 
29 #include "psa/crypto_se_driver.h"
30 
31 #include "psa_crypto_se.h"
32 
33 #if defined(MBEDTLS_PSA_ITS_FILE_C)
34 #include "psa_crypto_its.h"
35 #else /* Native ITS implementation */
36 #include "psa/error.h"
37 #include "psa/internal_trusted_storage.h"
38 #endif
39 
40 #include "mbedtls/platform.h"
41 
42 
43 
44 /****************************************************************/
45 /* Driver lookup */
46 /****************************************************************/
47 
48 /* This structure is identical to psa_drv_se_context_t declared in
49  * `crypto_se_driver.h`, except that some parts are writable here
50  * (non-const, or pointer to non-const). */
51 typedef struct
52 {
53     void *persistent_data;
54     size_t persistent_data_size;
55     uintptr_t transient_data;
56 } psa_drv_se_internal_context_t;
57 
58 struct psa_se_drv_table_entry_s
59 {
60     psa_key_location_t location;
61     const psa_drv_se_t *methods;
62     union
63     {
64         psa_drv_se_internal_context_t internal;
65         psa_drv_se_context_t context;
66     } u;
67 };
68 
69 static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
70 
psa_get_se_driver_entry(psa_key_lifetime_t lifetime)71 psa_se_drv_table_entry_t *psa_get_se_driver_entry(
72     psa_key_lifetime_t lifetime )
73 {
74     size_t i;
75     psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime );
76     /* In the driver table, location=0 means an entry that isn't used.
77      * No driver has a location of 0 because it's a reserved value
78      * (which designates transparent keys). Make sure we never return
79      * a driver entry for location 0. */
80     if( location == 0 )
81         return( NULL );
82     for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
83     {
84         if( driver_table[i].location == location )
85             return( &driver_table[i] );
86     }
87     return( NULL );
88 }
89 
psa_get_se_driver_methods(const psa_se_drv_table_entry_t * driver)90 const psa_drv_se_t *psa_get_se_driver_methods(
91     const psa_se_drv_table_entry_t *driver )
92 {
93     return( driver->methods );
94 }
95 
psa_get_se_driver_context(psa_se_drv_table_entry_t * driver)96 psa_drv_se_context_t *psa_get_se_driver_context(
97     psa_se_drv_table_entry_t *driver )
98 {
99     return( &driver->u.context );
100 }
101 
psa_get_se_driver(psa_key_lifetime_t lifetime,const psa_drv_se_t ** p_methods,psa_drv_se_context_t ** p_drv_context)102 int psa_get_se_driver( psa_key_lifetime_t lifetime,
103                        const psa_drv_se_t **p_methods,
104                        psa_drv_se_context_t **p_drv_context)
105 {
106     psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
107     if( p_methods != NULL )
108         *p_methods = ( driver ? driver->methods : NULL );
109     if( p_drv_context != NULL )
110         *p_drv_context = ( driver ? &driver->u.context : NULL );
111     return( driver != NULL );
112 }
113 
114 
115 
116 /****************************************************************/
117 /* Persistent data management */
118 /****************************************************************/
119 
psa_get_se_driver_its_file_uid(const psa_se_drv_table_entry_t * driver,psa_storage_uid_t * uid)120 static psa_status_t psa_get_se_driver_its_file_uid(
121     const psa_se_drv_table_entry_t *driver,
122     psa_storage_uid_t *uid )
123 {
124     if( driver->location > PSA_MAX_SE_LOCATION )
125         return( PSA_ERROR_NOT_SUPPORTED );
126 
127 #if SIZE_MAX > UINT32_MAX
128     /* ITS file sizes are limited to 32 bits. */
129     if( driver->u.internal.persistent_data_size > UINT32_MAX )
130         return( PSA_ERROR_NOT_SUPPORTED );
131 #endif
132 
133     /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
134     *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
135     return( PSA_SUCCESS );
136 }
137 
psa_load_se_persistent_data(const psa_se_drv_table_entry_t * driver)138 psa_status_t psa_load_se_persistent_data(
139     const psa_se_drv_table_entry_t *driver )
140 {
141     psa_status_t status;
142     psa_storage_uid_t uid;
143     size_t length;
144 
145     status = psa_get_se_driver_its_file_uid( driver, &uid );
146     if( status != PSA_SUCCESS )
147         return( status );
148 
149     /* Read the amount of persistent data that the driver requests.
150      * If the data in storage is larger, it is truncated. If the data
151      * in storage is smaller, silently keep what is already at the end
152      * of the output buffer. */
153     /* psa_get_se_driver_its_file_uid ensures that the size_t
154      * persistent_data_size is in range, but compilers don't know that,
155      * so cast to reassure them. */
156     return( psa_its_get( uid, 0,
157                          (uint32_t) driver->u.internal.persistent_data_size,
158                          driver->u.internal.persistent_data,
159                          &length ) );
160 }
161 
psa_save_se_persistent_data(const psa_se_drv_table_entry_t * driver)162 psa_status_t psa_save_se_persistent_data(
163     const psa_se_drv_table_entry_t *driver )
164 {
165     psa_status_t status;
166     psa_storage_uid_t uid;
167 
168     status = psa_get_se_driver_its_file_uid( driver, &uid );
169     if( status != PSA_SUCCESS )
170         return( status );
171 
172     /* psa_get_se_driver_its_file_uid ensures that the size_t
173      * persistent_data_size is in range, but compilers don't know that,
174      * so cast to reassure them. */
175     return( psa_its_set( uid,
176                          (uint32_t) driver->u.internal.persistent_data_size,
177                          driver->u.internal.persistent_data,
178                          0 ) );
179 }
180 
psa_destroy_se_persistent_data(psa_key_location_t location)181 psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location )
182 {
183     psa_storage_uid_t uid;
184     if( location > PSA_MAX_SE_LOCATION )
185         return( PSA_ERROR_NOT_SUPPORTED );
186     uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
187     return( psa_its_remove( uid ) );
188 }
189 
psa_find_se_slot_for_key(const psa_key_attributes_t * attributes,psa_key_creation_method_t method,psa_se_drv_table_entry_t * driver,psa_key_slot_number_t * slot_number)190 psa_status_t psa_find_se_slot_for_key(
191     const psa_key_attributes_t *attributes,
192     psa_key_creation_method_t method,
193     psa_se_drv_table_entry_t *driver,
194     psa_key_slot_number_t *slot_number )
195 {
196     psa_status_t status;
197     psa_key_location_t key_location =
198         PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) );
199 
200     /* If the location is wrong, it's a bug in the library. */
201     if( driver->location != key_location )
202         return( PSA_ERROR_CORRUPTION_DETECTED );
203 
204     /* If the driver doesn't support key creation in any way, give up now. */
205     if( driver->methods->key_management == NULL )
206         return( PSA_ERROR_NOT_SUPPORTED );
207 
208     if( psa_get_key_slot_number( attributes, slot_number ) == PSA_SUCCESS )
209     {
210         /* The application wants to use a specific slot. Allow it if
211          * the driver supports it. On a system with isolation,
212          * the crypto service must check that the application is
213          * permitted to request this slot. */
214         psa_drv_se_validate_slot_number_t p_validate_slot_number =
215             driver->methods->key_management->p_validate_slot_number;
216         if( p_validate_slot_number == NULL )
217             return( PSA_ERROR_NOT_SUPPORTED );
218         status = p_validate_slot_number( &driver->u.context,
219                                          driver->u.internal.persistent_data,
220                                          attributes, method,
221                                          *slot_number );
222     }
223     else if( method == PSA_KEY_CREATION_REGISTER )
224     {
225         /* The application didn't specify a slot number. This doesn't
226          * make sense when registering a slot. */
227         return( PSA_ERROR_INVALID_ARGUMENT );
228     }
229     else
230     {
231         /* The application didn't tell us which slot to use. Let the driver
232          * choose. This is the normal case. */
233         psa_drv_se_allocate_key_t p_allocate =
234             driver->methods->key_management->p_allocate;
235         if( p_allocate == NULL )
236             return( PSA_ERROR_NOT_SUPPORTED );
237         status = p_allocate( &driver->u.context,
238                              driver->u.internal.persistent_data,
239                              attributes, method,
240                              slot_number );
241     }
242     return( status );
243 }
244 
psa_destroy_se_key(psa_se_drv_table_entry_t * driver,psa_key_slot_number_t slot_number)245 psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
246                                  psa_key_slot_number_t slot_number )
247 {
248     psa_status_t status;
249     psa_status_t storage_status;
250     /* Normally a missing method would mean that the action is not
251      * supported. But psa_destroy_key() is not supposed to return
252      * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
253      * be able to destroy it. The only use case for a driver that
254      * does not have a way to destroy keys at all is if the keys are
255      * locked in a read-only state: we can use the keys but not
256      * destroy them. Hence, if the driver doesn't support destroying
257      * keys, it's really a lack of permission. */
258     if( driver->methods->key_management == NULL ||
259         driver->methods->key_management->p_destroy == NULL )
260         return( PSA_ERROR_NOT_PERMITTED );
261     status = driver->methods->key_management->p_destroy(
262         &driver->u.context,
263         driver->u.internal.persistent_data,
264         slot_number );
265     storage_status = psa_save_se_persistent_data( driver );
266     return( status == PSA_SUCCESS ? storage_status : status );
267 }
268 
psa_init_all_se_drivers(void)269 psa_status_t psa_init_all_se_drivers( void )
270 {
271     size_t i;
272     for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
273     {
274         psa_se_drv_table_entry_t *driver = &driver_table[i];
275         if( driver->location == 0 )
276             continue; /* skipping unused entry */
277         const psa_drv_se_t *methods = psa_get_se_driver_methods( driver );
278         if( methods->p_init != NULL )
279         {
280             psa_status_t status = methods->p_init(
281                 &driver->u.context,
282                 driver->u.internal.persistent_data,
283                 driver->location );
284             if( status != PSA_SUCCESS )
285                 return( status );
286             status = psa_save_se_persistent_data( driver );
287             if( status != PSA_SUCCESS )
288                 return( status );
289         }
290     }
291     return( PSA_SUCCESS );
292 }
293 
294 
295 
296 /****************************************************************/
297 /* Driver registration */
298 /****************************************************************/
299 
psa_register_se_driver(psa_key_location_t location,const psa_drv_se_t * methods)300 psa_status_t psa_register_se_driver(
301     psa_key_location_t location,
302     const psa_drv_se_t *methods)
303 {
304     size_t i;
305     psa_status_t status;
306 
307     if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
308         return( PSA_ERROR_NOT_SUPPORTED );
309     /* Driver table entries are 0-initialized. 0 is not a valid driver
310      * location because it means a transparent key. */
311 #if defined(static_assert)
312     static_assert( PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
313                    "Secure element support requires 0 to mean a local key" );
314 #endif
315     if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
316         return( PSA_ERROR_INVALID_ARGUMENT );
317     if( location > PSA_MAX_SE_LOCATION )
318         return( PSA_ERROR_NOT_SUPPORTED );
319 
320     for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
321     {
322         if( driver_table[i].location == 0 )
323             break;
324         /* Check that location isn't already in use up to the first free
325          * entry. Since entries are created in order and never deleted,
326          * there can't be a used entry after the first free entry. */
327         if( driver_table[i].location == location )
328             return( PSA_ERROR_ALREADY_EXISTS );
329     }
330     if( i == PSA_MAX_SE_DRIVERS )
331         return( PSA_ERROR_INSUFFICIENT_MEMORY );
332 
333     driver_table[i].location = location;
334     driver_table[i].methods = methods;
335     driver_table[i].u.internal.persistent_data_size =
336         methods->persistent_data_size;
337 
338     if( methods->persistent_data_size != 0 )
339     {
340         driver_table[i].u.internal.persistent_data =
341             mbedtls_calloc( 1, methods->persistent_data_size );
342         if( driver_table[i].u.internal.persistent_data == NULL )
343         {
344             status = PSA_ERROR_INSUFFICIENT_MEMORY;
345             goto error;
346         }
347         /* Load the driver's persistent data. On first use, the persistent
348          * data does not exist in storage, and is initialized to
349          * all-bits-zero by the calloc call just above. */
350         status = psa_load_se_persistent_data( &driver_table[i] );
351         if( status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST )
352             goto error;
353     }
354 
355     return( PSA_SUCCESS );
356 
357 error:
358     memset( &driver_table[i], 0, sizeof( driver_table[i] ) );
359     return( status );
360 }
361 
psa_unregister_all_se_drivers(void)362 void psa_unregister_all_se_drivers( void )
363 {
364     size_t i;
365     for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
366     {
367         if( driver_table[i].u.internal.persistent_data != NULL )
368             mbedtls_free( driver_table[i].u.internal.persistent_data );
369     }
370     memset( driver_table, 0, sizeof( driver_table ) );
371 }
372 
373 
374 
375 /****************************************************************/
376 /* The end */
377 /****************************************************************/
378 
379 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
380