blob: 32142eb9a28fb584f5ee137d115c5823f02d7de6 [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
28#if defined(MBEDTLS_PSA_CRYPTO_C)
29
Gilles Peskined0890212019-06-24 14:34:43 +020030#include <string.h>
31
Gilles Peskinea899a722019-06-24 14:06:43 +020032#include "psa_crypto_se.h"
33
34typedef struct
35{
36 psa_key_lifetime_t lifetime;
37 const psa_drv_se_t *methods;
38} method_table_entry_t;
39
40static method_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
41
42psa_status_t psa_register_se_driver(
43 psa_key_lifetime_t lifetime,
44 const psa_drv_se_t *methods)
45{
46 size_t i;
47
48 if( methods->hal_version != PSA_DRV_SE_HAL_VERSION )
49 return( PSA_ERROR_NOT_SUPPORTED );
50 if( lifetime == PSA_KEY_LIFETIME_VOLATILE ||
51 lifetime == PSA_KEY_LIFETIME_PERSISTENT )
52 {
53 return( PSA_ERROR_INVALID_ARGUMENT );
54 }
55
56 for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ )
57 {
58 if( driver_table[i].lifetime == 0 )
59 break;
60 /* Check that lifetime isn't already in use up to the first free
61 * entry. Since entries are created in order and never deleted,
62 * there can't be a used entry after the first free entry. */
63 if( driver_table[i].lifetime == lifetime )
64 return( PSA_ERROR_ALREADY_EXISTS );
65 }
66 if( i == PSA_MAX_SE_DRIVERS )
67 return( PSA_ERROR_INSUFFICIENT_MEMORY );
68
69 driver_table[i].lifetime = lifetime;
70 driver_table[i].methods = methods;
71 return( PSA_SUCCESS );
72}
73
Gilles Peskined0890212019-06-24 14:34:43 +020074void psa_unregister_all_se_drivers( void )
75{
76 memset( driver_table, 0, sizeof( driver_table ) );
77}
78
Gilles Peskinea899a722019-06-24 14:06:43 +020079#endif /* MBEDTLS_PSA_CRYPTO_C */