blob: a84be7d8374659d62136f12e971bd642b774706c [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 *
Steven Cooreman863470a2021-02-15 14:03:19 +010018 * The last #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation
19 * range of key identifiers are reserved for volatile key identifiers.
Ronald Cron96783552020-10-19 12:06:30 +020020 * A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the
Ronald Cron2a993152020-07-17 14:13:26 +020021 * index of the key slot containing the volatile key definition.
22 */
23
24/** The minimum value for a volatile key identifier.
25 */
Gilles Peskine449bd832023-01-11 14:50:10 +010026#define PSA_KEY_ID_VOLATILE_MIN (PSA_KEY_ID_VENDOR_MAX - \
27 MBEDTLS_PSA_KEY_SLOT_COUNT + 1)
Ronald Cron2a993152020-07-17 14:13:26 +020028
29/** The maximum value for a volatile key identifier.
30 */
31#define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
32
Ronald Cron97c8ad52020-10-15 11:17:11 +020033/** Test whether a key identifier is a volatile key identifier.
34 *
35 * \param key_id Key identifier to test.
36 *
37 * \retval 1
38 * The key identifier is a volatile key identifier.
39 * \retval 0
40 * The key identifier is not a volatile key identifier.
41 */
Gilles Peskine449bd832023-01-11 14:50:10 +010042static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
Ronald Cron97c8ad52020-10-15 11:17:11 +020043{
Gilles Peskine449bd832023-01-11 14:50:10 +010044 return (key_id >= PSA_KEY_ID_VOLATILE_MIN) &&
45 (key_id <= PSA_KEY_ID_VOLATILE_MAX);
Ronald Cron97c8ad52020-10-15 11:17:11 +020046}
47
Ronald Cron5c522922020-11-14 16:35:34 +010048/** Get the description of a key given its identifier and lock it.
Gilles Peskine09829032018-12-10 17:00:38 +010049 *
Ronald Cron5c522922020-11-14 16:35:34 +010050 * The descriptions of volatile keys and loaded persistent keys are stored in
51 * key slots. This function returns a pointer to the key slot containing the
52 * description of a key given its identifier.
Ronald Cronc4d1b512020-07-31 11:26:37 +020053 *
Ronald Cron5c522922020-11-14 16:35:34 +010054 * In case of a persistent key, the function loads the description of the key
55 * into a key slot if not already done.
Ronald Cronc4d1b512020-07-31 11:26:37 +020056 *
Ryan Everett098c6652024-01-03 13:03:36 +000057 * On success, the returned key slot has been registered for reading.
58 * It is the responsibility of the caller to call psa_unregister_read(slot)
59 * when they have finished reading the contents of the slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +020060 *
Ryan Everett231f15b2024-04-29 18:26:19 +010061 * On failure, `*p_slot` is set to NULL. This ensures that it is always valid
62 * to call psa_unregister_read on the returned slot.
63 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020064 * \param key Key identifier to query.
Gilles Peskine09829032018-12-10 17:00:38 +010065 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
Ronald Cronc4d1b512020-07-31 11:26:37 +020066 * key slot containing the description of the key
67 * identified by \p key.
Gilles Peskine09829032018-12-10 17:00:38 +010068 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020069 * \retval #PSA_SUCCESS
Ronald Cron1d12d872020-11-18 17:21:22 +010070 * \p *p_slot contains a pointer to the key slot containing the
71 * description of the key identified by \p key.
72 * The key slot counter has been incremented.
Ronald Cronc4d1b512020-07-31 11:26:37 +020073 * \retval #PSA_ERROR_BAD_STATE
Ryan Everettdfe8bf82024-01-12 17:45:05 +000074 * The library has not been initialized.
Ronald Cronc4d1b512020-07-31 11:26:37 +020075 * \retval #PSA_ERROR_INVALID_HANDLE
76 * \p key is not a valid key identifier.
77 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
78 * \p key is a persistent key identifier. The implementation does not
79 * have sufficient resources to load the persistent key. This can be
80 * due to a lack of empty key slot, or available memory.
81 * \retval #PSA_ERROR_DOES_NOT_EXIST
82 * There is no key with key identifier \p key.
Gilles Peskineed733552023-02-14 19:21:09 +010083 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
84 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
85 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskine09829032018-12-10 17:00:38 +010086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
88 psa_key_slot_t **p_slot);
Gilles Peskine66fb1262018-12-10 16:29:04 +010089
Gilles Peskine09829032018-12-10 17:00:38 +010090/** Initialize the key slot structures.
91 *
Ronald Cron96783552020-10-19 12:06:30 +020092 * \retval #PSA_SUCCESS
Gilles Peskine09829032018-12-10 17:00:38 +010093 * Currently this function always succeeds.
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095psa_status_t psa_initialize_key_slots(void);
Gilles Peskine66fb1262018-12-10 16:29:04 +010096
Gilles Peskine09829032018-12-10 17:00:38 +010097/** Delete all data from key slots in memory.
Ryan Everett16abd592024-01-24 17:37:46 +000098 * This function is not thread safe, it wipes every key slot regardless of
99 * state and reader count. It should only be called when no slot is in use.
Gilles Peskine09829032018-12-10 17:00:38 +0100100 *
101 * This does not affect persistent storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void psa_wipe_all_key_slots(void);
Gilles Peskine66fb1262018-12-10 16:29:04 +0100103
Ryan Everett2afb5162023-12-22 15:59:45 +0000104/** Find a free key slot and reserve it to be filled with a key.
Gilles Peskine41e50d22019-07-31 15:01:55 +0200105 *
Ryan Everett2afb5162023-12-22 15:59:45 +0000106 * This function finds a key slot that is free,
107 * sets its state to PSA_SLOT_FILLING and then returns the slot.
108 *
109 * On success, the key slot's state is PSA_SLOT_FILLING.
110 * It is the responsibility of the caller to change the slot's state to
111 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200112 *
Ryan Everett3d8118d2024-01-30 16:58:47 +0000113 * If multi-threading is enabled, the caller must hold the
114 * global key slot mutex.
115 *
Ronald Cron2a993152020-07-17 14:13:26 +0200116 * \param[out] volatile_key_id On success, volatile key identifier
117 * associated to the returned slot.
118 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200119 *
Gilles Peskineed733552023-02-14 19:21:09 +0100120 * \retval #PSA_SUCCESS \emptydescription
Ryan Everett2afb5162023-12-22 15:59:45 +0000121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
122 * There were no free key slots.
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000123 * \retval #PSA_ERROR_BAD_STATE \emptydescription
124 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett2afb5162023-12-22 15:59:45 +0000125 * This function attempted to operate on a key slot which was in an
126 * unexpected state.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200127 */
Ryan Everett2afb5162023-12-22 15:59:45 +0000128psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
129 psa_key_slot_t **p_slot);
130
Ryan Everett4a782772024-01-04 10:53:26 +0000131/** Change the state of a key slot.
132 *
133 * This function changes the state of the key slot from expected_state to
134 * new state. If the state of the slot was not expected_state, the state is
135 * unchanged.
136 *
Ryan Everettfb02d572024-01-08 11:13:03 +0000137 * If multi-threading is enabled, the caller must hold the
138 * global key slot mutex.
139 *
Ryan Everett4a782772024-01-04 10:53:26 +0000140 * \param[in] slot The key slot.
141 * \param[in] expected_state The current state of the slot.
142 * \param[in] new_state The new state of the slot.
143 *
144 * \retval #PSA_SUCCESS
145 The key slot's state variable is new_state.
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000146 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett4a782772024-01-04 10:53:26 +0000147 * The slot's state was not expected_state.
148 */
149static inline psa_status_t psa_key_slot_state_transition(
150 psa_key_slot_t *slot, psa_key_slot_state_t expected_state,
151 psa_key_slot_state_t new_state)
152{
153 if (slot->state != expected_state) {
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000154 return PSA_ERROR_CORRUPTION_DETECTED;
Ryan Everett4a782772024-01-04 10:53:26 +0000155 }
156 slot->state = new_state;
157 return PSA_SUCCESS;
158}
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200159
Ryan Everett39cc9d72023-12-21 17:57:14 +0000160/** Register as a reader of a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200161 *
Ryan Everett39cc9d72023-12-21 17:57:14 +0000162 * This function increments the key slot registered reader counter by one.
Ryan Everettfb02d572024-01-08 11:13:03 +0000163 * If multi-threading is enabled, the caller must hold the
164 * global key slot mutex.
Ryan Everett7aeacc12024-01-19 13:02:58 +0000165 *
Ronald Cronf95a2b12020-10-22 15:24:49 +0200166 * \param[in] slot The key slot.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100167 *
168 * \retval #PSA_SUCCESS
Ryan Everett39cc9d72023-12-21 17:57:14 +0000169 The key slot registered reader counter was incremented.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100170 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett39cc9d72023-12-21 17:57:14 +0000171 * The reader counter already reached its maximum value and was not
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000172 * increased, or the slot's state was not PSA_SLOT_FULL.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200173 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000174static inline psa_status_t psa_register_read(psa_key_slot_t *slot)
Ronald Cronf95a2b12020-10-22 15:24:49 +0200175{
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000176 if ((slot->state != PSA_SLOT_FULL) ||
177 (slot->registered_readers >= SIZE_MAX)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_CORRUPTION_DETECTED;
179 }
Ryan Everett39cc9d72023-12-21 17:57:14 +0000180 slot->registered_readers++;
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200183}
184
Ryan Everett39cc9d72023-12-21 17:57:14 +0000185/** Unregister from reading a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200186 *
Ryan Everett39cc9d72023-12-21 17:57:14 +0000187 * This function decrements the key slot registered reader counter by one.
188 * If the state of the slot is PSA_SLOT_PENDING_DELETION,
189 * and there is only one registered reader (the caller),
Ryan Everett558da2f2024-01-19 12:59:28 +0000190 * this function will call psa_wipe_key_slot().
Ryan Everettfb02d572024-01-08 11:13:03 +0000191 * If multi-threading is enabled, the caller must hold the
192 * global key slot mutex.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200193 *
194 * \note To ease the handling of errors in retrieving a key slot
195 * a NULL input pointer is valid, and the function returns
196 * successfully without doing anything in that case.
197 *
198 * \param[in] slot The key slot.
199 * \retval #PSA_SUCCESS
Ryan Everett39cc9d72023-12-21 17:57:14 +0000200 * \p slot is NULL or the key slot reader counter has been
201 * decremented (and potentially wiped) successfully.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200202 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ryan Everett39cc9d72023-12-21 17:57:14 +0000203 * The slot's state was neither PSA_SLOT_FULL nor
Ryan Everettdfe8bf82024-01-12 17:45:05 +0000204 * PSA_SLOT_PENDING_DELETION.
205 * Or a wipe was attempted and the slot's state was not
206 * PSA_SLOT_PENDING_DELETION.
207 * Or registered_readers was equal to 0.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200208 */
Ryan Everett39cc9d72023-12-21 17:57:14 +0000209psa_status_t psa_unregister_read(psa_key_slot_t *slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200210
Ryan Everetteb1722a2024-01-31 13:36:39 +0000211/** Wrap a call to psa_unregister_read in the global key slot mutex.
212 *
213 * If threading is disabled, this simply calls psa_unregister_read.
214 *
215 * \note To ease the handling of errors in retrieving a key slot
216 * a NULL input pointer is valid, and the function returns
217 * successfully without doing anything in that case.
218 *
219 * \param[in] slot The key slot.
220 * \retval #PSA_SUCCESS
221 * \p slot is NULL or the key slot reader counter has been
222 * decremented (and potentially wiped) successfully.
223 * \retval #PSA_ERROR_CORRUPTION_DETECTED
224 * The slot's state was neither PSA_SLOT_FULL nor
225 * PSA_SLOT_PENDING_DELETION.
226 * Or a wipe was attempted and the slot's state was not
227 * PSA_SLOT_PENDING_DELETION.
228 * Or registered_readers was equal to 0.
229 */
230psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot);
231
Gilles Peskine011e4282019-06-26 18:34:38 +0200232/** Test whether a lifetime designates a key in an external cryptoprocessor.
233 *
234 * \param lifetime The lifetime to test.
235 *
236 * \retval 1
237 * The lifetime designates an external key. There should be a
238 * registered driver for this lifetime, otherwise the key cannot
239 * be created or manipulated.
240 * \retval 0
241 * The lifetime designates a key that is volatile or in internal
242 * storage.
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
Gilles Peskine011e4282019-06-26 18:34:38 +0200245{
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
247 != PSA_KEY_LOCATION_LOCAL_STORAGE;
Gilles Peskine011e4282019-06-26 18:34:38 +0200248}
249
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200250/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +0200251 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200252 * This function checks whether the key's attributes point to a location that
253 * is known to the PSA Core, and returns the driver function table if the key
254 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200255 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200256 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200257 * \param[out] p_drv On success, when a key is located in external
258 * storage, returns a pointer to the driver table
259 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200260 *
Gilles Peskineed733552023-02-14 19:21:09 +0100261 * \retval #PSA_SUCCESS \emptydescription
262 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Gilles Peskined167b942019-04-19 18:19:40 +0200263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
265 psa_se_drv_table_entry_t **p_drv);
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200266
Ronald Crond2ed4812020-07-17 16:11:30 +0200267/** Validate the persistence of a key.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200268 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200269 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200270 *
Gilles Peskineed733552023-02-14 19:21:09 +0100271 * \retval #PSA_SUCCESS \emptydescription
Ronald Cronde825e62021-04-01 13:59:10 +0200272 * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
273 * are not supported.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime);
Ronald Crond2ed4812020-07-17 16:11:30 +0200276
277/** Validate a key identifier.
278 *
Ronald Cronfc9c5562020-10-15 19:24:49 +0200279 * \param[in] key The key identifier.
280 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
281 * vendor range are allowed, volatile key identifiers
282 * excepted \c 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200283 *
Ronald Cron77e412c2021-03-31 17:36:31 +0200284 * \retval <> 0 if the key identifier is valid, 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok);
Gilles Peskined167b942019-04-19 18:19:40 +0200287
Gilles Peskine961849f2018-11-30 18:54:54 +0100288#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */