blob: d51ccd77ffecfe23689bb9fd5e3a4ab7a42b6718 [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 {
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
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300398 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300399 {
400 memcpy( ctx->iv, iv, actual_iv_size );
401 ctx->iv_size = actual_iv_size;
402 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200403
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200404 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200408{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 CIPHER_VALIDATE_RET( ctx != NULL );
410 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200412
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000413#if defined(MBEDTLS_USE_PSA_CRYPTO)
414 if( ctx->psa_enabled == 1 )
415 {
416 /* We don't support resetting PSA-based
417 * cipher contexts, yet. */
418 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
419 }
420#endif /* MBEDTLS_USE_PSA_CRYPTO */
421
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422 ctx->unprocessed_len = 0;
423
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200424 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200425}
426
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200427#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200429 const unsigned char *ad, size_t ad_len )
430{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500431 CIPHER_VALIDATE_RET( ctx != NULL );
432 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
433 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200435
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000436#if defined(MBEDTLS_USE_PSA_CRYPTO)
437 if( ctx->psa_enabled == 1 )
438 {
439 /* While PSA Crypto has an API for multipart
440 * operations, we currently don't make it
441 * accessible through the cipher layer. */
442 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
443 }
444#endif /* MBEDTLS_USE_PSA_CRYPTO */
445
Daniel King8fe47012016-05-17 20:33:28 -0300446#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200448 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500449 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
450 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200451 }
Daniel King8fe47012016-05-17 20:33:28 -0300452#endif
453
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200454#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300455 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
456 {
457 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200458 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300459
460 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200461 ? MBEDTLS_CHACHAPOLY_ENCRYPT
462 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300463
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200464 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300465 ctx->iv,
466 mode );
467 if ( result != 0 )
468 return( result );
469
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
471 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300472 }
473#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200474
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200477#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200480 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481{
Janos Follath24eed8d2019-11-22 13:21:35 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500483 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 CIPHER_VALIDATE_RET( ctx != NULL );
486 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
487 CIPHER_VALIDATE_RET( output != NULL );
488 CIPHER_VALIDATE_RET( olen != NULL );
489 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000491
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000492#if defined(MBEDTLS_USE_PSA_CRYPTO)
493 if( ctx->psa_enabled == 1 )
494 {
495 /* While PSA Crypto has an API for multipart
496 * operations, we currently don't make it
497 * accessible through the cipher layer. */
498 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
499 }
500#endif /* MBEDTLS_USE_PSA_CRYPTO */
501
Paul Bakker6c212762013-12-16 15:24:50 +0100502 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100503 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100504 if ( 0 == block_size )
505 {
506 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
507 }
Paul Bakker6c212762013-12-16 15:24:50 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200510 {
Janos Follath98e28a72016-05-31 14:03:54 +0100511 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200513
514 *olen = ilen;
515
516 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
517 ctx->operation, input, output ) ) )
518 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200520 }
521
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200523 }
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_GCM_C)
526 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200527 {
528 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500529 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
530 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200531 }
532#endif
533
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200534#if defined(MBEDTLS_CHACHAPOLY_C)
535 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
536 {
537 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500538 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
539 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200540 }
541#endif
542
Paul Bakker68884e32013-01-07 18:20:04 +0100543 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100544 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_CIPHER_MODE_CBC)
550 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200552 size_t copy_len = 0;
553
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554 /*
555 * If there is not enough data for a full block, cache it.
556 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700557 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000558 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700559 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
560 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000562 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000563 {
564 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
565 ilen );
566
567 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200568 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 }
570
571 /*
572 * Process cached data first
573 */
Janos Follath98e28a72016-05-31 14:03:54 +0100574 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 {
Janos Follath98e28a72016-05-31 14:03:54 +0100576 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577
578 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
579 copy_len );
580
Paul Bakkerff61a782011-06-09 15:42:02 +0000581 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100582 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000583 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 }
587
Janos Follath98e28a72016-05-31 14:03:54 +0100588 *olen += block_size;
589 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590 ctx->unprocessed_len = 0;
591
592 input += copy_len;
593 ilen -= copy_len;
594 }
595
596 /*
597 * Cache final, incomplete block
598 */
599 if( 0 != ilen )
600 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700601 /* Encryption: only cache partial blocks
602 * Decryption w/ padding: always keep at least one whole block
603 * Decryption w/o padding: only cache partial blocks
604 */
Janos Follath98e28a72016-05-31 14:03:54 +0100605 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700606 if( copy_len == 0 &&
607 ctx->operation == MBEDTLS_DECRYPT &&
608 NULL != ctx->add_padding)
609 {
Janos Follath98e28a72016-05-31 14:03:54 +0100610 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700611 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612
613 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
614 copy_len );
615
616 ctx->unprocessed_len += copy_len;
617 ilen -= copy_len;
618 }
619
620 /*
621 * Process remaining full blocks
622 */
623 if( ilen )
624 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000625 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
626 ctx->operation, ilen, ctx->iv, input, 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 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200630
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 *olen += ilen;
632 }
633
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200634 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_CIPHER_MODE_CFB)
639 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000640 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000641 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000642 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000643 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000644 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000646 }
647
648 *olen = ilen;
649
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000653
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100654#if defined(MBEDTLS_CIPHER_MODE_OFB)
655 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
656 {
657 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
658 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
659 {
660 return( ret );
661 }
662
663 *olen = ilen;
664
665 return( 0 );
666 }
667#endif /* MBEDTLS_CIPHER_MODE_OFB */
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_CIPHER_MODE_CTR)
670 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000671 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000672 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000673 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000674 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000675 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000677 }
678
679 *olen = ilen;
680
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200681 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000684
Jaeden Ameroc6539902018-04-30 17:17:41 +0100685#if defined(MBEDTLS_CIPHER_MODE_XTS)
686 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
687 {
688 if( ctx->unprocessed_len > 0 ) {
689 /* We can only process an entire data unit at a time. */
690 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
691 }
692
693 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
694 ctx->operation, ilen, ctx->iv, input, output );
695 if( ret != 0 )
696 {
697 return( ret );
698 }
699
700 *olen = ilen;
701
702 return( 0 );
703 }
704#endif /* MBEDTLS_CIPHER_MODE_XTS */
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_CIPHER_MODE_STREAM)
707 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200708 {
709 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
710 ilen, input, output ) ) )
711 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200712 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200713 }
714
715 *olen = ilen;
716
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200717 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200718 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722}
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
725#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200726/*
727 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
728 */
Paul Bakker23986e52011-04-24 08:57:21 +0000729static void add_pkcs_padding( unsigned char *output, size_t output_len,
730 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731{
Paul Bakker23986e52011-04-24 08:57:21 +0000732 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100733 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734
735 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000736 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000737}
738
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200739static int get_pkcs_padding( unsigned char *input, size_t input_len,
740 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100742 size_t i, pad_idx;
743 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744
Paul Bakkera885d682011-01-20 16:35:05 +0000745 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
748 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749 *data_len = input_len - padding_len;
750
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100751 /* Avoid logical || since it results in a branch */
752 bad |= padding_len > input_len;
753 bad |= padding_len == 0;
754
755 /* The number of bytes checked must be independent of padding_len,
756 * so pick input_len, which is usually 8 or 16 (one block) */
757 pad_idx = input_len - padding_len;
758 for( i = 0; i < input_len; i++ )
759 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200766/*
767 * One and zeros padding: fill with 80 00 ... 00
768 */
769static void add_one_and_zeros_padding( unsigned char *output,
770 size_t output_len, size_t data_len )
771{
772 size_t padding_len = output_len - data_len;
773 unsigned char i = 0;
774
775 output[data_len] = 0x80;
776 for( i = 1; i < padding_len; i++ )
777 output[data_len + i] = 0x00;
778}
779
780static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
781 size_t *data_len )
782{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100783 size_t i;
784 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200785
786 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200788
Micha Krausba8316f2017-12-23 23:40:08 +0100789 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100790 *data_len = 0;
791 for( i = input_len; i > 0; i-- )
792 {
793 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100794 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100795 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100796 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100797 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200800
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200805/*
806 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
807 */
808static void add_zeros_and_len_padding( unsigned char *output,
809 size_t output_len, size_t data_len )
810{
811 size_t padding_len = output_len - data_len;
812 unsigned char i = 0;
813
814 for( i = 1; i < padding_len; i++ )
815 output[data_len + i - 1] = 0x00;
816 output[output_len - 1] = (unsigned char) padding_len;
817}
818
819static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
820 size_t *data_len )
821{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100822 size_t i, pad_idx;
823 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200824
825 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200827
828 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200829 *data_len = input_len - padding_len;
830
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100831 /* Avoid logical || since it results in a branch */
832 bad |= padding_len > input_len;
833 bad |= padding_len == 0;
834
835 /* The number of bytes checked must be independent of padding_len */
836 pad_idx = input_len - padding_len;
837 for( i = 0; i < input_len - 1; i++ )
838 bad |= input[i] * ( i >= pad_idx );
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200845/*
846 * Zero padding: fill with 00 ... 00
847 */
848static void add_zeros_padding( unsigned char *output,
849 size_t output_len, size_t data_len )
850{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200851 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200852
853 for( i = data_len; i < output_len; i++ )
854 output[i] = 0x00;
855}
856
857static int get_zeros_padding( unsigned char *input, size_t input_len,
858 size_t *data_len )
859{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100860 size_t i;
861 unsigned char done = 0, prev_done;
862
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200863 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200865
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100866 *data_len = 0;
867 for( i = input_len; i > 0; i-- )
868 {
869 prev_done = done;
870 done |= ( input[i-1] != 0 );
871 *data_len |= i * ( done != prev_done );
872 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200873
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200874 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200877
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200878/*
879 * No padding: don't pad :)
880 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200882 * but a trivial get_padding function
883 */
884static int get_no_padding( unsigned char *input, size_t input_len,
885 size_t *data_len )
886{
887 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200889
890 *data_len = input_len;
891
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200892 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200897 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000898{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500899 CIPHER_VALIDATE_RET( ctx != NULL );
900 CIPHER_VALIDATE_RET( output != NULL );
901 CIPHER_VALIDATE_RET( olen != NULL );
902 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000904
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000905#if defined(MBEDTLS_USE_PSA_CRYPTO)
906 if( ctx->psa_enabled == 1 )
907 {
908 /* While PSA Crypto has an API for multipart
909 * operations, we currently don't make it
910 * accessible through the cipher layer. */
911 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
912 }
913#endif /* MBEDTLS_USE_PSA_CRYPTO */
914
Paul Bakker8123e9d2011-01-06 15:37:30 +0000915 *olen = 0;
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100918 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
920 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100921 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000923 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200924 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000925 }
926
Daniel King8fe47012016-05-17 20:33:28 -0300927 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
928 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300929 {
930 return( 0 );
931 }
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200934 {
935 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200937
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200938 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200939 }
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_CIPHER_MODE_CBC)
942 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000943 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200944 int ret = 0;
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000947 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200948 /* check for 'no padding' mode */
949 if( NULL == ctx->add_padding )
950 {
951 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200953
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200954 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200955 }
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000958 ctx->unprocessed_len );
959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000961 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200962 /*
963 * For decrypt operations, expect a full block,
964 * or an empty block if no padding
965 */
966 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200967 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000970 }
971
972 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000973 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000975 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000976 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200977 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 }
979
980 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500982 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
983 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000984
985 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000988 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200989#else
990 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000994}
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +0000997int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
998 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200999{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001000 CIPHER_VALIDATE_RET( ctx != NULL );
1001
1002 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001005 }
1006
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001007#if defined(MBEDTLS_USE_PSA_CRYPTO)
1008 if( ctx->psa_enabled == 1 )
1009 {
1010 /* While PSA Crypto knows about CBC padding
1011 * schemes, we currently don't make them
1012 * accessible through the cipher layer. */
1013 if( mode != MBEDTLS_PADDING_NONE )
1014 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1015
1016 return( 0 );
1017 }
1018#endif /* MBEDTLS_USE_PSA_CRYPTO */
1019
Paul Bakker1a45d912013-08-14 12:04:26 +02001020 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1023 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001024 ctx->add_padding = add_pkcs_padding;
1025 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001026 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1029 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001030 ctx->add_padding = add_one_and_zeros_padding;
1031 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001032 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1035 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001036 ctx->add_padding = add_zeros_and_len_padding;
1037 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001038 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1041 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001042 ctx->add_padding = add_zeros_padding;
1043 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001044 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001047 ctx->add_padding = NULL;
1048 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001049 break;
1050
1051 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001053 }
1054
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001055 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001058
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001059#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001061 unsigned char *tag, size_t tag_len )
1062{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001063 CIPHER_VALIDATE_RET( ctx != NULL );
1064 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1065 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 if( MBEDTLS_ENCRYPT != ctx->operation )
1069 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001070
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001071#if defined(MBEDTLS_USE_PSA_CRYPTO)
1072 if( ctx->psa_enabled == 1 )
1073 {
1074 /* While PSA Crypto has an API for multipart
1075 * operations, we currently don't make it
1076 * accessible through the cipher layer. */
1077 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001078 }
1079#endif /* MBEDTLS_USE_PSA_CRYPTO */
1080
Daniel King8fe47012016-05-17 20:33:28 -03001081#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001083 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1084 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001085#endif
1086
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001087#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001088 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1089 {
1090 /* Don't allow truncated MAC for Poly1305 */
1091 if ( tag_len != 16U )
1092 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1093
Hanno Becker18597cd2018-11-09 16:36:33 +00001094 return( mbedtls_chachapoly_finish(
1095 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001096 }
1097#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001098
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001099 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001100}
Paul Bakker9af723c2014-05-01 13:03:14 +02001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001103 const unsigned char *tag, size_t tag_len )
1104{
Daniel King8fe47012016-05-17 20:33:28 -03001105 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001107
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001108 CIPHER_VALIDATE_RET( ctx != NULL );
1109 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1110 if( ctx->cipher_info == NULL )
1111 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1112
1113 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001116 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001117
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001118#if defined(MBEDTLS_USE_PSA_CRYPTO)
1119 if( ctx->psa_enabled == 1 )
1120 {
1121 /* While PSA Crypto has an API for multipart
1122 * operations, we currently don't make it
1123 * accessible through the cipher layer. */
1124 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1125 }
1126#endif /* MBEDTLS_USE_PSA_CRYPTO */
1127
Daniel King8fe47012016-05-17 20:33:28 -03001128#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001130 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001131 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001133
Hanno Becker18597cd2018-11-09 16:36:33 +00001134 if( 0 != ( ret = mbedtls_gcm_finish(
1135 (mbedtls_gcm_context *) ctx->cipher_ctx,
1136 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001137 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001138 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001139 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001140
1141 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001142 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001144
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001145 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146 }
Daniel King8fe47012016-05-17 20:33:28 -03001147#endif /* MBEDTLS_GCM_C */
1148
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001149#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001150 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1151 {
1152 /* Don't allow truncated MAC for Poly1305 */
1153 if ( tag_len != sizeof( check_tag ) )
1154 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1155
Hanno Becker18597cd2018-11-09 16:36:33 +00001156 ret = mbedtls_chachapoly_finish(
1157 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001158 if ( ret != 0 )
1159 {
1160 return( ret );
1161 }
1162
1163 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001164 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Daniel King8fe47012016-05-17 20:33:28 -03001165 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1166
1167 return( 0 );
1168 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001169#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001170
1171 return( 0 );
1172}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001173#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001174
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001175/*
1176 * Packet-oriented wrapper for non-AEAD modes
1177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001179 const unsigned char *iv, size_t iv_len,
1180 const unsigned char *input, size_t ilen,
1181 unsigned char *output, size_t *olen )
1182{
Janos Follath24eed8d2019-11-22 13:21:35 +00001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001184 size_t finish_olen;
1185
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001186 CIPHER_VALIDATE_RET( ctx != NULL );
1187 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1188 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1189 CIPHER_VALIDATE_RET( output != NULL );
1190 CIPHER_VALIDATE_RET( olen != NULL );
1191
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001192#if defined(MBEDTLS_USE_PSA_CRYPTO)
1193 if( ctx->psa_enabled == 1 )
1194 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001195 /* As in the non-PSA case, we don't check that
1196 * a key has been set. If not, the key slot will
1197 * still be in its default state of 0, which is
1198 * guaranteed to be invalid, hence the PSA-call
1199 * below will gracefully fail. */
1200 mbedtls_cipher_context_psa * const cipher_psa =
1201 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1202
1203 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001204 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001205 size_t part_len;
1206
1207 if( ctx->operation == MBEDTLS_DECRYPT )
1208 {
1209 status = psa_cipher_decrypt_setup( &cipher_op,
1210 cipher_psa->slot,
1211 cipher_psa->alg );
1212 }
1213 else if( ctx->operation == MBEDTLS_ENCRYPT )
1214 {
1215 status = psa_cipher_encrypt_setup( &cipher_op,
1216 cipher_psa->slot,
1217 cipher_psa->alg );
1218 }
1219 else
1220 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1221
1222 /* In the following, we can immediately return on an error,
1223 * because the PSA Crypto API guarantees that cipher operations
1224 * are terminated by unsuccessful calls to psa_cipher_update(),
1225 * and by any call to psa_cipher_finish(). */
1226 if( status != PSA_SUCCESS )
1227 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1228
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001229 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1230 {
1231 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1232 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6c0ec0e2021-09-30 15:51:05 +02001233 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001234 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001235
1236 status = psa_cipher_update( &cipher_op,
1237 input, ilen,
1238 output, ilen, olen );
1239 if( status != PSA_SUCCESS )
1240 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1241
1242 status = psa_cipher_finish( &cipher_op,
1243 output + *olen, ilen - *olen,
1244 &part_len );
1245 if( status != PSA_SUCCESS )
1246 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1247
1248 *olen += part_len;
1249 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001250 }
1251#endif /* MBEDTLS_USE_PSA_CRYPTO */
1252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001254 return( ret );
1255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001257 return( ret );
1258
Hanno Becker18597cd2018-11-09 16:36:33 +00001259 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1260 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001261 return( ret );
1262
Hanno Becker18597cd2018-11-09 16:36:33 +00001263 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1264 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001265 return( ret );
1266
1267 *olen += finish_olen;
1268
1269 return( 0 );
1270}
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001273/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001274 * Packet-oriented encryption for AEAD modes: internal function shared by
1275 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001276 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001277static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001278 const unsigned char *iv, size_t iv_len,
1279 const unsigned char *ad, size_t ad_len,
1280 const unsigned char *input, size_t ilen,
1281 unsigned char *output, size_t *olen,
1282 unsigned char *tag, size_t tag_len )
1283{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001284#if defined(MBEDTLS_USE_PSA_CRYPTO)
1285 if( ctx->psa_enabled == 1 )
1286 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001287 /* As in the non-PSA case, we don't check that
1288 * a key has been set. If not, the key slot will
1289 * still be in its default state of 0, which is
1290 * guaranteed to be invalid, hence the PSA-call
1291 * below will gracefully fail. */
1292 mbedtls_cipher_context_psa * const cipher_psa =
1293 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1294
1295 psa_status_t status;
1296
1297 /* PSA Crypto API always writes the authentication tag
1298 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001299 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001300 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1301
1302 status = psa_aead_encrypt( cipher_psa->slot,
1303 cipher_psa->alg,
1304 iv, iv_len,
1305 ad, ad_len,
1306 input, ilen,
1307 output, ilen + tag_len, olen );
1308 if( status != PSA_SUCCESS )
1309 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1310
1311 *olen -= tag_len;
1312 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001313 }
1314#endif /* MBEDTLS_USE_PSA_CRYPTO */
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#if defined(MBEDTLS_GCM_C)
1317 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001318 {
1319 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001320 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1321 ilen, iv, iv_len, ad, ad_len,
1322 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_GCM_C */
1325#if defined(MBEDTLS_CCM_C)
1326 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001327 {
1328 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001330 iv, iv_len, ad, ad_len, input, output,
1331 tag, tag_len ) );
1332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001334#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001335 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1336 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001337 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001338 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001339 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001340 {
1341 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1342 }
1343
1344 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001345 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001346 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001347 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001348#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001351}
1352
1353/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001354 * Packet-oriented encryption for AEAD modes: internal function shared by
1355 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001356 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001357static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001358 const unsigned char *iv, size_t iv_len,
1359 const unsigned char *ad, size_t ad_len,
1360 const unsigned char *input, size_t ilen,
1361 unsigned char *output, size_t *olen,
1362 const unsigned char *tag, size_t tag_len )
1363{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001364#if defined(MBEDTLS_USE_PSA_CRYPTO)
1365 if( ctx->psa_enabled == 1 )
1366 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001367 /* As in the non-PSA case, we don't check that
1368 * a key has been set. If not, the key slot will
1369 * still be in its default state of 0, which is
1370 * guaranteed to be invalid, hence the PSA-call
1371 * below will gracefully fail. */
1372 mbedtls_cipher_context_psa * const cipher_psa =
1373 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1374
1375 psa_status_t status;
1376
1377 /* PSA Crypto API always writes the authentication tag
1378 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001379 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001380 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1381
1382 status = psa_aead_decrypt( cipher_psa->slot,
1383 cipher_psa->alg,
1384 iv, iv_len,
1385 ad, ad_len,
1386 input, ilen + tag_len,
1387 output, ilen, olen );
1388 if( status == PSA_ERROR_INVALID_SIGNATURE )
1389 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1390 else if( status != PSA_SUCCESS )
1391 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1392
1393 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001394 }
1395#endif /* MBEDTLS_USE_PSA_CRYPTO */
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_GCM_C)
1398 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001399 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401
1402 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404 iv, iv_len, ad, ad_len,
1405 tag, tag_len, input, output );
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1408 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001409
1410 return( ret );
1411 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#endif /* MBEDTLS_GCM_C */
1413#if defined(MBEDTLS_CCM_C)
1414 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001415 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001417
1418 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001420 iv, iv_len, ad, ad_len,
1421 input, output, tag, tag_len );
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1424 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001425
1426 return( ret );
1427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001429#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001430 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1431 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001433
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001434 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001435 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001436 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001437 {
1438 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1439 }
1440
1441 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001442 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1443 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001444
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001445 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1446 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001447
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001448 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001449 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001450#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001451
1452 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1453}
1454
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001455#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001456/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001457 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001458 */
1459int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1460 const unsigned char *iv, size_t iv_len,
1461 const unsigned char *ad, size_t ad_len,
1462 const unsigned char *input, size_t ilen,
1463 unsigned char *output, size_t *olen,
1464 unsigned char *tag, size_t tag_len )
1465{
1466 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001467 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001468 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1469 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001470 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001471 CIPHER_VALIDATE_RET( olen != NULL );
1472 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1473
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001474 return( mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1475 input, ilen, output, olen,
1476 tag, tag_len ) );
1477}
1478
1479/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001480 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001481 */
1482int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1483 const unsigned char *iv, size_t iv_len,
1484 const unsigned char *ad, size_t ad_len,
1485 const unsigned char *input, size_t ilen,
1486 unsigned char *output, size_t *olen,
1487 const unsigned char *tag, size_t tag_len )
1488{
1489 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001490 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001491 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1492 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001493 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001494 CIPHER_VALIDATE_RET( olen != NULL );
1495 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1496
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001497 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1498 input, ilen, output, olen,
1499 tag, tag_len ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001500}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001501#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001503
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001504#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1505/*
1506 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1507 */
1508int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1509 const unsigned char *iv, size_t iv_len,
1510 const unsigned char *ad, size_t ad_len,
1511 const unsigned char *input, size_t ilen,
1512 unsigned char *output, size_t output_len,
1513 size_t *olen, size_t tag_len )
1514{
1515 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001516 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001517 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1518 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1519 CIPHER_VALIDATE_RET( output != NULL );
1520 CIPHER_VALIDATE_RET( olen != NULL );
1521
1522#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001523 if(
1524#if defined(MBEDTLS_USE_PSA_CRYPTO)
1525 ctx->psa_enabled == 0 &&
1526#endif
1527 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1528 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001529 {
1530 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1531 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1532
1533 /* There is no iv, tag or ad associated with KW and KWP,
1534 * so these length should be 0 as documented. */
1535 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1536 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1537
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001538 (void) iv;
1539 (void) ad;
1540
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001541 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1542 output, olen, output_len ) );
1543 }
1544#endif /* MBEDTLS_NIST_KW_C */
1545
1546#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1547 /* AEAD case: check length before passing on to shared function */
1548 if( output_len < ilen + tag_len )
1549 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1550
1551 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1552 input, ilen, output, olen,
1553 output + ilen, tag_len );
1554 *olen += tag_len;
1555 return( ret );
1556#else
1557 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1558#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1559}
1560
1561/*
1562 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1563 */
1564int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1565 const unsigned char *iv, size_t iv_len,
1566 const unsigned char *ad, size_t ad_len,
1567 const unsigned char *input, size_t ilen,
1568 unsigned char *output, size_t output_len,
1569 size_t *olen, size_t tag_len )
1570{
1571 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001572 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1574 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1575 CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
1576 CIPHER_VALIDATE_RET( olen != NULL );
1577
1578#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001579 if(
1580#if defined(MBEDTLS_USE_PSA_CRYPTO)
1581 ctx->psa_enabled == 0 &&
1582#endif
1583 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1584 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001585 {
1586 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1587 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1588
1589 /* There is no iv, tag or ad associated with KW and KWP,
1590 * so these length should be 0 as documented. */
1591 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1592 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1593
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001594 (void) iv;
1595 (void) ad;
1596
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001597 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1598 output, olen, output_len ) );
1599 }
1600#endif /* MBEDTLS_NIST_KW_C */
1601
1602#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1603 /* AEAD case: check length before passing on to shared function */
1604 if( ilen < tag_len || output_len < ilen - tag_len )
1605 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1606
1607 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1608 input, ilen - tag_len, output, olen,
1609 input + ilen - tag_len, tag_len ) );
1610#else
1611 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1612#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1613}
1614#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_CIPHER_C */