blob: 0d9d71068d4aca7310de188e9152e71f7a8c2f31 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#if defined(MBEDTLS_PLATFORM_C)
67#include "mbedtls/platform.h"
68#else
69#define mbedtls_calloc calloc
70#define mbedtls_free free
71#endif
72
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073#define CIPHER_VALIDATE_RET( cond ) \
74 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
75#define CIPHER_VALIDATE( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE( cond )
77
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083 int *type;
84
85 if( ! supported_init )
86 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 def = mbedtls_cipher_definitions;
88 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020089
90 while( def->type != 0 )
91 *type++ = (*def++).type;
92
93 *type = 0;
94
95 supported_init = 1;
96 }
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000099}
100
Hanno Becker18597cd2018-11-09 16:36:33 +0000101const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
102 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200107 if( def->type == cipher_type )
108 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000109
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200110 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111}
112
Hanno Becker18597cd2018-11-09 16:36:33 +0000113const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
114 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200119 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200122 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200123 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000124
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200125 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126}
127
Hanno Becker18597cd2018-11-09 16:36:33 +0000128const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
129 const mbedtls_cipher_id_t cipher_id,
130 int key_bitlen,
131 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200136 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200137 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200138 def->info->mode == mode )
139 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200140
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200141 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142}
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200145{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151{
152 if( ctx == NULL )
153 return;
154
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000155#if defined(MBEDTLS_USE_PSA_CRYPTO)
156 if( ctx->psa_enabled == 1 )
157 {
Hanno Becker6118e432018-11-09 16:47:20 +0000158 if( ctx->cipher_ctx != NULL )
159 {
160 mbedtls_cipher_context_psa * const cipher_psa =
161 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
162
Hanno Becker19086552018-11-17 22:11:16 +0000163 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000164 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000165 /* xxx_free() doesn't allow to return failures. */
166 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000167 }
168
169 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
170 mbedtls_free( cipher_psa );
171 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000172
173 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
174 return;
175 }
176#endif /* MBEDTLS_USE_PSA_CRYPTO */
177
Simon Butcher327398a2016-10-05 14:09:11 +0100178#if defined(MBEDTLS_CMAC_C)
179 if( ctx->cmac_ctx )
180 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500181 mbedtls_platform_zeroize( ctx->cmac_ctx,
182 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100183 mbedtls_free( ctx->cmac_ctx );
184 }
185#endif
186
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187 if( ctx->cipher_ctx )
188 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
189
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500190 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191}
192
Hanno Becker18597cd2018-11-09 16:36:33 +0000193int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
194 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 CIPHER_VALIDATE_RET( ctx != NULL );
197 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Paul Bakker343a8702011-06-09 14:27:58 +0000202 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204
205 ctx->cipher_info = cipher_info;
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200208 /*
209 * Ignore possible errors caused by a cipher mode that doesn't use padding
210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
212 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200213#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200217
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200218 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219}
220
Hanno Becker4ccfc402018-11-09 16:10:57 +0000221#if defined(MBEDTLS_USE_PSA_CRYPTO)
222int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000223 const mbedtls_cipher_info_t *cipher_info,
224 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000225{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000226 psa_algorithm_t alg;
227 mbedtls_cipher_context_psa *cipher_psa;
228
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000229 if( NULL == cipher_info || NULL == ctx )
230 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
231
Hanno Becker4ee7e762018-11-17 22:00:38 +0000232 /* Check that the underlying cipher mode and cipher type are
233 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000234 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000235 if( alg == 0 )
236 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
237 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000238 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000239
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000240 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
241
Hanno Beckeredda8b82018-11-12 11:59:30 +0000242 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
243 if( cipher_psa == NULL )
244 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
245 cipher_psa->alg = alg;
246 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000247 ctx->cipher_info = cipher_info;
248 ctx->psa_enabled = 1;
249 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000250}
251#endif /* MBEDTLS_USE_PSA_CRYPTO */
252
Hanno Becker18597cd2018-11-09 16:36:33 +0000253int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
254 const unsigned char *key,
255 int key_bitlen,
256 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 CIPHER_VALIDATE_RET( ctx != NULL );
259 CIPHER_VALIDATE_RET( key != NULL );
260 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100261 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500262 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000265#if defined(MBEDTLS_USE_PSA_CRYPTO)
266 if( ctx->psa_enabled == 1 )
267 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000268 mbedtls_cipher_context_psa * const cipher_psa =
269 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
270
271 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
272
273 psa_status_t status;
274 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276
277 /* PSA Crypto API only accepts byte-aligned keys. */
278 if( key_bitlen % 8 != 0 )
279 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
280
281 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000282 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000283 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
284
Andrzej Kurekc7509322019-01-08 09:36:01 -0500285 key_type = mbedtls_psa_translate_cipher_type(
286 ctx->cipher_info->type );
287 if( key_type == 0 )
288 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200289 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000290
291 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500292 * (encrypt vs. decrypt): it is possible to setup a key for encryption
293 * and use it for AEAD decryption. Until tests relying on this
294 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200295 psa_set_key_usage_flags( &attributes,
296 /* mbedtls_psa_translate_cipher_operation( operation ); */
297 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
298 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000299
Gilles Peskined2d45c12019-05-27 14:53:13 +0200300 status = psa_import_key( &attributes, key, key_bytelen,
301 &cipher_psa->slot );
302 switch( status )
303 {
304 case PSA_SUCCESS:
305 break;
306 case PSA_ERROR_INSUFFICIENT_MEMORY:
307 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
308 case PSA_ERROR_NOT_SUPPORTED:
309 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
310 default:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200311 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200312 }
313 /* Indicate that we own the key slot and need to
314 * destroy it in mbedtls_cipher_free(). */
315 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000316
317 ctx->key_bitlen = key_bitlen;
318 ctx->operation = operation;
319 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000320 }
321#endif /* MBEDTLS_USE_PSA_CRYPTO */
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200324 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200327 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200328
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200329 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330 ctx->operation = operation;
331
Paul Bakker343a8702011-06-09 14:27:58 +0000332 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100333 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( MBEDTLS_ENCRYPT == operation ||
336 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100337 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000339 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
341 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000342 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
346 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349}
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352 const unsigned char *iv,
353 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200355 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500357 CIPHER_VALIDATE_RET( ctx != NULL );
358 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
359 if( ctx->cipher_info == NULL )
360 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000361#if defined(MBEDTLS_USE_PSA_CRYPTO)
362 if( ctx->psa_enabled == 1 )
363 {
364 /* While PSA Crypto has an API for multipart
365 * operations, we currently don't make it
366 * accessible through the cipher layer. */
367 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
368 }
369#endif /* MBEDTLS_USE_PSA_CRYPTO */
370
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200371 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
373 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200376 actual_iv_size = iv_len;
377 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200378 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200379 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200380
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200381 /* avoid reading past the end of input buffer */
382 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200384 }
385
Daniel Kingbd920622016-05-15 19:56:20 -0300386#if defined(MBEDTLS_CHACHA20_C)
387 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
388 {
389 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
390 iv,
391 0U ) ) /* Initial counter value */
392 {
393 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
394 }
395 }
396#endif
397
Gilles Peskine295fc132021-04-15 18:32:23 +0200398#if defined(MBEDTLS_GCM_C)
399 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
400 {
401 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx,
402 ctx->operation,
403 iv, iv_len ) );
404 }
405#endif
406
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200407#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200408 if( MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200409 {
410 int set_lengths_result;
411 int ccm_star_mode;
412
413 set_lengths_result = mbedtls_ccm_set_lengths(
414 (mbedtls_ccm_context *) ctx->cipher_ctx,
415 0, 0, 0 );
416 if( set_lengths_result != 0 )
417 return set_lengths_result;
418
419 if( ctx->operation == MBEDTLS_DECRYPT )
420 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
421 else if( ctx->operation == MBEDTLS_ENCRYPT )
422 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
423 else
424 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
425
426 return( mbedtls_ccm_starts( (mbedtls_ccm_context *) ctx->cipher_ctx,
427 ccm_star_mode,
428 iv, iv_len ) );
429 }
430#endif
431
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300432 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300433 {
434 memcpy( ctx->iv, iv, actual_iv_size );
435 ctx->iv_size = actual_iv_size;
436 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200437
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200438 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200439}
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200442{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500443 CIPHER_VALIDATE_RET( ctx != NULL );
444 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200446
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000447#if defined(MBEDTLS_USE_PSA_CRYPTO)
448 if( ctx->psa_enabled == 1 )
449 {
450 /* We don't support resetting PSA-based
451 * cipher contexts, yet. */
452 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
453 }
454#endif /* MBEDTLS_USE_PSA_CRYPTO */
455
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456 ctx->unprocessed_len = 0;
457
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200458 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200459}
460
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200461#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200463 const unsigned char *ad, size_t ad_len )
464{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 CIPHER_VALIDATE_RET( ctx != NULL );
466 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
467 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200469
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000470#if defined(MBEDTLS_USE_PSA_CRYPTO)
471 if( ctx->psa_enabled == 1 )
472 {
473 /* While PSA Crypto has an API for multipart
474 * operations, we currently don't make it
475 * accessible through the cipher layer. */
476 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
477 }
478#endif /* MBEDTLS_USE_PSA_CRYPTO */
479
Daniel King8fe47012016-05-17 20:33:28 -0300480#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200482 {
Gilles Peskine295fc132021-04-15 18:32:23 +0200483 return( mbedtls_gcm_update_ad( (mbedtls_gcm_context *) ctx->cipher_ctx,
484 ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200485 }
Daniel King8fe47012016-05-17 20:33:28 -0300486#endif
487
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200488#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300489 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
490 {
491 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200492 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300493
494 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200495 ? MBEDTLS_CHACHAPOLY_ENCRYPT
496 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300497
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200498 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300499 ctx->iv,
500 mode );
501 if ( result != 0 )
502 return( result );
503
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
505 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300506 }
507#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200508
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200509 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200511#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200514 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515{
Janos Follath24eed8d2019-11-22 13:21:35 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500517 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500519 CIPHER_VALIDATE_RET( ctx != NULL );
520 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
521 CIPHER_VALIDATE_RET( output != NULL );
522 CIPHER_VALIDATE_RET( olen != NULL );
523 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000526#if defined(MBEDTLS_USE_PSA_CRYPTO)
527 if( ctx->psa_enabled == 1 )
528 {
529 /* While PSA Crypto has an API for multipart
530 * operations, we currently don't make it
531 * accessible through the cipher layer. */
532 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
533 }
534#endif /* MBEDTLS_USE_PSA_CRYPTO */
535
Paul Bakker6c212762013-12-16 15:24:50 +0100536 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100537 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100538 if ( 0 == block_size )
539 {
540 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
541 }
Paul Bakker6c212762013-12-16 15:24:50 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200544 {
Janos Follath98e28a72016-05-31 14:03:54 +0100545 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200547
548 *olen = ilen;
549
550 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
551 ctx->operation, input, output ) ) )
552 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200554 }
555
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200556 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200557 }
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_GCM_C)
560 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200561 {
Gilles Peskinea56c4482021-04-15 17:22:35 +0200562 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx,
563 input, ilen,
564 output, ilen, olen ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200565 }
566#endif
567
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200568#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200569 if( ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200570 {
571 return( mbedtls_ccm_update( (mbedtls_ccm_context *) ctx->cipher_ctx,
572 input, ilen,
573 output, ilen, olen ) );
574 }
575#endif
576
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200577#if defined(MBEDTLS_CHACHAPOLY_C)
578 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
579 {
580 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500581 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
582 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200583 }
584#endif
585
Paul Bakker68884e32013-01-07 18:20:04 +0100586 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100587 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100590 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_CIPHER_MODE_CBC)
593 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200595 size_t copy_len = 0;
596
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 /*
598 * If there is not enough data for a full block, cache it.
599 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700600 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000601 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700602 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
603 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000605 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 {
607 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
608 ilen );
609
610 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200611 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 }
613
614 /*
615 * Process cached data first
616 */
Janos Follath98e28a72016-05-31 14:03:54 +0100617 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618 {
Janos Follath98e28a72016-05-31 14:03:54 +0100619 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620
621 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
622 copy_len );
623
Paul Bakkerff61a782011-06-09 15:42:02 +0000624 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100625 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000626 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200628 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629 }
630
Janos Follath98e28a72016-05-31 14:03:54 +0100631 *olen += block_size;
632 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633 ctx->unprocessed_len = 0;
634
635 input += copy_len;
636 ilen -= copy_len;
637 }
638
639 /*
640 * Cache final, incomplete block
641 */
642 if( 0 != ilen )
643 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700644 /* Encryption: only cache partial blocks
645 * Decryption w/ padding: always keep at least one whole block
646 * Decryption w/o padding: only cache partial blocks
647 */
Janos Follath98e28a72016-05-31 14:03:54 +0100648 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700649 if( copy_len == 0 &&
650 ctx->operation == MBEDTLS_DECRYPT &&
651 NULL != ctx->add_padding)
652 {
Janos Follath98e28a72016-05-31 14:03:54 +0100653 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700654 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
656 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
657 copy_len );
658
659 ctx->unprocessed_len += copy_len;
660 ilen -= copy_len;
661 }
662
663 /*
664 * Process remaining full blocks
665 */
666 if( ilen )
667 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000668 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
669 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200673
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 *olen += ilen;
675 }
676
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200677 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_CIPHER_MODE_CFB)
682 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000683 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000684 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000685 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000686 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000687 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200688 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000689 }
690
691 *olen = ilen;
692
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200693 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000696
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100697#if defined(MBEDTLS_CIPHER_MODE_OFB)
698 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
699 {
700 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
701 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
702 {
703 return( ret );
704 }
705
706 *olen = ilen;
707
708 return( 0 );
709 }
710#endif /* MBEDTLS_CIPHER_MODE_OFB */
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_CIPHER_MODE_CTR)
713 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000714 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000715 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000716 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000717 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000718 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200719 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000720 }
721
722 *olen = ilen;
723
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200724 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000727
Jaeden Ameroc6539902018-04-30 17:17:41 +0100728#if defined(MBEDTLS_CIPHER_MODE_XTS)
729 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
730 {
731 if( ctx->unprocessed_len > 0 ) {
732 /* We can only process an entire data unit at a time. */
733 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
734 }
735
736 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
737 ctx->operation, ilen, ctx->iv, input, output );
738 if( ret != 0 )
739 {
740 return( ret );
741 }
742
743 *olen = ilen;
744
745 return( 0 );
746 }
747#endif /* MBEDTLS_CIPHER_MODE_XTS */
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_CIPHER_MODE_STREAM)
750 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200751 {
752 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
753 ilen, input, output ) ) )
754 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200755 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200756 }
757
758 *olen = ilen;
759
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200760 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765}
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
768#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200769/*
770 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
771 */
Paul Bakker23986e52011-04-24 08:57:21 +0000772static void add_pkcs_padding( unsigned char *output, size_t output_len,
773 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000774{
Paul Bakker23986e52011-04-24 08:57:21 +0000775 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100776 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000777
778 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000779 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780}
781
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200782static int get_pkcs_padding( unsigned char *input, size_t input_len,
783 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100785 size_t i, pad_idx;
786 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787
Paul Bakkera885d682011-01-20 16:35:05 +0000788 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790
791 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 *data_len = input_len - padding_len;
793
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100794 /* Avoid logical || since it results in a branch */
795 bad |= padding_len > input_len;
796 bad |= padding_len == 0;
797
798 /* The number of bytes checked must be independent of padding_len,
799 * so pick input_len, which is usually 8 or 16 (one block) */
800 pad_idx = input_len - padding_len;
801 for( i = 0; i < input_len; i++ )
802 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200809/*
810 * One and zeros padding: fill with 80 00 ... 00
811 */
812static void add_one_and_zeros_padding( unsigned char *output,
813 size_t output_len, size_t data_len )
814{
815 size_t padding_len = output_len - data_len;
816 unsigned char i = 0;
817
818 output[data_len] = 0x80;
819 for( i = 1; i < padding_len; i++ )
820 output[data_len + i] = 0x00;
821}
822
823static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
824 size_t *data_len )
825{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100826 size_t i;
827 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200828
829 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200831
Micha Krausba8316f2017-12-23 23:40:08 +0100832 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100833 *data_len = 0;
834 for( i = input_len; i > 0; i-- )
835 {
836 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100837 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100838 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100839 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100840 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200843
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200848/*
849 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
850 */
851static void add_zeros_and_len_padding( unsigned char *output,
852 size_t output_len, size_t data_len )
853{
854 size_t padding_len = output_len - data_len;
855 unsigned char i = 0;
856
857 for( i = 1; i < padding_len; i++ )
858 output[data_len + i - 1] = 0x00;
859 output[output_len - 1] = (unsigned char) padding_len;
860}
861
862static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
863 size_t *data_len )
864{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100865 size_t i, pad_idx;
866 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200867
868 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200870
871 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200872 *data_len = input_len - padding_len;
873
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100874 /* Avoid logical || since it results in a branch */
875 bad |= padding_len > input_len;
876 bad |= padding_len == 0;
877
878 /* The number of bytes checked must be independent of padding_len */
879 pad_idx = input_len - padding_len;
880 for( i = 0; i < input_len - 1; i++ )
881 bad |= input[i] * ( i >= pad_idx );
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200888/*
889 * Zero padding: fill with 00 ... 00
890 */
891static void add_zeros_padding( unsigned char *output,
892 size_t output_len, size_t data_len )
893{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200894 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895
896 for( i = data_len; i < output_len; i++ )
897 output[i] = 0x00;
898}
899
900static int get_zeros_padding( unsigned char *input, size_t input_len,
901 size_t *data_len )
902{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100903 size_t i;
904 unsigned char done = 0, prev_done;
905
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200906 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200908
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100909 *data_len = 0;
910 for( i = input_len; i > 0; i-- )
911 {
912 prev_done = done;
913 done |= ( input[i-1] != 0 );
914 *data_len |= i * ( done != prev_done );
915 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200916
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200917 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200920
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200921/*
922 * No padding: don't pad :)
923 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200925 * but a trivial get_padding function
926 */
927static int get_no_padding( unsigned char *input, size_t input_len,
928 size_t *data_len )
929{
930 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200932
933 *data_len = input_len;
934
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200935 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200940 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000941{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500942 CIPHER_VALIDATE_RET( ctx != NULL );
943 CIPHER_VALIDATE_RET( output != NULL );
944 CIPHER_VALIDATE_RET( olen != NULL );
945 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000947
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000948#if defined(MBEDTLS_USE_PSA_CRYPTO)
949 if( ctx->psa_enabled == 1 )
950 {
951 /* While PSA Crypto has an API for multipart
952 * operations, we currently don't make it
953 * accessible through the cipher layer. */
954 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
955 }
956#endif /* MBEDTLS_USE_PSA_CRYPTO */
957
Paul Bakker8123e9d2011-01-06 15:37:30 +0000958 *olen = 0;
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100961 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
963 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200964 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100965 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000967 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200968 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000969 }
970
Daniel King8fe47012016-05-17 20:33:28 -0300971 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
972 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300973 {
974 return( 0 );
975 }
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200978 {
979 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200981
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200982 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200983 }
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_CIPHER_MODE_CBC)
986 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200988 int ret = 0;
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000991 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992 /* check for 'no padding' mode */
993 if( NULL == ctx->add_padding )
994 {
995 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200997
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200999 }
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002 ctx->unprocessed_len );
1003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001006 /*
1007 * For decrypt operations, expect a full block,
1008 * or an empty block if no padding
1009 */
1010 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001011 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014 }
1015
1016 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001017 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001019 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001021 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001022 }
1023
1024 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001026 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1027 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028
1029 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001031 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001032 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001033#else
1034 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001038}
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001041int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1042 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001043{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001044 CIPHER_VALIDATE_RET( ctx != NULL );
1045
1046 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001049 }
1050
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001051#if defined(MBEDTLS_USE_PSA_CRYPTO)
1052 if( ctx->psa_enabled == 1 )
1053 {
1054 /* While PSA Crypto knows about CBC padding
1055 * schemes, we currently don't make them
1056 * accessible through the cipher layer. */
1057 if( mode != MBEDTLS_PADDING_NONE )
1058 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1059
1060 return( 0 );
1061 }
1062#endif /* MBEDTLS_USE_PSA_CRYPTO */
1063
Paul Bakker1a45d912013-08-14 12:04:26 +02001064 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1067 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001068 ctx->add_padding = add_pkcs_padding;
1069 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001070 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1073 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001074 ctx->add_padding = add_one_and_zeros_padding;
1075 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001076 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1079 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001080 ctx->add_padding = add_zeros_and_len_padding;
1081 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001082 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1085 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001086 ctx->add_padding = add_zeros_padding;
1087 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001088 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001091 ctx->add_padding = NULL;
1092 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001093 break;
1094
1095 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001097 }
1098
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001099 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001102
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001103#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001105 unsigned char *tag, size_t tag_len )
1106{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001107 CIPHER_VALIDATE_RET( ctx != NULL );
1108 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1109 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 if( MBEDTLS_ENCRYPT != ctx->operation )
1113 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001114
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001115#if defined(MBEDTLS_USE_PSA_CRYPTO)
1116 if( ctx->psa_enabled == 1 )
1117 {
1118 /* While PSA Crypto has an API for multipart
1119 * operations, we currently don't make it
1120 * accessible through the cipher layer. */
1121 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001122 }
1123#endif /* MBEDTLS_USE_PSA_CRYPTO */
1124
Daniel King8fe47012016-05-17 20:33:28 -03001125#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Gilles Peskine5a7be102021-06-23 21:51:32 +02001127 {
1128 size_t output_length;
1129 /* The code here doesn't yet support alternative implementations
1130 * that can delay up to a block of output. */
Hanno Becker18597cd2018-11-09 16:36:33 +00001131 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001132 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001133 tag, tag_len ) );
Gilles Peskine5a7be102021-06-23 21:51:32 +02001134 }
Daniel King8fe47012016-05-17 20:33:28 -03001135#endif
1136
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001137#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001138 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1139 {
1140 /* Don't allow truncated MAC for Poly1305 */
1141 if ( tag_len != 16U )
1142 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1143
Hanno Becker18597cd2018-11-09 16:36:33 +00001144 return( mbedtls_chachapoly_finish(
1145 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001146 }
1147#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001148
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001149 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001150}
Paul Bakker9af723c2014-05-01 13:03:14 +02001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001153 const unsigned char *tag, size_t tag_len )
1154{
Daniel King8fe47012016-05-17 20:33:28 -03001155 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001157
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001158 CIPHER_VALIDATE_RET( ctx != NULL );
1159 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1160 if( ctx->cipher_info == NULL )
1161 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1162
1163 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001167
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
1169 if( ctx->psa_enabled == 1 )
1170 {
1171 /* While PSA Crypto has an API for multipart
1172 * operations, we currently don't make it
1173 * accessible through the cipher layer. */
1174 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1175 }
1176#endif /* MBEDTLS_USE_PSA_CRYPTO */
1177
Daniel King8fe47012016-05-17 20:33:28 -03001178#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001180 {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001181 size_t output_length;
1182 /* The code here doesn't yet support alternative implementations
1183 * that can delay up to a block of output. */
1184
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001185 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001187
Hanno Becker18597cd2018-11-09 16:36:33 +00001188 if( 0 != ( ret = mbedtls_gcm_finish(
1189 (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001190 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001191 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001192 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001193 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001194 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001195
1196 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001197 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001199
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001200 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001201 }
Daniel King8fe47012016-05-17 20:33:28 -03001202#endif /* MBEDTLS_GCM_C */
1203
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001204#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001205 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1206 {
1207 /* Don't allow truncated MAC for Poly1305 */
1208 if ( tag_len != sizeof( check_tag ) )
1209 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1210
Hanno Becker18597cd2018-11-09 16:36:33 +00001211 ret = mbedtls_chachapoly_finish(
1212 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001213 if ( ret != 0 )
1214 {
1215 return( ret );
1216 }
1217
1218 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001219 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Daniel King8fe47012016-05-17 20:33:28 -03001220 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1221
1222 return( 0 );
1223 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001224#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001225
1226 return( 0 );
1227}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001228#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001229
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001230/*
1231 * Packet-oriented wrapper for non-AEAD modes
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001234 const unsigned char *iv, size_t iv_len,
1235 const unsigned char *input, size_t ilen,
1236 unsigned char *output, size_t *olen )
1237{
Janos Follath24eed8d2019-11-22 13:21:35 +00001238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001239 size_t finish_olen;
1240
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 CIPHER_VALIDATE_RET( ctx != NULL );
1242 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1243 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1244 CIPHER_VALIDATE_RET( output != NULL );
1245 CIPHER_VALIDATE_RET( olen != NULL );
1246
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001247#if defined(MBEDTLS_USE_PSA_CRYPTO)
1248 if( ctx->psa_enabled == 1 )
1249 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001250 /* As in the non-PSA case, we don't check that
1251 * a key has been set. If not, the key slot will
1252 * still be in its default state of 0, which is
1253 * guaranteed to be invalid, hence the PSA-call
1254 * below will gracefully fail. */
1255 mbedtls_cipher_context_psa * const cipher_psa =
1256 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1257
1258 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001259 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001260 size_t part_len;
1261
1262 if( ctx->operation == MBEDTLS_DECRYPT )
1263 {
1264 status = psa_cipher_decrypt_setup( &cipher_op,
1265 cipher_psa->slot,
1266 cipher_psa->alg );
1267 }
1268 else if( ctx->operation == MBEDTLS_ENCRYPT )
1269 {
1270 status = psa_cipher_encrypt_setup( &cipher_op,
1271 cipher_psa->slot,
1272 cipher_psa->alg );
1273 }
1274 else
1275 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1276
1277 /* In the following, we can immediately return on an error,
1278 * because the PSA Crypto API guarantees that cipher operations
1279 * are terminated by unsuccessful calls to psa_cipher_update(),
1280 * and by any call to psa_cipher_finish(). */
1281 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001282 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001283
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001284 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1285 {
1286 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1287 if( status != PSA_SUCCESS )
1288 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1289 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001290
1291 status = psa_cipher_update( &cipher_op,
1292 input, ilen,
1293 output, ilen, olen );
1294 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001295 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001296
1297 status = psa_cipher_finish( &cipher_op,
1298 output + *olen, ilen - *olen,
1299 &part_len );
1300 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001301 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001302
1303 *olen += part_len;
1304 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001305 }
1306#endif /* MBEDTLS_USE_PSA_CRYPTO */
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001309 return( ret );
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001312 return( ret );
1313
Hanno Becker18597cd2018-11-09 16:36:33 +00001314 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1315 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001316 return( ret );
1317
Hanno Becker18597cd2018-11-09 16:36:33 +00001318 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1319 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001320 return( ret );
1321
1322 *olen += finish_olen;
1323
1324 return( 0 );
1325}
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001328/*
TRodziewicz18efb732021-04-29 23:12:19 +02001329 * Packet-oriented encryption for AEAD modes: internal function used by
1330 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001331 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001332static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001333 const unsigned char *iv, size_t iv_len,
1334 const unsigned char *ad, size_t ad_len,
1335 const unsigned char *input, size_t ilen,
1336 unsigned char *output, size_t *olen,
1337 unsigned char *tag, size_t tag_len )
1338{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001339#if defined(MBEDTLS_USE_PSA_CRYPTO)
1340 if( ctx->psa_enabled == 1 )
1341 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001342 /* As in the non-PSA case, we don't check that
1343 * a key has been set. If not, the key slot will
1344 * still be in its default state of 0, which is
1345 * guaranteed to be invalid, hence the PSA-call
1346 * below will gracefully fail. */
1347 mbedtls_cipher_context_psa * const cipher_psa =
1348 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1349
1350 psa_status_t status;
1351
1352 /* PSA Crypto API always writes the authentication tag
1353 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001354 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001355 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1356
1357 status = psa_aead_encrypt( cipher_psa->slot,
1358 cipher_psa->alg,
1359 iv, iv_len,
1360 ad, ad_len,
1361 input, ilen,
1362 output, ilen + tag_len, olen );
1363 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001364 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001365
1366 *olen -= tag_len;
1367 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001368 }
1369#endif /* MBEDTLS_USE_PSA_CRYPTO */
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_GCM_C)
1372 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001373 {
1374 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001375 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1376 ilen, iv, iv_len, ad, ad_len,
1377 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_GCM_C */
1380#if defined(MBEDTLS_CCM_C)
1381 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001382 {
1383 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001385 iv, iv_len, ad, ad_len, input, output,
1386 tag, tag_len ) );
1387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001389#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001390 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1391 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001392 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001393 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001394 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001395 {
1396 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1397 }
1398
1399 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001400 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001401 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001402 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001403#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001406}
1407
1408/*
TRodziewicz18efb732021-04-29 23:12:19 +02001409 * Packet-oriented encryption for AEAD modes: internal function used by
1410 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001411 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001412static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001413 const unsigned char *iv, size_t iv_len,
1414 const unsigned char *ad, size_t ad_len,
1415 const unsigned char *input, size_t ilen,
1416 unsigned char *output, size_t *olen,
1417 const unsigned char *tag, size_t tag_len )
1418{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001419#if defined(MBEDTLS_USE_PSA_CRYPTO)
1420 if( ctx->psa_enabled == 1 )
1421 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001422 /* As in the non-PSA case, we don't check that
1423 * a key has been set. If not, the key slot will
1424 * still be in its default state of 0, which is
1425 * guaranteed to be invalid, hence the PSA-call
1426 * below will gracefully fail. */
1427 mbedtls_cipher_context_psa * const cipher_psa =
1428 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1429
1430 psa_status_t status;
1431
1432 /* PSA Crypto API always writes the authentication tag
1433 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001434 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001435 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1436
1437 status = psa_aead_decrypt( cipher_psa->slot,
1438 cipher_psa->alg,
1439 iv, iv_len,
1440 ad, ad_len,
1441 input, ilen + tag_len,
1442 output, ilen, olen );
1443 if( status == PSA_ERROR_INVALID_SIGNATURE )
1444 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1445 else if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001446 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001447
1448 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001449 }
1450#endif /* MBEDTLS_USE_PSA_CRYPTO */
1451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_GCM_C)
1453 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001454 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001456
1457 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001459 iv, iv_len, ad, ad_len,
1460 tag, tag_len, input, output );
1461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1463 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001464
1465 return( ret );
1466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#endif /* MBEDTLS_GCM_C */
1468#if defined(MBEDTLS_CCM_C)
1469 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001470 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001472
1473 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001475 iv, iv_len, ad, ad_len,
1476 input, output, tag, tag_len );
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1479 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001480
1481 return( ret );
1482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001484#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001485 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1486 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001488
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001489 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001490 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001491 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001492 {
1493 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1494 }
1495
1496 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001497 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1498 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001499
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001500 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1501 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001502
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001503 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001504 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001505#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001506
1507 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001510
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001511#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1512/*
1513 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1514 */
1515int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1516 const unsigned char *iv, size_t iv_len,
1517 const unsigned char *ad, size_t ad_len,
1518 const unsigned char *input, size_t ilen,
1519 unsigned char *output, size_t output_len,
1520 size_t *olen, size_t tag_len )
1521{
1522 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001523 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001524 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1525 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1526 CIPHER_VALIDATE_RET( output != NULL );
1527 CIPHER_VALIDATE_RET( olen != NULL );
1528
1529#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001530 if(
1531#if defined(MBEDTLS_USE_PSA_CRYPTO)
1532 ctx->psa_enabled == 0 &&
1533#endif
1534 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1535 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001536 {
1537 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1538 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1539
1540 /* There is no iv, tag or ad associated with KW and KWP,
1541 * so these length should be 0 as documented. */
1542 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1543 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1544
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001545 (void) iv;
1546 (void) ad;
1547
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001548 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1549 output, olen, output_len ) );
1550 }
1551#endif /* MBEDTLS_NIST_KW_C */
1552
1553#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1554 /* AEAD case: check length before passing on to shared function */
1555 if( output_len < ilen + tag_len )
1556 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1557
1558 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1559 input, ilen, output, olen,
1560 output + ilen, tag_len );
1561 *olen += tag_len;
1562 return( ret );
1563#else
1564 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1565#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1566}
1567
1568/*
1569 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1570 */
1571int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1572 const unsigned char *iv, size_t iv_len,
1573 const unsigned char *ad, size_t ad_len,
1574 const unsigned char *input, size_t ilen,
1575 unsigned char *output, size_t output_len,
1576 size_t *olen, size_t tag_len )
1577{
1578 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001579 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001580 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1581 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1582 CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
1583 CIPHER_VALIDATE_RET( olen != NULL );
1584
1585#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001586 if(
1587#if defined(MBEDTLS_USE_PSA_CRYPTO)
1588 ctx->psa_enabled == 0 &&
1589#endif
1590 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1591 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001592 {
1593 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1594 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1595
1596 /* There is no iv, tag or ad associated with KW and KWP,
1597 * so these length should be 0 as documented. */
1598 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1599 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1600
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001601 (void) iv;
1602 (void) ad;
1603
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001604 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1605 output, olen, output_len ) );
1606 }
1607#endif /* MBEDTLS_NIST_KW_C */
1608
1609#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1610 /* AEAD case: check length before passing on to shared function */
1611 if( ilen < tag_len || output_len < ilen - tag_len )
1612 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1613
1614 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1615 input, ilen - tag_len, output, olen,
1616 input + ilen - tag_len, tag_len ) );
1617#else
1618 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1619#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1620}
1621#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#endif /* MBEDTLS_CIPHER_C */