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 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 28 | /* Number of key slots (plus one because 0 is not used). |
| 29 | * The value is a compile-time constant for now, for simplicity. */ |
| 30 | #define PSA_KEY_SLOT_COUNT 32 |
| 31 | |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 32 | /** Range of volatile key identifiers. |
| 33 | * |
| 34 | * The last PSA_KEY_SLOT_COUNT identifiers of the implementation range |
| 35 | * of key identifiers are reserved for volatile key identifiers. |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 36 | * 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] | 37 | * index of the key slot containing the volatile key definition. |
| 38 | */ |
| 39 | |
| 40 | /** The minimum value for a volatile key identifier. |
| 41 | */ |
| 42 | #define PSA_KEY_ID_VOLATILE_MIN ( PSA_KEY_ID_VENDOR_MAX - \ |
| 43 | PSA_KEY_SLOT_COUNT + 1 ) |
| 44 | |
| 45 | /** The maximum value for a volatile key identifier. |
| 46 | */ |
| 47 | #define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX |
| 48 | |
Ronald Cron | 97c8ad5 | 2020-10-15 11:17:11 +0200 | [diff] [blame] | 49 | /** Test whether a key identifier is a volatile key identifier. |
| 50 | * |
| 51 | * \param key_id Key identifier to test. |
| 52 | * |
| 53 | * \retval 1 |
| 54 | * The key identifier is a volatile key identifier. |
| 55 | * \retval 0 |
| 56 | * The key identifier is not a volatile key identifier. |
| 57 | */ |
| 58 | static inline int psa_key_id_is_volatile( psa_key_id_t key_id ) |
| 59 | { |
| 60 | return( ( key_id >= PSA_KEY_ID_VOLATILE_MIN ) && |
| 61 | ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) ); |
| 62 | } |
| 63 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 64 | /** Retrieve the description of a key given its identifier. |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 65 | * |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 66 | * The descriptions of volatile keys and loaded persistent keys are |
| 67 | * stored in key slots. This function returns a pointer to the key slot |
| 68 | * containing the description of a key given its identifier. |
| 69 | * |
| 70 | * In case of a persistent key, the function loads the description of the key |
| 71 | * into a key slot if not already done. |
| 72 | * |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 73 | * On success, the access counter of the returned key slot is incremented by |
| 74 | * one. It is the responsibility of the caller to call |
| 75 | * psa_decrement_key_slot_access_count() when it does not access the slot |
| 76 | * anymore. |
| 77 | * |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 78 | * \param key Key identifier to query. |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 79 | * \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] | 80 | * key slot containing the description of the key |
| 81 | * identified by \p key. |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 82 | * |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 83 | * \retval #PSA_SUCCESS |
| 84 | * The pointer to the key slot containing the description of the key |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 85 | * identified by \p key was returned. The key slot counter was |
| 86 | * implemented. |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 87 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 88 | * The library has not been initialized. |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 89 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 90 | * \p key is not a valid key identifier. |
| 91 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 92 | * \p key is a persistent key identifier. The implementation does not |
| 93 | * have sufficient resources to load the persistent key. This can be |
| 94 | * due to a lack of empty key slot, or available memory. |
| 95 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 96 | * There is no key with key identifier \p key. |
| 97 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 98 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 99 | * \retval #PSA_ERROR_DATA_CORRUPT |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 100 | */ |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 101 | psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key, |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 102 | psa_key_slot_t **p_slot ); |
| 103 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 104 | /** Initialize the key slot structures. |
| 105 | * |
Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 106 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 107 | * Currently this function always succeeds. |
| 108 | */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 109 | psa_status_t psa_initialize_key_slots( void ); |
| 110 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 111 | /** Delete all data from key slots in memory. |
| 112 | * |
| 113 | * This does not affect persistent storage. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 114 | void psa_wipe_all_key_slots( void ); |
| 115 | |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 116 | /** Find a free key slot. |
| 117 | * |
| 118 | * This function returns a key slot that is available for use and is in its |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 119 | * ground state (all-bits-zero). On success, the access counter of the |
| 120 | * returned key slot is incremented by one. It is the responsibility of the |
| 121 | * caller to call psa_decrement_key_slot_access_count() when it does not access |
| 122 | * the key slot anymore. |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 123 | * |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 124 | * \param[out] volatile_key_id On success, volatile key identifier |
| 125 | * associated to the returned slot. |
| 126 | * \param[out] p_slot On success, a pointer to the slot. |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 127 | * |
| 128 | * \retval #PSA_SUCCESS |
| 129 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 130 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 131 | */ |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 132 | 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] | 133 | psa_key_slot_t **p_slot ); |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 134 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 135 | /** Increment slot access counter. |
| 136 | * |
| 137 | * This function increments the slot access counter by one. |
| 138 | * |
| 139 | * \param[in] slot The key slot. |
| 140 | */ |
| 141 | static inline void psa_increment_key_slot_access_count( psa_key_slot_t *slot ) |
| 142 | { |
| 143 | slot->access_count++; |
| 144 | } |
| 145 | |
| 146 | /** Decrement slot access counter. |
| 147 | * |
| 148 | * This function decrements the slot access counter by one. |
| 149 | * |
| 150 | * \note To ease the handling of errors in retrieving a key slot |
| 151 | * a NULL input pointer is valid, and the function returns |
| 152 | * successfully without doing anything in that case. |
| 153 | * |
| 154 | * \param[in] slot The key slot. |
| 155 | * \retval #PSA_SUCCESS |
| 156 | * \p slot is NULL or the key slot access pointer has been |
| 157 | * decremented successfully. |
| 158 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 159 | * The access counter was equal to 0. |
| 160 | * |
| 161 | */ |
| 162 | psa_status_t psa_decrement_key_slot_access_count( psa_key_slot_t *slot ); |
| 163 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 164 | /** Test whether a lifetime designates a key in an external cryptoprocessor. |
| 165 | * |
| 166 | * \param lifetime The lifetime to test. |
| 167 | * |
| 168 | * \retval 1 |
| 169 | * The lifetime designates an external key. There should be a |
| 170 | * registered driver for this lifetime, otherwise the key cannot |
| 171 | * be created or manipulated. |
| 172 | * \retval 0 |
| 173 | * The lifetime designates a key that is volatile or in internal |
| 174 | * storage. |
| 175 | */ |
| 176 | static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime ) |
| 177 | { |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 178 | return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) |
| 179 | != PSA_KEY_LOCATION_LOCAL_STORAGE ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 182 | /** Validate a key's location. |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 183 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 184 | * This function checks whether the key's attributes point to a location that |
| 185 | * is known to the PSA Core, and returns the driver function table if the key |
| 186 | * is to be found in an external location. |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 187 | * |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 188 | * \param[in] lifetime The key lifetime attribute. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 189 | * \param[out] p_drv On success, when a key is located in external |
| 190 | * storage, returns a pointer to the driver table |
| 191 | * associated with the key's storage location. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 192 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 193 | * \retval #PSA_SUCCESS |
| 194 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 195 | */ |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 196 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 197 | psa_se_drv_table_entry_t **p_drv ); |
| 198 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 199 | /** Validate the persistence of a key. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 200 | * |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 201 | * \param[in] lifetime The key lifetime attribute. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 202 | * |
| 203 | * \retval #PSA_SUCCESS |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 204 | * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent |
| 205 | * keys are not supported. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 206 | */ |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 207 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime ); |
| 208 | |
| 209 | /** Validate a key identifier. |
| 210 | * |
Ronald Cron | fc9c556 | 2020-10-15 19:24:49 +0200 | [diff] [blame] | 211 | * \param[in] key The key identifier. |
| 212 | * \param[in] vendor_ok Non-zero to indicate that key identifiers in the |
| 213 | * vendor range are allowed, volatile key identifiers |
| 214 | * excepted \c 0 otherwise. |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 215 | * |
| 216 | * \retval #PSA_SUCCESS The identifier is valid. |
| 217 | * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid. |
| 218 | */ |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame^] | 219 | psa_status_t psa_validate_key_id( mbedtls_svc_key_id_t key, int vendor_ok ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 220 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 221 | #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */ |