| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  PSA crypto layer on top of Mbed TLS crypto | 
|  | 3 | */ | 
| Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | *  Copyright The Mbed TLS Contributors | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 6 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 7 | * | 
|  | 8 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 9 | *  not use this file except in compliance with the License. | 
|  | 10 | *  You may obtain a copy of the License at | 
|  | 11 | * | 
|  | 12 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 13 | * | 
|  | 14 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 15 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 16 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 17 | *  See the License for the specific language governing permissions and | 
|  | 18 | *  limitations under the License. | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 19 | */ | 
|  | 20 |  | 
|  | 21 | #ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H | 
|  | 22 | #define PSA_CRYPTO_SLOT_MANAGEMENT_H | 
|  | 23 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 24 | #include "psa/crypto.h" | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 25 | #include "psa_crypto_core.h" | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 26 | #include "psa_crypto_se.h" | 
|  | 27 |  | 
| Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 28 | /** Range of volatile key identifiers. | 
|  | 29 | * | 
| Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 30 | *  The last #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation | 
|  | 31 | *  range of key identifiers are reserved for volatile key identifiers. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 32 | *  A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the | 
| Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 33 | *  index of the key slot containing the volatile key definition. | 
|  | 34 | */ | 
|  | 35 |  | 
|  | 36 | /** The minimum value for a volatile key identifier. | 
|  | 37 | */ | 
|  | 38 | #define PSA_KEY_ID_VOLATILE_MIN  ( PSA_KEY_ID_VENDOR_MAX - \ | 
| Steven Cooreman | 863470a | 2021-02-15 14:03:19 +0100 | [diff] [blame] | 39 | MBEDTLS_PSA_KEY_SLOT_COUNT + 1 ) | 
| Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 40 |  | 
|  | 41 | /** The maximum value for a volatile key identifier. | 
|  | 42 | */ | 
|  | 43 | #define PSA_KEY_ID_VOLATILE_MAX  PSA_KEY_ID_VENDOR_MAX | 
|  | 44 |  | 
| Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 45 | /** Test whether a key identifier is a volatile key identifier. | 
|  | 46 | * | 
|  | 47 | * \param key_id  Key identifier to test. | 
|  | 48 | * | 
|  | 49 | * \retval 1 | 
|  | 50 | *         The key identifier is a volatile key identifier. | 
|  | 51 | * \retval 0 | 
|  | 52 | *         The key identifier is not a volatile key identifier. | 
|  | 53 | */ | 
|  | 54 | static inline int psa_key_id_is_volatile( psa_key_id_t key_id ) | 
|  | 55 | { | 
|  | 56 | return( ( key_id >= PSA_KEY_ID_VOLATILE_MIN ) && | 
|  | 57 | ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) ); | 
|  | 58 | } | 
|  | 59 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 60 | /** Get the description of a key given its identifier and lock it. | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 61 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 62 | * The descriptions of volatile keys and loaded persistent keys are stored in | 
|  | 63 | * key slots. This function returns a pointer to the key slot containing the | 
|  | 64 | * description of a key given its identifier. | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 65 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 66 | * In case of a persistent key, the function loads the description of the key | 
|  | 67 | * into a key slot if not already done. | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 68 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 69 | * On success, the returned key slot is locked. It is the responsibility of | 
|  | 70 | * the caller to unlock the key slot when it does not access it anymore. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 71 | * | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 72 | * \param key           Key identifier to query. | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 73 | * \param[out] p_slot   On success, `*p_slot` contains a pointer to the | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 74 | *                      key slot containing the description of the key | 
|  | 75 | *                      identified by \p key. | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 76 | * | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 77 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 1d12d87 | 2020-11-18 17:21:22 +0100 | [diff] [blame] | 78 | *         \p *p_slot contains a pointer to the key slot containing the | 
|  | 79 | *         description of the key identified by \p key. | 
|  | 80 | *         The key slot counter has been incremented. | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 81 | * \retval #PSA_ERROR_BAD_STATE | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 82 | *         The library has not been initialized. | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 83 | * \retval #PSA_ERROR_INVALID_HANDLE | 
|  | 84 | *         \p key is not a valid key identifier. | 
|  | 85 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY | 
|  | 86 | *         \p key is a persistent key identifier. The implementation does not | 
|  | 87 | *         have sufficient resources to load the persistent key. This can be | 
|  | 88 | *         due to a lack of empty key slot, or available memory. | 
|  | 89 | * \retval #PSA_ERROR_DOES_NOT_EXIST | 
|  | 90 | *         There is no key with key identifier \p key. | 
|  | 91 | * \retval #PSA_ERROR_CORRUPTION_DETECTED | 
|  | 92 | * \retval #PSA_ERROR_STORAGE_FAILURE | 
|  | 93 | * \retval #PSA_ERROR_DATA_CORRUPT | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 94 | */ | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 95 | psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key, | 
|  | 96 | psa_key_slot_t **p_slot ); | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 97 |  | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 98 | /** Initialize the key slot structures. | 
|  | 99 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 100 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 101 | *         Currently this function always succeeds. | 
|  | 102 | */ | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 103 | psa_status_t psa_initialize_key_slots( void ); | 
|  | 104 |  | 
| Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 105 | /** Delete all data from key slots in memory. | 
|  | 106 | * | 
|  | 107 | * This does not affect persistent storage. */ | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 108 | void psa_wipe_all_key_slots( void ); | 
|  | 109 |  | 
| Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 110 | /** Find a free key slot. | 
|  | 111 | * | 
|  | 112 | * This function returns a key slot that is available for use and is in its | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 113 | * ground state (all-bits-zero). On success, the key slot is locked. It is | 
|  | 114 | * the responsibility of the caller to unlock the key slot when it does not | 
|  | 115 | * access it anymore. | 
| Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 116 | * | 
| Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 117 | * \param[out] volatile_key_id   On success, volatile key identifier | 
|  | 118 | *                               associated to the returned slot. | 
|  | 119 | * \param[out] p_slot            On success, a pointer to the slot. | 
| Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 120 | * | 
|  | 121 | * \retval #PSA_SUCCESS | 
|  | 122 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY | 
| Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 123 | * \retval #PSA_ERROR_BAD_STATE | 
| Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 124 | */ | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 125 | psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id, | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 126 | psa_key_slot_t **p_slot ); | 
| Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 127 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 128 | /** Lock a key slot. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 129 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 130 | * This function increments the key slot lock counter by one. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 131 | * | 
|  | 132 | * \param[in] slot  The key slot. | 
| Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 133 | * | 
|  | 134 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 135 | The key slot lock counter was incremented. | 
| Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 136 | * \retval #PSA_ERROR_CORRUPTION_DETECTED | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 137 | *             The lock counter already reached its maximum value and was not | 
| Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 138 | *             increased. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 139 | */ | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 140 | static inline psa_status_t psa_lock_key_slot( psa_key_slot_t *slot ) | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 141 | { | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 142 | if( slot->lock_count >= SIZE_MAX ) | 
| Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 143 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
|  | 144 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 145 | slot->lock_count++; | 
| Ronald Cron | cbf6a1d | 2020-11-13 15:59:59 +0100 | [diff] [blame] | 146 |  | 
|  | 147 | return( PSA_SUCCESS ); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 150 | /** Unlock a key slot. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 151 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 152 | * This function decrements the key slot lock counter by one. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 153 | * | 
|  | 154 | * \note To ease the handling of errors in retrieving a key slot | 
|  | 155 | *       a NULL input pointer is valid, and the function returns | 
|  | 156 | *       successfully without doing anything in that case. | 
|  | 157 | * | 
|  | 158 | * \param[in] slot  The key slot. | 
|  | 159 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 160 | *             \p slot is NULL or the key slot lock counter has been | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 161 | *             decremented successfully. | 
|  | 162 | * \retval #PSA_ERROR_CORRUPTION_DETECTED | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 163 | *             The lock counter was equal to 0. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 164 | * | 
|  | 165 | */ | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 166 | psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot ); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 167 |  | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 168 | /** Test whether a lifetime designates a key in an external cryptoprocessor. | 
|  | 169 | * | 
|  | 170 | * \param lifetime      The lifetime to test. | 
|  | 171 | * | 
|  | 172 | * \retval 1 | 
|  | 173 | *         The lifetime designates an external key. There should be a | 
|  | 174 | *         registered driver for this lifetime, otherwise the key cannot | 
|  | 175 | *         be created or manipulated. | 
|  | 176 | * \retval 0 | 
|  | 177 | *         The lifetime designates a key that is volatile or in internal | 
|  | 178 | *         storage. | 
|  | 179 | */ | 
|  | 180 | static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime ) | 
|  | 181 | { | 
| Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 182 | return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) | 
|  | 183 | != PSA_KEY_LOCATION_LOCAL_STORAGE ); | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 186 | /** Validate a key's location. | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 187 | * | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 188 | * This function checks whether the key's attributes point to a location that | 
|  | 189 | * is known to the PSA Core, and returns the driver function table if the key | 
|  | 190 | * is to be found in an external location. | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 191 | * | 
| Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 192 | * \param[in] lifetime      The key lifetime attribute. | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 193 | * \param[out] p_drv        On success, when a key is located in external | 
|  | 194 | *                          storage, returns a pointer to the driver table | 
|  | 195 | *                          associated with the key's storage location. | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 196 | * | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 197 | * \retval #PSA_SUCCESS | 
|  | 198 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 199 | */ | 
| Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 200 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 201 | psa_se_drv_table_entry_t **p_drv ); | 
|  | 202 |  | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 203 | /** Validate the persistence of a key. | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 204 | * | 
| Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 205 | * \param[in] lifetime  The key lifetime attribute. | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 206 | * | 
|  | 207 | * \retval #PSA_SUCCESS | 
| Ronald Cron | de825e6 | 2021-04-01 13:59:10 +0200 | [diff] [blame] | 208 | * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys | 
|  | 209 | *             are not supported. | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 210 | */ | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 211 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime ); | 
|  | 212 |  | 
|  | 213 | /** Validate a key identifier. | 
|  | 214 | * | 
| Ronald Cron | fc9c556 | 2020-10-15 19:24:49 +0200 | [diff] [blame] | 215 | * \param[in] key           The key identifier. | 
|  | 216 | * \param[in] vendor_ok     Non-zero to indicate that key identifiers in the | 
|  | 217 | *                          vendor range are allowed, volatile key identifiers | 
|  | 218 | *                          excepted \c 0 otherwise. | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 219 | * | 
| Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 220 | * \retval <> 0 if the key identifier is valid, 0 otherwise. | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 221 | */ | 
| Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 222 | int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok ); | 
| Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 223 |  | 
| Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 224 | #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */ |