2018-12-07 18:24:41 +01:00
|
|
|
/*
|
|
|
|
* PSA crypto core internal interfaces
|
|
|
|
*/
|
|
|
|
/* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PSA_CRYPTO_CORE_H
|
|
|
|
#define PSA_CRYPTO_CORE_H
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
2019-07-12 23:44:37 +02:00
|
|
|
#include "psa/crypto_se_driver.h"
|
2018-12-07 18:24:41 +01:00
|
|
|
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
|
|
|
|
/** The data structure representing a key slot, containing key material
|
|
|
|
* and metadata for one key.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2019-07-30 20:06:31 +02:00
|
|
|
psa_core_key_attributes_t attr;
|
2018-12-07 18:24:41 +01:00
|
|
|
union
|
|
|
|
{
|
2019-07-12 23:44:37 +02:00
|
|
|
/* Raw-data key (key_type_is_raw_bytes() in psa_crypto.c) */
|
2018-12-07 18:24:41 +01:00
|
|
|
struct raw_data
|
|
|
|
{
|
|
|
|
uint8_t *data;
|
|
|
|
size_t bytes;
|
|
|
|
} raw;
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2019-07-12 23:44:37 +02:00
|
|
|
/* RSA public key or key pair */
|
2018-12-07 18:24:41 +01:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
#endif /* MBEDTLS_RSA_C */
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2019-07-12 23:44:37 +02:00
|
|
|
/* EC public key or key pair */
|
2018-12-07 18:24:41 +01:00
|
|
|
mbedtls_ecp_keypair *ecp;
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2019-08-02 19:21:49 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-07-12 23:44:37 +02:00
|
|
|
/* Any key type in a secure element */
|
|
|
|
struct se
|
|
|
|
{
|
|
|
|
psa_key_slot_number_t slot_number;
|
|
|
|
} se;
|
2019-08-02 19:21:49 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2018-12-07 18:24:41 +01:00
|
|
|
} data;
|
|
|
|
} psa_key_slot_t;
|
|
|
|
|
2019-08-02 19:19:39 +02:00
|
|
|
/* A mask of key attribute flags used only internally.
|
|
|
|
* Currently there aren't any. */
|
2019-08-05 17:32:13 +02:00
|
|
|
#define PSA_KA_MASK_INTERNAL_ONLY ( \
|
2019-08-02 19:19:39 +02:00
|
|
|
0 )
|
|
|
|
|
2019-07-31 15:01:55 +02:00
|
|
|
/** Test whether a key slot is occupied.
|
|
|
|
*
|
|
|
|
* A key slot is occupied iff the key type is nonzero. This works because
|
|
|
|
* no valid key can have 0 as its key type.
|
|
|
|
*
|
|
|
|
* \param[in] slot The key slot to test.
|
|
|
|
*
|
|
|
|
* \return 1 if the slot is occupied, 0 otherwise.
|
|
|
|
*/
|
|
|
|
static inline int psa_is_key_slot_occupied( const psa_key_slot_t *slot )
|
|
|
|
{
|
|
|
|
return( slot->attr.type != 0 );
|
|
|
|
}
|
2019-07-30 20:06:31 +02:00
|
|
|
|
|
|
|
/** Retrieve flags from psa_key_slot_t::attr::core::flags.
|
|
|
|
*
|
|
|
|
* \param[in] slot The key slot to query.
|
|
|
|
* \param mask The mask of bits to extract.
|
|
|
|
*
|
|
|
|
* \return The key attribute flags in the given slot,
|
|
|
|
* bitwise-anded with \p mask.
|
|
|
|
*/
|
|
|
|
static inline uint16_t psa_key_slot_get_flags( const psa_key_slot_t *slot,
|
|
|
|
uint16_t mask )
|
|
|
|
{
|
|
|
|
return( slot->attr.flags & mask );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set flags in psa_key_slot_t::attr::core::flags.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot The key slot to modify.
|
|
|
|
* \param mask The mask of bits to modify.
|
|
|
|
* \param value The new value of the selected bits.
|
|
|
|
*/
|
|
|
|
static inline void psa_key_slot_set_flags( psa_key_slot_t *slot,
|
|
|
|
uint16_t mask,
|
|
|
|
uint16_t value )
|
|
|
|
{
|
|
|
|
slot->attr.flags = ( ( ~mask & slot->attr.flags ) |
|
|
|
|
( mask & value ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Turn on flags in psa_key_slot_t::attr::core::flags.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot The key slot to modify.
|
|
|
|
* \param mask The mask of bits to set.
|
|
|
|
*/
|
|
|
|
static inline void psa_key_slot_set_bits_in_flags( psa_key_slot_t *slot,
|
|
|
|
uint16_t mask )
|
|
|
|
{
|
|
|
|
slot->attr.flags |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Turn off flags in psa_key_slot_t::attr::core::flags.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot The key slot to modify.
|
|
|
|
* \param mask The mask of bits to clear.
|
|
|
|
*/
|
|
|
|
static inline void psa_key_slot_clear_bits( psa_key_slot_t *slot,
|
|
|
|
uint16_t mask )
|
|
|
|
{
|
|
|
|
slot->attr.flags &= ~mask;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:29:04 +01:00
|
|
|
/** Completely wipe a slot in memory, including its policy.
|
2018-12-10 17:00:38 +01:00
|
|
|
*
|
|
|
|
* Persistent storage is not affected.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot The key slot to wipe.
|
|
|
|
*
|
|
|
|
* \retval PSA_SUCCESS
|
|
|
|
* Success. This includes the case of a key slot that was
|
|
|
|
* already fully wiped.
|
2019-05-16 21:35:18 +02:00
|
|
|
* \retval PSA_ERROR_CORRUPTION_DETECTED
|
2018-12-10 17:00:38 +01:00
|
|
|
*/
|
2018-12-10 16:29:04 +01:00
|
|
|
psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot );
|
|
|
|
|
2018-12-10 17:00:38 +01:00
|
|
|
/** Import key data into a slot.
|
|
|
|
*
|
|
|
|
* `slot->type` must have been set previously.
|
|
|
|
* This function assumes that the slot does not contain any key material yet.
|
|
|
|
* On failure, the slot content is unchanged.
|
|
|
|
*
|
|
|
|
* Persistent storage is not affected.
|
|
|
|
*
|
|
|
|
* \param[in,out] slot The key slot to import data into.
|
|
|
|
* Its `type` field must have previously been set to
|
|
|
|
* the desired key type.
|
|
|
|
* It must not contain any key material yet.
|
|
|
|
* \param[in] data Buffer containing the key material to parse and import.
|
|
|
|
* \param data_length Size of \p data in bytes.
|
|
|
|
*
|
|
|
|
* \retval PSA_SUCCESS
|
|
|
|
* \retval PSA_ERROR_INVALID_ARGUMENT
|
|
|
|
* \retval PSA_ERROR_NOT_SUPPORTED
|
|
|
|
* \retval PSA_ERROR_INSUFFICIENT_MEMORY
|
|
|
|
*/
|
2018-12-10 16:48:53 +01:00
|
|
|
psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length );
|
|
|
|
|
2018-12-07 18:24:41 +01:00
|
|
|
#endif /* PSA_CRYPTO_CORE_H */
|