132b31808SJens Wiklander /** 232b31808SJens Wiklander * \file psa/crypto_se_driver.h 332b31808SJens Wiklander * \brief PSA external cryptoprocessor driver module 432b31808SJens Wiklander * 532b31808SJens Wiklander * This header declares types and function signatures for cryptography 632b31808SJens Wiklander * drivers that access key material via opaque references. 732b31808SJens Wiklander * This is meant for cryptoprocessors that have a separate key storage from the 832b31808SJens Wiklander * space in which the PSA Crypto implementation runs, typically secure 932b31808SJens Wiklander * elements (SEs). 1032b31808SJens Wiklander * 1132b31808SJens Wiklander * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer), 1232b31808SJens Wiklander * containing functions for driver developers to implement to enable hardware 1332b31808SJens Wiklander * to be called in a standardized way by a PSA Cryptography API 1432b31808SJens Wiklander * implementation. The functions comprising the driver HAL, which driver 1532b31808SJens Wiklander * authors implement, are not intended to be called by application developers. 1632b31808SJens Wiklander */ 1732b31808SJens Wiklander 1832b31808SJens Wiklander /* 1932b31808SJens Wiklander * Copyright The Mbed TLS Contributors 20*b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2132b31808SJens Wiklander */ 2232b31808SJens Wiklander #ifndef PSA_CRYPTO_SE_DRIVER_H 2332b31808SJens Wiklander #define PSA_CRYPTO_SE_DRIVER_H 2432b31808SJens Wiklander #include "mbedtls/private_access.h" 2532b31808SJens Wiklander 2632b31808SJens Wiklander #include "crypto_driver_common.h" 2732b31808SJens Wiklander 2832b31808SJens Wiklander #ifdef __cplusplus 2932b31808SJens Wiklander extern "C" { 3032b31808SJens Wiklander #endif 3132b31808SJens Wiklander 3232b31808SJens Wiklander /** \defgroup se_init Secure element driver initialization 3332b31808SJens Wiklander */ 3432b31808SJens Wiklander /**@{*/ 3532b31808SJens Wiklander 3632b31808SJens Wiklander /** \brief Driver context structure 3732b31808SJens Wiklander * 3832b31808SJens Wiklander * Driver functions receive a pointer to this structure. 3932b31808SJens Wiklander * Each registered driver has one instance of this structure. 4032b31808SJens Wiklander * 4132b31808SJens Wiklander * Implementations must include the fields specified here and 4232b31808SJens Wiklander * may include other fields. 4332b31808SJens Wiklander */ 4432b31808SJens Wiklander typedef struct { 4532b31808SJens Wiklander /** A read-only pointer to the driver's persistent data. 4632b31808SJens Wiklander * 4732b31808SJens Wiklander * Drivers typically use this persistent data to keep track of 4832b31808SJens Wiklander * which slot numbers are available. This is only a guideline: 4932b31808SJens Wiklander * drivers may use the persistent data for any purpose, keeping 5032b31808SJens Wiklander * in mind the restrictions on when the persistent data is saved 5132b31808SJens Wiklander * to storage: the persistent data is only saved after calling 5232b31808SJens Wiklander * certain functions that receive a writable pointer to the 5332b31808SJens Wiklander * persistent data. 5432b31808SJens Wiklander * 5532b31808SJens Wiklander * The core allocates a memory buffer for the persistent data. 5632b31808SJens Wiklander * The pointer is guaranteed to be suitably aligned for any data type, 5732b31808SJens Wiklander * like a pointer returned by `malloc` (but the core can use any 5832b31808SJens Wiklander * method to allocate the buffer, not necessarily `malloc`). 5932b31808SJens Wiklander * 6032b31808SJens Wiklander * The size of this buffer is in the \c persistent_data_size field of 6132b31808SJens Wiklander * this structure. 6232b31808SJens Wiklander * 6332b31808SJens Wiklander * Before the driver is initialized for the first time, the content of 6432b31808SJens Wiklander * the persistent data is all-bits-zero. After a driver upgrade, if the 6532b31808SJens Wiklander * size of the persistent data has increased, the original data is padded 6632b31808SJens Wiklander * on the right with zeros; if the size has decreased, the original data 6732b31808SJens Wiklander * is truncated to the new size. 6832b31808SJens Wiklander * 6932b31808SJens Wiklander * This pointer is to read-only data. Only a few driver functions are 7032b31808SJens Wiklander * allowed to modify the persistent data. These functions receive a 7132b31808SJens Wiklander * writable pointer. These functions are: 7232b31808SJens Wiklander * - psa_drv_se_t::p_init 7332b31808SJens Wiklander * - psa_drv_se_key_management_t::p_allocate 7432b31808SJens Wiklander * - psa_drv_se_key_management_t::p_destroy 7532b31808SJens Wiklander * 7632b31808SJens Wiklander * The PSA Cryptography core saves the persistent data from one 7732b31808SJens Wiklander * session to the next. It does this before returning from API functions 7832b31808SJens Wiklander * that call a driver method that is allowed to modify the persistent 7932b31808SJens Wiklander * data, specifically: 8032b31808SJens Wiklander * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call 8132b31808SJens Wiklander * psa_drv_se_key_management_t::p_destroy to complete an action 8232b31808SJens Wiklander * that was interrupted by a power failure. 8332b31808SJens Wiklander * - Key creation functions cause a call to 8432b31808SJens Wiklander * psa_drv_se_key_management_t::p_allocate, and may cause a call to 8532b31808SJens Wiklander * psa_drv_se_key_management_t::p_destroy in case an error occurs. 8632b31808SJens Wiklander * - psa_destroy_key() causes a call to 8732b31808SJens Wiklander * psa_drv_se_key_management_t::p_destroy. 8832b31808SJens Wiklander */ 8932b31808SJens Wiklander const void *const MBEDTLS_PRIVATE(persistent_data); 9032b31808SJens Wiklander 9132b31808SJens Wiklander /** The size of \c persistent_data in bytes. 9232b31808SJens Wiklander * 9332b31808SJens Wiklander * This is always equal to the value of the `persistent_data_size` field 9432b31808SJens Wiklander * of the ::psa_drv_se_t structure when the driver is registered. 9532b31808SJens Wiklander */ 9632b31808SJens Wiklander const size_t MBEDTLS_PRIVATE(persistent_data_size); 9732b31808SJens Wiklander 9832b31808SJens Wiklander /** Driver transient data. 9932b31808SJens Wiklander * 10032b31808SJens Wiklander * The core initializes this value to 0 and does not read or modify it 10132b31808SJens Wiklander * afterwards. The driver may store whatever it wants in this field. 10232b31808SJens Wiklander */ 10332b31808SJens Wiklander uintptr_t MBEDTLS_PRIVATE(transient_data); 10432b31808SJens Wiklander } psa_drv_se_context_t; 10532b31808SJens Wiklander 10632b31808SJens Wiklander /** \brief A driver initialization function. 10732b31808SJens Wiklander * 10832b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 10932b31808SJens Wiklander * \param[in,out] persistent_data A pointer to the persistent data 11032b31808SJens Wiklander * that allows writing. 11132b31808SJens Wiklander * \param location The location value for which this driver 11232b31808SJens Wiklander * is registered. The driver will be invoked 11332b31808SJens Wiklander * for all keys whose lifetime is in this 11432b31808SJens Wiklander * location. 11532b31808SJens Wiklander * 11632b31808SJens Wiklander * \retval #PSA_SUCCESS 11732b31808SJens Wiklander * The driver is operational. 11832b31808SJens Wiklander * The core will update the persistent data in storage. 11932b31808SJens Wiklander * \return 12032b31808SJens Wiklander * Any other return value prevents the driver from being used in 12132b31808SJens Wiklander * this session. 12232b31808SJens Wiklander * The core will NOT update the persistent data in storage. 12332b31808SJens Wiklander */ 12432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context, 12532b31808SJens Wiklander void *persistent_data, 12632b31808SJens Wiklander psa_key_location_t location); 12732b31808SJens Wiklander 12832b31808SJens Wiklander #if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C) 129*b0563631STom Van Eyck /* Mbed TLS with secure element support enabled defines this type in 13032b31808SJens Wiklander * crypto_types.h because it is also visible to applications through an 13132b31808SJens Wiklander * implementation-specific extension. 13232b31808SJens Wiklander * For the PSA Cryptography specification, this type is only visible 13332b31808SJens Wiklander * via crypto_se_driver.h. */ 13432b31808SJens Wiklander /** An internal designation of a key slot between the core part of the 13532b31808SJens Wiklander * PSA Crypto implementation and the driver. The meaning of this value 13632b31808SJens Wiklander * is driver-dependent. */ 13732b31808SJens Wiklander typedef uint64_t psa_key_slot_number_t; 13832b31808SJens Wiklander #endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */ 13932b31808SJens Wiklander 14032b31808SJens Wiklander /**@}*/ 14132b31808SJens Wiklander 14232b31808SJens Wiklander /** \defgroup se_mac Secure Element Message Authentication Codes 14332b31808SJens Wiklander * Generation and authentication of Message Authentication Codes (MACs) using 14432b31808SJens Wiklander * a secure element can be done either as a single function call (via the 14532b31808SJens Wiklander * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in 14632b31808SJens Wiklander * parts using the following sequence: 14732b31808SJens Wiklander * - `psa_drv_se_mac_setup_t` 14832b31808SJens Wiklander * - `psa_drv_se_mac_update_t` 14932b31808SJens Wiklander * - `psa_drv_se_mac_update_t` 15032b31808SJens Wiklander * - ... 15132b31808SJens Wiklander * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t` 15232b31808SJens Wiklander * 15332b31808SJens Wiklander * If a previously started secure element MAC operation needs to be terminated, 15432b31808SJens Wiklander * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may 15532b31808SJens Wiklander * result in allocated resources not being freed or in other undefined 15632b31808SJens Wiklander * behavior. 15732b31808SJens Wiklander */ 15832b31808SJens Wiklander /**@{*/ 15932b31808SJens Wiklander /** \brief A function that starts a secure element MAC operation for a PSA 16032b31808SJens Wiklander * Crypto Driver implementation 16132b31808SJens Wiklander * 16232b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 16332b31808SJens Wiklander * \param[in,out] op_context A structure that will contain the 16432b31808SJens Wiklander * hardware-specific MAC context 16532b31808SJens Wiklander * \param[in] key_slot The slot of the key to be used for the 16632b31808SJens Wiklander * operation 16732b31808SJens Wiklander * \param[in] algorithm The algorithm to be used to underly the MAC 16832b31808SJens Wiklander * operation 16932b31808SJens Wiklander * 17032b31808SJens Wiklander * \retval #PSA_SUCCESS 17132b31808SJens Wiklander * Success. 17232b31808SJens Wiklander */ 17332b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context, 17432b31808SJens Wiklander void *op_context, 17532b31808SJens Wiklander psa_key_slot_number_t key_slot, 17632b31808SJens Wiklander psa_algorithm_t algorithm); 17732b31808SJens Wiklander 17832b31808SJens Wiklander /** \brief A function that continues a previously started secure element MAC 17932b31808SJens Wiklander * operation 18032b31808SJens Wiklander * 18132b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the 18232b31808SJens Wiklander * previously-established MAC operation to be 18332b31808SJens Wiklander * updated 18432b31808SJens Wiklander * \param[in] p_input A buffer containing the message to be appended 18532b31808SJens Wiklander * to the MAC operation 18632b31808SJens Wiklander * \param[in] input_length The size in bytes of the input message buffer 18732b31808SJens Wiklander */ 18832b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context, 18932b31808SJens Wiklander const uint8_t *p_input, 19032b31808SJens Wiklander size_t input_length); 19132b31808SJens Wiklander 19232b31808SJens Wiklander /** \brief a function that completes a previously started secure element MAC 19332b31808SJens Wiklander * operation by returning the resulting MAC. 19432b31808SJens Wiklander * 19532b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the 19632b31808SJens Wiklander * previously started MAC operation to be 19732b31808SJens Wiklander * finished 19832b31808SJens Wiklander * \param[out] p_mac A buffer where the generated MAC will be 19932b31808SJens Wiklander * placed 20032b31808SJens Wiklander * \param[in] mac_size The size in bytes of the buffer that has been 20132b31808SJens Wiklander * allocated for the `output` buffer 20232b31808SJens Wiklander * \param[out] p_mac_length After completion, will contain the number of 20332b31808SJens Wiklander * bytes placed in the `p_mac` buffer 20432b31808SJens Wiklander * 20532b31808SJens Wiklander * \retval #PSA_SUCCESS 20632b31808SJens Wiklander * Success. 20732b31808SJens Wiklander */ 20832b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context, 20932b31808SJens Wiklander uint8_t *p_mac, 21032b31808SJens Wiklander size_t mac_size, 21132b31808SJens Wiklander size_t *p_mac_length); 21232b31808SJens Wiklander 21332b31808SJens Wiklander /** \brief A function that completes a previously started secure element MAC 21432b31808SJens Wiklander * operation by comparing the resulting MAC against a provided value 21532b31808SJens Wiklander * 21632b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the previously 21732b31808SJens Wiklander * started MAC operation to be finished 21832b31808SJens Wiklander * \param[in] p_mac The MAC value against which the resulting MAC 21932b31808SJens Wiklander * will be compared against 22032b31808SJens Wiklander * \param[in] mac_length The size in bytes of the value stored in `p_mac` 22132b31808SJens Wiklander * 22232b31808SJens Wiklander * \retval #PSA_SUCCESS 22332b31808SJens Wiklander * The operation completed successfully and the MACs matched each 22432b31808SJens Wiklander * other 22532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 22632b31808SJens Wiklander * The operation completed successfully, but the calculated MAC did 22732b31808SJens Wiklander * not match the provided MAC 22832b31808SJens Wiklander */ 22932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context, 23032b31808SJens Wiklander const uint8_t *p_mac, 23132b31808SJens Wiklander size_t mac_length); 23232b31808SJens Wiklander 23332b31808SJens Wiklander /** \brief A function that aborts a previous started secure element MAC 23432b31808SJens Wiklander * operation 23532b31808SJens Wiklander * 23632b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the previously 23732b31808SJens Wiklander * started MAC operation to be aborted 23832b31808SJens Wiklander */ 23932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context); 24032b31808SJens Wiklander 24132b31808SJens Wiklander /** \brief A function that performs a secure element MAC operation in one 24232b31808SJens Wiklander * command and returns the calculated MAC 24332b31808SJens Wiklander * 24432b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 24532b31808SJens Wiklander * \param[in] p_input A buffer containing the message to be MACed 24632b31808SJens Wiklander * \param[in] input_length The size in bytes of `p_input` 24732b31808SJens Wiklander * \param[in] key_slot The slot of the key to be used 24832b31808SJens Wiklander * \param[in] alg The algorithm to be used to underlie the MAC 24932b31808SJens Wiklander * operation 25032b31808SJens Wiklander * \param[out] p_mac A buffer where the generated MAC will be 25132b31808SJens Wiklander * placed 25232b31808SJens Wiklander * \param[in] mac_size The size in bytes of the `p_mac` buffer 25332b31808SJens Wiklander * \param[out] p_mac_length After completion, will contain the number of 25432b31808SJens Wiklander * bytes placed in the `output` buffer 25532b31808SJens Wiklander * 25632b31808SJens Wiklander * \retval #PSA_SUCCESS 25732b31808SJens Wiklander * Success. 25832b31808SJens Wiklander */ 25932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context, 26032b31808SJens Wiklander const uint8_t *p_input, 26132b31808SJens Wiklander size_t input_length, 26232b31808SJens Wiklander psa_key_slot_number_t key_slot, 26332b31808SJens Wiklander psa_algorithm_t alg, 26432b31808SJens Wiklander uint8_t *p_mac, 26532b31808SJens Wiklander size_t mac_size, 26632b31808SJens Wiklander size_t *p_mac_length); 26732b31808SJens Wiklander 26832b31808SJens Wiklander /** \brief A function that performs a secure element MAC operation in one 26932b31808SJens Wiklander * command and compares the resulting MAC against a provided value 27032b31808SJens Wiklander * 27132b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 27232b31808SJens Wiklander * \param[in] p_input A buffer containing the message to be MACed 27332b31808SJens Wiklander * \param[in] input_length The size in bytes of `input` 27432b31808SJens Wiklander * \param[in] key_slot The slot of the key to be used 27532b31808SJens Wiklander * \param[in] alg The algorithm to be used to underlie the MAC 27632b31808SJens Wiklander * operation 27732b31808SJens Wiklander * \param[in] p_mac The MAC value against which the resulting MAC will 27832b31808SJens Wiklander * be compared against 27932b31808SJens Wiklander * \param[in] mac_length The size in bytes of `mac` 28032b31808SJens Wiklander * 28132b31808SJens Wiklander * \retval #PSA_SUCCESS 28232b31808SJens Wiklander * The operation completed successfully and the MACs matched each 28332b31808SJens Wiklander * other 28432b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_SIGNATURE 28532b31808SJens Wiklander * The operation completed successfully, but the calculated MAC did 28632b31808SJens Wiklander * not match the provided MAC 28732b31808SJens Wiklander */ 28832b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context, 28932b31808SJens Wiklander const uint8_t *p_input, 29032b31808SJens Wiklander size_t input_length, 29132b31808SJens Wiklander psa_key_slot_number_t key_slot, 29232b31808SJens Wiklander psa_algorithm_t alg, 29332b31808SJens Wiklander const uint8_t *p_mac, 29432b31808SJens Wiklander size_t mac_length); 29532b31808SJens Wiklander 29632b31808SJens Wiklander /** \brief A struct containing all of the function pointers needed to 29732b31808SJens Wiklander * perform secure element MAC operations 29832b31808SJens Wiklander * 29932b31808SJens Wiklander * PSA Crypto API implementations should populate the table as appropriate 30032b31808SJens Wiklander * upon startup. 30132b31808SJens Wiklander * 30232b31808SJens Wiklander * If one of the functions is not implemented (such as 30332b31808SJens Wiklander * `psa_drv_se_mac_generate_t`), it should be set to NULL. 30432b31808SJens Wiklander * 30532b31808SJens Wiklander * Driver implementers should ensure that they implement all of the functions 30632b31808SJens Wiklander * that make sense for their hardware, and that they provide a full solution 30732b31808SJens Wiklander * (for example, if they support `p_setup`, they should also support 30832b31808SJens Wiklander * `p_update` and at least one of `p_finish` or `p_finish_verify`). 30932b31808SJens Wiklander * 31032b31808SJens Wiklander */ 31132b31808SJens Wiklander typedef struct { 31232b31808SJens Wiklander /**The size in bytes of the hardware-specific secure element MAC context 31332b31808SJens Wiklander * structure 31432b31808SJens Wiklander */ 31532b31808SJens Wiklander size_t MBEDTLS_PRIVATE(context_size); 31632b31808SJens Wiklander /** Function that performs a MAC setup operation 31732b31808SJens Wiklander */ 31832b31808SJens Wiklander psa_drv_se_mac_setup_t MBEDTLS_PRIVATE(p_setup); 31932b31808SJens Wiklander /** Function that performs a MAC update operation 32032b31808SJens Wiklander */ 32132b31808SJens Wiklander psa_drv_se_mac_update_t MBEDTLS_PRIVATE(p_update); 32232b31808SJens Wiklander /** Function that completes a MAC operation 32332b31808SJens Wiklander */ 32432b31808SJens Wiklander psa_drv_se_mac_finish_t MBEDTLS_PRIVATE(p_finish); 32532b31808SJens Wiklander /** Function that completes a MAC operation with a verify check 32632b31808SJens Wiklander */ 32732b31808SJens Wiklander psa_drv_se_mac_finish_verify_t MBEDTLS_PRIVATE(p_finish_verify); 32832b31808SJens Wiklander /** Function that aborts a previously started MAC operation 32932b31808SJens Wiklander */ 33032b31808SJens Wiklander psa_drv_se_mac_abort_t MBEDTLS_PRIVATE(p_abort); 33132b31808SJens Wiklander /** Function that performs a MAC operation in one call 33232b31808SJens Wiklander */ 33332b31808SJens Wiklander psa_drv_se_mac_generate_t MBEDTLS_PRIVATE(p_mac); 33432b31808SJens Wiklander /** Function that performs a MAC and verify operation in one call 33532b31808SJens Wiklander */ 33632b31808SJens Wiklander psa_drv_se_mac_verify_t MBEDTLS_PRIVATE(p_mac_verify); 33732b31808SJens Wiklander } psa_drv_se_mac_t; 33832b31808SJens Wiklander /**@}*/ 33932b31808SJens Wiklander 34032b31808SJens Wiklander /** \defgroup se_cipher Secure Element Symmetric Ciphers 34132b31808SJens Wiklander * 34232b31808SJens Wiklander * Encryption and Decryption using secure element keys in block modes other 34332b31808SJens Wiklander * than ECB must be done in multiple parts, using the following flow: 34432b31808SJens Wiklander * - `psa_drv_se_cipher_setup_t` 34532b31808SJens Wiklander * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode) 34632b31808SJens Wiklander * - `psa_drv_se_cipher_update_t` 34732b31808SJens Wiklander * - `psa_drv_se_cipher_update_t` 34832b31808SJens Wiklander * - ... 34932b31808SJens Wiklander * - `psa_drv_se_cipher_finish_t` 35032b31808SJens Wiklander * 35132b31808SJens Wiklander * If a previously started secure element Cipher operation needs to be 35232b31808SJens Wiklander * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure 35332b31808SJens Wiklander * to do so may result in allocated resources not being freed or in other 35432b31808SJens Wiklander * undefined behavior. 35532b31808SJens Wiklander * 35632b31808SJens Wiklander * In situations where a PSA Cryptographic API implementation is using a block 35732b31808SJens Wiklander * mode not-supported by the underlying hardware or driver, it can construct 35832b31808SJens Wiklander * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function 35932b31808SJens Wiklander * for the cipher operations. 36032b31808SJens Wiklander */ 36132b31808SJens Wiklander /**@{*/ 36232b31808SJens Wiklander 36332b31808SJens Wiklander /** \brief A function that provides the cipher setup function for a 36432b31808SJens Wiklander * secure element driver 36532b31808SJens Wiklander * 36632b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 36732b31808SJens Wiklander * \param[in,out] op_context A structure that will contain the 36832b31808SJens Wiklander * hardware-specific cipher context. 36932b31808SJens Wiklander * \param[in] key_slot The slot of the key to be used for the 37032b31808SJens Wiklander * operation 37132b31808SJens Wiklander * \param[in] algorithm The algorithm to be used in the cipher 37232b31808SJens Wiklander * operation 37332b31808SJens Wiklander * \param[in] direction Indicates whether the operation is an encrypt 37432b31808SJens Wiklander * or decrypt 37532b31808SJens Wiklander * 37632b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 37732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 37832b31808SJens Wiklander */ 37932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context, 38032b31808SJens Wiklander void *op_context, 38132b31808SJens Wiklander psa_key_slot_number_t key_slot, 38232b31808SJens Wiklander psa_algorithm_t algorithm, 38332b31808SJens Wiklander psa_encrypt_or_decrypt_t direction); 38432b31808SJens Wiklander 38532b31808SJens Wiklander /** \brief A function that sets the initialization vector (if 38632b31808SJens Wiklander * necessary) for a secure element cipher operation 38732b31808SJens Wiklander * 38832b31808SJens Wiklander * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has 38932b31808SJens Wiklander * two IV functions: one to set the IV, and one to generate it internally. The 39032b31808SJens Wiklander * generate function is not necessary for the drivers to implement as the PSA 39132b31808SJens Wiklander * Crypto implementation can do the generation using its RNG features. 39232b31808SJens Wiklander * 39332b31808SJens Wiklander * \param[in,out] op_context A structure that contains the previously set up 39432b31808SJens Wiklander * hardware-specific cipher context 39532b31808SJens Wiklander * \param[in] p_iv A buffer containing the initialization vector 39632b31808SJens Wiklander * \param[in] iv_length The size (in bytes) of the `p_iv` buffer 39732b31808SJens Wiklander * 39832b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 39932b31808SJens Wiklander */ 40032b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context, 40132b31808SJens Wiklander const uint8_t *p_iv, 40232b31808SJens Wiklander size_t iv_length); 40332b31808SJens Wiklander 40432b31808SJens Wiklander /** \brief A function that continues a previously started secure element cipher 40532b31808SJens Wiklander * operation 40632b31808SJens Wiklander * 40732b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the 40832b31808SJens Wiklander * previously started cipher operation 40932b31808SJens Wiklander * \param[in] p_input A buffer containing the data to be 41032b31808SJens Wiklander * encrypted/decrypted 41132b31808SJens Wiklander * \param[in] input_size The size in bytes of the buffer pointed to 41232b31808SJens Wiklander * by `p_input` 41332b31808SJens Wiklander * \param[out] p_output The caller-allocated buffer where the 41432b31808SJens Wiklander * output will be placed 41532b31808SJens Wiklander * \param[in] output_size The allocated size in bytes of the 41632b31808SJens Wiklander * `p_output` buffer 41732b31808SJens Wiklander * \param[out] p_output_length After completion, will contain the number 41832b31808SJens Wiklander * of bytes placed in the `p_output` buffer 41932b31808SJens Wiklander * 42032b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 42132b31808SJens Wiklander */ 42232b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context, 42332b31808SJens Wiklander const uint8_t *p_input, 42432b31808SJens Wiklander size_t input_size, 42532b31808SJens Wiklander uint8_t *p_output, 42632b31808SJens Wiklander size_t output_size, 42732b31808SJens Wiklander size_t *p_output_length); 42832b31808SJens Wiklander 42932b31808SJens Wiklander /** \brief A function that completes a previously started secure element cipher 43032b31808SJens Wiklander * operation 43132b31808SJens Wiklander * 43232b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the 43332b31808SJens Wiklander * previously started cipher operation 43432b31808SJens Wiklander * \param[out] p_output The caller-allocated buffer where the output 43532b31808SJens Wiklander * will be placed 43632b31808SJens Wiklander * \param[in] output_size The allocated size in bytes of the `p_output` 43732b31808SJens Wiklander * buffer 43832b31808SJens Wiklander * \param[out] p_output_length After completion, will contain the number of 43932b31808SJens Wiklander * bytes placed in the `p_output` buffer 44032b31808SJens Wiklander * 44132b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 44232b31808SJens Wiklander */ 44332b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context, 44432b31808SJens Wiklander uint8_t *p_output, 44532b31808SJens Wiklander size_t output_size, 44632b31808SJens Wiklander size_t *p_output_length); 44732b31808SJens Wiklander 44832b31808SJens Wiklander /** \brief A function that aborts a previously started secure element cipher 44932b31808SJens Wiklander * operation 45032b31808SJens Wiklander * 45132b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure for the 45232b31808SJens Wiklander * previously started cipher operation 45332b31808SJens Wiklander */ 45432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context); 45532b31808SJens Wiklander 45632b31808SJens Wiklander /** \brief A function that performs the ECB block mode for secure element 45732b31808SJens Wiklander * cipher operations 45832b31808SJens Wiklander * 45932b31808SJens Wiklander * Note: this function should only be used with implementations that do not 46032b31808SJens Wiklander * provide a needed higher-level operation. 46132b31808SJens Wiklander * 46232b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 46332b31808SJens Wiklander * \param[in] key_slot The slot of the key to be used for the operation 46432b31808SJens Wiklander * \param[in] algorithm The algorithm to be used in the cipher operation 46532b31808SJens Wiklander * \param[in] direction Indicates whether the operation is an encrypt or 46632b31808SJens Wiklander * decrypt 46732b31808SJens Wiklander * \param[in] p_input A buffer containing the data to be 46832b31808SJens Wiklander * encrypted/decrypted 46932b31808SJens Wiklander * \param[in] input_size The size in bytes of the buffer pointed to by 47032b31808SJens Wiklander * `p_input` 47132b31808SJens Wiklander * \param[out] p_output The caller-allocated buffer where the output 47232b31808SJens Wiklander * will be placed 47332b31808SJens Wiklander * \param[in] output_size The allocated size in bytes of the `p_output` 47432b31808SJens Wiklander * buffer 47532b31808SJens Wiklander * 47632b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 47732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 47832b31808SJens Wiklander */ 47932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context, 48032b31808SJens Wiklander psa_key_slot_number_t key_slot, 48132b31808SJens Wiklander psa_algorithm_t algorithm, 48232b31808SJens Wiklander psa_encrypt_or_decrypt_t direction, 48332b31808SJens Wiklander const uint8_t *p_input, 48432b31808SJens Wiklander size_t input_size, 48532b31808SJens Wiklander uint8_t *p_output, 48632b31808SJens Wiklander size_t output_size); 48732b31808SJens Wiklander 48832b31808SJens Wiklander /** 48932b31808SJens Wiklander * \brief A struct containing all of the function pointers needed to implement 49032b31808SJens Wiklander * cipher operations using secure elements. 49132b31808SJens Wiklander * 49232b31808SJens Wiklander * PSA Crypto API implementations should populate instances of the table as 49332b31808SJens Wiklander * appropriate upon startup or at build time. 49432b31808SJens Wiklander * 49532b31808SJens Wiklander * If one of the functions is not implemented (such as 49632b31808SJens Wiklander * `psa_drv_se_cipher_ecb_t`), it should be set to NULL. 49732b31808SJens Wiklander */ 49832b31808SJens Wiklander typedef struct { 49932b31808SJens Wiklander /** The size in bytes of the hardware-specific secure element cipher 50032b31808SJens Wiklander * context structure 50132b31808SJens Wiklander */ 50232b31808SJens Wiklander size_t MBEDTLS_PRIVATE(context_size); 50332b31808SJens Wiklander /** Function that performs a cipher setup operation */ 50432b31808SJens Wiklander psa_drv_se_cipher_setup_t MBEDTLS_PRIVATE(p_setup); 50532b31808SJens Wiklander /** Function that sets a cipher IV (if necessary) */ 50632b31808SJens Wiklander psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv); 50732b31808SJens Wiklander /** Function that performs a cipher update operation */ 50832b31808SJens Wiklander psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update); 50932b31808SJens Wiklander /** Function that completes a cipher operation */ 51032b31808SJens Wiklander psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish); 51132b31808SJens Wiklander /** Function that aborts a cipher operation */ 51232b31808SJens Wiklander psa_drv_se_cipher_abort_t MBEDTLS_PRIVATE(p_abort); 51332b31808SJens Wiklander /** Function that performs ECB mode for a cipher operation 51432b31808SJens Wiklander * (Danger: ECB mode should not be used directly by clients of the PSA 51532b31808SJens Wiklander * Crypto Client API) 51632b31808SJens Wiklander */ 51732b31808SJens Wiklander psa_drv_se_cipher_ecb_t MBEDTLS_PRIVATE(p_ecb); 51832b31808SJens Wiklander } psa_drv_se_cipher_t; 51932b31808SJens Wiklander 52032b31808SJens Wiklander /**@}*/ 52132b31808SJens Wiklander 52232b31808SJens Wiklander /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography 52332b31808SJens Wiklander * 52432b31808SJens Wiklander * Since the amount of data that can (or should) be encrypted or signed using 52532b31808SJens Wiklander * asymmetric keys is limited by the key size, asymmetric key operations using 52632b31808SJens Wiklander * keys in a secure element must be done in single function calls. 52732b31808SJens Wiklander */ 52832b31808SJens Wiklander /**@{*/ 52932b31808SJens Wiklander 53032b31808SJens Wiklander /** 53132b31808SJens Wiklander * \brief A function that signs a hash or short message with a private key in 53232b31808SJens Wiklander * a secure element 53332b31808SJens Wiklander * 53432b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 53532b31808SJens Wiklander * \param[in] key_slot Key slot of an asymmetric key pair 53632b31808SJens Wiklander * \param[in] alg A signature algorithm that is compatible 53732b31808SJens Wiklander * with the type of `key` 53832b31808SJens Wiklander * \param[in] p_hash The hash to sign 53932b31808SJens Wiklander * \param[in] hash_length Size of the `p_hash` buffer in bytes 54032b31808SJens Wiklander * \param[out] p_signature Buffer where the signature is to be written 54132b31808SJens Wiklander * \param[in] signature_size Size of the `p_signature` buffer in bytes 54232b31808SJens Wiklander * \param[out] p_signature_length On success, the number of bytes 54332b31808SJens Wiklander * that make up the returned signature value 54432b31808SJens Wiklander * 54532b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 54632b31808SJens Wiklander */ 54732b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context, 54832b31808SJens Wiklander psa_key_slot_number_t key_slot, 54932b31808SJens Wiklander psa_algorithm_t alg, 55032b31808SJens Wiklander const uint8_t *p_hash, 55132b31808SJens Wiklander size_t hash_length, 55232b31808SJens Wiklander uint8_t *p_signature, 55332b31808SJens Wiklander size_t signature_size, 55432b31808SJens Wiklander size_t *p_signature_length); 55532b31808SJens Wiklander 55632b31808SJens Wiklander /** 55732b31808SJens Wiklander * \brief A function that verifies the signature a hash or short message using 55832b31808SJens Wiklander * an asymmetric public key in a secure element 55932b31808SJens Wiklander * 56032b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 56132b31808SJens Wiklander * \param[in] key_slot Key slot of a public key or an asymmetric key 56232b31808SJens Wiklander * pair 56332b31808SJens Wiklander * \param[in] alg A signature algorithm that is compatible with 56432b31808SJens Wiklander * the type of `key` 56532b31808SJens Wiklander * \param[in] p_hash The hash whose signature is to be verified 56632b31808SJens Wiklander * \param[in] hash_length Size of the `p_hash` buffer in bytes 56732b31808SJens Wiklander * \param[in] p_signature Buffer containing the signature to verify 56832b31808SJens Wiklander * \param[in] signature_length Size of the `p_signature` buffer in bytes 56932b31808SJens Wiklander * 57032b31808SJens Wiklander * \retval #PSA_SUCCESS 57132b31808SJens Wiklander * The signature is valid. 57232b31808SJens Wiklander */ 57332b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context, 57432b31808SJens Wiklander psa_key_slot_number_t key_slot, 57532b31808SJens Wiklander psa_algorithm_t alg, 57632b31808SJens Wiklander const uint8_t *p_hash, 57732b31808SJens Wiklander size_t hash_length, 57832b31808SJens Wiklander const uint8_t *p_signature, 57932b31808SJens Wiklander size_t signature_length); 58032b31808SJens Wiklander 58132b31808SJens Wiklander /** 58232b31808SJens Wiklander * \brief A function that encrypts a short message with an asymmetric public 58332b31808SJens Wiklander * key in a secure element 58432b31808SJens Wiklander * 58532b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 58632b31808SJens Wiklander * \param[in] key_slot Key slot of a public key or an asymmetric key 58732b31808SJens Wiklander * pair 58832b31808SJens Wiklander * \param[in] alg An asymmetric encryption algorithm that is 58932b31808SJens Wiklander * compatible with the type of `key` 59032b31808SJens Wiklander * \param[in] p_input The message to encrypt 59132b31808SJens Wiklander * \param[in] input_length Size of the `p_input` buffer in bytes 59232b31808SJens Wiklander * \param[in] p_salt A salt or label, if supported by the 59332b31808SJens Wiklander * encryption algorithm 59432b31808SJens Wiklander * If the algorithm does not support a 59532b31808SJens Wiklander * salt, pass `NULL`. 59632b31808SJens Wiklander * If the algorithm supports an optional 59732b31808SJens Wiklander * salt and you do not want to pass a salt, 59832b31808SJens Wiklander * pass `NULL`. 59932b31808SJens Wiklander * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 60032b31808SJens Wiklander * supported. 60132b31808SJens Wiklander * \param[in] salt_length Size of the `p_salt` buffer in bytes 60232b31808SJens Wiklander * If `p_salt` is `NULL`, pass 0. 60332b31808SJens Wiklander * \param[out] p_output Buffer where the encrypted message is to 60432b31808SJens Wiklander * be written 60532b31808SJens Wiklander * \param[in] output_size Size of the `p_output` buffer in bytes 60632b31808SJens Wiklander * \param[out] p_output_length On success, the number of bytes that make up 60732b31808SJens Wiklander * the returned output 60832b31808SJens Wiklander * 60932b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 61032b31808SJens Wiklander */ 61132b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context, 61232b31808SJens Wiklander psa_key_slot_number_t key_slot, 61332b31808SJens Wiklander psa_algorithm_t alg, 61432b31808SJens Wiklander const uint8_t *p_input, 61532b31808SJens Wiklander size_t input_length, 61632b31808SJens Wiklander const uint8_t *p_salt, 61732b31808SJens Wiklander size_t salt_length, 61832b31808SJens Wiklander uint8_t *p_output, 61932b31808SJens Wiklander size_t output_size, 62032b31808SJens Wiklander size_t *p_output_length); 62132b31808SJens Wiklander 62232b31808SJens Wiklander /** 62332b31808SJens Wiklander * \brief A function that decrypts a short message with an asymmetric private 62432b31808SJens Wiklander * key in a secure element. 62532b31808SJens Wiklander * 62632b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 62732b31808SJens Wiklander * \param[in] key_slot Key slot of an asymmetric key pair 62832b31808SJens Wiklander * \param[in] alg An asymmetric encryption algorithm that is 62932b31808SJens Wiklander * compatible with the type of `key` 63032b31808SJens Wiklander * \param[in] p_input The message to decrypt 63132b31808SJens Wiklander * \param[in] input_length Size of the `p_input` buffer in bytes 63232b31808SJens Wiklander * \param[in] p_salt A salt or label, if supported by the 63332b31808SJens Wiklander * encryption algorithm 63432b31808SJens Wiklander * If the algorithm does not support a 63532b31808SJens Wiklander * salt, pass `NULL`. 63632b31808SJens Wiklander * If the algorithm supports an optional 63732b31808SJens Wiklander * salt and you do not want to pass a salt, 63832b31808SJens Wiklander * pass `NULL`. 63932b31808SJens Wiklander * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 64032b31808SJens Wiklander * supported. 64132b31808SJens Wiklander * \param[in] salt_length Size of the `p_salt` buffer in bytes 64232b31808SJens Wiklander * If `p_salt` is `NULL`, pass 0. 64332b31808SJens Wiklander * \param[out] p_output Buffer where the decrypted message is to 64432b31808SJens Wiklander * be written 64532b31808SJens Wiklander * \param[in] output_size Size of the `p_output` buffer in bytes 64632b31808SJens Wiklander * \param[out] p_output_length On success, the number of bytes 64732b31808SJens Wiklander * that make up the returned output 64832b31808SJens Wiklander * 64932b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 65032b31808SJens Wiklander */ 65132b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context, 65232b31808SJens Wiklander psa_key_slot_number_t key_slot, 65332b31808SJens Wiklander psa_algorithm_t alg, 65432b31808SJens Wiklander const uint8_t *p_input, 65532b31808SJens Wiklander size_t input_length, 65632b31808SJens Wiklander const uint8_t *p_salt, 65732b31808SJens Wiklander size_t salt_length, 65832b31808SJens Wiklander uint8_t *p_output, 65932b31808SJens Wiklander size_t output_size, 66032b31808SJens Wiklander size_t *p_output_length); 66132b31808SJens Wiklander 66232b31808SJens Wiklander /** 66332b31808SJens Wiklander * \brief A struct containing all of the function pointers needed to implement 66432b31808SJens Wiklander * asymmetric cryptographic operations using secure elements. 66532b31808SJens Wiklander * 66632b31808SJens Wiklander * PSA Crypto API implementations should populate instances of the table as 66732b31808SJens Wiklander * appropriate upon startup or at build time. 66832b31808SJens Wiklander * 66932b31808SJens Wiklander * If one of the functions is not implemented, it should be set to NULL. 67032b31808SJens Wiklander */ 67132b31808SJens Wiklander typedef struct { 67232b31808SJens Wiklander /** Function that performs an asymmetric sign operation */ 67332b31808SJens Wiklander psa_drv_se_asymmetric_sign_t MBEDTLS_PRIVATE(p_sign); 67432b31808SJens Wiklander /** Function that performs an asymmetric verify operation */ 67532b31808SJens Wiklander psa_drv_se_asymmetric_verify_t MBEDTLS_PRIVATE(p_verify); 67632b31808SJens Wiklander /** Function that performs an asymmetric encrypt operation */ 67732b31808SJens Wiklander psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt); 67832b31808SJens Wiklander /** Function that performs an asymmetric decrypt operation */ 67932b31808SJens Wiklander psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt); 68032b31808SJens Wiklander } psa_drv_se_asymmetric_t; 68132b31808SJens Wiklander 68232b31808SJens Wiklander /**@}*/ 68332b31808SJens Wiklander 68432b31808SJens Wiklander /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data 68532b31808SJens Wiklander * Authenticated Encryption with Additional Data (AEAD) operations with secure 68632b31808SJens Wiklander * elements must be done in one function call. While this creates a burden for 68732b31808SJens Wiklander * implementers as there must be sufficient space in memory for the entire 68832b31808SJens Wiklander * message, it prevents decrypted data from being made available before the 68932b31808SJens Wiklander * authentication operation is complete and the data is known to be authentic. 69032b31808SJens Wiklander */ 69132b31808SJens Wiklander /**@{*/ 69232b31808SJens Wiklander 69332b31808SJens Wiklander /** \brief A function that performs a secure element authenticated encryption 69432b31808SJens Wiklander * operation 69532b31808SJens Wiklander * 69632b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 69732b31808SJens Wiklander * \param[in] key_slot Slot containing the key to use. 69832b31808SJens Wiklander * \param[in] algorithm The AEAD algorithm to compute 69932b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 70032b31808SJens Wiklander * #PSA_ALG_IS_AEAD(`alg`) is true) 70132b31808SJens Wiklander * \param[in] p_nonce Nonce or IV to use 70232b31808SJens Wiklander * \param[in] nonce_length Size of the `p_nonce` buffer in bytes 70332b31808SJens Wiklander * \param[in] p_additional_data Additional data that will be 70432b31808SJens Wiklander * authenticated but not encrypted 70532b31808SJens Wiklander * \param[in] additional_data_length Size of `p_additional_data` in bytes 70632b31808SJens Wiklander * \param[in] p_plaintext Data that will be authenticated and 70732b31808SJens Wiklander * encrypted 70832b31808SJens Wiklander * \param[in] plaintext_length Size of `p_plaintext` in bytes 70932b31808SJens Wiklander * \param[out] p_ciphertext Output buffer for the authenticated and 71032b31808SJens Wiklander * encrypted data. The additional data is 71132b31808SJens Wiklander * not part of this output. For algorithms 71232b31808SJens Wiklander * where the encrypted data and the 71332b31808SJens Wiklander * authentication tag are defined as 71432b31808SJens Wiklander * separate outputs, the authentication 71532b31808SJens Wiklander * tag is appended to the encrypted data. 71632b31808SJens Wiklander * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in 71732b31808SJens Wiklander * bytes 71832b31808SJens Wiklander * \param[out] p_ciphertext_length On success, the size of the output in 71932b31808SJens Wiklander * the `p_ciphertext` buffer 72032b31808SJens Wiklander * 72132b31808SJens Wiklander * \retval #PSA_SUCCESS 72232b31808SJens Wiklander * Success. 72332b31808SJens Wiklander */ 72432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context, 72532b31808SJens Wiklander psa_key_slot_number_t key_slot, 72632b31808SJens Wiklander psa_algorithm_t algorithm, 72732b31808SJens Wiklander const uint8_t *p_nonce, 72832b31808SJens Wiklander size_t nonce_length, 72932b31808SJens Wiklander const uint8_t *p_additional_data, 73032b31808SJens Wiklander size_t additional_data_length, 73132b31808SJens Wiklander const uint8_t *p_plaintext, 73232b31808SJens Wiklander size_t plaintext_length, 73332b31808SJens Wiklander uint8_t *p_ciphertext, 73432b31808SJens Wiklander size_t ciphertext_size, 73532b31808SJens Wiklander size_t *p_ciphertext_length); 73632b31808SJens Wiklander 73732b31808SJens Wiklander /** A function that performs a secure element authenticated decryption operation 73832b31808SJens Wiklander * 73932b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 74032b31808SJens Wiklander * \param[in] key_slot Slot containing the key to use 74132b31808SJens Wiklander * \param[in] algorithm The AEAD algorithm to compute 74232b31808SJens Wiklander * (\c PSA_ALG_XXX value such that 74332b31808SJens Wiklander * #PSA_ALG_IS_AEAD(`alg`) is true) 74432b31808SJens Wiklander * \param[in] p_nonce Nonce or IV to use 74532b31808SJens Wiklander * \param[in] nonce_length Size of the `p_nonce` buffer in bytes 74632b31808SJens Wiklander * \param[in] p_additional_data Additional data that has been 74732b31808SJens Wiklander * authenticated but not encrypted 74832b31808SJens Wiklander * \param[in] additional_data_length Size of `p_additional_data` in bytes 74932b31808SJens Wiklander * \param[in] p_ciphertext Data that has been authenticated and 75032b31808SJens Wiklander * encrypted. 75132b31808SJens Wiklander * For algorithms where the encrypted data 75232b31808SJens Wiklander * and the authentication tag are defined 75332b31808SJens Wiklander * as separate inputs, the buffer must 75432b31808SJens Wiklander * contain the encrypted data followed by 75532b31808SJens Wiklander * the authentication tag. 75632b31808SJens Wiklander * \param[in] ciphertext_length Size of `p_ciphertext` in bytes 75732b31808SJens Wiklander * \param[out] p_plaintext Output buffer for the decrypted data 75832b31808SJens Wiklander * \param[in] plaintext_size Size of the `p_plaintext` buffer in 75932b31808SJens Wiklander * bytes 76032b31808SJens Wiklander * \param[out] p_plaintext_length On success, the size of the output in 76132b31808SJens Wiklander * the `p_plaintext` buffer 76232b31808SJens Wiklander * 76332b31808SJens Wiklander * \retval #PSA_SUCCESS 76432b31808SJens Wiklander * Success. 76532b31808SJens Wiklander */ 76632b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context, 76732b31808SJens Wiklander psa_key_slot_number_t key_slot, 76832b31808SJens Wiklander psa_algorithm_t algorithm, 76932b31808SJens Wiklander const uint8_t *p_nonce, 77032b31808SJens Wiklander size_t nonce_length, 77132b31808SJens Wiklander const uint8_t *p_additional_data, 77232b31808SJens Wiklander size_t additional_data_length, 77332b31808SJens Wiklander const uint8_t *p_ciphertext, 77432b31808SJens Wiklander size_t ciphertext_length, 77532b31808SJens Wiklander uint8_t *p_plaintext, 77632b31808SJens Wiklander size_t plaintext_size, 77732b31808SJens Wiklander size_t *p_plaintext_length); 77832b31808SJens Wiklander 77932b31808SJens Wiklander /** 78032b31808SJens Wiklander * \brief A struct containing all of the function pointers needed to implement 78132b31808SJens Wiklander * secure element Authenticated Encryption with Additional Data operations 78232b31808SJens Wiklander * 78332b31808SJens Wiklander * PSA Crypto API implementations should populate instances of the table as 78432b31808SJens Wiklander * appropriate upon startup. 78532b31808SJens Wiklander * 78632b31808SJens Wiklander * If one of the functions is not implemented, it should be set to NULL. 78732b31808SJens Wiklander */ 78832b31808SJens Wiklander typedef struct { 78932b31808SJens Wiklander /** Function that performs the AEAD encrypt operation */ 79032b31808SJens Wiklander psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt); 79132b31808SJens Wiklander /** Function that performs the AEAD decrypt operation */ 79232b31808SJens Wiklander psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt); 79332b31808SJens Wiklander } psa_drv_se_aead_t; 79432b31808SJens Wiklander /**@}*/ 79532b31808SJens Wiklander 79632b31808SJens Wiklander /** \defgroup se_key_management Secure Element Key Management 79732b31808SJens Wiklander * Currently, key management is limited to importing keys in the clear, 79832b31808SJens Wiklander * destroying keys, and exporting keys in the clear. 79932b31808SJens Wiklander * Whether a key may be exported is determined by the key policies in place 80032b31808SJens Wiklander * on the key slot. 80132b31808SJens Wiklander */ 80232b31808SJens Wiklander /**@{*/ 80332b31808SJens Wiklander 80432b31808SJens Wiklander /** An enumeration indicating how a key is created. 80532b31808SJens Wiklander */ 80632b31808SJens Wiklander typedef enum { 80732b31808SJens Wiklander PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */ 80832b31808SJens Wiklander PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */ 80932b31808SJens Wiklander PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */ 81032b31808SJens Wiklander PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */ 81132b31808SJens Wiklander 81232b31808SJens Wiklander #ifndef __DOXYGEN_ONLY__ 81332b31808SJens Wiklander /** A key is being registered with mbedtls_psa_register_se_key(). 81432b31808SJens Wiklander * 81532b31808SJens Wiklander * The core only passes this value to 81632b31808SJens Wiklander * psa_drv_se_key_management_t::p_validate_slot_number, not to 81732b31808SJens Wiklander * psa_drv_se_key_management_t::p_allocate. The call to 81832b31808SJens Wiklander * `p_validate_slot_number` is not followed by any other call to the 81932b31808SJens Wiklander * driver: the key is considered successfully registered if the call to 82032b31808SJens Wiklander * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is 82132b31808SJens Wiklander * null. 82232b31808SJens Wiklander * 82332b31808SJens Wiklander * With this creation method, the driver must return #PSA_SUCCESS if 82432b31808SJens Wiklander * the given attributes are compatible with the existing key in the slot, 82532b31808SJens Wiklander * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there 82632b31808SJens Wiklander * is no key with the specified slot number. 82732b31808SJens Wiklander * 828*b0563631STom Van Eyck * This is an Mbed TLS extension. 82932b31808SJens Wiklander */ 83032b31808SJens Wiklander PSA_KEY_CREATION_REGISTER, 83132b31808SJens Wiklander #endif 83232b31808SJens Wiklander } psa_key_creation_method_t; 83332b31808SJens Wiklander 83432b31808SJens Wiklander /** \brief A function that allocates a slot for a key. 83532b31808SJens Wiklander * 83632b31808SJens Wiklander * To create a key in a specific slot in a secure element, the core 83732b31808SJens Wiklander * first calls this function to determine a valid slot number, 83832b31808SJens Wiklander * then calls a function to create the key material in that slot. 83932b31808SJens Wiklander * In nominal conditions (that is, if no error occurs), 84032b31808SJens Wiklander * the effect of a call to a key creation function in the PSA Cryptography 84132b31808SJens Wiklander * API with a lifetime that places the key in a secure element is the 84232b31808SJens Wiklander * following: 84332b31808SJens Wiklander * -# The core calls psa_drv_se_key_management_t::p_allocate 84432b31808SJens Wiklander * (or in some implementations 84532b31808SJens Wiklander * psa_drv_se_key_management_t::p_validate_slot_number). The driver 84632b31808SJens Wiklander * selects (or validates) a suitable slot number given the key attributes 84732b31808SJens Wiklander * and the state of the secure element. 84832b31808SJens Wiklander * -# The core calls a key creation function in the driver. 84932b31808SJens Wiklander * 85032b31808SJens Wiklander * The key creation functions in the PSA Cryptography API are: 85132b31808SJens Wiklander * - psa_import_key(), which causes 85232b31808SJens Wiklander * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT 85332b31808SJens Wiklander * then a call to psa_drv_se_key_management_t::p_import. 85432b31808SJens Wiklander * - psa_generate_key(), which causes 85532b31808SJens Wiklander * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE 85632b31808SJens Wiklander * then a call to psa_drv_se_key_management_t::p_import. 85732b31808SJens Wiklander * - psa_key_derivation_output_key(), which causes 85832b31808SJens Wiklander * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE 85932b31808SJens Wiklander * then a call to psa_drv_se_key_derivation_t::p_derive. 86032b31808SJens Wiklander * - psa_copy_key(), which causes 86132b31808SJens Wiklander * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY 86232b31808SJens Wiklander * then a call to psa_drv_se_key_management_t::p_export. 86332b31808SJens Wiklander * 86432b31808SJens Wiklander * In case of errors, other behaviors are possible. 86532b31808SJens Wiklander * - If the PSA Cryptography subsystem dies after the first step, 86632b31808SJens Wiklander * for example because the device has lost power abruptly, 86732b31808SJens Wiklander * the second step may never happen, or may happen after a reset 86832b31808SJens Wiklander * and re-initialization. Alternatively, after a reset and 86932b31808SJens Wiklander * re-initialization, the core may call 87032b31808SJens Wiklander * psa_drv_se_key_management_t::p_destroy on the slot number that 87132b31808SJens Wiklander * was allocated (or validated) instead of calling a key creation function. 87232b31808SJens Wiklander * - If an error occurs, the core may call 87332b31808SJens Wiklander * psa_drv_se_key_management_t::p_destroy on the slot number that 87432b31808SJens Wiklander * was allocated (or validated) instead of calling a key creation function. 87532b31808SJens Wiklander * 87632b31808SJens Wiklander * Errors and system resets also have an impact on the driver's persistent 87732b31808SJens Wiklander * data. If a reset happens before the overall key creation process is 87832b31808SJens Wiklander * completed (before or after the second step above), it is unspecified 87932b31808SJens Wiklander * whether the persistent data after the reset is identical to what it 88032b31808SJens Wiklander * was before or after the call to `p_allocate` (or `p_validate_slot_number`). 88132b31808SJens Wiklander * 88232b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 88332b31808SJens Wiklander * \param[in,out] persistent_data A pointer to the persistent data 88432b31808SJens Wiklander * that allows writing. 88532b31808SJens Wiklander * \param[in] attributes Attributes of the key. 88632b31808SJens Wiklander * \param method The way in which the key is being created. 88732b31808SJens Wiklander * \param[out] key_slot Slot where the key will be stored. 88832b31808SJens Wiklander * This must be a valid slot for a key of the 88932b31808SJens Wiklander * chosen type. It must be unoccupied. 89032b31808SJens Wiklander * 89132b31808SJens Wiklander * \retval #PSA_SUCCESS 89232b31808SJens Wiklander * Success. 89332b31808SJens Wiklander * The core will record \c *key_slot as the key slot where the key 89432b31808SJens Wiklander * is stored and will update the persistent data in storage. 89532b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 89632b31808SJens Wiklander * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 89732b31808SJens Wiklander */ 89832b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_allocate_key_t)( 89932b31808SJens Wiklander psa_drv_se_context_t *drv_context, 90032b31808SJens Wiklander void *persistent_data, 90132b31808SJens Wiklander const psa_key_attributes_t *attributes, 90232b31808SJens Wiklander psa_key_creation_method_t method, 90332b31808SJens Wiklander psa_key_slot_number_t *key_slot); 90432b31808SJens Wiklander 90532b31808SJens Wiklander /** \brief A function that determines whether a slot number is valid 90632b31808SJens Wiklander * for a key. 90732b31808SJens Wiklander * 90832b31808SJens Wiklander * To create a key in a specific slot in a secure element, the core 90932b31808SJens Wiklander * first calls this function to validate the choice of slot number, 91032b31808SJens Wiklander * then calls a function to create the key material in that slot. 91132b31808SJens Wiklander * See the documentation of #psa_drv_se_allocate_key_t for more details. 91232b31808SJens Wiklander * 91332b31808SJens Wiklander * As of the PSA Cryptography API specification version 1.0, there is no way 91432b31808SJens Wiklander * for applications to trigger a call to this function. However some 91532b31808SJens Wiklander * implementations offer the capability to create or declare a key in 91632b31808SJens Wiklander * a specific slot via implementation-specific means, generally for the 91732b31808SJens Wiklander * sake of initial device provisioning or onboarding. Such a mechanism may 91832b31808SJens Wiklander * be added to a future version of the PSA Cryptography API specification. 91932b31808SJens Wiklander * 92032b31808SJens Wiklander * This function may update the driver's persistent data through 92132b31808SJens Wiklander * \p persistent_data. The core will save the updated persistent data at the 92232b31808SJens Wiklander * end of the key creation process. See the description of 92332b31808SJens Wiklander * ::psa_drv_se_allocate_key_t for more information. 92432b31808SJens Wiklander * 92532b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 92632b31808SJens Wiklander * \param[in,out] persistent_data A pointer to the persistent data 92732b31808SJens Wiklander * that allows writing. 92832b31808SJens Wiklander * \param[in] attributes Attributes of the key. 92932b31808SJens Wiklander * \param method The way in which the key is being created. 93032b31808SJens Wiklander * \param[in] key_slot Slot where the key is to be stored. 93132b31808SJens Wiklander * 93232b31808SJens Wiklander * \retval #PSA_SUCCESS 93332b31808SJens Wiklander * The given slot number is valid for a key with the given 93432b31808SJens Wiklander * attributes. 93532b31808SJens Wiklander * \retval #PSA_ERROR_INVALID_ARGUMENT 93632b31808SJens Wiklander * The given slot number is not valid for a key with the 93732b31808SJens Wiklander * given attributes. This includes the case where the slot 93832b31808SJens Wiklander * number is not valid at all. 93932b31808SJens Wiklander * \retval #PSA_ERROR_ALREADY_EXISTS 94032b31808SJens Wiklander * There is already a key with the specified slot number. 94132b31808SJens Wiklander * Drivers may choose to return this error from the key 94232b31808SJens Wiklander * creation function instead. 94332b31808SJens Wiklander */ 94432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_validate_slot_number_t)( 94532b31808SJens Wiklander psa_drv_se_context_t *drv_context, 94632b31808SJens Wiklander void *persistent_data, 94732b31808SJens Wiklander const psa_key_attributes_t *attributes, 94832b31808SJens Wiklander psa_key_creation_method_t method, 94932b31808SJens Wiklander psa_key_slot_number_t key_slot); 95032b31808SJens Wiklander 95132b31808SJens Wiklander /** \brief A function that imports a key into a secure element in binary format 95232b31808SJens Wiklander * 95332b31808SJens Wiklander * This function can support any output from psa_export_key(). Refer to the 95432b31808SJens Wiklander * documentation of psa_export_key() for the format for each key type. 95532b31808SJens Wiklander * 95632b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 95732b31808SJens Wiklander * \param key_slot Slot where the key will be stored. 95832b31808SJens Wiklander * This must be a valid slot for a key of the 95932b31808SJens Wiklander * chosen type. It must be unoccupied. 96032b31808SJens Wiklander * \param[in] attributes The key attributes, including the lifetime, 96132b31808SJens Wiklander * the key type and the usage policy. 96232b31808SJens Wiklander * Drivers should not access the key size stored 96332b31808SJens Wiklander * in the attributes: it may not match the 96432b31808SJens Wiklander * data passed in \p data. 96532b31808SJens Wiklander * Drivers can call psa_get_key_lifetime(), 96632b31808SJens Wiklander * psa_get_key_type(), 96732b31808SJens Wiklander * psa_get_key_usage_flags() and 96832b31808SJens Wiklander * psa_get_key_algorithm() to access this 96932b31808SJens Wiklander * information. 97032b31808SJens Wiklander * \param[in] data Buffer containing the key data. 97132b31808SJens Wiklander * \param[in] data_length Size of the \p data buffer in bytes. 97232b31808SJens Wiklander * \param[out] bits On success, the key size in bits. The driver 97332b31808SJens Wiklander * must determine this value after parsing the 97432b31808SJens Wiklander * key according to the key type. 97532b31808SJens Wiklander * This value is not used if the function fails. 97632b31808SJens Wiklander * 97732b31808SJens Wiklander * \retval #PSA_SUCCESS 97832b31808SJens Wiklander * Success. 97932b31808SJens Wiklander */ 98032b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_import_key_t)( 98132b31808SJens Wiklander psa_drv_se_context_t *drv_context, 98232b31808SJens Wiklander psa_key_slot_number_t key_slot, 98332b31808SJens Wiklander const psa_key_attributes_t *attributes, 98432b31808SJens Wiklander const uint8_t *data, 98532b31808SJens Wiklander size_t data_length, 98632b31808SJens Wiklander size_t *bits); 98732b31808SJens Wiklander 98832b31808SJens Wiklander /** 98932b31808SJens Wiklander * \brief A function that destroys a secure element key and restore the slot to 99032b31808SJens Wiklander * its default state 99132b31808SJens Wiklander * 99232b31808SJens Wiklander * This function destroys the content of the key from a secure element. 99332b31808SJens Wiklander * Implementations shall make a best effort to ensure that any previous content 99432b31808SJens Wiklander * of the slot is unrecoverable. 99532b31808SJens Wiklander * 99632b31808SJens Wiklander * This function returns the specified slot to its default state. 99732b31808SJens Wiklander * 99832b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 99932b31808SJens Wiklander * \param[in,out] persistent_data A pointer to the persistent data 100032b31808SJens Wiklander * that allows writing. 100132b31808SJens Wiklander * \param key_slot The key slot to erase. 100232b31808SJens Wiklander * 100332b31808SJens Wiklander * \retval #PSA_SUCCESS 100432b31808SJens Wiklander * The slot's content, if any, has been erased. 100532b31808SJens Wiklander */ 100632b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_destroy_key_t)( 100732b31808SJens Wiklander psa_drv_se_context_t *drv_context, 100832b31808SJens Wiklander void *persistent_data, 100932b31808SJens Wiklander psa_key_slot_number_t key_slot); 101032b31808SJens Wiklander 101132b31808SJens Wiklander /** 101232b31808SJens Wiklander * \brief A function that exports a secure element key in binary format 101332b31808SJens Wiklander * 101432b31808SJens Wiklander * The output of this function can be passed to psa_import_key() to 101532b31808SJens Wiklander * create an equivalent object. 101632b31808SJens Wiklander * 101732b31808SJens Wiklander * If a key is created with `psa_import_key()` and then exported with 101832b31808SJens Wiklander * this function, it is not guaranteed that the resulting data is 101932b31808SJens Wiklander * identical: the implementation may choose a different representation 102032b31808SJens Wiklander * of the same key if the format permits it. 102132b31808SJens Wiklander * 102232b31808SJens Wiklander * This function should generate output in the same format that 102332b31808SJens Wiklander * `psa_export_key()` does. Refer to the 102432b31808SJens Wiklander * documentation of `psa_export_key()` for the format for each key type. 102532b31808SJens Wiklander * 102632b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 102732b31808SJens Wiklander * \param[in] key Slot whose content is to be exported. This must 102832b31808SJens Wiklander * be an occupied key slot. 102932b31808SJens Wiklander * \param[out] p_data Buffer where the key data is to be written. 103032b31808SJens Wiklander * \param[in] data_size Size of the `p_data` buffer in bytes. 103132b31808SJens Wiklander * \param[out] p_data_length On success, the number of bytes 103232b31808SJens Wiklander * that make up the key data. 103332b31808SJens Wiklander * 103432b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 103532b31808SJens Wiklander * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription 103632b31808SJens Wiklander * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 103732b31808SJens Wiklander * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 103832b31808SJens Wiklander * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 103932b31808SJens Wiklander * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 104032b31808SJens Wiklander * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 104132b31808SJens Wiklander */ 104232b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context, 104332b31808SJens Wiklander psa_key_slot_number_t key, 104432b31808SJens Wiklander uint8_t *p_data, 104532b31808SJens Wiklander size_t data_size, 104632b31808SJens Wiklander size_t *p_data_length); 104732b31808SJens Wiklander 104832b31808SJens Wiklander /** 104932b31808SJens Wiklander * \brief A function that generates a symmetric or asymmetric key on a secure 105032b31808SJens Wiklander * element 105132b31808SJens Wiklander * 105232b31808SJens Wiklander * If the key type \c type recorded in \p attributes 105332b31808SJens Wiklander * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1), 105432b31808SJens Wiklander * the driver may export the public key at the time of generation, 105532b31808SJens Wiklander * in the format documented for psa_export_public_key() by writing it 105632b31808SJens Wiklander * to the \p pubkey buffer. 105732b31808SJens Wiklander * This is optional, intended for secure elements that output the 105832b31808SJens Wiklander * public key at generation time and that cannot export the public key 105932b31808SJens Wiklander * later. Drivers that do not need this feature should leave 106032b31808SJens Wiklander * \p *pubkey_length set to 0 and should 106132b31808SJens Wiklander * implement the psa_drv_key_management_t::p_export_public function. 106232b31808SJens Wiklander * Some implementations do not support this feature, in which case 106332b31808SJens Wiklander * \p pubkey is \c NULL and \p pubkey_size is 0. 106432b31808SJens Wiklander * 106532b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 106632b31808SJens Wiklander * \param key_slot Slot where the key will be stored. 106732b31808SJens Wiklander * This must be a valid slot for a key of the 106832b31808SJens Wiklander * chosen type. It must be unoccupied. 106932b31808SJens Wiklander * \param[in] attributes The key attributes, including the lifetime, 107032b31808SJens Wiklander * the key type and size, and the usage policy. 107132b31808SJens Wiklander * Drivers can call psa_get_key_lifetime(), 107232b31808SJens Wiklander * psa_get_key_type(), psa_get_key_bits(), 107332b31808SJens Wiklander * psa_get_key_usage_flags() and 107432b31808SJens Wiklander * psa_get_key_algorithm() to access this 107532b31808SJens Wiklander * information. 107632b31808SJens Wiklander * \param[out] pubkey A buffer where the driver can write the 107732b31808SJens Wiklander * public key, when generating an asymmetric 107832b31808SJens Wiklander * key pair. 107932b31808SJens Wiklander * This is \c NULL when generating a symmetric 108032b31808SJens Wiklander * key or if the core does not support 108132b31808SJens Wiklander * exporting the public key at generation time. 108232b31808SJens Wiklander * \param pubkey_size The size of the `pubkey` buffer in bytes. 108332b31808SJens Wiklander * This is 0 when generating a symmetric 108432b31808SJens Wiklander * key or if the core does not support 108532b31808SJens Wiklander * exporting the public key at generation time. 108632b31808SJens Wiklander * \param[out] pubkey_length On entry, this is always 0. 108732b31808SJens Wiklander * On success, the number of bytes written to 108832b31808SJens Wiklander * \p pubkey. If this is 0 or unchanged on return, 108932b31808SJens Wiklander * the core will not read the \p pubkey buffer, 109032b31808SJens Wiklander * and will instead call the driver's 109132b31808SJens Wiklander * psa_drv_key_management_t::p_export_public 109232b31808SJens Wiklander * function to export the public key when needed. 109332b31808SJens Wiklander */ 109432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_generate_key_t)( 109532b31808SJens Wiklander psa_drv_se_context_t *drv_context, 109632b31808SJens Wiklander psa_key_slot_number_t key_slot, 109732b31808SJens Wiklander const psa_key_attributes_t *attributes, 109832b31808SJens Wiklander uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length); 109932b31808SJens Wiklander 110032b31808SJens Wiklander /** 110132b31808SJens Wiklander * \brief A struct containing all of the function pointers needed to for secure 110232b31808SJens Wiklander * element key management 110332b31808SJens Wiklander * 110432b31808SJens Wiklander * PSA Crypto API implementations should populate instances of the table as 110532b31808SJens Wiklander * appropriate upon startup or at build time. 110632b31808SJens Wiklander * 110732b31808SJens Wiklander * If one of the functions is not implemented, it should be set to NULL. 110832b31808SJens Wiklander */ 110932b31808SJens Wiklander typedef struct { 111032b31808SJens Wiklander /** Function that allocates a slot for a key. */ 111132b31808SJens Wiklander psa_drv_se_allocate_key_t MBEDTLS_PRIVATE(p_allocate); 111232b31808SJens Wiklander /** Function that checks the validity of a slot for a key. */ 111332b31808SJens Wiklander psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number); 111432b31808SJens Wiklander /** Function that performs a key import operation */ 111532b31808SJens Wiklander psa_drv_se_import_key_t MBEDTLS_PRIVATE(p_import); 111632b31808SJens Wiklander /** Function that performs a generation */ 111732b31808SJens Wiklander psa_drv_se_generate_key_t MBEDTLS_PRIVATE(p_generate); 111832b31808SJens Wiklander /** Function that performs a key destroy operation */ 111932b31808SJens Wiklander psa_drv_se_destroy_key_t MBEDTLS_PRIVATE(p_destroy); 112032b31808SJens Wiklander /** Function that performs a key export operation */ 112132b31808SJens Wiklander psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export); 112232b31808SJens Wiklander /** Function that performs a public key export operation */ 112332b31808SJens Wiklander psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export_public); 112432b31808SJens Wiklander } psa_drv_se_key_management_t; 112532b31808SJens Wiklander 112632b31808SJens Wiklander /**@}*/ 112732b31808SJens Wiklander 112832b31808SJens Wiklander /** \defgroup driver_derivation Secure Element Key Derivation and Agreement 112932b31808SJens Wiklander * Key derivation is the process of generating new key material using an 113032b31808SJens Wiklander * existing key and additional parameters, iterating through a basic 113132b31808SJens Wiklander * cryptographic function, such as a hash. 113232b31808SJens Wiklander * Key agreement is a part of cryptographic protocols that allows two parties 113332b31808SJens Wiklander * to agree on the same key value, but starting from different original key 113432b31808SJens Wiklander * material. 113532b31808SJens Wiklander * The flows are similar, and the PSA Crypto Driver Model uses the same functions 113632b31808SJens Wiklander * for both of the flows. 113732b31808SJens Wiklander * 113832b31808SJens Wiklander * There are two different final functions for the flows, 113932b31808SJens Wiklander * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`. 114032b31808SJens Wiklander * `psa_drv_se_key_derivation_derive` is used when the key material should be 114132b31808SJens Wiklander * placed in a slot on the hardware and not exposed to the caller. 114232b31808SJens Wiklander * `psa_drv_se_key_derivation_export` is used when the key material should be 114332b31808SJens Wiklander * returned to the PSA Cryptographic API implementation. 114432b31808SJens Wiklander * 114532b31808SJens Wiklander * Different key derivation algorithms require a different number of inputs. 114632b31808SJens Wiklander * Instead of having an API that takes as input variable length arrays, which 114732b31808SJens Wiklander * can be problematic to manage on embedded platforms, the inputs are passed 114832b31808SJens Wiklander * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that 114932b31808SJens Wiklander * is called multiple times with different `collateral_id`s. Thus, for a key 115032b31808SJens Wiklander * derivation algorithm that required 3 parameter inputs, the flow would look 115132b31808SJens Wiklander * something like: 115232b31808SJens Wiklander * ~~~~~~~~~~~~~{.c} 115332b31808SJens Wiklander * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes); 115432b31808SJens Wiklander * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0, 115532b31808SJens Wiklander * p_collateral_0, 115632b31808SJens Wiklander * collateral_0_size); 115732b31808SJens Wiklander * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1, 115832b31808SJens Wiklander * p_collateral_1, 115932b31808SJens Wiklander * collateral_1_size); 116032b31808SJens Wiklander * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2, 116132b31808SJens Wiklander * p_collateral_2, 116232b31808SJens Wiklander * collateral_2_size); 116332b31808SJens Wiklander * psa_drv_se_key_derivation_derive(); 116432b31808SJens Wiklander * ~~~~~~~~~~~~~ 116532b31808SJens Wiklander * 116632b31808SJens Wiklander * key agreement example: 116732b31808SJens Wiklander * ~~~~~~~~~~~~~{.c} 116832b31808SJens Wiklander * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes); 116932b31808SJens Wiklander * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size); 117032b31808SJens Wiklander * psa_drv_se_key_derivation_export(p_session_key, 117132b31808SJens Wiklander * session_key_size, 117232b31808SJens Wiklander * &session_key_length); 117332b31808SJens Wiklander * ~~~~~~~~~~~~~ 117432b31808SJens Wiklander */ 117532b31808SJens Wiklander /**@{*/ 117632b31808SJens Wiklander 117732b31808SJens Wiklander /** \brief A function that Sets up a secure element key derivation operation by 117832b31808SJens Wiklander * specifying the algorithm and the source key sot 117932b31808SJens Wiklander * 118032b31808SJens Wiklander * \param[in,out] drv_context The driver context structure. 118132b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure containing any 118232b31808SJens Wiklander * context information for the implementation 118332b31808SJens Wiklander * \param[in] kdf_alg The algorithm to be used for the key derivation 118432b31808SJens Wiklander * \param[in] source_key The key to be used as the source material for 118532b31808SJens Wiklander * the key derivation 118632b31808SJens Wiklander * 118732b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 118832b31808SJens Wiklander */ 118932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context, 119032b31808SJens Wiklander void *op_context, 119132b31808SJens Wiklander psa_algorithm_t kdf_alg, 119232b31808SJens Wiklander psa_key_slot_number_t source_key); 119332b31808SJens Wiklander 119432b31808SJens Wiklander /** \brief A function that provides collateral (parameters) needed for a secure 119532b31808SJens Wiklander * element key derivation or key agreement operation 119632b31808SJens Wiklander * 119732b31808SJens Wiklander * Since many key derivation algorithms require multiple parameters, it is 119832b31808SJens Wiklander * expected that this function may be called multiple times for the same 119932b31808SJens Wiklander * operation, each with a different algorithm-specific `collateral_id` 120032b31808SJens Wiklander * 120132b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure containing any 120232b31808SJens Wiklander * context information for the implementation 120332b31808SJens Wiklander * \param[in] collateral_id An ID for the collateral being provided 120432b31808SJens Wiklander * \param[in] p_collateral A buffer containing the collateral data 120532b31808SJens Wiklander * \param[in] collateral_size The size in bytes of the collateral 120632b31808SJens Wiklander * 120732b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 120832b31808SJens Wiklander */ 120932b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context, 121032b31808SJens Wiklander uint32_t collateral_id, 121132b31808SJens Wiklander const uint8_t *p_collateral, 121232b31808SJens Wiklander size_t collateral_size); 121332b31808SJens Wiklander 121432b31808SJens Wiklander /** \brief A function that performs the final secure element key derivation 121532b31808SJens Wiklander * step and place the generated key material in a slot 121632b31808SJens Wiklander * 121732b31808SJens Wiklander * \param[in,out] op_context A hardware-specific structure containing any 121832b31808SJens Wiklander * context information for the implementation 121932b31808SJens Wiklander * \param[in] dest_key The slot where the generated key material 122032b31808SJens Wiklander * should be placed 122132b31808SJens Wiklander * 122232b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 122332b31808SJens Wiklander */ 122432b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context, 122532b31808SJens Wiklander psa_key_slot_number_t dest_key); 122632b31808SJens Wiklander 122732b31808SJens Wiklander /** \brief A function that performs the final step of a secure element key 122832b31808SJens Wiklander * agreement and place the generated key material in a buffer 122932b31808SJens Wiklander * 123032b31808SJens Wiklander * \param[out] p_output Buffer in which to place the generated key 123132b31808SJens Wiklander * material 123232b31808SJens Wiklander * \param[in] output_size The size in bytes of `p_output` 123332b31808SJens Wiklander * \param[out] p_output_length Upon success, contains the number of bytes of 123432b31808SJens Wiklander * key material placed in `p_output` 123532b31808SJens Wiklander * 123632b31808SJens Wiklander * \retval #PSA_SUCCESS \emptydescription 123732b31808SJens Wiklander */ 123832b31808SJens Wiklander typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context, 123932b31808SJens Wiklander uint8_t *p_output, 124032b31808SJens Wiklander size_t output_size, 124132b31808SJens Wiklander size_t *p_output_length); 124232b31808SJens Wiklander 124332b31808SJens Wiklander /** 124432b31808SJens Wiklander * \brief A struct containing all of the function pointers needed to for secure 124532b31808SJens Wiklander * element key derivation and agreement 124632b31808SJens Wiklander * 124732b31808SJens Wiklander * PSA Crypto API implementations should populate instances of the table as 124832b31808SJens Wiklander * appropriate upon startup. 124932b31808SJens Wiklander * 125032b31808SJens Wiklander * If one of the functions is not implemented, it should be set to NULL. 125132b31808SJens Wiklander */ 125232b31808SJens Wiklander typedef struct { 125332b31808SJens Wiklander /** The driver-specific size of the key derivation context */ 125432b31808SJens Wiklander size_t MBEDTLS_PRIVATE(context_size); 125532b31808SJens Wiklander /** Function that performs a key derivation setup */ 125632b31808SJens Wiklander psa_drv_se_key_derivation_setup_t MBEDTLS_PRIVATE(p_setup); 125732b31808SJens Wiklander /** Function that sets key derivation collateral */ 125832b31808SJens Wiklander psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral); 125932b31808SJens Wiklander /** Function that performs a final key derivation step */ 126032b31808SJens Wiklander psa_drv_se_key_derivation_derive_t MBEDTLS_PRIVATE(p_derive); 126132b31808SJens Wiklander /** Function that performs a final key derivation or agreement and 126232b31808SJens Wiklander * exports the key */ 126332b31808SJens Wiklander psa_drv_se_key_derivation_export_t MBEDTLS_PRIVATE(p_export); 126432b31808SJens Wiklander } psa_drv_se_key_derivation_t; 126532b31808SJens Wiklander 126632b31808SJens Wiklander /**@}*/ 126732b31808SJens Wiklander 126832b31808SJens Wiklander /** \defgroup se_registration Secure element driver registration 126932b31808SJens Wiklander */ 127032b31808SJens Wiklander /**@{*/ 127132b31808SJens Wiklander 127232b31808SJens Wiklander /** A structure containing pointers to all the entry points of a 127332b31808SJens Wiklander * secure element driver. 127432b31808SJens Wiklander * 127532b31808SJens Wiklander * Future versions of this specification may add extra substructures at 127632b31808SJens Wiklander * the end of this structure. 127732b31808SJens Wiklander */ 127832b31808SJens Wiklander typedef struct { 127932b31808SJens Wiklander /** The version of the driver HAL that this driver implements. 128032b31808SJens Wiklander * This is a protection against loading driver binaries built against 128132b31808SJens Wiklander * a different version of this specification. 128232b31808SJens Wiklander * Use #PSA_DRV_SE_HAL_VERSION. 128332b31808SJens Wiklander */ 128432b31808SJens Wiklander uint32_t MBEDTLS_PRIVATE(hal_version); 128532b31808SJens Wiklander 128632b31808SJens Wiklander /** The size of the driver's persistent data in bytes. 128732b31808SJens Wiklander * 128832b31808SJens Wiklander * This can be 0 if the driver does not need persistent data. 128932b31808SJens Wiklander * 129032b31808SJens Wiklander * See the documentation of psa_drv_se_context_t::persistent_data 129132b31808SJens Wiklander * for more information about why and how a driver can use 129232b31808SJens Wiklander * persistent data. 129332b31808SJens Wiklander */ 129432b31808SJens Wiklander size_t MBEDTLS_PRIVATE(persistent_data_size); 129532b31808SJens Wiklander 129632b31808SJens Wiklander /** The driver initialization function. 129732b31808SJens Wiklander * 129832b31808SJens Wiklander * This function is called once during the initialization of the 129932b31808SJens Wiklander * PSA Cryptography subsystem, before any other function of the 130032b31808SJens Wiklander * driver is called. If this function returns a failure status, 130132b31808SJens Wiklander * the driver will be unusable, at least until the next system reset. 130232b31808SJens Wiklander * 130332b31808SJens Wiklander * If this field is \c NULL, it is equivalent to a function that does 130432b31808SJens Wiklander * nothing and returns #PSA_SUCCESS. 130532b31808SJens Wiklander */ 130632b31808SJens Wiklander psa_drv_se_init_t MBEDTLS_PRIVATE(p_init); 130732b31808SJens Wiklander 130832b31808SJens Wiklander const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management); 130932b31808SJens Wiklander const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac); 131032b31808SJens Wiklander const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher); 131132b31808SJens Wiklander const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead); 131232b31808SJens Wiklander const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric); 131332b31808SJens Wiklander const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation); 131432b31808SJens Wiklander } psa_drv_se_t; 131532b31808SJens Wiklander 131632b31808SJens Wiklander /** The current version of the secure element driver HAL. 131732b31808SJens Wiklander */ 131832b31808SJens Wiklander /* 0.0.0 patchlevel 5 */ 131932b31808SJens Wiklander #define PSA_DRV_SE_HAL_VERSION 0x00000005 132032b31808SJens Wiklander 132132b31808SJens Wiklander /** Register an external cryptoprocessor (secure element) driver. 132232b31808SJens Wiklander * 132332b31808SJens Wiklander * This function is only intended to be used by driver code, not by 132432b31808SJens Wiklander * application code. In implementations with separation between the 132532b31808SJens Wiklander * PSA cryptography module and applications, this function should 132632b31808SJens Wiklander * only be available to callers that run in the same memory space as 132732b31808SJens Wiklander * the cryptography module, and should not be exposed to applications 132832b31808SJens Wiklander * running in a different memory space. 132932b31808SJens Wiklander * 133032b31808SJens Wiklander * This function may be called before psa_crypto_init(). It is 133132b31808SJens Wiklander * implementation-defined whether this function may be called 133232b31808SJens Wiklander * after psa_crypto_init(). 133332b31808SJens Wiklander * 133432b31808SJens Wiklander * \note Implementations store metadata about keys including the lifetime 133532b31808SJens Wiklander * value, which contains the driver's location indicator. Therefore, 133632b31808SJens Wiklander * from one instantiation of the PSA Cryptography 133732b31808SJens Wiklander * library to the next one, if there is a key in storage with a certain 133832b31808SJens Wiklander * lifetime value, you must always register the same driver (or an 133932b31808SJens Wiklander * updated version that communicates with the same secure element) 134032b31808SJens Wiklander * with the same location value. 134132b31808SJens Wiklander * 134232b31808SJens Wiklander * \param location The location value through which this driver will 134332b31808SJens Wiklander * be exposed to applications. 134432b31808SJens Wiklander * This driver will be used for all keys such that 134532b31808SJens Wiklander * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`. 134632b31808SJens Wiklander * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved 134732b31808SJens Wiklander * and may not be used for drivers. Implementations 134832b31808SJens Wiklander * may reserve other values. 134932b31808SJens Wiklander * \param[in] methods The method table of the driver. This structure must 135032b31808SJens Wiklander * remain valid for as long as the cryptography 135132b31808SJens Wiklander * module keeps running. It is typically a global 135232b31808SJens Wiklander * constant. 135332b31808SJens Wiklander * 135432b31808SJens Wiklander * \return #PSA_SUCCESS 135532b31808SJens Wiklander * The driver was successfully registered. Applications can now 135632b31808SJens Wiklander * use \p location to access keys through the methods passed to 135732b31808SJens Wiklander * this function. 135832b31808SJens Wiklander * \return #PSA_ERROR_BAD_STATE 135932b31808SJens Wiklander * This function was called after the initialization of the 136032b31808SJens Wiklander * cryptography module, and this implementation does not support 136132b31808SJens Wiklander * driver registration at this stage. 136232b31808SJens Wiklander * \return #PSA_ERROR_ALREADY_EXISTS 136332b31808SJens Wiklander * There is already a registered driver for this value of \p location. 136432b31808SJens Wiklander * \return #PSA_ERROR_INVALID_ARGUMENT 136532b31808SJens Wiklander * \p location is a reserved value. 136632b31808SJens Wiklander * \return #PSA_ERROR_NOT_SUPPORTED 136732b31808SJens Wiklander * `methods->hal_version` is not supported by this implementation. 136832b31808SJens Wiklander * \return #PSA_ERROR_INSUFFICIENT_MEMORY 136932b31808SJens Wiklander * \return #PSA_ERROR_NOT_PERMITTED 137032b31808SJens Wiklander * \return #PSA_ERROR_STORAGE_FAILURE 137132b31808SJens Wiklander * \return #PSA_ERROR_DATA_CORRUPT 137232b31808SJens Wiklander */ 137332b31808SJens Wiklander psa_status_t psa_register_se_driver( 137432b31808SJens Wiklander psa_key_location_t location, 137532b31808SJens Wiklander const psa_drv_se_t *methods); 137632b31808SJens Wiklander 137732b31808SJens Wiklander /**@}*/ 137832b31808SJens Wiklander 137932b31808SJens Wiklander #ifdef __cplusplus 138032b31808SJens Wiklander } 138132b31808SJens Wiklander #endif 138232b31808SJens Wiklander 138332b31808SJens Wiklander #endif /* PSA_CRYPTO_SE_DRIVER_H */ 1384