2018-11-30 18:54:54 +01:00
|
|
|
/*
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-11-30 18:54:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
|
|
|
|
#define PSA_CRYPTO_SLOT_MANAGEMENT_H
|
|
|
|
|
2019-06-26 18:34:38 +02:00
|
|
|
#include "psa/crypto.h"
|
2020-07-31 11:26:37 +02:00
|
|
|
#include "psa_crypto_core.h"
|
2019-06-26 18:34:38 +02:00
|
|
|
#include "psa_crypto_se.h"
|
|
|
|
|
2020-07-17 14:13:26 +02:00
|
|
|
/** Range of volatile key identifiers.
|
|
|
|
*
|
2021-02-15 14:03:19 +01:00
|
|
|
* The last #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation
|
|
|
|
* range of key identifiers are reserved for volatile key identifiers.
|
2020-10-19 12:06:30 +02:00
|
|
|
* A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the
|
2020-07-17 14:13:26 +02:00
|
|
|
* index of the key slot containing the volatile key definition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** The minimum value for a volatile key identifier.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
#define PSA_KEY_ID_VOLATILE_MIN (PSA_KEY_ID_VENDOR_MAX - \
|
|
|
|
MBEDTLS_PSA_KEY_SLOT_COUNT + 1)
|
2020-07-17 14:13:26 +02:00
|
|
|
|
|
|
|
/** The maximum value for a volatile key identifier.
|
|
|
|
*/
|
|
|
|
#define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
|
|
|
|
|
2020-10-15 11:17:11 +02:00
|
|
|
/** Test whether a key identifier is a volatile key identifier.
|
|
|
|
*
|
|
|
|
* \param key_id Key identifier to test.
|
|
|
|
*
|
|
|
|
* \retval 1
|
|
|
|
* The key identifier is a volatile key identifier.
|
|
|
|
* \retval 0
|
|
|
|
* The key identifier is not a volatile key identifier.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
|
2020-10-15 11:17:11 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (key_id >= PSA_KEY_ID_VOLATILE_MIN) &&
|
|
|
|
(key_id <= PSA_KEY_ID_VOLATILE_MAX);
|
2020-10-15 11:17:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
/** Get the description of a key given its identifier and lock it.
|
2018-12-10 17:00:38 +01:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* The descriptions of volatile keys and loaded persistent keys are stored in
|
|
|
|
* key slots. This function returns a pointer to the key slot containing the
|
|
|
|
* description of a key given its identifier.
|
2020-07-31 11:26:37 +02:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* In case of a persistent key, the function loads the description of the key
|
|
|
|
* into a key slot if not already done.
|
2020-07-31 11:26:37 +02:00
|
|
|
*
|
2024-01-03 14:03:36 +01:00
|
|
|
* On success, the returned key slot has been registered for reading.
|
|
|
|
* It is the responsibility of the caller to call psa_unregister_read(slot)
|
|
|
|
* when they have finished reading the contents of the slot.
|
2020-10-22 15:24:49 +02:00
|
|
|
*
|
2020-07-31 11:26:37 +02:00
|
|
|
* \param key Key identifier to query.
|
2018-12-10 17:00:38 +01:00
|
|
|
* \param[out] p_slot On success, `*p_slot` contains a pointer to the
|
2020-07-31 11:26:37 +02:00
|
|
|
* key slot containing the description of the key
|
|
|
|
* identified by \p key.
|
2018-12-10 17:00:38 +01:00
|
|
|
*
|
2020-07-31 11:26:37 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
2020-11-18 17:21:22 +01:00
|
|
|
* \p *p_slot contains a pointer to the key slot containing the
|
|
|
|
* description of the key identified by \p key.
|
|
|
|
* The key slot counter has been incremented.
|
2020-07-31 11:26:37 +02:00
|
|
|
* \retval #PSA_ERROR_BAD_STATE
|
2024-01-12 18:45:05 +01:00
|
|
|
* The library has not been initialized.
|
2020-07-31 11:26:37 +02:00
|
|
|
* \retval #PSA_ERROR_INVALID_HANDLE
|
|
|
|
* \p key is not a valid key identifier.
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
|
|
|
* \p key is a persistent key identifier. The implementation does not
|
|
|
|
* have sufficient resources to load the persistent key. This can be
|
|
|
|
* due to a lack of empty key slot, or available memory.
|
|
|
|
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
|
|
|
* There is no key with key identifier \p key.
|
2023-02-14 19:21:09 +01:00
|
|
|
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
|
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
|
|
|
|
* \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
|
2018-12-10 17:00:38 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
|
|
|
|
psa_key_slot_t **p_slot);
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2018-12-10 17:00:38 +01:00
|
|
|
/** Initialize the key slot structures.
|
2024-01-08 12:10:03 +01:00
|
|
|
* If multi-threading is enabled then initialize the key slot mutex.
|
|
|
|
* This function is not thread-safe,
|
|
|
|
* if called by competing threads the key slot mutex may be initialized
|
|
|
|
* more than once.
|
2018-12-10 17:00:38 +01:00
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
2018-12-10 17:00:38 +01:00
|
|
|
* Currently this function always succeeds.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_initialize_key_slots(void);
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2018-12-10 17:00:38 +01:00
|
|
|
/** Delete all data from key slots in memory.
|
2024-01-08 12:10:03 +01:00
|
|
|
* If multi-threading is enabled then free the key slot mutex.
|
|
|
|
* This function is not thread-safe,
|
|
|
|
* if called by competing threads the key slot mutex may be freed
|
|
|
|
* more than once.
|
2018-12-10 17:00:38 +01:00
|
|
|
*
|
|
|
|
* This does not affect persistent storage. */
|
2023-01-11 14:50:10 +01:00
|
|
|
void psa_wipe_all_key_slots(void);
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2023-12-22 16:59:45 +01:00
|
|
|
/** Find a free key slot and reserve it to be filled with a key.
|
2019-07-31 15:01:55 +02:00
|
|
|
*
|
2023-12-22 16:59:45 +01:00
|
|
|
* This function finds a key slot that is free,
|
|
|
|
* sets its state to PSA_SLOT_FILLING and then returns the slot.
|
|
|
|
*
|
|
|
|
* On success, the key slot's state is PSA_SLOT_FILLING.
|
|
|
|
* It is the responsibility of the caller to change the slot's state to
|
|
|
|
* PSA_SLOT_EMPTY/FULL once key creation has finished.
|
2019-05-27 14:53:10 +02:00
|
|
|
*
|
2020-07-17 14:13:26 +02:00
|
|
|
* \param[out] volatile_key_id On success, volatile key identifier
|
|
|
|
* associated to the returned slot.
|
|
|
|
* \param[out] p_slot On success, a pointer to the slot.
|
2019-05-27 14:53:10 +02:00
|
|
|
*
|
2023-02-14 19:21:09 +01:00
|
|
|
* \retval #PSA_SUCCESS \emptydescription
|
2023-12-22 16:59:45 +01:00
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY
|
|
|
|
* There were no free key slots.
|
2024-01-12 18:45:05 +01:00
|
|
|
* \retval #PSA_ERROR_BAD_STATE \emptydescription
|
|
|
|
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
2023-12-22 16:59:45 +01:00
|
|
|
* This function attempted to operate on a key slot which was in an
|
|
|
|
* unexpected state.
|
2019-05-27 14:53:10 +02:00
|
|
|
*/
|
2023-12-22 16:59:45 +01:00
|
|
|
psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
|
|
|
|
psa_key_slot_t **p_slot);
|
|
|
|
|
2024-01-04 11:53:26 +01:00
|
|
|
/** Change the state of a key slot.
|
|
|
|
*
|
|
|
|
* This function changes the state of the key slot from expected_state to
|
|
|
|
* new state. If the state of the slot was not expected_state, the state is
|
|
|
|
* unchanged.
|
|
|
|
*
|
2024-01-08 12:13:03 +01:00
|
|
|
* If multi-threading is enabled, the caller must hold the
|
|
|
|
* global key slot mutex.
|
|
|
|
*
|
2024-01-04 11:53:26 +01:00
|
|
|
* \param[in] slot The key slot.
|
|
|
|
* \param[in] expected_state The current state of the slot.
|
|
|
|
* \param[in] new_state The new state of the slot.
|
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
The key slot's state variable is new_state.
|
2024-01-12 18:45:05 +01:00
|
|
|
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
2024-01-04 11:53:26 +01:00
|
|
|
* The slot's state was not expected_state.
|
|
|
|
*/
|
|
|
|
static inline psa_status_t psa_key_slot_state_transition(
|
|
|
|
psa_key_slot_t *slot, psa_key_slot_state_t expected_state,
|
|
|
|
psa_key_slot_state_t new_state)
|
|
|
|
{
|
|
|
|
if (slot->state != expected_state) {
|
2024-01-12 18:45:05 +01:00
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2024-01-04 11:53:26 +01:00
|
|
|
}
|
|
|
|
slot->state = new_state;
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2019-05-27 14:53:10 +02:00
|
|
|
|
2023-12-21 18:57:14 +01:00
|
|
|
/** Register as a reader of a key slot.
|
2020-10-22 15:24:49 +02:00
|
|
|
*
|
2023-12-21 18:57:14 +01:00
|
|
|
* This function increments the key slot registered reader counter by one.
|
2024-01-08 12:13:03 +01:00
|
|
|
* If multi-threading is enabled, the caller must hold the
|
|
|
|
* global key slot mutex.
|
2020-10-22 15:24:49 +02:00
|
|
|
* \param[in] slot The key slot.
|
2020-11-13 15:59:59 +01:00
|
|
|
*
|
|
|
|
* \retval #PSA_SUCCESS
|
2023-12-21 18:57:14 +01:00
|
|
|
The key slot registered reader counter was incremented.
|
2020-11-13 15:59:59 +01:00
|
|
|
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
2023-12-21 18:57:14 +01:00
|
|
|
* The reader counter already reached its maximum value and was not
|
2024-01-12 18:45:05 +01:00
|
|
|
* increased, or the slot's state was not PSA_SLOT_FULL.
|
2020-10-22 15:24:49 +02:00
|
|
|
*/
|
2023-12-21 18:57:14 +01:00
|
|
|
static inline psa_status_t psa_register_read(psa_key_slot_t *slot)
|
2020-10-22 15:24:49 +02:00
|
|
|
{
|
2024-01-12 18:45:05 +01:00
|
|
|
if ((slot->state != PSA_SLOT_FULL) ||
|
|
|
|
(slot->registered_readers >= SIZE_MAX)) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
2023-12-21 18:57:14 +01:00
|
|
|
slot->registered_readers++;
|
2020-11-13 15:59:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-10-22 15:24:49 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 18:57:14 +01:00
|
|
|
/** Unregister from reading a key slot.
|
2020-10-22 15:24:49 +02:00
|
|
|
*
|
2023-12-21 18:57:14 +01:00
|
|
|
* This function decrements the key slot registered reader counter by one.
|
|
|
|
* If the state of the slot is PSA_SLOT_PENDING_DELETION,
|
|
|
|
* and there is only one registered reader (the caller),
|
2024-01-08 12:13:03 +01:00
|
|
|
* this function will call psa_wipe_slot().
|
|
|
|
* If multi-threading is enabled, the caller must hold the
|
|
|
|
* global key slot mutex.
|
2020-10-22 15:24:49 +02:00
|
|
|
*
|
|
|
|
* \note To ease the handling of errors in retrieving a key slot
|
|
|
|
* a NULL input pointer is valid, and the function returns
|
|
|
|
* successfully without doing anything in that case.
|
|
|
|
*
|
|
|
|
* \param[in] slot The key slot.
|
|
|
|
* \retval #PSA_SUCCESS
|
2023-12-21 18:57:14 +01:00
|
|
|
* \p slot is NULL or the key slot reader counter has been
|
|
|
|
* decremented (and potentially wiped) successfully.
|
2020-10-22 15:24:49 +02:00
|
|
|
* \retval #PSA_ERROR_CORRUPTION_DETECTED
|
2023-12-21 18:57:14 +01:00
|
|
|
* The slot's state was neither PSA_SLOT_FULL nor
|
2024-01-12 18:45:05 +01:00
|
|
|
* PSA_SLOT_PENDING_DELETION.
|
|
|
|
* Or a wipe was attempted and the slot's state was not
|
|
|
|
* PSA_SLOT_PENDING_DELETION.
|
|
|
|
* Or registered_readers was equal to 0.
|
2020-10-22 15:24:49 +02:00
|
|
|
*/
|
2023-12-21 18:57:14 +01:00
|
|
|
psa_status_t psa_unregister_read(psa_key_slot_t *slot);
|
2020-10-22 15:24:49 +02:00
|
|
|
|
2019-06-26 18:34:38 +02:00
|
|
|
/** Test whether a lifetime designates a key in an external cryptoprocessor.
|
|
|
|
*
|
|
|
|
* \param lifetime The lifetime to test.
|
|
|
|
*
|
|
|
|
* \retval 1
|
|
|
|
* The lifetime designates an external key. There should be a
|
|
|
|
* registered driver for this lifetime, otherwise the key cannot
|
|
|
|
* be created or manipulated.
|
|
|
|
* \retval 0
|
|
|
|
* The lifetime designates a key that is volatile or in internal
|
|
|
|
* storage.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
|
2019-06-26 18:34:38 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
|
|
|
|
!= PSA_KEY_LOCATION_LOCAL_STORAGE;
|
2019-06-26 18:34:38 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 14:52:05 +02:00
|
|
|
/** Validate a key's location.
|
2019-04-19 18:19:40 +02:00
|
|
|
*
|
2020-06-08 18:37:19 +02:00
|
|
|
* This function checks whether the key's attributes point to a location that
|
|
|
|
* is known to the PSA Core, and returns the driver function table if the key
|
|
|
|
* is to be found in an external location.
|
2019-04-19 18:19:40 +02:00
|
|
|
*
|
2020-06-17 14:52:05 +02:00
|
|
|
* \param[in] lifetime The key lifetime attribute.
|
2020-06-08 18:37:19 +02:00
|
|
|
* \param[out] p_drv On success, when a key is located in external
|
|
|
|
* storage, returns a pointer to the driver table
|
|
|
|
* associated with the key's storage location.
|
2019-06-26 18:34:38 +02:00
|
|
|
*
|
2023-02-14 19:21:09 +01:00
|
|
|
* \retval #PSA_SUCCESS \emptydescription
|
|
|
|
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
|
2020-06-08 18:37:19 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
|
|
|
|
psa_se_drv_table_entry_t **p_drv);
|
2020-06-08 18:37:19 +02:00
|
|
|
|
2020-07-17 16:11:30 +02:00
|
|
|
/** Validate the persistence of a key.
|
2020-06-08 18:37:19 +02:00
|
|
|
*
|
2020-07-23 12:30:41 +02:00
|
|
|
* \param[in] lifetime The key lifetime attribute.
|
2020-06-08 18:37:19 +02:00
|
|
|
*
|
2023-02-14 19:21:09 +01:00
|
|
|
* \retval #PSA_SUCCESS \emptydescription
|
2021-04-01 13:59:10 +02:00
|
|
|
* \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
|
|
|
|
* are not supported.
|
2020-07-17 16:11:30 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime);
|
2020-07-17 16:11:30 +02:00
|
|
|
|
|
|
|
/** Validate a key identifier.
|
|
|
|
*
|
2020-10-15 19:24:49 +02:00
|
|
|
* \param[in] key The key identifier.
|
|
|
|
* \param[in] vendor_ok Non-zero to indicate that key identifiers in the
|
|
|
|
* vendor range are allowed, volatile key identifiers
|
|
|
|
* excepted \c 0 otherwise.
|
2020-07-17 16:11:30 +02:00
|
|
|
*
|
2021-03-31 17:36:31 +02:00
|
|
|
* \retval <> 0 if the key identifier is valid, 0 otherwise.
|
2019-04-19 18:19:40 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok);
|
2019-04-19 18:19:40 +02:00
|
|
|
|
2018-11-30 18:54:54 +01:00
|
|
|
#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */
|