1*b0563631STom Van Eyck /* 2*b0563631STom Van Eyck * PSA cipher driver entry points and associated auxiliary functions 3*b0563631STom Van Eyck */ 4*b0563631STom Van Eyck /* 5*b0563631STom Van Eyck * Copyright The Mbed TLS Contributors 6*b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7*b0563631STom Van Eyck */ 8*b0563631STom Van Eyck 9*b0563631STom Van Eyck #ifndef PSA_CRYPTO_CIPHER_H 10*b0563631STom Van Eyck #define PSA_CRYPTO_CIPHER_H 11*b0563631STom Van Eyck 12*b0563631STom Van Eyck #include <mbedtls/cipher.h> 13*b0563631STom Van Eyck #include <psa/crypto.h> 14*b0563631STom Van Eyck 15*b0563631STom Van Eyck /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier 16*b0563631STom Van Eyck * as well as the PSA type and size of the key to be used with the cipher 17*b0563631STom Van Eyck * algorithm. 18*b0563631STom Van Eyck * 19*b0563631STom Van Eyck * \param[in] alg PSA cipher algorithm identifier 20*b0563631STom Van Eyck * \param[in] key_type PSA key type 21*b0563631STom Van Eyck * \param[in,out] key_bits Size of the key in bits. The value provided in input 22*b0563631STom Van Eyck * might be updated if necessary. 23*b0563631STom Van Eyck * \param[out] mode Mbed TLS cipher mode 24*b0563631STom Van Eyck * \param[out] cipher_id Mbed TLS cipher algorithm identifier 25*b0563631STom Van Eyck * 26*b0563631STom Van Eyck * \return On success \c PSA_SUCCESS is returned and key_bits, mode and cipher_id 27*b0563631STom Van Eyck * are properly updated. 28*b0563631STom Van Eyck * \c PSA_ERROR_NOT_SUPPORTED is returned if the cipher algorithm is not 29*b0563631STom Van Eyck * supported. 30*b0563631STom Van Eyck */ 31*b0563631STom Van Eyck 32*b0563631STom Van Eyck psa_status_t mbedtls_cipher_values_from_psa(psa_algorithm_t alg, psa_key_type_t key_type, 33*b0563631STom Van Eyck size_t *key_bits, mbedtls_cipher_mode_t *mode, 34*b0563631STom Van Eyck mbedtls_cipher_id_t *cipher_id); 35*b0563631STom Van Eyck 36*b0563631STom Van Eyck #if defined(MBEDTLS_CIPHER_C) 37*b0563631STom Van Eyck /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier 38*b0563631STom Van Eyck * as well as the PSA type and size of the key to be used with the cipher 39*b0563631STom Van Eyck * algorithm. 40*b0563631STom Van Eyck * 41*b0563631STom Van Eyck * \param alg PSA cipher algorithm identifier 42*b0563631STom Van Eyck * \param key_type PSA key type 43*b0563631STom Van Eyck * \param key_bits Size of the key in bits 44*b0563631STom Van Eyck * \param[out] cipher_id Mbed TLS cipher algorithm identifier 45*b0563631STom Van Eyck * 46*b0563631STom Van Eyck * \return The Mbed TLS cipher information of the cipher algorithm. 47*b0563631STom Van Eyck * \c NULL if the PSA cipher algorithm is not supported. 48*b0563631STom Van Eyck */ 49*b0563631STom Van Eyck const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( 50*b0563631STom Van Eyck psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits, 51*b0563631STom Van Eyck mbedtls_cipher_id_t *cipher_id); 52*b0563631STom Van Eyck #endif /* MBEDTLS_CIPHER_C */ 53*b0563631STom Van Eyck 54*b0563631STom Van Eyck /** 55*b0563631STom Van Eyck * \brief Set the key for a multipart symmetric encryption operation. 56*b0563631STom Van Eyck * 57*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 58*b0563631STom Van Eyck * cipher_encrypt_setup entry point. This function behaves as a 59*b0563631STom Van Eyck * cipher_encrypt_setup entry point as defined in the PSA driver 60*b0563631STom Van Eyck * interface specification for transparent drivers. 61*b0563631STom Van Eyck * 62*b0563631STom Van Eyck * \param[in,out] operation The operation object to set up. It has been 63*b0563631STom Van Eyck * initialized as per the documentation for 64*b0563631STom Van Eyck * #psa_cipher_operation_t and not yet in use. 65*b0563631STom Van Eyck * \param[in] attributes The attributes of the key to use for the 66*b0563631STom Van Eyck * operation. 67*b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the key context. 68*b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 69*b0563631STom Van Eyck * \param[in] alg The cipher algorithm to compute 70*b0563631STom Van Eyck * (\c PSA_ALG_XXX value such that 71*b0563631STom Van Eyck * #PSA_ALG_IS_CIPHER(\p alg) is true). 72*b0563631STom Van Eyck * 73*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 74*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 75*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 76*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 77*b0563631STom Van Eyck */ 78*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_encrypt_setup( 79*b0563631STom Van Eyck mbedtls_psa_cipher_operation_t *operation, 80*b0563631STom Van Eyck const psa_key_attributes_t *attributes, 81*b0563631STom Van Eyck const uint8_t *key_buffer, size_t key_buffer_size, 82*b0563631STom Van Eyck psa_algorithm_t alg); 83*b0563631STom Van Eyck 84*b0563631STom Van Eyck /** 85*b0563631STom Van Eyck * \brief Set the key for a multipart symmetric decryption operation. 86*b0563631STom Van Eyck * 87*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 88*b0563631STom Van Eyck * cipher_decrypt_setup entry point. This function behaves as a 89*b0563631STom Van Eyck * cipher_decrypt_setup entry point as defined in the PSA driver 90*b0563631STom Van Eyck * interface specification for transparent drivers. 91*b0563631STom Van Eyck * 92*b0563631STom Van Eyck * \param[in,out] operation The operation object to set up. It has been 93*b0563631STom Van Eyck * initialized as per the documentation for 94*b0563631STom Van Eyck * #psa_cipher_operation_t and not yet in use. 95*b0563631STom Van Eyck * \param[in] attributes The attributes of the key to use for the 96*b0563631STom Van Eyck * operation. 97*b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the key context. 98*b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 99*b0563631STom Van Eyck * \param[in] alg The cipher algorithm to compute 100*b0563631STom Van Eyck * (\c PSA_ALG_XXX value such that 101*b0563631STom Van Eyck * #PSA_ALG_IS_CIPHER(\p alg) is true). 102*b0563631STom Van Eyck * 103*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 104*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 105*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 106*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 107*b0563631STom Van Eyck */ 108*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_decrypt_setup( 109*b0563631STom Van Eyck mbedtls_psa_cipher_operation_t *operation, 110*b0563631STom Van Eyck const psa_key_attributes_t *attributes, 111*b0563631STom Van Eyck const uint8_t *key_buffer, size_t key_buffer_size, 112*b0563631STom Van Eyck psa_algorithm_t alg); 113*b0563631STom Van Eyck 114*b0563631STom Van Eyck /** Set the IV for a symmetric encryption or decryption operation. 115*b0563631STom Van Eyck * 116*b0563631STom Van Eyck * This function sets the IV (initialization vector), nonce 117*b0563631STom Van Eyck * or initial counter value for the encryption or decryption operation. 118*b0563631STom Van Eyck * 119*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 120*b0563631STom Van Eyck * cipher_set_iv entry point. This function behaves as a 121*b0563631STom Van Eyck * cipher_set_iv entry point as defined in the PSA driver 122*b0563631STom Van Eyck * interface specification for transparent drivers. 123*b0563631STom Van Eyck * 124*b0563631STom Van Eyck * \param[in,out] operation Active cipher operation. 125*b0563631STom Van Eyck * \param[in] iv Buffer containing the IV to use. 126*b0563631STom Van Eyck * \param[in] iv_length Size of the IV in bytes. It is guaranteed by 127*b0563631STom Van Eyck * the core to be less or equal to 128*b0563631STom Van Eyck * PSA_CIPHER_IV_MAX_SIZE. 129*b0563631STom Van Eyck * 130*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 131*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 132*b0563631STom Van Eyck * The size of \p iv is not acceptable for the chosen algorithm, 133*b0563631STom Van Eyck * or the chosen algorithm does not use an IV. 134*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 135*b0563631STom Van Eyck */ 136*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_set_iv( 137*b0563631STom Van Eyck mbedtls_psa_cipher_operation_t *operation, 138*b0563631STom Van Eyck const uint8_t *iv, size_t iv_length); 139*b0563631STom Van Eyck 140*b0563631STom Van Eyck /** Encrypt or decrypt a message fragment in an active cipher operation. 141*b0563631STom Van Eyck * 142*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 143*b0563631STom Van Eyck * cipher_update entry point. This function behaves as a 144*b0563631STom Van Eyck * cipher_update entry point as defined in the PSA driver 145*b0563631STom Van Eyck * interface specification for transparent drivers. 146*b0563631STom Van Eyck * 147*b0563631STom Van Eyck * \param[in,out] operation Active cipher operation. 148*b0563631STom Van Eyck * \param[in] input Buffer containing the message fragment to 149*b0563631STom Van Eyck * encrypt or decrypt. 150*b0563631STom Van Eyck * \param[in] input_length Size of the \p input buffer in bytes. 151*b0563631STom Van Eyck * \param[out] output Buffer where the output is to be written. 152*b0563631STom Van Eyck * \param[in] output_size Size of the \p output buffer in bytes. 153*b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes 154*b0563631STom Van Eyck * that make up the returned output. 155*b0563631STom Van Eyck * 156*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 157*b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 158*b0563631STom Van Eyck * The size of the \p output buffer is too small. 159*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 160*b0563631STom Van Eyck */ 161*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_update( 162*b0563631STom Van Eyck mbedtls_psa_cipher_operation_t *operation, 163*b0563631STom Van Eyck const uint8_t *input, size_t input_length, 164*b0563631STom Van Eyck uint8_t *output, size_t output_size, size_t *output_length); 165*b0563631STom Van Eyck 166*b0563631STom Van Eyck /** Finish encrypting or decrypting a message in a cipher operation. 167*b0563631STom Van Eyck * 168*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 169*b0563631STom Van Eyck * cipher_finish entry point. This function behaves as a 170*b0563631STom Van Eyck * cipher_finish entry point as defined in the PSA driver 171*b0563631STom Van Eyck * interface specification for transparent drivers. 172*b0563631STom Van Eyck * 173*b0563631STom Van Eyck * \param[in,out] operation Active cipher operation. 174*b0563631STom Van Eyck * \param[out] output Buffer where the output is to be written. 175*b0563631STom Van Eyck * \param[in] output_size Size of the \p output buffer in bytes. 176*b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes 177*b0563631STom Van Eyck * that make up the returned output. 178*b0563631STom Van Eyck * 179*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 180*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 181*b0563631STom Van Eyck * The total input size passed to this operation is not valid for 182*b0563631STom Van Eyck * this particular algorithm. For example, the algorithm is a based 183*b0563631STom Van Eyck * on block cipher and requires a whole number of blocks, but the 184*b0563631STom Van Eyck * total input size is not a multiple of the block size. 185*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_PADDING 186*b0563631STom Van Eyck * This is a decryption operation for an algorithm that includes 187*b0563631STom Van Eyck * padding, and the ciphertext does not contain valid padding. 188*b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 189*b0563631STom Van Eyck * The size of the \p output buffer is too small. 190*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 191*b0563631STom Van Eyck */ 192*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_finish( 193*b0563631STom Van Eyck mbedtls_psa_cipher_operation_t *operation, 194*b0563631STom Van Eyck uint8_t *output, size_t output_size, size_t *output_length); 195*b0563631STom Van Eyck 196*b0563631STom Van Eyck /** Abort a cipher operation. 197*b0563631STom Van Eyck * 198*b0563631STom Van Eyck * Aborting an operation frees all associated resources except for the 199*b0563631STom Van Eyck * \p operation structure itself. Once aborted, the operation object 200*b0563631STom Van Eyck * can be reused for another operation. 201*b0563631STom Van Eyck * 202*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 203*b0563631STom Van Eyck * cipher_abort entry point. This function behaves as a 204*b0563631STom Van Eyck * cipher_abort entry point as defined in the PSA driver 205*b0563631STom Van Eyck * interface specification for transparent drivers. 206*b0563631STom Van Eyck * 207*b0563631STom Van Eyck * \param[in,out] operation Initialized cipher operation. 208*b0563631STom Van Eyck * 209*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 210*b0563631STom Van Eyck */ 211*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation); 212*b0563631STom Van Eyck 213*b0563631STom Van Eyck /** Encrypt a message using a symmetric cipher. 214*b0563631STom Van Eyck * 215*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 216*b0563631STom Van Eyck * cipher_encrypt entry point. This function behaves as a 217*b0563631STom Van Eyck * cipher_encrypt entry point as defined in the PSA driver 218*b0563631STom Van Eyck * interface specification for transparent drivers. 219*b0563631STom Van Eyck * 220*b0563631STom Van Eyck * \param[in] attributes The attributes of the key to use for the 221*b0563631STom Van Eyck * operation. 222*b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the key context. 223*b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 224*b0563631STom Van Eyck * \param[in] alg The cipher algorithm to compute 225*b0563631STom Van Eyck * (\c PSA_ALG_XXX value such that 226*b0563631STom Van Eyck * #PSA_ALG_IS_CIPHER(\p alg) is true). 227*b0563631STom Van Eyck * \param[in] iv Buffer containing the IV for encryption. The 228*b0563631STom Van Eyck * IV has been generated by the core. 229*b0563631STom Van Eyck * \param[in] iv_length Size of the \p iv in bytes. 230*b0563631STom Van Eyck * \param[in] input Buffer containing the message to encrypt. 231*b0563631STom Van Eyck * \param[in] input_length Size of the \p input buffer in bytes. 232*b0563631STom Van Eyck * \param[in,out] output Buffer where the output is to be written. 233*b0563631STom Van Eyck * \param[in] output_size Size of the \p output buffer in bytes. 234*b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes that make up 235*b0563631STom Van Eyck * the returned output. Initialized to zero 236*b0563631STom Van Eyck * by the core. 237*b0563631STom Van Eyck * 238*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 239*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 240*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 241*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 242*b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 243*b0563631STom Van Eyck * The size of the \p output buffer is too small. 244*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 245*b0563631STom Van Eyck * The size \p iv_length is not acceptable for the chosen algorithm, 246*b0563631STom Van Eyck * or the chosen algorithm does not use an IV. 247*b0563631STom Van Eyck * The total input size passed to this operation is not valid for 248*b0563631STom Van Eyck * this particular algorithm. For example, the algorithm is a based 249*b0563631STom Van Eyck * on block cipher and requires a whole number of blocks, but the 250*b0563631STom Van Eyck * total input size is not a multiple of the block size. 251*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_PADDING 252*b0563631STom Van Eyck * This is a decryption operation for an algorithm that includes 253*b0563631STom Van Eyck * padding, and the ciphertext does not contain valid padding. 254*b0563631STom Van Eyck */ 255*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes, 256*b0563631STom Van Eyck const uint8_t *key_buffer, 257*b0563631STom Van Eyck size_t key_buffer_size, 258*b0563631STom Van Eyck psa_algorithm_t alg, 259*b0563631STom Van Eyck const uint8_t *iv, 260*b0563631STom Van Eyck size_t iv_length, 261*b0563631STom Van Eyck const uint8_t *input, 262*b0563631STom Van Eyck size_t input_length, 263*b0563631STom Van Eyck uint8_t *output, 264*b0563631STom Van Eyck size_t output_size, 265*b0563631STom Van Eyck size_t *output_length); 266*b0563631STom Van Eyck 267*b0563631STom Van Eyck /** Decrypt a message using a symmetric cipher. 268*b0563631STom Van Eyck * 269*b0563631STom Van Eyck * \note The signature of this function is that of a PSA driver 270*b0563631STom Van Eyck * cipher_decrypt entry point. This function behaves as a 271*b0563631STom Van Eyck * cipher_decrypt entry point as defined in the PSA driver 272*b0563631STom Van Eyck * interface specification for transparent drivers. 273*b0563631STom Van Eyck * 274*b0563631STom Van Eyck * \param[in] attributes The attributes of the key to use for the 275*b0563631STom Van Eyck * operation. 276*b0563631STom Van Eyck * \param[in] key_buffer The buffer containing the key context. 277*b0563631STom Van Eyck * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. 278*b0563631STom Van Eyck * \param[in] alg The cipher algorithm to compute 279*b0563631STom Van Eyck * (\c PSA_ALG_XXX value such that 280*b0563631STom Van Eyck * #PSA_ALG_IS_CIPHER(\p alg) is true). 281*b0563631STom Van Eyck * \param[in] input Buffer containing the iv and the ciphertext. 282*b0563631STom Van Eyck * \param[in] input_length Size of the \p input buffer in bytes. 283*b0563631STom Van Eyck * \param[out] output Buffer where the output is to be written. 284*b0563631STom Van Eyck * \param[in] output_size Size of the \p output buffer in bytes. 285*b0563631STom Van Eyck * \param[out] output_length On success, the number of bytes that make up 286*b0563631STom Van Eyck * the returned output. Initialized to zero 287*b0563631STom Van Eyck * by the core. 288*b0563631STom Van Eyck * 289*b0563631STom Van Eyck * \retval #PSA_SUCCESS \emptydescription 290*b0563631STom Van Eyck * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription 291*b0563631STom Van Eyck * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 292*b0563631STom Van Eyck * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 293*b0563631STom Van Eyck * \retval #PSA_ERROR_BUFFER_TOO_SMALL 294*b0563631STom Van Eyck * The size of the \p output buffer is too small. 295*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_ARGUMENT 296*b0563631STom Van Eyck * The size of \p iv is not acceptable for the chosen algorithm, 297*b0563631STom Van Eyck * or the chosen algorithm does not use an IV. 298*b0563631STom Van Eyck * The total input size passed to this operation is not valid for 299*b0563631STom Van Eyck * this particular algorithm. For example, the algorithm is a based 300*b0563631STom Van Eyck * on block cipher and requires a whole number of blocks, but the 301*b0563631STom Van Eyck * total input size is not a multiple of the block size. 302*b0563631STom Van Eyck * \retval #PSA_ERROR_INVALID_PADDING 303*b0563631STom Van Eyck * This is a decryption operation for an algorithm that includes 304*b0563631STom Van Eyck * padding, and the ciphertext does not contain valid padding. 305*b0563631STom Van Eyck */ 306*b0563631STom Van Eyck psa_status_t mbedtls_psa_cipher_decrypt(const psa_key_attributes_t *attributes, 307*b0563631STom Van Eyck const uint8_t *key_buffer, 308*b0563631STom Van Eyck size_t key_buffer_size, 309*b0563631STom Van Eyck psa_algorithm_t alg, 310*b0563631STom Van Eyck const uint8_t *input, 311*b0563631STom Van Eyck size_t input_length, 312*b0563631STom Van Eyck uint8_t *output, 313*b0563631STom Van Eyck size_t output_size, 314*b0563631STom Van Eyck size_t *output_length); 315*b0563631STom Van Eyck 316*b0563631STom Van Eyck #endif /* PSA_CRYPTO_CIPHER_H */ 317