2019-06-24 14:06:43 +02:00
|
|
|
/*
|
|
|
|
* PSA crypto support for secure element drivers
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2019-06-24 14:06:43 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PSA_CRYPTO_SE_H
|
|
|
|
#define PSA_CRYPTO_SE_H
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "psa/crypto_se_driver.h"
|
|
|
|
|
2020-05-10 00:44:04 +02:00
|
|
|
/** The maximum location value that this implementation supports
|
2019-07-23 17:38:08 +02:00
|
|
|
* for a secure element.
|
|
|
|
*
|
|
|
|
* This is not a characteristic that each PSA implementation has, but a
|
|
|
|
* limitation of the current implementation due to the constraints imposed
|
|
|
|
* by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
|
|
|
|
*
|
2020-05-10 00:44:04 +02:00
|
|
|
* The minimum location value for a secure element is 1, like on any
|
|
|
|
* PSA implementation (0 means a transparent key).
|
2019-07-23 17:38:08 +02:00
|
|
|
*/
|
2020-05-10 00:44:04 +02:00
|
|
|
#define PSA_MAX_SE_LOCATION 255
|
2019-07-23 17:38:08 +02:00
|
|
|
|
|
|
|
/** The base of the range of ITS file identifiers for secure element
|
|
|
|
* driver persistent data.
|
|
|
|
*
|
2020-07-23 12:30:41 +02:00
|
|
|
* We use a slice of the implementation reserved range 0xffff0000..0xffffffff,
|
2019-07-23 17:38:08 +02:00
|
|
|
* specifically the range 0xfffffe00..0xfffffeff. The length of this range
|
2020-05-10 00:44:04 +02:00
|
|
|
* drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
|
|
|
|
* actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
|
|
|
|
* which doesn't have a driver.
|
2019-07-23 17:38:08 +02:00
|
|
|
*/
|
2020-07-23 16:07:42 +02:00
|
|
|
#define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ( (psa_key_id_t) 0xfffffe00 )
|
2019-07-23 17:38:08 +02:00
|
|
|
|
2020-05-10 00:44:04 +02:00
|
|
|
/** The maximum number of registered secure element driver locations. */
|
2019-06-24 14:06:43 +02:00
|
|
|
#define PSA_MAX_SE_DRIVERS 4
|
|
|
|
|
2019-06-24 14:34:43 +02:00
|
|
|
/** Unregister all secure element drivers.
|
|
|
|
*
|
|
|
|
* \warning Do not call this function while the library is in the initialized
|
|
|
|
* state. This function is only intended to be called at the end
|
|
|
|
* of mbedtls_psa_crypto_free().
|
|
|
|
*/
|
|
|
|
void psa_unregister_all_se_drivers( void );
|
|
|
|
|
2019-10-01 15:22:29 +02:00
|
|
|
/** Initialize all secure element drivers.
|
|
|
|
*
|
|
|
|
* Called from psa_crypto_init().
|
|
|
|
*/
|
|
|
|
psa_status_t psa_init_all_se_drivers( void );
|
|
|
|
|
2019-06-26 18:18:12 +02:00
|
|
|
/** A structure that describes a registered secure element driver.
|
|
|
|
*
|
|
|
|
* A secure element driver table entry contains a pointer to the
|
2019-07-12 23:38:19 +02:00
|
|
|
* driver's method table as well as the driver context structure.
|
2019-06-26 18:18:12 +02:00
|
|
|
*/
|
|
|
|
typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
|
|
|
|
|
2019-07-12 23:38:19 +02:00
|
|
|
/** Return the secure element driver information for a lifetime value.
|
|
|
|
*
|
|
|
|
* \param lifetime The lifetime value to query.
|
|
|
|
* \param[out] p_methods On output, if there is a driver,
|
|
|
|
* \c *methods points to its method table.
|
|
|
|
* Otherwise \c *methods is \c NULL.
|
|
|
|
* \param[out] p_drv_context On output, if there is a driver,
|
|
|
|
* \c *drv_context points to its context
|
|
|
|
* structure.
|
|
|
|
* Otherwise \c *drv_context is \c NULL.
|
|
|
|
*
|
|
|
|
* \retval 1
|
|
|
|
* \p lifetime corresponds to a registered driver.
|
|
|
|
* \retval 0
|
|
|
|
* \p lifetime does not correspond to a registered driver.
|
|
|
|
*/
|
|
|
|
int psa_get_se_driver( psa_key_lifetime_t lifetime,
|
|
|
|
const psa_drv_se_t **p_methods,
|
|
|
|
psa_drv_se_context_t **p_drv_context);
|
|
|
|
|
2019-06-26 18:18:12 +02:00
|
|
|
/** Return the secure element driver table entry for a lifetime value.
|
|
|
|
*
|
|
|
|
* \param lifetime The lifetime value to query.
|
|
|
|
*
|
|
|
|
* \return The driver table entry for \p lifetime, or
|
|
|
|
* \p NULL if \p lifetime does not correspond to a registered driver.
|
|
|
|
*/
|
2019-07-12 23:38:19 +02:00
|
|
|
psa_se_drv_table_entry_t *psa_get_se_driver_entry(
|
2019-06-26 18:18:12 +02:00
|
|
|
psa_key_lifetime_t lifetime );
|
|
|
|
|
|
|
|
/** Return the method table for a secure element driver.
|
|
|
|
*
|
2019-07-12 23:38:19 +02:00
|
|
|
* \param[in] driver The driver table entry to access, or \c NULL.
|
2019-06-26 18:18:12 +02:00
|
|
|
*
|
2019-07-12 23:38:19 +02:00
|
|
|
* \return The driver's method table.
|
|
|
|
* \c NULL if \p driver is \c NULL.
|
2019-06-26 18:18:12 +02:00
|
|
|
*/
|
|
|
|
const psa_drv_se_t *psa_get_se_driver_methods(
|
2019-07-12 23:38:19 +02:00
|
|
|
const psa_se_drv_table_entry_t *driver );
|
2019-06-26 18:18:12 +02:00
|
|
|
|
2019-07-12 23:38:19 +02:00
|
|
|
/** Return the context of a secure element driver.
|
2019-06-26 18:18:12 +02:00
|
|
|
*
|
2019-07-12 23:38:19 +02:00
|
|
|
* \param[in] driver The driver table entry to access, or \c NULL.
|
2019-06-26 18:18:12 +02:00
|
|
|
*
|
2019-07-12 23:38:19 +02:00
|
|
|
* \return A pointer to the driver context.
|
|
|
|
* \c NULL if \p driver is \c NULL.
|
2019-06-26 18:18:12 +02:00
|
|
|
*/
|
2019-07-12 23:38:19 +02:00
|
|
|
psa_drv_se_context_t *psa_get_se_driver_context(
|
|
|
|
psa_se_drv_table_entry_t *driver );
|
|
|
|
|
2019-07-12 23:46:04 +02:00
|
|
|
/** Find a free slot for a key that is to be created.
|
|
|
|
*
|
|
|
|
* This function calls the relevant method in the driver to find a suitable
|
|
|
|
* slot for a key with the given attributes.
|
|
|
|
*
|
|
|
|
* \param[in] attributes Metadata about the key that is about to be created.
|
|
|
|
* \param[in] driver The driver table entry to query.
|
|
|
|
* \param[out] slot_number On success, a slot number that is free in this
|
|
|
|
* secure element.
|
|
|
|
*/
|
|
|
|
psa_status_t psa_find_se_slot_for_key(
|
|
|
|
const psa_key_attributes_t *attributes,
|
2019-08-05 16:44:14 +02:00
|
|
|
psa_key_creation_method_t method,
|
2019-07-12 23:46:04 +02:00
|
|
|
psa_se_drv_table_entry_t *driver,
|
|
|
|
psa_key_slot_number_t *slot_number );
|
|
|
|
|
2019-07-12 23:46:38 +02:00
|
|
|
/** Destoy a key in a secure element.
|
|
|
|
*
|
|
|
|
* This function calls the relevant driver method to destroy a key
|
|
|
|
* and updates the driver's persistent data.
|
|
|
|
*/
|
|
|
|
psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
|
|
|
|
psa_key_slot_number_t slot_number );
|
|
|
|
|
2019-07-12 23:38:19 +02:00
|
|
|
/** Load the persistent data of a secure element driver.
|
|
|
|
*
|
|
|
|
* \param driver The driver table entry containing the persistent
|
|
|
|
* data to load from storage.
|
|
|
|
*/
|
|
|
|
psa_status_t psa_load_se_persistent_data(
|
|
|
|
const psa_se_drv_table_entry_t *driver );
|
|
|
|
|
|
|
|
/** Save the persistent data of a secure element driver.
|
|
|
|
*
|
|
|
|
* \param[in] driver The driver table entry containing the persistent
|
|
|
|
* data to save to storage.
|
|
|
|
*/
|
|
|
|
psa_status_t psa_save_se_persistent_data(
|
|
|
|
const psa_se_drv_table_entry_t *driver );
|
2019-06-26 18:18:12 +02:00
|
|
|
|
2019-07-23 17:38:08 +02:00
|
|
|
/** Destroy the persistent data of a secure element driver.
|
|
|
|
*
|
|
|
|
* This is currently only used for testing.
|
|
|
|
*
|
2020-05-10 00:44:04 +02:00
|
|
|
* \param[in] location The location identifier for the driver whose
|
|
|
|
* persistent data is to be erased.
|
2019-07-23 17:38:08 +02:00
|
|
|
*/
|
2020-05-10 00:44:04 +02:00
|
|
|
psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location );
|
2019-07-23 17:38:08 +02:00
|
|
|
|
2019-07-30 21:32:04 +02:00
|
|
|
|
|
|
|
/** The storage representation of a key whose data is in a secure element.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t slot_number[sizeof( psa_key_slot_number_t )];
|
|
|
|
uint8_t bits[sizeof( psa_key_bits_t )];
|
|
|
|
} psa_se_key_data_storage_t;
|
|
|
|
|
2019-06-24 14:06:43 +02:00
|
|
|
#endif /* PSA_CRYPTO_SE_H */
|