blob: 2f2e03ba1855abbc7e042ba9e5d5f8f24351149f [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#if defined(MBEDTLS_PLATFORM_C)
67#include "mbedtls/platform.h"
68#else
69#define mbedtls_calloc calloc
70#define mbedtls_free free
71#endif
72
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078 int *type;
79
80 if( ! supported_init )
81 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
85 while( def->type != 0 )
86 *type++ = (*def++).type;
87
88 *type = 0;
89
90 supported_init = 1;
91 }
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000094}
95
Hanno Becker18597cd2018-11-09 16:36:33 +000096const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
97 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200102 if( def->type == cipher_type )
103 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000104
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200105 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106}
107
Hanno Becker18597cd2018-11-09 16:36:33 +0000108const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
109 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200112
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200114 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200117 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200118 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000119
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200120 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121}
122
Hanno Becker18597cd2018-11-09 16:36:33 +0000123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
124 const mbedtls_cipher_id_t cipher_id,
125 int key_bitlen,
126 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200131 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200132 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200133 def->info->mode == mode )
134 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200142}
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200145{
146 if( ctx == NULL )
147 return;
148
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000149#if defined(MBEDTLS_USE_PSA_CRYPTO)
150 if( ctx->psa_enabled == 1 )
151 {
Hanno Becker6118e432018-11-09 16:47:20 +0000152 if( ctx->cipher_ctx != NULL )
153 {
154 mbedtls_cipher_context_psa * const cipher_psa =
155 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
156
Hanno Becker19086552018-11-17 22:11:16 +0000157 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000158 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000159 /* xxx_free() doesn't allow to return failures. */
160 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000161 }
162
163 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
164 mbedtls_free( cipher_psa );
165 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000166
167 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
168 return;
169 }
170#endif /* MBEDTLS_USE_PSA_CRYPTO */
171
Simon Butcher327398a2016-10-05 14:09:11 +0100172#if defined(MBEDTLS_CMAC_C)
173 if( ctx->cmac_ctx )
174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500175 mbedtls_platform_zeroize( ctx->cmac_ctx,
176 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100177 mbedtls_free( ctx->cmac_ctx );
178 }
179#endif
180
Paul Bakker84bbeb52014-07-01 14:53:22 +0200181 if( ctx->cipher_ctx )
182 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
183
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500184 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185}
186
Hanno Becker18597cd2018-11-09 16:36:33 +0000187int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
188 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194
Paul Bakker343a8702011-06-09 14:27:58 +0000195 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197
198 ctx->cipher_info = cipher_info;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200201 /*
202 * Ignore possible errors caused by a cipher mode that doesn't use padding
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
205 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200206#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200210
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200211 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212}
213
Hanno Becker4ccfc402018-11-09 16:10:57 +0000214#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200215#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000216int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000217 const mbedtls_cipher_info_t *cipher_info,
218 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000219{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000220 psa_algorithm_t alg;
221 mbedtls_cipher_context_psa *cipher_psa;
222
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000223 if( NULL == cipher_info || NULL == ctx )
224 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
225
Hanno Becker4ee7e762018-11-17 22:00:38 +0000226 /* Check that the underlying cipher mode and cipher type are
227 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000228 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000229 if( alg == 0 )
230 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
231 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000232 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000233
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000234 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
235
Hanno Beckeredda8b82018-11-12 11:59:30 +0000236 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
237 if( cipher_psa == NULL )
238 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
239 cipher_psa->alg = alg;
240 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000241 ctx->cipher_info = cipher_info;
242 ctx->psa_enabled = 1;
243 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000244}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200245#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000246#endif /* MBEDTLS_USE_PSA_CRYPTO */
247
Hanno Becker18597cd2018-11-09 16:36:33 +0000248int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
249 const unsigned char *key,
250 int key_bitlen,
251 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252{
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100253 if( operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT )
254 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000258#if defined(MBEDTLS_USE_PSA_CRYPTO)
259 if( ctx->psa_enabled == 1 )
260 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000261 mbedtls_cipher_context_psa * const cipher_psa =
262 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
263
264 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
265
266 psa_status_t status;
267 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000269
270 /* PSA Crypto API only accepts byte-aligned keys. */
271 if( key_bitlen % 8 != 0 )
272 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
273
274 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000275 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
277
Andrzej Kurekc7509322019-01-08 09:36:01 -0500278 key_type = mbedtls_psa_translate_cipher_type(
279 ctx->cipher_info->type );
280 if( key_type == 0 )
281 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200282 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000283
284 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500285 * (encrypt vs. decrypt): it is possible to setup a key for encryption
286 * and use it for AEAD decryption. Until tests relying on this
287 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200288 psa_set_key_usage_flags( &attributes,
289 /* mbedtls_psa_translate_cipher_operation( operation ); */
290 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
291 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000292
Gilles Peskined2d45c12019-05-27 14:53:13 +0200293 status = psa_import_key( &attributes, key, key_bytelen,
294 &cipher_psa->slot );
295 switch( status )
296 {
297 case PSA_SUCCESS:
298 break;
299 case PSA_ERROR_INSUFFICIENT_MEMORY:
300 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
301 case PSA_ERROR_NOT_SUPPORTED:
302 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
303 default:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200304 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200305 }
306 /* Indicate that we own the key slot and need to
307 * destroy it in mbedtls_cipher_free(). */
308 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000309
310 ctx->key_bitlen = key_bitlen;
311 ctx->operation = operation;
312 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000313 }
314#endif /* MBEDTLS_USE_PSA_CRYPTO */
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200317 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200320 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200321
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200322 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 ctx->operation = operation;
324
Paul Bakker343a8702011-06-09 14:27:58 +0000325 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100326 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( MBEDTLS_ENCRYPT == operation ||
329 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100330 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000332 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
334 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000335 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
339 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342}
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 const unsigned char *iv,
346 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200348 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500350 if( ctx->cipher_info == NULL )
351 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000352#if defined(MBEDTLS_USE_PSA_CRYPTO)
353 if( ctx->psa_enabled == 1 )
354 {
355 /* While PSA Crypto has an API for multipart
356 * operations, we currently don't make it
357 * accessible through the cipher layer. */
358 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
359 }
360#endif /* MBEDTLS_USE_PSA_CRYPTO */
361
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200362 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
364 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200367 actual_iv_size = iv_len;
368 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200369 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200370 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200371
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200372 /* avoid reading past the end of input buffer */
373 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200375 }
376
Daniel Kingbd920622016-05-15 19:56:20 -0300377#if defined(MBEDTLS_CHACHA20_C)
378 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
379 {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100380 /* Even though the actual_iv_size is overwritten with a correct value
381 * of 12 from the cipher info, return an error to indicate that
382 * the input iv_len is wrong. */
383 if( iv_len != 12 )
384 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
385
Daniel Kingbd920622016-05-15 19:56:20 -0300386 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
387 iv,
388 0U ) ) /* Initial counter value */
389 {
390 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
391 }
392 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100393#if defined(MBEDTLS_CHACHAPOLY_C)
394 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
395 iv_len != 12 )
396 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
397#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300398#endif
399
Gilles Peskine295fc132021-04-15 18:32:23 +0200400#if defined(MBEDTLS_GCM_C)
401 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
402 {
403 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx,
404 ctx->operation,
405 iv, iv_len ) );
406 }
407#endif
408
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200409#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200410 if( MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200411 {
412 int set_lengths_result;
413 int ccm_star_mode;
414
415 set_lengths_result = mbedtls_ccm_set_lengths(
416 (mbedtls_ccm_context *) ctx->cipher_ctx,
417 0, 0, 0 );
418 if( set_lengths_result != 0 )
419 return set_lengths_result;
420
421 if( ctx->operation == MBEDTLS_DECRYPT )
422 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
423 else if( ctx->operation == MBEDTLS_ENCRYPT )
424 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
425 else
426 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
427
428 return( mbedtls_ccm_starts( (mbedtls_ccm_context *) ctx->cipher_ctx,
429 ccm_star_mode,
430 iv, iv_len ) );
431 }
432#endif
433
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300434 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300435 {
436 memcpy( ctx->iv, iv, actual_iv_size );
437 ctx->iv_size = actual_iv_size;
438 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200439
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200440 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200441}
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200444{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200447
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000448#if defined(MBEDTLS_USE_PSA_CRYPTO)
449 if( ctx->psa_enabled == 1 )
450 {
451 /* We don't support resetting PSA-based
452 * cipher contexts, yet. */
453 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
454 }
455#endif /* MBEDTLS_USE_PSA_CRYPTO */
456
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457 ctx->unprocessed_len = 0;
458
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200459 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200460}
461
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200462#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200464 const unsigned char *ad, size_t ad_len )
465{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200468
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000469#if defined(MBEDTLS_USE_PSA_CRYPTO)
470 if( ctx->psa_enabled == 1 )
471 {
472 /* While PSA Crypto has an API for multipart
473 * operations, we currently don't make it
474 * accessible through the cipher layer. */
475 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
476 }
477#endif /* MBEDTLS_USE_PSA_CRYPTO */
478
Daniel King8fe47012016-05-17 20:33:28 -0300479#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200481 {
Gilles Peskine295fc132021-04-15 18:32:23 +0200482 return( mbedtls_gcm_update_ad( (mbedtls_gcm_context *) ctx->cipher_ctx,
483 ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200484 }
Daniel King8fe47012016-05-17 20:33:28 -0300485#endif
486
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200487#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300488 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
489 {
490 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200491 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300492
493 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494 ? MBEDTLS_CHACHAPOLY_ENCRYPT
495 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300496
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200497 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300498 ctx->iv,
499 mode );
500 if ( result != 0 )
501 return( result );
502
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
504 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300505 }
506#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200507
Denis V. Lunev2df73ae2018-11-01 12:22:27 +0300508 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200510#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200513 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514{
Janos Follath24eed8d2019-11-22 13:21:35 +0000515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500516 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500518 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000521#if defined(MBEDTLS_USE_PSA_CRYPTO)
522 if( ctx->psa_enabled == 1 )
523 {
524 /* While PSA Crypto has an API for multipart
525 * operations, we currently don't make it
526 * accessible through the cipher layer. */
527 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
528 }
529#endif /* MBEDTLS_USE_PSA_CRYPTO */
530
Paul Bakker6c212762013-12-16 15:24:50 +0100531 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100532 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100533 if ( 0 == block_size )
534 {
535 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
536 }
Paul Bakker6c212762013-12-16 15:24:50 +0100537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200539 {
Janos Follath98e28a72016-05-31 14:03:54 +0100540 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200542
543 *olen = ilen;
544
545 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
546 ctx->operation, input, output ) ) )
547 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200548 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200549 }
550
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200551 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200552 }
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_GCM_C)
555 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200556 {
Gilles Peskinea56c4482021-04-15 17:22:35 +0200557 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx,
558 input, ilen,
559 output, ilen, olen ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200560 }
561#endif
562
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200563#if defined(MBEDTLS_CCM_C)
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200564 if( ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG )
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200565 {
566 return( mbedtls_ccm_update( (mbedtls_ccm_context *) ctx->cipher_ctx,
567 input, ilen,
568 output, ilen, olen ) );
569 }
570#endif
571
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200572#if defined(MBEDTLS_CHACHAPOLY_C)
573 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
574 {
575 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500576 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
577 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200578 }
579#endif
580
Paul Bakker68884e32013-01-07 18:20:04 +0100581 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100582 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100585 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_CIPHER_MODE_CBC)
588 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200590 size_t copy_len = 0;
591
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 /*
593 * If there is not enough data for a full block, cache it.
594 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700595 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000596 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700597 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
598 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000600 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 {
602 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
603 ilen );
604
605 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200606 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607 }
608
609 /*
610 * Process cached data first
611 */
Janos Follath98e28a72016-05-31 14:03:54 +0100612 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613 {
Janos Follath98e28a72016-05-31 14:03:54 +0100614 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615
616 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
617 copy_len );
618
Paul Bakkerff61a782011-06-09 15:42:02 +0000619 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100620 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000621 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200623 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624 }
625
Janos Follath98e28a72016-05-31 14:03:54 +0100626 *olen += block_size;
627 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628 ctx->unprocessed_len = 0;
629
630 input += copy_len;
631 ilen -= copy_len;
632 }
633
634 /*
635 * Cache final, incomplete block
636 */
637 if( 0 != ilen )
638 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700639 /* Encryption: only cache partial blocks
640 * Decryption w/ padding: always keep at least one whole block
641 * Decryption w/o padding: only cache partial blocks
642 */
Janos Follath98e28a72016-05-31 14:03:54 +0100643 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700644 if( copy_len == 0 &&
645 ctx->operation == MBEDTLS_DECRYPT &&
646 NULL != ctx->add_padding)
647 {
Janos Follath98e28a72016-05-31 14:03:54 +0100648 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700649 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
651 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
652 copy_len );
653
654 ctx->unprocessed_len += copy_len;
655 ilen -= copy_len;
656 }
657
658 /*
659 * Process remaining full blocks
660 */
661 if( ilen )
662 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000663 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
664 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000665 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200668
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 *olen += ilen;
670 }
671
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_CIPHER_MODE_CFB)
677 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000678 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000679 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000680 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000681 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000682 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200683 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000684 }
685
686 *olen = ilen;
687
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200688 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000691
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100692#if defined(MBEDTLS_CIPHER_MODE_OFB)
693 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
694 {
695 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
696 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
697 {
698 return( ret );
699 }
700
701 *olen = ilen;
702
703 return( 0 );
704 }
705#endif /* MBEDTLS_CIPHER_MODE_OFB */
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_CIPHER_MODE_CTR)
708 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000709 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000710 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000711 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000712 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000713 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200714 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000715 }
716
717 *olen = ilen;
718
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200719 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000720 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000722
Jaeden Ameroc6539902018-04-30 17:17:41 +0100723#if defined(MBEDTLS_CIPHER_MODE_XTS)
724 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
725 {
726 if( ctx->unprocessed_len > 0 ) {
727 /* We can only process an entire data unit at a time. */
728 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
729 }
730
731 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
732 ctx->operation, ilen, ctx->iv, input, output );
733 if( ret != 0 )
734 {
735 return( ret );
736 }
737
738 *olen = ilen;
739
740 return( 0 );
741 }
742#endif /* MBEDTLS_CIPHER_MODE_XTS */
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_CIPHER_MODE_STREAM)
745 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200746 {
747 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
748 ilen, input, output ) ) )
749 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200750 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200751 }
752
753 *olen = ilen;
754
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200755 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760}
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
763#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200764/*
765 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
766 */
Paul Bakker23986e52011-04-24 08:57:21 +0000767static void add_pkcs_padding( unsigned char *output, size_t output_len,
768 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769{
Paul Bakker23986e52011-04-24 08:57:21 +0000770 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100771 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772
773 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000774 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775}
776
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200777static int get_pkcs_padding( unsigned char *input, size_t input_len,
778 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000779{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100780 size_t i, pad_idx;
781 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782
Paul Bakkera885d682011-01-20 16:35:05 +0000783 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785
786 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787 *data_len = input_len - padding_len;
788
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100789 /* Avoid logical || since it results in a branch */
790 bad |= padding_len > input_len;
791 bad |= padding_len == 0;
792
793 /* The number of bytes checked must be independent of padding_len,
794 * so pick input_len, which is usually 8 or 16 (one block) */
795 pad_idx = input_len - padding_len;
796 for( i = 0; i < input_len; i++ )
797 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804/*
805 * One and zeros padding: fill with 80 00 ... 00
806 */
807static void add_one_and_zeros_padding( unsigned char *output,
808 size_t output_len, size_t data_len )
809{
810 size_t padding_len = output_len - data_len;
811 unsigned char i = 0;
812
813 output[data_len] = 0x80;
814 for( i = 1; i < padding_len; i++ )
815 output[data_len + i] = 0x00;
816}
817
818static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
819 size_t *data_len )
820{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100821 size_t i;
822 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823
824 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
Micha Krausba8316f2017-12-23 23:40:08 +0100827 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100828 *data_len = 0;
829 for( i = input_len; i > 0; i-- )
830 {
831 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100832 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100833 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100834 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100835 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200838
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200843/*
844 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
845 */
846static void add_zeros_and_len_padding( unsigned char *output,
847 size_t output_len, size_t data_len )
848{
849 size_t padding_len = output_len - data_len;
850 unsigned char i = 0;
851
852 for( i = 1; i < padding_len; i++ )
853 output[data_len + i - 1] = 0x00;
854 output[output_len - 1] = (unsigned char) padding_len;
855}
856
857static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
858 size_t *data_len )
859{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100860 size_t i, pad_idx;
861 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200862
863 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é-Gonnard8d4291b2013-07-26 14:55:18 +0200865
866 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200867 *data_len = input_len - padding_len;
868
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100869 /* Avoid logical || since it results in a branch */
870 bad |= padding_len > input_len;
871 bad |= padding_len == 0;
872
873 /* The number of bytes checked must be independent of padding_len */
874 pad_idx = input_len - padding_len;
875 for( i = 0; i < input_len - 1; i++ )
876 bad |= input[i] * ( i >= pad_idx );
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200883/*
884 * Zero padding: fill with 00 ... 00
885 */
886static void add_zeros_padding( unsigned char *output,
887 size_t output_len, size_t data_len )
888{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200889 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200890
891 for( i = data_len; i < output_len; i++ )
892 output[i] = 0x00;
893}
894
895static int get_zeros_padding( unsigned char *input, size_t input_len,
896 size_t *data_len )
897{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100898 size_t i;
899 unsigned char done = 0, prev_done;
900
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200901 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200903
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100904 *data_len = 0;
905 for( i = input_len; i > 0; i-- )
906 {
907 prev_done = done;
908 done |= ( input[i-1] != 0 );
909 *data_len |= i * ( done != prev_done );
910 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200911
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200912 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200915
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200916/*
917 * No padding: don't pad :)
918 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200920 * but a trivial get_padding function
921 */
922static int get_no_padding( unsigned char *input, size_t input_len,
923 size_t *data_len )
924{
925 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200927
928 *data_len = input_len;
929
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200930 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200935 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000936{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500937 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000939
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 if( ctx->psa_enabled == 1 )
942 {
943 /* While PSA Crypto has an API for multipart
944 * operations, we currently don't make it
945 * accessible through the cipher layer. */
946 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
947 }
948#endif /* MBEDTLS_USE_PSA_CRYPTO */
949
Paul Bakker8123e9d2011-01-06 15:37:30 +0000950 *olen = 0;
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100953 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
955 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200956 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100957 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000959 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200960 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000961 }
962
Daniel King8fe47012016-05-17 20:33:28 -0300963 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
964 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300965 {
966 return( 0 );
967 }
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200970 {
971 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200973
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200974 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200975 }
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_CIPHER_MODE_CBC)
978 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000979 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200980 int ret = 0;
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000983 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200984 /* check for 'no padding' mode */
985 if( NULL == ctx->add_padding )
986 {
987 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200989
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200990 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200991 }
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000994 ctx->unprocessed_len );
995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998 /*
999 * For decrypt operations, expect a full block,
1000 * or an empty block if no padding
1001 */
1002 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001006 }
1007
1008 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001009 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001011 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001012 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001013 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014 }
1015
1016 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001018 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1019 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
1021 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001023 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001024 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001025#else
1026 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001030}
1031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001033int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1034 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001035{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001036 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001039 }
1040
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001041#if defined(MBEDTLS_USE_PSA_CRYPTO)
1042 if( ctx->psa_enabled == 1 )
1043 {
1044 /* While PSA Crypto knows about CBC padding
1045 * schemes, we currently don't make them
1046 * accessible through the cipher layer. */
1047 if( mode != MBEDTLS_PADDING_NONE )
1048 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1049
1050 return( 0 );
1051 }
1052#endif /* MBEDTLS_USE_PSA_CRYPTO */
1053
Paul Bakker1a45d912013-08-14 12:04:26 +02001054 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1057 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001058 ctx->add_padding = add_pkcs_padding;
1059 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001060 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1063 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001064 ctx->add_padding = add_one_and_zeros_padding;
1065 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001066 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1069 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001070 ctx->add_padding = add_zeros_and_len_padding;
1071 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001072 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1075 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001076 ctx->add_padding = add_zeros_padding;
1077 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001078 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001081 ctx->add_padding = NULL;
1082 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001083 break;
1084
1085 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001087 }
1088
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001089 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001092
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001093#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001095 unsigned char *tag, size_t tag_len )
1096{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001097 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( MBEDTLS_ENCRYPT != ctx->operation )
1101 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001102
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001103#if defined(MBEDTLS_USE_PSA_CRYPTO)
1104 if( ctx->psa_enabled == 1 )
1105 {
1106 /* While PSA Crypto has an API for multipart
1107 * operations, we currently don't make it
1108 * accessible through the cipher layer. */
1109 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001110 }
1111#endif /* MBEDTLS_USE_PSA_CRYPTO */
1112
Daniel King8fe47012016-05-17 20:33:28 -03001113#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Gilles Peskine5a7be102021-06-23 21:51:32 +02001115 {
1116 size_t output_length;
1117 /* The code here doesn't yet support alternative implementations
1118 * that can delay up to a block of output. */
Hanno Becker18597cd2018-11-09 16:36:33 +00001119 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001120 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001121 tag, tag_len ) );
Gilles Peskine5a7be102021-06-23 21:51:32 +02001122 }
Daniel King8fe47012016-05-17 20:33:28 -03001123#endif
1124
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001125#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001126 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1127 {
1128 /* Don't allow truncated MAC for Poly1305 */
1129 if ( tag_len != 16U )
1130 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1131
Hanno Becker18597cd2018-11-09 16:36:33 +00001132 return( mbedtls_chachapoly_finish(
1133 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001134 }
1135#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001136
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001137 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001138}
Paul Bakker9af723c2014-05-01 13:03:14 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001141 const unsigned char *tag, size_t tag_len )
1142{
Daniel King8fe47012016-05-17 20:33:28 -03001143 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001145
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001146 if( ctx->cipher_info == NULL )
1147 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1148
1149 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001153
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001154#if defined(MBEDTLS_USE_PSA_CRYPTO)
1155 if( ctx->psa_enabled == 1 )
1156 {
1157 /* While PSA Crypto has an API for multipart
1158 * operations, we currently don't make it
1159 * accessible through the cipher layer. */
1160 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1161 }
1162#endif /* MBEDTLS_USE_PSA_CRYPTO */
1163
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001164 /* Status to return on a non-authenticated algorithm. */
1165 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001166
Daniel King8fe47012016-05-17 20:33:28 -03001167#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001169 {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001170 size_t output_length;
1171 /* The code here doesn't yet support alternative implementations
1172 * that can delay up to a block of output. */
1173
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001174 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001176
Hanno Becker18597cd2018-11-09 16:36:33 +00001177 if( 0 != ( ret = mbedtls_gcm_finish(
1178 (mbedtls_gcm_context *) ctx->cipher_ctx,
Gilles Peskine5a7be102021-06-23 21:51:32 +02001179 NULL, 0, &output_length,
Hanno Becker18597cd2018-11-09 16:36:33 +00001180 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001181 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001182 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001183 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001184
1185 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001186 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinecd742982021-12-13 16:57:47 +01001187 {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001188 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001189 goto exit;
1190 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001191 }
Daniel King8fe47012016-05-17 20:33:28 -03001192#endif /* MBEDTLS_GCM_C */
1193
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001194#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001195 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1196 {
1197 /* Don't allow truncated MAC for Poly1305 */
1198 if ( tag_len != sizeof( check_tag ) )
1199 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1200
Hanno Becker18597cd2018-11-09 16:36:33 +00001201 ret = mbedtls_chachapoly_finish(
1202 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001203 if ( ret != 0 )
1204 {
1205 return( ret );
1206 }
1207
1208 /* Check the tag in "constant-time" */
Gabor Mezei90437e32021-10-20 11:59:27 +02001209 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinecd742982021-12-13 16:57:47 +01001210 {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001211 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001212 goto exit;
1213 }
Daniel King8fe47012016-05-17 20:33:28 -03001214 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001215#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001216
Gilles Peskinecd742982021-12-13 16:57:47 +01001217exit:
Gilles Peskinee7835d92021-12-13 12:32:43 +01001218 mbedtls_platform_zeroize( check_tag, tag_len );
1219 return( ret );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001220}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001221#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001222
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001223/*
1224 * Packet-oriented wrapper for non-AEAD modes
1225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001227 const unsigned char *iv, size_t iv_len,
1228 const unsigned char *input, size_t ilen,
1229 unsigned char *output, size_t *olen )
1230{
Janos Follath24eed8d2019-11-22 13:21:35 +00001231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001232 size_t finish_olen;
1233
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001234#if defined(MBEDTLS_USE_PSA_CRYPTO)
1235 if( ctx->psa_enabled == 1 )
1236 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001237 /* As in the non-PSA case, we don't check that
1238 * a key has been set. If not, the key slot will
1239 * still be in its default state of 0, which is
1240 * guaranteed to be invalid, hence the PSA-call
1241 * below will gracefully fail. */
1242 mbedtls_cipher_context_psa * const cipher_psa =
1243 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1244
1245 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001246 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001247 size_t part_len;
1248
1249 if( ctx->operation == MBEDTLS_DECRYPT )
1250 {
1251 status = psa_cipher_decrypt_setup( &cipher_op,
1252 cipher_psa->slot,
1253 cipher_psa->alg );
1254 }
1255 else if( ctx->operation == MBEDTLS_ENCRYPT )
1256 {
1257 status = psa_cipher_encrypt_setup( &cipher_op,
1258 cipher_psa->slot,
1259 cipher_psa->alg );
1260 }
1261 else
1262 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1263
1264 /* In the following, we can immediately return on an error,
1265 * because the PSA Crypto API guarantees that cipher operations
1266 * are terminated by unsuccessful calls to psa_cipher_update(),
1267 * and by any call to psa_cipher_finish(). */
1268 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001269 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001270
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001271 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1272 {
1273 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1274 if( status != PSA_SUCCESS )
1275 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1276 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001277
1278 status = psa_cipher_update( &cipher_op,
1279 input, ilen,
1280 output, ilen, olen );
1281 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001282 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001283
1284 status = psa_cipher_finish( &cipher_op,
1285 output + *olen, ilen - *olen,
1286 &part_len );
1287 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001288 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001289
1290 *olen += part_len;
1291 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001292 }
1293#endif /* MBEDTLS_USE_PSA_CRYPTO */
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001296 return( ret );
1297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001299 return( ret );
1300
Hanno Becker18597cd2018-11-09 16:36:33 +00001301 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1302 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001303 return( ret );
1304
Hanno Becker18597cd2018-11-09 16:36:33 +00001305 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1306 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001307 return( ret );
1308
1309 *olen += finish_olen;
1310
1311 return( 0 );
1312}
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001315/*
TRodziewicz18efb732021-04-29 23:12:19 +02001316 * Packet-oriented encryption for AEAD modes: internal function used by
1317 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001318 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001319static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001320 const unsigned char *iv, size_t iv_len,
1321 const unsigned char *ad, size_t ad_len,
1322 const unsigned char *input, size_t ilen,
1323 unsigned char *output, size_t *olen,
1324 unsigned char *tag, size_t tag_len )
1325{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001326#if defined(MBEDTLS_USE_PSA_CRYPTO)
1327 if( ctx->psa_enabled == 1 )
1328 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001329 /* As in the non-PSA case, we don't check that
1330 * a key has been set. If not, the key slot will
1331 * still be in its default state of 0, which is
1332 * guaranteed to be invalid, hence the PSA-call
1333 * below will gracefully fail. */
1334 mbedtls_cipher_context_psa * const cipher_psa =
1335 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1336
1337 psa_status_t status;
1338
1339 /* PSA Crypto API always writes the authentication tag
1340 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001341 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001342 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1343
1344 status = psa_aead_encrypt( cipher_psa->slot,
1345 cipher_psa->alg,
1346 iv, iv_len,
1347 ad, ad_len,
1348 input, ilen,
1349 output, ilen + tag_len, olen );
1350 if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001351 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001352
1353 *olen -= tag_len;
1354 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001355 }
1356#endif /* MBEDTLS_USE_PSA_CRYPTO */
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_GCM_C)
1359 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001360 {
1361 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001362 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1363 ilen, iv, iv_len, ad, ad_len,
1364 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_GCM_C */
1367#if defined(MBEDTLS_CCM_C)
1368 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001369 {
1370 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001372 iv, iv_len, ad, ad_len, input, output,
1373 tag, tag_len ) );
1374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001376#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001377 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1378 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001379 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001380 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001381 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001382 {
1383 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1384 }
1385
1386 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001387 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001388 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001389 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001390#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001393}
1394
1395/*
TRodziewicz18efb732021-04-29 23:12:19 +02001396 * Packet-oriented encryption for AEAD modes: internal function used by
1397 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001398 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001399static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001400 const unsigned char *iv, size_t iv_len,
1401 const unsigned char *ad, size_t ad_len,
1402 const unsigned char *input, size_t ilen,
1403 unsigned char *output, size_t *olen,
1404 const unsigned char *tag, size_t tag_len )
1405{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001406#if defined(MBEDTLS_USE_PSA_CRYPTO)
1407 if( ctx->psa_enabled == 1 )
1408 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001409 /* As in the non-PSA case, we don't check that
1410 * a key has been set. If not, the key slot will
1411 * still be in its default state of 0, which is
1412 * guaranteed to be invalid, hence the PSA-call
1413 * below will gracefully fail. */
1414 mbedtls_cipher_context_psa * const cipher_psa =
1415 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1416
1417 psa_status_t status;
1418
1419 /* PSA Crypto API always writes the authentication tag
1420 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001421 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001422 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1423
1424 status = psa_aead_decrypt( cipher_psa->slot,
1425 cipher_psa->alg,
1426 iv, iv_len,
1427 ad, ad_len,
1428 input, ilen + tag_len,
1429 output, ilen, olen );
1430 if( status == PSA_ERROR_INVALID_SIGNATURE )
1431 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1432 else if( status != PSA_SUCCESS )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001433 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001434
1435 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001436 }
1437#endif /* MBEDTLS_USE_PSA_CRYPTO */
1438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_GCM_C)
1440 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001441 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001443
1444 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001446 iv, iv_len, ad, ad_len,
1447 tag, tag_len, input, output );
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1450 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001451
1452 return( ret );
1453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_GCM_C */
1455#if defined(MBEDTLS_CCM_C)
1456 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001457 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001459
1460 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001462 iv, iv_len, ad, ad_len,
1463 input, output, tag, tag_len );
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1466 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001467
1468 return( ret );
1469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001471#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001472 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1473 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001475
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001476 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001477 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001478 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001479 {
1480 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1481 }
1482
1483 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001484 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1485 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001486
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001487 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1488 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001489
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001490 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001491 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001492#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001493
1494 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001497
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001498#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1499/*
1500 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1501 */
1502int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1503 const unsigned char *iv, size_t iv_len,
1504 const unsigned char *ad, size_t ad_len,
1505 const unsigned char *input, size_t ilen,
1506 unsigned char *output, size_t output_len,
1507 size_t *olen, size_t tag_len )
1508{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001509#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001510 if(
1511#if defined(MBEDTLS_USE_PSA_CRYPTO)
1512 ctx->psa_enabled == 0 &&
1513#endif
1514 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1515 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001516 {
1517 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1518 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1519
1520 /* There is no iv, tag or ad associated with KW and KWP,
1521 * so these length should be 0 as documented. */
1522 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1523 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1524
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001525 (void) iv;
1526 (void) ad;
1527
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001528 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1529 output, olen, output_len ) );
1530 }
1531#endif /* MBEDTLS_NIST_KW_C */
1532
1533#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1534 /* AEAD case: check length before passing on to shared function */
1535 if( output_len < ilen + tag_len )
1536 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1537
1538 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1539 input, ilen, output, olen,
1540 output + ilen, tag_len );
1541 *olen += tag_len;
1542 return( ret );
1543#else
1544 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1545#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1546}
1547
1548/*
1549 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1550 */
1551int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1552 const unsigned char *iv, size_t iv_len,
1553 const unsigned char *ad, size_t ad_len,
1554 const unsigned char *input, size_t ilen,
1555 unsigned char *output, size_t output_len,
1556 size_t *olen, size_t tag_len )
1557{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001558#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001559 if(
1560#if defined(MBEDTLS_USE_PSA_CRYPTO)
1561 ctx->psa_enabled == 0 &&
1562#endif
1563 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1564 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001565 {
1566 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1567 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1568
1569 /* There is no iv, tag or ad associated with KW and KWP,
1570 * so these length should be 0 as documented. */
1571 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1572 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1573
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001574 (void) iv;
1575 (void) ad;
1576
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001577 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1578 output, olen, output_len ) );
1579 }
1580#endif /* MBEDTLS_NIST_KW_C */
1581
1582#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1583 /* AEAD case: check length before passing on to shared function */
1584 if( ilen < tag_len || output_len < ilen - tag_len )
1585 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1586
1587 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1588 input, ilen - tag_len, output, olen,
1589 input + ilen - tag_len, tag_len ) );
1590#else
1591 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1592#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1593}
1594#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_CIPHER_C */