blob: fb86775e515d224a589a03522faba7d8203fa457 [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
139psa_status_t mbedtls_psa_aead_encrypt(
140 const psa_key_attributes_t *attributes,
141 const uint8_t *key_buffer, size_t key_buffer_size,
142 psa_algorithm_t alg,
143 const uint8_t *nonce, size_t nonce_length,
144 const uint8_t *additional_data, size_t additional_data_length,
145 const uint8_t *plaintext, size_t plaintext_length,
146 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
147{
148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100149 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100150 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100151
Paul Elliottcc358592021-05-12 12:22:28 +0100152 status = psa_aead_setup( &operation, attributes, key_buffer,
153 key_buffer_size, alg );
154
Ronald Cron46f91782021-03-17 08:16:34 +0100155 if( status != PSA_SUCCESS )
156 goto exit;
157
158 /* For all currently supported modes, the tag is at the end of the
159 * ciphertext. */
160 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
161 {
162 status = PSA_ERROR_BUFFER_TOO_SMALL;
163 goto exit;
164 }
165 tag = ciphertext + plaintext_length;
166
Ronald Cron46f91782021-03-17 08:16:34 +0100167#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100168 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100169 {
170 status = mbedtls_to_psa_error(
171 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
172 plaintext_length,
173 nonce, nonce_length,
174 additional_data,
175 additional_data_length,
176 plaintext, ciphertext,
177 tag, operation.tag_length ) );
178 }
179 else
180#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200181#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100182 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200183 {
184 status = mbedtls_to_psa_error(
185 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
186 MBEDTLS_GCM_ENCRYPT,
187 plaintext_length,
188 nonce, nonce_length,
189 additional_data, additional_data_length,
190 plaintext, ciphertext,
191 operation.tag_length, tag ) );
192 }
193 else
194#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100195#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100196 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100197 {
198 if( nonce_length != 12 || operation.tag_length != 16 )
199 {
200 status = PSA_ERROR_NOT_SUPPORTED;
201 goto exit;
202 }
203 status = mbedtls_to_psa_error(
204 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
205 plaintext_length,
206 nonce,
207 additional_data,
208 additional_data_length,
209 plaintext,
210 ciphertext,
211 tag ) );
212 }
213 else
214#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
215 {
216 (void) tag;
217 return( PSA_ERROR_NOT_SUPPORTED );
218 }
219
220 if( status == PSA_SUCCESS )
221 *ciphertext_length = plaintext_length + operation.tag_length;
222
223exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100224 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100225
226 return( status );
227}
228
229/* Locate the tag in a ciphertext buffer containing the encrypted data
230 * followed by the tag. Return the length of the part preceding the tag in
231 * *plaintext_length. This is the size of the plaintext in modes where
232 * the encrypted data has the same size as the plaintext, such as
233 * CCM and GCM. */
234static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
235 const uint8_t *ciphertext,
236 size_t ciphertext_length,
237 size_t plaintext_size,
238 const uint8_t **p_tag )
239{
240 size_t payload_length;
241 if( tag_length > ciphertext_length )
242 return( PSA_ERROR_INVALID_ARGUMENT );
243 payload_length = ciphertext_length - tag_length;
244 if( payload_length > plaintext_size )
245 return( PSA_ERROR_BUFFER_TOO_SMALL );
246 *p_tag = ciphertext + payload_length;
247 return( PSA_SUCCESS );
248}
249
250psa_status_t mbedtls_psa_aead_decrypt(
251 const psa_key_attributes_t *attributes,
252 const uint8_t *key_buffer, size_t key_buffer_size,
253 psa_algorithm_t alg,
254 const uint8_t *nonce, size_t nonce_length,
255 const uint8_t *additional_data, size_t additional_data_length,
256 const uint8_t *ciphertext, size_t ciphertext_length,
257 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
258{
259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100260 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100261 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100262
Paul Elliottcc358592021-05-12 12:22:28 +0100263 status = psa_aead_setup( &operation, attributes, key_buffer,
264 key_buffer_size, alg );
265
Ronald Cron46f91782021-03-17 08:16:34 +0100266 if( status != PSA_SUCCESS )
267 goto exit;
268
269 status = psa_aead_unpadded_locate_tag( operation.tag_length,
270 ciphertext, ciphertext_length,
271 plaintext_size, &tag );
272 if( status != PSA_SUCCESS )
273 goto exit;
274
Ronald Cron46f91782021-03-17 08:16:34 +0100275#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100276 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100277 {
278 status = mbedtls_to_psa_error(
279 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
280 ciphertext_length - operation.tag_length,
281 nonce, nonce_length,
282 additional_data,
283 additional_data_length,
284 ciphertext, plaintext,
285 tag, operation.tag_length ) );
286 }
287 else
288#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200289#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100290 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200291 {
292 status = mbedtls_to_psa_error(
293 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
294 ciphertext_length - operation.tag_length,
295 nonce, nonce_length,
296 additional_data,
297 additional_data_length,
298 tag, operation.tag_length,
299 ciphertext, plaintext ) );
300 }
301 else
302#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100303#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100304 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100305 {
306 if( nonce_length != 12 || operation.tag_length != 16 )
307 {
308 status = PSA_ERROR_NOT_SUPPORTED;
309 goto exit;
310 }
311 status = mbedtls_to_psa_error(
312 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
313 ciphertext_length - operation.tag_length,
314 nonce,
315 additional_data,
316 additional_data_length,
317 tag,
318 ciphertext,
319 plaintext ) );
320 }
321 else
322#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
323 {
324 return( PSA_ERROR_NOT_SUPPORTED );
325 }
326
327 if( status == PSA_SUCCESS )
328 *plaintext_length = ciphertext_length - operation.tag_length;
329
330exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100331 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100332
333 if( status == PSA_SUCCESS )
334 *plaintext_length = ciphertext_length - operation.tag_length;
335 return( status );
336}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100337
Paul Elliottadb8b162021-04-20 16:06:57 +0100338/* Set the key and algorithm for a multipart authenticated encryption
339 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100340psa_status_t mbedtls_psa_aead_encrypt_setup(
341 mbedtls_psa_aead_operation_t *operation,
342 const psa_key_attributes_t *attributes,
343 const uint8_t *key_buffer,
344 size_t key_buffer_size,
345 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100346{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100348
Paul Elliott60aa2032021-05-20 18:57:02 +0100349#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
350 if( operation->alg == PSA_ALG_CCM )
351 {
352 return ( PSA_ERROR_NOT_SUPPORTED );
353 }
354#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
355
Paul Elliottcc358592021-05-12 12:22:28 +0100356 status = psa_aead_setup( operation, attributes, key_buffer,
357 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100358
359 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100360 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100361
362 return ( status );
363}
364
365/* Set the key and algorithm for a multipart authenticated decryption
366 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100367psa_status_t mbedtls_psa_aead_decrypt_setup(
368 mbedtls_psa_aead_operation_t *operation,
369 const psa_key_attributes_t *attributes,
370 const uint8_t *key_buffer,
371 size_t key_buffer_size,
372 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100373{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100375
Paul Elliott60aa2032021-05-20 18:57:02 +0100376 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
377 if( operation->alg == PSA_ALG_CCM )
378 {
379 return ( PSA_ERROR_NOT_SUPPORTED );
380 }
381#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100382
Paul Elliottcc358592021-05-12 12:22:28 +0100383 status = psa_aead_setup( operation, attributes, key_buffer,
384 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100385
386 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100387 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100388
389 return ( status );
390}
391
Paul Elliottadb8b162021-04-20 16:06:57 +0100392/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100393psa_status_t mbedtls_psa_aead_set_nonce(
394 mbedtls_psa_aead_operation_t *operation,
395 const uint8_t *nonce,
396 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100397{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100399
Paul Elliottadb8b162021-04-20 16:06:57 +0100400 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
401 if( operation->alg == PSA_ALG_GCM )
402 {
Paul Elliott1a98aca2021-05-20 18:24:07 +0100403 operation->nonce = mbedtls_calloc( 1, nonce_length );
404
405 if( operation->nonce == NULL )
406 return( PSA_ERROR_INSUFFICIENT_MEMORY );
407
Paul Elliottadb8b162021-04-20 16:06:57 +0100408 /* GCM sets nonce once additional data has been supplied */
Paul Elliotte9eeea32021-05-19 14:32:58 +0100409 memcpy( operation->nonce, nonce, nonce_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100410
411 /* We know that nonce size cannot exceed the uint8_t size */
Paul Elliott1a98aca2021-05-20 18:24:07 +0100412 operation->nonce_length = nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100413 status = PSA_SUCCESS;
414 }
415 else
416#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
417#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
418 if( operation->alg == PSA_ALG_CCM )
419 {
Paul Elliott60aa2032021-05-20 18:57:02 +0100420 ( void ) nonce;
421 ( void ) nonce_length;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100422
Paul Elliott60aa2032021-05-20 18:57:02 +0100423 return ( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100424 }
425 else
426#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
427#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
428 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
429 {
430 if( nonce_length != 12 && nonce_length != 8)
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 Elliottadb8b162021-04-20 16:06:57 +0100441 }
442 else
443#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
444 {
445 ( void ) nonce;
446 ( void ) nonce_length;
447
448 return ( PSA_ERROR_NOT_SUPPORTED );
449 }
450
Paul Elliottadb8b162021-04-20 16:06:57 +0100451 return( status );
452}
453 /* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100454psa_status_t mbedtls_psa_aead_set_lengths(
455 mbedtls_psa_aead_operation_t *operation,
456 size_t ad_length,
457 size_t plaintext_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100458{
459
Paul Elliottadb8b162021-04-20 16:06:57 +0100460#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
461 if( operation->alg == PSA_ALG_GCM )
462 {
Paul Elliottef29e172021-05-10 19:33:03 +0100463 /* Lengths can only be too large for GCM if size_t is bigger than 32
Paul Elliotte9eeea32021-05-19 14:32:58 +0100464 * bits. Without the guard this code will generate warnings on 32bit
465 builds */
Paul Elliottadb8b162021-04-20 16:06:57 +0100466#if SIZE_MAX > UINT32_MAX
467 if( ( (uint64_t) ad_length ) >> 61 != 0 ||
468 ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull )
469 {
470 return ( PSA_ERROR_INVALID_ARGUMENT );
471 }
472#endif
473 }
474 else
475#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
476#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
477 if( operation->alg == PSA_ALG_CCM )
478 {
479 if( ad_length > 0xFF00 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100480 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100481 }
482 else
483#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
484#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
485 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
486 {
487 /* No length restrictions for ChaChaPoly. */
488 }
489 else
490#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
491 {
492 ( void ) ad_length;
493 ( void ) plaintext_length;
494
495 return ( PSA_ERROR_NOT_SUPPORTED );
496 }
497
Paul Elliottadb8b162021-04-20 16:06:57 +0100498 return ( PSA_SUCCESS );
499}
500
501/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100502psa_status_t mbedtls_psa_aead_update_ad(
503 mbedtls_psa_aead_operation_t *operation,
504 const uint8_t *input,
505 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100506{
507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
508
Paul Elliottadb8b162021-04-20 16:06:57 +0100509#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
510 if( operation->alg == PSA_ALG_GCM )
511 {
Paul Elliotte9eeea32021-05-19 14:32:58 +0100512 /* GCM currently requires all the additional data to be passed in
513 * in one contiguous buffer, so until that is re-done, we have to
514 * enforce this, as we cannot allocate a buffer to collate multiple
515 * calls into. */
Paul Elliottc10ad212021-05-13 17:08:29 +0100516 if( operation->ad_started )
517 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100518
Paul Elliott2df40052021-05-07 17:52:18 +0100519 status = mbedtls_to_psa_error(
520 mbedtls_gcm_starts( &operation->ctx.gcm,
521 operation->is_encrypt ?
522 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
523 operation->nonce,
524 operation->nonce_length,
525 input,
526 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100527
528 }
529 else
530#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
531#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
532 if( operation->alg == PSA_ALG_CCM )
533 {
Paul Elliott60aa2032021-05-20 18:57:02 +0100534 (void) input;
535 (void) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100536
Paul Elliott60aa2032021-05-20 18:57:02 +0100537 return ( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100538 }
539 else
540#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
541#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
542 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
543 {
Paul Elliott2df40052021-05-07 17:52:18 +0100544 status = mbedtls_to_psa_error(
545 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
546 input,
547 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100548 }
549 else
550#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
551 {
552 (void) input;
553 (void) input_length;
554
555 return ( PSA_ERROR_NOT_SUPPORTED );
556 }
557
558 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100559 operation->ad_started = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100560
561 return ( status );
562}
563
564/* Encrypt or decrypt a message fragment in an active multipart AEAD
565 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100566psa_status_t mbedtls_psa_aead_update(
567 mbedtls_psa_aead_operation_t *operation,
568 const uint8_t *input,
569 size_t input_length,
570 uint8_t *output,
571 size_t output_size,
572 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100573{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100574 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
576
Paul Elliotte2c788d2021-05-13 17:16:01 +0100577 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100578
Paul Elliott72c10082021-04-23 19:02:16 +0100579 if( PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, operation->alg,
580 input_length ) > output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100581 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100582
Paul Elliottadb8b162021-04-20 16:06:57 +0100583#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
584 if( operation->alg == PSA_ALG_GCM )
585 {
586 /* For the time being set the requirement that all of the body data
587 * must be passed in in one update, rather than deal with the complexity
588 * of non block size aligned updates. This will be fixed in 3.0 when
589 we can change the signature of the GCM multipart functions */
Paul Elliottc10ad212021-05-13 17:08:29 +0100590 if( operation->body_started )
591 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100592
Paul Elliottadb8b162021-04-20 16:06:57 +0100593
594 status = mbedtls_to_psa_error( mbedtls_gcm_update( &operation->ctx.gcm,
595 input_length,
596 input,
597 output ) );
598 }
599 else
600#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
601#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
602 if( operation->alg == PSA_ALG_CCM )
603 {
Paul Elliott60aa2032021-05-20 18:57:02 +0100604 (void) input;
605 (void) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100606
Paul Elliott60aa2032021-05-20 18:57:02 +0100607 return ( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100608 }
609 else
610#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
611#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
612 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
613 {
Paul Elliott2df40052021-05-07 17:52:18 +0100614 status = mbedtls_to_psa_error(
615 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
616 input_length,
617 input,
618 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100619 }
620 else
621#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
622 {
623 (void) input;
624 (void) input_length;
625
626 return ( PSA_ERROR_NOT_SUPPORTED );
627 }
628
629 if( status == PSA_SUCCESS )
630 {
Paul Elliotte2c788d2021-05-13 17:16:01 +0100631 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100632 operation->body_started = 1;
633 }
634
635 return( status );
636}
637
638/* Common checks for both mbedtls_psa_aead_finish() and
639 mbedtls_psa_aead_verify() */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100640static psa_status_t mbedtls_psa_aead_finish_checks(
641 mbedtls_psa_aead_operation_t *operation,
642 size_t output_size,
643 size_t tag_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100644{
Paul Elliottfd3ca242021-04-25 18:10:42 +0100645 size_t finish_output_size;
646
Paul Elliottfd3ca242021-04-25 18:10:42 +0100647 if( tag_size < operation->tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100648 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100649
Paul Elliott80acb7e2021-05-12 12:41:33 +0100650 finish_output_size = operation->is_encrypt ?
651 PSA_AEAD_FINISH_OUTPUT_SIZE( operation->key_type, operation->alg ) :
652 PSA_AEAD_VERIFY_OUTPUT_SIZE( operation->key_type, operation->alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100653
Paul Elliottfd3ca242021-04-25 18:10:42 +0100654 if( output_size < finish_output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100655 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100656
657 return ( PSA_SUCCESS );
Paul Elliottadb8b162021-04-20 16:06:57 +0100658}
659
660/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100661psa_status_t mbedtls_psa_aead_finish(
662 mbedtls_psa_aead_operation_t *operation,
663 uint8_t *ciphertext,
664 size_t ciphertext_size,
665 size_t *ciphertext_length,
666 uint8_t *tag,
667 size_t tag_size,
668 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100669{
670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100671 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100672
Paul Elliott2df40052021-05-07 17:52:18 +0100673 status = mbedtls_psa_aead_finish_checks( operation, ciphertext_size,
674 tag_size );
Paul Elliottadb8b162021-04-20 16:06:57 +0100675
676 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100677 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100678
679#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
680 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100681 /* We will need to do final GCM pass in here when multipart is done. */
682 status = mbedtls_to_psa_error( mbedtls_gcm_finish( &operation->ctx.gcm,
683 tag,
684 tag_size ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100685 else
686#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
687#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
688 if( operation->alg == PSA_ALG_CCM )
689 {
Paul Elliott60aa2032021-05-20 18:57:02 +0100690 ( void ) ciphertext;
691 ( void ) ciphertext_size;
692 ( void ) ciphertext_length;
693 ( void ) tag;
694 ( void ) tag_size;
695 ( void ) tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100696
Paul Elliott60aa2032021-05-20 18:57:02 +0100697 return ( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100698 }
699 else
700#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
701#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
702 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliott2df40052021-05-07 17:52:18 +0100703 status = mbedtls_to_psa_error(
704 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
705 tag ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100706 else
707#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
708 {
709 ( void ) ciphertext;
710 ( void ) ciphertext_size;
711 ( void ) ciphertext_length;
712 ( void ) tag;
713 ( void ) tag_size;
714 ( void ) tag_length;
715
716 return ( PSA_ERROR_NOT_SUPPORTED );
717 }
718
719 if( status == PSA_SUCCESS )
720 {
721 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100722 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100723 }
724
Paul Elliottadb8b162021-04-20 16:06:57 +0100725 return ( status );
726}
727
728/* Finish authenticating and decrypting a message in a multipart AEAD
729 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100730psa_status_t mbedtls_psa_aead_verify(
731 mbedtls_psa_aead_operation_t *operation,
732 uint8_t *plaintext,
733 size_t plaintext_size,
734 size_t *plaintext_length,
735 const uint8_t *tag,
736 size_t tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100737{
738 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100739
740 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100741
742 int do_tag_check = 1;
Paul Elliottccaea402021-05-13 14:22:52 +0100743 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottadb8b162021-04-20 16:06:57 +0100744
Paul Elliott2df40052021-05-07 17:52:18 +0100745 status = mbedtls_psa_aead_finish_checks( operation, plaintext_size,
746 tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100747
748 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100749 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100750
751#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
752 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100753 /* Call finish to get the tag for comparison */
Paul Elliott2df40052021-05-07 17:52:18 +0100754 status = mbedtls_to_psa_error(
755 mbedtls_gcm_finish( &operation->ctx.gcm,
756 check_tag,
757 operation->tag_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100758 else
759#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
760#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
761 if( operation->alg == PSA_ALG_CCM )
762 {
Paul Elliott60aa2032021-05-20 18:57:02 +0100763 ( void ) plaintext;
764 ( void ) plaintext_size;
765 ( void ) plaintext_length;
766 ( void ) tag;
767 ( void ) tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100768
Paul Elliott60aa2032021-05-20 18:57:02 +0100769 return ( PSA_ERROR_NOT_SUPPORTED );
Paul Elliottadb8b162021-04-20 16:06:57 +0100770 }
771 else
772#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
773#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
774 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100775 // call finish to get the tag for comparison.
Paul Elliott2df40052021-05-07 17:52:18 +0100776 status = mbedtls_to_psa_error(
777 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
778 check_tag ) );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100779
Paul Elliottadb8b162021-04-20 16:06:57 +0100780 else
781#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
782 {
783 ( void ) plaintext;
784 ( void ) plaintext_size;
785 ( void ) plaintext_length;
786 ( void ) tag;
787 ( void ) tag_length;
788
789 return ( PSA_ERROR_NOT_SUPPORTED );
790 }
791
792 if( status == PSA_SUCCESS )
793 {
Paul Elliott72c10082021-04-23 19:02:16 +0100794 *plaintext_length = finish_output_size;
795
Paul Elliott6edb7472021-05-10 19:29:35 +0100796 if( do_tag_check &&
797 mbedtls_psa_safer_memcmp(tag, check_tag, tag_length) != 0 )
Paul Elliott811d8d42021-04-22 11:31:14 +0100798 status = PSA_ERROR_INVALID_SIGNATURE;
Paul Elliottadb8b162021-04-20 16:06:57 +0100799 }
800
Paul Elliottadb8b162021-04-20 16:06:57 +0100801 return ( status );
802}
803
804/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100805psa_status_t mbedtls_psa_aead_abort(
806 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100807{
Paul Elliott811d8d42021-04-22 11:31:14 +0100808 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100809 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100810#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
811 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100812 mbedtls_ccm_free( &operation->ctx.ccm );
813 break;
814#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
815#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
816 case PSA_ALG_GCM:
817 mbedtls_gcm_free( &operation->ctx.gcm );
818 break;
819#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
820#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100821 case PSA_ALG_CHACHA20_POLY1305:
822 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
823 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100824#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
825 }
826
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100827 operation->is_encrypt = 0;
828 operation->ad_started = 0;
829 operation->body_started = 0;
830
Paul Elliott80acb7e2021-05-12 12:41:33 +0100831 mbedtls_free( operation->ad_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100832 operation->ad_buffer = NULL;
833 operation->ad_length = 0;
834
Paul Elliott80acb7e2021-05-12 12:41:33 +0100835 mbedtls_free( operation->body_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100836 operation->body_buffer = NULL;
837 operation->body_length = 0;
838
Paul Elliott80acb7e2021-05-12 12:41:33 +0100839 mbedtls_free( operation->tag_buffer );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100840 operation->tag_buffer = NULL;
841
Paul Elliott1a98aca2021-05-20 18:24:07 +0100842 mbedtls_free( operation->nonce );
843 operation->nonce = NULL;
844 operation->nonce_length = 0;
845
Paul Elliottadb8b162021-04-20 16:06:57 +0100846 return( PSA_SUCCESS );
847}
848
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100849#endif /* MBEDTLS_PSA_CRYPTO_C */
850