blob: 1e6e935b59495ba4cb7b4c2a823129e51f5fb070 [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine961849f2018-11-30 18:54:54 +01007 */
8
9#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
10#define PSA_CRYPTO_SLOT_MANAGEMENT_H
11
Gilles Peskine011e4282019-06-26 18:34:38 +020012#include "psa/crypto.h"
Ronald Cronc4d1b512020-07-31 11:26:37 +020013#include "psa_crypto_core.h"
Gilles Peskine011e4282019-06-26 18:34:38 +020014#include "psa_crypto_se.h"
15
Ronald Cron2a993152020-07-17 14:13:26 +020016/** Range of volatile key identifiers.
17 *
Gilles Peskine7dea0962024-07-16 21:24:05 +020018 * The first #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation
Steven Cooreman863470a2021-02-15 14:03:19 +010019 * range of key identifiers are reserved for volatile key identifiers.
Gilles Peskinee8199f52024-06-10 11:53:33 +020020 *
21 * If \c id is a a volatile key identifier, #PSA_KEY_ID_VOLATILE_MIN - \c id
22 * indicates the key slot containing the volatile key definition. See
23 * psa_crypto_slot_management.c for details.
Ronald Cron2a993152020-07-17 14:13:26 +020024 */
25
26/** The minimum value for a volatile key identifier.
27 */
Gilles Peskine7dea0962024-07-16 21:24:05 +020028#define PSA_KEY_ID_VOLATILE_MIN PSA_KEY_ID_VENDOR_MIN
Ronald Cron2a993152020-07-17 14:13:26 +020029
30/** The maximum value for a volatile key identifier.
31 */
Gilles Peskinee8199f52024-06-10 11:53:33 +020032#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
33#define PSA_KEY_ID_VOLATILE_MAX (MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1)
34#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Gilles Peskine7dea0962024-07-16 21:24:05 +020035#define PSA_KEY_ID_VOLATILE_MAX \
36 (PSA_KEY_ID_VOLATILE_MIN + MBEDTLS_PSA_KEY_SLOT_COUNT - 1)
Gilles Peskinee8199f52024-06-10 11:53:33 +020037#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
Ronald Cron2a993152020-07-17 14:13:26 +020038
Ronald Cron97c8ad52020-10-15 11:17:11 +020039/** Test whether a key identifier is a volatile key identifier.
40 *
41 * \param key_id Key identifier to test.
42 *
43 * \retval 1
44 * The key identifier is a volatile key identifier.
45 * \retval 0
46 * The key identifier is not a volatile key identifier.
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
Ronald Cron97c8ad52020-10-15 11:17:11 +020049{
Gilles Peskine449bd832023-01-11 14:50:10 +010050 return (key_id >= PSA_KEY_ID_VOLATILE_MIN) &&
51 (key_id <= PSA_KEY_ID_VOLATILE_MAX);
Ronald Cron97c8ad52020-10-15 11:17:11 +020052}
53
Ronald Cron5c522922020-11-14 16:35:34 +010054/** Get the description of a key given its identifier and lock it.
Gilles Peskine09829032018-12-10 17:00:38 +010055 *
Ronald Cron5c522922020-11-14 16:35:34 +010056 * The descriptions of volatile keys and loaded persistent keys are stored in
57 * key slots. This function returns a pointer to the key slot containing the
58 * description of a key given its identifier.
Ronald Cronc4d1b512020-07-31 11:26:37 +020059 *
Ronald Cron5c522922020-11-14 16:35:34 +010060 * In case of a persistent key, the function loads the description of the key
61 * into a key slot if not already done.
Ronald Cronc4d1b512020-07-31 11:26:37 +020062 *
Ryan Everett098c6652024-01-03 13:03:36 +000063 * On success, the returned key slot has been registered for reading.
64 * It is the responsibility of the caller to call psa_unregister_read(slot)
65 * when they have finished reading the contents of the slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +020066 *
Ryan Everett231f15b2024-04-29 18:26:19 +010067 * On failure, `*p_slot` is set to NULL. This ensures that it is always valid
68 * to call psa_unregister_read on the returned slot.
69 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020070 * \param key Key identifier to query.
Gilles Peskine09829032018-12-10 17:00:38 +010071 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
Ronald Cronc4d1b512020-07-31 11:26:37 +020072 * key slot containing the description of the key
73 * identified by \p key.
Gilles Peskine09829032018-12-10 17:00:38 +010074 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020075 * \retval #PSA_SUCCESS
Ronald Cron1d12d872020-11-18 17:21:22 +010076 * \p *p_slot contains a pointer to the key slot containing the
77 * description of the key identified by \p key.
78 * The key slot counter has been incremented.
Ronald Cronc4d1b512020-07-31 11:26:37 +020079 * \retval #PSA_ERROR_BAD_STATE
Ryan Everettdfe8bf82024-01-12 17:45:05 +000080 * The library has not been initialized.
Ronald Cronc4d1b512020-07-31 11:26:37 +020081 * \retval #PSA_ERROR_INVALID_HANDLE
82 * \p key is not a valid key identifier.
83 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
84 * \p key is a persistent key identifier. The implementation does not
85 * have sufficient resources to load the persistent key. This can be
86 * due to a lack of empty key slot, or available memory.
87 * \retval #PSA_ERROR_DOES_NOT_EXIST
88 * There is no key with key identifier \p key.
Gilles Peskineed733552023-02-14 19:21:09 +010089 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
90 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
91 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskine09829032018-12-10 17:00:38 +010092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
94 psa_key_slot_t **p_slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +010095
Gilles Peskine09829032018-12-10 17:00:38 +010096/** Initialize the key slot structures.
97 *
Ronald Cron96783552020-10-19 12:06:30 +020098 * \retval #PSA_SUCCESS
Gilles Peskine09829032018-12-10 17:00:38 +010099 * Currently this function always succeeds.
100 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101psa_status_t psa_initialize_key_slots(void);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100102
Gilles Peskine09829032018-12-10 17:00:38 +0100103/** Delete all data from key slots in memory.
Ryan Everett16abd592024-01-24 17:37:46 +0000104 * This function is not thread safe, it wipes every key slot regardless of
105 * state and reader count. It should only be called when no slot is in use.
Gilles Peskine09829032018-12-10 17:00:38 +0100106 *
107 * This does not affect persistent storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void psa_wipe_all_key_slots(void);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100109
Ryan Everett2afb5162023-12-22 15:59:45 +0000110/** Find a free key slot and reserve it to be filled with a key.
Gilles Peskine41e50d22019-07-31 15:01:55 +0200111 *
Ryan Everett2afb5162023-12-22 15:59:45 +0000112 * This function finds a key slot that is free,
113 * sets its state to PSA_SLOT_FILLING and then returns the slot.
114 *
115 * On success, the key slot's state is PSA_SLOT_FILLING.
116 * It is the responsibility of the caller to change the slot's state to
117 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200118 *
Ryan Everett3d8118d2024-01-30 16:58:47 +0000119 * If multi-threading is enabled, the caller must hold the
120 * global key slot mutex.
121 *
Gilles Peskinee8199f52024-06-10 11:53:33 +0200122 * \param[out] volatile_key_id If null, reserve a cache slot for
123 * a persistent or built-in key.
124 * If non-null, allocate a slot for
125 * a volatile key.
126 * If non-null, on success, the volatile key
127 * identifier corresponding with the
128 * returned slot.
Ronald Cron2a993152020-07-17 14:13:26 +0200129 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200130 *
Gilles Peskineed733552023-02-14 19:21:09 +0100131 * \retval #PSA_SUCCESS \emptydescription
Ryan Everett2afb5162023-12-22 15:59:45 +0000132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
133 * There were no free key slots.
Gilles Peskinee8199f52024-06-10 11:53:33 +0200134 * When #MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, there was not
135 * enough memory to allocate more slots.
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000136 * \retval #PSA_ERROR_BAD_STATE \emptydescription
137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett2afb5162023-12-22 15:59:45 +0000138 * This function attempted to operate on a key slot which was in an
139 * unexpected state.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200140 */
Ryan Everett2afb5162023-12-22 15:59:45 +0000141psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
142 psa_key_slot_t **p_slot);
143
Gilles Peskinee8199f52024-06-10 11:53:33 +0200144#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
145/** Return a key slot to the free list.
146 *
147 * Call this function when a slot obtained from psa_reserve_free_key_slot()
148 * is no longer in use.
149 *
150 * If multi-threading is enabled, the caller must hold the
151 * global key slot mutex.
152 *
153 * \param slice_idx The slice containing the slot.
154 * This is `slot->slice_index` when the slot
155 * is obtained from psa_reserve_free_key_slot().
156 * \param slot The key slot.
157 *
158 * \retval #PSA_SUCCESS \emptydescription
159 * \retval #PSA_ERROR_CORRUPTION_DETECTED
160 * This function attempted to operate on a key slot which was in an
161 * unexpected state.
162 */
163psa_status_t psa_free_key_slot(size_t slice_idx,
164 psa_key_slot_t *slot);
165#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
166
Ryan Everett4a782772024-01-04 10:53:26 +0000167/** Change the state of a key slot.
168 *
169 * This function changes the state of the key slot from expected_state to
170 * new state. If the state of the slot was not expected_state, the state is
171 * unchanged.
172 *
Ryan Everettfb02d572024-01-08 11:13:03 +0000173 * If multi-threading is enabled, the caller must hold the
174 * global key slot mutex.
175 *
Ryan Everett4a782772024-01-04 10:53:26 +0000176 * \param[in] slot The key slot.
177 * \param[in] expected_state The current state of the slot.
178 * \param[in] new_state The new state of the slot.
179 *
180 * \retval #PSA_SUCCESS
181 The key slot's state variable is new_state.
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000182 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett4a782772024-01-04 10:53:26 +0000183 * The slot's state was not expected_state.
184 */
185static inline psa_status_t psa_key_slot_state_transition(
186 psa_key_slot_t *slot, psa_key_slot_state_t expected_state,
187 psa_key_slot_state_t new_state)
188{
189 if (slot->state != expected_state) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000190 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett4a782772024-01-04 10:53:26 +0000191 }
192 slot->state = new_state;
193 return PSA_SUCCESS;
194}
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200195
Ryan Everett39cc9d72023-12-21 17:57:14 +0000196/** Register as a reader of a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200197 *
Ryan Everett39cc9d72023-12-21 17:57:14 +0000198 * This function increments the key slot registered reader counter by one.
Ryan Everettfb02d572024-01-08 11:13:03 +0000199 * If multi-threading is enabled, the caller must hold the
200 * global key slot mutex.
Ryan Everett7aeacc12024-01-19 13:02:58 +0000201 *
Ronald Cronf95a2b12020-10-22 15:24:49 +0200202 * \param[in] slot The key slot.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100203 *
204 * \retval #PSA_SUCCESS
Ryan Everett39cc9d72023-12-21 17:57:14 +0000205 The key slot registered reader counter was incremented.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100206 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett39cc9d72023-12-21 17:57:14 +0000207 * The reader counter already reached its maximum value and was not
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000208 * increased, or the slot's state was not PSA_SLOT_FULL.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200209 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000210static inline psa_status_t psa_register_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200211{
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000212 if ((slot->state != PSA_SLOT_FULL) ||
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200213 (slot->var.occupied.registered_readers >= SIZE_MAX)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_CORRUPTION_DETECTED;
215 }
Gilles Peskine47ad2f72024-06-10 11:42:41 +0200216 slot->var.occupied.registered_readers++;
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200219}
220
Ryan Everett39cc9d72023-12-21 17:57:14 +0000221/** Unregister from reading a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200222 *
Ryan Everett39cc9d72023-12-21 17:57:14 +0000223 * This function decrements the key slot registered reader counter by one.
224 * If the state of the slot is PSA_SLOT_PENDING_DELETION,
225 * and there is only one registered reader (the caller),
Ryan Everett558da2f2024-01-19 12:59:28 +0000226 * this function will call psa_wipe_key_slot().
Ryan Everettfb02d572024-01-08 11:13:03 +0000227 * If multi-threading is enabled, the caller must hold the
228 * global key slot mutex.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200229 *
230 * \note To ease the handling of errors in retrieving a key slot
231 * a NULL input pointer is valid, and the function returns
232 * successfully without doing anything in that case.
233 *
234 * \param[in] slot The key slot.
235 * \retval #PSA_SUCCESS
Ryan Everett39cc9d72023-12-21 17:57:14 +0000236 * \p slot is NULL or the key slot reader counter has been
237 * decremented (and potentially wiped) successfully.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200238 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett39cc9d72023-12-21 17:57:14 +0000239 * The slot's state was neither PSA_SLOT_FULL nor
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000240 * PSA_SLOT_PENDING_DELETION.
241 * Or a wipe was attempted and the slot's state was not
242 * PSA_SLOT_PENDING_DELETION.
243 * Or registered_readers was equal to 0.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200244 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000245psa_status_t psa_unregister_read(psa_key_slot_t *slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200246
Ryan Everetteb1722a2024-01-31 13:36:39 +0000247/** Wrap a call to psa_unregister_read in the global key slot mutex.
248 *
249 * If threading is disabled, this simply calls psa_unregister_read.
250 *
251 * \note To ease the handling of errors in retrieving a key slot
252 * a NULL input pointer is valid, and the function returns
253 * successfully without doing anything in that case.
254 *
255 * \param[in] slot The key slot.
256 * \retval #PSA_SUCCESS
257 * \p slot is NULL or the key slot reader counter has been
258 * decremented (and potentially wiped) successfully.
259 * \retval #PSA_ERROR_CORRUPTION_DETECTED
260 * The slot's state was neither PSA_SLOT_FULL nor
261 * PSA_SLOT_PENDING_DELETION.
262 * Or a wipe was attempted and the slot's state was not
263 * PSA_SLOT_PENDING_DELETION.
264 * Or registered_readers was equal to 0.
265 */
266psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot);
267
Gilles Peskine011e4282019-06-26 18:34:38 +0200268/** Test whether a lifetime designates a key in an external cryptoprocessor.
269 *
270 * \param lifetime The lifetime to test.
271 *
272 * \retval 1
273 * The lifetime designates an external key. There should be a
274 * registered driver for this lifetime, otherwise the key cannot
275 * be created or manipulated.
276 * \retval 0
277 * The lifetime designates a key that is volatile or in internal
278 * storage.
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
Gilles Peskine011e4282019-06-26 18:34:38 +0200281{
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
283 != PSA_KEY_LOCATION_LOCAL_STORAGE;
Gilles Peskine011e4282019-06-26 18:34:38 +0200284}
285
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200286/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +0200287 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200288 * This function checks whether the key's attributes point to a location that
289 * is known to the PSA Core, and returns the driver function table if the key
290 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200291 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200292 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200293 * \param[out] p_drv On success, when a key is located in external
294 * storage, returns a pointer to the driver table
295 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200296 *
Gilles Peskineed733552023-02-14 19:21:09 +0100297 * \retval #PSA_SUCCESS \emptydescription
298 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Gilles Peskined167b942019-04-19 18:19:40 +0200299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
301 psa_se_drv_table_entry_t **p_drv);
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200302
Ronald Crond2ed4812020-07-17 16:11:30 +0200303/** Validate the persistence of a key.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200304 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200305 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200306 *
Gilles Peskineed733552023-02-14 19:21:09 +0100307 * \retval #PSA_SUCCESS \emptydescription
Ronald Cronde825e62021-04-01 13:59:10 +0200308 * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
309 * are not supported.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200310 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime);
Ronald Crond2ed4812020-07-17 16:11:30 +0200312
313/** Validate a key identifier.
314 *
Ronald Cronfc9c5562020-10-15 19:24:49 +0200315 * \param[in] key The key identifier.
316 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
317 * vendor range are allowed, volatile key identifiers
318 * excepted \c 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200319 *
Ronald Cron77e412c2021-03-31 17:36:31 +0200320 * \retval <> 0 if the key identifier is valid, 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok);
Gilles Peskined167b942019-04-19 18:19:40 +0200323
Gilles Peskine961849f2018-11-30 18:54:54 +0100324#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */