blob: dffe3adca407e4918be2437a5b972f1f4b9a8612 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073 int *type;
74
75 if( ! supported_init )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 def = mbedtls_cipher_definitions;
78 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079
80 while( def->type != 0 )
81 *type++ = (*def++).type;
82
83 *type = 0;
84
85 supported_init = 1;
86 }
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000089}
90
Hanno Becker18597cd2018-11-09 16:36:33 +000091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
92 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020097 if( def->type == cipher_type )
98 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000099
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200100 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101}
102
Hanno Becker18597cd2018-11-09 16:36:33 +0000103const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
104 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200107
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200109 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200112 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200113 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000114
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200115 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116}
117
Hanno Becker18597cd2018-11-09 16:36:33 +0000118const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
119 const mbedtls_cipher_id_t cipher_id,
120 int key_bitlen,
121 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200126 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200127 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200128 def->info->mode == mode )
129 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200130
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200131 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200140{
141 if( ctx == NULL )
142 return;
143
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000144#if defined(MBEDTLS_USE_PSA_CRYPTO)
145 if( ctx->psa_enabled == 1 )
146 {
Hanno Becker6118e432018-11-09 16:47:20 +0000147 if( ctx->cipher_ctx != NULL )
148 {
149 mbedtls_cipher_context_psa * const cipher_psa =
150 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
151
Hanno Becker19086552018-11-17 22:11:16 +0000152 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000153 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000154 /* xxx_free() doesn't allow to return failures. */
155 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000156 }
157
158 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
159 mbedtls_free( cipher_psa );
160 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000161
162 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
163 return;
164 }
165#endif /* MBEDTLS_USE_PSA_CRYPTO */
166
Simon Butcher327398a2016-10-05 14:09:11 +0100167#if defined(MBEDTLS_CMAC_C)
168 if( ctx->cmac_ctx )
169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500170 mbedtls_platform_zeroize( ctx->cmac_ctx,
171 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100172 mbedtls_free( ctx->cmac_ctx );
173 }
174#endif
175
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176 if( ctx->cipher_ctx )
177 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
178
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500179 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180}
181
Hanno Becker18597cd2018-11-09 16:36:33 +0000182int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
183 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189
Paul Bakker343a8702011-06-09 14:27:58 +0000190 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
193 ctx->cipher_info = cipher_info;
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200196 /*
197 * Ignore possible errors caused by a cipher mode that doesn't use padding
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
200 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200201#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200205
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200206 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207}
208
Hanno Becker4ccfc402018-11-09 16:10:57 +0000209#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200210#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000211int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000212 const mbedtls_cipher_info_t *cipher_info,
213 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000214{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000215 psa_algorithm_t alg;
216 mbedtls_cipher_context_psa *cipher_psa;
217
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000218 if( NULL == cipher_info || NULL == ctx )
219 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
220
Hanno Becker4ee7e762018-11-17 22:00:38 +0000221 /* Check that the underlying cipher mode and cipher type are
222 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000223 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000224 if( alg == 0 )
225 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
226 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000227 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000228
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000229 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
230
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
232 if( cipher_psa == NULL )
233 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
234 cipher_psa->alg = alg;
235 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000236 ctx->cipher_info = cipher_info;
237 ctx->psa_enabled = 1;
238 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000239}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200240#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000241#endif /* MBEDTLS_USE_PSA_CRYPTO */
242
Hanno Becker18597cd2018-11-09 16:36:33 +0000243int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
244 const unsigned char *key,
245 int key_bitlen,
246 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247{
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100248 if( operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT )
249 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000253#if defined(MBEDTLS_USE_PSA_CRYPTO)
254 if( ctx->psa_enabled == 1 )
255 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000256 mbedtls_cipher_context_psa * const cipher_psa =
257 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
258
259 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
260
261 psa_status_t status;
262 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000264
265 /* PSA Crypto API only accepts byte-aligned keys. */
266 if( key_bitlen % 8 != 0 )
267 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
268
269 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000270 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
272
Andrzej Kurekc7509322019-01-08 09:36:01 -0500273 key_type = mbedtls_psa_translate_cipher_type(
274 ctx->cipher_info->type );
275 if( key_type == 0 )
276 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200277 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000278
279 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500280 * (encrypt vs. decrypt): it is possible to setup a key for encryption
281 * and use it for AEAD decryption. Until tests relying on this
282 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200283 psa_set_key_usage_flags( &attributes,
284 /* mbedtls_psa_translate_cipher_operation( operation ); */
285 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
286 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000287
Gilles Peskined2d45c12019-05-27 14:53:13 +0200288 status = psa_import_key( &attributes, key, key_bytelen,
289 &cipher_psa->slot );
290 switch( status )
291 {
292 case PSA_SUCCESS:
293 break;
294 case PSA_ERROR_INSUFFICIENT_MEMORY:
295 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
296 case PSA_ERROR_NOT_SUPPORTED:
297 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
298 default:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200299 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200300 }
301 /* Indicate that we own the key slot and need to
302 * destroy it in mbedtls_cipher_free(). */
303 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000304
305 ctx->key_bitlen = key_bitlen;
306 ctx->operation = operation;
307 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000308 }
309#endif /* MBEDTLS_USE_PSA_CRYPTO */
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200312 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200315 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200316
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200317 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000318 ctx->operation = operation;
319
Paul Bakker343a8702011-06-09 14:27:58 +0000320 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100321 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( MBEDTLS_ENCRYPT == operation ||
324 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100325 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000327 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
329 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000330 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
334 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 const unsigned char *iv,
341 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200343 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 if( ctx->cipher_info == NULL )
346 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000347#if defined(MBEDTLS_USE_PSA_CRYPTO)
348 if( ctx->psa_enabled == 1 )
349 {
350 /* While PSA Crypto has an API for multipart
351 * operations, we currently don't make it
352 * accessible through the cipher layer. */
353 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
354 }
355#endif /* MBEDTLS_USE_PSA_CRYPTO */
356
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200357 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
359 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200362 actual_iv_size = iv_len;
363 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200364 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200365 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200366
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200367 /* avoid reading past the end of input buffer */
368 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200370 }
371
Daniel Kingbd920622016-05-15 19:56:20 -0300372#if defined(MBEDTLS_CHACHA20_C)
373 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
374 {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100375 /* Even though the actual_iv_size is overwritten with a correct value
376 * of 12 from the cipher info, return an error to indicate that
377 * the input iv_len is wrong. */
378 if( iv_len != 12 )
379 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
380
Daniel Kingbd920622016-05-15 19:56:20 -0300381 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
382 iv,
383 0U ) ) /* Initial counter value */
384 {
385 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
386 }
387 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100388#if defined(MBEDTLS_CHACHAPOLY_C)
389 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
390 iv_len != 12 )
391 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
392#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300393#endif
394
Gilles Peskine295fc132021-04-15 18:32:23 +0200395#if defined(MBEDTLS_GCM_C)
396 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
397 {
398 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx,
399 ctx->operation,
400 iv, iv_len ) );
401 }
402#endif
403
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200404#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200405 if( MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200406 {
407 int set_lengths_result;
408 int ccm_star_mode;
409
410 set_lengths_result = mbedtls_ccm_set_lengths(
411 (mbedtls_ccm_context *) ctx->cipher_ctx,
412 0, 0, 0 );
413 if( set_lengths_result != 0 )
414 return set_lengths_result;
415
416 if( ctx->operation == MBEDTLS_DECRYPT )
417 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
418 else if( ctx->operation == MBEDTLS_ENCRYPT )
419 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
420 else
421 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
422
423 return( mbedtls_ccm_starts( (mbedtls_ccm_context *) ctx->cipher_ctx,
424 ccm_star_mode,
425 iv, iv_len ) );
426 }
427#endif
428
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300429 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300430 {
431 memcpy( ctx->iv, iv, actual_iv_size );
432 ctx->iv_size = actual_iv_size;
433 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200434
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200435 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200436}
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200439{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500440 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200442
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000443#if defined(MBEDTLS_USE_PSA_CRYPTO)
444 if( ctx->psa_enabled == 1 )
445 {
446 /* We don't support resetting PSA-based
447 * cipher contexts, yet. */
448 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
449 }
450#endif /* MBEDTLS_USE_PSA_CRYPTO */
451
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452 ctx->unprocessed_len = 0;
453
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200454 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200455}
456
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200457#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200459 const unsigned char *ad, size_t ad_len )
460{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200463
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000464#if defined(MBEDTLS_USE_PSA_CRYPTO)
465 if( ctx->psa_enabled == 1 )
466 {
467 /* While PSA Crypto has an API for multipart
468 * operations, we currently don't make it
469 * accessible through the cipher layer. */
470 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
471 }
472#endif /* MBEDTLS_USE_PSA_CRYPTO */
473
Daniel King8fe47012016-05-17 20:33:28 -0300474#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200476 {
Gilles Peskine295fc132021-04-15 18:32:23 +0200477 return( mbedtls_gcm_update_ad( (mbedtls_gcm_context *) ctx->cipher_ctx,
478 ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200479 }
Daniel King8fe47012016-05-17 20:33:28 -0300480#endif
481
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200482#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300483 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
484 {
485 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200486 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300487
488 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200489 ? MBEDTLS_CHACHAPOLY_ENCRYPT
490 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300491
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200492 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300493 ctx->iv,
494 mode );
495 if ( result != 0 )
496 return( result );
497
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500498 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
499 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300500 }
501#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200502
Denis V. Lunev2df73ae2018-11-01 12:22:27 +0300503 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200505#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200508 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509{
Janos Follath24eed8d2019-11-22 13:21:35 +0000510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500513 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000516#if defined(MBEDTLS_USE_PSA_CRYPTO)
517 if( ctx->psa_enabled == 1 )
518 {
519 /* While PSA Crypto has an API for multipart
520 * operations, we currently don't make it
521 * accessible through the cipher layer. */
522 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
523 }
524#endif /* MBEDTLS_USE_PSA_CRYPTO */
525
Paul Bakker6c212762013-12-16 15:24:50 +0100526 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100527 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100528 if ( 0 == block_size )
529 {
530 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
531 }
Paul Bakker6c212762013-12-16 15:24:50 +0100532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200534 {
Janos Follath98e28a72016-05-31 14:03:54 +0100535 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200537
538 *olen = ilen;
539
540 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
541 ctx->operation, input, output ) ) )
542 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200543 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200544 }
545
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200547 }
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_GCM_C)
550 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200551 {
Gilles Peskinea56c4482021-04-15 17:22:35 +0200552 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx,
553 input, ilen,
554 output, ilen, olen ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200555 }
556#endif
557
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200558#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200559 if( ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200560 {
561 return( mbedtls_ccm_update( (mbedtls_ccm_context *) ctx->cipher_ctx,
562 input, ilen,
563 output, ilen, olen ) );
564 }
565#endif
566
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200567#if defined(MBEDTLS_CHACHAPOLY_C)
568 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
569 {
570 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500571 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
572 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200573 }
574#endif
575
Paul Bakker68884e32013-01-07 18:20:04 +0100576 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100577 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100580 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_CIPHER_MODE_CBC)
583 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200585 size_t copy_len = 0;
586
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587 /*
588 * If there is not enough data for a full block, cache it.
589 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700590 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000591 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700592 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
593 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000595 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 {
597 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
598 ilen );
599
600 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200601 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602 }
603
604 /*
605 * Process cached data first
606 */
Janos Follath98e28a72016-05-31 14:03:54 +0100607 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 {
Janos Follath98e28a72016-05-31 14:03:54 +0100609 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
611 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
612 copy_len );
613
Paul Bakkerff61a782011-06-09 15:42:02 +0000614 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100615 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000616 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000619 }
620
Janos Follath98e28a72016-05-31 14:03:54 +0100621 *olen += block_size;
622 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623 ctx->unprocessed_len = 0;
624
625 input += copy_len;
626 ilen -= copy_len;
627 }
628
629 /*
630 * Cache final, incomplete block
631 */
632 if( 0 != ilen )
633 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700634 /* Encryption: only cache partial blocks
635 * Decryption w/ padding: always keep at least one whole block
636 * Decryption w/o padding: only cache partial blocks
637 */
Janos Follath98e28a72016-05-31 14:03:54 +0100638 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700639 if( copy_len == 0 &&
640 ctx->operation == MBEDTLS_DECRYPT &&
641 NULL != ctx->add_padding)
642 {
Janos Follath98e28a72016-05-31 14:03:54 +0100643 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700644 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
646 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
647 copy_len );
648
649 ctx->unprocessed_len += copy_len;
650 ilen -= copy_len;
651 }
652
653 /*
654 * Process remaining full blocks
655 */
656 if( ilen )
657 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000658 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
659 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200663
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 *olen += ilen;
665 }
666
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_CIPHER_MODE_CFB)
672 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000673 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000674 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000675 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000676 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000677 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200678 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000679 }
680
681 *olen = ilen;
682
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200683 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000686
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100687#if defined(MBEDTLS_CIPHER_MODE_OFB)
688 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
689 {
690 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
691 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
692 {
693 return( ret );
694 }
695
696 *olen = ilen;
697
698 return( 0 );
699 }
700#endif /* MBEDTLS_CIPHER_MODE_OFB */
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#if defined(MBEDTLS_CIPHER_MODE_CTR)
703 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000704 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000705 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000706 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000707 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000708 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200709 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000710 }
711
712 *olen = ilen;
713
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200714 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000717
Jaeden Ameroc6539902018-04-30 17:17:41 +0100718#if defined(MBEDTLS_CIPHER_MODE_XTS)
719 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
720 {
721 if( ctx->unprocessed_len > 0 ) {
722 /* We can only process an entire data unit at a time. */
723 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
724 }
725
726 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
727 ctx->operation, ilen, ctx->iv, input, output );
728 if( ret != 0 )
729 {
730 return( ret );
731 }
732
733 *olen = ilen;
734
735 return( 0 );
736 }
737#endif /* MBEDTLS_CIPHER_MODE_XTS */
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_CIPHER_MODE_STREAM)
740 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200741 {
742 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
743 ilen, input, output ) ) )
744 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200745 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200746 }
747
748 *olen = ilen;
749
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200750 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755}
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
758#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200759/*
760 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
761 */
Paul Bakker23986e52011-04-24 08:57:21 +0000762static void add_pkcs_padding( unsigned char *output, size_t output_len,
763 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764{
Paul Bakker23986e52011-04-24 08:57:21 +0000765 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100766 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767
768 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000769 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770}
771
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200772static int get_pkcs_padding( unsigned char *input, size_t input_len,
773 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000774{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100775 size_t i, pad_idx;
776 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000777
Paul Bakkera885d682011-01-20 16:35:05 +0000778 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780
781 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782 *data_len = input_len - padding_len;
783
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100784 /* Avoid logical || since it results in a branch */
785 bad |= padding_len > input_len;
786 bad |= padding_len == 0;
787
788 /* The number of bytes checked must be independent of padding_len,
789 * so pick input_len, which is usually 8 or 16 (one block) */
790 pad_idx = input_len - padding_len;
791 for( i = 0; i < input_len; i++ )
792 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200799/*
800 * One and zeros padding: fill with 80 00 ... 00
801 */
802static void add_one_and_zeros_padding( unsigned char *output,
803 size_t output_len, size_t data_len )
804{
805 size_t padding_len = output_len - data_len;
806 unsigned char i = 0;
807
808 output[data_len] = 0x80;
809 for( i = 1; i < padding_len; i++ )
810 output[data_len + i] = 0x00;
811}
812
813static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
814 size_t *data_len )
815{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100816 size_t i;
817 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200818
819 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200821
Micha Krausba8316f2017-12-23 23:40:08 +0100822 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100823 *data_len = 0;
824 for( i = input_len; i > 0; i-- )
825 {
826 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100827 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100828 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100829 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100830 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200838/*
839 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
840 */
841static void add_zeros_and_len_padding( unsigned char *output,
842 size_t output_len, size_t data_len )
843{
844 size_t padding_len = output_len - data_len;
845 unsigned char i = 0;
846
847 for( i = 1; i < padding_len; i++ )
848 output[data_len + i - 1] = 0x00;
849 output[output_len - 1] = (unsigned char) padding_len;
850}
851
852static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
853 size_t *data_len )
854{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100855 size_t i, pad_idx;
856 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857
858 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200860
861 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200862 *data_len = input_len - padding_len;
863
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100864 /* Avoid logical || since it results in a branch */
865 bad |= padding_len > input_len;
866 bad |= padding_len == 0;
867
868 /* The number of bytes checked must be independent of padding_len */
869 pad_idx = input_len - padding_len;
870 for( i = 0; i < input_len - 1; i++ )
871 bad |= input[i] * ( i >= pad_idx );
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200878/*
879 * Zero padding: fill with 00 ... 00
880 */
881static void add_zeros_padding( unsigned char *output,
882 size_t output_len, size_t data_len )
883{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200884 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885
886 for( i = data_len; i < output_len; i++ )
887 output[i] = 0x00;
888}
889
890static int get_zeros_padding( unsigned char *input, size_t input_len,
891 size_t *data_len )
892{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100893 size_t i;
894 unsigned char done = 0, prev_done;
895
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200896 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200898
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100899 *data_len = 0;
900 for( i = input_len; i > 0; i-- )
901 {
902 prev_done = done;
903 done |= ( input[i-1] != 0 );
904 *data_len |= i * ( done != prev_done );
905 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200906
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200907 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200910
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200911/*
912 * No padding: don't pad :)
913 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915 * but a trivial get_padding function
916 */
917static int get_no_padding( unsigned char *input, size_t input_len,
918 size_t *data_len )
919{
920 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200922
923 *data_len = input_len;
924
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200925 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200930 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000931{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500932 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000934
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000935#if defined(MBEDTLS_USE_PSA_CRYPTO)
936 if( ctx->psa_enabled == 1 )
937 {
938 /* While PSA Crypto has an API for multipart
939 * operations, we currently don't make it
940 * accessible through the cipher layer. */
941 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
942 }
943#endif /* MBEDTLS_USE_PSA_CRYPTO */
944
Paul Bakker8123e9d2011-01-06 15:37:30 +0000945 *olen = 0;
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100948 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
950 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200951 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100952 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000954 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200955 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000956 }
957
Daniel King8fe47012016-05-17 20:33:28 -0300958 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
959 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300960 {
961 return( 0 );
962 }
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200965 {
966 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200968
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200969 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200970 }
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_CIPHER_MODE_CBC)
973 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000974 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200975 int ret = 0;
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200979 /* check for 'no padding' mode */
980 if( NULL == ctx->add_padding )
981 {
982 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200984
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200985 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200986 }
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000989 ctx->unprocessed_len );
990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000992 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200993 /*
994 * For decrypt operations, expect a full block,
995 * or an empty block if no padding
996 */
997 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001001 }
1002
1003 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001004 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001006 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001007 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001009 }
1010
1011 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001013 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1014 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001015
1016 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001018 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001019 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001020#else
1021 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001025}
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001028int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1029 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001030{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001031 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001034 }
1035
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001036#if defined(MBEDTLS_USE_PSA_CRYPTO)
1037 if( ctx->psa_enabled == 1 )
1038 {
1039 /* While PSA Crypto knows about CBC padding
1040 * schemes, we currently don't make them
1041 * accessible through the cipher layer. */
1042 if( mode != MBEDTLS_PADDING_NONE )
1043 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1044
1045 return( 0 );
1046 }
1047#endif /* MBEDTLS_USE_PSA_CRYPTO */
1048
Paul Bakker1a45d912013-08-14 12:04:26 +02001049 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1052 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001053 ctx->add_padding = add_pkcs_padding;
1054 ctx->get_padding = get_pkcs_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#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1058 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001059 ctx->add_padding = add_one_and_zeros_padding;
1060 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001061 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1064 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001065 ctx->add_padding = add_zeros_and_len_padding;
1066 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001067 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1070 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001071 ctx->add_padding = add_zeros_padding;
1072 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001073 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001076 ctx->add_padding = NULL;
1077 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001078 break;
1079
1080 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001082 }
1083
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001084 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001087
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001088#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001090 unsigned char *tag, size_t tag_len )
1091{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001092 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( MBEDTLS_ENCRYPT != ctx->operation )
1096 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001097
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001098#if defined(MBEDTLS_USE_PSA_CRYPTO)
1099 if( ctx->psa_enabled == 1 )
1100 {
1101 /* While PSA Crypto has an API for multipart
1102 * operations, we currently don't make it
1103 * accessible through the cipher layer. */
1104 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001105 }
1106#endif /* MBEDTLS_USE_PSA_CRYPTO */
1107
Daniel King8fe47012016-05-17 20:33:28 -03001108#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Gilles Peskine5a7be102021-06-23 21:51:32 +02001110 {
1111 size_t output_length;
1112 /* The code here doesn't yet support alternative implementations
1113 * that can delay up to a block of output. */
Hanno Becker18597cd2018-11-09 16:36:33 +00001114 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001115 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001116 tag, tag_len ) );
Gilles Peskine5a7be102021-06-23 21:51:32 +02001117 }
Daniel King8fe47012016-05-17 20:33:28 -03001118#endif
1119
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001120#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001121 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1122 {
1123 /* Don't allow truncated MAC for Poly1305 */
1124 if ( tag_len != 16U )
1125 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1126
Hanno Becker18597cd2018-11-09 16:36:33 +00001127 return( mbedtls_chachapoly_finish(
1128 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001129 }
1130#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001131
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001132 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001133}
Paul Bakker9af723c2014-05-01 13:03:14 +02001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001136 const unsigned char *tag, size_t tag_len )
1137{
Daniel King8fe47012016-05-17 20:33:28 -03001138 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001140
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001141 if( ctx->cipher_info == NULL )
1142 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1143
1144 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001147 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001148
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001149#if defined(MBEDTLS_USE_PSA_CRYPTO)
1150 if( ctx->psa_enabled == 1 )
1151 {
1152 /* While PSA Crypto has an API for multipart
1153 * operations, we currently don't make it
1154 * accessible through the cipher layer. */
1155 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1156 }
1157#endif /* MBEDTLS_USE_PSA_CRYPTO */
1158
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001159 /* Status to return on a non-authenticated algorithm. */
1160 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001161
Daniel King8fe47012016-05-17 20:33:28 -03001162#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001164 {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001165 size_t output_length;
1166 /* The code here doesn't yet support alternative implementations
1167 * that can delay up to a block of output. */
1168
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001169 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001171
Hanno Becker18597cd2018-11-09 16:36:33 +00001172 if( 0 != ( ret = mbedtls_gcm_finish(
1173 (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001174 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001175 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001176 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001177 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001178 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001179
1180 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001181 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinecd742982021-12-13 16:57:47 +01001182 {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001183 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001184 goto exit;
1185 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001186 }
Daniel King8fe47012016-05-17 20:33:28 -03001187#endif /* MBEDTLS_GCM_C */
1188
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001189#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001190 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1191 {
1192 /* Don't allow truncated MAC for Poly1305 */
1193 if ( tag_len != sizeof( check_tag ) )
1194 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1195
Hanno Becker18597cd2018-11-09 16:36:33 +00001196 ret = mbedtls_chachapoly_finish(
1197 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001198 if ( ret != 0 )
1199 {
1200 return( ret );
1201 }
1202
1203 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001204 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinecd742982021-12-13 16:57:47 +01001205 {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001206 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001207 goto exit;
1208 }
Daniel King8fe47012016-05-17 20:33:28 -03001209 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001210#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001211
Gilles Peskinecd742982021-12-13 16:57:47 +01001212exit:
Gilles Peskinee7835d92021-12-13 12:32:43 +01001213 mbedtls_platform_zeroize( check_tag, tag_len );
1214 return( ret );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001215}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001216#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001217
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001218/*
1219 * Packet-oriented wrapper for non-AEAD modes
1220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001222 const unsigned char *iv, size_t iv_len,
1223 const unsigned char *input, size_t ilen,
1224 unsigned char *output, size_t *olen )
1225{
Janos Follath24eed8d2019-11-22 13:21:35 +00001226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001227 size_t finish_olen;
1228
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001229#if defined(MBEDTLS_USE_PSA_CRYPTO)
1230 if( ctx->psa_enabled == 1 )
1231 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001232 /* As in the non-PSA case, we don't check that
1233 * a key has been set. If not, the key slot will
1234 * still be in its default state of 0, which is
1235 * guaranteed to be invalid, hence the PSA-call
1236 * below will gracefully fail. */
1237 mbedtls_cipher_context_psa * const cipher_psa =
1238 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1239
1240 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001241 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001242 size_t part_len;
1243
1244 if( ctx->operation == MBEDTLS_DECRYPT )
1245 {
1246 status = psa_cipher_decrypt_setup( &cipher_op,
1247 cipher_psa->slot,
1248 cipher_psa->alg );
1249 }
1250 else if( ctx->operation == MBEDTLS_ENCRYPT )
1251 {
1252 status = psa_cipher_encrypt_setup( &cipher_op,
1253 cipher_psa->slot,
1254 cipher_psa->alg );
1255 }
1256 else
1257 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1258
1259 /* In the following, we can immediately return on an error,
1260 * because the PSA Crypto API guarantees that cipher operations
1261 * are terminated by unsuccessful calls to psa_cipher_update(),
1262 * and by any call to psa_cipher_finish(). */
1263 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001264 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001265
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001266 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1267 {
1268 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1269 if( status != PSA_SUCCESS )
1270 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1271 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001272
1273 status = psa_cipher_update( &cipher_op,
1274 input, ilen,
1275 output, ilen, olen );
1276 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001277 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001278
1279 status = psa_cipher_finish( &cipher_op,
1280 output + *olen, ilen - *olen,
1281 &part_len );
1282 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001283 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001284
1285 *olen += part_len;
1286 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001287 }
1288#endif /* MBEDTLS_USE_PSA_CRYPTO */
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001291 return( ret );
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001294 return( ret );
1295
Hanno Becker18597cd2018-11-09 16:36:33 +00001296 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1297 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001298 return( ret );
1299
Hanno Becker18597cd2018-11-09 16:36:33 +00001300 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1301 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001302 return( ret );
1303
1304 *olen += finish_olen;
1305
1306 return( 0 );
1307}
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001310/*
TRodziewicz18efb732021-04-29 23:12:19 +02001311 * Packet-oriented encryption for AEAD modes: internal function used by
1312 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001313 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001314static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001315 const unsigned char *iv, size_t iv_len,
1316 const unsigned char *ad, size_t ad_len,
1317 const unsigned char *input, size_t ilen,
1318 unsigned char *output, size_t *olen,
1319 unsigned char *tag, size_t tag_len )
1320{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001321#if defined(MBEDTLS_USE_PSA_CRYPTO)
1322 if( ctx->psa_enabled == 1 )
1323 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001324 /* As in the non-PSA case, we don't check that
1325 * a key has been set. If not, the key slot will
1326 * still be in its default state of 0, which is
1327 * guaranteed to be invalid, hence the PSA-call
1328 * below will gracefully fail. */
1329 mbedtls_cipher_context_psa * const cipher_psa =
1330 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1331
1332 psa_status_t status;
1333
1334 /* PSA Crypto API always writes the authentication tag
1335 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001336 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001337 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1338
1339 status = psa_aead_encrypt( cipher_psa->slot,
1340 cipher_psa->alg,
1341 iv, iv_len,
1342 ad, ad_len,
1343 input, ilen,
1344 output, ilen + tag_len, olen );
1345 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001346 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001347
1348 *olen -= tag_len;
1349 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001350 }
1351#endif /* MBEDTLS_USE_PSA_CRYPTO */
1352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_GCM_C)
1354 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001355 {
1356 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001357 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1358 ilen, iv, iv_len, ad, ad_len,
1359 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_GCM_C */
1362#if defined(MBEDTLS_CCM_C)
1363 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001364 {
1365 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001367 iv, iv_len, ad, ad_len, input, output,
1368 tag, tag_len ) );
1369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001371#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001372 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1373 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001374 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001375 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001376 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001377 {
1378 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1379 }
1380
1381 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001382 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001383 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001384 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001385#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001388}
1389
1390/*
TRodziewicz18efb732021-04-29 23:12:19 +02001391 * Packet-oriented encryption for AEAD modes: internal function used by
1392 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001393 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001394static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001395 const unsigned char *iv, size_t iv_len,
1396 const unsigned char *ad, size_t ad_len,
1397 const unsigned char *input, size_t ilen,
1398 unsigned char *output, size_t *olen,
1399 const unsigned char *tag, size_t tag_len )
1400{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001401#if defined(MBEDTLS_USE_PSA_CRYPTO)
1402 if( ctx->psa_enabled == 1 )
1403 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001404 /* As in the non-PSA case, we don't check that
1405 * a key has been set. If not, the key slot will
1406 * still be in its default state of 0, which is
1407 * guaranteed to be invalid, hence the PSA-call
1408 * below will gracefully fail. */
1409 mbedtls_cipher_context_psa * const cipher_psa =
1410 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1411
1412 psa_status_t status;
1413
1414 /* PSA Crypto API always writes the authentication tag
1415 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001416 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001417 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1418
1419 status = psa_aead_decrypt( cipher_psa->slot,
1420 cipher_psa->alg,
1421 iv, iv_len,
1422 ad, ad_len,
1423 input, ilen + tag_len,
1424 output, ilen, olen );
1425 if( status == PSA_ERROR_INVALID_SIGNATURE )
1426 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1427 else if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001428 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001429
1430 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001431 }
1432#endif /* MBEDTLS_USE_PSA_CRYPTO */
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_GCM_C)
1435 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001436 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001438
1439 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001441 iv, iv_len, ad, ad_len,
1442 tag, tag_len, input, output );
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1445 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001446
1447 return( ret );
1448 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_GCM_C */
1450#if defined(MBEDTLS_CCM_C)
1451 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001452 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001454
1455 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001457 iv, iv_len, ad, ad_len,
1458 input, output, tag, tag_len );
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1461 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001462
1463 return( ret );
1464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001466#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001467 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1468 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001470
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001471 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001472 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001473 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001474 {
1475 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1476 }
1477
1478 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001479 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1480 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001481
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001482 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1483 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001484
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001485 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001486 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001487#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001488
1489 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001492
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001493#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1494/*
1495 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1496 */
1497int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1498 const unsigned char *iv, size_t iv_len,
1499 const unsigned char *ad, size_t ad_len,
1500 const unsigned char *input, size_t ilen,
1501 unsigned char *output, size_t output_len,
1502 size_t *olen, size_t tag_len )
1503{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001504#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001505 if(
1506#if defined(MBEDTLS_USE_PSA_CRYPTO)
1507 ctx->psa_enabled == 0 &&
1508#endif
1509 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1510 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001511 {
1512 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1513 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1514
1515 /* There is no iv, tag or ad associated with KW and KWP,
1516 * so these length should be 0 as documented. */
1517 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1518 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1519
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001520 (void) iv;
1521 (void) ad;
1522
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001523 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1524 output, olen, output_len ) );
1525 }
1526#endif /* MBEDTLS_NIST_KW_C */
1527
1528#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1529 /* AEAD case: check length before passing on to shared function */
1530 if( output_len < ilen + tag_len )
1531 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1532
1533 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1534 input, ilen, output, olen,
1535 output + ilen, tag_len );
1536 *olen += tag_len;
1537 return( ret );
1538#else
1539 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1540#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1541}
1542
1543/*
1544 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1545 */
1546int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1547 const unsigned char *iv, size_t iv_len,
1548 const unsigned char *ad, size_t ad_len,
1549 const unsigned char *input, size_t ilen,
1550 unsigned char *output, size_t output_len,
1551 size_t *olen, size_t tag_len )
1552{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001553#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001554 if(
1555#if defined(MBEDTLS_USE_PSA_CRYPTO)
1556 ctx->psa_enabled == 0 &&
1557#endif
1558 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1559 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001560 {
1561 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1562 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1563
1564 /* There is no iv, tag or ad associated with KW and KWP,
1565 * so these length should be 0 as documented. */
1566 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1567 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1568
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001569 (void) iv;
1570 (void) ad;
1571
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1573 output, olen, output_len ) );
1574 }
1575#endif /* MBEDTLS_NIST_KW_C */
1576
1577#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1578 /* AEAD case: check length before passing on to shared function */
1579 if( ilen < tag_len || output_len < ilen - tag_len )
1580 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1581
1582 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1583 input, ilen - tag_len, output, olen,
1584 input + ilen - tag_len, tag_len ) );
1585#else
1586 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1587#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1588}
1589#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_CIPHER_C */