blob: 0daa3034a1cffce54e6b3d887f194586dd2709d9 [file] [log] [blame]
Ronald Cron7ceee8d2021-03-17 16:55:43 +01001/*
2 * PSA AEAD 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#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include "psa_crypto_aead.h"
Ronald Cron46f91782021-03-17 08:16:34 +010026#include "psa_crypto_core.h"
27
Paul Elliottadb8b162021-04-20 16:06:57 +010028#include <string.h>
29#include "mbedtls/platform.h"
30#if !defined(MBEDTLS_PLATFORM_C)
31#define mbedtls_calloc calloc
32#define mbedtls_free free
33#endif
34
Ronald Cron46f91782021-03-17 08:16:34 +010035#include "mbedtls/ccm.h"
36#include "mbedtls/chachapoly.h"
37#include "mbedtls/cipher.h"
38#include "mbedtls/gcm.h"
Paul Elliottadb8b162021-04-20 16:06:57 +010039#include "mbedtls/error.h"
Ronald Cron46f91782021-03-17 08:16:34 +010040
Ronald Cron46f91782021-03-17 08:16:34 +010041static psa_status_t psa_aead_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010042 mbedtls_psa_aead_operation_t *operation,
Ronald Cron46f91782021-03-17 08:16:34 +010043 const psa_key_attributes_t *attributes,
44 const uint8_t *key_buffer,
Paul Elliottcc358592021-05-12 12:22:28 +010045 size_t key_buffer_size,
Ronald Cron46f91782021-03-17 08:16:34 +010046 psa_algorithm_t alg )
47{
48 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
49 size_t key_bits;
Ronald Cronecbc0682021-03-26 13:25:17 +010050 const mbedtls_cipher_info_t *cipher_info;
Ronald Cron46f91782021-03-17 08:16:34 +010051 mbedtls_cipher_id_t cipher_id;
Ronald Cronecbc0682021-03-26 13:25:17 +010052 size_t full_tag_length = 0;
Ronald Cron46f91782021-03-17 08:16:34 +010053
Paul Elliottcc358592021-05-12 12:22:28 +010054 ( void ) key_buffer_size;
55
Ronald Cron46f91782021-03-17 08:16:34 +010056 key_bits = attributes->core.bits;
57
Ronald Cronecbc0682021-03-26 13:25:17 +010058 cipher_info = mbedtls_cipher_info_from_psa( alg,
59 attributes->core.type, key_bits,
60 &cipher_id );
61 if( cipher_info == NULL )
Ronald Cron46f91782021-03-17 08:16:34 +010062 return( PSA_ERROR_NOT_SUPPORTED );
63
64 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
65 {
66#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
67 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010068 operation->alg = PSA_ALG_CCM;
Ronald Cronecbc0682021-03-26 13:25:17 +010069 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +010070 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
71 * The call to mbedtls_ccm_encrypt_and_tag or
72 * mbedtls_ccm_auth_decrypt will validate the tag length. */
73 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
74 return( PSA_ERROR_INVALID_ARGUMENT );
75
76 mbedtls_ccm_init( &operation->ctx.ccm );
77 status = mbedtls_to_psa_error(
78 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
79 key_buffer, (unsigned int) key_bits ) );
80 if( status != PSA_SUCCESS )
81 return( status );
82 break;
83#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
84
85#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
86 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010087 operation->alg = PSA_ALG_GCM;
Ronald Cronecbc0682021-03-26 13:25:17 +010088 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +010089 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
90 * The call to mbedtls_gcm_crypt_and_tag or
91 * mbedtls_gcm_auth_decrypt will validate the tag length. */
92 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
93 return( PSA_ERROR_INVALID_ARGUMENT );
94
95 mbedtls_gcm_init( &operation->ctx.gcm );
96 status = mbedtls_to_psa_error(
97 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
98 key_buffer, (unsigned int) key_bits ) );
99 if( status != PSA_SUCCESS )
100 return( status );
101 break;
102#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
103
104#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
105 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +0100106 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cronecbc0682021-03-26 13:25:17 +0100107 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +0100108 /* We only support the default tag length. */
109 if( alg != PSA_ALG_CHACHA20_POLY1305 )
110 return( PSA_ERROR_NOT_SUPPORTED );
111
112 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
113 status = mbedtls_to_psa_error(
114 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
115 key_buffer ) );
116 if( status != PSA_SUCCESS )
117 return( status );
118 break;
119#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
120
121 default:
122 return( PSA_ERROR_NOT_SUPPORTED );
123 }
124
Bence Szépkútiec174e22021-03-19 18:46:15 +0100125 if( PSA_AEAD_TAG_LENGTH( attributes->core.type,
126 key_bits, alg )
127 > full_tag_length )
Ronald Cron46f91782021-03-17 08:16:34 +0100128 return( PSA_ERROR_INVALID_ARGUMENT );
129
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100130 operation->key_type = psa_get_key_type( attributes );
131
132 operation->tag_length = PSA_AEAD_TAG_LENGTH( operation->key_type,
Bence Szépkútiec174e22021-03-19 18:46:15 +0100133 key_bits,
134 alg );
Ronald Cron46f91782021-03-17 08:16:34 +0100135
136 return( PSA_SUCCESS );
137}
138
139psa_status_t mbedtls_psa_aead_encrypt(
140 const psa_key_attributes_t *attributes,
141 const uint8_t *key_buffer, size_t key_buffer_size,
142 psa_algorithm_t alg,
143 const uint8_t *nonce, size_t nonce_length,
144 const uint8_t *additional_data, size_t additional_data_length,
145 const uint8_t *plaintext, size_t plaintext_length,
146 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
147{
148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100149 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100150 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100151
Paul Elliottcc358592021-05-12 12:22:28 +0100152 status = psa_aead_setup( &operation, attributes, key_buffer,
153 key_buffer_size, alg );
154
Ronald Cron46f91782021-03-17 08:16:34 +0100155 if( status != PSA_SUCCESS )
156 goto exit;
157
158 /* For all currently supported modes, the tag is at the end of the
159 * ciphertext. */
160 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
161 {
162 status = PSA_ERROR_BUFFER_TOO_SMALL;
163 goto exit;
164 }
165 tag = ciphertext + plaintext_length;
166
Ronald Cron46f91782021-03-17 08:16:34 +0100167#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100168 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100169 {
170 status = mbedtls_to_psa_error(
171 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
172 plaintext_length,
173 nonce, nonce_length,
174 additional_data,
175 additional_data_length,
176 plaintext, ciphertext,
177 tag, operation.tag_length ) );
178 }
179 else
180#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200181#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100182 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200183 {
184 status = mbedtls_to_psa_error(
185 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
186 MBEDTLS_GCM_ENCRYPT,
187 plaintext_length,
188 nonce, nonce_length,
189 additional_data, additional_data_length,
190 plaintext, ciphertext,
191 operation.tag_length, tag ) );
192 }
193 else
194#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100195#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100196 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100197 {
198 if( nonce_length != 12 || operation.tag_length != 16 )
199 {
200 status = PSA_ERROR_NOT_SUPPORTED;
201 goto exit;
202 }
203 status = mbedtls_to_psa_error(
204 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
205 plaintext_length,
206 nonce,
207 additional_data,
208 additional_data_length,
209 plaintext,
210 ciphertext,
211 tag ) );
212 }
213 else
214#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
215 {
216 (void) tag;
217 return( PSA_ERROR_NOT_SUPPORTED );
218 }
219
220 if( status == PSA_SUCCESS )
221 *ciphertext_length = plaintext_length + operation.tag_length;
222
223exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100224 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100225
226 return( status );
227}
228
229/* Locate the tag in a ciphertext buffer containing the encrypted data
230 * followed by the tag. Return the length of the part preceding the tag in
231 * *plaintext_length. This is the size of the plaintext in modes where
232 * the encrypted data has the same size as the plaintext, such as
233 * CCM and GCM. */
234static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
235 const uint8_t *ciphertext,
236 size_t ciphertext_length,
237 size_t plaintext_size,
238 const uint8_t **p_tag )
239{
240 size_t payload_length;
241 if( tag_length > ciphertext_length )
242 return( PSA_ERROR_INVALID_ARGUMENT );
243 payload_length = ciphertext_length - tag_length;
244 if( payload_length > plaintext_size )
245 return( PSA_ERROR_BUFFER_TOO_SMALL );
246 *p_tag = ciphertext + payload_length;
247 return( PSA_SUCCESS );
248}
249
250psa_status_t mbedtls_psa_aead_decrypt(
251 const psa_key_attributes_t *attributes,
252 const uint8_t *key_buffer, size_t key_buffer_size,
253 psa_algorithm_t alg,
254 const uint8_t *nonce, size_t nonce_length,
255 const uint8_t *additional_data, size_t additional_data_length,
256 const uint8_t *ciphertext, size_t ciphertext_length,
257 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
258{
259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100260 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100261 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100262
Paul Elliottcc358592021-05-12 12:22:28 +0100263 status = psa_aead_setup( &operation, attributes, key_buffer,
264 key_buffer_size, alg );
265
Ronald Cron46f91782021-03-17 08:16:34 +0100266 if( status != PSA_SUCCESS )
267 goto exit;
268
269 status = psa_aead_unpadded_locate_tag( operation.tag_length,
270 ciphertext, ciphertext_length,
271 plaintext_size, &tag );
272 if( status != PSA_SUCCESS )
273 goto exit;
274
Ronald Cron46f91782021-03-17 08:16:34 +0100275#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100276 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100277 {
278 status = mbedtls_to_psa_error(
279 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
280 ciphertext_length - operation.tag_length,
281 nonce, nonce_length,
282 additional_data,
283 additional_data_length,
284 ciphertext, plaintext,
285 tag, operation.tag_length ) );
286 }
287 else
288#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200289#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100290 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200291 {
292 status = mbedtls_to_psa_error(
293 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
294 ciphertext_length - operation.tag_length,
295 nonce, nonce_length,
296 additional_data,
297 additional_data_length,
298 tag, operation.tag_length,
299 ciphertext, plaintext ) );
300 }
301 else
302#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100303#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100304 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100305 {
306 if( nonce_length != 12 || operation.tag_length != 16 )
307 {
308 status = PSA_ERROR_NOT_SUPPORTED;
309 goto exit;
310 }
311 status = mbedtls_to_psa_error(
312 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
313 ciphertext_length - operation.tag_length,
314 nonce,
315 additional_data,
316 additional_data_length,
317 tag,
318 ciphertext,
319 plaintext ) );
320 }
321 else
322#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
323 {
324 return( PSA_ERROR_NOT_SUPPORTED );
325 }
326
327 if( status == PSA_SUCCESS )
328 *plaintext_length = ciphertext_length - operation.tag_length;
329
330exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100331 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100332
333 if( status == PSA_SUCCESS )
334 *plaintext_length = ciphertext_length - operation.tag_length;
335 return( status );
336}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100337
Paul Elliottadb8b162021-04-20 16:06:57 +0100338/* Set the key and algorithm for a multipart authenticated encryption
339 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100340psa_status_t mbedtls_psa_aead_encrypt_setup(
341 mbedtls_psa_aead_operation_t *operation,
342 const psa_key_attributes_t *attributes,
343 const uint8_t *key_buffer,
344 size_t key_buffer_size,
345 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100346{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100348
Paul Elliottcc358592021-05-12 12:22:28 +0100349 status = psa_aead_setup( operation, attributes, key_buffer,
350 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100351
352 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100353 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100354
355 return ( status );
356}
357
358/* Set the key and algorithm for a multipart authenticated decryption
359 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100360psa_status_t mbedtls_psa_aead_decrypt_setup(
361 mbedtls_psa_aead_operation_t *operation,
362 const psa_key_attributes_t *attributes,
363 const uint8_t *key_buffer,
364 size_t key_buffer_size,
365 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100366{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100368
369 (void) key_buffer_size;
370
Paul Elliottcc358592021-05-12 12:22:28 +0100371 status = psa_aead_setup( operation, attributes, key_buffer,
372 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100373
374 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100375 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100376
377 return ( status );
378}
379
Paul Elliottadb8b162021-04-20 16:06:57 +0100380/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100381psa_status_t mbedtls_psa_aead_set_nonce(
382 mbedtls_psa_aead_operation_t *operation,
383 const uint8_t *nonce,
384 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100385{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100387
Paul Elliottadb8b162021-04-20 16:06:57 +0100388 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
389 if( operation->alg == PSA_ALG_GCM )
390 {
391 /* GCM sets nonce once additional data has been supplied */
Paul Elliotte9eeea32021-05-19 14:32:58 +0100392 memcpy( operation->nonce, nonce, nonce_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100393
394 /* We know that nonce size cannot exceed the uint8_t size */
395 operation->nonce_length = ( uint8_t ) nonce_length;
396 status = PSA_SUCCESS;
397 }
398 else
399#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
400#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
401 if( operation->alg == PSA_ALG_CCM )
402 {
403 /* Multipart CCM not supported as yet, so CCM is basically operating
404 in oneshot mode. Store the nonce as we need this later */
Paul Elliott80acb7e2021-05-12 12:41:33 +0100405 memcpy( operation->nonce, nonce, nonce_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100406
407 /* We know that nonce size cannot exceed the uint8_t size */
408 operation->nonce_length = ( uint8_t ) nonce_length;
409 status = PSA_SUCCESS;
410 }
411 else
412#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
413#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
414 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
415 {
416 if( nonce_length != 12 && nonce_length != 8)
417 {
418 return( PSA_ERROR_INVALID_ARGUMENT );
419 }
420
Paul Elliott2df40052021-05-07 17:52:18 +0100421 status = mbedtls_to_psa_error(
422 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
423 nonce,
424 operation->is_encrypt ?
425 MBEDTLS_CHACHAPOLY_ENCRYPT :
426 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100427 }
428 else
429#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
430 {
431 ( void ) nonce;
432 ( void ) nonce_length;
433
434 return ( PSA_ERROR_NOT_SUPPORTED );
435 }
436
Paul Elliottadb8b162021-04-20 16:06:57 +0100437 return( status );
438}
439 /* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100440psa_status_t mbedtls_psa_aead_set_lengths(
441 mbedtls_psa_aead_operation_t *operation,
442 size_t ad_length,
443 size_t plaintext_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100444{
445
Paul Elliottadb8b162021-04-20 16:06:57 +0100446#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
447 if( operation->alg == PSA_ALG_GCM )
448 {
Paul Elliottef29e172021-05-10 19:33:03 +0100449 /* Lengths can only be too large for GCM if size_t is bigger than 32
Paul Elliotte9eeea32021-05-19 14:32:58 +0100450 * bits. Without the guard this code will generate warnings on 32bit
451 builds */
Paul Elliottadb8b162021-04-20 16:06:57 +0100452#if SIZE_MAX > UINT32_MAX
453 if( ( (uint64_t) ad_length ) >> 61 != 0 ||
454 ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull )
455 {
456 return ( PSA_ERROR_INVALID_ARGUMENT );
457 }
458#endif
459 }
460 else
461#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
462#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
463 if( operation->alg == PSA_ALG_CCM )
464 {
465 if( ad_length > 0xFF00 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100466 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100467 }
468 else
469#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
470#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
471 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
472 {
473 /* No length restrictions for ChaChaPoly. */
474 }
475 else
476#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
477 {
478 ( void ) ad_length;
479 ( void ) plaintext_length;
480
481 return ( PSA_ERROR_NOT_SUPPORTED );
482 }
483
484 operation->ad_remaining = ad_length;
485 operation->body_remaining = plaintext_length;
486 operation->lengths_set = 1;
487
488 return ( PSA_SUCCESS );
489}
490
491/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100492psa_status_t mbedtls_psa_aead_update_ad(
493 mbedtls_psa_aead_operation_t *operation,
494 const uint8_t *input,
495 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100496{
497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
498
Paul Elliottadb8b162021-04-20 16:06:57 +0100499 if( operation->lengths_set )
500 {
501 if ( operation->ad_remaining < input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100502 return( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100503
504 operation->ad_remaining -= input_length;
505 }
506
507#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
508 if( operation->alg == PSA_ALG_GCM )
509 {
Paul Elliotte9eeea32021-05-19 14:32:58 +0100510 /* GCM currently requires all the additional data to be passed in
511 * in one contiguous buffer, so until that is re-done, we have to
512 * enforce this, as we cannot allocate a buffer to collate multiple
513 * calls into. */
Paul Elliottc10ad212021-05-13 17:08:29 +0100514 if( operation->ad_started )
515 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100516
Paul Elliott2df40052021-05-07 17:52:18 +0100517 status = mbedtls_to_psa_error(
518 mbedtls_gcm_starts( &operation->ctx.gcm,
519 operation->is_encrypt ?
520 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
521 operation->nonce,
522 operation->nonce_length,
523 input,
524 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100525
526 }
527 else
528#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
529#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
530 if( operation->alg == PSA_ALG_CCM )
531 {
532 /* CCM requires all additional data to be passed in in one go at the
533 minute, as we are basically operating in oneshot mode. */
Paul Elliott72c10082021-04-23 19:02:16 +0100534 if( operation->ad_started )
Paul Elliottc10ad212021-05-13 17:08:29 +0100535 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100536
537 /* Save the additional data for later, this will be passed in
538 when we have the body. */
Paul Elliott6108ee72021-05-13 18:26:41 +0100539 operation->ad_buffer = ( uint8_t * ) mbedtls_calloc( 1, input_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100540
Paul Elliott6108ee72021-05-13 18:26:41 +0100541 if( operation->ad_buffer == NULL )
Paul Elliott6108ee72021-05-13 18:26:41 +0100542 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Paul Elliott6108ee72021-05-13 18:26:41 +0100543
544 memcpy( operation->ad_buffer, input, input_length );
545 operation->ad_length = input_length;
546 status = PSA_SUCCESS;
Paul Elliottadb8b162021-04-20 16:06:57 +0100547 }
548 else
549#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
550#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
551 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
552 {
Paul Elliott2df40052021-05-07 17:52:18 +0100553 status = mbedtls_to_psa_error(
554 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
555 input,
556 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100557 }
558 else
559#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
560 {
561 (void) input;
562 (void) input_length;
563
564 return ( PSA_ERROR_NOT_SUPPORTED );
565 }
566
567 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100568 operation->ad_started = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100569
570 return ( status );
571}
572
573/* Encrypt or decrypt a message fragment in an active multipart AEAD
574 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100575psa_status_t mbedtls_psa_aead_update(
576 mbedtls_psa_aead_operation_t *operation,
577 const uint8_t *input,
578 size_t input_length,
579 uint8_t *output,
580 size_t output_size,
581 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100582{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100583 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100584 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100586
Paul Elliotte2c788d2021-05-13 17:16:01 +0100587 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100588
Paul Elliott72c10082021-04-23 19:02:16 +0100589 if( PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, operation->alg,
590 input_length ) > output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100591 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100592
593 if( operation->lengths_set)
594 {
595 /* Additional data length was supplied, but not all the additional
596 data was supplied.*/
597 if( operation->ad_remaining != 0 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100598 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100599
600 /* Too much data provided. */
601 if( operation->body_remaining < input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100602 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100603
604 operation->body_remaining -= input_length;
605 }
606
607#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
608 if( operation->alg == PSA_ALG_GCM )
609 {
610 /* For the time being set the requirement that all of the body data
611 * must be passed in in one update, rather than deal with the complexity
612 * of non block size aligned updates. This will be fixed in 3.0 when
613 we can change the signature of the GCM multipart functions */
Paul Elliottc10ad212021-05-13 17:08:29 +0100614 if( operation->body_started )
615 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100616
Paul Elliottadb8b162021-04-20 16:06:57 +0100617
618 status = mbedtls_to_psa_error( mbedtls_gcm_update( &operation->ctx.gcm,
619 input_length,
620 input,
621 output ) );
622 }
623 else
624#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
625#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
626 if( operation->alg == PSA_ALG_CCM )
627 {
Paul Elliott80acb7e2021-05-12 12:41:33 +0100628 /* CCM does not support multipart yet, so all the input has to be
Paul Elliottfd3ca242021-04-25 18:10:42 +0100629 passed in in one go. */
Paul Elliott72c10082021-04-23 19:02:16 +0100630 if( operation->body_started )
Paul Elliottc10ad212021-05-13 17:08:29 +0100631 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100632
Paul Elliottfd3ca242021-04-25 18:10:42 +0100633 /* Need to store tag for Finish() / Verify() */
Paul Elliott2df40052021-05-07 17:52:18 +0100634 operation->tag_buffer =
Paul Elliott80acb7e2021-05-12 12:41:33 +0100635 ( uint8_t * ) mbedtls_calloc( 1, operation->tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100636
Paul Elliott6108ee72021-05-13 18:26:41 +0100637 if( operation->tag_buffer == NULL)
Paul Elliottadb8b162021-04-20 16:06:57 +0100638 {
Paul Elliott6108ee72021-05-13 18:26:41 +0100639 return( PSA_ERROR_INSUFFICIENT_MEMORY );
640 }
Paul Elliottfd3ca242021-04-25 18:10:42 +0100641
Paul Elliott6108ee72021-05-13 18:26:41 +0100642 if( operation->is_encrypt )
643 {
644 /* Perform oneshot CCM encryption with additional data already
645 stored, as CCM does not support multipart yet.*/
646 status = mbedtls_to_psa_error(
647 mbedtls_ccm_encrypt_and_tag( &operation->ctx.ccm,
648 input_length,
649 operation->nonce,
650 operation->nonce_length,
651 operation->ad_buffer,
652 operation->ad_length,
653 input,
654 output,
655 operation->tag_buffer,
656 operation->tag_length ) );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100657
Paul Elliottadb8b162021-04-20 16:06:57 +0100658 }
659 else
Paul Elliott6108ee72021-05-13 18:26:41 +0100660 {
661 /* Need to back up the body data so we can do this again
662 later.*/
663 operation->body_buffer =
664 ( uint8_t * ) mbedtls_calloc(1, input_length );
665
666 if( operation->body_buffer == NULL)
Paul Elliott6108ee72021-05-13 18:26:41 +0100667 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Paul Elliott6108ee72021-05-13 18:26:41 +0100668
669 memcpy( operation->body_buffer, input, input_length );
670 operation->body_length = input_length;
671
672 /* this will fail, as the tag is clearly false, but will
673 write the decrypted data to the output buffer.*/
674 ret = mbedtls_ccm_auth_decrypt( &operation->ctx.ccm,
675 input_length,
676 operation->nonce,
677 operation->nonce_length,
678 operation->ad_buffer,
679 operation->ad_length,
680 input, output,
681 operation->tag_buffer,
682 operation->tag_length );
683
684 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
685 status = PSA_SUCCESS;
686 else
687 status = mbedtls_to_psa_error( ret );
688 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100689 }
690 else
691#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
692#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
693 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
694 {
Paul Elliott2df40052021-05-07 17:52:18 +0100695 status = mbedtls_to_psa_error(
696 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
697 input_length,
698 input,
699 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100700 }
701 else
702#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
703 {
704 (void) input;
705 (void) input_length;
706
707 return ( PSA_ERROR_NOT_SUPPORTED );
708 }
709
710 if( status == PSA_SUCCESS )
711 {
Paul Elliotte2c788d2021-05-13 17:16:01 +0100712 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100713 operation->body_started = 1;
714 }
715
716 return( status );
717}
718
719/* Common checks for both mbedtls_psa_aead_finish() and
720 mbedtls_psa_aead_verify() */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100721static psa_status_t mbedtls_psa_aead_finish_checks(
722 mbedtls_psa_aead_operation_t *operation,
723 size_t output_size,
724 size_t tag_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100725{
Paul Elliottfd3ca242021-04-25 18:10:42 +0100726 size_t finish_output_size;
727
Paul Elliottadb8b162021-04-20 16:06:57 +0100728 if( operation->lengths_set )
Paul Elliottadb8b162021-04-20 16:06:57 +0100729 if( operation->ad_remaining != 0 || operation->body_remaining != 0 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100730 return( PSA_ERROR_BAD_STATE );
Paul Elliottadb8b162021-04-20 16:06:57 +0100731
Paul Elliottfd3ca242021-04-25 18:10:42 +0100732 if( tag_size < operation->tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100733 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100734
Paul Elliott80acb7e2021-05-12 12:41:33 +0100735 finish_output_size = operation->is_encrypt ?
736 PSA_AEAD_FINISH_OUTPUT_SIZE( operation->key_type, operation->alg ) :
737 PSA_AEAD_VERIFY_OUTPUT_SIZE( operation->key_type, operation->alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100738
Paul Elliottfd3ca242021-04-25 18:10:42 +0100739 if( output_size < finish_output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100740 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100741
742 return ( PSA_SUCCESS );
Paul Elliottadb8b162021-04-20 16:06:57 +0100743}
744
745/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100746psa_status_t mbedtls_psa_aead_finish(
747 mbedtls_psa_aead_operation_t *operation,
748 uint8_t *ciphertext,
749 size_t ciphertext_size,
750 size_t *ciphertext_length,
751 uint8_t *tag,
752 size_t tag_size,
753 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100754{
755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100756 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100757
Paul Elliott2df40052021-05-07 17:52:18 +0100758 status = mbedtls_psa_aead_finish_checks( operation, ciphertext_size,
759 tag_size );
Paul Elliottadb8b162021-04-20 16:06:57 +0100760
761 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100762 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100763
764#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
765 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100766 /* We will need to do final GCM pass in here when multipart is done. */
767 status = mbedtls_to_psa_error( mbedtls_gcm_finish( &operation->ctx.gcm,
768 tag,
769 tag_size ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100770 else
771#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
772#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
773 if( operation->alg == PSA_ALG_CCM )
774 {
Paul Elliottfd3ca242021-04-25 18:10:42 +0100775 /* Copy the previously generated tag into place */
776 memcpy( tag, operation->tag_buffer, operation->tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100777
Paul Elliottfd3ca242021-04-25 18:10:42 +0100778 status = PSA_SUCCESS;
Paul Elliottadb8b162021-04-20 16:06:57 +0100779 }
780 else
781#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
782#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
783 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliott2df40052021-05-07 17:52:18 +0100784 status = mbedtls_to_psa_error(
785 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
786 tag ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100787 else
788#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
789 {
790 ( void ) ciphertext;
791 ( void ) ciphertext_size;
792 ( void ) ciphertext_length;
793 ( void ) tag;
794 ( void ) tag_size;
795 ( void ) tag_length;
796
797 return ( PSA_ERROR_NOT_SUPPORTED );
798 }
799
800 if( status == PSA_SUCCESS )
801 {
802 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100803 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100804 }
805
Paul Elliottadb8b162021-04-20 16:06:57 +0100806 return ( status );
807}
808
809/* Finish authenticating and decrypting a message in a multipart AEAD
810 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100811psa_status_t mbedtls_psa_aead_verify(
812 mbedtls_psa_aead_operation_t *operation,
813 uint8_t *plaintext,
814 size_t plaintext_size,
815 size_t *plaintext_length,
816 const uint8_t *tag,
817 size_t tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100818{
819 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
820 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
821
Paul Elliottfd3ca242021-04-25 18:10:42 +0100822 uint8_t * temp_buffer;
823 size_t temp_buffer_size;
824
825 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100826
827 int do_tag_check = 1;
Paul Elliottccaea402021-05-13 14:22:52 +0100828 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottadb8b162021-04-20 16:06:57 +0100829
Paul Elliott2df40052021-05-07 17:52:18 +0100830 status = mbedtls_psa_aead_finish_checks( operation, plaintext_size,
831 tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100832
833 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100834 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100835
836#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
837 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100838 /* Call finish to get the tag for comparison */
Paul Elliott2df40052021-05-07 17:52:18 +0100839 status = mbedtls_to_psa_error(
840 mbedtls_gcm_finish( &operation->ctx.gcm,
841 check_tag,
842 operation->tag_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100843 else
844#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
845#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
846 if( operation->alg == PSA_ALG_CCM )
847 {
Paul Elliottfd3ca242021-04-25 18:10:42 +0100848 /* Perform oneshot CCM decryption *again*, as its the
849 * only way to get the tag, but this time throw away the
850 results, as verify cannot write that much data. */
851 temp_buffer_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type,
Paul Elliott2df40052021-05-07 17:52:18 +0100852 operation->alg,
853 operation->body_length
854 );
Paul Elliottadb8b162021-04-20 16:06:57 +0100855
Paul Elliottfd3ca242021-04-25 18:10:42 +0100856 temp_buffer = ( uint8_t * ) mbedtls_calloc(1, temp_buffer_size );
Paul Elliottadb8b162021-04-20 16:06:57 +0100857
Paul Elliott6108ee72021-05-13 18:26:41 +0100858 if( temp_buffer == NULL)
Paul Elliott6108ee72021-05-13 18:26:41 +0100859 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Paul Elliott6108ee72021-05-13 18:26:41 +0100860
861 ret = mbedtls_ccm_auth_decrypt( &operation->ctx.ccm,
862 operation->body_length,
863 operation->nonce,
864 operation->nonce_length,
865 operation->ad_buffer,
866 operation->ad_length,
867 operation->body_buffer,
868 temp_buffer, tag, tag_length );
869
870 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
871 status = PSA_ERROR_INVALID_SIGNATURE;
Paul Elliottadb8b162021-04-20 16:06:57 +0100872 else
Paul Elliott6108ee72021-05-13 18:26:41 +0100873 {
874 status = mbedtls_to_psa_error( ret );
875 do_tag_check = 0;
876 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100877
878 /* Even if the above operation fails, we no longer need the data */
Paul Elliotte9eeea32021-05-19 14:32:58 +0100879 mbedtls_free( temp_buffer );
Paul Elliottadb8b162021-04-20 16:06:57 +0100880 }
881 else
882#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
883#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
884 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100885 // call finish to get the tag for comparison.
Paul Elliott2df40052021-05-07 17:52:18 +0100886 status = mbedtls_to_psa_error(
887 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
888 check_tag ) );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100889
Paul Elliottadb8b162021-04-20 16:06:57 +0100890 else
891#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
892 {
893 ( void ) plaintext;
894 ( void ) plaintext_size;
895 ( void ) plaintext_length;
896 ( void ) tag;
897 ( void ) tag_length;
898
899 return ( PSA_ERROR_NOT_SUPPORTED );
900 }
901
902 if( status == PSA_SUCCESS )
903 {
Paul Elliott72c10082021-04-23 19:02:16 +0100904 *plaintext_length = finish_output_size;
905
Paul Elliott6edb7472021-05-10 19:29:35 +0100906 if( do_tag_check &&
907 mbedtls_psa_safer_memcmp(tag, check_tag, tag_length) != 0 )
Paul Elliott811d8d42021-04-22 11:31:14 +0100908 status = PSA_ERROR_INVALID_SIGNATURE;
Paul Elliottadb8b162021-04-20 16:06:57 +0100909 }
910
Paul Elliottadb8b162021-04-20 16:06:57 +0100911 return ( status );
912}
913
914/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100915psa_status_t mbedtls_psa_aead_abort(
916 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100917{
Paul Elliott811d8d42021-04-22 11:31:14 +0100918 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100919 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100920#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
921 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100922 mbedtls_ccm_free( &operation->ctx.ccm );
923 break;
924#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
925#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
926 case PSA_ALG_GCM:
927 mbedtls_gcm_free( &operation->ctx.gcm );
928 break;
929#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
930#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100931 case PSA_ALG_CHACHA20_POLY1305:
932 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
933 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100934#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
935 }
936
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100937 operation->lengths_set = 0;
938 operation->is_encrypt = 0;
939 operation->ad_started = 0;
940 operation->body_started = 0;
941
Paul Elliott80acb7e2021-05-12 12:41:33 +0100942 mbedtls_free( operation->ad_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100943 operation->ad_buffer = NULL;
944 operation->ad_length = 0;
945
Paul Elliott80acb7e2021-05-12 12:41:33 +0100946 mbedtls_free( operation->body_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100947 operation->body_buffer = NULL;
948 operation->body_length = 0;
949
Paul Elliott80acb7e2021-05-12 12:41:33 +0100950 mbedtls_free( operation->tag_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100951 operation->tag_buffer = NULL;
952
Paul Elliottadb8b162021-04-20 16:06:57 +0100953 return( PSA_SUCCESS );
954}
955
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100956#endif /* MBEDTLS_PSA_CRYPTO_C */
957