blob: 70e3a168028a9fe969d42676ebd0a907b38eea18 [file] [log] [blame]
Gilles Peskinea899a722019-06-24 14:06:43 +02001/*
2 * PSA crypto support for secure element drivers
3 */
4/* Copyright (C) 2019, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of Mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinea899a722019-06-24 14:06:43 +020029
Gilles Peskine9717d102019-06-26 11:50:04 +020030#include <assert.h>
Gilles Peskined0890212019-06-24 14:34:43 +020031#include <string.h>
32
Gilles Peskinea899a722019-06-24 14:06:43 +020033#include "psa_crypto_se.h"
34
Gilles Peskinef989dbe2019-06-26 18:18:12 +020035/****************************************************************/
36/* Driver lookup */
37/****************************************************************/
38
39typedef struct psa_se_drv_table_entry_s
Gilles Peskinea899a722019-06-24 14:06:43 +020040{
41 psa_key_lifetime_t lifetime;
42 const psa_drv_se_t *methods;
Gilles Peskinef989dbe2019-06-26 18:18:12 +020043} psa_se_drv_table_entry_t;
Gilles Peskinea899a722019-06-24 14:06:43 +020044
Gilles Peskinef989dbe2019-06-26 18:18:12 +020045static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
46
47const psa_se_drv_table_entry_t *psa_get_se_driver_entry(
48 psa_key_lifetime_t lifetime )
49{
50 size_t i;
51 if( lifetime == 0 )
52 return( NULL );
53 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
54 {
55 if( driver_table[i].lifetime == lifetime )
56 return( &driver_table[i] );
57 }
58 return( NULL );
59}
60
61const psa_drv_se_t *psa_get_se_driver_methods(
62 const psa_se_drv_table_entry_t *drv )
63{
64 return( drv->methods );
65}
66
67const psa_drv_se_t *psa_get_se_driver( psa_key_lifetime_t lifetime )
68{
69 const psa_se_drv_table_entry_t *drv = psa_get_se_driver_entry( lifetime );
70 if( drv == NULL )
71 return( NULL );
72 else
73 return( drv->methods );
74}
75
76
77
78
79/****************************************************************/
80/* Driver registration */
81/****************************************************************/
Gilles Peskinea899a722019-06-24 14:06:43 +020082
83psa_status_t psa_register_se_driver(
84 psa_key_lifetime_t lifetime,
85 const psa_drv_se_t *methods)
86{
87 size_t i;
88
89 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
90 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9717d102019-06-26 11:50:04 +020091 /* Driver table entries are 0-initialized. 0 is not a valid driver
92 * lifetime because it means a volatile key. */
93#if defined(static_assert)
94 static_assert( PSA_KEY_LIFETIME_VOLATILE == 0,
95 "Secure element support requires 0 to mean a volatile key" );
96#endif
Gilles Peskinea899a722019-06-24 14:06:43 +020097 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
98 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
99 {
100 return( PSA_ERROR_INVALID_ARGUMENT );
101 }
102
103 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
104 {
105 if( driver_table[i].lifetime == 0 )
106 break;
107 /* Check that lifetime isn't already in use up to the first free
108 * entry. Since entries are created in order and never deleted,
109 * there can't be a used entry after the first free entry. */
110 if( driver_table[i].lifetime == lifetime )
111 return( PSA_ERROR_ALREADY_EXISTS );
112 }
113 if( i == PSA_MAX_SE_DRIVERS )
114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
115
116 driver_table[i].lifetime = lifetime;
117 driver_table[i].methods = methods;
118 return( PSA_SUCCESS );
119}
120
Gilles Peskined0890212019-06-24 14:34:43 +0200121void psa_unregister_all_se_drivers( void )
122{
123 memset( driver_table, 0, sizeof( driver_table ) );
124}
125
Gilles Peskinef989dbe2019-06-26 18:18:12 +0200126
127
128/****************************************************************/
129/* The end */
130/****************************************************************/
131
Gilles Peskinea8ade162019-06-26 11:24:49 +0200132#endif /* MBEDTLS_PSA_CRYPTO_SE_C */