godot/thirdparty/mbedtls/library/psa_crypto_slot_management.c

/*
 *  PSA crypto layer on top of Mbed TLS crypto
 */
/*
 *  Copyright The Mbed TLS Contributors
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 */

#include "common.h"

#if defined(MBEDTLS_PSA_CRYPTO_C)

#include "psa/crypto.h"

#include "psa_crypto_core.h"
#include "psa_crypto_driver_wrappers_no_static.h"
#include "psa_crypto_slot_management.h"
#include "psa_crypto_storage.h"
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
#include "psa_crypto_se.h"
#endif

#include <stdlib.h>
#include <string.h>
#include "mbedtls/platform.h"
#if defined(MBEDTLS_THREADING_C)
#include "mbedtls/threading.h"
#endif



/* Make sure we have distinct ranges of key identifiers for distinct
 * purposes. */
MBEDTLS_STATIC_ASSERT();
MBEDTLS_STATIC_ASSERT();
MBEDTLS_STATIC_ASSERT();
MBEDTLS_STATIC_ASSERT();

MBEDTLS_STATIC_ASSERT();

MBEDTLS_STATIC_ASSERT();

MBEDTLS_STATIC_ASSERT();

MBEDTLS_STATIC_ASSERT();



#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)

/* Dynamic key store.
 *
 * The key store consists of multiple slices.
 *
 * The volatile keys are stored in variable-sized tables called slices.
 * Slices are allocated on demand and deallocated when possible.
 * The size of slices increases exponentially, so the average overhead
 * (number of slots that are allocated but not used) is roughly
 * proportional to the number of keys (with a factor that grows
 * when the key store is fragmented).
 *
 * One slice is dedicated to the cache of persistent and built-in keys.
 * For simplicity, they are separated from volatile keys. This cache
 * slice has a fixed size and has the slice index KEY_SLOT_CACHE_SLICE_INDEX,
 * located after the slices for volatile keys.
 */

/* Size of the last slice containing the cache of persistent and built-in keys. */
#define PERSISTENT_KEY_CACHE_COUNT

/* Volatile keys are stored in slices 0 through
 * (KEY_SLOT_VOLATILE_SLICE_COUNT - 1) inclusive.
 * Each slice is twice the size of the previous slice.
 * Volatile key identifiers encode the slice number as follows:
 *     bits 30..31:  0b10 (mandated by the PSA Crypto specification).
 *     bits 25..29:  slice index (0...KEY_SLOT_VOLATILE_SLICE_COUNT-1)
 *     bits 0..24:   slot index in slice
 */
#define KEY_ID_SLOT_INDEX_WIDTH
#define KEY_ID_SLICE_INDEX_WIDTH

#define KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH
#define KEY_SLOT_VOLATILE_SLICE_COUNT
#define KEY_SLICE_COUNT
#define KEY_SLOT_CACHE_SLICE_INDEX


/* Check that the length of the largest slice (calculated as
 * KEY_SLICE_LENGTH_MAX below) does not overflow size_t. We use
 * an indirect method in case the calculation of KEY_SLICE_LENGTH_MAX
 * itself overflows uintmax_t: if (BASE_LENGTH << c)
 * overflows size_t then BASE_LENGTH > SIZE_MAX >> c.
 */
#if (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH >              \
     SIZE_MAX >> (KEY_SLOT_VOLATILE_SLICE_COUNT - 1))
#error "Maximum slice length overflows size_t"
#endif

#if KEY_ID_SLICE_INDEX_WIDTH + KEY_ID_SLOT_INDEX_WIDTH > 30
#error "Not enough room in volatile key IDs for slice index and slot index"
#endif
#if KEY_SLOT_VOLATILE_SLICE_COUNT > (1 << KEY_ID_SLICE_INDEX_WIDTH)
#error "Too many slices to fit the slice index in a volatile key ID"
#endif
#define KEY_SLICE_LENGTH_MAX
#if KEY_SLICE_LENGTH_MAX > 1 << KEY_ID_SLOT_INDEX_WIDTH
#error "Not enough room in volatile key IDs for a slot index in the largest slice"
#endif
#if KEY_ID_SLICE_INDEX_WIDTH > 8
#error "Slice index does not fit in uint8_t for psa_key_slot_t::slice_index"
#endif


