1*62c56f98SSadaf Ebrahimi /* 2*62c56f98SSadaf Ebrahimi * PSA AEAD driver entry points 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_AEAD_H 10*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_AEAD_H 11*62c56f98SSadaf Ebrahimi 12*62c56f98SSadaf Ebrahimi #include <psa/crypto.h> 13*62c56f98SSadaf Ebrahimi 14*62c56f98SSadaf Ebrahimi /** 15*62c56f98SSadaf Ebrahimi * \brief Process an authenticated encryption operation. 16*62c56f98SSadaf Ebrahimi * 17*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 18*62c56f98SSadaf Ebrahimi * aead_encrypt entry point. This function behaves as an aead_encrypt 19*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 20*62c56f98SSadaf Ebrahimi * transparent drivers. 21*62c56f98SSadaf Ebrahimi * 22*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the key to use for the 23*62c56f98SSadaf Ebrahimi * operation. 24*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the key context. 25*62c56f98SSadaf Ebrahimi * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 26*62c56f98SSadaf Ebrahimi * \param alg The AEAD algorithm to compute. 27*62c56f98SSadaf Ebrahimi * \param[in] nonce Nonce or IV to use. 28*62c56f98SSadaf Ebrahimi * \param nonce_length Size of the nonce buffer in bytes. This must 29*62c56f98SSadaf Ebrahimi * be appropriate for the selected algorithm. 30*62c56f98SSadaf Ebrahimi * The default nonce size is 31*62c56f98SSadaf Ebrahimi * PSA_AEAD_NONCE_LENGTH(key_type, alg) where 32*62c56f98SSadaf Ebrahimi * key_type is the type of key. 33*62c56f98SSadaf Ebrahimi * \param[in] additional_data Additional data that will be authenticated 34*62c56f98SSadaf Ebrahimi * but not encrypted. 35*62c56f98SSadaf Ebrahimi * \param additional_data_length Size of additional_data in bytes. 36*62c56f98SSadaf Ebrahimi * \param[in] plaintext Data that will be authenticated and encrypted. 37*62c56f98SSadaf Ebrahimi * \param plaintext_length Size of plaintext in bytes. 38*62c56f98SSadaf Ebrahimi * \param[out] ciphertext Output buffer for the authenticated and 39*62c56f98SSadaf Ebrahimi * encrypted data. The additional data is not 40*62c56f98SSadaf Ebrahimi * part of this output. For algorithms where the 41*62c56f98SSadaf Ebrahimi * encrypted data and the authentication tag are 42*62c56f98SSadaf Ebrahimi * defined as separate outputs, the 43*62c56f98SSadaf Ebrahimi * authentication tag is appended to the 44*62c56f98SSadaf Ebrahimi * encrypted data. 45*62c56f98SSadaf Ebrahimi * \param ciphertext_size Size of the ciphertext buffer in bytes. This 46*62c56f98SSadaf Ebrahimi * must be appropriate for the selected algorithm 47*62c56f98SSadaf Ebrahimi * and key: 48*62c56f98SSadaf Ebrahimi * - A sufficient output size is 49*62c56f98SSadaf Ebrahimi * PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, 50*62c56f98SSadaf Ebrahimi * plaintext_length) where key_type is the type 51*62c56f98SSadaf Ebrahimi * of key. 52*62c56f98SSadaf Ebrahimi * - PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( 53*62c56f98SSadaf Ebrahimi * plaintext_length) evaluates to the maximum 54*62c56f98SSadaf Ebrahimi * ciphertext size of any supported AEAD 55*62c56f98SSadaf Ebrahimi * encryption. 56*62c56f98SSadaf Ebrahimi * \param[out] ciphertext_length On success, the size of the output in the 57*62c56f98SSadaf Ebrahimi * ciphertext buffer. 58*62c56f98SSadaf Ebrahimi * 59*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS Success. 60*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 61*62c56f98SSadaf Ebrahimi * \p alg is not supported. 62*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 63*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 64*62c56f98SSadaf Ebrahimi * ciphertext_size is too small. 65*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 66*62c56f98SSadaf Ebrahimi */ 67*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_encrypt( 68*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 69*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 70*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 71*62c56f98SSadaf Ebrahimi const uint8_t *nonce, size_t nonce_length, 72*62c56f98SSadaf Ebrahimi const uint8_t *additional_data, size_t additional_data_length, 73*62c56f98SSadaf Ebrahimi const uint8_t *plaintext, size_t plaintext_length, 74*62c56f98SSadaf Ebrahimi uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length); 75*62c56f98SSadaf Ebrahimi 76*62c56f98SSadaf Ebrahimi /** 77*62c56f98SSadaf Ebrahimi * \brief Process an authenticated decryption operation. 78*62c56f98SSadaf Ebrahimi * 79*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 80*62c56f98SSadaf Ebrahimi * aead_decrypt entry point. This function behaves as an aead_decrypt 81*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 82*62c56f98SSadaf Ebrahimi * transparent drivers. 83*62c56f98SSadaf Ebrahimi * 84*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the key to use for the 85*62c56f98SSadaf Ebrahimi * operation. 86*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the key context. 87*62c56f98SSadaf Ebrahimi * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 88*62c56f98SSadaf Ebrahimi * \param alg The AEAD algorithm to compute. 89*62c56f98SSadaf Ebrahimi * \param[in] nonce Nonce or IV to use. 90*62c56f98SSadaf Ebrahimi * \param nonce_length Size of the nonce buffer in bytes. This must 91*62c56f98SSadaf Ebrahimi * be appropriate for the selected algorithm. 92*62c56f98SSadaf Ebrahimi * The default nonce size is 93*62c56f98SSadaf Ebrahimi * PSA_AEAD_NONCE_LENGTH(key_type, alg) where 94*62c56f98SSadaf Ebrahimi * key_type is the type of key. 95*62c56f98SSadaf Ebrahimi * \param[in] additional_data Additional data that has been authenticated 96*62c56f98SSadaf Ebrahimi * but not encrypted. 97*62c56f98SSadaf Ebrahimi * \param additional_data_length Size of additional_data in bytes. 98*62c56f98SSadaf Ebrahimi * \param[in] ciphertext Data that has been authenticated and 99*62c56f98SSadaf Ebrahimi * encrypted. For algorithms where the encrypted 100*62c56f98SSadaf Ebrahimi * data and the authentication tag are defined 101*62c56f98SSadaf Ebrahimi * as separate inputs, the buffer contains 102*62c56f98SSadaf Ebrahimi * encrypted data followed by the authentication 103*62c56f98SSadaf Ebrahimi * tag. 104*62c56f98SSadaf Ebrahimi * \param ciphertext_length Size of ciphertext in bytes. 105*62c56f98SSadaf Ebrahimi * \param[out] plaintext Output buffer for the decrypted data. 106*62c56f98SSadaf Ebrahimi * \param plaintext_size Size of the plaintext buffer in bytes. This 107*62c56f98SSadaf Ebrahimi * must be appropriate for the selected algorithm 108*62c56f98SSadaf Ebrahimi * and key: 109*62c56f98SSadaf Ebrahimi * - A sufficient output size is 110*62c56f98SSadaf Ebrahimi * PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, 111*62c56f98SSadaf Ebrahimi * ciphertext_length) where key_type is the 112*62c56f98SSadaf Ebrahimi * type of key. 113*62c56f98SSadaf Ebrahimi * - PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( 114*62c56f98SSadaf Ebrahimi * ciphertext_length) evaluates to the maximum 115*62c56f98SSadaf Ebrahimi * plaintext size of any supported AEAD 116*62c56f98SSadaf Ebrahimi * decryption. 117*62c56f98SSadaf Ebrahimi * \param[out] plaintext_length On success, the size of the output in the 118*62c56f98SSadaf Ebrahimi * plaintext buffer. 119*62c56f98SSadaf Ebrahimi * 120*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS Success. 121*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_SIGNATURE 122*62c56f98SSadaf Ebrahimi * The cipher is not authentic. 123*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 124*62c56f98SSadaf Ebrahimi * \p alg is not supported. 125*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 126*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 127*62c56f98SSadaf Ebrahimi * plaintext_size is too small. 128*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 129*62c56f98SSadaf Ebrahimi */ 130*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_decrypt( 131*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 132*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, size_t key_buffer_size, 133*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 134*62c56f98SSadaf Ebrahimi const uint8_t *nonce, size_t nonce_length, 135*62c56f98SSadaf Ebrahimi const uint8_t *additional_data, size_t additional_data_length, 136*62c56f98SSadaf Ebrahimi const uint8_t *ciphertext, size_t ciphertext_length, 137*62c56f98SSadaf Ebrahimi uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length); 138*62c56f98SSadaf Ebrahimi 139*62c56f98SSadaf Ebrahimi /** Set the key for a multipart authenticated encryption operation. 140*62c56f98SSadaf Ebrahimi * 141*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 142*62c56f98SSadaf Ebrahimi * aead_encrypt_setup entry point. This function behaves as an 143*62c56f98SSadaf Ebrahimi * aead_encrypt_setup entry point as defined in the PSA driver interface 144*62c56f98SSadaf Ebrahimi * specification for transparent drivers. 145*62c56f98SSadaf Ebrahimi * 146*62c56f98SSadaf Ebrahimi * If an error occurs at any step after a call to 147*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_encrypt_setup(), the operation is reset by the PSA core by a 148*62c56f98SSadaf Ebrahimi * call to mbedtls_psa_aead_abort(). The PSA core may call 149*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort() at any time after the operation has been 150*62c56f98SSadaf Ebrahimi * initialized, and is required to when the operation is no longer needed. 151*62c56f98SSadaf Ebrahimi * 152*62c56f98SSadaf Ebrahimi * \param[in,out] operation The operation object to set up. It must have 153*62c56f98SSadaf Ebrahimi * been initialized as per the documentation for 154*62c56f98SSadaf Ebrahimi * #mbedtls_psa_aead_operation_t and not yet in 155*62c56f98SSadaf Ebrahimi * use. 156*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the key to use for the 157*62c56f98SSadaf Ebrahimi * operation. 158*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the key context. 159*62c56f98SSadaf Ebrahimi * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 160*62c56f98SSadaf Ebrahimi It must be consistent with the size in bits 161*62c56f98SSadaf Ebrahimi recorded in \p attributes. 162*62c56f98SSadaf Ebrahimi * \param alg The AEAD algorithm to compute 163*62c56f98SSadaf Ebrahimi * (\c PSA_ALG_XXX value such that 164*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(\p alg) is true). 165*62c56f98SSadaf Ebrahimi * 166*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 167*62c56f98SSadaf Ebrahimi * Success. 168*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 169*62c56f98SSadaf Ebrahimi * An invalid block length was supplied. 170*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 171*62c56f98SSadaf Ebrahimi * \p alg is not supported. 172*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY 173*62c56f98SSadaf Ebrahimi * Failed to allocate memory for key material 174*62c56f98SSadaf Ebrahimi */ 175*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_encrypt_setup( 176*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 177*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 178*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, 179*62c56f98SSadaf Ebrahimi size_t key_buffer_size, 180*62c56f98SSadaf Ebrahimi psa_algorithm_t alg); 181*62c56f98SSadaf Ebrahimi 182*62c56f98SSadaf Ebrahimi /** Set the key for a multipart authenticated decryption operation. 183*62c56f98SSadaf Ebrahimi * 184*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 185*62c56f98SSadaf Ebrahimi * aead_decrypt_setup entry point. This function behaves as an 186*62c56f98SSadaf Ebrahimi * aead_decrypt_setup entry point as defined in the PSA driver interface 187*62c56f98SSadaf Ebrahimi * specification for transparent drivers. 188*62c56f98SSadaf Ebrahimi * 189*62c56f98SSadaf Ebrahimi * If an error occurs at any step after a call to 190*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_decrypt_setup(), the PSA core resets the operation by a 191*62c56f98SSadaf Ebrahimi * call to mbedtls_psa_aead_abort(). The PSA core may call 192*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort() at any time after the operation has been 193*62c56f98SSadaf Ebrahimi * initialized, and is required to when the operation is no longer needed. 194*62c56f98SSadaf Ebrahimi * 195*62c56f98SSadaf Ebrahimi * \param[in,out] operation The operation object to set up. It must have 196*62c56f98SSadaf Ebrahimi * been initialized as per the documentation for 197*62c56f98SSadaf Ebrahimi * #mbedtls_psa_aead_operation_t and not yet in 198*62c56f98SSadaf Ebrahimi * use. 199*62c56f98SSadaf Ebrahimi * \param[in] attributes The attributes of the key to use for the 200*62c56f98SSadaf Ebrahimi * operation. 201*62c56f98SSadaf Ebrahimi * \param[in] key_buffer The buffer containing the key context. 202*62c56f98SSadaf Ebrahimi * \param key_buffer_size Size of the \p key_buffer buffer in bytes. 203*62c56f98SSadaf Ebrahimi It must be consistent with the size in bits 204*62c56f98SSadaf Ebrahimi recorded in \p attributes. 205*62c56f98SSadaf Ebrahimi * \param alg The AEAD algorithm to compute 206*62c56f98SSadaf Ebrahimi * (\c PSA_ALG_XXX value such that 207*62c56f98SSadaf Ebrahimi * #PSA_ALG_IS_AEAD(\p alg) is true). 208*62c56f98SSadaf Ebrahimi * 209*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 210*62c56f98SSadaf Ebrahimi * Success. 211*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 212*62c56f98SSadaf Ebrahimi * An invalid block length was supplied. 213*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 214*62c56f98SSadaf Ebrahimi * \p alg is not supported. 215*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY 216*62c56f98SSadaf Ebrahimi * Failed to allocate memory for key material 217*62c56f98SSadaf Ebrahimi */ 218*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_decrypt_setup( 219*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 220*62c56f98SSadaf Ebrahimi const psa_key_attributes_t *attributes, 221*62c56f98SSadaf Ebrahimi const uint8_t *key_buffer, 222*62c56f98SSadaf Ebrahimi size_t key_buffer_size, 223*62c56f98SSadaf Ebrahimi psa_algorithm_t alg); 224*62c56f98SSadaf Ebrahimi 225*62c56f98SSadaf Ebrahimi /** Set the nonce for an authenticated encryption or decryption operation. 226*62c56f98SSadaf Ebrahimi * 227*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver aead_set_nonce 228*62c56f98SSadaf Ebrahimi * entry point. This function behaves as an aead_set_nonce entry point as 229*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 230*62c56f98SSadaf Ebrahimi * drivers. 231*62c56f98SSadaf Ebrahimi * 232*62c56f98SSadaf Ebrahimi * This function sets the nonce for the authenticated 233*62c56f98SSadaf Ebrahimi * encryption or decryption operation. 234*62c56f98SSadaf Ebrahimi * 235*62c56f98SSadaf Ebrahimi * The PSA core calls mbedtls_psa_aead_encrypt_setup() or 236*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_decrypt_setup() before calling this function. 237*62c56f98SSadaf Ebrahimi * 238*62c56f98SSadaf Ebrahimi * If this function returns an error status, the PSA core will call 239*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort(). 240*62c56f98SSadaf Ebrahimi * 241*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active AEAD operation. 242*62c56f98SSadaf Ebrahimi * \param[in] nonce Buffer containing the nonce to use. 243*62c56f98SSadaf Ebrahimi * \param nonce_length Size of the nonce in bytes. 244*62c56f98SSadaf Ebrahimi * 245*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 246*62c56f98SSadaf Ebrahimi * Success. 247*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 248*62c56f98SSadaf Ebrahimi * The size of \p nonce is not acceptable for the chosen algorithm. 249*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 250*62c56f98SSadaf Ebrahimi * Algorithm previously set is not supported in this configuration of 251*62c56f98SSadaf Ebrahimi * the library. 252*62c56f98SSadaf Ebrahimi */ 253*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_set_nonce( 254*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 255*62c56f98SSadaf Ebrahimi const uint8_t *nonce, 256*62c56f98SSadaf Ebrahimi size_t nonce_length); 257*62c56f98SSadaf Ebrahimi 258*62c56f98SSadaf Ebrahimi /** Declare the lengths of the message and additional data for AEAD. 259*62c56f98SSadaf Ebrahimi * 260*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver aead_set_lengths 261*62c56f98SSadaf Ebrahimi * entry point. This function behaves as an aead_set_lengths entry point 262*62c56f98SSadaf Ebrahimi * as defined in the PSA driver interface specification for transparent 263*62c56f98SSadaf Ebrahimi * drivers. 264*62c56f98SSadaf Ebrahimi * 265*62c56f98SSadaf Ebrahimi * The PSA core calls this function before calling mbedtls_psa_aead_update_ad() 266*62c56f98SSadaf Ebrahimi * or mbedtls_psa_aead_update() if the algorithm for the operation requires it. 267*62c56f98SSadaf Ebrahimi * If the algorithm does not require it, calling this function is optional, but 268*62c56f98SSadaf Ebrahimi * if this function is called then the implementation must enforce the lengths. 269*62c56f98SSadaf Ebrahimi * 270*62c56f98SSadaf Ebrahimi * The PSA core may call this function before or after setting the nonce with 271*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_set_nonce(). 272*62c56f98SSadaf Ebrahimi * 273*62c56f98SSadaf Ebrahimi * - For #PSA_ALG_CCM, calling this function is required. 274*62c56f98SSadaf Ebrahimi * - For the other AEAD algorithms defined in this specification, calling 275*62c56f98SSadaf Ebrahimi * this function is not required. 276*62c56f98SSadaf Ebrahimi * 277*62c56f98SSadaf Ebrahimi * If this function returns an error status, the PSA core calls 278*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort(). 279*62c56f98SSadaf Ebrahimi * 280*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active AEAD operation. 281*62c56f98SSadaf Ebrahimi * \param ad_length Size of the non-encrypted additional 282*62c56f98SSadaf Ebrahimi * authenticated data in bytes. 283*62c56f98SSadaf Ebrahimi * \param plaintext_length Size of the plaintext to encrypt in bytes. 284*62c56f98SSadaf Ebrahimi * 285*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 286*62c56f98SSadaf Ebrahimi * Success. 287*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INVALID_ARGUMENT 288*62c56f98SSadaf Ebrahimi * At least one of the lengths is not acceptable for the chosen 289*62c56f98SSadaf Ebrahimi * algorithm. 290*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 291*62c56f98SSadaf Ebrahimi * Algorithm previously set is not supported in this configuration of 292*62c56f98SSadaf Ebrahimi * the library. 293*62c56f98SSadaf Ebrahimi */ 294*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_set_lengths( 295*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 296*62c56f98SSadaf Ebrahimi size_t ad_length, 297*62c56f98SSadaf Ebrahimi size_t plaintext_length); 298*62c56f98SSadaf Ebrahimi 299*62c56f98SSadaf Ebrahimi /** Pass additional data to an active AEAD operation. 300*62c56f98SSadaf Ebrahimi * 301*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 302*62c56f98SSadaf Ebrahimi * aead_update_ad entry point. This function behaves as an aead_update_ad 303*62c56f98SSadaf Ebrahimi * entry point as defined in the PSA driver interface specification for 304*62c56f98SSadaf Ebrahimi * transparent drivers. 305*62c56f98SSadaf Ebrahimi * 306*62c56f98SSadaf Ebrahimi * Additional data is authenticated, but not encrypted. 307*62c56f98SSadaf Ebrahimi * 308*62c56f98SSadaf Ebrahimi * The PSA core can call this function multiple times to pass successive 309*62c56f98SSadaf Ebrahimi * fragments of the additional data. It will not call this function after 310*62c56f98SSadaf Ebrahimi * passing data to encrypt or decrypt with mbedtls_psa_aead_update(). 311*62c56f98SSadaf Ebrahimi * 312*62c56f98SSadaf Ebrahimi * Before calling this function, the PSA core will: 313*62c56f98SSadaf Ebrahimi * 1. Call either mbedtls_psa_aead_encrypt_setup() or 314*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_decrypt_setup(). 315*62c56f98SSadaf Ebrahimi * 2. Set the nonce with mbedtls_psa_aead_set_nonce(). 316*62c56f98SSadaf Ebrahimi * 317*62c56f98SSadaf Ebrahimi * If this function returns an error status, the PSA core will call 318*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort(). 319*62c56f98SSadaf Ebrahimi * 320*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active AEAD operation. 321*62c56f98SSadaf Ebrahimi * \param[in] input Buffer containing the fragment of 322*62c56f98SSadaf Ebrahimi * additional data. 323*62c56f98SSadaf Ebrahimi * \param input_length Size of the \p input buffer in bytes. 324*62c56f98SSadaf Ebrahimi * 325*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 326*62c56f98SSadaf Ebrahimi * Success. 327*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 328*62c56f98SSadaf Ebrahimi * Algorithm previously set is not supported in this configuration of 329*62c56f98SSadaf Ebrahimi * the library. 330*62c56f98SSadaf Ebrahimi */ 331*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_update_ad( 332*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 333*62c56f98SSadaf Ebrahimi const uint8_t *input, 334*62c56f98SSadaf Ebrahimi size_t input_length); 335*62c56f98SSadaf Ebrahimi 336*62c56f98SSadaf Ebrahimi /** Encrypt or decrypt a message fragment in an active AEAD operation. 337*62c56f98SSadaf Ebrahimi * 338*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 339*62c56f98SSadaf Ebrahimi * aead_update entry point. This function behaves as an aead_update entry 340*62c56f98SSadaf Ebrahimi * point as defined in the PSA driver interface specification for 341*62c56f98SSadaf Ebrahimi * transparent drivers. 342*62c56f98SSadaf Ebrahimi * 343*62c56f98SSadaf Ebrahimi * Before calling this function, the PSA core will: 344*62c56f98SSadaf Ebrahimi * 1. Call either mbedtls_psa_aead_encrypt_setup() or 345*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_decrypt_setup(). The choice of setup function 346*62c56f98SSadaf Ebrahimi * determines whether this function encrypts or decrypts its input. 347*62c56f98SSadaf Ebrahimi * 2. Set the nonce with mbedtls_psa_aead_set_nonce(). 348*62c56f98SSadaf Ebrahimi * 3. Call mbedtls_psa_aead_update_ad() to pass all the additional data. 349*62c56f98SSadaf Ebrahimi * 350*62c56f98SSadaf Ebrahimi * If this function returns an error status, the PSA core will call 351*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_abort(). 352*62c56f98SSadaf Ebrahimi * 353*62c56f98SSadaf Ebrahimi * This function does not require the input to be aligned to any 354*62c56f98SSadaf Ebrahimi * particular block boundary. If the implementation can only process 355*62c56f98SSadaf Ebrahimi * a whole block at a time, it must consume all the input provided, but 356*62c56f98SSadaf Ebrahimi * it may delay the end of the corresponding output until a subsequent 357*62c56f98SSadaf Ebrahimi * call to mbedtls_psa_aead_update(), mbedtls_psa_aead_finish() provides 358*62c56f98SSadaf Ebrahimi * sufficient input. The amount of data that can be delayed in this way is 359*62c56f98SSadaf Ebrahimi * bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE. 360*62c56f98SSadaf Ebrahimi * 361*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active AEAD operation. 362*62c56f98SSadaf Ebrahimi * \param[in] input Buffer containing the message fragment to 363*62c56f98SSadaf Ebrahimi * encrypt or decrypt. 364*62c56f98SSadaf Ebrahimi * \param input_length Size of the \p input buffer in bytes. 365*62c56f98SSadaf Ebrahimi * \param[out] output Buffer where the output is to be written. 366*62c56f98SSadaf Ebrahimi * \param output_size Size of the \p output buffer in bytes. 367*62c56f98SSadaf Ebrahimi * This must be appropriate for the selected 368*62c56f98SSadaf Ebrahimi * algorithm and key: 369*62c56f98SSadaf Ebrahimi * - A sufficient output size is 370*62c56f98SSadaf Ebrahimi * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, 371*62c56f98SSadaf Ebrahimi * \c alg, \p input_length) where 372*62c56f98SSadaf Ebrahimi * \c key_type is the type of key and \c alg is 373*62c56f98SSadaf Ebrahimi * the algorithm that were used to set up the 374*62c56f98SSadaf Ebrahimi * operation. 375*62c56f98SSadaf Ebrahimi * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p 376*62c56f98SSadaf Ebrahimi * input_length) evaluates to the maximum 377*62c56f98SSadaf Ebrahimi * output size of any supported AEAD 378*62c56f98SSadaf Ebrahimi * algorithm. 379*62c56f98SSadaf Ebrahimi * \param[out] output_length On success, the number of bytes 380*62c56f98SSadaf Ebrahimi * that make up the returned output. 381*62c56f98SSadaf Ebrahimi * 382*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 383*62c56f98SSadaf Ebrahimi * Success. 384*62c56f98SSadaf Ebrahimi * 385*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 386*62c56f98SSadaf Ebrahimi * The size of the \p output buffer is too small. 387*62c56f98SSadaf Ebrahimi * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or 388*62c56f98SSadaf Ebrahimi * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to 389*62c56f98SSadaf Ebrahimi * determine the required buffer size. 390*62c56f98SSadaf Ebrahimi */ 391*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_update( 392*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 393*62c56f98SSadaf Ebrahimi const uint8_t *input, 394*62c56f98SSadaf Ebrahimi size_t input_length, 395*62c56f98SSadaf Ebrahimi uint8_t *output, 396*62c56f98SSadaf Ebrahimi size_t output_size, 397*62c56f98SSadaf Ebrahimi size_t *output_length); 398*62c56f98SSadaf Ebrahimi 399*62c56f98SSadaf Ebrahimi /** Finish encrypting a message in an AEAD operation. 400*62c56f98SSadaf Ebrahimi * 401*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 402*62c56f98SSadaf Ebrahimi * aead_finish entry point. This function behaves as an aead_finish entry 403*62c56f98SSadaf Ebrahimi * point as defined in the PSA driver interface specification for 404*62c56f98SSadaf Ebrahimi * transparent drivers. 405*62c56f98SSadaf Ebrahimi * 406*62c56f98SSadaf Ebrahimi * The operation must have been set up by the PSA core with 407*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_encrypt_setup(). 408*62c56f98SSadaf Ebrahimi * 409*62c56f98SSadaf Ebrahimi * This function finishes the authentication of the additional data 410*62c56f98SSadaf Ebrahimi * formed by concatenating the inputs passed to preceding calls to 411*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_update_ad() with the plaintext formed by concatenating the 412*62c56f98SSadaf Ebrahimi * inputs passed to preceding calls to mbedtls_psa_aead_update(). 413*62c56f98SSadaf Ebrahimi * 414*62c56f98SSadaf Ebrahimi * This function has two output buffers: 415*62c56f98SSadaf Ebrahimi * - \p ciphertext contains trailing ciphertext that was buffered from 416*62c56f98SSadaf Ebrahimi * preceding calls to mbedtls_psa_aead_update(). 417*62c56f98SSadaf Ebrahimi * - \p tag contains the authentication tag. 418*62c56f98SSadaf Ebrahimi * 419*62c56f98SSadaf Ebrahimi * Whether or not this function returns successfully, the PSA core subsequently 420*62c56f98SSadaf Ebrahimi * calls mbedtls_psa_aead_abort() to deactivate the operation. 421*62c56f98SSadaf Ebrahimi * 422*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active AEAD operation. 423*62c56f98SSadaf Ebrahimi * \param[out] ciphertext Buffer where the last part of the ciphertext 424*62c56f98SSadaf Ebrahimi * is to be written. 425*62c56f98SSadaf Ebrahimi * \param ciphertext_size Size of the \p ciphertext buffer in bytes. 426*62c56f98SSadaf Ebrahimi * This must be appropriate for the selected 427*62c56f98SSadaf Ebrahimi * algorithm and key: 428*62c56f98SSadaf Ebrahimi * - A sufficient output size is 429*62c56f98SSadaf Ebrahimi * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, 430*62c56f98SSadaf Ebrahimi * \c alg) where \c key_type is the type of key 431*62c56f98SSadaf Ebrahimi * and \c alg is the algorithm that were used to 432*62c56f98SSadaf Ebrahimi * set up the operation. 433*62c56f98SSadaf Ebrahimi * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to 434*62c56f98SSadaf Ebrahimi * the maximum output size of any supported AEAD 435*62c56f98SSadaf Ebrahimi * algorithm. 436*62c56f98SSadaf Ebrahimi * \param[out] ciphertext_length On success, the number of bytes of 437*62c56f98SSadaf Ebrahimi * returned ciphertext. 438*62c56f98SSadaf Ebrahimi * \param[out] tag Buffer where the authentication tag is 439*62c56f98SSadaf Ebrahimi * to be written. 440*62c56f98SSadaf Ebrahimi * \param tag_size Size of the \p tag buffer in bytes. 441*62c56f98SSadaf Ebrahimi * This must be appropriate for the selected 442*62c56f98SSadaf Ebrahimi * algorithm and key: 443*62c56f98SSadaf Ebrahimi * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c 444*62c56f98SSadaf Ebrahimi * key_type, \c key_bits, \c alg) where 445*62c56f98SSadaf Ebrahimi * \c key_type and \c key_bits are the type and 446*62c56f98SSadaf Ebrahimi * bit-size of the key, and \c alg are the 447*62c56f98SSadaf Ebrahimi * algorithm that were used in the call to 448*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_encrypt_setup(). 449*62c56f98SSadaf Ebrahimi * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the 450*62c56f98SSadaf Ebrahimi * maximum tag size of any supported AEAD 451*62c56f98SSadaf Ebrahimi * algorithm. 452*62c56f98SSadaf Ebrahimi * \param[out] tag_length On success, the number of bytes 453*62c56f98SSadaf Ebrahimi * that make up the returned tag. 454*62c56f98SSadaf Ebrahimi * 455*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 456*62c56f98SSadaf Ebrahimi * Success. 457*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 458*62c56f98SSadaf Ebrahimi * The size of the \p tag buffer is too small. 459*62c56f98SSadaf Ebrahimi * #PSA_AEAD_TAG_LENGTH(\c key_type, key_bits, \c alg) or 460*62c56f98SSadaf Ebrahimi * #PSA_AEAD_TAG_MAX_SIZE can be used to determine the required \p tag 461*62c56f98SSadaf Ebrahimi * buffer size. 462*62c56f98SSadaf Ebrahimi */ 463*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_finish( 464*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation, 465*62c56f98SSadaf Ebrahimi uint8_t *ciphertext, 466*62c56f98SSadaf Ebrahimi size_t ciphertext_size, 467*62c56f98SSadaf Ebrahimi size_t *ciphertext_length, 468*62c56f98SSadaf Ebrahimi uint8_t *tag, 469*62c56f98SSadaf Ebrahimi size_t tag_size, 470*62c56f98SSadaf Ebrahimi size_t *tag_length); 471*62c56f98SSadaf Ebrahimi 472*62c56f98SSadaf Ebrahimi /** Abort an AEAD operation. 473*62c56f98SSadaf Ebrahimi * 474*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver 475*62c56f98SSadaf Ebrahimi * aead_abort entry point. This function behaves as an aead_abort entry 476*62c56f98SSadaf Ebrahimi * point as defined in the PSA driver interface specification for 477*62c56f98SSadaf Ebrahimi * transparent drivers. 478*62c56f98SSadaf Ebrahimi * 479*62c56f98SSadaf Ebrahimi * Aborting an operation frees all associated resources except for the 480*62c56f98SSadaf Ebrahimi * \p operation structure itself. Once aborted, the operation object 481*62c56f98SSadaf Ebrahimi * can be reused for another operation by the PSA core by it calling 482*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup() again. 483*62c56f98SSadaf Ebrahimi * 484*62c56f98SSadaf Ebrahimi * The PSA core may call this function any time after the operation object has 485*62c56f98SSadaf Ebrahimi * been initialized as described in #mbedtls_psa_aead_operation_t. 486*62c56f98SSadaf Ebrahimi * 487*62c56f98SSadaf Ebrahimi * In particular, calling mbedtls_psa_aead_abort() after the operation has been 488*62c56f98SSadaf Ebrahimi * terminated by a call to mbedtls_psa_aead_abort() or 489*62c56f98SSadaf Ebrahimi * mbedtls_psa_aead_finish() is safe and has no effect. 490*62c56f98SSadaf Ebrahimi * 491*62c56f98SSadaf Ebrahimi * \param[in,out] operation Initialized AEAD operation. 492*62c56f98SSadaf Ebrahimi * 493*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 494*62c56f98SSadaf Ebrahimi * Success. 495*62c56f98SSadaf Ebrahimi */ 496*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_aead_abort( 497*62c56f98SSadaf Ebrahimi mbedtls_psa_aead_operation_t *operation); 498*62c56f98SSadaf Ebrahimi 499*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_AEAD_H */ 500