blob: 76d95bcc698596ba2792d7fb8e2b8acc5e907bc7 [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;
Ronald Cronecbc0682021-03-26 13:25:17 +010048 size_t full_tag_length = 0;
Ronald Cron46f91782021-03-17 08:16:34 +010049
Paul Elliottcc358592021-05-12 12:22:28 +010050 ( void ) key_buffer_size;
51
Ronald Cron46f91782021-03-17 08:16:34 +010052 key_bits = attributes->core.bits;
53
Ronald Cronecbc0682021-03-26 13:25:17 +010054 cipher_info = mbedtls_cipher_info_from_psa( alg,
55 attributes->core.type, key_bits,
56 &cipher_id );
57 if( cipher_info == NULL )
Ronald Cron46f91782021-03-17 08:16:34 +010058 return( PSA_ERROR_NOT_SUPPORTED );
59
60 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
61 {
62#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
63 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010064 operation->alg = PSA_ALG_CCM;
Ronald Cronecbc0682021-03-26 13:25:17 +010065 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +010066 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
67 * The call to mbedtls_ccm_encrypt_and_tag or
68 * mbedtls_ccm_auth_decrypt will validate the tag length. */
69 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
70 return( PSA_ERROR_INVALID_ARGUMENT );
71
72 mbedtls_ccm_init( &operation->ctx.ccm );
73 status = mbedtls_to_psa_error(
74 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
75 key_buffer, (unsigned int) key_bits ) );
76 if( status != PSA_SUCCESS )
77 return( status );
78 break;
79#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
80
81#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
82 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010083 operation->alg = PSA_ALG_GCM;
Ronald Cronecbc0682021-03-26 13:25:17 +010084 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +010085 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
86 * The call to mbedtls_gcm_crypt_and_tag or
87 * mbedtls_gcm_auth_decrypt will validate the tag length. */
88 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
89 return( PSA_ERROR_INVALID_ARGUMENT );
90
91 mbedtls_gcm_init( &operation->ctx.gcm );
92 status = mbedtls_to_psa_error(
93 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
94 key_buffer, (unsigned int) key_bits ) );
95 if( status != PSA_SUCCESS )
96 return( status );
97 break;
98#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
99
100#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
101 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +0100102 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cronecbc0682021-03-26 13:25:17 +0100103 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +0100104 /* We only support the default tag length. */
105 if( alg != PSA_ALG_CHACHA20_POLY1305 )
106 return( PSA_ERROR_NOT_SUPPORTED );
107
108 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
109 status = mbedtls_to_psa_error(
110 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
111 key_buffer ) );
112 if( status != PSA_SUCCESS )
113 return( status );
114 break;
115#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
116
117 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200118 (void) status;
119 (void) key_buffer;
Ronald Cron46f91782021-03-17 08:16:34 +0100120 return( PSA_ERROR_NOT_SUPPORTED );
121 }
122
Bence Szépkútiec174e22021-03-19 18:46:15 +0100123 if( PSA_AEAD_TAG_LENGTH( attributes->core.type,
124 key_bits, alg )
125 > full_tag_length )
Ronald Cron46f91782021-03-17 08:16:34 +0100126 return( PSA_ERROR_INVALID_ARGUMENT );
127
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100128 operation->key_type = psa_get_key_type( attributes );
129
130 operation->tag_length = PSA_AEAD_TAG_LENGTH( operation->key_type,
Bence Szépkútiec174e22021-03-19 18:46:15 +0100131 key_bits,
132 alg );
Ronald Cron46f91782021-03-17 08:16:34 +0100133
134 return( PSA_SUCCESS );
135}
136
137psa_status_t mbedtls_psa_aead_encrypt(
138 const psa_key_attributes_t *attributes,
139 const uint8_t *key_buffer, size_t key_buffer_size,
140 psa_algorithm_t alg,
141 const uint8_t *nonce, size_t nonce_length,
142 const uint8_t *additional_data, size_t additional_data_length,
143 const uint8_t *plaintext, size_t plaintext_length,
144 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
145{
146 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100147 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100148 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100149
Paul Elliottcc358592021-05-12 12:22:28 +0100150 status = psa_aead_setup( &operation, attributes, key_buffer,
151 key_buffer_size, alg );
152
Ronald Cron46f91782021-03-17 08:16:34 +0100153 if( status != PSA_SUCCESS )
154 goto exit;
155
156 /* For all currently supported modes, the tag is at the end of the
157 * ciphertext. */
158 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
159 {
160 status = PSA_ERROR_BUFFER_TOO_SMALL;
161 goto exit;
162 }
163 tag = ciphertext + plaintext_length;
164
Ronald Cron46f91782021-03-17 08:16:34 +0100165#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100166 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100167 {
168 status = mbedtls_to_psa_error(
169 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
170 plaintext_length,
171 nonce, nonce_length,
172 additional_data,
173 additional_data_length,
174 plaintext, ciphertext,
175 tag, operation.tag_length ) );
176 }
177 else
178#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200179#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100180 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200181 {
182 status = mbedtls_to_psa_error(
183 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
184 MBEDTLS_GCM_ENCRYPT,
185 plaintext_length,
186 nonce, nonce_length,
187 additional_data, additional_data_length,
188 plaintext, ciphertext,
189 operation.tag_length, tag ) );
190 }
191 else
192#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100193#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100194 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100195 {
Paul Elliott96b01732021-07-16 17:00:26 +0100196 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100197 {
198 status = PSA_ERROR_NOT_SUPPORTED;
199 goto exit;
200 }
201 status = mbedtls_to_psa_error(
202 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
203 plaintext_length,
204 nonce,
205 additional_data,
206 additional_data_length,
207 plaintext,
208 ciphertext,
209 tag ) );
210 }
211 else
212#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
213 {
214 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200215 (void) nonce;
216 (void) nonce_length;
217 (void) additional_data;
218 (void) additional_data_length;
219 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100220 return( PSA_ERROR_NOT_SUPPORTED );
221 }
222
223 if( status == PSA_SUCCESS )
224 *ciphertext_length = plaintext_length + operation.tag_length;
225
226exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100227 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100228
229 return( status );
230}
231
232/* Locate the tag in a ciphertext buffer containing the encrypted data
233 * followed by the tag. Return the length of the part preceding the tag in
234 * *plaintext_length. This is the size of the plaintext in modes where
235 * the encrypted data has the same size as the plaintext, such as
236 * CCM and GCM. */
237static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
238 const uint8_t *ciphertext,
239 size_t ciphertext_length,
240 size_t plaintext_size,
241 const uint8_t **p_tag )
242{
243 size_t payload_length;
244 if( tag_length > ciphertext_length )
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 payload_length = ciphertext_length - tag_length;
247 if( payload_length > plaintext_size )
248 return( PSA_ERROR_BUFFER_TOO_SMALL );
249 *p_tag = ciphertext + payload_length;
250 return( PSA_SUCCESS );
251}
252
253psa_status_t mbedtls_psa_aead_decrypt(
254 const psa_key_attributes_t *attributes,
255 const uint8_t *key_buffer, size_t key_buffer_size,
256 psa_algorithm_t alg,
257 const uint8_t *nonce, size_t nonce_length,
258 const uint8_t *additional_data, size_t additional_data_length,
259 const uint8_t *ciphertext, size_t ciphertext_length,
260 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
261{
262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100263 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100264 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100265
Paul Elliottcc358592021-05-12 12:22:28 +0100266 status = psa_aead_setup( &operation, attributes, key_buffer,
267 key_buffer_size, alg );
268
Ronald Cron46f91782021-03-17 08:16:34 +0100269 if( status != PSA_SUCCESS )
270 goto exit;
271
272 status = psa_aead_unpadded_locate_tag( operation.tag_length,
273 ciphertext, ciphertext_length,
274 plaintext_size, &tag );
275 if( status != PSA_SUCCESS )
276 goto exit;
277
Ronald Cron46f91782021-03-17 08:16:34 +0100278#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100279 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100280 {
281 status = mbedtls_to_psa_error(
282 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
283 ciphertext_length - operation.tag_length,
284 nonce, nonce_length,
285 additional_data,
286 additional_data_length,
287 ciphertext, plaintext,
288 tag, operation.tag_length ) );
289 }
290 else
291#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200292#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100293 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200294 {
295 status = mbedtls_to_psa_error(
296 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
297 ciphertext_length - operation.tag_length,
298 nonce, nonce_length,
299 additional_data,
300 additional_data_length,
301 tag, operation.tag_length,
302 ciphertext, plaintext ) );
303 }
304 else
305#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100306#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100307 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100308 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100309 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100310 {
311 status = PSA_ERROR_NOT_SUPPORTED;
312 goto exit;
313 }
314 status = mbedtls_to_psa_error(
315 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
316 ciphertext_length - operation.tag_length,
317 nonce,
318 additional_data,
319 additional_data_length,
320 tag,
321 ciphertext,
322 plaintext ) );
323 }
324 else
325#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
326 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200327 (void) nonce;
328 (void) nonce_length;
329 (void) additional_data;
330 (void) additional_data_length;
331 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100332 return( PSA_ERROR_NOT_SUPPORTED );
333 }
334
335 if( status == PSA_SUCCESS )
336 *plaintext_length = ciphertext_length - operation.tag_length;
337
338exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100339 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100340
341 if( status == PSA_SUCCESS )
342 *plaintext_length = ciphertext_length - operation.tag_length;
343 return( status );
344}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100345
Paul Elliottadb8b162021-04-20 16:06:57 +0100346/* Set the key and algorithm for a multipart authenticated encryption
347 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100348psa_status_t mbedtls_psa_aead_encrypt_setup(
349 mbedtls_psa_aead_operation_t *operation,
350 const psa_key_attributes_t *attributes,
351 const uint8_t *key_buffer,
352 size_t key_buffer_size,
353 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100354{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100356
Paul Elliottcc358592021-05-12 12:22:28 +0100357 status = psa_aead_setup( operation, attributes, key_buffer,
358 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100359
360 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100361 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100362
363 return ( status );
364}
365
366/* Set the key and algorithm for a multipart authenticated decryption
367 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100368psa_status_t mbedtls_psa_aead_decrypt_setup(
369 mbedtls_psa_aead_operation_t *operation,
370 const psa_key_attributes_t *attributes,
371 const uint8_t *key_buffer,
372 size_t key_buffer_size,
373 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100374{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100376
Paul Elliottcc358592021-05-12 12:22:28 +0100377 status = psa_aead_setup( operation, attributes, key_buffer,
378 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100379
380 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100381 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100382
383 return ( status );
384}
385
Paul Elliottadb8b162021-04-20 16:06:57 +0100386/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100387psa_status_t mbedtls_psa_aead_set_nonce(
388 mbedtls_psa_aead_operation_t *operation,
389 const uint8_t *nonce,
390 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100391{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100393
Paul Elliottbc949782021-06-03 15:29:00 +0100394#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100395 if( operation->alg == PSA_ALG_GCM )
396 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100397 status = mbedtls_to_psa_error(
398 mbedtls_gcm_starts( &operation->ctx.gcm,
399 operation->is_encrypt ?
400 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
401 nonce,
402 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100403 }
404 else
405#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100406#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
407 if( operation->alg == PSA_ALG_CCM )
408 {
409 status = mbedtls_to_psa_error(
410 mbedtls_ccm_starts( &operation->ctx.ccm,
411 operation->is_encrypt ?
412 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
413 nonce,
414 nonce_length ) );
415 }
416 else
417#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100418#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
419 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
420 {
Paul Elliott946c9202021-09-28 14:32:55 +0100421 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
422 * allocate a buffer in the operation, copy the nonce to it and pad
423 * it, so for now check the nonce is 12 bytes, as
424 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
425 * passed in buffer. */
426 if( nonce_length != 12 )
427 {
428 return( PSA_ERROR_INVALID_ARGUMENT );
429 }
430
Paul Elliott2df40052021-05-07 17:52:18 +0100431 status = mbedtls_to_psa_error(
432 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
433 nonce,
434 operation->is_encrypt ?
435 MBEDTLS_CHACHAPOLY_ENCRYPT :
436 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottefda3402021-08-25 17:16:52 +0100437 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100438 else
439#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
440 {
Ronald Cron17006702021-12-03 15:25:24 +0100441 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100442 ( void ) nonce;
Ronald Cron17006702021-12-03 15:25:24 +0100443 ( void ) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100444
445 return ( PSA_ERROR_NOT_SUPPORTED );
446 }
447
Paul Elliottadb8b162021-04-20 16:06:57 +0100448 return( status );
449}
Paul Elliottefda3402021-08-25 17:16:52 +0100450
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100451 /* Declare the lengths of the message and additional data for AEAD. */
452psa_status_t mbedtls_psa_aead_set_lengths(
453 mbedtls_psa_aead_operation_t *operation,
454 size_t ad_length,
455 size_t plaintext_length )
456{
Paul Elliotte193ea82021-10-01 13:00:16 +0100457#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
458 if( operation->alg == PSA_ALG_CCM )
459 {
460 return( mbedtls_to_psa_error(
461 mbedtls_ccm_set_lengths( &operation->ctx.ccm,
462 ad_length,
463 plaintext_length,
464 operation->tag_length ) ) );
465
466 }
467#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100468 ( void ) operation;
469 ( void ) ad_length;
470 ( void ) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100471#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100472
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100473 return ( PSA_SUCCESS );
474}
475
Paul Elliottadb8b162021-04-20 16:06:57 +0100476/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100477psa_status_t mbedtls_psa_aead_update_ad(
478 mbedtls_psa_aead_operation_t *operation,
479 const uint8_t *input,
480 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100481{
482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
483
Paul Elliottadb8b162021-04-20 16:06:57 +0100484#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
485 if( operation->alg == PSA_ALG_GCM )
486 {
Paul Elliott2df40052021-05-07 17:52:18 +0100487 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100488 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100489 }
490 else
491#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100492#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
493 if( operation->alg == PSA_ALG_CCM )
494 {
495 status = mbedtls_to_psa_error(
496 mbedtls_ccm_update_ad( &operation->ctx.ccm, input, input_length ) );
497 }
498 else
499#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100500#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
501 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
502 {
Paul Elliott2df40052021-05-07 17:52:18 +0100503 status = mbedtls_to_psa_error(
504 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
505 input,
506 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100507 }
508 else
509#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
510 {
Paul Elliottbc949782021-06-03 15:29:00 +0100511 ( void ) operation;
512 ( void ) input;
513 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100514
515 return ( PSA_ERROR_NOT_SUPPORTED );
516 }
517
Paul Elliottadb8b162021-04-20 16:06:57 +0100518 return ( status );
519}
520
521/* Encrypt or decrypt a message fragment in an active multipart AEAD
522 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100523psa_status_t mbedtls_psa_aead_update(
524 mbedtls_psa_aead_operation_t *operation,
525 const uint8_t *input,
526 size_t input_length,
527 uint8_t *output,
528 size_t output_size,
529 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100530{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100531 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
533
Paul Elliotte2c788d2021-05-13 17:16:01 +0100534 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100535
Paul Elliottadb8b162021-04-20 16:06:57 +0100536#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
537 if( operation->alg == PSA_ALG_GCM )
538 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100539 status = mbedtls_to_psa_error(
540 mbedtls_gcm_update( &operation->ctx.gcm,
541 input, input_length,
542 output, output_size,
543 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100544 }
545 else
546#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100547#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
548 if( operation->alg == PSA_ALG_CCM )
549 {
550 if( output_size < input_length )
551 return( PSA_ERROR_BUFFER_TOO_SMALL );
552
553 status = mbedtls_to_psa_error(
554 mbedtls_ccm_update( &operation->ctx.ccm,
555 input, input_length,
556 output, output_size,
557 &update_output_length ) );
558 }
559 else
560#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100561#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
562 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
563 {
Paul Elliottecce9012021-07-23 15:44:11 +0100564 if( output_size < input_length )
565 return( PSA_ERROR_BUFFER_TOO_SMALL );
566
Paul Elliott2df40052021-05-07 17:52:18 +0100567 status = mbedtls_to_psa_error(
568 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
569 input_length,
570 input,
571 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100572 }
573 else
574#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
575 {
Ronald Cron17006702021-12-03 15:25:24 +0100576 ( void ) operation;
Paul Elliottbc949782021-06-03 15:29:00 +0100577 ( void ) input;
Ronald Cron17006702021-12-03 15:25:24 +0100578 ( void ) output;
579 ( void ) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100580
581 return ( PSA_ERROR_NOT_SUPPORTED );
582 }
583
584 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100585 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100586
587 return( status );
588}
589
Paul Elliottadb8b162021-04-20 16:06:57 +0100590/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100591psa_status_t mbedtls_psa_aead_finish(
592 mbedtls_psa_aead_operation_t *operation,
593 uint8_t *ciphertext,
594 size_t ciphertext_size,
595 size_t *ciphertext_length,
596 uint8_t *tag,
597 size_t tag_size,
598 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100599{
600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100601 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100602
Paul Elliott315628d2021-07-20 18:25:54 +0100603 if( tag_size < operation->tag_length )
604 return( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100605
606#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
607 if( operation->alg == PSA_ALG_GCM )
Paul Elliottecce9012021-07-23 15:44:11 +0100608 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100609 status = mbedtls_to_psa_error(
610 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott71b05672021-09-24 11:18:13 +0100611 ciphertext, ciphertext_size, ciphertext_length,
Paul Elliott2fe5db82021-07-22 18:10:43 +0100612 tag, operation->tag_length ) );
Paul Elliottecce9012021-07-23 15:44:11 +0100613 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100614 else
615#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100616#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
617 if( operation->alg == PSA_ALG_CCM )
618 {
619 /* tag must be big enough to store a tag of size passed into set
620 * lengths. */
621 if( tag_size < operation->tag_length )
622 return( PSA_ERROR_BUFFER_TOO_SMALL );
623
624 status = mbedtls_to_psa_error(
625 mbedtls_ccm_finish( &operation->ctx.ccm,
626 tag, operation->tag_length ) );
627 }
628 else
629#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100630#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
631 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliotted08cf82021-07-22 18:48:24 +0100632 {
633 /* Belt and braces. Although the above tag_size check should have
634 * already done this, if we later start supporting smaller tag sizes
635 * for chachapoly, then passing a tag buffer smaller than 16 into here
636 * could cause a buffer overflow, so better safe than sorry. */
637 if( tag_size < 16 )
638 return( PSA_ERROR_BUFFER_TOO_SMALL );
639
Paul Elliott2df40052021-05-07 17:52:18 +0100640 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100641 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
642 tag ) );
Paul Elliotted08cf82021-07-22 18:48:24 +0100643 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100644 else
645#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
646 {
647 ( void ) ciphertext;
648 ( void ) ciphertext_size;
649 ( void ) ciphertext_length;
650 ( void ) tag;
651 ( void ) tag_size;
652 ( void ) tag_length;
653
654 return ( PSA_ERROR_NOT_SUPPORTED );
655 }
656
657 if( status == PSA_SUCCESS )
658 {
Paul Elliott8ff74212021-09-19 18:39:23 +0100659 /* This will be zero for all supported algorithms currently, but left
660 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100661 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100662 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100663 }
664
Paul Elliottadb8b162021-04-20 16:06:57 +0100665 return ( status );
666}
667
Paul Elliottadb8b162021-04-20 16:06:57 +0100668/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100669psa_status_t mbedtls_psa_aead_abort(
670 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100671{
Paul Elliott811d8d42021-04-22 11:31:14 +0100672 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100673 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100674#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
675 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100676 mbedtls_ccm_free( &operation->ctx.ccm );
677 break;
678#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
679#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
680 case PSA_ALG_GCM:
681 mbedtls_gcm_free( &operation->ctx.gcm );
682 break;
683#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
684#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100685 case PSA_ALG_CHACHA20_POLY1305:
686 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
687 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100688#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
689 }
690
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100691 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100692
Paul Elliottadb8b162021-04-20 16:06:57 +0100693 return( PSA_SUCCESS );
694}
695
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100696#endif /* MBEDTLS_PSA_CRYPTO_C */
697