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