2018-06-15 14:06:04 +02:00
|
|
|
/*
|
|
|
|
* PSA persistent key storage
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2018-06-15 14:06:04 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-05-19 17:32:14 +02:00
|
|
|
#include "common.h"
|
|
|
|
|
2018-06-15 14:06:04 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "psa_crypto_storage.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
|
2019-02-24 17:10:18 +01:00
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
|
|
|
#include "psa_crypto_its.h"
|
|
|
|
#else /* Native ITS implementation */
|
|
|
|
#include "psa/error.h"
|
|
|
|
#include "psa/internal_trusted_storage.h"
|
|
|
|
#endif
|
|
|
|
|
2018-06-15 14:06:04 +02:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2019-07-22 19:26:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* Key storage */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2020-08-28 19:01:50 +02:00
|
|
|
/* Determine a file name (ITS file identifier) for the given key identifier.
|
|
|
|
* The file name must be distinct from any file that is used for a purpose
|
|
|
|
* other than storing a key. Currently, the only such file is the random seed
|
|
|
|
* file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
|
|
|
|
* 0xFFFFFF52. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
2020-07-23 17:13:42 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2019-02-24 17:00:27 +01:00
|
|
|
/* Encode the owner in the upper 32 bits. This means that if
|
|
|
|
* owner values are nonzero (as they are on a PSA platform),
|
|
|
|
* no key file will ever have a value less than 0x100000000, so
|
|
|
|
* the whole range 0..0xffffffff is available for non-key files. */
|
2023-01-11 14:50:10 +01:00
|
|
|
uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
|
|
|
|
return ((uint64_t) unsigned_owner_id << 32) |
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
#else
|
|
|
|
/* Use the key id directly as a file name.
|
2020-08-28 19:01:50 +02:00
|
|
|
* psa_is_key_id_valid() in psa_crypto_slot_management.c
|
2019-02-24 17:00:27 +01:00
|
|
|
* is responsible for ensuring that key identifiers do not have a
|
|
|
|
* value that is reserved for non-key files. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return key;
|
2019-02-24 17:00:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-24 17:10:18 +01:00
|
|
|
/**
|
|
|
|
* \brief Load persistent data for the given key slot number.
|
|
|
|
*
|
|
|
|
* This function reads data from a storage backend and returns the data in a
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* \param key Persistent identifier of the key to be loaded. This
|
|
|
|
* should be an occupied storage location.
|
|
|
|
* \param[out] data Buffer where the data is to be written.
|
|
|
|
* \param data_size Size of the \c data buffer in bytes.
|
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
2020-11-09 17:42:55 +01:00
|
|
|
* \retval #PSA_ERROR_DATA_INVALID
|
|
|
|
* \retval #PSA_ERROR_DATA_CORRUPT
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE
|
|
|
|
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
2019-02-24 17:10:18 +01:00
|
|
|
*/
|
2020-08-28 19:01:50 +02:00
|
|
|
static psa_status_t psa_crypto_storage_load(
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2019-07-10 17:34:21 +02:00
|
|
|
size_t data_length = 0;
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get_info(data_identifier, &data_identifier_info);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get(data_identifier, 0, (uint32_t) data_size, data, &data_length);
|
|
|
|
if (data_size != data_length) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
|
|
|
psa_status_t ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
struct psa_storage_info_t data_identifier_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = psa_its_get_info(data_identifier, &data_identifier_info);
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == PSA_ERROR_DOES_NOT_EXIST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 17:10:18 +01:00
|
|
|
/**
|
|
|
|
* \brief Store persistent data for the given key slot number.
|
|
|
|
*
|
|
|
|
* This function stores the given data buffer to a persistent storage.
|
|
|
|
*
|
|
|
|
* \param key Persistent identifier of the key to be stored. This
|
|
|
|
* should be an unoccupied storage location.
|
|
|
|
* \param[in] data Buffer containing the data to be stored.
|
|
|
|
* \param data_length The number of bytes
|
|
|
|
* that make up the data.
|
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE
|
|
|
|
* \retval #PSA_ERROR_ALREADY_EXISTS
|
2020-11-30 16:50:34 +01:00
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE
|
2020-11-09 17:42:55 +01:00
|
|
|
* \retval #PSA_ERROR_DATA_INVALID
|
2019-02-24 17:10:18 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
struct psa_storage_info_t data_identifier_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_is_key_present_in_storage(key) == 1) {
|
|
|
|
return PSA_ERROR_ALREADY_EXISTS;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_set(data_identifier, (uint32_t) data_length, data, 0);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get_info(data_identifier, &data_identifier_info);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2019-02-24 17:00:27 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data_identifier_info.size != data_length) {
|
2020-11-09 17:39:56 +01:00
|
|
|
status = PSA_ERROR_DATA_INVALID;
|
2019-02-24 17:00:27 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2020-08-25 22:50:06 +02:00
|
|
|
/* Remove the file in case we managed to create it but something
|
|
|
|
* went wrong. It's ok if the file doesn't exist. If the file exists
|
|
|
|
* but the removal fails, we're already reporting an error so there's
|
|
|
|
* nothing else we can do. */
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) psa_its_remove(data_identifier);
|
2020-08-25 22:50:06 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
|
|
|
psa_status_t ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
struct psa_storage_info_t data_identifier_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = psa_its_get_info(data_identifier, &data_identifier_info);
|
|
|
|
if (ret == PSA_ERROR_DOES_NOT_EXIST) {
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_its_remove(data_identifier) != PSA_SUCCESS) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = psa_its_get_info(data_identifier, &data_identifier_info);
|
|
|
|
if (ret != PSA_ERROR_DOES_NOT_EXIST) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 17:10:18 +01:00
|
|
|
/**
|
|
|
|
* \brief Get data length for given key slot number.
|
|
|
|
*
|
|
|
|
* \param key Persistent identifier whose stored data length
|
|
|
|
* is to be obtained.
|
|
|
|
* \param[out] data_length The number of bytes that make up the data.
|
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
|
|
|
* \retval #PSA_ERROR_STORAGE_FAILURE
|
2020-11-09 17:42:55 +01:00
|
|
|
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
|
|
|
* \retval #PSA_ERROR_DATA_CORRUPT
|
2019-02-24 17:10:18 +01:00
|
|
|
*/
|
|
|
|
static psa_status_t psa_crypto_storage_get_data_length(
|
2020-08-28 19:01:50 +02:00
|
|
|
const mbedtls_svc_key_id_t key,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *data_length)
|
2019-02-24 17:00:27 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
|
2019-02-24 17:00:27 +01:00
|
|
|
struct psa_storage_info_t data_identifier_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get_info(data_identifier, &data_identifier_info);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-02-24 17:00:27 +01:00
|
|
|
|
|
|
|
*data_length = (size_t) data_identifier_info.size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2019-02-24 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 17:02:17 +02:00
|
|
|
/**
|
|
|
|
* Persistent key storage magic header.
|
|
|
|
*/
|
|
|
|
#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
|
2023-01-11 14:50:10 +01:00
|
|
|
#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
|
2018-06-28 17:02:17 +02:00
|
|
|
|
2018-06-15 14:06:04 +02:00
|
|
|
typedef struct {
|
2018-06-28 17:02:17 +02:00
|
|
|
uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
|
2018-06-15 14:06:04 +02:00
|
|
|
uint8_t version[4];
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t lifetime[sizeof(psa_key_lifetime_t)];
|
2020-10-07 10:50:15 +02:00
|
|
|
uint8_t type[2];
|
|
|
|
uint8_t bits[2];
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t policy[sizeof(psa_key_policy_t)];
|
2018-06-15 14:06:04 +02:00
|
|
|
uint8_t data_len[4];
|
|
|
|
uint8_t key_data[];
|
|
|
|
} psa_persistent_key_storage_format;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void psa_format_key_data_for_storage(const uint8_t *data,
|
|
|
|
const size_t data_length,
|
|
|
|
const psa_core_key_attributes_t *attr,
|
|
|
|
uint8_t *storage_data)
|
2018-06-15 14:06:04 +02:00
|
|
|
{
|
|
|
|
psa_persistent_key_storage_format *storage_format =
|
|
|
|
(psa_persistent_key_storage_format *) storage_data;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
|
|
|
|
PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(0, storage_format->version, 0);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_LE((uint16_t) attr->type, storage_format->type, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_LE((uint16_t) attr->bits, storage_format->bits, 0);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
|
|
|
|
MBEDTLS_PUT_UINT32_LE(attr->policy.alg2, storage_format->policy, 2 * sizeof(uint32_t));
|
|
|
|
MBEDTLS_PUT_UINT32_LE(data_length, storage_format->data_len, 0);
|
|
|
|
memcpy(storage_format->key_data, data, data_length);
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t check_magic_header(const uint8_t *data)
|
2018-06-28 17:02:17 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
|
|
|
|
PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
return PSA_SUCCESS;
|
2018-06-28 17:02:17 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
|
|
|
|
size_t storage_data_length,
|
|
|
|
uint8_t **key_data,
|
|
|
|
size_t *key_data_length,
|
|
|
|
psa_core_key_attributes_t *attr)
|
2018-06-15 14:06:04 +02:00
|
|
|
{
|
2018-06-28 17:02:17 +02:00
|
|
|
psa_status_t status;
|
2018-06-15 14:06:04 +02:00
|
|
|
const psa_persistent_key_storage_format *storage_format =
|
2023-01-11 14:50:10 +01:00
|
|
|
(const psa_persistent_key_storage_format *) storage_data;
|
2018-06-15 14:06:04 +02:00
|
|
|
uint32_t version;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (storage_data_length < sizeof(*storage_format)) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2018-06-28 17:02:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = check_magic_header(storage_data);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-06-28 17:02:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
version = MBEDTLS_GET_UINT32_LE(storage_format->version, 0);
|
|
|
|
if (version != 0) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*key_data_length = MBEDTLS_GET_UINT32_LE(storage_format->data_len, 0);
|
|
|
|
if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
|
|
|
|
*key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*key_data_length == 0) {
|
2019-04-25 13:47:06 +02:00
|
|
|
*key_data = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
*key_data = mbedtls_calloc(1, *key_data_length);
|
|
|
|
if (*key_data == NULL) {
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(*key_data, storage_format->key_data, *key_data_length);
|
2019-04-25 13:47:06 +02:00
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
attr->lifetime = MBEDTLS_GET_UINT32_LE(storage_format->lifetime, 0);
|
|
|
|
attr->type = MBEDTLS_GET_UINT16_LE(storage_format->type, 0);
|
|
|
|
attr->bits = MBEDTLS_GET_UINT16_LE(storage_format->bits, 0);
|
|
|
|
attr->policy.usage = MBEDTLS_GET_UINT32_LE(storage_format->policy, 0);
|
|
|
|
attr->policy.alg = MBEDTLS_GET_UINT32_LE(storage_format->policy, sizeof(uint32_t));
|
|
|
|
attr->policy.alg2 = MBEDTLS_GET_UINT32_LE(storage_format->policy, 2 * sizeof(uint32_t));
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_save_persistent_key(const psa_core_key_attributes_t *attr,
|
|
|
|
const uint8_t *data,
|
|
|
|
const size_t data_length)
|
2018-06-15 14:06:04 +02:00
|
|
|
{
|
|
|
|
size_t storage_data_length;
|
|
|
|
uint8_t *storage_data;
|
|
|
|
psa_status_t status;
|
|
|
|
|
2021-01-26 12:45:39 +01:00
|
|
|
/* All keys saved to persistent storage always have a key context */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data == NULL || data_length == 0) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2021-01-26 12:45:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
|
|
|
|
return PSA_ERROR_INSUFFICIENT_STORAGE;
|
|
|
|
}
|
|
|
|
storage_data_length = data_length + sizeof(psa_persistent_key_storage_format);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
storage_data = mbedtls_calloc(1, storage_data_length);
|
|
|
|
if (storage_data == NULL) {
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_format_key_data_for_storage(data, data_length, attr, storage_data);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_crypto_storage_store(attr->id,
|
|
|
|
storage_data, storage_data_length);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(storage_data, storage_data_length);
|
|
|
|
mbedtls_free(storage_data);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
|
2018-06-15 14:06:04 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_data != NULL) {
|
|
|
|
mbedtls_platform_zeroize(key_data, key_data_length);
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(key_data);
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_load_persistent_key(psa_core_key_attributes_t *attr,
|
|
|
|
uint8_t **data,
|
|
|
|
size_t *data_length)
|
2018-06-15 14:06:04 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
uint8_t *loaded_data;
|
|
|
|
size_t storage_data_length = 0;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t key = attr->id;
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_crypto_storage_get_data_length(key, &storage_data_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
loaded_data = mbedtls_calloc(1, storage_data_length);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (loaded_data == NULL) {
|
|
|
|
return PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2018-06-15 14:06:04 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
|
|
|
|
data, data_length, attr);
|
2018-06-15 14:06:04 +02:00
|
|
|
|
2021-01-26 12:45:39 +01:00
|
|
|
/* All keys saved to persistent storage always have a key context */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS &&
|
|
|
|
(*data == NULL || *data_length == 0)) {
|
2021-01-26 12:45:39 +01:00
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-26 12:45:39 +01:00
|
|
|
|
2018-06-15 14:06:04 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(loaded_data, storage_data_length);
|
|
|
|
mbedtls_free(loaded_data);
|
|
|
|
return status;
|
2018-06-15 14:06:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 19:26:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* Transactions */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
|
|
|
|
|
|
|
|
psa_crypto_transaction_t psa_crypto_transaction;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_crypto_save_transaction(void)
|
2019-07-22 19:26:12 +02:00
|
|
|
{
|
|
|
|
struct psa_storage_info_t p_info;
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
|
|
|
|
if (status == PSA_SUCCESS) {
|
2019-07-22 19:26:12 +02:00
|
|
|
/* This shouldn't happen: we're trying to start a transaction while
|
|
|
|
* there is still a transaction that hasn't been replayed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
} else if (status != PSA_ERROR_DOES_NOT_EXIST) {
|
|
|
|
return status;
|
2019-07-22 19:26:12 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
|
|
|
|
sizeof(psa_crypto_transaction),
|
|
|
|
&psa_crypto_transaction,
|
|
|
|
0);
|
2019-07-22 19:26:12 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_crypto_load_transaction(void)
|
2019-07-22 19:26:12 +02:00
|
|
|
{
|
2019-07-31 17:57:57 +02:00
|
|
|
psa_status_t status;
|
|
|
|
size_t length;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
|
|
|
|
sizeof(psa_crypto_transaction),
|
|
|
|
&psa_crypto_transaction, &length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (length != sizeof(psa_crypto_transaction)) {
|
|
|
|
return PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
return PSA_SUCCESS;
|
2019-07-22 19:26:12 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_crypto_stop_transaction(void)
|
2019-07-22 19:26:12 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
|
2019-07-22 19:26:12 +02:00
|
|
|
/* Whether or not updating the storage succeeded, the transaction is
|
|
|
|
* finished now. It's too late to go back, so zero out the in-memory
|
|
|
|
* data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
|
|
|
|
return status;
|
2019-07-22 19:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* Random generator state */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2019-02-25 11:04:06 +01:00
|
|
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
|
|
|
|
size_t seed_size)
|
2019-02-25 11:04:06 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
struct psa_storage_info_t p_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
|
2019-02-25 11:04:06 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ERROR_DOES_NOT_EXIST == status) { /* No seed exists */
|
|
|
|
status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
|
|
|
|
} else if (PSA_SUCCESS == status) {
|
2019-02-25 11:04:06 +01:00
|
|
|
/* You should not be here. Seed needs to be injected only once */
|
|
|
|
status = PSA_ERROR_NOT_PERMITTED;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2019-02-25 11:04:06 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
|
|
|
|
|
2019-07-22 19:26:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* The end */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2018-06-15 14:06:04 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|