/* Calculate the volatile key id to use for a given slot.
 * This function assumes valid parameter values. */
static psa_key_id_t volatile_key_id_of_index(size_t slice_idx,
                                             size_t slot_idx)
{}

/* Calculate the slice containing the given volatile key.
 * This function assumes valid parameter values. */
static size_t slice_index_of_volatile_key_id(psa_key_id_t key_id)
{}

/* Calculate the index of the slot containing the given volatile key.
 * This function assumes valid parameter values. */
static size_t slot_index_of_volatile_key_id(psa_key_id_t key_id)
{}

/* In global_data.first_free_slot_index, use this special value to
 * indicate that the slice is full. */
#define FREE_SLOT_INDEX_NONE

#if defined(MBEDTLS_TEST_HOOKS)
size_t psa_key_slot_volatile_slice_count(void)
{
    return KEY_SLOT_VOLATILE_SLICE_COUNT;
}
#endif

#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */

/* Static key store.
 *
 * All the keys (volatile or persistent) are in a single slice.
 * We only use slices as a concept to allow some differences between
 * static and dynamic key store management to be buried in auxiliary
 * functions.
 */

#define PERSISTENT_KEY_CACHE_COUNT
#define KEY_SLICE_COUNT
#define KEY_SLOT_CACHE_SLICE_INDEX

#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */


psa_global_data_t;

static psa_global_data_t global_data;

static uint8_t psa_get_key_slots_initialized(void)
{}



/** The length of the given slice in the key slot table.
 *
 * \param slice_idx     The slice number. It must satisfy
 *                      0 <= slice_idx < KEY_SLICE_COUNT.
 *
 * \return              The number of elements in the given slice.
 */
static inline size_t key_slice_length(size_t slice_idx);

/** Get a pointer to the slot where the given volatile key is located.
 *
 * \param key_id        The key identifier. It must be a valid volatile key
 *                      identifier.
 * \return              A pointer to the only slot that the given key
 *                      can be in. Note that the slot may be empty or
 *                      contain a different key.
 */
static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id);

/** Get a pointer to an entry in the persistent key cache.
 *
 * \param slot_idx      The index in the table. It must satisfy
 *                      0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT.
 * \return              A pointer to the slot containing the given
 *                      persistent key cache entry.
 */
static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx);

/** Get a pointer to a slot given by slice and index.
 *
 * \param slice_idx     The slice number. It must satisfy
 *                      0 <= slice_idx < KEY_SLICE_COUNT.
 * \param slot_idx      An index in the given slice. It must satisfy
 *                      0 <= slot_idx < key_slice_length(slice_idx).
 *
 * \return              A pointer to the given slot.
 */
static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx);

#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)

#if defined(MBEDTLS_TEST_HOOKS)
size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(size_t slice_idx) = NULL;
#endif

static inline size_t key_slice_length(size_t slice_idx)
{}

static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
{}

static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
{}

static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
{}

#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */

static inline size_t key_slice_length(size_t slice_idx)
{
    (void) slice_idx;
    return ARRAY_LENGTH(global_data.key_slots);
}

static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
{
    MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
                          PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
                          "The key slot array is larger than the volatile key ID range");
    return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
}

static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
{
    return &global_data.key_slots[slot_idx];
}

static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
{
    (void) slice_idx;
    return &global_data.key_slots[slot_idx];
}

#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */



int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
{}

