blob: a9d268773e4a1f09da0ddde4402754b054a5417f [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 *
158 * The sequence of operations to encrypt a message with authentication
159 * is as follows:
160 * -# Allocate an operation object which will be passed to all the functions
161 * listed here.
162 * -# Initialize the operation object with one of the methods described in the
163 * documentation for #psa_aead_operation_t, e.g.
164 * #PSA_AEAD_OPERATION_INIT.
165 * -# Call mbedtls_psa_aead_encrypt_setup() to specify the algorithm and key.
166 * -# If needed, call mbedtls_psa_aead_set_lengths() to specify the length of
167 * the inputs to the subsequent calls to mbedtls_psa_aead_update_ad() and
168 * mbedtls_psa_aead_update(). See the documentation of mbedtls_psa_aead_set_lengths()
169 * for details.
Paul Elliott302ff6b2021-04-20 18:10:30 +0100170 * -# Call either psa_aead_generate_nonce() or
171 * mbedtls_psa_aead_set_nonce() to generate or set the nonce. You should use
172 * psa_aead_generate_nonce() unless the protocol you are implementing
Paul Elliottadb8b162021-04-20 16:06:57 +0100173 * requires a specific nonce value.
174 * -# Call mbedtls_psa_aead_update_ad() zero, one or more times, passing a fragment
175 * of the non-encrypted additional authenticated data each time.
176 * -# Call mbedtls_psa_aead_update() zero, one or more times, passing a fragment
177 * of the message to encrypt each time.
178 * -# Call mbedtls_psa_aead_finish().
179 *
180 * If an error occurs at any step after a call to mbedtls_psa_aead_encrypt_setup(),
181 * the operation will need to be reset by a call to mbedtls_psa_aead_abort(). The
182 * application may call mbedtls_psa_aead_abort() at any time after the operation
183 * has been initialized.
184 *
185 * After a successful call to mbedtls_psa_aead_encrypt_setup(), the application must
186 * eventually terminate the operation. The following events terminate an
187 * operation:
188 * - A successful call to mbedtls_psa_aead_finish().
189 * - A call to mbedtls_psa_aead_abort().
190 *
191 * \param[in,out] operation The operation object to set up. It must have
192 * been initialized as per the documentation for
193 * #mbedtls_psa_aead_operation_t and not yet in use.
194 * \param[in] attributes The attributes of the key to use for the
195 * operation.
196 * \param[in] key_buffer The buffer containing the key context.
197 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
198 * \param alg The AEAD algorithm to compute
199 * (\c PSA_ALG_XXX value such that
200 * #PSA_ALG_IS_AEAD(\p alg) is true).
201 *
202 * \retval #PSA_SUCCESS
203 * Success.
204 * \retval #PSA_ERROR_BAD_STATE
205 * The operation state is not valid (it must be inactive).
206 * \retval #PSA_ERROR_INVALID_HANDLE
207 * \retval #PSA_ERROR_NOT_PERMITTED
208 * \retval #PSA_ERROR_INVALID_ARGUMENT
209 * \p key is not compatible with \p alg.
210 * \retval #PSA_ERROR_NOT_SUPPORTED
211 * \p alg is not supported or is not an AEAD algorithm.
212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
214 * \retval #PSA_ERROR_HARDWARE_FAILURE
215 * \retval #PSA_ERROR_CORRUPTION_DETECTED
216 * \retval #PSA_ERROR_STORAGE_FAILURE
217 * \retval #PSA_ERROR_BAD_STATE
218 * The library has not been previously initialized by psa_crypto_init().
219 * It is implementation-dependent whether a failure to initialize
220 * results in this error code.
221 */
222psa_status_t mbedtls_psa_aead_encrypt_setup(psa_aead_operation_t *operation,
223 const psa_key_attributes_t *attributes,
224 const uint8_t *key_buffer, size_t key_buffer_size,
225 psa_algorithm_t alg);
226
227/** Set the key for a multipart authenticated decryption operation.
228 *
229 * \note The signature of this function is that of a PSA driver
230 * aead_decrypt_setup entry point. This function behaves as an
231 * aead_decrypt_setup entry point as defined in the PSA driver interface
232 * specification for transparent drivers.
233 *
234 * The sequence of operations to decrypt a message with authentication
235 * is as follows:
236 * -# Allocate an operation object which will be passed to all the functions
237 * listed here.
238 * -# Initialize the operation object with one of the methods described in the
239 * documentation for #psa_aead_operation_t, e.g.
240 * #PSA_AEAD_OPERATION_INIT.
241 * -# Call mbedtls_psa_aead_decrypt_setup() to specify the algorithm and key.
242 * -# If needed, call mbedtls_psa_aead_set_lengths() to specify the length of the
243 * inputs to the subsequent calls to mbedtls_psa_aead_update_ad() and
244 * mbedtls_psa_aead_update(). See the documentation of mbedtls_psa_aead_set_lengths()
245 * for details.
246 * -# Call mbedtls_psa_aead_set_nonce() with the nonce for the decryption.
247 * -# Call mbedtls_psa_aead_update_ad() zero, one or more times, passing a fragment
248 * of the non-encrypted additional authenticated data each time.
249 * -# Call mbedtls_psa_aead_update() zero, one or more times, passing a fragment
250 * of the ciphertext to decrypt each time.
251 * -# Call mbedtls_psa_aead_verify().
252 *
253 * If an error occurs at any step after a call to mbedtls_psa_aead_decrypt_setup(),
254 * the operation will need to be reset by a call to mbedtls_psa_aead_abort(). The
255 * application may call mbedtls_psa_aead_abort() at any time after the operation
256 * has been initialized.
257 *
258 * After a successful call to mbedtls_psa_aead_decrypt_setup(), the application must
259 * eventually terminate the operation. The following events terminate an
260 * operation:
261 * - A successful call to mbedtls_psa_aead_verify().
262 * - A call to mbedtls_psa_aead_abort().
263 *
264 * \param[in,out] operation The operation object to set up. It must have
265 * been initialized as per the documentation for
266 * #psa_aead_operation_t and not yet in use.
267 * \param[in] attributes The attributes of the key to use for the
268 * operation.
269 * \param[in] key_buffer The buffer containing the key context.
270 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
271 * \param alg The AEAD algorithm to compute
272 * (\c PSA_ALG_XXX value such that
273 * #PSA_ALG_IS_AEAD(\p alg) is true).
274 *
275 * \retval #PSA_SUCCESS
276 * Success.
277 * \retval #PSA_ERROR_BAD_STATE
278 * The operation state is not valid (it must be inactive).
279 * \retval #PSA_ERROR_INVALID_HANDLE
280 * \retval #PSA_ERROR_NOT_PERMITTED
281 * \retval #PSA_ERROR_INVALID_ARGUMENT
282 * \p key is not compatible with \p alg.
283 * \retval #PSA_ERROR_NOT_SUPPORTED
284 * \p alg is not supported or is not an AEAD algorithm.
285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
287 * \retval #PSA_ERROR_HARDWARE_FAILURE
288 * \retval #PSA_ERROR_CORRUPTION_DETECTED
289 * \retval #PSA_ERROR_STORAGE_FAILURE
290 * \retval #PSA_ERROR_BAD_STATE
291 * The library has not been previously initialized by psa_crypto_init().
292 * It is implementation-dependent whether a failure to initialize
293 * results in this error code.
294 */
295psa_status_t mbedtls_psa_aead_decrypt_setup(psa_aead_operation_t *operation,
296 const psa_key_attributes_t *attributes,
297 const uint8_t *key_buffer, size_t key_buffer_size,
298 psa_algorithm_t alg);
299
Paul Elliottadb8b162021-04-20 16:06:57 +0100300/** Set the nonce for an authenticated encryption or decryption operation.
301 *
302 * \note The signature of this function is that of a PSA driver
303 * psa_aead_set_nonce entry point. This function behaves as an
304 * psa_aead_set_nonce entry point as defined in the PSA driver interface
305 * specification for transparent drivers.
306 *
307 * This function sets the nonce for the authenticated
308 * encryption or decryption operation.
309 *
310 * The application must call mbedtls_psa_aead_encrypt_setup() or
311 * mbedtls_psa_aead_decrypt_setup() before calling this function.
312 *
313 * If this function returns an error status, the operation enters an error
314 * state and must be aborted by calling mbedtls_psa_aead_abort().
315 *
316 * \note When encrypting, applications should use mbedtls_psa_aead_generate_nonce()
317 * instead of this function, unless implementing a protocol that requires
318 * a non-random IV.
319 *
320 * \param[in,out] operation Active AEAD operation.
321 * \param[in] nonce Buffer containing the nonce to use.
322 * \param nonce_length Size of the nonce in bytes.
323 *
324 * \retval #PSA_SUCCESS
325 * Success.
326 * \retval #PSA_ERROR_BAD_STATE
327 * The operation state is not valid (it must be active, with no nonce
328 * set).
329 * \retval #PSA_ERROR_INVALID_ARGUMENT
330 * The size of \p nonce is not acceptable for the chosen algorithm.
331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
333 * \retval #PSA_ERROR_HARDWARE_FAILURE
334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
335 * \retval #PSA_ERROR_STORAGE_FAILURE
336 * \retval #PSA_ERROR_BAD_STATE
337 * The library has not been previously initialized by psa_crypto_init().
338 * It is implementation-dependent whether a failure to initialize
339 * results in this error code.
340 */
341psa_status_t mbedtls_psa_aead_set_nonce(psa_aead_operation_t *operation,
342 const uint8_t *nonce,
343 size_t nonce_length);
344
345/** Declare the lengths of the message and additional data for AEAD.
346 *
347 * \note The signature of this function is that of a PSA driver
348 * psa_aead_set_lengths entry point. This function behaves as an
349 * psa_aead_set_lengths entry point as defined in the PSA driver interface
350 * specification for transparent drivers.
351 *
352 * The application must call this function before calling
353 * mbedtls_psa_aead_update_ad() or mbedtls_psa_aead_update() if the algorithm for
354 * the operation requires it. If the algorithm does not require it,
355 * calling this function is optional, but if this function is called
356 * then the implementation must enforce the lengths.
357 *
358 * You may call this function before or after setting the nonce with
Paul Elliott302ff6b2021-04-20 18:10:30 +0100359 * mbedtls_psa_aead_set_nonce() or psa_aead_generate_nonce().
Paul Elliottadb8b162021-04-20 16:06:57 +0100360 *
361 * - For #PSA_ALG_CCM, calling this function is required.
362 * - For the other AEAD algorithms defined in this specification, calling
363 * this function is not required.
364 * - For vendor-defined algorithm, refer to the vendor documentation.
365 *
366 * If this function returns an error status, the operation enters an error
367 * state and must be aborted by calling mbedtls_psa_aead_abort().
368 *
369 * \param[in,out] operation Active AEAD operation.
370 * \param ad_length Size of the non-encrypted additional
371 * authenticated data in bytes.
372 * \param plaintext_length Size of the plaintext to encrypt in bytes.
373 *
374 * \retval #PSA_SUCCESS
375 * Success.
376 * \retval #PSA_ERROR_BAD_STATE
377 * The operation state is not valid (it must be active, and
378 * mbedtls_psa_aead_update_ad() and mbedtls_psa_aead_update() must not have been
379 * called yet).
380 * \retval #PSA_ERROR_INVALID_ARGUMENT
381 * At least one of the lengths is not acceptable for the chosen
382 * algorithm.
383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
385 * \retval #PSA_ERROR_HARDWARE_FAILURE
386 * \retval #PSA_ERROR_CORRUPTION_DETECTED
387 * \retval #PSA_ERROR_BAD_STATE
388 * The library has not been previously initialized by psa_crypto_init().
389 * It is implementation-dependent whether a failure to initialize
390 * results in this error code.
391 */
392psa_status_t mbedtls_psa_aead_set_lengths(psa_aead_operation_t *operation,
393 size_t ad_length,
394 size_t plaintext_length);
395
396/** Pass additional data to an active AEAD operation.
397 *
398 * \note The signature of this function is that of a PSA driver
399 * aead_update_ad entry point. This function behaves as an aead_update_ad
400 * entry point as defined in the PSA driver interface specification for
401 * transparent drivers.
402 *
403 * Additional data is authenticated, but not encrypted.
404 *
405 * You may call this function multiple times to pass successive fragments
406 * of the additional data. You may not call this function after passing
407 * data to encrypt or decrypt with mbedtls_psa_aead_update().
408 *
409 * Before calling this function, you must:
410 * 1. Call either mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup().
Paul Elliott302ff6b2021-04-20 18:10:30 +0100411 * 2. Set the nonce with psa_aead_generate_nonce() or
Paul Elliottadb8b162021-04-20 16:06:57 +0100412 * mbedtls_psa_aead_set_nonce().
413 *
414 * If this function returns an error status, the operation enters an error
415 * state and must be aborted by calling mbedtls_psa_aead_abort().
416 *
417 * \warning When decrypting, until mbedtls_psa_aead_verify() has returned #PSA_SUCCESS,
418 * there is no guarantee that the input is valid. Therefore, until
419 * you have called mbedtls_psa_aead_verify() and it has returned #PSA_SUCCESS,
420 * treat the input as untrusted and prepare to undo any action that
421 * depends on the input if mbedtls_psa_aead_verify() returns an error status.
422 *
423 * \note For the time being #PSA_ALG_CCM and #PSA_ALG_GCM require the entire
424 * additional data to be passed in in one go, i.e. only call
425 * mbedtls_mbedtls_psa_aead_update_ad() once.
426 *
427 * \param[in,out] operation Active AEAD operation.
428 * \param[in] input Buffer containing the fragment of
429 * additional data.
430 * \param input_length Size of the \p input buffer in bytes.
431 *
432 * \retval #PSA_SUCCESS
433 * Success.
434 * \retval #PSA_ERROR_BAD_STATE
435 * The operation state is not valid (it must be active, have a nonce
436 * set, have lengths set if required by the algorithm, and
437 * mbedtls_psa_aead_update() must not have been called yet).
438 * \retval #PSA_ERROR_INVALID_ARGUMENT
439 * The total input length overflows the additional data length that
440 * was previously specified with mbedtls_psa_aead_set_lengths().
441 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
442 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
443 * \retval #PSA_ERROR_HARDWARE_FAILURE
444 * \retval #PSA_ERROR_CORRUPTION_DETECTED
445 * \retval #PSA_ERROR_STORAGE_FAILURE
446 * \retval #PSA_ERROR_BAD_STATE
447 * The library has not been previously initialized by psa_crypto_init().
448 * It is implementation-dependent whether a failure to initialize
449 * results in this error code.
450 */
451psa_status_t mbedtls_psa_aead_update_ad(psa_aead_operation_t *operation,
452 const uint8_t *input,
453 size_t input_length);
454
455/** Encrypt or decrypt a message fragment in an active AEAD operation.
456 *
457 * \note The signature of this function is that of a PSA driver
458 * aead_update entry point. This function behaves as an aead_update entry
459 * point as defined in the PSA driver interface specification for
460 * transparent drivers.
461 *
462 * Before calling this function, you must:
463 * 1. Call either mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup().
464 * The choice of setup function determines whether this function
465 * encrypts or decrypts its input.
Paul Elliott302ff6b2021-04-20 18:10:30 +0100466 * 2. Set the nonce with psa_aead_generate_nonce() or
467 * mbedtls_psa_aead_set_nonce(). 3. Call mbedtls_psa_aead_update_ad() to pass
468 * all the additional data.
Paul Elliottadb8b162021-04-20 16:06:57 +0100469 *
470 * If this function returns an error status, the operation enters an error
471 * state and must be aborted by calling mbedtls_psa_aead_abort().
472 *
473 * \warning When decrypting, until mbedtls_psa_aead_verify() has returned
474 * #PSA_SUCCESS, there is no guarantee that the input is valid.
475 * Therefore, until you have called mbedtls_psa_aead_verify() and it
476 * has returned #PSA_SUCCESS:
477 * - Do not use the output in any way other than storing it in a
478 * confidential location. If you take any action that depends
479 * on the tentative decrypted data, this action will need to be
480 * undone if the input turns out not to be valid. Furthermore,
481 * if an adversary can observe that this action took place
482 * (for example through timing), they may be able to use this
483 * fact as an oracle to decrypt any message encrypted with the
484 * same key.
485 * - In particular, do not copy the output anywhere but to a
486 * memory or storage space that you have exclusive access to.
487 *
488 * This function does not require the input to be aligned to any
489 * particular block boundary. If the implementation can only process
490 * a whole block at a time, it must consume all the input provided, but
491 * it may delay the end of the corresponding output until a subsequent
492 * call to mbedtls_psa_aead_update(), mbedtls_psa_aead_finish() or
493 * mbedtls_psa_aead_verify() provides sufficient input. The amount of data that
494 * can be delayed in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
495 *
496 * \note For the time being #PSA_ALG_CCM and #PSA_ALG_GCM require the entire
497 * data to be passed in in one go, i.e. only call
498 * mbedtls_mbedtls_psa_aead_update() once.
499 *
500 * \param[in,out] operation Active AEAD operation.
501 * \param[in] input Buffer containing the message fragment to
502 * encrypt or decrypt.
503 * \param input_length Size of the \p input buffer in bytes.
504 * \param[out] output Buffer where the output is to be written.
505 * \param output_size Size of the \p output buffer in bytes.
506 * This must be at least
507 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
508 * \p input_length) where \c alg is the
509 * algorithm that is being calculated.
510 * \param[out] output_length On success, the number of bytes
511 * that make up the returned output.
512 *
513 * \retval #PSA_SUCCESS
514 * Success.
515 * \retval #PSA_ERROR_BAD_STATE
516 * The operation state is not valid (it must be active, have a nonce
517 * set, and have lengths set if required by the algorithm).
518 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
519 * The size of the \p output buffer is too small.
520 * You can determine a sufficient buffer size by calling
521 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
522 * where \c alg is the algorithm that is being calculated.
523 * \retval #PSA_ERROR_INVALID_ARGUMENT
524 * The total length of input to mbedtls_psa_aead_update_ad() so far is
525 * less than the additional data length that was previously
526 * specified with mbedtls_psa_aead_set_lengths().
527 * \retval #PSA_ERROR_INVALID_ARGUMENT
528 * The total input length overflows the plaintext length that
529 * was previously specified with mbedtls_psa_aead_set_lengths().
530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
532 * \retval #PSA_ERROR_HARDWARE_FAILURE
533 * \retval #PSA_ERROR_CORRUPTION_DETECTED
534 * \retval #PSA_ERROR_STORAGE_FAILURE
535 * \retval #PSA_ERROR_BAD_STATE
536 * The library has not been previously initialized by psa_crypto_init().
537 * It is implementation-dependent whether a failure to initialize
538 * results in this error code.
539 */
540psa_status_t mbedtls_psa_aead_update(psa_aead_operation_t *operation,
541 const uint8_t *input,
542 size_t input_length,
543 uint8_t *output,
544 size_t output_size,
545 size_t *output_length);
546
547/** Finish encrypting a message in an AEAD operation.
548 *
549 * \note The signature of this function is that of a PSA driver
550 * aead_finish entry point. This function behaves as an aead_finish entry
551 * point as defined in the PSA driver interface specification for
552 * transparent drivers.
553 *
554 * The operation must have been set up with mbedtls_psa_aead_encrypt_setup().
555 *
556 * This function finishes the authentication of the additional data
557 * formed by concatenating the inputs passed to preceding calls to
558 * mbedtls_psa_aead_update_ad() with the plaintext formed by concatenating the
559 * inputs passed to preceding calls to mbedtls_psa_aead_update().
560 *
561 * This function has two output buffers:
562 * - \p ciphertext contains trailing ciphertext that was buffered from
563 * preceding calls to mbedtls_psa_aead_update().
564 * - \p tag contains the authentication tag. Its length is always
565 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
566 * that the operation performs.
567 *
568 * When this function returns successfuly, the operation becomes inactive.
569 * If this function returns an error status, the operation enters an error
570 * state and must be aborted by calling mbedtls_psa_aead_abort().
571 *
572 * \param[in,out] operation Active AEAD operation.
573 * \param[out] ciphertext Buffer where the last part of the ciphertext
574 * is to be written.
575 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
576 * This must be at least
577 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
578 * \c alg is the algorithm that is being
579 * calculated.
580 * \param[out] ciphertext_length On success, the number of bytes of
581 * returned ciphertext.
582 * \param[out] tag Buffer where the authentication tag is
583 * to be written.
584 * \param tag_size Size of the \p tag buffer in bytes.
585 * This must be at least
586 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
587 * the algorithm that is being calculated.
588 * \param[out] tag_length On success, the number of bytes
589 * that make up the returned tag.
590 *
591 * \retval #PSA_SUCCESS
592 * Success.
593 * \retval #PSA_ERROR_BAD_STATE
594 * The operation state is not valid (it must be an active encryption
595 * operation with a nonce set).
596 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
597 * The size of the \p ciphertext or \p tag buffer is too small.
598 * You can determine a sufficient buffer size for \p ciphertext by
599 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
600 * where \c alg is the algorithm that is being calculated.
601 * You can determine a sufficient buffer size for \p tag by
602 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
603 * \retval #PSA_ERROR_INVALID_ARGUMENT
604 * The total length of input to psa_aead_update_ad() so far is
605 * less than the additional data length that was previously
606 * specified with psa_aead_set_lengths().
607 * \retval #PSA_ERROR_INVALID_ARGUMENT
608 * The total length of input to mbedtls_psa_aead_update() so far is
609 * less than the plaintext length that was previously
610 * specified with mbedtls_psa_aead_set_lengths().
611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
613 * \retval #PSA_ERROR_HARDWARE_FAILURE
614 * \retval #PSA_ERROR_CORRUPTION_DETECTED
615 * \retval #PSA_ERROR_STORAGE_FAILURE
616 * \retval #PSA_ERROR_BAD_STATE
617 * The library has not been previously initialized by psa_crypto_init().
618 * It is implementation-dependent whether a failure to initialize
619 * results in this error code.
620 */
621psa_status_t mbedtls_psa_aead_finish(psa_aead_operation_t *operation,
622 uint8_t *ciphertext,
623 size_t ciphertext_size,
624 size_t *ciphertext_length,
625 uint8_t *tag,
626 size_t tag_size,
627 size_t *tag_length);
628
629/** Finish authenticating and decrypting a message in an AEAD operation.
630 *
631 * \note The signature of this function is that of a PSA driver
632 * aead_verify entry point. This function behaves as an aead_verify entry
633 * point as defined in the PSA driver interface specification for
634 * transparent drivers.
635 *
636 * The operation must have been set up with mbedtls_psa_aead_decrypt_setup().
637 *
638 * This function finishes the authenticated decryption of the message
639 * components:
640 *
641 * - The additional data consisting of the concatenation of the inputs
642 * passed to preceding calls to mbedtls_psa_aead_update_ad().
643 * - The ciphertext consisting of the concatenation of the inputs passed to
644 * preceding calls to mbedtls_psa_aead_update().
645 * - The tag passed to this function call.
646 *
647 * If the authentication tag is correct, this function outputs any remaining
648 * plaintext and reports success. If the authentication tag is not correct,
649 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
650 *
651 * When this function returns successfuly, the operation becomes inactive.
652 * If this function returns an error status, the operation enters an error
653 * state and must be aborted by calling mbedtls_psa_aead_abort().
654 *
655 * \note Implementations shall make the best effort to ensure that the
656 * comparison between the actual tag and the expected tag is performed
657 * in constant time.
658 *
659 * \param[in,out] operation Active AEAD operation.
660 * \param[out] plaintext Buffer where the last part of the plaintext
661 * is to be written. This is the remaining data
662 * from previous calls to mbedtls_psa_aead_update()
663 * that could not be processed until the end
664 * of the input.
665 * \param plaintext_size Size of the \p plaintext buffer in bytes.
666 * This must be at least
667 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
668 * \c alg is the algorithm that is being
669 * calculated.
670 * \param[out] plaintext_length On success, the number of bytes of
671 * returned plaintext.
672 * \param[in] tag Buffer containing the authentication tag.
673 * \param tag_length Size of the \p tag buffer in bytes.
674 *
675 * \retval #PSA_SUCCESS
676 * Success.
677 * \retval #PSA_ERROR_INVALID_SIGNATURE
678 * The calculations were successful, but the authentication tag is
679 * not correct.
680 * \retval #PSA_ERROR_BAD_STATE
681 * The operation state is not valid (it must be an active decryption
682 * operation with a nonce set).
683 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
684 * The size of the \p plaintext buffer is too small.
685 * You can determine a sufficient buffer size for \p plaintext by
686 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
687 * where \c alg is the algorithm that is being calculated.
688 * \retval #PSA_ERROR_INVALID_ARGUMENT
689 * The total length of input to mbedtls_psa_aead_update_ad() so far is
690 * less than the additional data length that was previously
691 * specified with mbedtls_psa_aead_set_lengths().
692 * \retval #PSA_ERROR_INVALID_ARGUMENT
693 * The total length of input to mbedtls_psa_aead_update() so far is
694 * less than the plaintext length that was previously
695 * specified with psa_aead_set_lengths().
696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
698 * \retval #PSA_ERROR_HARDWARE_FAILURE
699 * \retval #PSA_ERROR_CORRUPTION_DETECTED
700 * \retval #PSA_ERROR_STORAGE_FAILURE
701 * \retval #PSA_ERROR_BAD_STATE
702 * The library has not been previously initialized by psa_crypto_init().
703 * It is implementation-dependent whether a failure to initialize
704 * results in this error code.
705 */
706psa_status_t mbedtls_psa_aead_verify(psa_aead_operation_t *operation,
707 uint8_t *plaintext,
708 size_t plaintext_size,
709 size_t *plaintext_length,
710 const uint8_t *tag,
711 size_t tag_length);
712
713/** Abort an AEAD operation.
714 *
715 * \note The signature of this function is that of a PSA driver
716 * aead_abort entry point. This function behaves as an aead_abort entry
717 * point as defined in the PSA driver interface specification for
718 * transparent drivers.
719 *
720 * Aborting an operation frees all associated resources except for the
721 * \p operation structure itself. Once aborted, the operation object
722 * can be reused for another operation by calling
723 * mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup() again.
724 *
725 * You may call this function any time after the operation object has
726 * been initialized as described in #psa_aead_operation_t.
727 *
728 * In particular, calling mbedtls_psa_aead_abort() after the operation has been
729 * terminated by a call to mbedtls_psa_aead_abort(), mbedtls_psa_aead_finish() or
730 * mbedtls_psa_aead_verify() is safe and has no effect.
731 *
732 * \param[in,out] operation Initialized AEAD operation.
733 *
734 * \retval #PSA_SUCCESS
735 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
736 * \retval #PSA_ERROR_HARDWARE_FAILURE
737 * \retval #PSA_ERROR_CORRUPTION_DETECTED
738 * \retval #PSA_ERROR_BAD_STATE
739 * The library has not been previously initialized by psa_crypto_init().
740 * It is implementation-dependent whether a failure to initialize
741 * results in this error code.
742 */
743psa_status_t mbedtls_psa_aead_abort(psa_aead_operation_t *operation);
744
745
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100746#endif /* PSA_CRYPTO_AEAD */