blob: 714d950a1494949a804c5add7bb457937dded22a [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:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200122 (void) status;
123 (void) key_buffer;
Ronald Cron46f91782021-03-17 08:16:34 +0100124 return( PSA_ERROR_NOT_SUPPORTED );
125 }
126
Bence Szépkútiec174e22021-03-19 18:46:15 +0100127 if( PSA_AEAD_TAG_LENGTH( attributes->core.type,
128 key_bits, alg )
129 > full_tag_length )
Ronald Cron46f91782021-03-17 08:16:34 +0100130 return( PSA_ERROR_INVALID_ARGUMENT );
131
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100132 operation->key_type = psa_get_key_type( attributes );
133
134 operation->tag_length = PSA_AEAD_TAG_LENGTH( operation->key_type,
Bence Szépkútiec174e22021-03-19 18:46:15 +0100135 key_bits,
136 alg );
Ronald Cron46f91782021-03-17 08:16:34 +0100137
138 return( PSA_SUCCESS );
139}
140
141psa_status_t mbedtls_psa_aead_encrypt(
142 const psa_key_attributes_t *attributes,
143 const uint8_t *key_buffer, size_t key_buffer_size,
144 psa_algorithm_t alg,
145 const uint8_t *nonce, size_t nonce_length,
146 const uint8_t *additional_data, size_t additional_data_length,
147 const uint8_t *plaintext, size_t plaintext_length,
148 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
149{
150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100151 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100152 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100153
Paul Elliottcc358592021-05-12 12:22:28 +0100154 status = psa_aead_setup( &operation, attributes, key_buffer,
155 key_buffer_size, alg );
156
Ronald Cron46f91782021-03-17 08:16:34 +0100157 if( status != PSA_SUCCESS )
158 goto exit;
159
160 /* For all currently supported modes, the tag is at the end of the
161 * ciphertext. */
162 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
163 {
164 status = PSA_ERROR_BUFFER_TOO_SMALL;
165 goto exit;
166 }
167 tag = ciphertext + plaintext_length;
168
Ronald Cron46f91782021-03-17 08:16:34 +0100169#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100170 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100171 {
172 status = mbedtls_to_psa_error(
173 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
174 plaintext_length,
175 nonce, nonce_length,
176 additional_data,
177 additional_data_length,
178 plaintext, ciphertext,
179 tag, operation.tag_length ) );
180 }
181 else
182#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200183#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100184 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200185 {
186 status = mbedtls_to_psa_error(
187 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
188 MBEDTLS_GCM_ENCRYPT,
189 plaintext_length,
190 nonce, nonce_length,
191 additional_data, additional_data_length,
192 plaintext, ciphertext,
193 operation.tag_length, tag ) );
194 }
195 else
196#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100197#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100198 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100199 {
Paul Elliott96b01732021-07-16 17:00:26 +0100200 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100201 {
202 status = PSA_ERROR_NOT_SUPPORTED;
203 goto exit;
204 }
205 status = mbedtls_to_psa_error(
206 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
207 plaintext_length,
208 nonce,
209 additional_data,
210 additional_data_length,
211 plaintext,
212 ciphertext,
213 tag ) );
214 }
215 else
216#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
217 {
218 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200219 (void) nonce;
220 (void) nonce_length;
221 (void) additional_data;
222 (void) additional_data_length;
223 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100224 return( PSA_ERROR_NOT_SUPPORTED );
225 }
226
227 if( status == PSA_SUCCESS )
228 *ciphertext_length = plaintext_length + operation.tag_length;
229
230exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100231 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100232
233 return( status );
234}
235
236/* Locate the tag in a ciphertext buffer containing the encrypted data
237 * followed by the tag. Return the length of the part preceding the tag in
238 * *plaintext_length. This is the size of the plaintext in modes where
239 * the encrypted data has the same size as the plaintext, such as
240 * CCM and GCM. */
241static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
242 const uint8_t *ciphertext,
243 size_t ciphertext_length,
244 size_t plaintext_size,
245 const uint8_t **p_tag )
246{
247 size_t payload_length;
248 if( tag_length > ciphertext_length )
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 payload_length = ciphertext_length - tag_length;
251 if( payload_length > plaintext_size )
252 return( PSA_ERROR_BUFFER_TOO_SMALL );
253 *p_tag = ciphertext + payload_length;
254 return( PSA_SUCCESS );
255}
256
257psa_status_t mbedtls_psa_aead_decrypt(
258 const psa_key_attributes_t *attributes,
259 const uint8_t *key_buffer, size_t key_buffer_size,
260 psa_algorithm_t alg,
261 const uint8_t *nonce, size_t nonce_length,
262 const uint8_t *additional_data, size_t additional_data_length,
263 const uint8_t *ciphertext, size_t ciphertext_length,
264 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
265{
266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100267 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100268 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100269
Paul Elliottcc358592021-05-12 12:22:28 +0100270 status = psa_aead_setup( &operation, attributes, key_buffer,
271 key_buffer_size, alg );
272
Ronald Cron46f91782021-03-17 08:16:34 +0100273 if( status != PSA_SUCCESS )
274 goto exit;
275
276 status = psa_aead_unpadded_locate_tag( operation.tag_length,
277 ciphertext, ciphertext_length,
278 plaintext_size, &tag );
279 if( status != PSA_SUCCESS )
280 goto exit;
281
Ronald Cron46f91782021-03-17 08:16:34 +0100282#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100283 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100284 {
285 status = mbedtls_to_psa_error(
286 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
287 ciphertext_length - operation.tag_length,
288 nonce, nonce_length,
289 additional_data,
290 additional_data_length,
291 ciphertext, plaintext,
292 tag, operation.tag_length ) );
293 }
294 else
295#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200296#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100297 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200298 {
299 status = mbedtls_to_psa_error(
300 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
301 ciphertext_length - operation.tag_length,
302 nonce, nonce_length,
303 additional_data,
304 additional_data_length,
305 tag, operation.tag_length,
306 ciphertext, plaintext ) );
307 }
308 else
309#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100310#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100311 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100312 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100313 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100314 {
315 status = PSA_ERROR_NOT_SUPPORTED;
316 goto exit;
317 }
318 status = mbedtls_to_psa_error(
319 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
320 ciphertext_length - operation.tag_length,
321 nonce,
322 additional_data,
323 additional_data_length,
324 tag,
325 ciphertext,
326 plaintext ) );
327 }
328 else
329#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
330 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200331 (void) nonce;
332 (void) nonce_length;
333 (void) additional_data;
334 (void) additional_data_length;
335 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100336 return( PSA_ERROR_NOT_SUPPORTED );
337 }
338
339 if( status == PSA_SUCCESS )
340 *plaintext_length = ciphertext_length - operation.tag_length;
341
342exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100343 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100344
345 if( status == PSA_SUCCESS )
346 *plaintext_length = ciphertext_length - operation.tag_length;
347 return( status );
348}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100349
Paul Elliottadb8b162021-04-20 16:06:57 +0100350/* Set the key and algorithm for a multipart authenticated encryption
351 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100352psa_status_t mbedtls_psa_aead_encrypt_setup(
353 mbedtls_psa_aead_operation_t *operation,
354 const psa_key_attributes_t *attributes,
355 const uint8_t *key_buffer,
356 size_t key_buffer_size,
357 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100358{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100360
Paul Elliottcc358592021-05-12 12:22:28 +0100361 status = psa_aead_setup( operation, attributes, key_buffer,
362 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100363
364 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100365 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100366
367 return ( status );
368}
369
370/* Set the key and algorithm for a multipart authenticated decryption
371 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100372psa_status_t mbedtls_psa_aead_decrypt_setup(
373 mbedtls_psa_aead_operation_t *operation,
374 const psa_key_attributes_t *attributes,
375 const uint8_t *key_buffer,
376 size_t key_buffer_size,
377 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100378{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100380
Paul Elliottcc358592021-05-12 12:22:28 +0100381 status = psa_aead_setup( operation, attributes, key_buffer,
382 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100383
384 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100385 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100386
387 return ( status );
388}
389
Paul Elliottadb8b162021-04-20 16:06:57 +0100390/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100391psa_status_t mbedtls_psa_aead_set_nonce(
392 mbedtls_psa_aead_operation_t *operation,
393 const uint8_t *nonce,
394 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100395{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100397
Paul Elliottbc949782021-06-03 15:29:00 +0100398#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100399 if( operation->alg == PSA_ALG_GCM )
400 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100401 status = mbedtls_to_psa_error(
402 mbedtls_gcm_starts( &operation->ctx.gcm,
403 operation->is_encrypt ?
404 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
405 nonce,
406 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100407 }
408 else
409#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100410#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
411 if( operation->alg == PSA_ALG_CCM )
412 {
413 status = mbedtls_to_psa_error(
414 mbedtls_ccm_starts( &operation->ctx.ccm,
415 operation->is_encrypt ?
416 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
417 nonce,
418 nonce_length ) );
419 }
420 else
421#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100422#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
423 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
424 {
Paul Elliott946c9202021-09-28 14:32:55 +0100425 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
426 * allocate a buffer in the operation, copy the nonce to it and pad
427 * it, so for now check the nonce is 12 bytes, as
428 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
429 * passed in buffer. */
430 if( nonce_length != 12 )
431 {
432 return( PSA_ERROR_INVALID_ARGUMENT );
433 }
434
Paul Elliott2df40052021-05-07 17:52:18 +0100435 status = mbedtls_to_psa_error(
436 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
437 nonce,
438 operation->is_encrypt ?
439 MBEDTLS_CHACHAPOLY_ENCRYPT :
440 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottefda3402021-08-25 17:16:52 +0100441 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100442 else
443#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
444 {
Ronald Cron17006702021-12-03 15:25:24 +0100445 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100446 ( void ) nonce;
Ronald Cron17006702021-12-03 15:25:24 +0100447 ( void ) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100448
449 return ( PSA_ERROR_NOT_SUPPORTED );
450 }
451
Paul Elliottadb8b162021-04-20 16:06:57 +0100452 return( status );
453}
Paul Elliottefda3402021-08-25 17:16:52 +0100454
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100455 /* Declare the lengths of the message and additional data for AEAD. */
456psa_status_t mbedtls_psa_aead_set_lengths(
457 mbedtls_psa_aead_operation_t *operation,
458 size_t ad_length,
459 size_t plaintext_length )
460{
Paul Elliotte193ea82021-10-01 13:00:16 +0100461#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
462 if( operation->alg == PSA_ALG_CCM )
463 {
464 return( mbedtls_to_psa_error(
465 mbedtls_ccm_set_lengths( &operation->ctx.ccm,
466 ad_length,
467 plaintext_length,
468 operation->tag_length ) ) );
469
470 }
471#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100472 ( void ) operation;
473 ( void ) ad_length;
474 ( void ) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100475#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100476
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100477 return ( PSA_SUCCESS );
478}
479
Paul Elliottadb8b162021-04-20 16:06:57 +0100480/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100481psa_status_t mbedtls_psa_aead_update_ad(
482 mbedtls_psa_aead_operation_t *operation,
483 const uint8_t *input,
484 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100485{
486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
487
Paul Elliottadb8b162021-04-20 16:06:57 +0100488#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
489 if( operation->alg == PSA_ALG_GCM )
490 {
Paul Elliott2df40052021-05-07 17:52:18 +0100491 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100492 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100493 }
494 else
495#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100496#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
497 if( operation->alg == PSA_ALG_CCM )
498 {
499 status = mbedtls_to_psa_error(
500 mbedtls_ccm_update_ad( &operation->ctx.ccm, input, input_length ) );
501 }
502 else
503#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100504#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
505 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
506 {
Paul Elliott2df40052021-05-07 17:52:18 +0100507 status = mbedtls_to_psa_error(
508 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
509 input,
510 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100511 }
512 else
513#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
514 {
Paul Elliottbc949782021-06-03 15:29:00 +0100515 ( void ) operation;
516 ( void ) input;
517 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100518
519 return ( PSA_ERROR_NOT_SUPPORTED );
520 }
521
Paul Elliottadb8b162021-04-20 16:06:57 +0100522 return ( status );
523}
524
525/* Encrypt or decrypt a message fragment in an active multipart AEAD
526 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100527psa_status_t mbedtls_psa_aead_update(
528 mbedtls_psa_aead_operation_t *operation,
529 const uint8_t *input,
530 size_t input_length,
531 uint8_t *output,
532 size_t output_size,
533 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100534{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100535 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
537
Paul Elliotte2c788d2021-05-13 17:16:01 +0100538 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100539
Paul Elliottadb8b162021-04-20 16:06:57 +0100540#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
541 if( operation->alg == PSA_ALG_GCM )
542 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100543 status = mbedtls_to_psa_error(
544 mbedtls_gcm_update( &operation->ctx.gcm,
545 input, input_length,
546 output, output_size,
547 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100548 }
549 else
550#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100551#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
552 if( operation->alg == PSA_ALG_CCM )
553 {
554 if( output_size < input_length )
555 return( PSA_ERROR_BUFFER_TOO_SMALL );
556
557 status = mbedtls_to_psa_error(
558 mbedtls_ccm_update( &operation->ctx.ccm,
559 input, input_length,
560 output, output_size,
561 &update_output_length ) );
562 }
563 else
564#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100565#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
566 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
567 {
Paul Elliottecce9012021-07-23 15:44:11 +0100568 if( output_size < input_length )
569 return( PSA_ERROR_BUFFER_TOO_SMALL );
570
Paul Elliott2df40052021-05-07 17:52:18 +0100571 status = mbedtls_to_psa_error(
572 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
573 input_length,
574 input,
575 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100576 }
577 else
578#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
579 {
Ronald Cron17006702021-12-03 15:25:24 +0100580 ( void ) operation;
Paul Elliottbc949782021-06-03 15:29:00 +0100581 ( void ) input;
Ronald Cron17006702021-12-03 15:25:24 +0100582 ( void ) output;
583 ( void ) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100584
585 return ( PSA_ERROR_NOT_SUPPORTED );
586 }
587
588 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100589 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100590
591 return( status );
592}
593
Paul Elliottadb8b162021-04-20 16:06:57 +0100594/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100595psa_status_t mbedtls_psa_aead_finish(
596 mbedtls_psa_aead_operation_t *operation,
597 uint8_t *ciphertext,
598 size_t ciphertext_size,
599 size_t *ciphertext_length,
600 uint8_t *tag,
601 size_t tag_size,
602 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100603{
604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100605 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100606
Paul Elliott315628d2021-07-20 18:25:54 +0100607 if( tag_size < operation->tag_length )
608 return( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100609
610#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
611 if( operation->alg == PSA_ALG_GCM )
Paul Elliottecce9012021-07-23 15:44:11 +0100612 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100613 status = mbedtls_to_psa_error(
614 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott71b05672021-09-24 11:18:13 +0100615 ciphertext, ciphertext_size, ciphertext_length,
Paul Elliott2fe5db82021-07-22 18:10:43 +0100616 tag, operation->tag_length ) );
Paul Elliottecce9012021-07-23 15:44:11 +0100617 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100618 else
619#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100620#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
621 if( operation->alg == PSA_ALG_CCM )
622 {
623 /* tag must be big enough to store a tag of size passed into set
624 * lengths. */
625 if( tag_size < operation->tag_length )
626 return( PSA_ERROR_BUFFER_TOO_SMALL );
627
628 status = mbedtls_to_psa_error(
629 mbedtls_ccm_finish( &operation->ctx.ccm,
630 tag, operation->tag_length ) );
631 }
632 else
633#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100634#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
635 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliotted08cf82021-07-22 18:48:24 +0100636 {
637 /* Belt and braces. Although the above tag_size check should have
638 * already done this, if we later start supporting smaller tag sizes
639 * for chachapoly, then passing a tag buffer smaller than 16 into here
640 * could cause a buffer overflow, so better safe than sorry. */
641 if( tag_size < 16 )
642 return( PSA_ERROR_BUFFER_TOO_SMALL );
643
Paul Elliott2df40052021-05-07 17:52:18 +0100644 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100645 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
646 tag ) );
Paul Elliotted08cf82021-07-22 18:48:24 +0100647 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100648 else
649#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
650 {
651 ( void ) ciphertext;
652 ( void ) ciphertext_size;
653 ( void ) ciphertext_length;
654 ( void ) tag;
655 ( void ) tag_size;
656 ( void ) tag_length;
657
658 return ( PSA_ERROR_NOT_SUPPORTED );
659 }
660
661 if( status == PSA_SUCCESS )
662 {
Paul Elliott8ff74212021-09-19 18:39:23 +0100663 /* This will be zero for all supported algorithms currently, but left
664 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100665 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100666 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100667 }
668
Paul Elliottadb8b162021-04-20 16:06:57 +0100669 return ( status );
670}
671
Paul Elliottadb8b162021-04-20 16:06:57 +0100672/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100673psa_status_t mbedtls_psa_aead_abort(
674 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100675{
Paul Elliott811d8d42021-04-22 11:31:14 +0100676 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100677 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100678#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
679 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100680 mbedtls_ccm_free( &operation->ctx.ccm );
681 break;
682#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
683#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
684 case PSA_ALG_GCM:
685 mbedtls_gcm_free( &operation->ctx.gcm );
686 break;
687#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
688#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100689 case PSA_ALG_CHACHA20_POLY1305:
690 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
691 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100692#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
693 }
694
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100695 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100696
Paul Elliottadb8b162021-04-20 16:06:57 +0100697 return( PSA_SUCCESS );
698}
699
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100700#endif /* MBEDTLS_PSA_CRYPTO_C */
701