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