1b0563631STom Van Eyck /* 2b0563631STom Van Eyck * PSA RSA layer on top of Mbed TLS crypto 3b0563631STom Van Eyck */ 4b0563631STom Van Eyck /* 5b0563631STom Van Eyck * Copyright The Mbed TLS Contributors 6b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7b0563631STom Van Eyck */ 8b0563631STom Van Eyck 9b0563631STom Van Eyck #ifndef PSA_CRYPTO_RSA_H 10b0563631STom Van Eyck #define PSA_CRYPTO_RSA_H 11b0563631STom Van Eyck 12b0563631STom Van Eyck #include <psa/crypto.h> 13b0563631STom Van Eyck #include <mbedtls/rsa.h> 14b0563631STom Van Eyck 15b0563631STom Van Eyck /** Load the contents of a key buffer into an internal RSA representation 16b0563631STom Van Eyck * 17b0563631STom Van Eyck * \param[in] type The type of key contained in \p data. 18b0563631STom Van Eyck * \param[in] data The buffer from which to load the representation. 19b0563631STom Van Eyck * \param[in] data_length The size in bytes of \p data. 20b0563631STom Van Eyck * \param[out] p_rsa Returns a pointer to an RSA context on success. 21b0563631STom Van Eyck * The caller is responsible for freeing both the 22b0563631STom Van Eyck * contents of the context and the context itself 23b0563631STom Van Eyck * when done. 24b0563631STom Van Eyck */ 25b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_load_representation(psa_key_type_t type, 26b0563631STom Van Eyck const uint8_t *data, 27b0563631STom Van Eyck size_t data_length, 28b0563631STom Van Eyck mbedtls_rsa_context **p_rsa); 29b0563631STom Van Eyck 30b0563631STom Van Eyck /** Import an RSA key in binary format. 31b0563631STom Van Eyck * 32b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 33b0563631STom Van Eyck * import_key entry point. This function behaves as an import_key 34b0563631STom Van Eyck * entry point as defined in the PSA driver interface specification for 35b0563631STom Van Eyck * transparent drivers. 36b0563631STom Van Eyck * 37b0563631STom Van Eyck * \param[in] attributes The attributes for the key to import. 38b0563631STom Van Eyck * \param[in] data The buffer containing the key data in import 39b0563631STom Van Eyck * format. 40b0563631STom Van Eyck * \param[in] data_length Size of the \p data buffer in bytes. 41b0563631STom Van Eyck * \param[out] key_buffer The buffer containing the key data in output 42b0563631STom Van Eyck * format. 43b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This 44b0563631STom Van Eyck * size is greater or equal to \p data_length. 45b0563631STom Van Eyck * \param[out] key_buffer_length The length of the data written in \p 46b0563631STom Van Eyck * key_buffer in bytes. 47b0563631STom Van Eyck * \param[out] bits The key size in number of bits. 48b0563631STom Van Eyck * 49b0563631STom Van Eyck * \retval #PSA_SUCCESS The RSA key was imported successfully. 50b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 51b0563631STom Van Eyck * The key data is not correctly formatted. 52b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 53b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 54b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 55b0563631STom Van Eyck */ 56b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_import_key( 57b0563631STom Van Eyck const psa_key_attributes_t *attributes, 58b0563631STom Van Eyck const uint8_t *data, size_t data_length, 59b0563631STom Van Eyck uint8_t *key_buffer, size_t key_buffer_size, 60b0563631STom Van Eyck size_t *key_buffer_length, size_t *bits); 61b0563631STom Van Eyck 62b0563631STom Van Eyck /** Export an RSA key to export representation 63b0563631STom Van Eyck * 64b0563631STom Van Eyck * \param[in] type The type of key (public/private) to export 65b0563631STom Van Eyck * \param[in] rsa The internal RSA representation from which to export 66b0563631STom Van Eyck * \param[out] data The buffer to export to 67b0563631STom Van Eyck * \param[in] data_size The length of the buffer to export to 68b0563631STom Van Eyck * \param[out] data_length The amount of bytes written to \p data 69b0563631STom Van Eyck */ 70b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type, 71b0563631STom Van Eyck mbedtls_rsa_context *rsa, 72b0563631STom Van Eyck uint8_t *data, 73b0563631STom Van Eyck size_t data_size, 74b0563631STom Van Eyck size_t *data_length); 75b0563631STom Van Eyck 76b0563631STom Van Eyck /** Export a public RSA key or the public part of an RSA key pair in binary 77b0563631STom Van Eyck * format. 78b0563631STom Van Eyck * 79b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 80b0563631STom Van Eyck * export_public_key entry point. This function behaves as an 81b0563631STom Van Eyck * export_public_key entry point as defined in the PSA driver interface 82b0563631STom Van Eyck * specification. 83b0563631STom Van Eyck * 84b0563631STom Van Eyck * \param[in] attributes The attributes for the key to export. 85b0563631STom Van Eyck * \param[in] key_buffer Material or context of the key to export. 86b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 87b0563631STom Van Eyck * \param[out] data Buffer where the key data is to be written. 88b0563631STom Van Eyck * \param[in] data_size Size of the \p data buffer in bytes. 89b0563631STom Van Eyck * \param[out] data_length On success, the number of bytes written in 90b0563631STom Van Eyck * \p data. 91b0563631STom Van Eyck * 92b0563631STom Van Eyck * \retval #PSA_SUCCESS The RSA public key was exported successfully. 93b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 94b0563631STom Van Eyck * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 95b0563631STom Van Eyck * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 96b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 97b0563631STom Van Eyck * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription 98b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 99b0563631STom Van Eyck */ 100b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_export_public_key( 101b0563631STom Van Eyck const psa_key_attributes_t *attributes, 102b0563631STom Van Eyck const uint8_t *key_buffer, size_t key_buffer_size, 103b0563631STom Van Eyck uint8_t *data, size_t data_size, size_t *data_length); 104b0563631STom Van Eyck 105b0563631STom Van Eyck /** 106b0563631STom Van Eyck * \brief Generate an RSA key. 107b0563631STom Van Eyck * 108b0563631STom Van Eyck * \param[in] attributes The attributes for the RSA key to generate. 109*cb034002SJerome Forissier * \param[in] custom_data The public exponent to use. 110b0563631STom Van Eyck * This can be a null pointer if 111b0563631STom Van Eyck * \c params_data_length is 0. 112*cb034002SJerome Forissier * \param custom_data_length Length of \p custom_data in bytes. 113b0563631STom Van Eyck * This can be 0, in which case the 114b0563631STom Van Eyck * public exponent will be 65537. 115b0563631STom Van Eyck * \param[out] key_buffer Buffer where the key data is to be written. 116b0563631STom Van Eyck * \param[in] key_buffer_size Size of \p key_buffer in bytes. 117b0563631STom Van Eyck * \param[out] key_buffer_length On success, the number of bytes written in 118b0563631STom Van Eyck * \p key_buffer. 119b0563631STom Van Eyck * 120b0563631STom Van Eyck * \retval #PSA_SUCCESS 121b0563631STom Van Eyck * The key was successfully generated. 122b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED 123b0563631STom Van Eyck * Key length or type not supported. 124b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 125b0563631STom Van Eyck * The size of \p key_buffer is too small. 126b0563631STom Van Eyck */ 127b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_generate_key( 128b0563631STom Van Eyck const psa_key_attributes_t *attributes, 129*cb034002SJerome Forissier const uint8_t *custom_data, size_t custom_data_length, 130b0563631STom Van Eyck uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length); 131b0563631STom Van Eyck 132b0563631STom Van Eyck /** Sign an already-calculated hash with an RSA private key. 133b0563631STom Van Eyck * 134b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 135b0563631STom Van Eyck * sign_hash entry point. This function behaves as a sign_hash 136b0563631STom Van Eyck * entry point as defined in the PSA driver interface specification for 137b0563631STom Van Eyck * transparent drivers. 138b0563631STom Van Eyck * 139b0563631STom Van Eyck * \param[in] attributes The attributes of the RSA key to use for the 140b0563631STom Van Eyck * operation. 141b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the RSA key context. 142b0563631STom Van Eyck * format. 143b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 144b0563631STom Van Eyck * \param[in] alg A signature algorithm that is compatible with 145b0563631STom Van Eyck * an RSA key. 146b0563631STom Van Eyck * \param[in] hash The hash or message to sign. 147b0563631STom Van Eyck * \param[in] hash_length Size of the \p hash buffer in bytes. 148b0563631STom Van Eyck * \param[out] signature Buffer where the signature is to be written. 149b0563631STom Van Eyck * \param[in] signature_size Size of the \p signature buffer in bytes. 150b0563631STom Van Eyck * \param[out] signature_length On success, the number of bytes 151b0563631STom Van Eyck * that make up the returned signature value. 152b0563631STom Van Eyck * 153b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 154b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 155b0563631STom Van Eyck * The size of the \p signature buffer is too small. You can 156b0563631STom Van Eyck * determine a sufficient buffer size by calling 157b0563631STom Van Eyck * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_RSA_KEY_PAIR, \c key_bits, 158b0563631STom Van Eyck * \p alg) where \c key_bits is the bit-size of the RSA key. 159b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 160b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 161b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 162b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 163b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 164b0563631STom Van Eyck */ 165b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_sign_hash( 166b0563631STom Van Eyck const psa_key_attributes_t *attributes, 167b0563631STom Van Eyck const uint8_t *key_buffer, size_t key_buffer_size, 168b0563631STom Van Eyck psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, 169b0563631STom Van Eyck uint8_t *signature, size_t signature_size, size_t *signature_length); 170b0563631STom Van Eyck 171b0563631STom Van Eyck /** 172b0563631STom Van Eyck * \brief Verify the signature a hash or short message using a public RSA key. 173b0563631STom Van Eyck * 174b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 175b0563631STom Van Eyck * verify_hash entry point. This function behaves as a verify_hash 176b0563631STom Van Eyck * entry point as defined in the PSA driver interface specification for 177b0563631STom Van Eyck * transparent drivers. 178b0563631STom Van Eyck * 179b0563631STom Van Eyck * \param[in] attributes The attributes of the RSA key to use for the 180b0563631STom Van Eyck * operation. 181b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the RSA key context. 182b0563631STom Van Eyck * format. 183b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 184b0563631STom Van Eyck * \param[in] alg A signature algorithm that is compatible with 185b0563631STom Van Eyck * an RSA key. 186b0563631STom Van Eyck * \param[in] hash The hash or message whose signature is to be 187b0563631STom Van Eyck * verified. 188b0563631STom Van Eyck * \param[in] hash_length Size of the \p hash buffer in bytes. 189b0563631STom Van Eyck * \param[in] signature Buffer containing the signature to verify. 190b0563631STom Van Eyck * \param[in] signature_length Size of the \p signature buffer in bytes. 191b0563631STom Van Eyck * 192b0563631STom Van Eyck * \retval #PSA_SUCCESS 193b0563631STom Van Eyck * The signature is valid. 194b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_SIGNATURE 195b0563631STom Van Eyck * The calculation was performed successfully, but the passed 196b0563631STom Van Eyck * signature is not a valid signature. 197b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 198b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 199b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 200b0563631STom Van Eyck */ 201b0563631STom Van Eyck psa_status_t mbedtls_psa_rsa_verify_hash( 202b0563631STom Van Eyck const psa_key_attributes_t *attributes, 203b0563631STom Van Eyck const uint8_t *key_buffer, size_t key_buffer_size, 204b0563631STom Van Eyck psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, 205b0563631STom Van Eyck const uint8_t *signature, size_t signature_length); 206b0563631STom Van Eyck 207b0563631STom Van Eyck /** 208b0563631STom Van Eyck * \brief Encrypt a short message with a public key. 209b0563631STom Van Eyck * 210b0563631STom Van Eyck * \param attributes The attributes for the key to import. 211b0563631STom Van Eyck * \param key_buffer Buffer where the key data is to be written. 212b0563631STom Van Eyck * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 213b0563631STom Van Eyck * \param input_length Size of the \p input buffer in bytes. 214b0563631STom Van Eyck * \param[in] salt A salt or label, if supported by the 215b0563631STom Van Eyck * encryption algorithm. 216b0563631STom Van Eyck * If the algorithm does not support a 217b0563631STom Van Eyck * salt, pass \c NULL. 218b0563631STom Van Eyck * If the algorithm supports an optional 219b0563631STom Van Eyck * salt and you do not want to pass a salt, 220b0563631STom Van Eyck * pass \c NULL. 221b0563631STom Van Eyck * 222b0563631STom Van Eyck * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 223b0563631STom Van Eyck * supported. 224b0563631STom Van Eyck * \param salt_length Size of the \p salt buffer in bytes. 225b0563631STom Van Eyck * If \p salt is \c NULL, pass 0. 226b0563631STom Van Eyck * \param[out] output Buffer where the encrypted message is to 227b0563631STom Van Eyck * be written. 228b0563631STom Van Eyck * \param output_size Size of the \p output buffer in bytes. 229b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes 230b0563631STom Van Eyck * that make up the returned output. 231b0563631STom Van Eyck * 232b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 233b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 234b0563631STom Van Eyck * The size of the \p output buffer is too small. You can 235b0563631STom Van Eyck * determine a sufficient buffer size by calling 236b0563631STom Van Eyck * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 237b0563631STom Van Eyck * where \c key_type and \c key_bits are the type and bit-size 238b0563631STom Van Eyck * respectively of \p key. 239b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 240b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 241b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 242b0563631STom Van Eyck * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 243b0563631STom Van Eyck * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 244b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 245b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 246b0563631STom Van Eyck * \retval #PSA_ERROR_BAD_STATE 247b0563631STom Van Eyck * The library has not been previously initialized by psa_crypto_init(). 248b0563631STom Van Eyck * It is implementation-dependent whether a failure to initialize 249b0563631STom Van Eyck * results in this error code. 250b0563631STom Van Eyck */ 251b0563631STom Van Eyck psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes, 252b0563631STom Van Eyck const uint8_t *key_buffer, 253b0563631STom Van Eyck size_t key_buffer_size, 254b0563631STom Van Eyck psa_algorithm_t alg, 255b0563631STom Van Eyck const uint8_t *input, 256b0563631STom Van Eyck size_t input_length, 257b0563631STom Van Eyck const uint8_t *salt, 258b0563631STom Van Eyck size_t salt_length, 259b0563631STom Van Eyck uint8_t *output, 260b0563631STom Van Eyck size_t output_size, 261b0563631STom Van Eyck size_t *output_length); 262b0563631STom Van Eyck 263b0563631STom Van Eyck /** 264b0563631STom Van Eyck * \brief Decrypt a short message with a private key. 265b0563631STom Van Eyck * 266b0563631STom Van Eyck * \param attributes The attributes for the key to import. 267b0563631STom Van Eyck * \param key_buffer Buffer where the key data is to be written. 268b0563631STom Van Eyck * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 269b0563631STom Van Eyck * \param[in] input The message to decrypt. 270b0563631STom Van Eyck * \param input_length Size of the \p input buffer in bytes. 271b0563631STom Van Eyck * \param[in] salt A salt or label, if supported by the 272b0563631STom Van Eyck * encryption algorithm. 273b0563631STom Van Eyck * If the algorithm does not support a 274b0563631STom Van Eyck * salt, pass \c NULL. 275b0563631STom Van Eyck * If the algorithm supports an optional 276b0563631STom Van Eyck * salt and you do not want to pass a salt, 277b0563631STom Van Eyck * pass \c NULL. 278b0563631STom Van Eyck * 279b0563631STom Van Eyck * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is 280b0563631STom Van Eyck * supported. 281b0563631STom Van Eyck * \param salt_length Size of the \p salt buffer in bytes. 282b0563631STom Van Eyck * If \p salt is \c NULL, pass 0. 283b0563631STom Van Eyck * \param[out] output Buffer where the decrypted message is to 284b0563631STom Van Eyck * be written. 285b0563631STom Van Eyck * \param output_size Size of the \c output buffer in bytes. 286b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes 287b0563631STom Van Eyck * that make up the returned output. 288b0563631STom Van Eyck * 289b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 290b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 291b0563631STom Van Eyck * The size of the \p output buffer is too small. You can 292b0563631STom Van Eyck * determine a sufficient buffer size by calling 293b0563631STom Van Eyck * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) 294b0563631STom Van Eyck * where \c key_type and \c key_bits are the type and bit-size 295b0563631STom Van Eyck * respectively of \p key. 296b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 297b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription 298b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 299b0563631STom Van Eyck * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription 300b0563631STom Van Eyck * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription 301b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 302b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription 303b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_PADDING \emptydescription 304b0563631STom Van Eyck * \retval #PSA_ERROR_BAD_STATE 305b0563631STom Van Eyck * The library has not been previously initialized by psa_crypto_init(). 306b0563631STom Van Eyck * It is implementation-dependent whether a failure to initialize 307b0563631STom Van Eyck * results in this error code. 308b0563631STom Van Eyck */ 309b0563631STom Van Eyck psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes, 310b0563631STom Van Eyck const uint8_t *key_buffer, 311b0563631STom Van Eyck size_t key_buffer_size, 312b0563631STom Van Eyck psa_algorithm_t alg, 313b0563631STom Van Eyck const uint8_t *input, 314b0563631STom Van Eyck size_t input_length, 315b0563631STom Van Eyck const uint8_t *salt, 316b0563631STom Van Eyck size_t salt_length, 317b0563631STom Van Eyck uint8_t *output, 318b0563631STom Van Eyck size_t output_size, 319b0563631STom Van Eyck size_t *output_length); 320b0563631STom Van Eyck 321b0563631STom Van Eyck #endif /* PSA_CRYPTO_RSA_H */ 322