blob: 9ac26467f11bf67662cdf5eefd99b84de10dbc89 [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
Paul Elliott96b01732021-07-16 17:00:26 +0100139/* Perform common nonce length checks */
140static psa_status_t mbedtls_aead_check_nonce_length(
141 mbedtls_psa_aead_operation_t *operation,
142 size_t nonce_length )
143{
144#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
145 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
146 {
147 if( nonce_length != 12 )
148 return( PSA_ERROR_NOT_SUPPORTED );
149 }
150#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
151
152 return PSA_SUCCESS;
153}
154
Ronald Cron46f91782021-03-17 08:16:34 +0100155psa_status_t mbedtls_psa_aead_encrypt(
156 const psa_key_attributes_t *attributes,
157 const uint8_t *key_buffer, size_t key_buffer_size,
158 psa_algorithm_t alg,
159 const uint8_t *nonce, size_t nonce_length,
160 const uint8_t *additional_data, size_t additional_data_length,
161 const uint8_t *plaintext, size_t plaintext_length,
162 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
163{
164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100165 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100166 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100167
Paul Elliottcc358592021-05-12 12:22:28 +0100168 status = psa_aead_setup( &operation, attributes, key_buffer,
169 key_buffer_size, alg );
170
Ronald Cron46f91782021-03-17 08:16:34 +0100171 if( status != PSA_SUCCESS )
172 goto exit;
173
174 /* For all currently supported modes, the tag is at the end of the
175 * ciphertext. */
176 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
177 {
178 status = PSA_ERROR_BUFFER_TOO_SMALL;
179 goto exit;
180 }
181 tag = ciphertext + plaintext_length;
182
Paul Elliott96b01732021-07-16 17:00:26 +0100183 if( mbedtls_aead_check_nonce_length( &operation, nonce_length )
184 != PSA_SUCCESS )
185 {
186 status = PSA_ERROR_NOT_SUPPORTED;
187 goto exit;
188 }
189
Ronald Cron46f91782021-03-17 08:16:34 +0100190#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100191 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100192 {
193 status = mbedtls_to_psa_error(
194 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
195 plaintext_length,
196 nonce, nonce_length,
197 additional_data,
198 additional_data_length,
199 plaintext, ciphertext,
200 tag, operation.tag_length ) );
201 }
202 else
203#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200204#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100205 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200206 {
207 status = mbedtls_to_psa_error(
208 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
209 MBEDTLS_GCM_ENCRYPT,
210 plaintext_length,
211 nonce, nonce_length,
212 additional_data, additional_data_length,
213 plaintext, ciphertext,
214 operation.tag_length, tag ) );
215 }
216 else
217#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100218#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100219 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100220 {
Paul Elliott96b01732021-07-16 17:00:26 +0100221 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100222 {
223 status = PSA_ERROR_NOT_SUPPORTED;
224 goto exit;
225 }
226 status = mbedtls_to_psa_error(
227 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
228 plaintext_length,
229 nonce,
230 additional_data,
231 additional_data_length,
232 plaintext,
233 ciphertext,
234 tag ) );
235 }
236 else
237#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
238 {
239 (void) tag;
240 return( PSA_ERROR_NOT_SUPPORTED );
241 }
242
243 if( status == PSA_SUCCESS )
244 *ciphertext_length = plaintext_length + operation.tag_length;
245
246exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100247 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100248
249 return( status );
250}
251
252/* Locate the tag in a ciphertext buffer containing the encrypted data
253 * followed by the tag. Return the length of the part preceding the tag in
254 * *plaintext_length. This is the size of the plaintext in modes where
255 * the encrypted data has the same size as the plaintext, such as
256 * CCM and GCM. */
257static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
258 const uint8_t *ciphertext,
259 size_t ciphertext_length,
260 size_t plaintext_size,
261 const uint8_t **p_tag )
262{
263 size_t payload_length;
264 if( tag_length > ciphertext_length )
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 payload_length = ciphertext_length - tag_length;
267 if( payload_length > plaintext_size )
268 return( PSA_ERROR_BUFFER_TOO_SMALL );
269 *p_tag = ciphertext + payload_length;
270 return( PSA_SUCCESS );
271}
272
273psa_status_t mbedtls_psa_aead_decrypt(
274 const psa_key_attributes_t *attributes,
275 const uint8_t *key_buffer, size_t key_buffer_size,
276 psa_algorithm_t alg,
277 const uint8_t *nonce, size_t nonce_length,
278 const uint8_t *additional_data, size_t additional_data_length,
279 const uint8_t *ciphertext, size_t ciphertext_length,
280 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
281{
282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100283 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100284 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100285
Paul Elliottcc358592021-05-12 12:22:28 +0100286 status = psa_aead_setup( &operation, attributes, key_buffer,
287 key_buffer_size, alg );
288
Ronald Cron46f91782021-03-17 08:16:34 +0100289 if( status != PSA_SUCCESS )
290 goto exit;
291
292 status = psa_aead_unpadded_locate_tag( operation.tag_length,
293 ciphertext, ciphertext_length,
294 plaintext_size, &tag );
295 if( status != PSA_SUCCESS )
296 goto exit;
297
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100298 if( mbedtls_aead_check_nonce_length( &operation, nonce_length )
299 != PSA_SUCCESS)
300 {
301 status = PSA_ERROR_NOT_SUPPORTED;
302 goto exit;
303 }
304
Ronald Cron46f91782021-03-17 08:16:34 +0100305#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100306 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100307 {
308 status = mbedtls_to_psa_error(
309 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
310 ciphertext_length - operation.tag_length,
311 nonce, nonce_length,
312 additional_data,
313 additional_data_length,
314 ciphertext, plaintext,
315 tag, operation.tag_length ) );
316 }
317 else
318#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200319#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100320 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200321 {
322 status = mbedtls_to_psa_error(
323 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
324 ciphertext_length - operation.tag_length,
325 nonce, nonce_length,
326 additional_data,
327 additional_data_length,
328 tag, operation.tag_length,
329 ciphertext, plaintext ) );
330 }
331 else
332#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100333#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100334 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100335 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100336 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100337 {
338 status = PSA_ERROR_NOT_SUPPORTED;
339 goto exit;
340 }
341 status = mbedtls_to_psa_error(
342 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
343 ciphertext_length - operation.tag_length,
344 nonce,
345 additional_data,
346 additional_data_length,
347 tag,
348 ciphertext,
349 plaintext ) );
350 }
351 else
352#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
353 {
354 return( PSA_ERROR_NOT_SUPPORTED );
355 }
356
357 if( status == PSA_SUCCESS )
358 *plaintext_length = ciphertext_length - operation.tag_length;
359
360exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100361 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100362
363 if( status == PSA_SUCCESS )
364 *plaintext_length = ciphertext_length - operation.tag_length;
365 return( status );
366}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100367
Paul Elliottadb8b162021-04-20 16:06:57 +0100368/* Set the key and algorithm for a multipart authenticated encryption
369 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100370psa_status_t mbedtls_psa_aead_encrypt_setup(
371 mbedtls_psa_aead_operation_t *operation,
372 const psa_key_attributes_t *attributes,
373 const uint8_t *key_buffer,
374 size_t key_buffer_size,
375 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100376{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100378
Paul Elliott60aa2032021-05-20 18:57:02 +0100379#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
380 if( operation->alg == PSA_ALG_CCM )
381 {
Paul Elliotte95259f2021-05-21 17:09:21 +0100382 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliott60aa2032021-05-20 18:57:02 +0100383 }
384#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
385
Paul Elliottcc358592021-05-12 12:22:28 +0100386 status = psa_aead_setup( operation, attributes, key_buffer,
387 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100388
389 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100390 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100391
392 return ( status );
393}
394
395/* Set the key and algorithm for a multipart authenticated decryption
396 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100397psa_status_t mbedtls_psa_aead_decrypt_setup(
398 mbedtls_psa_aead_operation_t *operation,
399 const psa_key_attributes_t *attributes,
400 const uint8_t *key_buffer,
401 size_t key_buffer_size,
402 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100403{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100405
Paul Elliotte95259f2021-05-21 17:09:21 +0100406#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott60aa2032021-05-20 18:57:02 +0100407 if( operation->alg == PSA_ALG_CCM )
408 {
Paul Elliotte95259f2021-05-21 17:09:21 +0100409 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliott60aa2032021-05-20 18:57:02 +0100410 }
411#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100412
Paul Elliottcc358592021-05-12 12:22:28 +0100413 status = psa_aead_setup( operation, attributes, key_buffer,
414 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100415
416 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100417 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100418
419 return ( status );
420}
421
Paul Elliottadb8b162021-04-20 16:06:57 +0100422/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100423psa_status_t mbedtls_psa_aead_set_nonce(
424 mbedtls_psa_aead_operation_t *operation,
425 const uint8_t *nonce,
426 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100427{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100429
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100430 if( mbedtls_aead_check_nonce_length( operation, nonce_length )
431 != PSA_SUCCESS)
432 {
433 return( PSA_ERROR_INVALID_ARGUMENT );
434 }
435
Paul Elliottbc949782021-06-03 15:29:00 +0100436#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100437 if( operation->alg == PSA_ALG_GCM )
438 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100439 status = mbedtls_to_psa_error(
440 mbedtls_gcm_starts( &operation->ctx.gcm,
441 operation->is_encrypt ?
442 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
443 nonce,
444 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100445 }
446 else
447#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100448#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
449 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
450 {
Paul Elliott2df40052021-05-07 17:52:18 +0100451 status = mbedtls_to_psa_error(
452 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
453 nonce,
454 operation->is_encrypt ?
455 MBEDTLS_CHACHAPOLY_ENCRYPT :
456 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100457 }
458 else
459#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
460 {
Paul Elliottbc949782021-06-03 15:29:00 +0100461 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100462 ( void ) nonce;
463 ( void ) nonce_length;
464
465 return ( PSA_ERROR_NOT_SUPPORTED );
466 }
467
Paul Elliottadb8b162021-04-20 16:06:57 +0100468 return( status );
469}
470 /* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100471psa_status_t mbedtls_psa_aead_set_lengths(
472 mbedtls_psa_aead_operation_t *operation,
473 size_t ad_length,
474 size_t plaintext_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100475{
476
Paul Elliottadb8b162021-04-20 16:06:57 +0100477#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
478 if( operation->alg == PSA_ALG_GCM )
479 {
Paul Elliottef29e172021-05-10 19:33:03 +0100480 /* Lengths can only be too large for GCM if size_t is bigger than 32
Paul Elliotte9eeea32021-05-19 14:32:58 +0100481 * bits. Without the guard this code will generate warnings on 32bit
Paul Elliotte95259f2021-05-21 17:09:21 +0100482 * builds */
Paul Elliottadb8b162021-04-20 16:06:57 +0100483#if SIZE_MAX > UINT32_MAX
484 if( ( (uint64_t) ad_length ) >> 61 != 0 ||
485 ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull )
486 {
487 return ( PSA_ERROR_INVALID_ARGUMENT );
488 }
489#endif
490 }
491 else
492#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
493#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
494 if( operation->alg == PSA_ALG_CCM )
495 {
496 if( ad_length > 0xFF00 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100497 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100498 }
499 else
500#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
501#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
502 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
503 {
504 /* No length restrictions for ChaChaPoly. */
505 }
506 else
507#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
508 {
Paul Elliottbc949782021-06-03 15:29:00 +0100509 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100510 ( void ) ad_length;
511 ( void ) plaintext_length;
512
513 return ( PSA_ERROR_NOT_SUPPORTED );
514 }
515
Paul Elliottadb8b162021-04-20 16:06:57 +0100516 return ( PSA_SUCCESS );
517}
518
519/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100520psa_status_t mbedtls_psa_aead_update_ad(
521 mbedtls_psa_aead_operation_t *operation,
522 const uint8_t *input,
523 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100524{
525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
526
Paul Elliottadb8b162021-04-20 16:06:57 +0100527#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
528 if( operation->alg == PSA_ALG_GCM )
529 {
Paul Elliott2df40052021-05-07 17:52:18 +0100530 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100531 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100532 }
533 else
534#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100535#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
536 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
537 {
Paul Elliott2df40052021-05-07 17:52:18 +0100538 status = mbedtls_to_psa_error(
539 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
540 input,
541 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100542 }
543 else
544#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
545 {
Paul Elliottbc949782021-06-03 15:29:00 +0100546 ( void ) operation;
547 ( void ) input;
548 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100549
550 return ( PSA_ERROR_NOT_SUPPORTED );
551 }
552
Paul Elliottadb8b162021-04-20 16:06:57 +0100553 return ( status );
554}
555
556/* Encrypt or decrypt a message fragment in an active multipart AEAD
557 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100558psa_status_t mbedtls_psa_aead_update(
559 mbedtls_psa_aead_operation_t *operation,
560 const uint8_t *input,
561 size_t input_length,
562 uint8_t *output,
563 size_t output_size,
564 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100565{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100566 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
568
Paul Elliotte2c788d2021-05-13 17:16:01 +0100569 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100570
Paul Elliott72c10082021-04-23 19:02:16 +0100571 if( PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, operation->alg,
572 input_length ) > output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100573 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100574
Paul Elliottadb8b162021-04-20 16:06:57 +0100575#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
576 if( operation->alg == PSA_ALG_GCM )
577 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100578 status = mbedtls_to_psa_error(
579 mbedtls_gcm_update( &operation->ctx.gcm,
580 input, input_length,
581 output, output_size,
582 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100583 }
584 else
585#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100586#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
587 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
588 {
Paul Elliott2df40052021-05-07 17:52:18 +0100589 status = mbedtls_to_psa_error(
590 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
591 input_length,
592 input,
593 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100594 }
595 else
596#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
597 {
Paul Elliottbc949782021-06-03 15:29:00 +0100598 ( void ) input;
599 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100600
601 return ( PSA_ERROR_NOT_SUPPORTED );
602 }
603
604 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100605 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100606
607 return( status );
608}
609
610/* Common checks for both mbedtls_psa_aead_finish() and
611 mbedtls_psa_aead_verify() */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100612static psa_status_t mbedtls_psa_aead_finish_checks(
613 mbedtls_psa_aead_operation_t *operation,
Paul Elliottbb8bf662021-05-19 17:29:42 +0100614 size_t tag_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100615{
Paul Elliottfd3ca242021-04-25 18:10:42 +0100616 if( tag_size < operation->tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100617 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100618
Paul Elliottadb8b162021-04-20 16:06:57 +0100619 return ( PSA_SUCCESS );
Paul Elliottadb8b162021-04-20 16:06:57 +0100620}
621
622/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100623psa_status_t mbedtls_psa_aead_finish(
624 mbedtls_psa_aead_operation_t *operation,
625 uint8_t *ciphertext,
626 size_t ciphertext_size,
627 size_t *ciphertext_length,
628 uint8_t *tag,
629 size_t tag_size,
630 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100631{
632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100633 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100634
Paul Elliotted68d742021-06-24 20:37:32 +0100635 status = mbedtls_psa_aead_finish_checks( operation, tag_size );
Paul Elliottadb8b162021-04-20 16:06:57 +0100636
637 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100638 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100639
640#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
641 if( operation->alg == PSA_ALG_GCM )
Paul Elliott83f09ef2021-05-21 19:28:26 +0100642 status = mbedtls_to_psa_error(
643 mbedtls_gcm_finish( &operation->ctx.gcm,
644 ciphertext, ciphertext_size,
645 tag, tag_size ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100646 else
647#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100648#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
649 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliott2df40052021-05-07 17:52:18 +0100650 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100651 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
652 tag ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100653 else
654#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
655 {
656 ( void ) ciphertext;
657 ( void ) ciphertext_size;
658 ( void ) ciphertext_length;
659 ( void ) tag;
660 ( void ) tag_size;
661 ( void ) tag_length;
662
663 return ( PSA_ERROR_NOT_SUPPORTED );
664 }
665
666 if( status == PSA_SUCCESS )
667 {
668 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100669 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100670 }
671
Paul Elliottadb8b162021-04-20 16:06:57 +0100672 return ( status );
673}
674
675/* Finish authenticating and decrypting a message in a multipart AEAD
676 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100677psa_status_t mbedtls_psa_aead_verify(
678 mbedtls_psa_aead_operation_t *operation,
679 uint8_t *plaintext,
680 size_t plaintext_size,
681 size_t *plaintext_length,
682 const uint8_t *tag,
683 size_t tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100684{
685 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100686 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100687 int do_tag_check = 1;
Paul Elliottccaea402021-05-13 14:22:52 +0100688 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottadb8b162021-04-20 16:06:57 +0100689
Paul Elliotted68d742021-06-24 20:37:32 +0100690 status = mbedtls_psa_aead_finish_checks( operation, tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100691
692 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100693 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100694
695#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
696 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100697 /* Call finish to get the tag for comparison */
Paul Elliott2df40052021-05-07 17:52:18 +0100698 status = mbedtls_to_psa_error(
699 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott83f09ef2021-05-21 19:28:26 +0100700 plaintext, plaintext_size,
701 check_tag, operation->tag_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100702 else
703#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100704#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
705 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100706 // call finish to get the tag for comparison.
Paul Elliott2df40052021-05-07 17:52:18 +0100707 status = mbedtls_to_psa_error(
708 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
709 check_tag ) );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100710
Paul Elliottadb8b162021-04-20 16:06:57 +0100711 else
712#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
713 {
714 ( void ) plaintext;
715 ( void ) plaintext_size;
716 ( void ) plaintext_length;
717 ( void ) tag;
718 ( void ) tag_length;
719
720 return ( PSA_ERROR_NOT_SUPPORTED );
721 }
722
723 if( status == PSA_SUCCESS )
724 {
Paul Elliott72c10082021-04-23 19:02:16 +0100725 *plaintext_length = finish_output_size;
726
Paul Elliott3a16e012021-05-21 18:03:15 +0100727 if( do_tag_check && ( tag_length != operation->tag_length ||
728 mbedtls_psa_safer_memcmp(tag, check_tag, tag_length) != 0 ) )
Paul Elliott811d8d42021-04-22 11:31:14 +0100729 status = PSA_ERROR_INVALID_SIGNATURE;
Paul Elliottadb8b162021-04-20 16:06:57 +0100730 }
731
Paul Elliottadb8b162021-04-20 16:06:57 +0100732 return ( status );
733}
734
735/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100736psa_status_t mbedtls_psa_aead_abort(
737 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100738{
Paul Elliott811d8d42021-04-22 11:31:14 +0100739 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100740 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100741#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
742 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100743 mbedtls_ccm_free( &operation->ctx.ccm );
744 break;
745#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
746#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
747 case PSA_ALG_GCM:
748 mbedtls_gcm_free( &operation->ctx.gcm );
749 break;
750#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
751#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100752 case PSA_ALG_CHACHA20_POLY1305:
753 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
754 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100755#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
756 }
757
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100758 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100759
Paul Elliottadb8b162021-04-20 16:06:57 +0100760 return( PSA_SUCCESS );
761}
762
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100763#endif /* MBEDTLS_PSA_CRYPTO_C */
764