blob: cf6230149df4919b8ca082d692acc7dfd86e6eca [file] [log] [blame]
Ronald Cron7ceee8d2021-03-17 16:55:43 +01001/*
2 * PSA AEAD driver entry points
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_AEAD_H
22#define PSA_CRYPTO_AEAD_H
23
24#include <psa/crypto.h>
25
Ronald Cron46f91782021-03-17 08:16:34 +010026/**
27 * \brief Process an authenticated encryption operation.
28 *
29 * \note The signature of this function is that of a PSA driver
30 * aead_encrypt entry point. This function behaves as an aead_encrypt
31 * entry point as defined in the PSA driver interface specification for
32 * transparent drivers.
33 *
34 * \param[in] attributes The attributes of the key to use for the
35 * operation.
36 * \param[in] key_buffer The buffer containing the key context.
37 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
38 * \param alg The AEAD algorithm to compute.
39 * \param[in] nonce Nonce or IV to use.
40 * \param nonce_length Size of the nonce buffer in bytes. This must
41 * be appropriate for the selected algorithm.
42 * The default nonce size is
43 * PSA_AEAD_NONCE_LENGTH(key_type, alg) where
44 * key_type is the type of key.
45 * \param[in] additional_data Additional data that will be authenticated
46 * but not encrypted.
47 * \param additional_data_length Size of additional_data in bytes.
48 * \param[in] plaintext Data that will be authenticated and encrypted.
49 * \param plaintext_length Size of plaintext in bytes.
50 * \param[out] ciphertext Output buffer for the authenticated and
51 * encrypted data. The additional data is not
52 * part of this output. For algorithms where the
53 * encrypted data and the authentication tag are
54 * defined as separate outputs, the
55 * authentication tag is appended to the
56 * encrypted data.
57 * \param ciphertext_size Size of the ciphertext buffer in bytes. This
58 * must be appropriate for the selected algorithm
59 * and key:
60 * - A sufficient output size is
61 * PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
62 * plaintext_length) where key_type is the type
63 * of key.
64 * - PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(
65 * plaintext_length) evaluates to the maximum
66 * ciphertext size of any supported AEAD
67 * encryption.
68 * \param[out] ciphertext_length On success, the size of the output in the
69 * ciphertext buffer.
70 *
71 * \retval #PSA_SUCCESS Success.
72 * \retval #PSA_ERROR_NOT_SUPPORTED
73 * \p alg is not supported.
74 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
75 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
76 * ciphertext_size is too small.
77 * \retval #PSA_ERROR_CORRUPTION_DETECTED
78 */
79psa_status_t mbedtls_psa_aead_encrypt(
80 const psa_key_attributes_t *attributes,
81 const uint8_t *key_buffer, size_t key_buffer_size,
82 psa_algorithm_t alg,
83 const uint8_t *nonce, size_t nonce_length,
84 const uint8_t *additional_data, size_t additional_data_length,
85 const uint8_t *plaintext, size_t plaintext_length,
86 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length );
87
88/**
89 * \brief Process an authenticated decryption operation.
90 *
91 * \note The signature of this function is that of a PSA driver
92 * aead_decrypt entry point. This function behaves as an aead_decrypt
93 * entry point as defined in the PSA driver interface specification for
94 * transparent drivers.
95 *
96 * \param[in] attributes The attributes of the key to use for the
97 * operation.
98 * \param[in] key_buffer The buffer containing the key context.
99 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
100 * \param alg The AEAD algorithm to compute.
101 * \param[in] nonce Nonce or IV to use.
102 * \param nonce_length Size of the nonce buffer in bytes. This must
103 * be appropriate for the selected algorithm.
104 * The default nonce size is
105 * PSA_AEAD_NONCE_LENGTH(key_type, alg) where
106 * key_type is the type of key.
107 * \param[in] additional_data Additional data that has been authenticated
108 * but not encrypted.
109 * \param additional_data_length Size of additional_data in bytes.
110 * \param[in] ciphertext Data that has been authenticated and
111 * encrypted. For algorithms where the encrypted
112 * data and the authentication tag are defined
113 * as separate inputs, the buffer contains
114 * encrypted data followed by the authentication
115 * tag.
116 * \param ciphertext_length Size of ciphertext in bytes.
117 * \param[out] plaintext Output buffer for the decrypted data.
118 * \param plaintext_size Size of the plaintext buffer in bytes. This
119 * must be appropriate for the selected algorithm
120 * and key:
121 * - A sufficient output size is
122 * PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
123 * ciphertext_length) where key_type is the
124 * type of key.
125 * - PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(
126 * ciphertext_length) evaluates to the maximum
127 * plaintext size of any supported AEAD
128 * decryption.
129 * \param[out] plaintext_length On success, the size of the output in the
130 * plaintext buffer.
131 *
132 * \retval #PSA_SUCCESS Success.
133 * \retval #PSA_ERROR_INVALID_SIGNATURE
134 * The cipher is not authentic.
135 * \retval #PSA_ERROR_NOT_SUPPORTED
136 * \p alg is not supported.
137 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
138 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
139 * plaintext_size is too small.
140 * \retval #PSA_ERROR_CORRUPTION_DETECTED
141 */
142psa_status_t mbedtls_psa_aead_decrypt(
143 const psa_key_attributes_t *attributes,
144 const uint8_t *key_buffer, size_t key_buffer_size,
145 psa_algorithm_t alg,
146 const uint8_t *nonce, size_t nonce_length,
147 const uint8_t *additional_data, size_t additional_data_length,
148 const uint8_t *ciphertext, size_t ciphertext_length,
149 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length );
150
Paul Elliottadb8b162021-04-20 16:06:57 +0100151/** Set the key for a multipart authenticated encryption operation.
152 *
153 * \note The signature of this function is that of a PSA driver
154 * aead_encrypt_setup entry point. This function behaves as an
155 * aead_encrypt_setup entry point as defined in the PSA driver interface
156 * specification for transparent drivers.
157 *
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100158 * If an error occurs at any step after a call to
Paul Elliott4148a682021-05-14 17:26:56 +0100159 * mbedtls_psa_aead_encrypt_setup(), the operation is reset by the PSA core by a
160 * call to mbedtls_psa_aead_abort(). The PSA core may call
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100161 * mbedtls_psa_aead_abort() at any time after the operation has been
162 * initialized.
Paul Elliottadb8b162021-04-20 16:06:57 +0100163 *
Paul Elliott4148a682021-05-14 17:26:56 +0100164 * After a successful call to mbedtls_psa_aead_encrypt_setup(), the PSA core
165 * eventually terminates the operation by calling mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100166 *
167 * \param[in,out] operation The operation object to set up. It must have
168 * been initialized as per the documentation for
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100169 * #mbedtls_psa_aead_operation_t and not yet in
170 * use.
Paul Elliottadb8b162021-04-20 16:06:57 +0100171 * \param[in] attributes The attributes of the key to use for the
172 * operation.
173 * \param[in] key_buffer The buffer containing the key context.
174 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
175 * \param alg The AEAD algorithm to compute
176 * (\c PSA_ALG_XXX value such that
177 * #PSA_ALG_IS_AEAD(\p alg) is true).
178 *
179 * \retval #PSA_SUCCESS
180 * Success.
Paul Elliottadb8b162021-04-20 16:06:57 +0100181 * \retval #PSA_ERROR_INVALID_ARGUMENT
182 * \p key is not compatible with \p alg.
183 * \retval #PSA_ERROR_NOT_SUPPORTED
184 * \p alg is not supported or is not an AEAD algorithm.
185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Paul Elliottb91f3312021-05-19 12:30:15 +0100186 * Failed to allocate memory for key material
Paul Elliottadb8b162021-04-20 16:06:57 +0100187 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100188psa_status_t mbedtls_psa_aead_encrypt_setup(mbedtls_psa_aead_operation_t
189 *operation,
190 const psa_key_attributes_t
191 *attributes,
192 const uint8_t *key_buffer,
193 size_t key_buffer_size,
Paul Elliottadb8b162021-04-20 16:06:57 +0100194 psa_algorithm_t alg);
195
196/** Set the key for a multipart authenticated decryption operation.
197 *
198 * \note The signature of this function is that of a PSA driver
199 * aead_decrypt_setup entry point. This function behaves as an
200 * aead_decrypt_setup entry point as defined in the PSA driver interface
201 * specification for transparent drivers.
202 *
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100203 * If an error occurs at any step after a call to
Paul Elliott4148a682021-05-14 17:26:56 +0100204 * mbedtls_psa_aead_decrypt_setup(), the PSA core resets the operation by a
205 * call to mbedtls_psa_aead_abort(). The PSA core may call
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100206 * mbedtls_psa_aead_abort() at any time after the operation has been
207 * initialized.
Paul Elliottadb8b162021-04-20 16:06:57 +0100208 *
Paul Elliott4148a682021-05-14 17:26:56 +0100209 * After a successful call to mbedtls_psa_aead_decrypt_setup(), the PSA core
210 * eventually terminates the operation by a call to mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100211 *
212 * \param[in,out] operation The operation object to set up. It must have
213 * been initialized as per the documentation for
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100214 * #mbedtls_psa_aead_operation_t and not yet in
215 * use.
Paul Elliottadb8b162021-04-20 16:06:57 +0100216 * \param[in] attributes The attributes of the key to use for the
217 * operation.
218 * \param[in] key_buffer The buffer containing the key context.
219 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
220 * \param alg The AEAD algorithm to compute
221 * (\c PSA_ALG_XXX value such that
222 * #PSA_ALG_IS_AEAD(\p alg) is true).
223 *
224 * \retval #PSA_SUCCESS
225 * Success.
Paul Elliottb91f3312021-05-19 12:30:15 +0100226 * * \retval #PSA_ERROR_INVALID_ARGUMENT
Paul Elliottadb8b162021-04-20 16:06:57 +0100227 * \p key is not compatible with \p alg.
228 * \retval #PSA_ERROR_NOT_SUPPORTED
229 * \p alg is not supported or is not an AEAD algorithm.
230 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Paul Elliottb91f3312021-05-19 12:30:15 +0100231 * Failed to allocate memory for key material
Paul Elliottadb8b162021-04-20 16:06:57 +0100232 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100233psa_status_t mbedtls_psa_aead_decrypt_setup(mbedtls_psa_aead_operation_t
234 *operation,
235 const psa_key_attributes_t
236 *attributes,
237 const uint8_t *key_buffer,
238 size_t key_buffer_size,
Paul Elliottadb8b162021-04-20 16:06:57 +0100239 psa_algorithm_t alg);
240
Paul Elliottadb8b162021-04-20 16:06:57 +0100241/** Set the nonce for an authenticated encryption or decryption operation.
242 *
Paul Elliott4148a682021-05-14 17:26:56 +0100243 * \note The signature of this function is that of a PSA driver aead_set_nonce
244 * entry point. This function behaves as an aead_set_nonce entry point as
245 * defined in the PSA driver interface specification for transparent
246 * drivers.
Paul Elliottadb8b162021-04-20 16:06:57 +0100247 *
248 * This function sets the nonce for the authenticated
249 * encryption or decryption operation.
250 *
Paul Elliott4148a682021-05-14 17:26:56 +0100251 * The PSA core calls mbedtls_psa_aead_encrypt_setup() or
Paul Elliottadb8b162021-04-20 16:06:57 +0100252 * mbedtls_psa_aead_decrypt_setup() before calling this function.
253 *
Paul Elliott498d3502021-05-17 18:16:20 +0100254 * If this function returns an error status, the PSA core will call
Paul Elliott4148a682021-05-14 17:26:56 +0100255 * mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100256 *
257 * \param[in,out] operation Active AEAD operation.
258 * \param[in] nonce Buffer containing the nonce to use.
259 * \param nonce_length Size of the nonce in bytes.
260 *
261 * \retval #PSA_SUCCESS
262 * Success.
Paul Elliottadb8b162021-04-20 16:06:57 +0100263 * \retval #PSA_ERROR_INVALID_ARGUMENT
264 * The size of \p nonce is not acceptable for the chosen algorithm.
Paul Elliottb91f3312021-05-19 12:30:15 +0100265 * \retval #PSA_ERROR_NOT_SUPPORTED
266 * Algorithm previously set is not supported in this configuration of
267 * the library.
Paul Elliottadb8b162021-04-20 16:06:57 +0100268 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100269psa_status_t mbedtls_psa_aead_set_nonce(mbedtls_psa_aead_operation_t *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100270 const uint8_t *nonce,
271 size_t nonce_length);
272
273/** Declare the lengths of the message and additional data for AEAD.
274 *
Paul Elliott4148a682021-05-14 17:26:56 +0100275 * \note The signature of this function is that of a PSA driver aead_set_lengths
276 * entry point. This function behaves as an aead_set_lengths entry point
277 * as defined in the PSA driver interface specification for transparent
278 * drivers.
Paul Elliottadb8b162021-04-20 16:06:57 +0100279 *
Paul Elliott4148a682021-05-14 17:26:56 +0100280 * The PSA core calls this function before calling mbedtls_psa_aead_update_ad()
281 * or mbedtls_psa_aead_update() if the algorithm for the operation requires it.
282 * If the algorithm does not require it, calling this function is optional, but
283 * if this function is called then the implementation must enforce the lengths.
Paul Elliottadb8b162021-04-20 16:06:57 +0100284 *
Paul Elliott4148a682021-05-14 17:26:56 +0100285 * The PSA core may call this function before or after setting the nonce with
286 * mbedtls_psa_aead_set_nonce().
Paul Elliottadb8b162021-04-20 16:06:57 +0100287 *
288 * - For #PSA_ALG_CCM, calling this function is required.
289 * - For the other AEAD algorithms defined in this specification, calling
290 * this function is not required.
291 * - For vendor-defined algorithm, refer to the vendor documentation.
292 *
Paul Elliott498d3502021-05-17 18:16:20 +0100293 * If this function returns an error status, the PSA core calls
294 * mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100295 *
296 * \param[in,out] operation Active AEAD operation.
297 * \param ad_length Size of the non-encrypted additional
298 * authenticated data in bytes.
299 * \param plaintext_length Size of the plaintext to encrypt in bytes.
300 *
301 * \retval #PSA_SUCCESS
302 * Success.
Paul Elliottadb8b162021-04-20 16:06:57 +0100303 * \retval #PSA_ERROR_INVALID_ARGUMENT
304 * At least one of the lengths is not acceptable for the chosen
305 * algorithm.
Paul Elliottb91f3312021-05-19 12:30:15 +0100306 * \retval #PSA_ERROR_NOT_SUPPORTED
307 * Algorithm previously set is not supported in this configuration of
308 * the library.
Paul Elliottadb8b162021-04-20 16:06:57 +0100309 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100310psa_status_t mbedtls_psa_aead_set_lengths(mbedtls_psa_aead_operation_t
311 *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100312 size_t ad_length,
313 size_t plaintext_length);
314
315/** Pass additional data to an active AEAD operation.
316 *
317 * \note The signature of this function is that of a PSA driver
318 * aead_update_ad entry point. This function behaves as an aead_update_ad
319 * entry point as defined in the PSA driver interface specification for
320 * transparent drivers.
321 *
322 * Additional data is authenticated, but not encrypted.
323 *
Paul Elliott4148a682021-05-14 17:26:56 +0100324 * The PSA core can call this function multiple times to pass successive
325 * fragments of the additional data. It will not call this function after
326 * passing data to encrypt or decrypt with mbedtls_psa_aead_update().
Paul Elliottadb8b162021-04-20 16:06:57 +0100327 *
Paul Elliott498d3502021-05-17 18:16:20 +0100328 * Before calling this function, the PSA core will:
Paul Elliott4148a682021-05-14 17:26:56 +0100329 * 1. Call either mbedtls_psa_aead_encrypt_setup() or
330 * mbedtls_psa_aead_decrypt_setup().
331 * 2. Set the nonce with mbedtls_psa_aead_set_nonce().
Paul Elliottadb8b162021-04-20 16:06:57 +0100332 *
Paul Elliott4148a682021-05-14 17:26:56 +0100333 * If this function returns an error status, the PSA core will call
334 * mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100335 *
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100336 * \warning When decrypting, until mbedtls_psa_aead_verify() has returned
337 * #PSA_SUCCESS, there is no guarantee that the input is valid.
338 * Therefore, until you have called mbedtls_psa_aead_verify() and it
339 * has returned #PSA_SUCCESS, treat the input as untrusted and prepare
340 * to undo any action that depends on the input if
341 * mbedtls_psa_aead_verify() returns an error status.
Paul Elliottadb8b162021-04-20 16:06:57 +0100342 *
343 * \note For the time being #PSA_ALG_CCM and #PSA_ALG_GCM require the entire
Paul Elliott4148a682021-05-14 17:26:56 +0100344 * additional data to be passed in in one go, i.e.
Paul Elliott498d3502021-05-17 18:16:20 +0100345 * mbedtls_psa_aead_update_ad() can only be called once.
Paul Elliottadb8b162021-04-20 16:06:57 +0100346 *
347 * \param[in,out] operation Active AEAD operation.
348 * \param[in] input Buffer containing the fragment of
349 * additional data.
350 * \param input_length Size of the \p input buffer in bytes.
351 *
352 * \retval #PSA_SUCCESS
353 * Success.
Paul Elliottadb8b162021-04-20 16:06:57 +0100354 * \retval #PSA_ERROR_INVALID_ARGUMENT
355 * The total input length overflows the additional data length that
356 * was previously specified with mbedtls_psa_aead_set_lengths().
Paul Elliottb91f3312021-05-19 12:30:15 +0100357 * \retval #PSA_ERROR_NOT_SUPPORTED
358 * (For GCM / CCM) PSA core attempted to call mbedtls_psa_update_ad()
359 * more than once.
360 * \retval #PSA_ERROR_NOT_SUPPORTED
361 * Algorithm previously set is not supported in this configuration of
362 * the library.
Paul Elliottadb8b162021-04-20 16:06:57 +0100363 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100364psa_status_t mbedtls_psa_aead_update_ad(mbedtls_psa_aead_operation_t *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100365 const uint8_t *input,
366 size_t input_length);
367
368/** Encrypt or decrypt a message fragment in an active AEAD operation.
369 *
370 * \note The signature of this function is that of a PSA driver
371 * aead_update entry point. This function behaves as an aead_update entry
372 * point as defined in the PSA driver interface specification for
373 * transparent drivers.
374 *
Paul Elliott4148a682021-05-14 17:26:56 +0100375 * Before calling this function, the PSA core will:
376 * 1. Call either mbedtls_psa_aead_encrypt_setup() or
377 * mbedtls_psa_aead_decrypt_setup(). The choice of setup function
378 * determines whether this function encrypts or decrypts its input.
379 * 2. Set the nonce with mbedtls_psa_aead_set_nonce().
380 * 3. Call mbedtls_psa_aead_update_ad() to pass all the additional data.
Paul Elliottadb8b162021-04-20 16:06:57 +0100381 *
Paul Elliott4148a682021-05-14 17:26:56 +0100382 * If this function returns an error status, the PSA core will call
383 * mbedtls_psa_aead_abort().
Paul Elliottadb8b162021-04-20 16:06:57 +0100384 *
385 * This function does not require the input to be aligned to any
386 * particular block boundary. If the implementation can only process
387 * a whole block at a time, it must consume all the input provided, but
388 * it may delay the end of the corresponding output until a subsequent
389 * call to mbedtls_psa_aead_update(), mbedtls_psa_aead_finish() or
390 * mbedtls_psa_aead_verify() provides sufficient input. The amount of data that
391 * can be delayed in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
392 *
393 * \note For the time being #PSA_ALG_CCM and #PSA_ALG_GCM require the entire
Paul Elliott498d3502021-05-17 18:16:20 +0100394 * data to be passed in in one go, i.e. mbedtls_psa_aead_update() can only
395 * be called once.
Paul Elliottadb8b162021-04-20 16:06:57 +0100396 *
397 * \param[in,out] operation Active AEAD operation.
398 * \param[in] input Buffer containing the message fragment to
399 * encrypt or decrypt.
400 * \param input_length Size of the \p input buffer in bytes.
401 * \param[out] output Buffer where the output is to be written.
402 * \param output_size Size of the \p output buffer in bytes.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100403 * This must be appropriate for the selected
404 * algorithm and key:
405 * - A sufficient output size is
406 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
407 * \c alg, \p input_length) where
408 * \c key_type is the type of key and \c alg is
409 * the algorithm that were used to set up the
410 * operation.
411 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
412 * input_length) evaluates to the maximum
413 * output size of any supported AEAD
414 * algorithm.
Paul Elliottadb8b162021-04-20 16:06:57 +0100415 * \param[out] output_length On success, the number of bytes
416 * that make up the returned output.
417 *
418 * \retval #PSA_SUCCESS
419 * Success.
Paul Elliottb91f3312021-05-19 12:30:15 +0100420
Paul Elliottadb8b162021-04-20 16:06:57 +0100421 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
422 * The size of the \p output buffer is too small.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100423 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
424 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
425 * determine the required buffer size.
Paul Elliottadb8b162021-04-20 16:06:57 +0100426 * \retval #PSA_ERROR_INVALID_ARGUMENT
427 * The total length of input to mbedtls_psa_aead_update_ad() so far is
428 * less than the additional data length that was previously
429 * specified with mbedtls_psa_aead_set_lengths().
430 * \retval #PSA_ERROR_INVALID_ARGUMENT
431 * The total input length overflows the plaintext length that
432 * was previously specified with mbedtls_psa_aead_set_lengths().
Paul Elliottb91f3312021-05-19 12:30:15 +0100433 * \retval #PSA_ERROR_NOT_SUPPORTED
434 * (GCM / CCM only) PSA core attempted to call mbedtls_psa_update() more
435 * than once.
Paul Elliottadb8b162021-04-20 16:06:57 +0100436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Paul Elliottb91f3312021-05-19 12:30:15 +0100437 * (CCM only) Unable to allocate memory for the tag or the body
438
Paul Elliottadb8b162021-04-20 16:06:57 +0100439 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100440psa_status_t mbedtls_psa_aead_update(mbedtls_psa_aead_operation_t *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100441 const uint8_t *input,
442 size_t input_length,
443 uint8_t *output,
444 size_t output_size,
445 size_t *output_length);
446
447/** Finish encrypting a message in an AEAD operation.
448 *
449 * \note The signature of this function is that of a PSA driver
450 * aead_finish entry point. This function behaves as an aead_finish entry
451 * point as defined in the PSA driver interface specification for
452 * transparent drivers.
453 *
Paul Elliott4148a682021-05-14 17:26:56 +0100454 * The operation must have been set up by the PSA core with
455 * mbedtls_psa_aead_encrypt_setup().
Paul Elliottadb8b162021-04-20 16:06:57 +0100456 *
457 * This function finishes the authentication of the additional data
458 * formed by concatenating the inputs passed to preceding calls to
459 * mbedtls_psa_aead_update_ad() with the plaintext formed by concatenating the
460 * inputs passed to preceding calls to mbedtls_psa_aead_update().
461 *
462 * This function has two output buffers:
463 * - \p ciphertext contains trailing ciphertext that was buffered from
Paul Elliott498d3502021-05-17 18:16:20 +0100464 * preceding calls to mbedtls_psa_aead_update().
Paul Elliott4148a682021-05-14 17:26:56 +0100465 * - \p tag contains the authentication tag.
Paul Elliottadb8b162021-04-20 16:06:57 +0100466 *
Paul Elliott4148a682021-05-14 17:26:56 +0100467 * Whether or not this function returns successfuly, the PSA core subsequently
468 * calls mbedtls_psa_aead_abort() to deactivate the operation.
Paul Elliottadb8b162021-04-20 16:06:57 +0100469 *
470 * \param[in,out] operation Active AEAD operation.
471 * \param[out] ciphertext Buffer where the last part of the ciphertext
472 * is to be written.
473 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100474 * This must be appropriate for the selected
475 * algorithm and key:
476 * - A sufficient output size is
477 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
478 * \c alg) where \c key_type is the type of key
479 * and \c alg is the algorithm that were used to
480 * set up the operation.
481 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
482 * the maximum output size of any supported AEAD
483 * algorithm.
Paul Elliottadb8b162021-04-20 16:06:57 +0100484 * \param[out] ciphertext_length On success, the number of bytes of
485 * returned ciphertext.
486 * \param[out] tag Buffer where the authentication tag is
487 * to be written.
488 * \param tag_size Size of the \p tag buffer in bytes.
Paul Elliott4148a682021-05-14 17:26:56 +0100489 * This must be appropriate for the selected
490 * algorithm and key:
491 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
492 * key_type, \c key_bits, \c alg) where
493 * \c key_type and \c key_bits are the type and
Paul Elliott498d3502021-05-17 18:16:20 +0100494 * bit-size of the key, and \c alg are the
Paul Elliott4148a682021-05-14 17:26:56 +0100495 * algorithm that were used in the call to
Paul Elliott498d3502021-05-17 18:16:20 +0100496 * mbedtls_psa_aead_encrypt_setup().
Paul Elliott4148a682021-05-14 17:26:56 +0100497 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
498 * maximum tag size of any supported AEAD
499 * algorithm.
Paul Elliottadb8b162021-04-20 16:06:57 +0100500 * \param[out] tag_length On success, the number of bytes
501 * that make up the returned tag.
502 *
503 * \retval #PSA_SUCCESS
504 * Success.
505 * \retval #PSA_ERROR_BAD_STATE
506 * The operation state is not valid (it must be an active encryption
507 * operation with a nonce set).
508 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
509 * The size of the \p ciphertext or \p tag buffer is too small.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100510 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
511 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
512 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
513 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
514 * determine the required \p tag buffer size.
Paul Elliottadb8b162021-04-20 16:06:57 +0100515 * \retval #PSA_ERROR_INVALID_ARGUMENT
Paul Elliott498d3502021-05-17 18:16:20 +0100516 * The total length of input to mbedtls_psa_aead_update_ad() so far is
Paul Elliottadb8b162021-04-20 16:06:57 +0100517 * less than the additional data length that was previously
Paul Elliott498d3502021-05-17 18:16:20 +0100518 * specified with mbedtls_psa_aead_set_lengths().
Paul Elliottadb8b162021-04-20 16:06:57 +0100519 * \retval #PSA_ERROR_INVALID_ARGUMENT
520 * The total length of input to mbedtls_psa_aead_update() so far is
521 * less than the plaintext length that was previously
522 * specified with mbedtls_psa_aead_set_lengths().
Paul Elliottadb8b162021-04-20 16:06:57 +0100523 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100524psa_status_t mbedtls_psa_aead_finish(mbedtls_psa_aead_operation_t *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100525 uint8_t *ciphertext,
526 size_t ciphertext_size,
527 size_t *ciphertext_length,
528 uint8_t *tag,
529 size_t tag_size,
530 size_t *tag_length);
531
532/** Finish authenticating and decrypting a message in an AEAD operation.
533 *
534 * \note The signature of this function is that of a PSA driver
535 * aead_verify entry point. This function behaves as an aead_verify entry
536 * point as defined in the PSA driver interface specification for
537 * transparent drivers.
538 *
Paul Elliott4148a682021-05-14 17:26:56 +0100539 * The operation must have been set up by the PSA core with
540 * mbedtls_psa_aead_decrypt_setup().
Paul Elliottadb8b162021-04-20 16:06:57 +0100541 *
542 * This function finishes the authenticated decryption of the message
543 * components:
544 *
545 * - The additional data consisting of the concatenation of the inputs
546 * passed to preceding calls to mbedtls_psa_aead_update_ad().
547 * - The ciphertext consisting of the concatenation of the inputs passed to
548 * preceding calls to mbedtls_psa_aead_update().
549 * - The tag passed to this function call.
550 *
551 * If the authentication tag is correct, this function outputs any remaining
552 * plaintext and reports success. If the authentication tag is not correct,
553 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
554 *
Paul Elliott4148a682021-05-14 17:26:56 +0100555 * Whether or not this function returns successfully, the PSA core subsequently
556 * calls mbedtls_psa_aead_abort() to deactivate the operation.
Paul Elliottadb8b162021-04-20 16:06:57 +0100557 *
558 * \note Implementations shall make the best effort to ensure that the
559 * comparison between the actual tag and the expected tag is performed
560 * in constant time.
561 *
562 * \param[in,out] operation Active AEAD operation.
563 * \param[out] plaintext Buffer where the last part of the plaintext
564 * is to be written. This is the remaining data
565 * from previous calls to mbedtls_psa_aead_update()
566 * that could not be processed until the end
567 * of the input.
568 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100569 * This must be appropriate for the selected
570 * algorithm and key:
571 * - A sufficient output size is
572 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
573 * \c alg) where \c key_type is the type of key
574 * and \c alg is the algorithm that were used to
575 * set up the operation.
576 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
577 * the maximum output size of any supported AEAD
578 * algorithm.
Paul Elliottadb8b162021-04-20 16:06:57 +0100579 * \param[out] plaintext_length On success, the number of bytes of
580 * returned plaintext.
581 * \param[in] tag Buffer containing the authentication tag.
582 * \param tag_length Size of the \p tag buffer in bytes.
583 *
584 * \retval #PSA_SUCCESS
585 * Success.
586 * \retval #PSA_ERROR_INVALID_SIGNATURE
587 * The calculations were successful, but the authentication tag is
588 * not correct.
589 * \retval #PSA_ERROR_BAD_STATE
590 * The operation state is not valid (it must be an active decryption
591 * operation with a nonce set).
592 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
593 * The size of the \p plaintext buffer is too small.
Paul Elliott9622c9a2021-05-17 17:30:52 +0100594 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
595 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
596 * required buffer size.
Paul Elliottadb8b162021-04-20 16:06:57 +0100597 * \retval #PSA_ERROR_INVALID_ARGUMENT
598 * The total length of input to mbedtls_psa_aead_update_ad() so far is
599 * less than the additional data length that was previously
600 * specified with mbedtls_psa_aead_set_lengths().
601 * \retval #PSA_ERROR_INVALID_ARGUMENT
602 * The total length of input to mbedtls_psa_aead_update() so far is
603 * less than the plaintext length that was previously
Paul Elliott498d3502021-05-17 18:16:20 +0100604 * specified with mbedtls_psa_aead_set_lengths().
Paul Elliottadb8b162021-04-20 16:06:57 +0100605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Paul Elliottb91f3312021-05-19 12:30:15 +0100606 * (CCM only) Failed to allocate temporary buffer
Paul Elliottadb8b162021-04-20 16:06:57 +0100607 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100608psa_status_t mbedtls_psa_aead_verify(mbedtls_psa_aead_operation_t *operation,
Paul Elliottadb8b162021-04-20 16:06:57 +0100609 uint8_t *plaintext,
610 size_t plaintext_size,
611 size_t *plaintext_length,
612 const uint8_t *tag,
613 size_t tag_length);
614
615/** Abort an AEAD operation.
616 *
617 * \note The signature of this function is that of a PSA driver
618 * aead_abort entry point. This function behaves as an aead_abort entry
619 * point as defined in the PSA driver interface specification for
620 * transparent drivers.
621 *
622 * Aborting an operation frees all associated resources except for the
623 * \p operation structure itself. Once aborted, the operation object
Paul Elliott4148a682021-05-14 17:26:56 +0100624 * can be reused for another operation by the PSA core by it calling
Paul Elliottadb8b162021-04-20 16:06:57 +0100625 * mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup() again.
626 *
Paul Elliott4148a682021-05-14 17:26:56 +0100627 * The PSA core may call this function any time after the operation object has
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100628 * been initialized as described in #mbedtls_psa_aead_operation_t.
Paul Elliottadb8b162021-04-20 16:06:57 +0100629 *
630 * In particular, calling mbedtls_psa_aead_abort() after the operation has been
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100631 * terminated by a call to mbedtls_psa_aead_abort(), mbedtls_psa_aead_finish()
632 * or mbedtls_psa_aead_verify() is safe and has no effect.
Paul Elliottadb8b162021-04-20 16:06:57 +0100633 *
634 * \param[in,out] operation Initialized AEAD operation.
635 *
636 * \retval #PSA_SUCCESS
Paul Elliottb91f3312021-05-19 12:30:15 +0100637 * Success.
Paul Elliottadb8b162021-04-20 16:06:57 +0100638 */
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100639psa_status_t mbedtls_psa_aead_abort(mbedtls_psa_aead_operation_t *operation);
Paul Elliottadb8b162021-04-20 16:06:57 +0100640
641
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100642#endif /* PSA_CRYPTO_AEAD */