blob: d877638ecf0baf00bbf12c94bf4aa49bfb4894c1 [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 )
Paul Elliott99f548d2021-07-22 18:03:50 +0100299 != PSA_SUCCESS )
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100300 {
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 )
Paul Elliott99f548d2021-07-22 18:03:50 +0100431 != PSA_SUCCESS )
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100432 {
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 Elliottadb8b162021-04-20 16:06:57 +0100571#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
572 if( operation->alg == PSA_ALG_GCM )
573 {
Paul Elliottecce9012021-07-23 15:44:11 +0100574 if( output_size < input_length )
575 return( PSA_ERROR_BUFFER_TOO_SMALL );
576
Paul Elliott83f09ef2021-05-21 19:28:26 +0100577 status = mbedtls_to_psa_error(
578 mbedtls_gcm_update( &operation->ctx.gcm,
579 input, input_length,
580 output, output_size,
581 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100582 }
583 else
584#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100585#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
586 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
587 {
Paul Elliottecce9012021-07-23 15:44:11 +0100588 if( output_size < input_length )
589 return( PSA_ERROR_BUFFER_TOO_SMALL );
590
Paul Elliott2df40052021-05-07 17:52:18 +0100591 status = mbedtls_to_psa_error(
592 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
593 input_length,
594 input,
595 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100596 }
597 else
598#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
599 {
Paul Elliottbc949782021-06-03 15:29:00 +0100600 ( void ) input;
601 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100602
603 return ( PSA_ERROR_NOT_SUPPORTED );
604 }
605
606 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100607 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100608
609 return( status );
610}
611
Paul Elliottadb8b162021-04-20 16:06:57 +0100612/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100613psa_status_t mbedtls_psa_aead_finish(
614 mbedtls_psa_aead_operation_t *operation,
615 uint8_t *ciphertext,
616 size_t ciphertext_size,
617 size_t *ciphertext_length,
618 uint8_t *tag,
619 size_t tag_size,
620 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100621{
622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100623 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100624
Paul Elliott315628d2021-07-20 18:25:54 +0100625 if( tag_size < operation->tag_length )
626 return( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100627
628#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
629 if( operation->alg == PSA_ALG_GCM )
Paul Elliottecce9012021-07-23 15:44:11 +0100630 {
631 if( ciphertext_size < 15 )
632 return( PSA_ERROR_BUFFER_TOO_SMALL );
633
Paul Elliott83f09ef2021-05-21 19:28:26 +0100634 status = mbedtls_to_psa_error(
635 mbedtls_gcm_finish( &operation->ctx.gcm,
636 ciphertext, ciphertext_size,
Paul Elliott2fe5db82021-07-22 18:10:43 +0100637 tag, operation->tag_length ) );
Paul Elliottecce9012021-07-23 15:44:11 +0100638 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100639 else
640#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100641#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
642 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliotted08cf82021-07-22 18:48:24 +0100643 {
644 /* Belt and braces. Although the above tag_size check should have
645 * already done this, if we later start supporting smaller tag sizes
646 * for chachapoly, then passing a tag buffer smaller than 16 into here
647 * could cause a buffer overflow, so better safe than sorry. */
648 if( tag_size < 16 )
649 return( PSA_ERROR_BUFFER_TOO_SMALL );
650
Paul Elliott2df40052021-05-07 17:52:18 +0100651 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100652 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
653 tag ) );
Paul Elliotted08cf82021-07-22 18:48:24 +0100654 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100655 else
656#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
657 {
658 ( void ) ciphertext;
659 ( void ) ciphertext_size;
660 ( void ) ciphertext_length;
661 ( void ) tag;
662 ( void ) tag_size;
663 ( void ) tag_length;
664
665 return ( PSA_ERROR_NOT_SUPPORTED );
666 }
667
668 if( status == PSA_SUCCESS )
669 {
670 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100671 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100672 }
673
Paul Elliottadb8b162021-04-20 16:06:57 +0100674 return ( status );
675}
676
Paul Elliottadb8b162021-04-20 16:06:57 +0100677/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100678psa_status_t mbedtls_psa_aead_abort(
679 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100680{
Paul Elliott811d8d42021-04-22 11:31:14 +0100681 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100682 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100683#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
684 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100685 mbedtls_ccm_free( &operation->ctx.ccm );
686 break;
687#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
688#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
689 case PSA_ALG_GCM:
690 mbedtls_gcm_free( &operation->ctx.gcm );
691 break;
692#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
693#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100694 case PSA_ALG_CHACHA20_POLY1305:
695 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
696 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100697#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
698 }
699
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100700 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100701
Paul Elliottadb8b162021-04-20 16:06:57 +0100702 return( PSA_SUCCESS );
703}
704
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100705#endif /* MBEDTLS_PSA_CRYPTO_C */
706