Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 1 | /* |
| 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 Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 29 | |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame^] | 30 | #include <assert.h> |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 31 | #include <string.h> |
| 32 | |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 33 | #include "psa_crypto_se.h" |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | psa_key_lifetime_t lifetime; |
| 38 | const psa_drv_se_t *methods; |
| 39 | } method_table_entry_t; |
| 40 | |
| 41 | static method_table_entry_t driver_table[PSA_MAX_SE_DRIVERS]; |
| 42 | |
| 43 | psa_status_t psa_register_se_driver( |
| 44 | psa_key_lifetime_t lifetime, |
| 45 | const psa_drv_se_t *methods) |
| 46 | { |
| 47 | size_t i; |
| 48 | |
| 49 | if( methods->hal_version != PSA_DRV_SE_HAL_VERSION ) |
| 50 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9717d10 | 2019-06-26 11:50:04 +0200 | [diff] [blame^] | 51 | /* Driver table entries are 0-initialized. 0 is not a valid driver |
| 52 | * lifetime because it means a volatile key. */ |
| 53 | #if defined(static_assert) |
| 54 | static_assert( PSA_KEY_LIFETIME_VOLATILE == 0, |
| 55 | "Secure element support requires 0 to mean a volatile key" ); |
| 56 | #endif |
Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 57 | if( lifetime == PSA_KEY_LIFETIME_VOLATILE || |
| 58 | lifetime == PSA_KEY_LIFETIME_PERSISTENT ) |
| 59 | { |
| 60 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 61 | } |
| 62 | |
| 63 | for( i = 0; i < PSA_MAX_SE_DRIVERS; i++ ) |
| 64 | { |
| 65 | if( driver_table[i].lifetime == 0 ) |
| 66 | break; |
| 67 | /* Check that lifetime isn't already in use up to the first free |
| 68 | * entry. Since entries are created in order and never deleted, |
| 69 | * there can't be a used entry after the first free entry. */ |
| 70 | if( driver_table[i].lifetime == lifetime ) |
| 71 | return( PSA_ERROR_ALREADY_EXISTS ); |
| 72 | } |
| 73 | if( i == PSA_MAX_SE_DRIVERS ) |
| 74 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 75 | |
| 76 | driver_table[i].lifetime = lifetime; |
| 77 | driver_table[i].methods = methods; |
| 78 | return( PSA_SUCCESS ); |
| 79 | } |
| 80 | |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 81 | void psa_unregister_all_se_drivers( void ) |
| 82 | { |
| 83 | memset( driver_table, 0, sizeof( driver_table ) ); |
| 84 | } |
| 85 | |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 86 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |