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