| 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 | #ifndef PSA_CRYPTO_SE_H | 
|  | 23 | #define PSA_CRYPTO_SE_H | 
|  | 24 |  | 
|  | 25 | #if !defined(MBEDTLS_CONFIG_FILE) | 
|  | 26 | #include "mbedtls/config.h" | 
|  | 27 | #else | 
|  | 28 | #include MBEDTLS_CONFIG_FILE | 
|  | 29 | #endif | 
|  | 30 |  | 
|  | 31 | #include "psa/crypto.h" | 
|  | 32 | #include "psa/crypto_se_driver.h" | 
|  | 33 |  | 
| Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 34 | /** The maximum lifetime value that this implementation supports | 
|  | 35 | * for a secure element. | 
|  | 36 | * | 
|  | 37 | * This is not a characteristic that each PSA implementation has, but a | 
|  | 38 | * limitation of the current implementation due to the constraints imposed | 
|  | 39 | * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. | 
|  | 40 | * | 
|  | 41 | * The minimum lifetime value for a secure element is 2, like on any | 
|  | 42 | * PSA implementation (0=volatile and 1=internal-storage are taken). | 
|  | 43 | */ | 
|  | 44 | #define PSA_MAX_SE_LIFETIME 255 | 
|  | 45 |  | 
|  | 46 | /** The base of the range of ITS file identifiers for secure element | 
|  | 47 | * driver persistent data. | 
|  | 48 | * | 
|  | 49 | * We use a slice of the implemenation reserved range 0xffff0000..0xffffffff, | 
|  | 50 | * specifically the range 0xfffffe00..0xfffffeff. The length of this range | 
|  | 51 | * drives the value of #PSA_MAX_SE_LIFETIME. | 
|  | 52 | * The identifiers 0xfffffe00 and 0xfffffe01 are actually not used since | 
|  | 53 | * they correspond to #PSA_KEY_LIFETIME_VOLATILE and | 
|  | 54 | * #PSA_KEY_LIFETIME_PERSISTENT which don't have a driver. | 
|  | 55 | */ | 
|  | 56 | #define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ( (psa_key_id_t) 0xfffffe00 ) | 
|  | 57 |  | 
| Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 58 | /** The maximum number of registered secure element driver lifetimes. */ | 
|  | 59 | #define PSA_MAX_SE_DRIVERS 4 | 
|  | 60 |  | 
| Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 61 | /** Unregister all secure element drivers. | 
|  | 62 | * | 
|  | 63 | * \warning Do not call this function while the library is in the initialized | 
|  | 64 | *          state. This function is only intended to be called at the end | 
|  | 65 | *          of mbedtls_psa_crypto_free(). | 
|  | 66 | */ | 
|  | 67 | void psa_unregister_all_se_drivers( void ); | 
|  | 68 |  | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 69 | /** A structure that describes a registered secure element driver. | 
|  | 70 | * | 
|  | 71 | * A secure element driver table entry contains a pointer to the | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 72 | * driver's method table as well as the driver context structure. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 73 | */ | 
|  | 74 | typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t; | 
|  | 75 |  | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 76 | /** Return the secure element driver information for a lifetime value. | 
|  | 77 | * | 
|  | 78 | * \param lifetime              The lifetime value to query. | 
|  | 79 | * \param[out] p_methods        On output, if there is a driver, | 
|  | 80 | *                              \c *methods points to its method table. | 
|  | 81 | *                              Otherwise \c *methods is \c NULL. | 
|  | 82 | * \param[out] p_drv_context    On output, if there is a driver, | 
|  | 83 | *                              \c *drv_context points to its context | 
|  | 84 | *                              structure. | 
|  | 85 | *                              Otherwise \c *drv_context is \c NULL. | 
|  | 86 | * | 
|  | 87 | * \retval 1 | 
|  | 88 | *         \p lifetime corresponds to a registered driver. | 
|  | 89 | * \retval 0 | 
|  | 90 | *         \p lifetime does not correspond to a registered driver. | 
|  | 91 | */ | 
|  | 92 | int psa_get_se_driver( psa_key_lifetime_t lifetime, | 
|  | 93 | const psa_drv_se_t **p_methods, | 
|  | 94 | psa_drv_se_context_t **p_drv_context); | 
|  | 95 |  | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 96 | /** Return the secure element driver table entry for a lifetime value. | 
|  | 97 | * | 
|  | 98 | * \param lifetime      The lifetime value to query. | 
|  | 99 | * | 
|  | 100 | * \return The driver table entry for \p lifetime, or | 
|  | 101 | *         \p NULL if \p lifetime does not correspond to a registered driver. | 
|  | 102 | */ | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 103 | psa_se_drv_table_entry_t *psa_get_se_driver_entry( | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 104 | psa_key_lifetime_t lifetime ); | 
|  | 105 |  | 
|  | 106 | /** Return the method table for a secure element driver. | 
|  | 107 | * | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 108 | * \param[in] driver    The driver table entry to access, or \c NULL. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 109 | * | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 110 | * \return The driver's method table. | 
|  | 111 | *         \c NULL if \p driver is \c NULL. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 112 | */ | 
|  | 113 | const psa_drv_se_t *psa_get_se_driver_methods( | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 114 | const psa_se_drv_table_entry_t *driver ); | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 115 |  | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 116 | /** Return the context of a secure element driver. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 117 | * | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 118 | * \param[in] driver    The driver table entry to access, or \c NULL. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 119 | * | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 120 | * \return A pointer to the driver context. | 
|  | 121 | *         \c NULL if \p driver is \c NULL. | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 122 | */ | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 123 | psa_drv_se_context_t *psa_get_se_driver_context( | 
|  | 124 | psa_se_drv_table_entry_t *driver ); | 
|  | 125 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 126 | /** Find a free slot for a key that is to be created. | 
|  | 127 | * | 
|  | 128 | * This function calls the relevant method in the driver to find a suitable | 
|  | 129 | * slot for a key with the given attributes. | 
|  | 130 | * | 
|  | 131 | * \param[in] attributes    Metadata about the key that is about to be created. | 
|  | 132 | * \param[in] driver        The driver table entry to query. | 
|  | 133 | * \param[out] slot_number  On success, a slot number that is free in this | 
|  | 134 | *                          secure element. | 
|  | 135 | */ | 
|  | 136 | psa_status_t psa_find_se_slot_for_key( | 
|  | 137 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 138 | psa_key_creation_method_t method, | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 139 | psa_se_drv_table_entry_t *driver, | 
|  | 140 | psa_key_slot_number_t *slot_number ); | 
|  | 141 |  | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 142 | /** Destoy a key in a secure element. | 
|  | 143 | * | 
|  | 144 | * This function calls the relevant driver method to destroy a key | 
|  | 145 | * and updates the driver's persistent data. | 
|  | 146 | */ | 
|  | 147 | psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver, | 
|  | 148 | psa_key_slot_number_t slot_number ); | 
|  | 149 |  | 
| Gilles Peskine | 5243a20 | 2019-07-12 23:38:19 +0200 | [diff] [blame] | 150 | /** Load the persistent data of a secure element driver. | 
|  | 151 | * | 
|  | 152 | * \param driver        The driver table entry containing the persistent | 
|  | 153 | *                      data to load from storage. | 
|  | 154 | */ | 
|  | 155 | psa_status_t psa_load_se_persistent_data( | 
|  | 156 | const psa_se_drv_table_entry_t *driver ); | 
|  | 157 |  | 
|  | 158 | /** Save the persistent data of a secure element driver. | 
|  | 159 | * | 
|  | 160 | * \param[in] driver    The driver table entry containing the persistent | 
|  | 161 | *                      data to save to storage. | 
|  | 162 | */ | 
|  | 163 | psa_status_t psa_save_se_persistent_data( | 
|  | 164 | const psa_se_drv_table_entry_t *driver ); | 
| Gilles Peskine | f989dbe | 2019-06-26 18:18:12 +0200 | [diff] [blame] | 165 |  | 
| Gilles Peskine | 8b96cad | 2019-07-23 17:38:08 +0200 | [diff] [blame] | 166 | /** Destroy the persistent data of a secure element driver. | 
|  | 167 | * | 
|  | 168 | * This is currently only used for testing. | 
|  | 169 | * | 
|  | 170 | * \param[in] lifetime  The driver lifetime whose persistent data should | 
|  | 171 | *                      be erased. | 
|  | 172 | */ | 
|  | 173 | psa_status_t psa_destroy_se_persistent_data( psa_key_lifetime_t lifetime ); | 
|  | 174 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 175 |  | 
|  | 176 | /** The storage representation of a key whose data is in a secure element. | 
|  | 177 | */ | 
|  | 178 | typedef struct | 
|  | 179 | { | 
|  | 180 | uint8_t slot_number[sizeof( psa_key_slot_number_t )]; | 
|  | 181 | uint8_t bits[sizeof( psa_key_bits_t )]; | 
|  | 182 | } psa_se_key_data_storage_t; | 
|  | 183 |  | 
| Gilles Peskine | a899a72 | 2019-06-24 14:06:43 +0200 | [diff] [blame] | 184 | #endif /* PSA_CRYPTO_SE_H */ |