1*62c56f98SSadaf Ebrahimi /** 2*62c56f98SSadaf Ebrahimi * \file psa/crypto_se_driver.h 3*62c56f98SSadaf Ebrahimi * \brief PSA external cryptoprocessor driver module 4*62c56f98SSadaf Ebrahimi * 5*62c56f98SSadaf Ebrahimi * This header declares types and function signatures for cryptography 6*62c56f98SSadaf Ebrahimi * drivers that access key material via opaque references. 7*62c56f98SSadaf Ebrahimi * This is meant for cryptoprocessors that have a separate key storage from the 8*62c56f98SSadaf Ebrahimi * space in which the PSA Crypto implementation runs, typically secure 9*62c56f98SSadaf Ebrahimi * elements (SEs). 10*62c56f98SSadaf Ebrahimi * 11*62c56f98SSadaf Ebrahimi * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer), 12*62c56f98SSadaf Ebrahimi * containing functions for driver developers to implement to enable hardware 13*62c56f98SSadaf Ebrahimi * to be called in a standardized way by a PSA Cryptography API 14*62c56f98SSadaf Ebrahimi * implementation. The functions comprising the driver HAL, which driver 15*62c56f98SSadaf Ebrahimi * authors implement, are not intended to be called by application developers. 16*62c56f98SSadaf Ebrahimi */ 17*62c56f98SSadaf Ebrahimi 18*62c56f98SSadaf Ebrahimi /* 19*62c56f98SSadaf Ebrahimi * Copyright The Mbed TLS Contributors 20*62c56f98SSadaf Ebrahimi * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 21*62c56f98SSadaf Ebrahimi */ 22*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_SE_DRIVER_H 23*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_SE_DRIVER_H 24*62c56f98SSadaf Ebrahimi #include "mbedtls/private_access.h" 25*62c56f98SSadaf Ebrahimi 26*62c56f98SSadaf Ebrahimi #include "crypto_driver_common.h" 27*62c56f98SSadaf Ebrahimi 28*62c56f98SSadaf Ebrahimi #ifdef __cplusplus 29*62c56f98SSadaf Ebrahimi extern "C" { 30*62c56f98SSadaf Ebrahimi #endif 31*62c56f98SSadaf Ebrahimi 32*62c56f98SSadaf Ebrahimi /** \defgroup se_init Secure element driver initialization 33*62c56f98SSadaf Ebrahimi */ 34*62c56f98SSadaf Ebrahimi /**@{*/ 35*62c56f98SSadaf Ebrahimi 36*62c56f98SSadaf Ebrahimi /** \brief Driver context structure 37*62c56f98SSadaf Ebrahimi * 38*62c56f98SSadaf Ebrahimi * Driver functions receive a pointer to this structure. 39*62c56f98SSadaf Ebrahimi * Each registered driver has one instance of this structure. 40*62c56f98SSadaf Ebrahimi * 41*62c56f98SSadaf Ebrahimi * Implementations must include the fields specified here and 42*62c56f98SSadaf Ebrahimi * may include other fields. 43*62c56f98SSadaf Ebrahimi */ 44*62c56f98SSadaf Ebrahimi typedef struct { 45*62c56f98SSadaf Ebrahimi /** A read-only pointer to the driver's persistent data. 46*62c56f98SSadaf Ebrahimi * 47*62c56f98SSadaf Ebrahimi * Drivers typically use this persistent data to keep track of 48*62c56f98SSadaf Ebrahimi * which slot numbers are available. This is only a guideline: 49*62c56f98SSadaf Ebrahimi * drivers may use the persistent data for any purpose, keeping 50*62c56f98SSadaf Ebrahimi * in mind the restrictions on when the persistent data is saved 51*62c56f98SSadaf Ebrahimi * to storage: the persistent data is only saved after calling 52*62c56f98SSadaf Ebrahimi * certain functions that receive a writable pointer to the 53*62c56f98SSadaf Ebrahimi * persistent data. 54*62c56f98SSadaf Ebrahimi * 55*62c56f98SSadaf Ebrahimi * The core allocates a memory buffer for the persistent data. 56*62c56f98SSadaf Ebrahimi * The pointer is guaranteed to be suitably aligned for any data type, 57*62c56f98SSadaf Ebrahimi * like a pointer returned by `malloc` (but the core can use any 58*62c56f98SSadaf Ebrahimi * method to allocate the buffer, not necessarily `malloc`). 59*62c56f98SSadaf Ebrahimi * 60*62c56f98SSadaf Ebrahimi * The size of this buffer is in the \c persistent_data_size field of 61*62c56f98SSadaf Ebrahimi * this structure. 62*62c56f98SSadaf Ebrahimi * 63*62c56f98SSadaf Ebrahimi * Before the driver is initialized for the first time, the content of 64*62c56f98SSadaf Ebrahimi * the persistent data is all-bits-zero. After a driver upgrade, if the 65*62c56f98SSadaf Ebrahimi * size of the persistent data has increased, the original data is padded 66*62c56f98SSadaf Ebrahimi * on the right with zeros; if the size has decreased, the original data 67*62c56f98SSadaf Ebrahimi * is truncated to the new size. 68*62c56f98SSadaf Ebrahimi * 69*62c56f98SSadaf Ebrahimi * This pointer is to read-only data. Only a few driver functions are 70*62c56f98SSadaf Ebrahimi * allowed to modify the persistent data. These functions receive a 71*62c56f98SSadaf Ebrahimi * writable pointer. These functions are: 72*62c56f98SSadaf Ebrahimi * - psa_drv_se_t::p_init 73*62c56f98SSadaf Ebrahimi * - psa_drv_se_key_management_t::p_allocate 74*62c56f98SSadaf Ebrahimi * - psa_drv_se_key_management_t::p_destroy 75*62c56f98SSadaf Ebrahimi * 76*62c56f98SSadaf Ebrahimi * The PSA Cryptography core saves the persistent data from one 77*62c56f98SSadaf Ebrahimi * session to the next. It does this before returning from API functions 78*62c56f98SSadaf Ebrahimi * that call a driver method that is allowed to modify the persistent 79*62c56f98SSadaf Ebrahimi * data, specifically: 80*62c56f98SSadaf Ebrahimi * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call 81*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_destroy to complete an action 82*62c56f98SSadaf Ebrahimi * that was interrupted by a power failure. 83*62c56f98SSadaf Ebrahimi * - Key creation functions cause a call to 84*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_allocate, and may cause a call to 85*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_destroy in case an error occurs. 86*62c56f98SSadaf Ebrahimi * - psa_destroy_key() causes a call to 87*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_destroy. 88*62c56f98SSadaf Ebrahimi */ 89*62c56f98SSadaf Ebrahimi const void *const MBEDTLS_PRIVATE(persistent_data); 90*62c56f98SSadaf Ebrahimi 91*62c56f98SSadaf Ebrahimi /** The size of \c persistent_data in bytes. 92*62c56f98SSadaf Ebrahimi * 93*62c56f98SSadaf Ebrahimi * This is always equal to the value of the `persistent_data_size` field 94*62c56f98SSadaf Ebrahimi * of the ::psa_drv_se_t structure when the driver is registered. 95*62c56f98SSadaf Ebrahimi */ 96*62c56f98SSadaf Ebrahimi const size_t MBEDTLS_PRIVATE(persistent_data_size); 97*62c56f98SSadaf Ebrahimi 98*62c56f98SSadaf Ebrahimi /** Driver transient data. 99*62c56f98SSadaf Ebrahimi * 100*62c56f98SSadaf Ebrahimi * The core initializes this value to 0 and does not read or modify it 101*62c56f98SSadaf Ebrahimi * afterwards. The driver may store whatever it wants in this field. 102*62c56f98SSadaf Ebrahimi */ 103*62c56f98SSadaf Ebrahimi uintptr_t MBEDTLS_PRIVATE(transient_data); 104*62c56f98SSadaf Ebrahimi } psa_drv_se_context_t; 105*62c56f98SSadaf Ebrahimi 106*62c56f98SSadaf Ebrahimi /** \brief A driver initialization function. 107*62c56f98SSadaf Ebrahimi * 108*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 109*62c56f98SSadaf Ebrahimi * \param[in,out] persistent_data A pointer to the persistent data 110*62c56f98SSadaf Ebrahimi * that allows writing. 111*62c56f98SSadaf Ebrahimi * \param location The location value for which this driver 112*62c56f98SSadaf Ebrahimi * is registered. The driver will be invoked 113*62c56f98SSadaf Ebrahimi * for all keys whose lifetime is in this 114*62c56f98SSadaf Ebrahimi * location. 115*62c56f98SSadaf Ebrahimi * 116*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 117*62c56f98SSadaf Ebrahimi * The driver is operational. 118*62c56f98SSadaf Ebrahimi * The core will update the persistent data in storage. 119*62c56f98SSadaf Ebrahimi * \return 120*62c56f98SSadaf Ebrahimi * Any other return value prevents the driver from being used in 121*62c56f98SSadaf Ebrahimi * this session. 122*62c56f98SSadaf Ebrahimi * The core will NOT update the persistent data in storage. 123*62c56f98SSadaf Ebrahimi */ 124*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context, 125*62c56f98SSadaf Ebrahimi void *persistent_data, 126*62c56f98SSadaf Ebrahimi psa_key_location_t location); 127*62c56f98SSadaf Ebrahimi 128*62c56f98SSadaf Ebrahimi #if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C) 129*62c56f98SSadaf Ebrahimi /* Mbed TLS with secure element support enabled defines this type in 130*62c56f98SSadaf Ebrahimi * crypto_types.h because it is also visible to applications through an 131*62c56f98SSadaf Ebrahimi * implementation-specific extension. 132*62c56f98SSadaf Ebrahimi * For the PSA Cryptography specification, this type is only visible 133*62c56f98SSadaf Ebrahimi * via crypto_se_driver.h. */ 134*62c56f98SSadaf Ebrahimi /** An internal designation of a key slot between the core part of the 135*62c56f98SSadaf Ebrahimi * PSA Crypto implementation and the driver. The meaning of this value 136*62c56f98SSadaf Ebrahimi * is driver-dependent. */ 137*62c56f98SSadaf Ebrahimi typedef uint64_t psa_key_slot_number_t; 138*62c56f98SSadaf Ebrahimi #endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */ 139*62c56f98SSadaf Ebrahimi 140*62c56f98SSadaf Ebrahimi /**@}*/ 141*62c56f98SSadaf Ebrahimi 142*62c56f98SSadaf Ebrahimi /** \defgroup se_mac Secure Element Message Authentication Codes 143*62c56f98SSadaf Ebrahimi * Generation and authentication of Message Authentication Codes (MACs) using 144*62c56f98SSadaf Ebrahimi * a secure element can be done either as a single function call (via the 145*62c56f98SSadaf Ebrahimi * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in 146*62c56f98SSadaf Ebrahimi * parts using the following sequence: 147*62c56f98SSadaf Ebrahimi * - `psa_drv_se_mac_setup_t` 148*62c56f98SSadaf Ebrahimi * - `psa_drv_se_mac_update_t` 149*62c56f98SSadaf Ebrahimi * - `psa_drv_se_mac_update_t` 150*62c56f98SSadaf Ebrahimi * - ... 151*62c56f98SSadaf Ebrahimi * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t` 152*62c56f98SSadaf Ebrahimi * 153*62c56f98SSadaf Ebrahimi * If a previously started secure element MAC operation needs to be terminated, 154*62c56f98SSadaf Ebrahimi * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may 155*62c56f98SSadaf Ebrahimi * result in allocated resources not being freed or in other undefined 156*62c56f98SSadaf Ebrahimi * behavior. 157*62c56f98SSadaf Ebrahimi */ 158*62c56f98SSadaf Ebrahimi /**@{*/ 159*62c56f98SSadaf Ebrahimi /** \brief A function that starts a secure element MAC operation for a PSA 160*62c56f98SSadaf Ebrahimi * Crypto Driver implementation 161*62c56f98SSadaf Ebrahimi * 162*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 163*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A structure that will contain the 164*62c56f98SSadaf Ebrahimi * hardware-specific MAC context 165*62c56f98SSadaf Ebrahimi * \param[in] key_slot The slot of the key to be used for the 166*62c56f98SSadaf Ebrahimi * operation 167*62c56f98SSadaf Ebrahimi * \param[in] algorithm The algorithm to be used to underly the MAC 168*62c56f98SSadaf Ebrahimi * operation 169*62c56f98SSadaf Ebrahimi * 170*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 171*62c56f98SSadaf Ebrahimi * Success. 172*62c56f98SSadaf Ebrahimi */ 173*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context, 174*62c56f98SSadaf Ebrahimi void *op_context, 175*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 176*62c56f98SSadaf Ebrahimi psa_algorithm_t algorithm); 177*62c56f98SSadaf Ebrahimi 178*62c56f98SSadaf Ebrahimi /** \brief A function that continues a previously started secure element MAC 179*62c56f98SSadaf Ebrahimi * operation 180*62c56f98SSadaf Ebrahimi * 181*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the 182*62c56f98SSadaf Ebrahimi * previously-established MAC operation to be 183*62c56f98SSadaf Ebrahimi * updated 184*62c56f98SSadaf Ebrahimi * \param[in] p_input A buffer containing the message to be appended 185*62c56f98SSadaf Ebrahimi * to the MAC operation 186*62c56f98SSadaf Ebrahimi * \param[in] input_length The size in bytes of the input message buffer 187*62c56f98SSadaf Ebrahimi */ 188*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context, 189*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 190*62c56f98SSadaf Ebrahimi size_t input_length); 191*62c56f98SSadaf Ebrahimi 192*62c56f98SSadaf Ebrahimi /** \brief a function that completes a previously started secure element MAC 193*62c56f98SSadaf Ebrahimi * operation by returning the resulting MAC. 194*62c56f98SSadaf Ebrahimi * 195*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the 196*62c56f98SSadaf Ebrahimi * previously started MAC operation to be 197*62c56f98SSadaf Ebrahimi * finished 198*62c56f98SSadaf Ebrahimi * \param[out] p_mac A buffer where the generated MAC will be 199*62c56f98SSadaf Ebrahimi * placed 200*62c56f98SSadaf Ebrahimi * \param[in] mac_size The size in bytes of the buffer that has been 201*62c56f98SSadaf Ebrahimi * allocated for the `output` buffer 202*62c56f98SSadaf Ebrahimi * \param[out] p_mac_length After completion, will contain the number of 203*62c56f98SSadaf Ebrahimi * bytes placed in the `p_mac` buffer 204*62c56f98SSadaf Ebrahimi * 205*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 206*62c56f98SSadaf Ebrahimi * Success. 207*62c56f98SSadaf Ebrahimi */ 208*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context, 209*62c56f98SSadaf Ebrahimi uint8_t *p_mac, 210*62c56f98SSadaf Ebrahimi size_t mac_size, 211*62c56f98SSadaf Ebrahimi size_t *p_mac_length); 212*62c56f98SSadaf Ebrahimi 213*62c56f98SSadaf Ebrahimi /** \brief A function that completes a previously started secure element MAC 214*62c56f98SSadaf Ebrahimi * operation by comparing the resulting MAC against a provided value 215*62c56f98SSadaf Ebrahimi * 216*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the previously 217*62c56f98SSadaf Ebrahimi * started MAC operation to be finished 218*62c56f98SSadaf Ebrahimi * \param[in] p_mac The MAC value against which the resulting MAC 219*62c56f98SSadaf Ebrahimi * will be compared against 220*62c56f98SSadaf Ebrahimi * \param[in] mac_length The size in bytes of the value stored in `p_mac` 221*62c56f98SSadaf Ebrahimi * 222*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 223*62c56f98SSadaf Ebrahimi * The operation completed successfully and the MACs matched each 224*62c56f98SSadaf Ebrahimi * other 225*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_SIGNATURE 226*62c56f98SSadaf Ebrahimi * The operation completed successfully, but the calculated MAC did 227*62c56f98SSadaf Ebrahimi * not match the provided MAC 228*62c56f98SSadaf Ebrahimi */ 229*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context, 230*62c56f98SSadaf Ebrahimi const uint8_t *p_mac, 231*62c56f98SSadaf Ebrahimi size_t mac_length); 232*62c56f98SSadaf Ebrahimi 233*62c56f98SSadaf Ebrahimi /** \brief A function that aborts a previous started secure element MAC 234*62c56f98SSadaf Ebrahimi * operation 235*62c56f98SSadaf Ebrahimi * 236*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the previously 237*62c56f98SSadaf Ebrahimi * started MAC operation to be aborted 238*62c56f98SSadaf Ebrahimi */ 239*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context); 240*62c56f98SSadaf Ebrahimi 241*62c56f98SSadaf Ebrahimi /** \brief A function that performs a secure element MAC operation in one 242*62c56f98SSadaf Ebrahimi * command and returns the calculated MAC 243*62c56f98SSadaf Ebrahimi * 244*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 245*62c56f98SSadaf Ebrahimi * \param[in] p_input A buffer containing the message to be MACed 246*62c56f98SSadaf Ebrahimi * \param[in] input_length The size in bytes of `p_input` 247*62c56f98SSadaf Ebrahimi * \param[in] key_slot The slot of the key to be used 248*62c56f98SSadaf Ebrahimi * \param[in] alg The algorithm to be used to underlie the MAC 249*62c56f98SSadaf Ebrahimi * operation 250*62c56f98SSadaf Ebrahimi * \param[out] p_mac A buffer where the generated MAC will be 251*62c56f98SSadaf Ebrahimi * placed 252*62c56f98SSadaf Ebrahimi * \param[in] mac_size The size in bytes of the `p_mac` buffer 253*62c56f98SSadaf Ebrahimi * \param[out] p_mac_length After completion, will contain the number of 254*62c56f98SSadaf Ebrahimi * bytes placed in the `output` buffer 255*62c56f98SSadaf Ebrahimi * 256*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 257*62c56f98SSadaf Ebrahimi * Success. 258*62c56f98SSadaf Ebrahimi */ 259*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context, 260*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 261*62c56f98SSadaf Ebrahimi size_t input_length, 262*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 263*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 264*62c56f98SSadaf Ebrahimi uint8_t *p_mac, 265*62c56f98SSadaf Ebrahimi size_t mac_size, 266*62c56f98SSadaf Ebrahimi size_t *p_mac_length); 267*62c56f98SSadaf Ebrahimi 268*62c56f98SSadaf Ebrahimi /** \brief A function that performs a secure element MAC operation in one 269*62c56f98SSadaf Ebrahimi * command and compares the resulting MAC against a provided value 270*62c56f98SSadaf Ebrahimi * 271*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 272*62c56f98SSadaf Ebrahimi * \param[in] p_input A buffer containing the message to be MACed 273*62c56f98SSadaf Ebrahimi * \param[in] input_length The size in bytes of `input` 274*62c56f98SSadaf Ebrahimi * \param[in] key_slot The slot of the key to be used 275*62c56f98SSadaf Ebrahimi * \param[in] alg The algorithm to be used to underlie the MAC 276*62c56f98SSadaf Ebrahimi * operation 277*62c56f98SSadaf Ebrahimi * \param[in] p_mac The MAC value against which the resulting MAC will 278*62c56f98SSadaf Ebrahimi * be compared against 279*62c56f98SSadaf Ebrahimi * \param[in] mac_length The size in bytes of `mac` 280*62c56f98SSadaf Ebrahimi * 281*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 282*62c56f98SSadaf Ebrahimi * The operation completed successfully and the MACs matched each 283*62c56f98SSadaf Ebrahimi * other 284*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_SIGNATURE 285*62c56f98SSadaf Ebrahimi * The operation completed successfully, but the calculated MAC did 286*62c56f98SSadaf Ebrahimi * not match the provided MAC 287*62c56f98SSadaf Ebrahimi */ 288*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context, 289*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 290*62c56f98SSadaf Ebrahimi size_t input_length, 291*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 292*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 293*62c56f98SSadaf Ebrahimi const uint8_t *p_mac, 294*62c56f98SSadaf Ebrahimi size_t mac_length); 295*62c56f98SSadaf Ebrahimi 296*62c56f98SSadaf Ebrahimi /** \brief A struct containing all of the function pointers needed to 297*62c56f98SSadaf Ebrahimi * perform secure element MAC operations 298*62c56f98SSadaf Ebrahimi * 299*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate the table as appropriate 300*62c56f98SSadaf Ebrahimi * upon startup. 301*62c56f98SSadaf Ebrahimi * 302*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented (such as 303*62c56f98SSadaf Ebrahimi * `psa_drv_se_mac_generate_t`), it should be set to NULL. 304*62c56f98SSadaf Ebrahimi * 305*62c56f98SSadaf Ebrahimi * Driver implementers should ensure that they implement all of the functions 306*62c56f98SSadaf Ebrahimi * that make sense for their hardware, and that they provide a full solution 307*62c56f98SSadaf Ebrahimi * (for example, if they support `p_setup`, they should also support 308*62c56f98SSadaf Ebrahimi * `p_update` and at least one of `p_finish` or `p_finish_verify`). 309*62c56f98SSadaf Ebrahimi * 310*62c56f98SSadaf Ebrahimi */ 311*62c56f98SSadaf Ebrahimi typedef struct { 312*62c56f98SSadaf Ebrahimi /**The size in bytes of the hardware-specific secure element MAC context 313*62c56f98SSadaf Ebrahimi * structure 314*62c56f98SSadaf Ebrahimi */ 315*62c56f98SSadaf Ebrahimi size_t MBEDTLS_PRIVATE(context_size); 316*62c56f98SSadaf Ebrahimi /** Function that performs a MAC setup operation 317*62c56f98SSadaf Ebrahimi */ 318*62c56f98SSadaf Ebrahimi psa_drv_se_mac_setup_t MBEDTLS_PRIVATE(p_setup); 319*62c56f98SSadaf Ebrahimi /** Function that performs a MAC update operation 320*62c56f98SSadaf Ebrahimi */ 321*62c56f98SSadaf Ebrahimi psa_drv_se_mac_update_t MBEDTLS_PRIVATE(p_update); 322*62c56f98SSadaf Ebrahimi /** Function that completes a MAC operation 323*62c56f98SSadaf Ebrahimi */ 324*62c56f98SSadaf Ebrahimi psa_drv_se_mac_finish_t MBEDTLS_PRIVATE(p_finish); 325*62c56f98SSadaf Ebrahimi /** Function that completes a MAC operation with a verify check 326*62c56f98SSadaf Ebrahimi */ 327*62c56f98SSadaf Ebrahimi psa_drv_se_mac_finish_verify_t MBEDTLS_PRIVATE(p_finish_verify); 328*62c56f98SSadaf Ebrahimi /** Function that aborts a previously started MAC operation 329*62c56f98SSadaf Ebrahimi */ 330*62c56f98SSadaf Ebrahimi psa_drv_se_mac_abort_t MBEDTLS_PRIVATE(p_abort); 331*62c56f98SSadaf Ebrahimi /** Function that performs a MAC operation in one call 332*62c56f98SSadaf Ebrahimi */ 333*62c56f98SSadaf Ebrahimi psa_drv_se_mac_generate_t MBEDTLS_PRIVATE(p_mac); 334*62c56f98SSadaf Ebrahimi /** Function that performs a MAC and verify operation in one call 335*62c56f98SSadaf Ebrahimi */ 336*62c56f98SSadaf Ebrahimi psa_drv_se_mac_verify_t MBEDTLS_PRIVATE(p_mac_verify); 337*62c56f98SSadaf Ebrahimi } psa_drv_se_mac_t; 338*62c56f98SSadaf Ebrahimi /**@}*/ 339*62c56f98SSadaf Ebrahimi 340*62c56f98SSadaf Ebrahimi /** \defgroup se_cipher Secure Element Symmetric Ciphers 341*62c56f98SSadaf Ebrahimi * 342*62c56f98SSadaf Ebrahimi * Encryption and Decryption using secure element keys in block modes other 343*62c56f98SSadaf Ebrahimi * than ECB must be done in multiple parts, using the following flow: 344*62c56f98SSadaf Ebrahimi * - `psa_drv_se_cipher_setup_t` 345*62c56f98SSadaf Ebrahimi * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode) 346*62c56f98SSadaf Ebrahimi * - `psa_drv_se_cipher_update_t` 347*62c56f98SSadaf Ebrahimi * - `psa_drv_se_cipher_update_t` 348*62c56f98SSadaf Ebrahimi * - ... 349*62c56f98SSadaf Ebrahimi * - `psa_drv_se_cipher_finish_t` 350*62c56f98SSadaf Ebrahimi * 351*62c56f98SSadaf Ebrahimi * If a previously started secure element Cipher operation needs to be 352*62c56f98SSadaf Ebrahimi * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure 353*62c56f98SSadaf Ebrahimi * to do so may result in allocated resources not being freed or in other 354*62c56f98SSadaf Ebrahimi * undefined behavior. 355*62c56f98SSadaf Ebrahimi * 356*62c56f98SSadaf Ebrahimi * In situations where a PSA Cryptographic API implementation is using a block 357*62c56f98SSadaf Ebrahimi * mode not-supported by the underlying hardware or driver, it can construct 358*62c56f98SSadaf Ebrahimi * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function 359*62c56f98SSadaf Ebrahimi * for the cipher operations. 360*62c56f98SSadaf Ebrahimi */ 361*62c56f98SSadaf Ebrahimi /**@{*/ 362*62c56f98SSadaf Ebrahimi 363*62c56f98SSadaf Ebrahimi /** \brief A function that provides the cipher setup function for a 364*62c56f98SSadaf Ebrahimi * secure element driver 365*62c56f98SSadaf Ebrahimi * 366*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 367*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A structure that will contain the 368*62c56f98SSadaf Ebrahimi * hardware-specific cipher context. 369*62c56f98SSadaf Ebrahimi * \param[in] key_slot The slot of the key to be used for the 370*62c56f98SSadaf Ebrahimi * operation 371*62c56f98SSadaf Ebrahimi * \param[in] algorithm The algorithm to be used in the cipher 372*62c56f98SSadaf Ebrahimi * operation 373*62c56f98SSadaf Ebrahimi * \param[in] direction Indicates whether the operation is an encrypt 374*62c56f98SSadaf Ebrahimi * or decrypt 375*62c56f98SSadaf Ebrahimi * 376*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 377*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 378*62c56f98SSadaf Ebrahimi */ 379*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context, 380*62c56f98SSadaf Ebrahimi void *op_context, 381*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 382*62c56f98SSadaf Ebrahimi psa_algorithm_t algorithm, 383*62c56f98SSadaf Ebrahimi psa_encrypt_or_decrypt_t direction); 384*62c56f98SSadaf Ebrahimi 385*62c56f98SSadaf Ebrahimi /** \brief A function that sets the initialization vector (if 386*62c56f98SSadaf Ebrahimi * necessary) for a secure element cipher operation 387*62c56f98SSadaf Ebrahimi * 388*62c56f98SSadaf Ebrahimi * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has 389*62c56f98SSadaf Ebrahimi * two IV functions: one to set the IV, and one to generate it internally. The 390*62c56f98SSadaf Ebrahimi * generate function is not necessary for the drivers to implement as the PSA 391*62c56f98SSadaf Ebrahimi * Crypto implementation can do the generation using its RNG features. 392*62c56f98SSadaf Ebrahimi * 393*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A structure that contains the previously set up 394*62c56f98SSadaf Ebrahimi * hardware-specific cipher context 395*62c56f98SSadaf Ebrahimi * \param[in] p_iv A buffer containing the initialization vector 396*62c56f98SSadaf Ebrahimi * \param[in] iv_length The size (in bytes) of the `p_iv` buffer 397*62c56f98SSadaf Ebrahimi * 398*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 399*62c56f98SSadaf Ebrahimi */ 400*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context, 401*62c56f98SSadaf Ebrahimi const uint8_t *p_iv, 402*62c56f98SSadaf Ebrahimi size_t iv_length); 403*62c56f98SSadaf Ebrahimi 404*62c56f98SSadaf Ebrahimi /** \brief A function that continues a previously started secure element cipher 405*62c56f98SSadaf Ebrahimi * operation 406*62c56f98SSadaf Ebrahimi * 407*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the 408*62c56f98SSadaf Ebrahimi * previously started cipher operation 409*62c56f98SSadaf Ebrahimi * \param[in] p_input A buffer containing the data to be 410*62c56f98SSadaf Ebrahimi * encrypted/decrypted 411*62c56f98SSadaf Ebrahimi * \param[in] input_size The size in bytes of the buffer pointed to 412*62c56f98SSadaf Ebrahimi * by `p_input` 413*62c56f98SSadaf Ebrahimi * \param[out] p_output The caller-allocated buffer where the 414*62c56f98SSadaf Ebrahimi * output will be placed 415*62c56f98SSadaf Ebrahimi * \param[in] output_size The allocated size in bytes of the 416*62c56f98SSadaf Ebrahimi * `p_output` buffer 417*62c56f98SSadaf Ebrahimi * \param[out] p_output_length After completion, will contain the number 418*62c56f98SSadaf Ebrahimi * of bytes placed in the `p_output` buffer 419*62c56f98SSadaf Ebrahimi * 420*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 421*62c56f98SSadaf Ebrahimi */ 422*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context, 423*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 424*62c56f98SSadaf Ebrahimi size_t input_size, 425*62c56f98SSadaf Ebrahimi uint8_t *p_output, 426*62c56f98SSadaf Ebrahimi size_t output_size, 427*62c56f98SSadaf Ebrahimi size_t *p_output_length); 428*62c56f98SSadaf Ebrahimi 429*62c56f98SSadaf Ebrahimi /** \brief A function that completes a previously started secure element cipher 430*62c56f98SSadaf Ebrahimi * operation 431*62c56f98SSadaf Ebrahimi * 432*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the 433*62c56f98SSadaf Ebrahimi * previously started cipher operation 434*62c56f98SSadaf Ebrahimi * \param[out] p_output The caller-allocated buffer where the output 435*62c56f98SSadaf Ebrahimi * will be placed 436*62c56f98SSadaf Ebrahimi * \param[in] output_size The allocated size in bytes of the `p_output` 437*62c56f98SSadaf Ebrahimi * buffer 438*62c56f98SSadaf Ebrahimi * \param[out] p_output_length After completion, will contain the number of 439*62c56f98SSadaf Ebrahimi * bytes placed in the `p_output` buffer 440*62c56f98SSadaf Ebrahimi * 441*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 442*62c56f98SSadaf Ebrahimi */ 443*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context, 444*62c56f98SSadaf Ebrahimi uint8_t *p_output, 445*62c56f98SSadaf Ebrahimi size_t output_size, 446*62c56f98SSadaf Ebrahimi size_t *p_output_length); 447*62c56f98SSadaf Ebrahimi 448*62c56f98SSadaf Ebrahimi /** \brief A function that aborts a previously started secure element cipher 449*62c56f98SSadaf Ebrahimi * operation 450*62c56f98SSadaf Ebrahimi * 451*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure for the 452*62c56f98SSadaf Ebrahimi * previously started cipher operation 453*62c56f98SSadaf Ebrahimi */ 454*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context); 455*62c56f98SSadaf Ebrahimi 456*62c56f98SSadaf Ebrahimi /** \brief A function that performs the ECB block mode for secure element 457*62c56f98SSadaf Ebrahimi * cipher operations 458*62c56f98SSadaf Ebrahimi * 459*62c56f98SSadaf Ebrahimi * Note: this function should only be used with implementations that do not 460*62c56f98SSadaf Ebrahimi * provide a needed higher-level operation. 461*62c56f98SSadaf Ebrahimi * 462*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 463*62c56f98SSadaf Ebrahimi * \param[in] key_slot The slot of the key to be used for the operation 464*62c56f98SSadaf Ebrahimi * \param[in] algorithm The algorithm to be used in the cipher operation 465*62c56f98SSadaf Ebrahimi * \param[in] direction Indicates whether the operation is an encrypt or 466*62c56f98SSadaf Ebrahimi * decrypt 467*62c56f98SSadaf Ebrahimi * \param[in] p_input A buffer containing the data to be 468*62c56f98SSadaf Ebrahimi * encrypted/decrypted 469*62c56f98SSadaf Ebrahimi * \param[in] input_size The size in bytes of the buffer pointed to by 470*62c56f98SSadaf Ebrahimi * `p_input` 471*62c56f98SSadaf Ebrahimi * \param[out] p_output The caller-allocated buffer where the output 472*62c56f98SSadaf Ebrahimi * will be placed 473*62c56f98SSadaf Ebrahimi * \param[in] output_size The allocated size in bytes of the `p_output` 474*62c56f98SSadaf Ebrahimi * buffer 475*62c56f98SSadaf Ebrahimi * 476*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 477*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 478*62c56f98SSadaf Ebrahimi */ 479*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context, 480*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 481*62c56f98SSadaf Ebrahimi psa_algorithm_t algorithm, 482*62c56f98SSadaf Ebrahimi psa_encrypt_or_decrypt_t direction, 483*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 484*62c56f98SSadaf Ebrahimi size_t input_size, 485*62c56f98SSadaf Ebrahimi uint8_t *p_output, 486*62c56f98SSadaf Ebrahimi size_t output_size); 487*62c56f98SSadaf Ebrahimi 488*62c56f98SSadaf Ebrahimi /** 489*62c56f98SSadaf Ebrahimi * \brief A struct containing all of the function pointers needed to implement 490*62c56f98SSadaf Ebrahimi * cipher operations using secure elements. 491*62c56f98SSadaf Ebrahimi * 492*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate instances of the table as 493*62c56f98SSadaf Ebrahimi * appropriate upon startup or at build time. 494*62c56f98SSadaf Ebrahimi * 495*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented (such as 496*62c56f98SSadaf Ebrahimi * `psa_drv_se_cipher_ecb_t`), it should be set to NULL. 497*62c56f98SSadaf Ebrahimi */ 498*62c56f98SSadaf Ebrahimi typedef struct { 499*62c56f98SSadaf Ebrahimi /** The size in bytes of the hardware-specific secure element cipher 500*62c56f98SSadaf Ebrahimi * context structure 501*62c56f98SSadaf Ebrahimi */ 502*62c56f98SSadaf Ebrahimi size_t MBEDTLS_PRIVATE(context_size); 503*62c56f98SSadaf Ebrahimi /** Function that performs a cipher setup operation */ 504*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_setup_t MBEDTLS_PRIVATE(p_setup); 505*62c56f98SSadaf Ebrahimi /** Function that sets a cipher IV (if necessary) */ 506*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv); 507*62c56f98SSadaf Ebrahimi /** Function that performs a cipher update operation */ 508*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update); 509*62c56f98SSadaf Ebrahimi /** Function that completes a cipher operation */ 510*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish); 511*62c56f98SSadaf Ebrahimi /** Function that aborts a cipher operation */ 512*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_abort_t MBEDTLS_PRIVATE(p_abort); 513*62c56f98SSadaf Ebrahimi /** Function that performs ECB mode for a cipher operation 514*62c56f98SSadaf Ebrahimi * (Danger: ECB mode should not be used directly by clients of the PSA 515*62c56f98SSadaf Ebrahimi * Crypto Client API) 516*62c56f98SSadaf Ebrahimi */ 517*62c56f98SSadaf Ebrahimi psa_drv_se_cipher_ecb_t MBEDTLS_PRIVATE(p_ecb); 518*62c56f98SSadaf Ebrahimi } psa_drv_se_cipher_t; 519*62c56f98SSadaf Ebrahimi 520*62c56f98SSadaf Ebrahimi /**@}*/ 521*62c56f98SSadaf Ebrahimi 522*62c56f98SSadaf Ebrahimi /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography 523*62c56f98SSadaf Ebrahimi * 524*62c56f98SSadaf Ebrahimi * Since the amount of data that can (or should) be encrypted or signed using 525*62c56f98SSadaf Ebrahimi * asymmetric keys is limited by the key size, asymmetric key operations using 526*62c56f98SSadaf Ebrahimi * keys in a secure element must be done in single function calls. 527*62c56f98SSadaf Ebrahimi */ 528*62c56f98SSadaf Ebrahimi /**@{*/ 529*62c56f98SSadaf Ebrahimi 530*62c56f98SSadaf Ebrahimi /** 531*62c56f98SSadaf Ebrahimi * \brief A function that signs a hash or short message with a private key in 532*62c56f98SSadaf Ebrahimi * a secure element 533*62c56f98SSadaf Ebrahimi * 534*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 535*62c56f98SSadaf Ebrahimi * \param[in] key_slot Key slot of an asymmetric key pair 536*62c56f98SSadaf Ebrahimi * \param[in] alg A signature algorithm that is compatible 537*62c56f98SSadaf Ebrahimi * with the type of `key` 538*62c56f98SSadaf Ebrahimi * \param[in] p_hash The hash to sign 539*62c56f98SSadaf Ebrahimi * \param[in] hash_length Size of the `p_hash` buffer in bytes 540*62c56f98SSadaf Ebrahimi * \param[out] p_signature Buffer where the signature is to be written 541*62c56f98SSadaf Ebrahimi * \param[in] signature_size Size of the `p_signature` buffer in bytes 542*62c56f98SSadaf Ebrahimi * \param[out] p_signature_length On success, the number of bytes 543*62c56f98SSadaf Ebrahimi * that make up the returned signature value 544*62c56f98SSadaf Ebrahimi * 545*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 546*62c56f98SSadaf Ebrahimi */ 547*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context, 548*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 549*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 550*62c56f98SSadaf Ebrahimi const uint8_t *p_hash, 551*62c56f98SSadaf Ebrahimi size_t hash_length, 552*62c56f98SSadaf Ebrahimi uint8_t *p_signature, 553*62c56f98SSadaf Ebrahimi size_t signature_size, 554*62c56f98SSadaf Ebrahimi size_t *p_signature_length); 555*62c56f98SSadaf Ebrahimi 556*62c56f98SSadaf Ebrahimi /** 557*62c56f98SSadaf Ebrahimi * \brief A function that verifies the signature a hash or short message using 558*62c56f98SSadaf Ebrahimi * an asymmetric public key in a secure element 559*62c56f98SSadaf Ebrahimi * 560*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 561*62c56f98SSadaf Ebrahimi * \param[in] key_slot Key slot of a public key or an asymmetric key 562*62c56f98SSadaf Ebrahimi * pair 563*62c56f98SSadaf Ebrahimi * \param[in] alg A signature algorithm that is compatible with 564*62c56f98SSadaf Ebrahimi * the type of `key` 565*62c56f98SSadaf Ebrahimi * \param[in] p_hash The hash whose signature is to be verified 566*62c56f98SSadaf Ebrahimi * \param[in] hash_length Size of the `p_hash` buffer in bytes 567*62c56f98SSadaf Ebrahimi * \param[in] p_signature Buffer containing the signature to verify 568*62c56f98SSadaf Ebrahimi * \param[in] signature_length Size of the `p_signature` buffer in bytes 569*62c56f98SSadaf Ebrahimi * 570*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 571*62c56f98SSadaf Ebrahimi * The signature is valid. 572*62c56f98SSadaf Ebrahimi */ 573*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context, 574*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 575*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 576*62c56f98SSadaf Ebrahimi const uint8_t *p_hash, 577*62c56f98SSadaf Ebrahimi size_t hash_length, 578*62c56f98SSadaf Ebrahimi const uint8_t *p_signature, 579*62c56f98SSadaf Ebrahimi size_t signature_length); 580*62c56f98SSadaf Ebrahimi 581*62c56f98SSadaf Ebrahimi /** 582*62c56f98SSadaf Ebrahimi * \brief A function that encrypts a short message with an asymmetric public 583*62c56f98SSadaf Ebrahimi * key in a secure element 584*62c56f98SSadaf Ebrahimi * 585*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 586*62c56f98SSadaf Ebrahimi * \param[in] key_slot Key slot of a public key or an asymmetric key 587*62c56f98SSadaf Ebrahimi * pair 588*62c56f98SSadaf Ebrahimi * \param[in] alg An asymmetric encryption algorithm that is 589*62c56f98SSadaf Ebrahimi * compatible with the type of `key` 590*62c56f98SSadaf Ebrahimi * \param[in] p_input The message to encrypt 591*62c56f98SSadaf Ebrahimi * \param[in] input_length Size of the `p_input` buffer in bytes 592*62c56f98SSadaf Ebrahimi * \param[in] p_salt A salt or label, if supported by the 593*62c56f98SSadaf Ebrahimi * encryption algorithm 594*62c56f98SSadaf Ebrahimi * If the algorithm does not support a 595*62c56f98SSadaf Ebrahimi * salt, pass `NULL`. 596*62c56f98SSadaf Ebrahimi * If the algorithm supports an optional 597*62c56f98SSadaf Ebrahimi * salt and you do not want to pass a salt, 598*62c56f98SSadaf Ebrahimi * pass `NULL`. 599*62c56f98SSadaf Ebrahimi * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 600*62c56f98SSadaf Ebrahimi * supported. 601*62c56f98SSadaf Ebrahimi * \param[in] salt_length Size of the `p_salt` buffer in bytes 602*62c56f98SSadaf Ebrahimi * If `p_salt` is `NULL`, pass 0. 603*62c56f98SSadaf Ebrahimi * \param[out] p_output Buffer where the encrypted message is to 604*62c56f98SSadaf Ebrahimi * be written 605*62c56f98SSadaf Ebrahimi * \param[in] output_size Size of the `p_output` buffer in bytes 606*62c56f98SSadaf Ebrahimi * \param[out] p_output_length On success, the number of bytes that make up 607*62c56f98SSadaf Ebrahimi * the returned output 608*62c56f98SSadaf Ebrahimi * 609*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 610*62c56f98SSadaf Ebrahimi */ 611*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context, 612*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 613*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 614*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 615*62c56f98SSadaf Ebrahimi size_t input_length, 616*62c56f98SSadaf Ebrahimi const uint8_t *p_salt, 617*62c56f98SSadaf Ebrahimi size_t salt_length, 618*62c56f98SSadaf Ebrahimi uint8_t *p_output, 619*62c56f98SSadaf Ebrahimi size_t output_size, 620*62c56f98SSadaf Ebrahimi size_t *p_output_length); 621*62c56f98SSadaf Ebrahimi 622*62c56f98SSadaf Ebrahimi /** 623*62c56f98SSadaf Ebrahimi * \brief A function that decrypts a short message with an asymmetric private 624*62c56f98SSadaf Ebrahimi * key in a secure element. 625*62c56f98SSadaf Ebrahimi * 626*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 627*62c56f98SSadaf Ebrahimi * \param[in] key_slot Key slot of an asymmetric key pair 628*62c56f98SSadaf Ebrahimi * \param[in] alg An asymmetric encryption algorithm that is 629*62c56f98SSadaf Ebrahimi * compatible with the type of `key` 630*62c56f98SSadaf Ebrahimi * \param[in] p_input The message to decrypt 631*62c56f98SSadaf Ebrahimi * \param[in] input_length Size of the `p_input` buffer in bytes 632*62c56f98SSadaf Ebrahimi * \param[in] p_salt A salt or label, if supported by the 633*62c56f98SSadaf Ebrahimi * encryption algorithm 634*62c56f98SSadaf Ebrahimi * If the algorithm does not support a 635*62c56f98SSadaf Ebrahimi * salt, pass `NULL`. 636*62c56f98SSadaf Ebrahimi * If the algorithm supports an optional 637*62c56f98SSadaf Ebrahimi * salt and you do not want to pass a salt, 638*62c56f98SSadaf Ebrahimi * pass `NULL`. 639*62c56f98SSadaf Ebrahimi * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 640*62c56f98SSadaf Ebrahimi * supported. 641*62c56f98SSadaf Ebrahimi * \param[in] salt_length Size of the `p_salt` buffer in bytes 642*62c56f98SSadaf Ebrahimi * If `p_salt` is `NULL`, pass 0. 643*62c56f98SSadaf Ebrahimi * \param[out] p_output Buffer where the decrypted message is to 644*62c56f98SSadaf Ebrahimi * be written 645*62c56f98SSadaf Ebrahimi * \param[in] output_size Size of the `p_output` buffer in bytes 646*62c56f98SSadaf Ebrahimi * \param[out] p_output_length On success, the number of bytes 647*62c56f98SSadaf Ebrahimi * that make up the returned output 648*62c56f98SSadaf Ebrahimi * 649*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 650*62c56f98SSadaf Ebrahimi */ 651*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context, 652*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 653*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 654*62c56f98SSadaf Ebrahimi const uint8_t *p_input, 655*62c56f98SSadaf Ebrahimi size_t input_length, 656*62c56f98SSadaf Ebrahimi const uint8_t *p_salt, 657*62c56f98SSadaf Ebrahimi size_t salt_length, 658*62c56f98SSadaf Ebrahimi uint8_t *p_output, 659*62c56f98SSadaf Ebrahimi size_t output_size, 660*62c56f98SSadaf Ebrahimi size_t *p_output_length); 661*62c56f98SSadaf Ebrahimi 662*62c56f98SSadaf Ebrahimi /** 663*62c56f98SSadaf Ebrahimi * \brief A struct containing all of the function pointers needed to implement 664*62c56f98SSadaf Ebrahimi * asymmetric cryptographic operations using secure elements. 665*62c56f98SSadaf Ebrahimi * 666*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate instances of the table as 667*62c56f98SSadaf Ebrahimi * appropriate upon startup or at build time. 668*62c56f98SSadaf Ebrahimi * 669*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented, it should be set to NULL. 670*62c56f98SSadaf Ebrahimi */ 671*62c56f98SSadaf Ebrahimi typedef struct { 672*62c56f98SSadaf Ebrahimi /** Function that performs an asymmetric sign operation */ 673*62c56f98SSadaf Ebrahimi psa_drv_se_asymmetric_sign_t MBEDTLS_PRIVATE(p_sign); 674*62c56f98SSadaf Ebrahimi /** Function that performs an asymmetric verify operation */ 675*62c56f98SSadaf Ebrahimi psa_drv_se_asymmetric_verify_t MBEDTLS_PRIVATE(p_verify); 676*62c56f98SSadaf Ebrahimi /** Function that performs an asymmetric encrypt operation */ 677*62c56f98SSadaf Ebrahimi psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt); 678*62c56f98SSadaf Ebrahimi /** Function that performs an asymmetric decrypt operation */ 679*62c56f98SSadaf Ebrahimi psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt); 680*62c56f98SSadaf Ebrahimi } psa_drv_se_asymmetric_t; 681*62c56f98SSadaf Ebrahimi 682*62c56f98SSadaf Ebrahimi /**@}*/ 683*62c56f98SSadaf Ebrahimi 684*62c56f98SSadaf Ebrahimi /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data 685*62c56f98SSadaf Ebrahimi * Authenticated Encryption with Additional Data (AEAD) operations with secure 686*62c56f98SSadaf Ebrahimi * elements must be done in one function call. While this creates a burden for 687*62c56f98SSadaf Ebrahimi * implementers as there must be sufficient space in memory for the entire 688*62c56f98SSadaf Ebrahimi * message, it prevents decrypted data from being made available before the 689*62c56f98SSadaf Ebrahimi * authentication operation is complete and the data is known to be authentic. 690*62c56f98SSadaf Ebrahimi */ 691*62c56f98SSadaf Ebrahimi /**@{*/ 692*62c56f98SSadaf Ebrahimi 693*62c56f98SSadaf Ebrahimi /** \brief A function that performs a secure element authenticated encryption 694*62c56f98SSadaf Ebrahimi * operation 695*62c56f98SSadaf Ebrahimi * 696*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 697*62c56f98SSadaf Ebrahimi * \param[in] key_slot Slot containing the key to use. 698*62c56f98SSadaf Ebrahimi * \param[in] algorithm The AEAD algorithm to compute 699*62c56f98SSadaf Ebrahimi * (\c PSA_ALG_XXX value such that 700*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(`alg`) is true) 701*62c56f98SSadaf Ebrahimi * \param[in] p_nonce Nonce or IV to use 702*62c56f98SSadaf Ebrahimi * \param[in] nonce_length Size of the `p_nonce` buffer in bytes 703*62c56f98SSadaf Ebrahimi * \param[in] p_additional_data Additional data that will be 704*62c56f98SSadaf Ebrahimi * authenticated but not encrypted 705*62c56f98SSadaf Ebrahimi * \param[in] additional_data_length Size of `p_additional_data` in bytes 706*62c56f98SSadaf Ebrahimi * \param[in] p_plaintext Data that will be authenticated and 707*62c56f98SSadaf Ebrahimi * encrypted 708*62c56f98SSadaf Ebrahimi * \param[in] plaintext_length Size of `p_plaintext` in bytes 709*62c56f98SSadaf Ebrahimi * \param[out] p_ciphertext Output buffer for the authenticated and 710*62c56f98SSadaf Ebrahimi * encrypted data. The additional data is 711*62c56f98SSadaf Ebrahimi * not part of this output. For algorithms 712*62c56f98SSadaf Ebrahimi * where the encrypted data and the 713*62c56f98SSadaf Ebrahimi * authentication tag are defined as 714*62c56f98SSadaf Ebrahimi * separate outputs, the authentication 715*62c56f98SSadaf Ebrahimi * tag is appended to the encrypted data. 716*62c56f98SSadaf Ebrahimi * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in 717*62c56f98SSadaf Ebrahimi * bytes 718*62c56f98SSadaf Ebrahimi * \param[out] p_ciphertext_length On success, the size of the output in 719*62c56f98SSadaf Ebrahimi * the `p_ciphertext` buffer 720*62c56f98SSadaf Ebrahimi * 721*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 722*62c56f98SSadaf Ebrahimi * Success. 723*62c56f98SSadaf Ebrahimi */ 724*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context, 725*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 726*62c56f98SSadaf Ebrahimi psa_algorithm_t algorithm, 727*62c56f98SSadaf Ebrahimi const uint8_t *p_nonce, 728*62c56f98SSadaf Ebrahimi size_t nonce_length, 729*62c56f98SSadaf Ebrahimi const uint8_t *p_additional_data, 730*62c56f98SSadaf Ebrahimi size_t additional_data_length, 731*62c56f98SSadaf Ebrahimi const uint8_t *p_plaintext, 732*62c56f98SSadaf Ebrahimi size_t plaintext_length, 733*62c56f98SSadaf Ebrahimi uint8_t *p_ciphertext, 734*62c56f98SSadaf Ebrahimi size_t ciphertext_size, 735*62c56f98SSadaf Ebrahimi size_t *p_ciphertext_length); 736*62c56f98SSadaf Ebrahimi 737*62c56f98SSadaf Ebrahimi /** A function that performs a secure element authenticated decryption operation 738*62c56f98SSadaf Ebrahimi * 739*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 740*62c56f98SSadaf Ebrahimi * \param[in] key_slot Slot containing the key to use 741*62c56f98SSadaf Ebrahimi * \param[in] algorithm The AEAD algorithm to compute 742*62c56f98SSadaf Ebrahimi * (\c PSA_ALG_XXX value such that 743*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(`alg`) is true) 744*62c56f98SSadaf Ebrahimi * \param[in] p_nonce Nonce or IV to use 745*62c56f98SSadaf Ebrahimi * \param[in] nonce_length Size of the `p_nonce` buffer in bytes 746*62c56f98SSadaf Ebrahimi * \param[in] p_additional_data Additional data that has been 747*62c56f98SSadaf Ebrahimi * authenticated but not encrypted 748*62c56f98SSadaf Ebrahimi * \param[in] additional_data_length Size of `p_additional_data` in bytes 749*62c56f98SSadaf Ebrahimi * \param[in] p_ciphertext Data that has been authenticated and 750*62c56f98SSadaf Ebrahimi * encrypted. 751*62c56f98SSadaf Ebrahimi * For algorithms where the encrypted data 752*62c56f98SSadaf Ebrahimi * and the authentication tag are defined 753*62c56f98SSadaf Ebrahimi * as separate inputs, the buffer must 754*62c56f98SSadaf Ebrahimi * contain the encrypted data followed by 755*62c56f98SSadaf Ebrahimi * the authentication tag. 756*62c56f98SSadaf Ebrahimi * \param[in] ciphertext_length Size of `p_ciphertext` in bytes 757*62c56f98SSadaf Ebrahimi * \param[out] p_plaintext Output buffer for the decrypted data 758*62c56f98SSadaf Ebrahimi * \param[in] plaintext_size Size of the `p_plaintext` buffer in 759*62c56f98SSadaf Ebrahimi * bytes 760*62c56f98SSadaf Ebrahimi * \param[out] p_plaintext_length On success, the size of the output in 761*62c56f98SSadaf Ebrahimi * the `p_plaintext` buffer 762*62c56f98SSadaf Ebrahimi * 763*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 764*62c56f98SSadaf Ebrahimi * Success. 765*62c56f98SSadaf Ebrahimi */ 766*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context, 767*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 768*62c56f98SSadaf Ebrahimi psa_algorithm_t algorithm, 769*62c56f98SSadaf Ebrahimi const uint8_t *p_nonce, 770*62c56f98SSadaf Ebrahimi size_t nonce_length, 771*62c56f98SSadaf Ebrahimi const uint8_t *p_additional_data, 772*62c56f98SSadaf Ebrahimi size_t additional_data_length, 773*62c56f98SSadaf Ebrahimi const uint8_t *p_ciphertext, 774*62c56f98SSadaf Ebrahimi size_t ciphertext_length, 775*62c56f98SSadaf Ebrahimi uint8_t *p_plaintext, 776*62c56f98SSadaf Ebrahimi size_t plaintext_size, 777*62c56f98SSadaf Ebrahimi size_t *p_plaintext_length); 778*62c56f98SSadaf Ebrahimi 779*62c56f98SSadaf Ebrahimi /** 780*62c56f98SSadaf Ebrahimi * \brief A struct containing all of the function pointers needed to implement 781*62c56f98SSadaf Ebrahimi * secure element Authenticated Encryption with Additional Data operations 782*62c56f98SSadaf Ebrahimi * 783*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate instances of the table as 784*62c56f98SSadaf Ebrahimi * appropriate upon startup. 785*62c56f98SSadaf Ebrahimi * 786*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented, it should be set to NULL. 787*62c56f98SSadaf Ebrahimi */ 788*62c56f98SSadaf Ebrahimi typedef struct { 789*62c56f98SSadaf Ebrahimi /** Function that performs the AEAD encrypt operation */ 790*62c56f98SSadaf Ebrahimi psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt); 791*62c56f98SSadaf Ebrahimi /** Function that performs the AEAD decrypt operation */ 792*62c56f98SSadaf Ebrahimi psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt); 793*62c56f98SSadaf Ebrahimi } psa_drv_se_aead_t; 794*62c56f98SSadaf Ebrahimi /**@}*/ 795*62c56f98SSadaf Ebrahimi 796*62c56f98SSadaf Ebrahimi /** \defgroup se_key_management Secure Element Key Management 797*62c56f98SSadaf Ebrahimi * Currently, key management is limited to importing keys in the clear, 798*62c56f98SSadaf Ebrahimi * destroying keys, and exporting keys in the clear. 799*62c56f98SSadaf Ebrahimi * Whether a key may be exported is determined by the key policies in place 800*62c56f98SSadaf Ebrahimi * on the key slot. 801*62c56f98SSadaf Ebrahimi */ 802*62c56f98SSadaf Ebrahimi /**@{*/ 803*62c56f98SSadaf Ebrahimi 804*62c56f98SSadaf Ebrahimi /** An enumeration indicating how a key is created. 805*62c56f98SSadaf Ebrahimi */ 806*62c56f98SSadaf Ebrahimi typedef enum { 807*62c56f98SSadaf Ebrahimi PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */ 808*62c56f98SSadaf Ebrahimi PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */ 809*62c56f98SSadaf Ebrahimi PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */ 810*62c56f98SSadaf Ebrahimi PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */ 811*62c56f98SSadaf Ebrahimi 812*62c56f98SSadaf Ebrahimi #ifndef __DOXYGEN_ONLY__ 813*62c56f98SSadaf Ebrahimi /** A key is being registered with mbedtls_psa_register_se_key(). 814*62c56f98SSadaf Ebrahimi * 815*62c56f98SSadaf Ebrahimi * The core only passes this value to 816*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_validate_slot_number, not to 817*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_allocate. The call to 818*62c56f98SSadaf Ebrahimi * `p_validate_slot_number` is not followed by any other call to the 819*62c56f98SSadaf Ebrahimi * driver: the key is considered successfully registered if the call to 820*62c56f98SSadaf Ebrahimi * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is 821*62c56f98SSadaf Ebrahimi * null. 822*62c56f98SSadaf Ebrahimi * 823*62c56f98SSadaf Ebrahimi * With this creation method, the driver must return #PSA_SUCCESS if 824*62c56f98SSadaf Ebrahimi * the given attributes are compatible with the existing key in the slot, 825*62c56f98SSadaf Ebrahimi * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there 826*62c56f98SSadaf Ebrahimi * is no key with the specified slot number. 827*62c56f98SSadaf Ebrahimi * 828*62c56f98SSadaf Ebrahimi * This is an Mbed TLS extension. 829*62c56f98SSadaf Ebrahimi */ 830*62c56f98SSadaf Ebrahimi PSA_KEY_CREATION_REGISTER, 831*62c56f98SSadaf Ebrahimi #endif 832*62c56f98SSadaf Ebrahimi } psa_key_creation_method_t; 833*62c56f98SSadaf Ebrahimi 834*62c56f98SSadaf Ebrahimi /** \brief A function that allocates a slot for a key. 835*62c56f98SSadaf Ebrahimi * 836*62c56f98SSadaf Ebrahimi * To create a key in a specific slot in a secure element, the core 837*62c56f98SSadaf Ebrahimi * first calls this function to determine a valid slot number, 838*62c56f98SSadaf Ebrahimi * then calls a function to create the key material in that slot. 839*62c56f98SSadaf Ebrahimi * In nominal conditions (that is, if no error occurs), 840*62c56f98SSadaf Ebrahimi * the effect of a call to a key creation function in the PSA Cryptography 841*62c56f98SSadaf Ebrahimi * API with a lifetime that places the key in a secure element is the 842*62c56f98SSadaf Ebrahimi * following: 843*62c56f98SSadaf Ebrahimi * -# The core calls psa_drv_se_key_management_t::p_allocate 844*62c56f98SSadaf Ebrahimi * (or in some implementations 845*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_validate_slot_number). The driver 846*62c56f98SSadaf Ebrahimi * selects (or validates) a suitable slot number given the key attributes 847*62c56f98SSadaf Ebrahimi * and the state of the secure element. 848*62c56f98SSadaf Ebrahimi * -# The core calls a key creation function in the driver. 849*62c56f98SSadaf Ebrahimi * 850*62c56f98SSadaf Ebrahimi * The key creation functions in the PSA Cryptography API are: 851*62c56f98SSadaf Ebrahimi * - psa_import_key(), which causes 852*62c56f98SSadaf Ebrahimi * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT 853*62c56f98SSadaf Ebrahimi * then a call to psa_drv_se_key_management_t::p_import. 854*62c56f98SSadaf Ebrahimi * - psa_generate_key(), which causes 855*62c56f98SSadaf Ebrahimi * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE 856*62c56f98SSadaf Ebrahimi * then a call to psa_drv_se_key_management_t::p_import. 857*62c56f98SSadaf Ebrahimi * - psa_key_derivation_output_key(), which causes 858*62c56f98SSadaf Ebrahimi * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE 859*62c56f98SSadaf Ebrahimi * then a call to psa_drv_se_key_derivation_t::p_derive. 860*62c56f98SSadaf Ebrahimi * - psa_copy_key(), which causes 861*62c56f98SSadaf Ebrahimi * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY 862*62c56f98SSadaf Ebrahimi * then a call to psa_drv_se_key_management_t::p_export. 863*62c56f98SSadaf Ebrahimi * 864*62c56f98SSadaf Ebrahimi * In case of errors, other behaviors are possible. 865*62c56f98SSadaf Ebrahimi * - If the PSA Cryptography subsystem dies after the first step, 866*62c56f98SSadaf Ebrahimi * for example because the device has lost power abruptly, 867*62c56f98SSadaf Ebrahimi * the second step may never happen, or may happen after a reset 868*62c56f98SSadaf Ebrahimi * and re-initialization. Alternatively, after a reset and 869*62c56f98SSadaf Ebrahimi * re-initialization, the core may call 870*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_destroy on the slot number that 871*62c56f98SSadaf Ebrahimi * was allocated (or validated) instead of calling a key creation function. 872*62c56f98SSadaf Ebrahimi * - If an error occurs, the core may call 873*62c56f98SSadaf Ebrahimi * psa_drv_se_key_management_t::p_destroy on the slot number that 874*62c56f98SSadaf Ebrahimi * was allocated (or validated) instead of calling a key creation function. 875*62c56f98SSadaf Ebrahimi * 876*62c56f98SSadaf Ebrahimi * Errors and system resets also have an impact on the driver's persistent 877*62c56f98SSadaf Ebrahimi * data. If a reset happens before the overall key creation process is 878*62c56f98SSadaf Ebrahimi * completed (before or after the second step above), it is unspecified 879*62c56f98SSadaf Ebrahimi * whether the persistent data after the reset is identical to what it 880*62c56f98SSadaf Ebrahimi * was before or after the call to `p_allocate` (or `p_validate_slot_number`). 881*62c56f98SSadaf Ebrahimi * 882*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 883*62c56f98SSadaf Ebrahimi * \param[in,out] persistent_data A pointer to the persistent data 884*62c56f98SSadaf Ebrahimi * that allows writing. 885*62c56f98SSadaf Ebrahimi * \param[in] attributes Attributes of the key. 886*62c56f98SSadaf Ebrahimi * \param method The way in which the key is being created. 887*62c56f98SSadaf Ebrahimi * \param[out] key_slot Slot where the key will be stored. 888*62c56f98SSadaf Ebrahimi * This must be a valid slot for a key of the 889*62c56f98SSadaf Ebrahimi * chosen type. It must be unoccupied. 890*62c56f98SSadaf Ebrahimi * 891*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 892*62c56f98SSadaf Ebrahimi * Success. 893*62c56f98SSadaf Ebrahimi * The core will record \c *key_slot as the key slot where the key 894*62c56f98SSadaf Ebrahimi * is stored and will update the persistent data in storage. 895*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 896*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription 897*62c56f98SSadaf Ebrahimi */ 898*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_allocate_key_t)( 899*62c56f98SSadaf Ebrahimi psa_drv_se_context_t *drv_context, 900*62c56f98SSadaf Ebrahimi void *persistent_data, 901*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 902*62c56f98SSadaf Ebrahimi psa_key_creation_method_t method, 903*62c56f98SSadaf Ebrahimi psa_key_slot_number_t *key_slot); 904*62c56f98SSadaf Ebrahimi 905*62c56f98SSadaf Ebrahimi /** \brief A function that determines whether a slot number is valid 906*62c56f98SSadaf Ebrahimi * for a key. 907*62c56f98SSadaf Ebrahimi * 908*62c56f98SSadaf Ebrahimi * To create a key in a specific slot in a secure element, the core 909*62c56f98SSadaf Ebrahimi * first calls this function to validate the choice of slot number, 910*62c56f98SSadaf Ebrahimi * then calls a function to create the key material in that slot. 911*62c56f98SSadaf Ebrahimi * See the documentation of #psa_drv_se_allocate_key_t for more details. 912*62c56f98SSadaf Ebrahimi * 913*62c56f98SSadaf Ebrahimi * As of the PSA Cryptography API specification version 1.0, there is no way 914*62c56f98SSadaf Ebrahimi * for applications to trigger a call to this function. However some 915*62c56f98SSadaf Ebrahimi * implementations offer the capability to create or declare a key in 916*62c56f98SSadaf Ebrahimi * a specific slot via implementation-specific means, generally for the 917*62c56f98SSadaf Ebrahimi * sake of initial device provisioning or onboarding. Such a mechanism may 918*62c56f98SSadaf Ebrahimi * be added to a future version of the PSA Cryptography API specification. 919*62c56f98SSadaf Ebrahimi * 920*62c56f98SSadaf Ebrahimi * This function may update the driver's persistent data through 921*62c56f98SSadaf Ebrahimi * \p persistent_data. The core will save the updated persistent data at the 922*62c56f98SSadaf Ebrahimi * end of the key creation process. See the description of 923*62c56f98SSadaf Ebrahimi * ::psa_drv_se_allocate_key_t for more information. 924*62c56f98SSadaf Ebrahimi * 925*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 926*62c56f98SSadaf Ebrahimi * \param[in,out] persistent_data A pointer to the persistent data 927*62c56f98SSadaf Ebrahimi * that allows writing. 928*62c56f98SSadaf Ebrahimi * \param[in] attributes Attributes of the key. 929*62c56f98SSadaf Ebrahimi * \param method The way in which the key is being created. 930*62c56f98SSadaf Ebrahimi * \param[in] key_slot Slot where the key is to be stored. 931*62c56f98SSadaf Ebrahimi * 932*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 933*62c56f98SSadaf Ebrahimi * The given slot number is valid for a key with the given 934*62c56f98SSadaf Ebrahimi * attributes. 935*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 936*62c56f98SSadaf Ebrahimi * The given slot number is not valid for a key with the 937*62c56f98SSadaf Ebrahimi * given attributes. This includes the case where the slot 938*62c56f98SSadaf Ebrahimi * number is not valid at all. 939*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_ALREADY_EXISTS 940*62c56f98SSadaf Ebrahimi * There is already a key with the specified slot number. 941*62c56f98SSadaf Ebrahimi * Drivers may choose to return this error from the key 942*62c56f98SSadaf Ebrahimi * creation function instead. 943*62c56f98SSadaf Ebrahimi */ 944*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_validate_slot_number_t)( 945*62c56f98SSadaf Ebrahimi psa_drv_se_context_t *drv_context, 946*62c56f98SSadaf Ebrahimi void *persistent_data, 947*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 948*62c56f98SSadaf Ebrahimi psa_key_creation_method_t method, 949*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot); 950*62c56f98SSadaf Ebrahimi 951*62c56f98SSadaf Ebrahimi /** \brief A function that imports a key into a secure element in binary format 952*62c56f98SSadaf Ebrahimi * 953*62c56f98SSadaf Ebrahimi * This function can support any output from psa_export_key(). Refer to the 954*62c56f98SSadaf Ebrahimi * documentation of psa_export_key() for the format for each key type. 955*62c56f98SSadaf Ebrahimi * 956*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 957*62c56f98SSadaf Ebrahimi * \param key_slot Slot where the key will be stored. 958*62c56f98SSadaf Ebrahimi * This must be a valid slot for a key of the 959*62c56f98SSadaf Ebrahimi * chosen type. It must be unoccupied. 960*62c56f98SSadaf Ebrahimi * \param[in] attributes The key attributes, including the lifetime, 961*62c56f98SSadaf Ebrahimi * the key type and the usage policy. 962*62c56f98SSadaf Ebrahimi * Drivers should not access the key size stored 963*62c56f98SSadaf Ebrahimi * in the attributes: it may not match the 964*62c56f98SSadaf Ebrahimi * data passed in \p data. 965*62c56f98SSadaf Ebrahimi * Drivers can call psa_get_key_lifetime(), 966*62c56f98SSadaf Ebrahimi * psa_get_key_type(), 967*62c56f98SSadaf Ebrahimi * psa_get_key_usage_flags() and 968*62c56f98SSadaf Ebrahimi * psa_get_key_algorithm() to access this 969*62c56f98SSadaf Ebrahimi * information. 970*62c56f98SSadaf Ebrahimi * \param[in] data Buffer containing the key data. 971*62c56f98SSadaf Ebrahimi * \param[in] data_length Size of the \p data buffer in bytes. 972*62c56f98SSadaf Ebrahimi * \param[out] bits On success, the key size in bits. The driver 973*62c56f98SSadaf Ebrahimi * must determine this value after parsing the 974*62c56f98SSadaf Ebrahimi * key according to the key type. 975*62c56f98SSadaf Ebrahimi * This value is not used if the function fails. 976*62c56f98SSadaf Ebrahimi * 977*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 978*62c56f98SSadaf Ebrahimi * Success. 979*62c56f98SSadaf Ebrahimi */ 980*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_import_key_t)( 981*62c56f98SSadaf Ebrahimi psa_drv_se_context_t *drv_context, 982*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 983*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 984*62c56f98SSadaf Ebrahimi const uint8_t *data, 985*62c56f98SSadaf Ebrahimi size_t data_length, 986*62c56f98SSadaf Ebrahimi size_t *bits); 987*62c56f98SSadaf Ebrahimi 988*62c56f98SSadaf Ebrahimi /** 989*62c56f98SSadaf Ebrahimi * \brief A function that destroys a secure element key and restore the slot to 990*62c56f98SSadaf Ebrahimi * its default state 991*62c56f98SSadaf Ebrahimi * 992*62c56f98SSadaf Ebrahimi * This function destroys the content of the key from a secure element. 993*62c56f98SSadaf Ebrahimi * Implementations shall make a best effort to ensure that any previous content 994*62c56f98SSadaf Ebrahimi * of the slot is unrecoverable. 995*62c56f98SSadaf Ebrahimi * 996*62c56f98SSadaf Ebrahimi * This function returns the specified slot to its default state. 997*62c56f98SSadaf Ebrahimi * 998*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 999*62c56f98SSadaf Ebrahimi * \param[in,out] persistent_data A pointer to the persistent data 1000*62c56f98SSadaf Ebrahimi * that allows writing. 1001*62c56f98SSadaf Ebrahimi * \param key_slot The key slot to erase. 1002*62c56f98SSadaf Ebrahimi * 1003*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 1004*62c56f98SSadaf Ebrahimi * The slot's content, if any, has been erased. 1005*62c56f98SSadaf Ebrahimi */ 1006*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_destroy_key_t)( 1007*62c56f98SSadaf Ebrahimi psa_drv_se_context_t *drv_context, 1008*62c56f98SSadaf Ebrahimi void *persistent_data, 1009*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot); 1010*62c56f98SSadaf Ebrahimi 1011*62c56f98SSadaf Ebrahimi /** 1012*62c56f98SSadaf Ebrahimi * \brief A function that exports a secure element key in binary format 1013*62c56f98SSadaf Ebrahimi * 1014*62c56f98SSadaf Ebrahimi * The output of this function can be passed to psa_import_key() to 1015*62c56f98SSadaf Ebrahimi * create an equivalent object. 1016*62c56f98SSadaf Ebrahimi * 1017*62c56f98SSadaf Ebrahimi * If a key is created with `psa_import_key()` and then exported with 1018*62c56f98SSadaf Ebrahimi * this function, it is not guaranteed that the resulting data is 1019*62c56f98SSadaf Ebrahimi * identical: the implementation may choose a different representation 1020*62c56f98SSadaf Ebrahimi * of the same key if the format permits it. 1021*62c56f98SSadaf Ebrahimi * 1022*62c56f98SSadaf Ebrahimi * This function should generate output in the same format that 1023*62c56f98SSadaf Ebrahimi * `psa_export_key()` does. Refer to the 1024*62c56f98SSadaf Ebrahimi * documentation of `psa_export_key()` for the format for each key type. 1025*62c56f98SSadaf Ebrahimi * 1026*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 1027*62c56f98SSadaf Ebrahimi * \param[in] key Slot whose content is to be exported. This must 1028*62c56f98SSadaf Ebrahimi * be an occupied key slot. 1029*62c56f98SSadaf Ebrahimi * \param[out] p_data Buffer where the key data is to be written. 1030*62c56f98SSadaf Ebrahimi * \param[in] data_size Size of the `p_data` buffer in bytes. 1031*62c56f98SSadaf Ebrahimi * \param[out] p_data_length On success, the number of bytes 1032*62c56f98SSadaf Ebrahimi * that make up the key data. 1033*62c56f98SSadaf Ebrahimi * 1034*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 1035*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription 1036*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 1037*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 1038*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 1039*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 1040*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 1041*62c56f98SSadaf Ebrahimi */ 1042*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context, 1043*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key, 1044*62c56f98SSadaf Ebrahimi uint8_t *p_data, 1045*62c56f98SSadaf Ebrahimi size_t data_size, 1046*62c56f98SSadaf Ebrahimi size_t *p_data_length); 1047*62c56f98SSadaf Ebrahimi 1048*62c56f98SSadaf Ebrahimi /** 1049*62c56f98SSadaf Ebrahimi * \brief A function that generates a symmetric or asymmetric key on a secure 1050*62c56f98SSadaf Ebrahimi * element 1051*62c56f98SSadaf Ebrahimi * 1052*62c56f98SSadaf Ebrahimi * If the key type \c type recorded in \p attributes 1053*62c56f98SSadaf Ebrahimi * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1), 1054*62c56f98SSadaf Ebrahimi * the driver may export the public key at the time of generation, 1055*62c56f98SSadaf Ebrahimi * in the format documented for psa_export_public_key() by writing it 1056*62c56f98SSadaf Ebrahimi * to the \p pubkey buffer. 1057*62c56f98SSadaf Ebrahimi * This is optional, intended for secure elements that output the 1058*62c56f98SSadaf Ebrahimi * public key at generation time and that cannot export the public key 1059*62c56f98SSadaf Ebrahimi * later. Drivers that do not need this feature should leave 1060*62c56f98SSadaf Ebrahimi * \p *pubkey_length set to 0 and should 1061*62c56f98SSadaf Ebrahimi * implement the psa_drv_key_management_t::p_export_public function. 1062*62c56f98SSadaf Ebrahimi * Some implementations do not support this feature, in which case 1063*62c56f98SSadaf Ebrahimi * \p pubkey is \c NULL and \p pubkey_size is 0. 1064*62c56f98SSadaf Ebrahimi * 1065*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 1066*62c56f98SSadaf Ebrahimi * \param key_slot Slot where the key will be stored. 1067*62c56f98SSadaf Ebrahimi * This must be a valid slot for a key of the 1068*62c56f98SSadaf Ebrahimi * chosen type. It must be unoccupied. 1069*62c56f98SSadaf Ebrahimi * \param[in] attributes The key attributes, including the lifetime, 1070*62c56f98SSadaf Ebrahimi * the key type and size, and the usage policy. 1071*62c56f98SSadaf Ebrahimi * Drivers can call psa_get_key_lifetime(), 1072*62c56f98SSadaf Ebrahimi * psa_get_key_type(), psa_get_key_bits(), 1073*62c56f98SSadaf Ebrahimi * psa_get_key_usage_flags() and 1074*62c56f98SSadaf Ebrahimi * psa_get_key_algorithm() to access this 1075*62c56f98SSadaf Ebrahimi * information. 1076*62c56f98SSadaf Ebrahimi * \param[out] pubkey A buffer where the driver can write the 1077*62c56f98SSadaf Ebrahimi * public key, when generating an asymmetric 1078*62c56f98SSadaf Ebrahimi * key pair. 1079*62c56f98SSadaf Ebrahimi * This is \c NULL when generating a symmetric 1080*62c56f98SSadaf Ebrahimi * key or if the core does not support 1081*62c56f98SSadaf Ebrahimi * exporting the public key at generation time. 1082*62c56f98SSadaf Ebrahimi * \param pubkey_size The size of the `pubkey` buffer in bytes. 1083*62c56f98SSadaf Ebrahimi * This is 0 when generating a symmetric 1084*62c56f98SSadaf Ebrahimi * key or if the core does not support 1085*62c56f98SSadaf Ebrahimi * exporting the public key at generation time. 1086*62c56f98SSadaf Ebrahimi * \param[out] pubkey_length On entry, this is always 0. 1087*62c56f98SSadaf Ebrahimi * On success, the number of bytes written to 1088*62c56f98SSadaf Ebrahimi * \p pubkey. If this is 0 or unchanged on return, 1089*62c56f98SSadaf Ebrahimi * the core will not read the \p pubkey buffer, 1090*62c56f98SSadaf Ebrahimi * and will instead call the driver's 1091*62c56f98SSadaf Ebrahimi * psa_drv_key_management_t::p_export_public 1092*62c56f98SSadaf Ebrahimi * function to export the public key when needed. 1093*62c56f98SSadaf Ebrahimi */ 1094*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_generate_key_t)( 1095*62c56f98SSadaf Ebrahimi psa_drv_se_context_t *drv_context, 1096*62c56f98SSadaf Ebrahimi psa_key_slot_number_t key_slot, 1097*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 1098*62c56f98SSadaf Ebrahimi uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length); 1099*62c56f98SSadaf Ebrahimi 1100*62c56f98SSadaf Ebrahimi /** 1101*62c56f98SSadaf Ebrahimi * \brief A struct containing all of the function pointers needed to for secure 1102*62c56f98SSadaf Ebrahimi * element key management 1103*62c56f98SSadaf Ebrahimi * 1104*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate instances of the table as 1105*62c56f98SSadaf Ebrahimi * appropriate upon startup or at build time. 1106*62c56f98SSadaf Ebrahimi * 1107*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented, it should be set to NULL. 1108*62c56f98SSadaf Ebrahimi */ 1109*62c56f98SSadaf Ebrahimi typedef struct { 1110*62c56f98SSadaf Ebrahimi /** Function that allocates a slot for a key. */ 1111*62c56f98SSadaf Ebrahimi psa_drv_se_allocate_key_t MBEDTLS_PRIVATE(p_allocate); 1112*62c56f98SSadaf Ebrahimi /** Function that checks the validity of a slot for a key. */ 1113*62c56f98SSadaf Ebrahimi psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number); 1114*62c56f98SSadaf Ebrahimi /** Function that performs a key import operation */ 1115*62c56f98SSadaf Ebrahimi psa_drv_se_import_key_t MBEDTLS_PRIVATE(p_import); 1116*62c56f98SSadaf Ebrahimi /** Function that performs a generation */ 1117*62c56f98SSadaf Ebrahimi psa_drv_se_generate_key_t MBEDTLS_PRIVATE(p_generate); 1118*62c56f98SSadaf Ebrahimi /** Function that performs a key destroy operation */ 1119*62c56f98SSadaf Ebrahimi psa_drv_se_destroy_key_t MBEDTLS_PRIVATE(p_destroy); 1120*62c56f98SSadaf Ebrahimi /** Function that performs a key export operation */ 1121*62c56f98SSadaf Ebrahimi psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export); 1122*62c56f98SSadaf Ebrahimi /** Function that performs a public key export operation */ 1123*62c56f98SSadaf Ebrahimi psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export_public); 1124*62c56f98SSadaf Ebrahimi } psa_drv_se_key_management_t; 1125*62c56f98SSadaf Ebrahimi 1126*62c56f98SSadaf Ebrahimi /**@}*/ 1127*62c56f98SSadaf Ebrahimi 1128*62c56f98SSadaf Ebrahimi /** \defgroup driver_derivation Secure Element Key Derivation and Agreement 1129*62c56f98SSadaf Ebrahimi * Key derivation is the process of generating new key material using an 1130*62c56f98SSadaf Ebrahimi * existing key and additional parameters, iterating through a basic 1131*62c56f98SSadaf Ebrahimi * cryptographic function, such as a hash. 1132*62c56f98SSadaf Ebrahimi * Key agreement is a part of cryptographic protocols that allows two parties 1133*62c56f98SSadaf Ebrahimi * to agree on the same key value, but starting from different original key 1134*62c56f98SSadaf Ebrahimi * material. 1135*62c56f98SSadaf Ebrahimi * The flows are similar, and the PSA Crypto Driver Model uses the same functions 1136*62c56f98SSadaf Ebrahimi * for both of the flows. 1137*62c56f98SSadaf Ebrahimi * 1138*62c56f98SSadaf Ebrahimi * There are two different final functions for the flows, 1139*62c56f98SSadaf Ebrahimi * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`. 1140*62c56f98SSadaf Ebrahimi * `psa_drv_se_key_derivation_derive` is used when the key material should be 1141*62c56f98SSadaf Ebrahimi * placed in a slot on the hardware and not exposed to the caller. 1142*62c56f98SSadaf Ebrahimi * `psa_drv_se_key_derivation_export` is used when the key material should be 1143*62c56f98SSadaf Ebrahimi * returned to the PSA Cryptographic API implementation. 1144*62c56f98SSadaf Ebrahimi * 1145*62c56f98SSadaf Ebrahimi * Different key derivation algorithms require a different number of inputs. 1146*62c56f98SSadaf Ebrahimi * Instead of having an API that takes as input variable length arrays, which 1147*62c56f98SSadaf Ebrahimi * can be problematic to manage on embedded platforms, the inputs are passed 1148*62c56f98SSadaf Ebrahimi * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that 1149*62c56f98SSadaf Ebrahimi * is called multiple times with different `collateral_id`s. Thus, for a key 1150*62c56f98SSadaf Ebrahimi * derivation algorithm that required 3 parameter inputs, the flow would look 1151*62c56f98SSadaf Ebrahimi * something like: 1152*62c56f98SSadaf Ebrahimi * ~~~~~~~~~~~~~{.c} 1153*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes); 1154*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0, 1155*62c56f98SSadaf Ebrahimi * p_collateral_0, 1156*62c56f98SSadaf Ebrahimi * collateral_0_size); 1157*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1, 1158*62c56f98SSadaf Ebrahimi * p_collateral_1, 1159*62c56f98SSadaf Ebrahimi * collateral_1_size); 1160*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2, 1161*62c56f98SSadaf Ebrahimi * p_collateral_2, 1162*62c56f98SSadaf Ebrahimi * collateral_2_size); 1163*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_derive(); 1164*62c56f98SSadaf Ebrahimi * ~~~~~~~~~~~~~ 1165*62c56f98SSadaf Ebrahimi * 1166*62c56f98SSadaf Ebrahimi * key agreement example: 1167*62c56f98SSadaf Ebrahimi * ~~~~~~~~~~~~~{.c} 1168*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes); 1169*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size); 1170*62c56f98SSadaf Ebrahimi * psa_drv_se_key_derivation_export(p_session_key, 1171*62c56f98SSadaf Ebrahimi * session_key_size, 1172*62c56f98SSadaf Ebrahimi * &session_key_length); 1173*62c56f98SSadaf Ebrahimi * ~~~~~~~~~~~~~ 1174*62c56f98SSadaf Ebrahimi */ 1175*62c56f98SSadaf Ebrahimi /**@{*/ 1176*62c56f98SSadaf Ebrahimi 1177*62c56f98SSadaf Ebrahimi /** \brief A function that Sets up a secure element key derivation operation by 1178*62c56f98SSadaf Ebrahimi * specifying the algorithm and the source key sot 1179*62c56f98SSadaf Ebrahimi * 1180*62c56f98SSadaf Ebrahimi * \param[in,out] drv_context The driver context structure. 1181*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure containing any 1182*62c56f98SSadaf Ebrahimi * context information for the implementation 1183*62c56f98SSadaf Ebrahimi * \param[in] kdf_alg The algorithm to be used for the key derivation 1184*62c56f98SSadaf Ebrahimi * \param[in] source_key The key to be used as the source material for 1185*62c56f98SSadaf Ebrahimi * the key derivation 1186*62c56f98SSadaf Ebrahimi * 1187*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 1188*62c56f98SSadaf Ebrahimi */ 1189*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context, 1190*62c56f98SSadaf Ebrahimi void *op_context, 1191*62c56f98SSadaf Ebrahimi psa_algorithm_t kdf_alg, 1192*62c56f98SSadaf Ebrahimi psa_key_slot_number_t source_key); 1193*62c56f98SSadaf Ebrahimi 1194*62c56f98SSadaf Ebrahimi /** \brief A function that provides collateral (parameters) needed for a secure 1195*62c56f98SSadaf Ebrahimi * element key derivation or key agreement operation 1196*62c56f98SSadaf Ebrahimi * 1197*62c56f98SSadaf Ebrahimi * Since many key derivation algorithms require multiple parameters, it is 1198*62c56f98SSadaf Ebrahimi * expected that this function may be called multiple times for the same 1199*62c56f98SSadaf Ebrahimi * operation, each with a different algorithm-specific `collateral_id` 1200*62c56f98SSadaf Ebrahimi * 1201*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure containing any 1202*62c56f98SSadaf Ebrahimi * context information for the implementation 1203*62c56f98SSadaf Ebrahimi * \param[in] collateral_id An ID for the collateral being provided 1204*62c56f98SSadaf Ebrahimi * \param[in] p_collateral A buffer containing the collateral data 1205*62c56f98SSadaf Ebrahimi * \param[in] collateral_size The size in bytes of the collateral 1206*62c56f98SSadaf Ebrahimi * 1207*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 1208*62c56f98SSadaf Ebrahimi */ 1209*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context, 1210*62c56f98SSadaf Ebrahimi uint32_t collateral_id, 1211*62c56f98SSadaf Ebrahimi const uint8_t *p_collateral, 1212*62c56f98SSadaf Ebrahimi size_t collateral_size); 1213*62c56f98SSadaf Ebrahimi 1214*62c56f98SSadaf Ebrahimi /** \brief A function that performs the final secure element key derivation 1215*62c56f98SSadaf Ebrahimi * step and place the generated key material in a slot 1216*62c56f98SSadaf Ebrahimi * 1217*62c56f98SSadaf Ebrahimi * \param[in,out] op_context A hardware-specific structure containing any 1218*62c56f98SSadaf Ebrahimi * context information for the implementation 1219*62c56f98SSadaf Ebrahimi * \param[in] dest_key The slot where the generated key material 1220*62c56f98SSadaf Ebrahimi * should be placed 1221*62c56f98SSadaf Ebrahimi * 1222*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 1223*62c56f98SSadaf Ebrahimi */ 1224*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context, 1225*62c56f98SSadaf Ebrahimi psa_key_slot_number_t dest_key); 1226*62c56f98SSadaf Ebrahimi 1227*62c56f98SSadaf Ebrahimi /** \brief A function that performs the final step of a secure element key 1228*62c56f98SSadaf Ebrahimi * agreement and place the generated key material in a buffer 1229*62c56f98SSadaf Ebrahimi * 1230*62c56f98SSadaf Ebrahimi * \param[out] p_output Buffer in which to place the generated key 1231*62c56f98SSadaf Ebrahimi * material 1232*62c56f98SSadaf Ebrahimi * \param[in] output_size The size in bytes of `p_output` 1233*62c56f98SSadaf Ebrahimi * \param[out] p_output_length Upon success, contains the number of bytes of 1234*62c56f98SSadaf Ebrahimi * key material placed in `p_output` 1235*62c56f98SSadaf Ebrahimi * 1236*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 1237*62c56f98SSadaf Ebrahimi */ 1238*62c56f98SSadaf Ebrahimi typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context, 1239*62c56f98SSadaf Ebrahimi uint8_t *p_output, 1240*62c56f98SSadaf Ebrahimi size_t output_size, 1241*62c56f98SSadaf Ebrahimi size_t *p_output_length); 1242*62c56f98SSadaf Ebrahimi 1243*62c56f98SSadaf Ebrahimi /** 1244*62c56f98SSadaf Ebrahimi * \brief A struct containing all of the function pointers needed to for secure 1245*62c56f98SSadaf Ebrahimi * element key derivation and agreement 1246*62c56f98SSadaf Ebrahimi * 1247*62c56f98SSadaf Ebrahimi * PSA Crypto API implementations should populate instances of the table as 1248*62c56f98SSadaf Ebrahimi * appropriate upon startup. 1249*62c56f98SSadaf Ebrahimi * 1250*62c56f98SSadaf Ebrahimi * If one of the functions is not implemented, it should be set to NULL. 1251*62c56f98SSadaf Ebrahimi */ 1252*62c56f98SSadaf Ebrahimi typedef struct { 1253*62c56f98SSadaf Ebrahimi /** The driver-specific size of the key derivation context */ 1254*62c56f98SSadaf Ebrahimi size_t MBEDTLS_PRIVATE(context_size); 1255*62c56f98SSadaf Ebrahimi /** Function that performs a key derivation setup */ 1256*62c56f98SSadaf Ebrahimi psa_drv_se_key_derivation_setup_t MBEDTLS_PRIVATE(p_setup); 1257*62c56f98SSadaf Ebrahimi /** Function that sets key derivation collateral */ 1258*62c56f98SSadaf Ebrahimi psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral); 1259*62c56f98SSadaf Ebrahimi /** Function that performs a final key derivation step */ 1260*62c56f98SSadaf Ebrahimi psa_drv_se_key_derivation_derive_t MBEDTLS_PRIVATE(p_derive); 1261*62c56f98SSadaf Ebrahimi /** Function that performs a final key derivation or agreement and 1262*62c56f98SSadaf Ebrahimi * exports the key */ 1263*62c56f98SSadaf Ebrahimi psa_drv_se_key_derivation_export_t MBEDTLS_PRIVATE(p_export); 1264*62c56f98SSadaf Ebrahimi } psa_drv_se_key_derivation_t; 1265*62c56f98SSadaf Ebrahimi 1266*62c56f98SSadaf Ebrahimi /**@}*/ 1267*62c56f98SSadaf Ebrahimi 1268*62c56f98SSadaf Ebrahimi /** \defgroup se_registration Secure element driver registration 1269*62c56f98SSadaf Ebrahimi */ 1270*62c56f98SSadaf Ebrahimi /**@{*/ 1271*62c56f98SSadaf Ebrahimi 1272*62c56f98SSadaf Ebrahimi /** A structure containing pointers to all the entry points of a 1273*62c56f98SSadaf Ebrahimi * secure element driver. 1274*62c56f98SSadaf Ebrahimi * 1275*62c56f98SSadaf Ebrahimi * Future versions of this specification may add extra substructures at 1276*62c56f98SSadaf Ebrahimi * the end of this structure. 1277*62c56f98SSadaf Ebrahimi */ 1278*62c56f98SSadaf Ebrahimi typedef struct { 1279*62c56f98SSadaf Ebrahimi /** The version of the driver HAL that this driver implements. 1280*62c56f98SSadaf Ebrahimi * This is a protection against loading driver binaries built against 1281*62c56f98SSadaf Ebrahimi * a different version of this specification. 1282*62c56f98SSadaf Ebrahimi * Use #PSA_DRV_SE_HAL_VERSION. 1283*62c56f98SSadaf Ebrahimi */ 1284*62c56f98SSadaf Ebrahimi uint32_t MBEDTLS_PRIVATE(hal_version); 1285*62c56f98SSadaf Ebrahimi 1286*62c56f98SSadaf Ebrahimi /** The size of the driver's persistent data in bytes. 1287*62c56f98SSadaf Ebrahimi * 1288*62c56f98SSadaf Ebrahimi * This can be 0 if the driver does not need persistent data. 1289*62c56f98SSadaf Ebrahimi * 1290*62c56f98SSadaf Ebrahimi * See the documentation of psa_drv_se_context_t::persistent_data 1291*62c56f98SSadaf Ebrahimi * for more information about why and how a driver can use 1292*62c56f98SSadaf Ebrahimi * persistent data. 1293*62c56f98SSadaf Ebrahimi */ 1294*62c56f98SSadaf Ebrahimi size_t MBEDTLS_PRIVATE(persistent_data_size); 1295*62c56f98SSadaf Ebrahimi 1296*62c56f98SSadaf Ebrahimi /** The driver initialization function. 1297*62c56f98SSadaf Ebrahimi * 1298*62c56f98SSadaf Ebrahimi * This function is called once during the initialization of the 1299*62c56f98SSadaf Ebrahimi * PSA Cryptography subsystem, before any other function of the 1300*62c56f98SSadaf Ebrahimi * driver is called. If this function returns a failure status, 1301*62c56f98SSadaf Ebrahimi * the driver will be unusable, at least until the next system reset. 1302*62c56f98SSadaf Ebrahimi * 1303*62c56f98SSadaf Ebrahimi * If this field is \c NULL, it is equivalent to a function that does 1304*62c56f98SSadaf Ebrahimi * nothing and returns #PSA_SUCCESS. 1305*62c56f98SSadaf Ebrahimi */ 1306*62c56f98SSadaf Ebrahimi psa_drv_se_init_t MBEDTLS_PRIVATE(p_init); 1307*62c56f98SSadaf Ebrahimi 1308*62c56f98SSadaf Ebrahimi const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management); 1309*62c56f98SSadaf Ebrahimi const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac); 1310*62c56f98SSadaf Ebrahimi const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher); 1311*62c56f98SSadaf Ebrahimi const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead); 1312*62c56f98SSadaf Ebrahimi const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric); 1313*62c56f98SSadaf Ebrahimi const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation); 1314*62c56f98SSadaf Ebrahimi } psa_drv_se_t; 1315*62c56f98SSadaf Ebrahimi 1316*62c56f98SSadaf Ebrahimi /** The current version of the secure element driver HAL. 1317*62c56f98SSadaf Ebrahimi */ 1318*62c56f98SSadaf Ebrahimi /* 0.0.0 patchlevel 5 */ 1319*62c56f98SSadaf Ebrahimi #define PSA_DRV_SE_HAL_VERSION 0x00000005 1320*62c56f98SSadaf Ebrahimi 1321*62c56f98SSadaf Ebrahimi /** Register an external cryptoprocessor (secure element) driver. 1322*62c56f98SSadaf Ebrahimi * 1323*62c56f98SSadaf Ebrahimi * This function is only intended to be used by driver code, not by 1324*62c56f98SSadaf Ebrahimi * application code. In implementations with separation between the 1325*62c56f98SSadaf Ebrahimi * PSA cryptography module and applications, this function should 1326*62c56f98SSadaf Ebrahimi * only be available to callers that run in the same memory space as 1327*62c56f98SSadaf Ebrahimi * the cryptography module, and should not be exposed to applications 1328*62c56f98SSadaf Ebrahimi * running in a different memory space. 1329*62c56f98SSadaf Ebrahimi * 1330*62c56f98SSadaf Ebrahimi * This function may be called before psa_crypto_init(). It is 1331*62c56f98SSadaf Ebrahimi * implementation-defined whether this function may be called 1332*62c56f98SSadaf Ebrahimi * after psa_crypto_init(). 1333*62c56f98SSadaf Ebrahimi * 1334*62c56f98SSadaf Ebrahimi * \note Implementations store metadata about keys including the lifetime 1335*62c56f98SSadaf Ebrahimi * value, which contains the driver's location indicator. Therefore, 1336*62c56f98SSadaf Ebrahimi * from one instantiation of the PSA Cryptography 1337*62c56f98SSadaf Ebrahimi * library to the next one, if there is a key in storage with a certain 1338*62c56f98SSadaf Ebrahimi * lifetime value, you must always register the same driver (or an 1339*62c56f98SSadaf Ebrahimi * updated version that communicates with the same secure element) 1340*62c56f98SSadaf Ebrahimi * with the same location value. 1341*62c56f98SSadaf Ebrahimi * 1342*62c56f98SSadaf Ebrahimi * \param location The location value through which this driver will 1343*62c56f98SSadaf Ebrahimi * be exposed to applications. 1344*62c56f98SSadaf Ebrahimi * This driver will be used for all keys such that 1345*62c56f98SSadaf Ebrahimi * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`. 1346*62c56f98SSadaf Ebrahimi * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved 1347*62c56f98SSadaf Ebrahimi * and may not be used for drivers. Implementations 1348*62c56f98SSadaf Ebrahimi * may reserve other values. 1349*62c56f98SSadaf Ebrahimi * \param[in] methods The method table of the driver. This structure must 1350*62c56f98SSadaf Ebrahimi * remain valid for as long as the cryptography 1351*62c56f98SSadaf Ebrahimi * module keeps running. It is typically a global 1352*62c56f98SSadaf Ebrahimi * constant. 1353*62c56f98SSadaf Ebrahimi * 1354*62c56f98SSadaf Ebrahimi * \return #PSA_SUCCESS 1355*62c56f98SSadaf Ebrahimi * The driver was successfully registered. Applications can now 1356*62c56f98SSadaf Ebrahimi * use \p location to access keys through the methods passed to 1357*62c56f98SSadaf Ebrahimi * this function. 1358*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_BAD_STATE 1359*62c56f98SSadaf Ebrahimi * This function was called after the initialization of the 1360*62c56f98SSadaf Ebrahimi * cryptography module, and this implementation does not support 1361*62c56f98SSadaf Ebrahimi * driver registration at this stage. 1362*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_ALREADY_EXISTS 1363*62c56f98SSadaf Ebrahimi * There is already a registered driver for this value of \p location. 1364*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_INVALID_ARGUMENT 1365*62c56f98SSadaf Ebrahimi * \p location is a reserved value. 1366*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_NOT_SUPPORTED 1367*62c56f98SSadaf Ebrahimi * `methods->hal_version` is not supported by this implementation. 1368*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_INSUFFICIENT_MEMORY 1369*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_NOT_PERMITTED 1370*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_STORAGE_FAILURE 1371*62c56f98SSadaf Ebrahimi * \return #PSA_ERROR_DATA_CORRUPT 1372*62c56f98SSadaf Ebrahimi */ 1373*62c56f98SSadaf Ebrahimi psa_status_t psa_register_se_driver( 1374*62c56f98SSadaf Ebrahimi psa_key_location_t location, 1375*62c56f98SSadaf Ebrahimi const psa_drv_se_t *methods); 1376*62c56f98SSadaf Ebrahimi 1377*62c56f98SSadaf Ebrahimi /**@}*/ 1378*62c56f98SSadaf Ebrahimi 1379*62c56f98SSadaf Ebrahimi #ifdef __cplusplus 1380*62c56f98SSadaf Ebrahimi } 1381*62c56f98SSadaf Ebrahimi #endif 1382*62c56f98SSadaf Ebrahimi 1383*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_SE_DRIVER_H */ 1384