blob: 1a515a14af736785bbe1022a2c34bb9cf885fe9e [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
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100250static psa_status_t mbedtls_aead_check_nonce_length(
251 mbedtls_psa_aead_operation_t *operation,
252 size_t nonce_length )
253{
254#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
255 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
256 {
257 if( nonce_length != 12 )
258 return( PSA_ERROR_NOT_SUPPORTED );
259 }
260#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
261
262 return PSA_SUCCESS;
263}
264
Ronald Cron46f91782021-03-17 08:16:34 +0100265psa_status_t mbedtls_psa_aead_decrypt(
266 const psa_key_attributes_t *attributes,
267 const uint8_t *key_buffer, size_t key_buffer_size,
268 psa_algorithm_t alg,
269 const uint8_t *nonce, size_t nonce_length,
270 const uint8_t *additional_data, size_t additional_data_length,
271 const uint8_t *ciphertext, size_t ciphertext_length,
272 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
273{
274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100275 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100276 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100277
Paul Elliottcc358592021-05-12 12:22:28 +0100278 status = psa_aead_setup( &operation, attributes, key_buffer,
279 key_buffer_size, alg );
280
Ronald Cron46f91782021-03-17 08:16:34 +0100281 if( status != PSA_SUCCESS )
282 goto exit;
283
284 status = psa_aead_unpadded_locate_tag( operation.tag_length,
285 ciphertext, ciphertext_length,
286 plaintext_size, &tag );
287 if( status != PSA_SUCCESS )
288 goto exit;
289
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100290 if( mbedtls_aead_check_nonce_length( &operation, nonce_length )
291 != PSA_SUCCESS)
292 {
293 status = PSA_ERROR_NOT_SUPPORTED;
294 goto exit;
295 }
296
Ronald Cron46f91782021-03-17 08:16:34 +0100297#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100298 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100299 {
300 status = mbedtls_to_psa_error(
301 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
302 ciphertext_length - operation.tag_length,
303 nonce, nonce_length,
304 additional_data,
305 additional_data_length,
306 ciphertext, plaintext,
307 tag, operation.tag_length ) );
308 }
309 else
310#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200311#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100312 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200313 {
314 status = mbedtls_to_psa_error(
315 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
316 ciphertext_length - operation.tag_length,
317 nonce, nonce_length,
318 additional_data,
319 additional_data_length,
320 tag, operation.tag_length,
321 ciphertext, plaintext ) );
322 }
323 else
324#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100325#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100326 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100327 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100328 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100329 {
330 status = PSA_ERROR_NOT_SUPPORTED;
331 goto exit;
332 }
333 status = mbedtls_to_psa_error(
334 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
335 ciphertext_length - operation.tag_length,
336 nonce,
337 additional_data,
338 additional_data_length,
339 tag,
340 ciphertext,
341 plaintext ) );
342 }
343 else
344#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
345 {
346 return( PSA_ERROR_NOT_SUPPORTED );
347 }
348
349 if( status == PSA_SUCCESS )
350 *plaintext_length = ciphertext_length - operation.tag_length;
351
352exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100353 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100354
355 if( status == PSA_SUCCESS )
356 *plaintext_length = ciphertext_length - operation.tag_length;
357 return( status );
358}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100359
Paul Elliottadb8b162021-04-20 16:06:57 +0100360/* Set the key and algorithm for a multipart authenticated encryption
361 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100362psa_status_t mbedtls_psa_aead_encrypt_setup(
363 mbedtls_psa_aead_operation_t *operation,
364 const psa_key_attributes_t *attributes,
365 const uint8_t *key_buffer,
366 size_t key_buffer_size,
367 psa_algorithm_t alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100368{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100370
Paul Elliott60aa2032021-05-20 18:57:02 +0100371#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
372 if( operation->alg == PSA_ALG_CCM )
373 {
Paul Elliotte95259f2021-05-21 17:09:21 +0100374 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliott60aa2032021-05-20 18:57:02 +0100375 }
376#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
377
Paul Elliottcc358592021-05-12 12:22:28 +0100378 status = psa_aead_setup( operation, attributes, key_buffer,
379 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100380
381 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100382 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100383
384 return ( status );
385}
386
387/* Set the key and algorithm for a multipart authenticated decryption
388 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100389psa_status_t mbedtls_psa_aead_decrypt_setup(
390 mbedtls_psa_aead_operation_t *operation,
391 const psa_key_attributes_t *attributes,
392 const uint8_t *key_buffer,
393 size_t key_buffer_size,
394 psa_algorithm_t alg )
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 Elliotte95259f2021-05-21 17:09:21 +0100398#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott60aa2032021-05-20 18:57:02 +0100399 if( operation->alg == PSA_ALG_CCM )
400 {
Paul Elliotte95259f2021-05-21 17:09:21 +0100401 return( PSA_ERROR_NOT_SUPPORTED );
Paul Elliott60aa2032021-05-20 18:57:02 +0100402 }
403#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100404
Paul Elliottcc358592021-05-12 12:22:28 +0100405 status = psa_aead_setup( operation, attributes, key_buffer,
406 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100407
408 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100409 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100410
411 return ( status );
412}
413
Paul Elliottadb8b162021-04-20 16:06:57 +0100414/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100415psa_status_t mbedtls_psa_aead_set_nonce(
416 mbedtls_psa_aead_operation_t *operation,
417 const uint8_t *nonce,
418 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100419{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100421
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100422 if( mbedtls_aead_check_nonce_length( operation, nonce_length )
423 != PSA_SUCCESS)
424 {
425 return( PSA_ERROR_INVALID_ARGUMENT );
426 }
427
Paul Elliottbc949782021-06-03 15:29:00 +0100428#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100429 if( operation->alg == PSA_ALG_GCM )
430 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100431 status = mbedtls_to_psa_error(
432 mbedtls_gcm_starts( &operation->ctx.gcm,
433 operation->is_encrypt ?
434 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
435 nonce,
436 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100437 }
438 else
439#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100440#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
441 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
442 {
Paul Elliott2df40052021-05-07 17:52:18 +0100443 status = mbedtls_to_psa_error(
444 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
445 nonce,
446 operation->is_encrypt ?
447 MBEDTLS_CHACHAPOLY_ENCRYPT :
448 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100449 }
450 else
451#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
452 {
Paul Elliottbc949782021-06-03 15:29:00 +0100453 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100454 ( void ) nonce;
455 ( void ) nonce_length;
456
457 return ( PSA_ERROR_NOT_SUPPORTED );
458 }
459
Paul Elliottadb8b162021-04-20 16:06:57 +0100460 return( status );
461}
462 /* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100463psa_status_t mbedtls_psa_aead_set_lengths(
464 mbedtls_psa_aead_operation_t *operation,
465 size_t ad_length,
466 size_t plaintext_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100467{
468
Paul Elliottadb8b162021-04-20 16:06:57 +0100469#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
470 if( operation->alg == PSA_ALG_GCM )
471 {
Paul Elliottef29e172021-05-10 19:33:03 +0100472 /* Lengths can only be too large for GCM if size_t is bigger than 32
Paul Elliotte9eeea32021-05-19 14:32:58 +0100473 * bits. Without the guard this code will generate warnings on 32bit
Paul Elliotte95259f2021-05-21 17:09:21 +0100474 * builds */
Paul Elliottadb8b162021-04-20 16:06:57 +0100475#if SIZE_MAX > UINT32_MAX
476 if( ( (uint64_t) ad_length ) >> 61 != 0 ||
477 ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull )
478 {
479 return ( PSA_ERROR_INVALID_ARGUMENT );
480 }
481#endif
482 }
483 else
484#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
485#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
486 if( operation->alg == PSA_ALG_CCM )
487 {
488 if( ad_length > 0xFF00 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100489 return ( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottadb8b162021-04-20 16:06:57 +0100490 }
491 else
492#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
493#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
494 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
495 {
496 /* No length restrictions for ChaChaPoly. */
497 }
498 else
499#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
500 {
Paul Elliottbc949782021-06-03 15:29:00 +0100501 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100502 ( void ) ad_length;
503 ( void ) plaintext_length;
504
505 return ( PSA_ERROR_NOT_SUPPORTED );
506 }
507
Paul Elliottadb8b162021-04-20 16:06:57 +0100508 return ( PSA_SUCCESS );
509}
510
511/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100512psa_status_t mbedtls_psa_aead_update_ad(
513 mbedtls_psa_aead_operation_t *operation,
514 const uint8_t *input,
515 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100516{
517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
518
Paul Elliottadb8b162021-04-20 16:06:57 +0100519#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
520 if( operation->alg == PSA_ALG_GCM )
521 {
Paul Elliott2df40052021-05-07 17:52:18 +0100522 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100523 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100524 }
525 else
526#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100527#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
528 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
529 {
Paul Elliott2df40052021-05-07 17:52:18 +0100530 status = mbedtls_to_psa_error(
531 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
532 input,
533 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100534 }
535 else
536#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
537 {
Paul Elliottbc949782021-06-03 15:29:00 +0100538 ( void ) operation;
539 ( void ) input;
540 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100541
542 return ( PSA_ERROR_NOT_SUPPORTED );
543 }
544
Paul Elliottadb8b162021-04-20 16:06:57 +0100545 return ( status );
546}
547
548/* Encrypt or decrypt a message fragment in an active multipart AEAD
549 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100550psa_status_t mbedtls_psa_aead_update(
551 mbedtls_psa_aead_operation_t *operation,
552 const uint8_t *input,
553 size_t input_length,
554 uint8_t *output,
555 size_t output_size,
556 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100557{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100558 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
560
Paul Elliotte2c788d2021-05-13 17:16:01 +0100561 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100562
Paul Elliott72c10082021-04-23 19:02:16 +0100563 if( PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, operation->alg,
564 input_length ) > output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100565 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100566
Paul Elliottadb8b162021-04-20 16:06:57 +0100567#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
568 if( operation->alg == PSA_ALG_GCM )
569 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100570 status = mbedtls_to_psa_error(
571 mbedtls_gcm_update( &operation->ctx.gcm,
572 input, input_length,
573 output, output_size,
574 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100575 }
576 else
577#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100578#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
579 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
580 {
Paul Elliott2df40052021-05-07 17:52:18 +0100581 status = mbedtls_to_psa_error(
582 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
583 input_length,
584 input,
585 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100586 }
587 else
588#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
589 {
Paul Elliottbc949782021-06-03 15:29:00 +0100590 ( void ) input;
591 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100592
593 return ( PSA_ERROR_NOT_SUPPORTED );
594 }
595
596 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100597 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100598
599 return( status );
600}
601
602/* Common checks for both mbedtls_psa_aead_finish() and
603 mbedtls_psa_aead_verify() */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100604static psa_status_t mbedtls_psa_aead_finish_checks(
605 mbedtls_psa_aead_operation_t *operation,
606 size_t output_size,
607 size_t tag_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100608{
Paul Elliottfd3ca242021-04-25 18:10:42 +0100609 size_t finish_output_size;
610
Paul Elliottfd3ca242021-04-25 18:10:42 +0100611 if( tag_size < operation->tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100612 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100613
Paul Elliott80acb7e2021-05-12 12:41:33 +0100614 finish_output_size = operation->is_encrypt ?
615 PSA_AEAD_FINISH_OUTPUT_SIZE( operation->key_type, operation->alg ) :
616 PSA_AEAD_VERIFY_OUTPUT_SIZE( operation->key_type, operation->alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100617
Paul Elliottfd3ca242021-04-25 18:10:42 +0100618 if( output_size < finish_output_size )
Paul Elliottadb8b162021-04-20 16:06:57 +0100619 return ( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100620
621 return ( PSA_SUCCESS );
Paul Elliottadb8b162021-04-20 16:06:57 +0100622}
623
624/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100625psa_status_t mbedtls_psa_aead_finish(
626 mbedtls_psa_aead_operation_t *operation,
627 uint8_t *ciphertext,
628 size_t ciphertext_size,
629 size_t *ciphertext_length,
630 uint8_t *tag,
631 size_t tag_size,
632 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100633{
634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100635 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100636
Paul Elliott2df40052021-05-07 17:52:18 +0100637 status = mbedtls_psa_aead_finish_checks( operation, ciphertext_size,
638 tag_size );
Paul Elliottadb8b162021-04-20 16:06:57 +0100639
640 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100641 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100642
643#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
644 if( operation->alg == PSA_ALG_GCM )
Paul Elliott83f09ef2021-05-21 19:28:26 +0100645 status = mbedtls_to_psa_error(
646 mbedtls_gcm_finish( &operation->ctx.gcm,
647 ciphertext, ciphertext_size,
648 tag, tag_size ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100649 else
650#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100651#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
652 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliott2df40052021-05-07 17:52:18 +0100653 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100654 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
655 tag ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100656 else
657#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
658 {
659 ( void ) ciphertext;
660 ( void ) ciphertext_size;
661 ( void ) ciphertext_length;
662 ( void ) tag;
663 ( void ) tag_size;
664 ( void ) tag_length;
665
666 return ( PSA_ERROR_NOT_SUPPORTED );
667 }
668
669 if( status == PSA_SUCCESS )
670 {
671 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100672 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100673 }
674
Paul Elliottadb8b162021-04-20 16:06:57 +0100675 return ( status );
676}
677
678/* Finish authenticating and decrypting a message in a multipart AEAD
679 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100680psa_status_t mbedtls_psa_aead_verify(
681 mbedtls_psa_aead_operation_t *operation,
682 uint8_t *plaintext,
683 size_t plaintext_size,
684 size_t *plaintext_length,
685 const uint8_t *tag,
686 size_t tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100687{
688 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100689 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100690 int do_tag_check = 1;
Paul Elliottccaea402021-05-13 14:22:52 +0100691 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottadb8b162021-04-20 16:06:57 +0100692
Paul Elliott2df40052021-05-07 17:52:18 +0100693 status = mbedtls_psa_aead_finish_checks( operation, plaintext_size,
694 tag_length );
Paul Elliottadb8b162021-04-20 16:06:57 +0100695
696 if( status != PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100697 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100698
699#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
700 if( operation->alg == PSA_ALG_GCM )
Paul Elliottadb8b162021-04-20 16:06:57 +0100701 /* Call finish to get the tag for comparison */
Paul Elliott2df40052021-05-07 17:52:18 +0100702 status = mbedtls_to_psa_error(
703 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott83f09ef2021-05-21 19:28:26 +0100704 plaintext, plaintext_size,
705 check_tag, operation->tag_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100706 else
707#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100708#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
709 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliottadb8b162021-04-20 16:06:57 +0100710 // call finish to get the tag for comparison.
Paul Elliott2df40052021-05-07 17:52:18 +0100711 status = mbedtls_to_psa_error(
712 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
713 check_tag ) );
Paul Elliottfd3ca242021-04-25 18:10:42 +0100714
Paul Elliottadb8b162021-04-20 16:06:57 +0100715 else
716#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
717 {
718 ( void ) plaintext;
719 ( void ) plaintext_size;
720 ( void ) plaintext_length;
721 ( void ) tag;
722 ( void ) tag_length;
723
724 return ( PSA_ERROR_NOT_SUPPORTED );
725 }
726
727 if( status == PSA_SUCCESS )
728 {
Paul Elliott72c10082021-04-23 19:02:16 +0100729 *plaintext_length = finish_output_size;
730
Paul Elliott3a16e012021-05-21 18:03:15 +0100731 if( do_tag_check && ( tag_length != operation->tag_length ||
732 mbedtls_psa_safer_memcmp(tag, check_tag, tag_length) != 0 ) )
Paul Elliott811d8d42021-04-22 11:31:14 +0100733 status = PSA_ERROR_INVALID_SIGNATURE;
Paul Elliottadb8b162021-04-20 16:06:57 +0100734 }
735
Paul Elliottadb8b162021-04-20 16:06:57 +0100736 return ( status );
737}
738
739/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100740psa_status_t mbedtls_psa_aead_abort(
741 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100742{
Paul Elliott811d8d42021-04-22 11:31:14 +0100743 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100744 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100745#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
746 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100747 mbedtls_ccm_free( &operation->ctx.ccm );
748 break;
749#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
750#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
751 case PSA_ALG_GCM:
752 mbedtls_gcm_free( &operation->ctx.gcm );
753 break;
754#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
755#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100756 case PSA_ALG_CHACHA20_POLY1305:
757 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
758 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100759#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
760 }
761
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100762 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100763
Paul Elliottadb8b162021-04-20 16:06:57 +0100764 return( PSA_SUCCESS );
765}
766
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100767#endif /* MBEDTLS_PSA_CRYPTO_C */
768