/** Get the description in memory of a key given its identifier and lock it.
 *
 * 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.
 *
 * The function searches the key slots containing the description of the key
 * with \p key identifier. The function does only read accesses to the key
 * slots. The function does not load any persistent key thus does not access
 * any storage.
 *
 * For volatile key identifiers, only one key slot is queried as a volatile
 * key with identifier key_id can only be stored in slot of index
 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
 *
 * On success, the function locks the key slot. It is the responsibility of
 * the caller to unlock the key slot when it does not access it anymore.
 *
 * If multi-threading is enabled, the caller must hold the
 * global key slot mutex.
 *
 * \param key           Key identifier to query.
 * \param[out] p_slot   On success, `*p_slot` contains a pointer to the
 *                      key slot containing the description of the key
 *                      identified by \p key.
 *
 * \retval #PSA_SUCCESS
 *         The pointer to the key slot containing the description of the key
 *         identified by \p key was returned.
 * \retval #PSA_ERROR_INVALID_HANDLE
 *         \p key is not a valid key identifier.
 * \retval #PSA_ERROR_DOES_NOT_EXIST
 *         There is no key with key identifier \p key in the key slots.
 */
static psa_status_t psa_get_and_lock_key_slot_in_memory(
    mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
{}

psa_status_t psa_initialize_key_slots(void)
{}

void psa_wipe_all_key_slots(void)
{}

#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)

static psa_status_t psa_allocate_volatile_key_slot(psa_key_id_t *key_id,
                                                   psa_key_slot_t **p_slot)
{}

psa_status_t psa_free_key_slot(size_t slice_idx,
                               psa_key_slot_t *slot)
{}
#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */

psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
                                       psa_key_slot_t **p_slot)
{}

#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
{}
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */

#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)

static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
{
    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
    psa_drv_slot_number_t slot_number = 0;
    size_t key_buffer_size = 0;
    size_t key_buffer_length = 0;

    if (!psa_key_id_is_builtin(
            MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
        return PSA_ERROR_DOES_NOT_EXIST;
    }

    /* Check the platform function to see whether this key actually exists */
    status = mbedtls_psa_platform_get_builtin_key(
        slot->attr.id, &lifetime, &slot_number);
    if (status != PSA_SUCCESS) {
        return status;
    }

    /* Set required key attributes to ensure get_builtin_key can retrieve the
     * full attributes. */
    psa_set_key_id(&attributes, slot->attr.id);
    psa_set_key_lifetime(&attributes, lifetime);

    /* Get the full key attributes from the driver in order to be able to
     * calculate the required buffer size. */
    status = psa_driver_wrapper_get_builtin_key(
        slot_number, &attributes,
        NULL, 0, NULL);
    if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
        /* Builtin keys cannot be defined by the attributes alone */
        if (status == PSA_SUCCESS) {
            status = PSA_ERROR_CORRUPTION_DETECTED;
        }
        return status;
    }

    /* If the key should exist according to the platform, then ask the driver
     * what its expected size is. */
    status = psa_driver_wrapper_get_key_buffer_size(&attributes,
                                                    &key_buffer_size);
    if (status != PSA_SUCCESS) {
        return status;
    }

    /* Allocate a buffer of the required size and load the builtin key directly
     * into the (now properly sized) slot buffer. */
    status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_driver_wrapper_get_builtin_key(
        slot_number, &attributes,
        slot->key.data, slot->key.bytes, &key_buffer_length);
    if (status != PSA_SUCCESS) {
        goto exit;
    }

    /* Copy actual key length and core attributes into the slot on success */
    slot->key.bytes = key_buffer_length;
    slot->attr = attributes;
exit:
    if (status != PSA_SUCCESS) {
        psa_remove_key_data_from_memory(slot);
    }
    return status;
}
#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */

psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
                                       psa_key_slot_t **p_slot)
{}

psa_status_t psa_unregister_read(psa_key_slot_t *slot)
{}

psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
{}

psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
                                       psa_se_drv_table_entry_t **p_drv)
{}

psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
{}

psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
{}

psa_status_t psa_close_key(psa_key_handle_t handle)
{}

psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
{}

void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
{}

#endif /* MBEDTLS_PSA_CRYPTO_C */