blob: 6153b333a8a93a988ffe57af2781fe8982343e8f [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"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020029#include "mbedtls/cipher_internal.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 Mezeie24dea82021-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:
311 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
312 }
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 {
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100389 /* Even though the actual_iv_size is overwritten with a correct value
390 * of 12 from the cipher info, return an error to indicate that
391 * the input iv_len is wrong. */
392 if( iv_len != 12 )
393 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
394
Daniel Kingbd920622016-05-15 19:56:20 -0300395 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
396 iv,
397 0U ) ) /* Initial counter value */
398 {
399 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
400 }
401 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100402#if defined(MBEDTLS_CHACHAPOLY_C)
403 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
404 iv_len != 12 )
405 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
406#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300407#endif
408
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300409 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300410 {
411 memcpy( ctx->iv, iv, actual_iv_size );
412 ctx->iv_size = actual_iv_size;
413 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200414
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200415 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200416}
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200419{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 CIPHER_VALIDATE_RET( ctx != NULL );
421 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200423
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000424#if defined(MBEDTLS_USE_PSA_CRYPTO)
425 if( ctx->psa_enabled == 1 )
426 {
427 /* We don't support resetting PSA-based
428 * cipher contexts, yet. */
429 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
430 }
431#endif /* MBEDTLS_USE_PSA_CRYPTO */
432
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433 ctx->unprocessed_len = 0;
434
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200435 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200436}
437
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200438#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200440 const unsigned char *ad, size_t ad_len )
441{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500442 CIPHER_VALIDATE_RET( ctx != NULL );
443 CIPHER_VALIDATE_RET( ad_len == 0 || ad != 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 /* While PSA Crypto has an API for multipart
451 * operations, we currently don't make it
452 * accessible through the cipher layer. */
453 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
454 }
455#endif /* MBEDTLS_USE_PSA_CRYPTO */
456
Daniel King8fe47012016-05-17 20:33:28 -0300457#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200459 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500460 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
461 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200462 }
Daniel King8fe47012016-05-17 20:33:28 -0300463#endif
464
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200465#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300466 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
467 {
468 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200469 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300470
471 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200472 ? MBEDTLS_CHACHAPOLY_ENCRYPT
473 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300474
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200475 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300476 ctx->iv,
477 mode );
478 if ( result != 0 )
479 return( result );
480
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500481 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
482 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300483 }
484#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200485
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200486 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200488#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200491 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492{
Janos Follath24eed8d2019-11-22 13:21:35 +0000493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500494 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500496 CIPHER_VALIDATE_RET( ctx != NULL );
497 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
498 CIPHER_VALIDATE_RET( output != NULL );
499 CIPHER_VALIDATE_RET( olen != NULL );
500 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000503#if defined(MBEDTLS_USE_PSA_CRYPTO)
504 if( ctx->psa_enabled == 1 )
505 {
506 /* While PSA Crypto has an API for multipart
507 * operations, we currently don't make it
508 * accessible through the cipher layer. */
509 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
510 }
511#endif /* MBEDTLS_USE_PSA_CRYPTO */
512
Paul Bakker6c212762013-12-16 15:24:50 +0100513 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100514 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100515 if ( 0 == block_size )
516 {
517 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
518 }
Paul Bakker6c212762013-12-16 15:24:50 +0100519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200521 {
Janos Follath98e28a72016-05-31 14:03:54 +0100522 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200524
525 *olen = ilen;
526
527 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
528 ctx->operation, input, output ) ) )
529 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200530 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200531 }
532
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200533 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_GCM_C)
537 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200538 {
539 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
541 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200542 }
543#endif
544
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200545#if defined(MBEDTLS_CHACHAPOLY_C)
546 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
547 {
548 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
550 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200551 }
552#endif
553
Paul Bakker68884e32013-01-07 18:20:04 +0100554 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100555 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100558 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_CIPHER_MODE_CBC)
561 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200563 size_t copy_len = 0;
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565 /*
566 * If there is not enough data for a full block, cache it.
567 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700568 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000569 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700570 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
571 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000573 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574 {
575 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
576 ilen );
577
578 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 }
581
582 /*
583 * Process cached data first
584 */
Janos Follath98e28a72016-05-31 14:03:54 +0100585 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 {
Janos Follath98e28a72016-05-31 14:03:54 +0100587 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588
589 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
590 copy_len );
591
Paul Bakkerff61a782011-06-09 15:42:02 +0000592 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100593 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000594 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200596 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 }
598
Janos Follath98e28a72016-05-31 14:03:54 +0100599 *olen += block_size;
600 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 ctx->unprocessed_len = 0;
602
603 input += copy_len;
604 ilen -= copy_len;
605 }
606
607 /*
608 * Cache final, incomplete block
609 */
610 if( 0 != ilen )
611 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700612 /* Encryption: only cache partial blocks
613 * Decryption w/ padding: always keep at least one whole block
614 * Decryption w/o padding: only cache partial blocks
615 */
Janos Follath98e28a72016-05-31 14:03:54 +0100616 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700617 if( copy_len == 0 &&
618 ctx->operation == MBEDTLS_DECRYPT &&
619 NULL != ctx->add_padding)
620 {
Janos Follath98e28a72016-05-31 14:03:54 +0100621 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700622 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623
624 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
625 copy_len );
626
627 ctx->unprocessed_len += copy_len;
628 ilen -= copy_len;
629 }
630
631 /*
632 * Process remaining full blocks
633 */
634 if( ilen )
635 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000636 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
637 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200639 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200641
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642 *olen += ilen;
643 }
644
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_CIPHER_MODE_CFB)
650 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000651 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000652 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000653 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000654 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000655 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200656 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000657 }
658
659 *olen = ilen;
660
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000664
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100665#if defined(MBEDTLS_CIPHER_MODE_OFB)
666 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
667 {
668 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
669 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
670 {
671 return( ret );
672 }
673
674 *olen = ilen;
675
676 return( 0 );
677 }
678#endif /* MBEDTLS_CIPHER_MODE_OFB */
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_CIPHER_MODE_CTR)
681 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000682 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000683 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000684 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000685 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000686 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000688 }
689
690 *olen = ilen;
691
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200692 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000695
Jaeden Ameroc6539902018-04-30 17:17:41 +0100696#if defined(MBEDTLS_CIPHER_MODE_XTS)
697 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
698 {
699 if( ctx->unprocessed_len > 0 ) {
700 /* We can only process an entire data unit at a time. */
701 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
702 }
703
704 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
705 ctx->operation, ilen, ctx->iv, input, output );
706 if( ret != 0 )
707 {
708 return( ret );
709 }
710
711 *olen = ilen;
712
713 return( 0 );
714 }
715#endif /* MBEDTLS_CIPHER_MODE_XTS */
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_CIPHER_MODE_STREAM)
718 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200719 {
720 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
721 ilen, input, output ) ) )
722 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200723 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200724 }
725
726 *olen = ilen;
727
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200728 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733}
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
736#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200737/*
738 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
739 */
Paul Bakker23986e52011-04-24 08:57:21 +0000740static void add_pkcs_padding( unsigned char *output, size_t output_len,
741 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742{
Paul Bakker23986e52011-04-24 08:57:21 +0000743 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100744 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000745
746 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000747 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748}
749
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200750static int get_pkcs_padding( unsigned char *input, size_t input_len,
751 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000752{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100753 size_t i, pad_idx;
754 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755
Paul Bakkera885d682011-01-20 16:35:05 +0000756 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758
759 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 *data_len = input_len - padding_len;
761
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100762 /* Avoid logical || since it results in a branch */
763 bad |= padding_len > input_len;
764 bad |= padding_len == 0;
765
766 /* The number of bytes checked must be independent of padding_len,
767 * so pick input_len, which is usually 8 or 16 (one block) */
768 pad_idx = input_len - padding_len;
769 for( i = 0; i < input_len; i++ )
770 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200777/*
778 * One and zeros padding: fill with 80 00 ... 00
779 */
780static void add_one_and_zeros_padding( unsigned char *output,
781 size_t output_len, size_t data_len )
782{
783 size_t padding_len = output_len - data_len;
784 unsigned char i = 0;
785
786 output[data_len] = 0x80;
787 for( i = 1; i < padding_len; i++ )
788 output[data_len + i] = 0x00;
789}
790
791static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
792 size_t *data_len )
793{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100794 size_t i;
795 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200796
797 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200799
Micha Krausba8316f2017-12-23 23:40:08 +0100800 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100801 *data_len = 0;
802 for( i = input_len; i > 0; i-- )
803 {
804 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100805 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100806 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100807 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100808 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200811
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200816/*
817 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
818 */
819static void add_zeros_and_len_padding( unsigned char *output,
820 size_t output_len, size_t data_len )
821{
822 size_t padding_len = output_len - data_len;
823 unsigned char i = 0;
824
825 for( i = 1; i < padding_len; i++ )
826 output[data_len + i - 1] = 0x00;
827 output[output_len - 1] = (unsigned char) padding_len;
828}
829
830static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
831 size_t *data_len )
832{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100833 size_t i, pad_idx;
834 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200835
836 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200838
839 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200840 *data_len = input_len - padding_len;
841
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100842 /* Avoid logical || since it results in a branch */
843 bad |= padding_len > input_len;
844 bad |= padding_len == 0;
845
846 /* The number of bytes checked must be independent of padding_len */
847 pad_idx = input_len - padding_len;
848 for( i = 0; i < input_len - 1; i++ )
849 bad |= input[i] * ( i >= pad_idx );
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200856/*
857 * Zero padding: fill with 00 ... 00
858 */
859static void add_zeros_padding( unsigned char *output,
860 size_t output_len, size_t data_len )
861{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200862 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200863
864 for( i = data_len; i < output_len; i++ )
865 output[i] = 0x00;
866}
867
868static int get_zeros_padding( unsigned char *input, size_t input_len,
869 size_t *data_len )
870{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100871 size_t i;
872 unsigned char done = 0, prev_done;
873
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200874 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200876
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100877 *data_len = 0;
878 for( i = input_len; i > 0; i-- )
879 {
880 prev_done = done;
881 done |= ( input[i-1] != 0 );
882 *data_len |= i * ( done != prev_done );
883 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200884
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200885 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200888
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200889/*
890 * No padding: don't pad :)
891 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200893 * but a trivial get_padding function
894 */
895static int get_no_padding( unsigned char *input, size_t input_len,
896 size_t *data_len )
897{
898 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200900
901 *data_len = input_len;
902
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200903 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200908 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000909{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500910 CIPHER_VALIDATE_RET( ctx != NULL );
911 CIPHER_VALIDATE_RET( output != NULL );
912 CIPHER_VALIDATE_RET( olen != NULL );
913 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000915
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000916#if defined(MBEDTLS_USE_PSA_CRYPTO)
917 if( ctx->psa_enabled == 1 )
918 {
919 /* While PSA Crypto has an API for multipart
920 * operations, we currently don't make it
921 * accessible through the cipher layer. */
922 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
923 }
924#endif /* MBEDTLS_USE_PSA_CRYPTO */
925
Paul Bakker8123e9d2011-01-06 15:37:30 +0000926 *olen = 0;
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100929 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
931 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100932 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000934 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200935 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000936 }
937
Daniel King8fe47012016-05-17 20:33:28 -0300938 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
939 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300940 {
941 return( 0 );
942 }
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200945 {
946 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200948
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200949 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200950 }
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952#if defined(MBEDTLS_CIPHER_MODE_CBC)
953 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000954 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200955 int ret = 0;
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000958 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200959 /* check for 'no padding' mode */
960 if( NULL == ctx->add_padding )
961 {
962 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200964
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200965 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200966 }
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000969 ctx->unprocessed_len );
970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000972 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200973 /*
974 * For decrypt operations, expect a full block,
975 * or an empty block if no padding
976 */
977 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200978 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000981 }
982
983 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000984 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000986 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200988 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000989 }
990
991 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500993 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
994 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000995
996 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000999 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001000#else
1001 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005}
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001008int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1009 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001010{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001011 CIPHER_VALIDATE_RET( ctx != NULL );
1012
1013 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001016 }
1017
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001018#if defined(MBEDTLS_USE_PSA_CRYPTO)
1019 if( ctx->psa_enabled == 1 )
1020 {
1021 /* While PSA Crypto knows about CBC padding
1022 * schemes, we currently don't make them
1023 * accessible through the cipher layer. */
1024 if( mode != MBEDTLS_PADDING_NONE )
1025 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1026
1027 return( 0 );
1028 }
1029#endif /* MBEDTLS_USE_PSA_CRYPTO */
1030
Paul Bakker1a45d912013-08-14 12:04:26 +02001031 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1034 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001035 ctx->add_padding = add_pkcs_padding;
1036 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001037 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1040 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001041 ctx->add_padding = add_one_and_zeros_padding;
1042 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001043 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1046 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001047 ctx->add_padding = add_zeros_and_len_padding;
1048 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001049 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1052 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001053 ctx->add_padding = add_zeros_padding;
1054 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001055 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001058 ctx->add_padding = NULL;
1059 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001060 break;
1061
1062 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001064 }
1065
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001066 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001069
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001070#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001072 unsigned char *tag, size_t tag_len )
1073{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001074 CIPHER_VALIDATE_RET( ctx != NULL );
1075 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1076 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( MBEDTLS_ENCRYPT != ctx->operation )
1080 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001081
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001082#if defined(MBEDTLS_USE_PSA_CRYPTO)
1083 if( ctx->psa_enabled == 1 )
1084 {
1085 /* While PSA Crypto has an API for multipart
1086 * operations, we currently don't make it
1087 * accessible through the cipher layer. */
1088 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001089 }
1090#endif /* MBEDTLS_USE_PSA_CRYPTO */
1091
Daniel King8fe47012016-05-17 20:33:28 -03001092#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001094 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1095 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001096#endif
1097
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001098#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001099 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1100 {
1101 /* Don't allow truncated MAC for Poly1305 */
1102 if ( tag_len != 16U )
1103 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1104
Hanno Becker18597cd2018-11-09 16:36:33 +00001105 return( mbedtls_chachapoly_finish(
1106 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001107 }
1108#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001109
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001110 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111}
Paul Bakker9af723c2014-05-01 13:03:14 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001114 const unsigned char *tag, size_t tag_len )
1115{
Daniel King8fe47012016-05-17 20:33:28 -03001116 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001118
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001119 CIPHER_VALIDATE_RET( ctx != NULL );
1120 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1121 if( ctx->cipher_info == NULL )
1122 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1123
1124 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001127 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001128
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001129#if defined(MBEDTLS_USE_PSA_CRYPTO)
1130 if( ctx->psa_enabled == 1 )
1131 {
1132 /* While PSA Crypto has an API for multipart
1133 * operations, we currently don't make it
1134 * accessible through the cipher layer. */
1135 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1136 }
1137#endif /* MBEDTLS_USE_PSA_CRYPTO */
1138
Daniel King8fe47012016-05-17 20:33:28 -03001139#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001141 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001142 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001144
Hanno Becker18597cd2018-11-09 16:36:33 +00001145 if( 0 != ( ret = mbedtls_gcm_finish(
1146 (mbedtls_gcm_context *) ctx->cipher_ctx,
1147 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001148 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001149 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001150 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001151
1152 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001153 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001155
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001156 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001157 }
Daniel King8fe47012016-05-17 20:33:28 -03001158#endif /* MBEDTLS_GCM_C */
1159
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001160#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001161 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1162 {
1163 /* Don't allow truncated MAC for Poly1305 */
1164 if ( tag_len != sizeof( check_tag ) )
1165 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1166
Hanno Becker18597cd2018-11-09 16:36:33 +00001167 ret = mbedtls_chachapoly_finish(
1168 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001169 if ( ret != 0 )
1170 {
1171 return( ret );
1172 }
1173
1174 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001175 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Daniel King8fe47012016-05-17 20:33:28 -03001176 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1177
1178 return( 0 );
1179 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001180#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001181
1182 return( 0 );
1183}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001184#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001186/*
1187 * Packet-oriented wrapper for non-AEAD modes
1188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001190 const unsigned char *iv, size_t iv_len,
1191 const unsigned char *input, size_t ilen,
1192 unsigned char *output, size_t *olen )
1193{
Janos Follath24eed8d2019-11-22 13:21:35 +00001194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001195 size_t finish_olen;
1196
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001197 CIPHER_VALIDATE_RET( ctx != NULL );
1198 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1199 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1200 CIPHER_VALIDATE_RET( output != NULL );
1201 CIPHER_VALIDATE_RET( olen != NULL );
1202
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001203#if defined(MBEDTLS_USE_PSA_CRYPTO)
1204 if( ctx->psa_enabled == 1 )
1205 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001206 /* As in the non-PSA case, we don't check that
1207 * a key has been set. If not, the key slot will
1208 * still be in its default state of 0, which is
1209 * guaranteed to be invalid, hence the PSA-call
1210 * below will gracefully fail. */
1211 mbedtls_cipher_context_psa * const cipher_psa =
1212 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1213
1214 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001215 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001216 size_t part_len;
1217
1218 if( ctx->operation == MBEDTLS_DECRYPT )
1219 {
1220 status = psa_cipher_decrypt_setup( &cipher_op,
1221 cipher_psa->slot,
1222 cipher_psa->alg );
1223 }
1224 else if( ctx->operation == MBEDTLS_ENCRYPT )
1225 {
1226 status = psa_cipher_encrypt_setup( &cipher_op,
1227 cipher_psa->slot,
1228 cipher_psa->alg );
1229 }
1230 else
1231 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1232
1233 /* In the following, we can immediately return on an error,
1234 * because the PSA Crypto API guarantees that cipher operations
1235 * are terminated by unsuccessful calls to psa_cipher_update(),
1236 * and by any call to psa_cipher_finish(). */
1237 if( status != PSA_SUCCESS )
1238 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1239
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001240 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1241 {
1242 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1243 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6c0ec0e2021-09-30 15:51:05 +02001244 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001245 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001246
1247 status = psa_cipher_update( &cipher_op,
1248 input, ilen,
1249 output, ilen, olen );
1250 if( status != PSA_SUCCESS )
1251 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1252
1253 status = psa_cipher_finish( &cipher_op,
1254 output + *olen, ilen - *olen,
1255 &part_len );
1256 if( status != PSA_SUCCESS )
1257 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1258
1259 *olen += part_len;
1260 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001261 }
1262#endif /* MBEDTLS_USE_PSA_CRYPTO */
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001265 return( ret );
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001268 return( ret );
1269
Hanno Becker18597cd2018-11-09 16:36:33 +00001270 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1271 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001272 return( ret );
1273
Hanno Becker18597cd2018-11-09 16:36:33 +00001274 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1275 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001276 return( ret );
1277
1278 *olen += finish_olen;
1279
1280 return( 0 );
1281}
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001284/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001285 * Packet-oriented encryption for AEAD modes: internal function shared by
1286 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001287 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001288static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001289 const unsigned char *iv, size_t iv_len,
1290 const unsigned char *ad, size_t ad_len,
1291 const unsigned char *input, size_t ilen,
1292 unsigned char *output, size_t *olen,
1293 unsigned char *tag, size_t tag_len )
1294{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001295#if defined(MBEDTLS_USE_PSA_CRYPTO)
1296 if( ctx->psa_enabled == 1 )
1297 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001298 /* As in the non-PSA case, we don't check that
1299 * a key has been set. If not, the key slot will
1300 * still be in its default state of 0, which is
1301 * guaranteed to be invalid, hence the PSA-call
1302 * below will gracefully fail. */
1303 mbedtls_cipher_context_psa * const cipher_psa =
1304 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1305
1306 psa_status_t status;
1307
1308 /* PSA Crypto API always writes the authentication tag
1309 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001310 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001311 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1312
1313 status = psa_aead_encrypt( cipher_psa->slot,
1314 cipher_psa->alg,
1315 iv, iv_len,
1316 ad, ad_len,
1317 input, ilen,
1318 output, ilen + tag_len, olen );
1319 if( status != PSA_SUCCESS )
1320 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1321
1322 *olen -= tag_len;
1323 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001324 }
1325#endif /* MBEDTLS_USE_PSA_CRYPTO */
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_GCM_C)
1328 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001329 {
1330 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001331 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1332 ilen, iv, iv_len, ad, ad_len,
1333 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_GCM_C */
1336#if defined(MBEDTLS_CCM_C)
1337 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001338 {
1339 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001341 iv, iv_len, ad, ad_len, input, output,
1342 tag, tag_len ) );
1343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001345#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001346 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1347 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001348 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001349 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001350 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001351 {
1352 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1353 }
1354
1355 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001356 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001357 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001358 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001359#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001362}
1363
1364/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001365 * Packet-oriented encryption for AEAD modes: internal function shared by
1366 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001367 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001368static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001369 const unsigned char *iv, size_t iv_len,
1370 const unsigned char *ad, size_t ad_len,
1371 const unsigned char *input, size_t ilen,
1372 unsigned char *output, size_t *olen,
1373 const unsigned char *tag, size_t tag_len )
1374{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001375#if defined(MBEDTLS_USE_PSA_CRYPTO)
1376 if( ctx->psa_enabled == 1 )
1377 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001378 /* As in the non-PSA case, we don't check that
1379 * a key has been set. If not, the key slot will
1380 * still be in its default state of 0, which is
1381 * guaranteed to be invalid, hence the PSA-call
1382 * below will gracefully fail. */
1383 mbedtls_cipher_context_psa * const cipher_psa =
1384 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1385
1386 psa_status_t status;
1387
1388 /* PSA Crypto API always writes the authentication tag
1389 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001390 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001391 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1392
1393 status = psa_aead_decrypt( cipher_psa->slot,
1394 cipher_psa->alg,
1395 iv, iv_len,
1396 ad, ad_len,
1397 input, ilen + tag_len,
1398 output, ilen, olen );
1399 if( status == PSA_ERROR_INVALID_SIGNATURE )
1400 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1401 else if( status != PSA_SUCCESS )
1402 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1403
1404 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001405 }
1406#endif /* MBEDTLS_USE_PSA_CRYPTO */
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_GCM_C)
1409 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001410 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001412
1413 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001415 iv, iv_len, ad, ad_len,
1416 tag, tag_len, input, output );
1417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1419 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001420
1421 return( ret );
1422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_GCM_C */
1424#if defined(MBEDTLS_CCM_C)
1425 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001426 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001428
1429 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001431 iv, iv_len, ad, ad_len,
1432 input, output, tag, tag_len );
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1435 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001436
1437 return( ret );
1438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001440#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001441 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1442 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001444
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001445 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001446 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001447 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001448 {
1449 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1450 }
1451
1452 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001453 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1454 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001455
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001456 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1457 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001458
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001459 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001460 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001461#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001462
1463 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1464}
1465
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001466#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001467/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001468 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001469 */
1470int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1471 const unsigned char *iv, size_t iv_len,
1472 const unsigned char *ad, size_t ad_len,
1473 const unsigned char *input, size_t ilen,
1474 unsigned char *output, size_t *olen,
1475 unsigned char *tag, size_t tag_len )
1476{
1477 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001478 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001479 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1480 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001481 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001482 CIPHER_VALIDATE_RET( olen != NULL );
1483 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1484
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001485 return( mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1486 input, ilen, output, olen,
1487 tag, tag_len ) );
1488}
1489
1490/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001491 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001492 */
1493int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1494 const unsigned char *iv, size_t iv_len,
1495 const unsigned char *ad, size_t ad_len,
1496 const unsigned char *input, size_t ilen,
1497 unsigned char *output, size_t *olen,
1498 const unsigned char *tag, size_t tag_len )
1499{
1500 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001501 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001502 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1503 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001504 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001505 CIPHER_VALIDATE_RET( olen != NULL );
1506 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1507
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001508 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1509 input, ilen, output, olen,
1510 tag, tag_len ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001511}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001512#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001514
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001515#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1516/*
1517 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1518 */
1519int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1520 const unsigned char *iv, size_t iv_len,
1521 const unsigned char *ad, size_t ad_len,
1522 const unsigned char *input, size_t ilen,
1523 unsigned char *output, size_t output_len,
1524 size_t *olen, size_t tag_len )
1525{
1526 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001527 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001528 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1529 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1530 CIPHER_VALIDATE_RET( output != NULL );
1531 CIPHER_VALIDATE_RET( olen != NULL );
1532
1533#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001534 if(
1535#if defined(MBEDTLS_USE_PSA_CRYPTO)
1536 ctx->psa_enabled == 0 &&
1537#endif
1538 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1539 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001540 {
1541 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1542 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1543
1544 /* There is no iv, tag or ad associated with KW and KWP,
1545 * so these length should be 0 as documented. */
1546 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1547 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1548
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001549 (void) iv;
1550 (void) ad;
1551
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001552 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1553 output, olen, output_len ) );
1554 }
1555#endif /* MBEDTLS_NIST_KW_C */
1556
1557#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1558 /* AEAD case: check length before passing on to shared function */
1559 if( output_len < ilen + tag_len )
1560 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1561
1562 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1563 input, ilen, output, olen,
1564 output + ilen, tag_len );
1565 *olen += tag_len;
1566 return( ret );
1567#else
1568 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1569#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1570}
1571
1572/*
1573 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1574 */
1575int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1576 const unsigned char *iv, size_t iv_len,
1577 const unsigned char *ad, size_t ad_len,
1578 const unsigned char *input, size_t ilen,
1579 unsigned char *output, size_t output_len,
1580 size_t *olen, size_t tag_len )
1581{
1582 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001583 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001584 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1585 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1586 CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
1587 CIPHER_VALIDATE_RET( olen != NULL );
1588
1589#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001590 if(
1591#if defined(MBEDTLS_USE_PSA_CRYPTO)
1592 ctx->psa_enabled == 0 &&
1593#endif
1594 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1595 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001596 {
1597 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1598 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1599
1600 /* There is no iv, tag or ad associated with KW and KWP,
1601 * so these length should be 0 as documented. */
1602 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1603 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1604
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001605 (void) iv;
1606 (void) ad;
1607
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001608 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1609 output, olen, output_len ) );
1610 }
1611#endif /* MBEDTLS_NIST_KW_C */
1612
1613#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1614 /* AEAD case: check length before passing on to shared function */
1615 if( ilen < tag_len || output_len < ilen - tag_len )
1616 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1617
1618 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1619 input, ilen - tag_len, output, olen,
1620 input + ilen - tag_len, tag_len ) );
1621#else
1622 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1623#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1624}
1625#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_CIPHER_C */