blob: 48969b3a3763863fb852d442ef5befbe7e93b658 [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"
Paul Elliottadb8b162021-04-20 16:06:57 +010030
Ronald Cron46f91782021-03-17 08:16:34 +010031#include "mbedtls/ccm.h"
32#include "mbedtls/chachapoly.h"
33#include "mbedtls/cipher.h"
34#include "mbedtls/gcm.h"
Paul Elliottadb8b162021-04-20 16:06:57 +010035#include "mbedtls/error.h"
Ronald Cron46f91782021-03-17 08:16:34 +010036
Ronald Cron46f91782021-03-17 08:16:34 +010037static psa_status_t psa_aead_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010038 mbedtls_psa_aead_operation_t *operation,
Ronald Cron46f91782021-03-17 08:16:34 +010039 const psa_key_attributes_t *attributes,
40 const uint8_t *key_buffer,
Paul Elliottcc358592021-05-12 12:22:28 +010041 size_t key_buffer_size,
Ronald Cron46f91782021-03-17 08:16:34 +010042 psa_algorithm_t alg )
43{
44 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
45 size_t key_bits;
Ronald Cronecbc0682021-03-26 13:25:17 +010046 const mbedtls_cipher_info_t *cipher_info;
Ronald Cron46f91782021-03-17 08:16:34 +010047 mbedtls_cipher_id_t cipher_id;
48
Paul Elliottcc358592021-05-12 12:22:28 +010049 ( void ) key_buffer_size;
50
Ronald Cron46f91782021-03-17 08:16:34 +010051 key_bits = attributes->core.bits;
52
Ronald Cronecbc0682021-03-26 13:25:17 +010053 cipher_info = mbedtls_cipher_info_from_psa( alg,
54 attributes->core.type, key_bits,
55 &cipher_id );
56 if( cipher_info == NULL )
Ronald Cron46f91782021-03-17 08:16:34 +010057 return( PSA_ERROR_NOT_SUPPORTED );
58
59 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
60 {
61#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
62 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010063 operation->alg = PSA_ALG_CCM;
Ronald Cron46f91782021-03-17 08:16:34 +010064 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
65 * The call to mbedtls_ccm_encrypt_and_tag or
66 * mbedtls_ccm_auth_decrypt will validate the tag length. */
67 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
68 return( PSA_ERROR_INVALID_ARGUMENT );
69
70 mbedtls_ccm_init( &operation->ctx.ccm );
71 status = mbedtls_to_psa_error(
72 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
73 key_buffer, (unsigned int) key_bits ) );
74 if( status != PSA_SUCCESS )
75 return( status );
76 break;
77#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
78
79#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
80 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010081 operation->alg = PSA_ALG_GCM;
Ronald Cron46f91782021-03-17 08:16:34 +010082 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
83 * The call to mbedtls_gcm_crypt_and_tag or
84 * mbedtls_gcm_auth_decrypt will validate the tag length. */
85 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
86 return( PSA_ERROR_INVALID_ARGUMENT );
87
88 mbedtls_gcm_init( &operation->ctx.gcm );
89 status = mbedtls_to_psa_error(
90 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
91 key_buffer, (unsigned int) key_bits ) );
92 if( status != PSA_SUCCESS )
93 return( status );
94 break;
95#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
96
97#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
98 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010099 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cron46f91782021-03-17 08:16:34 +0100100 /* We only support the default tag length. */
101 if( alg != PSA_ALG_CHACHA20_POLY1305 )
102 return( PSA_ERROR_NOT_SUPPORTED );
103
104 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
105 status = mbedtls_to_psa_error(
106 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
107 key_buffer ) );
108 if( status != PSA_SUCCESS )
109 return( status );
110 break;
111#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
112
113 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200114 (void) status;
115 (void) key_buffer;
Ronald Cron46f91782021-03-17 08:16:34 +0100116 return( PSA_ERROR_NOT_SUPPORTED );
117 }
118
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100119 operation->key_type = psa_get_key_type( attributes );
120
Przemek Stekiel88ade842022-10-08 17:56:18 +0200121 operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH( alg );
Ronald Cron46f91782021-03-17 08:16:34 +0100122
123 return( PSA_SUCCESS );
124}
125
126psa_status_t mbedtls_psa_aead_encrypt(
127 const psa_key_attributes_t *attributes,
128 const uint8_t *key_buffer, size_t key_buffer_size,
129 psa_algorithm_t alg,
130 const uint8_t *nonce, size_t nonce_length,
131 const uint8_t *additional_data, size_t additional_data_length,
132 const uint8_t *plaintext, size_t plaintext_length,
133 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
134{
135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100136 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100137 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100138
Paul Elliottcc358592021-05-12 12:22:28 +0100139 status = psa_aead_setup( &operation, attributes, key_buffer,
140 key_buffer_size, alg );
141
Ronald Cron46f91782021-03-17 08:16:34 +0100142 if( status != PSA_SUCCESS )
143 goto exit;
144
145 /* For all currently supported modes, the tag is at the end of the
146 * ciphertext. */
147 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
148 {
149 status = PSA_ERROR_BUFFER_TOO_SMALL;
150 goto exit;
151 }
152 tag = ciphertext + plaintext_length;
153
Ronald Cron46f91782021-03-17 08:16:34 +0100154#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100155 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100156 {
157 status = mbedtls_to_psa_error(
158 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
159 plaintext_length,
160 nonce, nonce_length,
161 additional_data,
162 additional_data_length,
163 plaintext, ciphertext,
164 tag, operation.tag_length ) );
165 }
166 else
167#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200168#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100169 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200170 {
171 status = mbedtls_to_psa_error(
172 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
173 MBEDTLS_GCM_ENCRYPT,
174 plaintext_length,
175 nonce, nonce_length,
176 additional_data, additional_data_length,
177 plaintext, ciphertext,
178 operation.tag_length, tag ) );
179 }
180 else
181#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100182#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100183 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100184 {
Paul Elliott96b01732021-07-16 17:00:26 +0100185 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100186 {
187 status = PSA_ERROR_NOT_SUPPORTED;
188 goto exit;
189 }
190 status = mbedtls_to_psa_error(
191 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
192 plaintext_length,
193 nonce,
194 additional_data,
195 additional_data_length,
196 plaintext,
197 ciphertext,
198 tag ) );
199 }
200 else
201#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
202 {
203 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200204 (void) nonce;
205 (void) nonce_length;
206 (void) additional_data;
207 (void) additional_data_length;
208 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100209 return( PSA_ERROR_NOT_SUPPORTED );
210 }
211
212 if( status == PSA_SUCCESS )
213 *ciphertext_length = plaintext_length + operation.tag_length;
214
215exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100216 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100217
218 return( status );
219}
220
221/* Locate the tag in a ciphertext buffer containing the encrypted data
222 * followed by the tag. Return the length of the part preceding the tag in
223 * *plaintext_length. This is the size of the plaintext in modes where
224 * the encrypted data has the same size as the plaintext, such as
225 * CCM and GCM. */
226static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
227 const uint8_t *ciphertext,
228 size_t ciphertext_length,
229 size_t plaintext_size,
230 const uint8_t **p_tag )
231{
232 size_t payload_length;
233 if( tag_length > ciphertext_length )
234 return( PSA_ERROR_INVALID_ARGUMENT );
235 payload_length = ciphertext_length - tag_length;
236 if( payload_length > plaintext_size )
237 return( PSA_ERROR_BUFFER_TOO_SMALL );
238 *p_tag = ciphertext + payload_length;
239 return( PSA_SUCCESS );
240}
241
242psa_status_t mbedtls_psa_aead_decrypt(
243 const psa_key_attributes_t *attributes,
244 const uint8_t *key_buffer, size_t key_buffer_size,
245 psa_algorithm_t alg,
246 const uint8_t *nonce, size_t nonce_length,
247 const uint8_t *additional_data, size_t additional_data_length,
248 const uint8_t *ciphertext, size_t ciphertext_length,
249 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
250{
251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100252 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100253 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100254
Paul Elliottcc358592021-05-12 12:22:28 +0100255 status = psa_aead_setup( &operation, attributes, key_buffer,
256 key_buffer_size, alg );
257
Ronald Cron46f91782021-03-17 08:16:34 +0100258 if( status != PSA_SUCCESS )
259 goto exit;
260
261 status = psa_aead_unpadded_locate_tag( operation.tag_length,
262 ciphertext, ciphertext_length,
263 plaintext_size, &tag );
264 if( status != PSA_SUCCESS )
265 goto exit;
266
Ronald Cron46f91782021-03-17 08:16:34 +0100267#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100268 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100269 {
270 status = mbedtls_to_psa_error(
271 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
272 ciphertext_length - operation.tag_length,
273 nonce, nonce_length,
274 additional_data,
275 additional_data_length,
276 ciphertext, plaintext,
277 tag, operation.tag_length ) );
278 }
279 else
280#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200281#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100282 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200283 {
284 status = mbedtls_to_psa_error(
285 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
286 ciphertext_length - operation.tag_length,
287 nonce, nonce_length,
288 additional_data,
289 additional_data_length,
290 tag, operation.tag_length,
291 ciphertext, plaintext ) );
292 }
293 else
294#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100295#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100296 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100297 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100298 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100299 {
300 status = PSA_ERROR_NOT_SUPPORTED;
301 goto exit;
302 }
303 status = mbedtls_to_psa_error(
304 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
305 ciphertext_length - operation.tag_length,
306 nonce,
307 additional_data,
308 additional_data_length,
309 tag,
310 ciphertext,
311 plaintext ) );
312 }
313 else
314#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
315 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200316 (void) nonce;
317 (void) nonce_length;
318 (void) additional_data;
319 (void) additional_data_length;
320 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100321 return( PSA_ERROR_NOT_SUPPORTED );
322 }
323
324 if( status == PSA_SUCCESS )
325 *plaintext_length = ciphertext_length - operation.tag_length;
326
327exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100328 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100329
330 if( status == PSA_SUCCESS )
331 *plaintext_length = ciphertext_length - operation.tag_length;
332 return( status );
333}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100334
Paul Elliottadb8b162021-04-20 16:06:57 +0100335/* Set the key and algorithm for a multipart authenticated encryption
336 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100337psa_status_t mbedtls_psa_aead_encrypt_setup(
338 mbedtls_psa_aead_operation_t *operation,
339 const psa_key_attributes_t *attributes,
340 const uint8_t *key_buffer,
341 size_t key_buffer_size,
342 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100343{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100345
Paul Elliottcc358592021-05-12 12:22:28 +0100346 status = psa_aead_setup( operation, attributes, key_buffer,
347 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100348
349 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100350 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100351
352 return ( status );
353}
354
355/* Set the key and algorithm for a multipart authenticated decryption
356 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100357psa_status_t mbedtls_psa_aead_decrypt_setup(
358 mbedtls_psa_aead_operation_t *operation,
359 const psa_key_attributes_t *attributes,
360 const uint8_t *key_buffer,
361 size_t key_buffer_size,
362 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100363{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100365
Paul Elliottcc358592021-05-12 12:22:28 +0100366 status = psa_aead_setup( operation, attributes, key_buffer,
367 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100368
369 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100370 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100371
372 return ( status );
373}
374
Paul Elliottadb8b162021-04-20 16:06:57 +0100375/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100376psa_status_t mbedtls_psa_aead_set_nonce(
377 mbedtls_psa_aead_operation_t *operation,
378 const uint8_t *nonce,
379 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100380{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100382
Paul Elliottbc949782021-06-03 15:29:00 +0100383#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100384 if( operation->alg == PSA_ALG_GCM )
385 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100386 status = mbedtls_to_psa_error(
387 mbedtls_gcm_starts( &operation->ctx.gcm,
388 operation->is_encrypt ?
389 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
390 nonce,
391 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100392 }
393 else
394#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100395#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
396 if( operation->alg == PSA_ALG_CCM )
397 {
398 status = mbedtls_to_psa_error(
399 mbedtls_ccm_starts( &operation->ctx.ccm,
400 operation->is_encrypt ?
401 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
402 nonce,
403 nonce_length ) );
404 }
405 else
406#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100407#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
408 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
409 {
Paul Elliott946c9202021-09-28 14:32:55 +0100410 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
411 * allocate a buffer in the operation, copy the nonce to it and pad
412 * it, so for now check the nonce is 12 bytes, as
413 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
414 * passed in buffer. */
415 if( nonce_length != 12 )
416 {
417 return( PSA_ERROR_INVALID_ARGUMENT );
418 }
419
Paul Elliott2df40052021-05-07 17:52:18 +0100420 status = mbedtls_to_psa_error(
421 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
422 nonce,
423 operation->is_encrypt ?
424 MBEDTLS_CHACHAPOLY_ENCRYPT :
425 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottefda3402021-08-25 17:16:52 +0100426 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100427 else
428#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
429 {
Ronald Cron17006702021-12-03 15:25:24 +0100430 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100431 ( void ) nonce;
Ronald Cron17006702021-12-03 15:25:24 +0100432 ( void ) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100433
434 return ( PSA_ERROR_NOT_SUPPORTED );
435 }
436
Paul Elliottadb8b162021-04-20 16:06:57 +0100437 return( status );
438}
Paul Elliottefda3402021-08-25 17:16:52 +0100439
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100440 /* Declare the lengths of the message and additional data for AEAD. */
441psa_status_t mbedtls_psa_aead_set_lengths(
442 mbedtls_psa_aead_operation_t *operation,
443 size_t ad_length,
444 size_t plaintext_length )
445{
Paul Elliotte193ea82021-10-01 13:00:16 +0100446#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
447 if( operation->alg == PSA_ALG_CCM )
448 {
449 return( mbedtls_to_psa_error(
450 mbedtls_ccm_set_lengths( &operation->ctx.ccm,
451 ad_length,
452 plaintext_length,
453 operation->tag_length ) ) );
454
455 }
456#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100457 ( void ) operation;
458 ( void ) ad_length;
459 ( void ) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100460#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100461
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100462 return ( PSA_SUCCESS );
463}
464
Paul Elliottadb8b162021-04-20 16:06:57 +0100465/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100466psa_status_t mbedtls_psa_aead_update_ad(
467 mbedtls_psa_aead_operation_t *operation,
468 const uint8_t *input,
469 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100470{
471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
472
Paul Elliottadb8b162021-04-20 16:06:57 +0100473#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
474 if( operation->alg == PSA_ALG_GCM )
475 {
Paul Elliott2df40052021-05-07 17:52:18 +0100476 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100477 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100478 }
479 else
480#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100481#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
482 if( operation->alg == PSA_ALG_CCM )
483 {
484 status = mbedtls_to_psa_error(
485 mbedtls_ccm_update_ad( &operation->ctx.ccm, input, input_length ) );
486 }
487 else
488#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100489#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
490 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
491 {
Paul Elliott2df40052021-05-07 17:52:18 +0100492 status = mbedtls_to_psa_error(
493 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
494 input,
495 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100496 }
497 else
498#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
499 {
Paul Elliottbc949782021-06-03 15:29:00 +0100500 ( void ) operation;
501 ( void ) input;
502 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100503
504 return ( PSA_ERROR_NOT_SUPPORTED );
505 }
506
Paul Elliottadb8b162021-04-20 16:06:57 +0100507 return ( status );
508}
509
510/* Encrypt or decrypt a message fragment in an active multipart AEAD
511 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100512psa_status_t mbedtls_psa_aead_update(
513 mbedtls_psa_aead_operation_t *operation,
514 const uint8_t *input,
515 size_t input_length,
516 uint8_t *output,
517 size_t output_size,
518 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100519{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100520 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
522
Paul Elliotte2c788d2021-05-13 17:16:01 +0100523 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100524
Paul Elliottadb8b162021-04-20 16:06:57 +0100525#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
526 if( operation->alg == PSA_ALG_GCM )
527 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100528 status = mbedtls_to_psa_error(
529 mbedtls_gcm_update( &operation->ctx.gcm,
530 input, input_length,
531 output, output_size,
532 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100533 }
534 else
535#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100536#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
537 if( operation->alg == PSA_ALG_CCM )
538 {
539 if( output_size < input_length )
540 return( PSA_ERROR_BUFFER_TOO_SMALL );
541
542 status = mbedtls_to_psa_error(
543 mbedtls_ccm_update( &operation->ctx.ccm,
544 input, input_length,
545 output, output_size,
546 &update_output_length ) );
547 }
548 else
549#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100550#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
551 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
552 {
Paul Elliottecce9012021-07-23 15:44:11 +0100553 if( output_size < input_length )
554 return( PSA_ERROR_BUFFER_TOO_SMALL );
555
Paul Elliott2df40052021-05-07 17:52:18 +0100556 status = mbedtls_to_psa_error(
557 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
558 input_length,
559 input,
560 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100561 }
562 else
563#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
564 {
Ronald Cron17006702021-12-03 15:25:24 +0100565 ( void ) operation;
Paul Elliottbc949782021-06-03 15:29:00 +0100566 ( void ) input;
Ronald Cron17006702021-12-03 15:25:24 +0100567 ( void ) output;
568 ( void ) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100569
570 return ( PSA_ERROR_NOT_SUPPORTED );
571 }
572
573 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100574 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100575
576 return( status );
577}
578
Paul Elliottadb8b162021-04-20 16:06:57 +0100579/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100580psa_status_t mbedtls_psa_aead_finish(
581 mbedtls_psa_aead_operation_t *operation,
582 uint8_t *ciphertext,
583 size_t ciphertext_size,
584 size_t *ciphertext_length,
585 uint8_t *tag,
586 size_t tag_size,
587 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100588{
589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100590 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100591
Paul Elliott315628d2021-07-20 18:25:54 +0100592 if( tag_size < operation->tag_length )
593 return( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100594
595#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
596 if( operation->alg == PSA_ALG_GCM )
Paul Elliottecce9012021-07-23 15:44:11 +0100597 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100598 status = mbedtls_to_psa_error(
599 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott71b05672021-09-24 11:18:13 +0100600 ciphertext, ciphertext_size, ciphertext_length,
Paul Elliott2fe5db82021-07-22 18:10:43 +0100601 tag, operation->tag_length ) );
Paul Elliottecce9012021-07-23 15:44:11 +0100602 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100603 else
604#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100605#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
606 if( operation->alg == PSA_ALG_CCM )
607 {
608 /* tag must be big enough to store a tag of size passed into set
609 * lengths. */
610 if( tag_size < operation->tag_length )
611 return( PSA_ERROR_BUFFER_TOO_SMALL );
612
613 status = mbedtls_to_psa_error(
614 mbedtls_ccm_finish( &operation->ctx.ccm,
615 tag, operation->tag_length ) );
616 }
617 else
618#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100619#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
620 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliotted08cf82021-07-22 18:48:24 +0100621 {
622 /* Belt and braces. Although the above tag_size check should have
623 * already done this, if we later start supporting smaller tag sizes
624 * for chachapoly, then passing a tag buffer smaller than 16 into here
625 * could cause a buffer overflow, so better safe than sorry. */
626 if( tag_size < 16 )
627 return( PSA_ERROR_BUFFER_TOO_SMALL );
628
Paul Elliott2df40052021-05-07 17:52:18 +0100629 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100630 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
631 tag ) );
Paul Elliotted08cf82021-07-22 18:48:24 +0100632 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100633 else
634#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
635 {
636 ( void ) ciphertext;
637 ( void ) ciphertext_size;
638 ( void ) ciphertext_length;
639 ( void ) tag;
640 ( void ) tag_size;
641 ( void ) tag_length;
642
643 return ( PSA_ERROR_NOT_SUPPORTED );
644 }
645
646 if( status == PSA_SUCCESS )
647 {
Paul Elliott8ff74212021-09-19 18:39:23 +0100648 /* This will be zero for all supported algorithms currently, but left
649 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100650 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100651 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100652 }
653
Paul Elliottadb8b162021-04-20 16:06:57 +0100654 return ( status );
655}
656
Paul Elliottadb8b162021-04-20 16:06:57 +0100657/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100658psa_status_t mbedtls_psa_aead_abort(
659 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100660{
Paul Elliott811d8d42021-04-22 11:31:14 +0100661 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100662 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100663#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
664 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100665 mbedtls_ccm_free( &operation->ctx.ccm );
666 break;
667#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
668#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
669 case PSA_ALG_GCM:
670 mbedtls_gcm_free( &operation->ctx.gcm );
671 break;
672#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
673#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100674 case PSA_ALG_CHACHA20_POLY1305:
675 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
676 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100677#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
678 }
679
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100680 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100681
Paul Elliottadb8b162021-04-20 16:06:57 +0100682 return( PSA_SUCCESS );
683}
684
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100685#endif /* MBEDTLS_PSA_CRYPTO_C */
686