1*62c56f98SSadaf Ebrahimi /* 2*62c56f98SSadaf Ebrahimi * PSA ECP layer on top of Mbed TLS crypto 3*62c56f98SSadaf Ebrahimi */ 4*62c56f98SSadaf Ebrahimi /* 5*62c56f98SSadaf Ebrahimi * Copyright The Mbed TLS Contributors 6*62c56f98SSadaf Ebrahimi * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7*62c56f98SSadaf Ebrahimi */ 8*62c56f98SSadaf Ebrahimi 9*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_ECP_H 10*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_ECP_H 11*62c56f98SSadaf Ebrahimi 12*62c56f98SSadaf Ebrahimi #include <psa/crypto.h> 13*62c56f98SSadaf Ebrahimi #include <mbedtls/ecp.h> 14*62c56f98SSadaf Ebrahimi 15*62c56f98SSadaf Ebrahimi /** Load the contents of a key buffer into an internal ECP representation 16*62c56f98SSadaf Ebrahimi * 17*62c56f98SSadaf Ebrahimi * \param[in] type The type of key contained in \p data. 18*62c56f98SSadaf Ebrahimi * \param[in] curve_bits The nominal bit-size of the curve. 19*62c56f98SSadaf Ebrahimi * It must be consistent with the representation 20*62c56f98SSadaf Ebrahimi * passed in \p data. 21*62c56f98SSadaf Ebrahimi * This can be 0, in which case the bit-size 22*62c56f98SSadaf Ebrahimi * is inferred from \p data_length (which is possible 23*62c56f98SSadaf Ebrahimi * for all key types and representation formats 24*62c56f98SSadaf Ebrahimi * formats that are currently supported or will 25*62c56f98SSadaf Ebrahimi * be in the foreseeable future). 26*62c56f98SSadaf Ebrahimi * \param[in] data The buffer from which to load the representation. 27*62c56f98SSadaf Ebrahimi * \param[in] data_length The size in bytes of \p data. 28*62c56f98SSadaf Ebrahimi * \param[out] p_ecp Returns a pointer to an ECP context on success. 29*62c56f98SSadaf Ebrahimi * The caller is responsible for freeing both the 30*62c56f98SSadaf Ebrahimi * contents of the context and the context itself 31*62c56f98SSadaf Ebrahimi * when done. 32*62c56f98SSadaf Ebrahimi */ 33*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_load_representation(psa_key_type_t type, 34*62c56f98SSadaf Ebrahimi size_t curve_bits, 35*62c56f98SSadaf Ebrahimi const uint8_t *data, 36*62c56f98SSadaf Ebrahimi size_t data_length, 37*62c56f98SSadaf Ebrahimi mbedtls_ecp_keypair **p_ecp); 38*62c56f98SSadaf Ebrahimi 39*62c56f98SSadaf Ebrahimi /** Load the public part of an internal ECP, if required. 40*62c56f98SSadaf Ebrahimi * 41*62c56f98SSadaf Ebrahimi * \param ecp The ECP context to load the public part for. 42*62c56f98SSadaf Ebrahimi * 43*62c56f98SSadaf Ebrahimi * \return PSA_SUCCESS on success, otherwise an MPI error. 44*62c56f98SSadaf Ebrahimi */ 45*62c56f98SSadaf Ebrahimi 46*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp); 47*62c56f98SSadaf Ebrahimi 48*62c56f98SSadaf Ebrahimi /** Import an ECP key in binary format. 49*62c56f98SSadaf Ebrahimi * 50*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 51*62c56f98SSadaf Ebrahimi * import_key entry point. This function behaves as an import_key 52*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 53*62c56f98SSadaf Ebrahimi * transparent drivers. 54*62c56f98SSadaf Ebrahimi * 55*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes for the key to import. 56*62c56f98SSadaf Ebrahimi * \param[in] data The buffer containing the key data in import 57*62c56f98SSadaf Ebrahimi * format. 58*62c56f98SSadaf Ebrahimi * \param[in] data_length Size of the \p data buffer in bytes. 59*62c56f98SSadaf Ebrahimi * \param[out] key_buffer The buffer containing the key data in output 60*62c56f98SSadaf Ebrahimi * format. 61*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This 62*62c56f98SSadaf Ebrahimi * size is greater or equal to \p data_length. 63*62c56f98SSadaf Ebrahimi * \param[out] key_buffer_length The length of the data written in \p 64*62c56f98SSadaf Ebrahimi * key_buffer in bytes. 65*62c56f98SSadaf Ebrahimi * \param[out] bits The key size in number of bits. 66*62c56f98SSadaf Ebrahimi * 67*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS The ECP key was imported successfully. 68*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 69*62c56f98SSadaf Ebrahimi * The key data is not correctly formatted. 70*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 71*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 72*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 73*62c56f98SSadaf Ebrahimi */ 74*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_import_key( 75*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 76*62c56f98SSadaf Ebrahimi const uint8_t *data, size_t data_length, 77*62c56f98SSadaf Ebrahimi uint8_t *key_buffer, size_t key_buffer_size, 78*62c56f98SSadaf Ebrahimi size_t *key_buffer_length, size_t *bits); 79*62c56f98SSadaf Ebrahimi 80*62c56f98SSadaf Ebrahimi /** Export an ECP key to export representation 81*62c56f98SSadaf Ebrahimi * 82*62c56f98SSadaf Ebrahimi * \param[in] type The type of key (public/private) to export 83*62c56f98SSadaf Ebrahimi * \param[in] ecp The internal ECP representation from which to export 84*62c56f98SSadaf Ebrahimi * \param[out] data The buffer to export to 85*62c56f98SSadaf Ebrahimi * \param[in] data_size The length of the buffer to export to 86*62c56f98SSadaf Ebrahimi * \param[out] data_length The amount of bytes written to \p data 87*62c56f98SSadaf Ebrahimi */ 88*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type, 89*62c56f98SSadaf Ebrahimi mbedtls_ecp_keypair *ecp, 90*62c56f98SSadaf Ebrahimi uint8_t *data, 91*62c56f98SSadaf Ebrahimi size_t data_size, 92*62c56f98SSadaf Ebrahimi size_t *data_length); 93*62c56f98SSadaf Ebrahimi 94*62c56f98SSadaf Ebrahimi /** Export an ECP public key or the public part of an ECP key pair in binary 95*62c56f98SSadaf Ebrahimi * format. 96*62c56f98SSadaf Ebrahimi * 97*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 98*62c56f98SSadaf Ebrahimi * export_public_key entry point. This function behaves as an 99*62c56f98SSadaf Ebrahimi * export_public_key entry point as defined in the PSA driver interface 100*62c56f98SSadaf Ebrahimi * specification. 101*62c56f98SSadaf Ebrahimi * 102*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes for the key to export. 103*62c56f98SSadaf Ebrahimi * \param[in] key_buffer Material or context of the key to export. 104*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 105*62c56f98SSadaf Ebrahimi * \param[out] data Buffer where the key data is to be written. 106*62c56f98SSadaf Ebrahimi * \param[in] data_size Size of the \p data buffer in bytes. 107*62c56f98SSadaf Ebrahimi * \param[out] data_length On success, the number of bytes written in 108*62c56f98SSadaf Ebrahimi * \p data 109*62c56f98SSadaf Ebrahimi * 110*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS The ECP public key was exported successfully. 111*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 112*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 113*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 114*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 115*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 116*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 117*62c56f98SSadaf Ebrahimi */ 118*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_export_public_key( 119*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 120*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 121*62c56f98SSadaf Ebrahimi uint8_t *data, size_t data_size, size_t *data_length); 122*62c56f98SSadaf Ebrahimi 123*62c56f98SSadaf Ebrahimi /** 124*62c56f98SSadaf Ebrahimi * \brief Generate an ECP key. 125*62c56f98SSadaf Ebrahimi * 126*62c56f98SSadaf Ebrahimi * \note The signature of the function is that of a PSA driver generate_key 127*62c56f98SSadaf Ebrahimi * entry point. 128*62c56f98SSadaf Ebrahimi * 129*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes for the ECP key to generate. 130*62c56f98SSadaf Ebrahimi * \param[out] key_buffer Buffer where the key data is to be written. 131*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of \p key_buffer in bytes. 132*62c56f98SSadaf Ebrahimi * \param[out] key_buffer_length On success, the number of bytes written in 133*62c56f98SSadaf Ebrahimi * \p key_buffer. 134*62c56f98SSadaf Ebrahimi * 135*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 136*62c56f98SSadaf Ebrahimi * The key was successfully generated. 137*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 138*62c56f98SSadaf Ebrahimi * Key length or type not supported. 139*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 140*62c56f98SSadaf Ebrahimi * The size of \p key_buffer is too small. 141*62c56f98SSadaf Ebrahimi */ 142*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecp_generate_key( 143*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 144*62c56f98SSadaf Ebrahimi uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length); 145*62c56f98SSadaf Ebrahimi 146*62c56f98SSadaf Ebrahimi /** Sign an already-calculated hash with ECDSA. 147*62c56f98SSadaf Ebrahimi * 148*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 149*62c56f98SSadaf Ebrahimi * sign_hash entry point. This function behaves as a sign_hash 150*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 151*62c56f98SSadaf Ebrahimi * transparent drivers. 152*62c56f98SSadaf Ebrahimi * 153*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the ECC key to use for the 154*62c56f98SSadaf Ebrahimi * operation. 155*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the ECC key context. 156*62c56f98SSadaf Ebrahimi * format. 157*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 158*62c56f98SSadaf Ebrahimi * \param[in] alg Randomized or deterministic ECDSA algorithm. 159*62c56f98SSadaf Ebrahimi * \param[in] hash The hash or message to sign. 160*62c56f98SSadaf Ebrahimi * \param[in] hash_length Size of the \p hash buffer in bytes. 161*62c56f98SSadaf Ebrahimi * \param[out] signature Buffer where the signature is to be written. 162*62c56f98SSadaf Ebrahimi * \param[in] signature_size Size of the \p signature buffer in bytes. 163*62c56f98SSadaf Ebrahimi * \param[out] signature_length On success, the number of bytes 164*62c56f98SSadaf Ebrahimi * that make up the returned signature value. 165*62c56f98SSadaf Ebrahimi * 166*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 167*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 168*62c56f98SSadaf Ebrahimi * The size of the \p signature buffer is too small. You can 169*62c56f98SSadaf Ebrahimi * determine a sufficient buffer size by calling 170*62c56f98SSadaf Ebrahimi * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_ECC_KEY_PAIR, \c key_bits, 171*62c56f98SSadaf Ebrahimi * \p alg) where \c key_bits is the bit-size of the ECC key. 172*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 173*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 174*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 175*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 176*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 177*62c56f98SSadaf Ebrahimi */ 178*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecdsa_sign_hash( 179*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 180*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 181*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, 182*62c56f98SSadaf Ebrahimi uint8_t *signature, size_t signature_size, size_t *signature_length); 183*62c56f98SSadaf Ebrahimi 184*62c56f98SSadaf Ebrahimi /** 185*62c56f98SSadaf Ebrahimi * \brief Verify an ECDSA hash or short message signature. 186*62c56f98SSadaf Ebrahimi * 187*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 188*62c56f98SSadaf Ebrahimi * verify_hash entry point. This function behaves as a verify_hash 189*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 190*62c56f98SSadaf Ebrahimi * transparent drivers. 191*62c56f98SSadaf Ebrahimi * 192*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the ECC key to use for the 193*62c56f98SSadaf Ebrahimi * operation. 194*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the ECC key context. 195*62c56f98SSadaf Ebrahimi * format. 196*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 197*62c56f98SSadaf Ebrahimi * \param[in] alg Randomized or deterministic ECDSA algorithm. 198*62c56f98SSadaf Ebrahimi * \param[in] hash The hash or message whose signature is to be 199*62c56f98SSadaf Ebrahimi * verified. 200*62c56f98SSadaf Ebrahimi * \param[in] hash_length Size of the \p hash buffer in bytes. 201*62c56f98SSadaf Ebrahimi * \param[in] signature Buffer containing the signature to verify. 202*62c56f98SSadaf Ebrahimi * \param[in] signature_length Size of the \p signature buffer in bytes. 203*62c56f98SSadaf Ebrahimi * 204*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 205*62c56f98SSadaf Ebrahimi * The signature is valid. 206*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_SIGNATURE 207*62c56f98SSadaf Ebrahimi * The calculation was performed successfully, but the passed 208*62c56f98SSadaf Ebrahimi * signature is not a valid signature. 209*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 210*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 211*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 212*62c56f98SSadaf Ebrahimi */ 213*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_ecdsa_verify_hash( 214*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 215*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 216*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, 217*62c56f98SSadaf Ebrahimi const uint8_t *signature, size_t signature_length); 218*62c56f98SSadaf Ebrahimi 219*62c56f98SSadaf Ebrahimi 220*62c56f98SSadaf Ebrahimi /** Perform a key agreement and return the raw ECDH shared secret. 221*62c56f98SSadaf Ebrahimi * 222*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 223*62c56f98SSadaf Ebrahimi * key_agreement entry point. This function behaves as a key_agreement 224*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 225*62c56f98SSadaf Ebrahimi * transparent drivers. 226*62c56f98SSadaf Ebrahimi * 227*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the key to use for the 228*62c56f98SSadaf Ebrahimi * operation. 229*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the private key 230*62c56f98SSadaf Ebrahimi * context. 231*62c56f98SSadaf Ebrahimi * \param[in] key_buffer_size Size of the \p key_buffer buffer in 232*62c56f98SSadaf Ebrahimi * bytes. 233*62c56f98SSadaf Ebrahimi * \param[in] alg A key agreement algorithm that is 234*62c56f98SSadaf Ebrahimi * compatible with the type of the key. 235*62c56f98SSadaf Ebrahimi * \param[in] peer_key The buffer containing the key context 236*62c56f98SSadaf Ebrahimi * of the peer's public key. 237*62c56f98SSadaf Ebrahimi * \param[in] peer_key_length Size of the \p peer_key buffer in 238*62c56f98SSadaf Ebrahimi * bytes. 239*62c56f98SSadaf Ebrahimi * \param[out] shared_secret The buffer to which the shared secret 240*62c56f98SSadaf Ebrahimi * is to be written. 241*62c56f98SSadaf Ebrahimi * \param[in] shared_secret_size Size of the \p shared_secret buffer in 242*62c56f98SSadaf Ebrahimi * bytes. 243*62c56f98SSadaf Ebrahimi * \param[out] shared_secret_length On success, the number of bytes that make 244*62c56f98SSadaf Ebrahimi * up the returned shared secret. 245*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 246*62c56f98SSadaf Ebrahimi * Success. Shared secret successfully calculated. 247*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription 248*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription 249*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 250*62c56f98SSadaf Ebrahimi * \p alg is not a key agreement algorithm, or 251*62c56f98SSadaf Ebrahimi * \p private_key is not compatible with \p alg, 252*62c56f98SSadaf Ebrahimi * or \p peer_key is not valid for \p alg or not compatible with 253*62c56f98SSadaf Ebrahimi * \p private_key. 254*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 255*62c56f98SSadaf Ebrahimi * \p shared_secret_size is too small 256*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 257*62c56f98SSadaf Ebrahimi * \p alg is not a supported key agreement algorithm. 258*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 259*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 260*62c56f98SSadaf Ebrahimi */ 261*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_key_agreement_ecdh( 262*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 263*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 264*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length, 265*62c56f98SSadaf Ebrahimi uint8_t *shared_secret, size_t shared_secret_size, 266*62c56f98SSadaf Ebrahimi size_t *shared_secret_length); 267*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_ECP_H */ 268