blob: 67e327458754f128794350aafe029c157326e9f8 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020029#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068#define CIPHER_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
70#define CIPHER_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
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{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200143}
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200146{
147 if( ctx == NULL )
148 return;
149
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000150#if defined(MBEDTLS_USE_PSA_CRYPTO)
151 if( ctx->psa_enabled == 1 )
152 {
Hanno Becker6118e432018-11-09 16:47:20 +0000153 if( ctx->cipher_ctx != NULL )
154 {
155 mbedtls_cipher_context_psa * const cipher_psa =
156 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
157
Hanno Becker19086552018-11-17 22:11:16 +0000158 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000159 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000160 /* xxx_free() doesn't allow to return failures. */
161 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000162 }
163
164 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
165 mbedtls_free( cipher_psa );
166 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000167
168 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
169 return;
170 }
171#endif /* MBEDTLS_USE_PSA_CRYPTO */
172
Simon Butcher327398a2016-10-05 14:09:11 +0100173#if defined(MBEDTLS_CMAC_C)
174 if( ctx->cmac_ctx )
175 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500176 mbedtls_platform_zeroize( ctx->cmac_ctx,
177 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100178 mbedtls_free( ctx->cmac_ctx );
179 }
180#endif
181
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182 if( ctx->cipher_ctx )
183 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
184
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500185 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200186}
187
Hanno Becker18597cd2018-11-09 16:36:33 +0000188int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
189 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 CIPHER_VALIDATE_RET( ctx != NULL );
192 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196
Paul Bakker343a8702011-06-09 14:27:58 +0000197 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
200 ctx->cipher_info = cipher_info;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200203 /*
204 * Ignore possible errors caused by a cipher mode that doesn't use padding
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
207 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200208#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200212
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200213 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214}
215
Hanno Becker4ccfc402018-11-09 16:10:57 +0000216#if defined(MBEDTLS_USE_PSA_CRYPTO)
217int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000218 const mbedtls_cipher_info_t *cipher_info,
219 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000220{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000221 psa_algorithm_t alg;
222 mbedtls_cipher_context_psa *cipher_psa;
223
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000224 if( NULL == cipher_info || NULL == ctx )
225 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
226
Hanno Becker4ee7e762018-11-17 22:00:38 +0000227 /* Check that the underlying cipher mode and cipher type are
228 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000229 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000230 if( alg == 0 )
231 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
232 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000233 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000234
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000235 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
236
Hanno Beckeredda8b82018-11-12 11:59:30 +0000237 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
238 if( cipher_psa == NULL )
239 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
240 cipher_psa->alg = alg;
241 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242 ctx->cipher_info = cipher_info;
243 ctx->psa_enabled = 1;
244 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000245}
246#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{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 CIPHER_VALIDATE_RET( ctx != NULL );
254 CIPHER_VALIDATE_RET( key != NULL );
255 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100256 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000260#if defined(MBEDTLS_USE_PSA_CRYPTO)
261 if( ctx->psa_enabled == 1 )
262 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000263 mbedtls_cipher_context_psa * const cipher_psa =
264 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
265
266 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
267
268 psa_status_t status;
269 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000271
272 /* PSA Crypto API only accepts byte-aligned keys. */
273 if( key_bitlen % 8 != 0 )
274 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
275
276 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000277 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000278 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
279
Andrzej Kurekc7509322019-01-08 09:36:01 -0500280 key_type = mbedtls_psa_translate_cipher_type(
281 ctx->cipher_info->type );
282 if( key_type == 0 )
283 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200284 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000285
286 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500287 * (encrypt vs. decrypt): it is possible to setup a key for encryption
288 * and use it for AEAD decryption. Until tests relying on this
289 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200290 psa_set_key_usage_flags( &attributes,
291 /* mbedtls_psa_translate_cipher_operation( operation ); */
292 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
293 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000294
Gilles Peskined2d45c12019-05-27 14:53:13 +0200295 status = psa_import_key( &attributes, key, key_bytelen,
296 &cipher_psa->slot );
297 switch( status )
298 {
299 case PSA_SUCCESS:
300 break;
301 case PSA_ERROR_INSUFFICIENT_MEMORY:
302 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
303 case PSA_ERROR_NOT_SUPPORTED:
304 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
305 default:
306 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
307 }
308 /* Indicate that we own the key slot and need to
309 * destroy it in mbedtls_cipher_free(). */
310 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000311
312 ctx->key_bitlen = key_bitlen;
313 ctx->operation = operation;
314 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000315 }
316#endif /* MBEDTLS_USE_PSA_CRYPTO */
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200319 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200322 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200323
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200324 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 ctx->operation = operation;
326
Paul Bakker343a8702011-06-09 14:27:58 +0000327 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100328 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( MBEDTLS_ENCRYPT == operation ||
331 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100332 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000334 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500335 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
336 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000337 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
341 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344}
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 const unsigned char *iv,
348 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200350 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352 CIPHER_VALIDATE_RET( ctx != NULL );
353 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
354 if( ctx->cipher_info == NULL )
355 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000356#if defined(MBEDTLS_USE_PSA_CRYPTO)
357 if( ctx->psa_enabled == 1 )
358 {
359 /* While PSA Crypto has an API for multipart
360 * operations, we currently don't make it
361 * accessible through the cipher layer. */
362 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
363 }
364#endif /* MBEDTLS_USE_PSA_CRYPTO */
365
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200366 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
368 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200371 actual_iv_size = iv_len;
372 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200373 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200374 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200375
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200376 /* avoid reading past the end of input buffer */
377 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200379 }
380
Daniel Kingbd920622016-05-15 19:56:20 -0300381#if defined(MBEDTLS_CHACHA20_C)
382 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
383 {
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100384 /* Even though the actual_iv_size is overwritten with a correct value
385 * of 12 from the cipher info, return an error to indicate that
386 * the input iv_len is wrong. */
387 if( iv_len != 12 )
388 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
389
Daniel Kingbd920622016-05-15 19:56:20 -0300390 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
391 iv,
392 0U ) ) /* Initial counter value */
393 {
394 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
395 }
396 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100397#if defined(MBEDTLS_CHACHAPOLY_C)
398 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
399 iv_len != 12 )
400 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
401#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300402#endif
403
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300404 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300405 {
406 memcpy( ctx->iv, iv, actual_iv_size );
407 ctx->iv_size = actual_iv_size;
408 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200409
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200414{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415 CIPHER_VALIDATE_RET( ctx != NULL );
416 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200418
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000419#if defined(MBEDTLS_USE_PSA_CRYPTO)
420 if( ctx->psa_enabled == 1 )
421 {
422 /* We don't support resetting PSA-based
423 * cipher contexts, yet. */
424 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
425 }
426#endif /* MBEDTLS_USE_PSA_CRYPTO */
427
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 ctx->unprocessed_len = 0;
429
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200430 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200431}
432
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200433#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200435 const unsigned char *ad, size_t ad_len )
436{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500437 CIPHER_VALIDATE_RET( ctx != NULL );
438 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
439 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200441
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000442#if defined(MBEDTLS_USE_PSA_CRYPTO)
443 if( ctx->psa_enabled == 1 )
444 {
445 /* While PSA Crypto has an API for multipart
446 * operations, we currently don't make it
447 * accessible through the cipher layer. */
448 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
449 }
450#endif /* MBEDTLS_USE_PSA_CRYPTO */
451
Daniel King8fe47012016-05-17 20:33:28 -0300452#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200454 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500455 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
456 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200457 }
Daniel King8fe47012016-05-17 20:33:28 -0300458#endif
459
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200460#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300461 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
462 {
463 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200464 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300465
466 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200467 ? MBEDTLS_CHACHAPOLY_ENCRYPT
468 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300469
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200470 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300471 ctx->iv,
472 mode );
473 if ( result != 0 )
474 return( result );
475
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
477 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300478 }
479#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200480
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200481 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200483#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200486 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487{
Janos Follath24eed8d2019-11-22 13:21:35 +0000488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 CIPHER_VALIDATE_RET( ctx != NULL );
492 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
493 CIPHER_VALIDATE_RET( output != NULL );
494 CIPHER_VALIDATE_RET( olen != NULL );
495 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000498#if defined(MBEDTLS_USE_PSA_CRYPTO)
499 if( ctx->psa_enabled == 1 )
500 {
501 /* While PSA Crypto has an API for multipart
502 * operations, we currently don't make it
503 * accessible through the cipher layer. */
504 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
505 }
506#endif /* MBEDTLS_USE_PSA_CRYPTO */
507
Paul Bakker6c212762013-12-16 15:24:50 +0100508 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100509 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100510 if ( 0 == block_size )
511 {
512 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
513 }
Paul Bakker6c212762013-12-16 15:24:50 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200516 {
Janos Follath98e28a72016-05-31 14:03:54 +0100517 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200519
520 *olen = ilen;
521
522 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
523 ctx->operation, input, output ) ) )
524 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200525 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200526 }
527
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200528 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200529 }
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_GCM_C)
532 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200533 {
534 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500535 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
536 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200537 }
538#endif
539
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200540#if defined(MBEDTLS_CHACHAPOLY_C)
541 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
542 {
543 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500544 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
545 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200546 }
547#endif
548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100550 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100553 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_CIPHER_MODE_CBC)
556 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000557 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200558 size_t copy_len = 0;
559
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560 /*
561 * If there is not enough data for a full block, cache it.
562 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700563 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000564 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700565 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
566 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000568 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 {
570 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
571 ilen );
572
573 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200574 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 }
576
577 /*
578 * Process cached data first
579 */
Janos Follath98e28a72016-05-31 14:03:54 +0100580 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581 {
Janos Follath98e28a72016-05-31 14:03:54 +0100582 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
584 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
585 copy_len );
586
Paul Bakkerff61a782011-06-09 15:42:02 +0000587 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100588 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000589 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200591 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 }
593
Janos Follath98e28a72016-05-31 14:03:54 +0100594 *olen += block_size;
595 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 ctx->unprocessed_len = 0;
597
598 input += copy_len;
599 ilen -= copy_len;
600 }
601
602 /*
603 * Cache final, incomplete block
604 */
605 if( 0 != ilen )
606 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700607 /* Encryption: only cache partial blocks
608 * Decryption w/ padding: always keep at least one whole block
609 * Decryption w/o padding: only cache partial blocks
610 */
Janos Follath98e28a72016-05-31 14:03:54 +0100611 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700612 if( copy_len == 0 &&
613 ctx->operation == MBEDTLS_DECRYPT &&
614 NULL != ctx->add_padding)
615 {
Janos Follath98e28a72016-05-31 14:03:54 +0100616 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700617 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618
619 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
620 copy_len );
621
622 ctx->unprocessed_len += copy_len;
623 ilen -= copy_len;
624 }
625
626 /*
627 * Process remaining full blocks
628 */
629 if( ilen )
630 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000631 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
632 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200634 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200636
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 *olen += ilen;
638 }
639
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200640 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_CIPHER_MODE_CFB)
645 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000646 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000647 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000648 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000649 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000650 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000652 }
653
654 *olen = ilen;
655
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200656 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000659
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100660#if defined(MBEDTLS_CIPHER_MODE_OFB)
661 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
662 {
663 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
664 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
665 {
666 return( ret );
667 }
668
669 *olen = ilen;
670
671 return( 0 );
672 }
673#endif /* MBEDTLS_CIPHER_MODE_OFB */
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_CIPHER_MODE_CTR)
676 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000677 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000678 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000679 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000680 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000681 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200682 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000683 }
684
685 *olen = ilen;
686
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000690
Jaeden Ameroc6539902018-04-30 17:17:41 +0100691#if defined(MBEDTLS_CIPHER_MODE_XTS)
692 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
693 {
694 if( ctx->unprocessed_len > 0 ) {
695 /* We can only process an entire data unit at a time. */
696 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
697 }
698
699 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
700 ctx->operation, ilen, ctx->iv, input, output );
701 if( ret != 0 )
702 {
703 return( ret );
704 }
705
706 *olen = ilen;
707
708 return( 0 );
709 }
710#endif /* MBEDTLS_CIPHER_MODE_XTS */
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_CIPHER_MODE_STREAM)
713 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200714 {
715 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
716 ilen, input, output ) ) )
717 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200718 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200719 }
720
721 *olen = ilen;
722
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200723 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200724 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728}
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
731#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200732/*
733 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
734 */
Paul Bakker23986e52011-04-24 08:57:21 +0000735static void add_pkcs_padding( unsigned char *output, size_t output_len,
736 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000737{
Paul Bakker23986e52011-04-24 08:57:21 +0000738 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100739 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740
741 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000742 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743}
744
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200745static int get_pkcs_padding( unsigned char *input, size_t input_len,
746 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100748 size_t i, pad_idx;
749 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750
Paul Bakkera885d682011-01-20 16:35:05 +0000751 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753
754 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755 *data_len = input_len - padding_len;
756
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100757 /* Avoid logical || since it results in a branch */
758 bad |= padding_len > input_len;
759 bad |= padding_len == 0;
760
761 /* The number of bytes checked must be independent of padding_len,
762 * so pick input_len, which is usually 8 or 16 (one block) */
763 pad_idx = input_len - padding_len;
764 for( i = 0; i < input_len; i++ )
765 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200772/*
773 * One and zeros padding: fill with 80 00 ... 00
774 */
775static void add_one_and_zeros_padding( unsigned char *output,
776 size_t output_len, size_t data_len )
777{
778 size_t padding_len = output_len - data_len;
779 unsigned char i = 0;
780
781 output[data_len] = 0x80;
782 for( i = 1; i < padding_len; i++ )
783 output[data_len + i] = 0x00;
784}
785
786static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
787 size_t *data_len )
788{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100789 size_t i;
790 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200791
792 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200794
Micha Krausba8316f2017-12-23 23:40:08 +0100795 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100796 *data_len = 0;
797 for( i = input_len; i > 0; i-- )
798 {
799 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100800 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100801 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100802 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100803 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200806
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200811/*
812 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
813 */
814static void add_zeros_and_len_padding( unsigned char *output,
815 size_t output_len, size_t data_len )
816{
817 size_t padding_len = output_len - data_len;
818 unsigned char i = 0;
819
820 for( i = 1; i < padding_len; i++ )
821 output[data_len + i - 1] = 0x00;
822 output[output_len - 1] = (unsigned char) padding_len;
823}
824
825static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
826 size_t *data_len )
827{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100828 size_t i, pad_idx;
829 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200830
831 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200833
834 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200835 *data_len = input_len - padding_len;
836
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100837 /* Avoid logical || since it results in a branch */
838 bad |= padding_len > input_len;
839 bad |= padding_len == 0;
840
841 /* The number of bytes checked must be independent of padding_len */
842 pad_idx = input_len - padding_len;
843 for( i = 0; i < input_len - 1; i++ )
844 bad |= input[i] * ( i >= pad_idx );
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200851/*
852 * Zero padding: fill with 00 ... 00
853 */
854static void add_zeros_padding( unsigned char *output,
855 size_t output_len, size_t data_len )
856{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200857 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200858
859 for( i = data_len; i < output_len; i++ )
860 output[i] = 0x00;
861}
862
863static int get_zeros_padding( unsigned char *input, size_t input_len,
864 size_t *data_len )
865{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100866 size_t i;
867 unsigned char done = 0, prev_done;
868
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200869 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200871
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100872 *data_len = 0;
873 for( i = input_len; i > 0; i-- )
874 {
875 prev_done = done;
876 done |= ( input[i-1] != 0 );
877 *data_len |= i * ( done != prev_done );
878 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200879
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200880 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200883
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200884/*
885 * No padding: don't pad :)
886 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200888 * but a trivial get_padding function
889 */
890static int get_no_padding( unsigned char *input, size_t input_len,
891 size_t *data_len )
892{
893 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895
896 *data_len = input_len;
897
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200898 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200903 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000904{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500905 CIPHER_VALIDATE_RET( ctx != NULL );
906 CIPHER_VALIDATE_RET( output != NULL );
907 CIPHER_VALIDATE_RET( olen != NULL );
908 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000910
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000911#if defined(MBEDTLS_USE_PSA_CRYPTO)
912 if( ctx->psa_enabled == 1 )
913 {
914 /* While PSA Crypto has an API for multipart
915 * operations, we currently don't make it
916 * accessible through the cipher layer. */
917 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
918 }
919#endif /* MBEDTLS_USE_PSA_CRYPTO */
920
Paul Bakker8123e9d2011-01-06 15:37:30 +0000921 *olen = 0;
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100924 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
926 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100927 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000929 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200930 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000931 }
932
Daniel King8fe47012016-05-17 20:33:28 -0300933 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
934 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300935 {
936 return( 0 );
937 }
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200940 {
941 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200943
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200944 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200945 }
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_CIPHER_MODE_CBC)
948 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000949 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200950 int ret = 0;
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000953 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200954 /* check for 'no padding' mode */
955 if( NULL == ctx->add_padding )
956 {
957 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200959
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200960 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200961 }
962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000964 ctx->unprocessed_len );
965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000967 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200968 /*
969 * For decrypt operations, expect a full block,
970 * or an empty block if no padding
971 */
972 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200973 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000976 }
977
978 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000979 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000981 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000982 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200983 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000984 }
985
986 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500988 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
989 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000990
991 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200993 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000994 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200995#else
996 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001000}
1001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001003int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1004 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001005{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001006 CIPHER_VALIDATE_RET( ctx != NULL );
1007
1008 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001011 }
1012
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001013#if defined(MBEDTLS_USE_PSA_CRYPTO)
1014 if( ctx->psa_enabled == 1 )
1015 {
1016 /* While PSA Crypto knows about CBC padding
1017 * schemes, we currently don't make them
1018 * accessible through the cipher layer. */
1019 if( mode != MBEDTLS_PADDING_NONE )
1020 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1021
1022 return( 0 );
1023 }
1024#endif /* MBEDTLS_USE_PSA_CRYPTO */
1025
Paul Bakker1a45d912013-08-14 12:04:26 +02001026 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1029 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001030 ctx->add_padding = add_pkcs_padding;
1031 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001032 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1035 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001036 ctx->add_padding = add_one_and_zeros_padding;
1037 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001038 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1041 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001042 ctx->add_padding = add_zeros_and_len_padding;
1043 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001044 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1047 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001048 ctx->add_padding = add_zeros_padding;
1049 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001050 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001053 ctx->add_padding = NULL;
1054 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001055 break;
1056
1057 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001059 }
1060
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001061 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001064
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001065#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001067 unsigned char *tag, size_t tag_len )
1068{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001069 CIPHER_VALIDATE_RET( ctx != NULL );
1070 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1071 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 if( MBEDTLS_ENCRYPT != ctx->operation )
1075 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001076
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001077#if defined(MBEDTLS_USE_PSA_CRYPTO)
1078 if( ctx->psa_enabled == 1 )
1079 {
1080 /* While PSA Crypto has an API for multipart
1081 * operations, we currently don't make it
1082 * accessible through the cipher layer. */
1083 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001084 }
1085#endif /* MBEDTLS_USE_PSA_CRYPTO */
1086
Daniel King8fe47012016-05-17 20:33:28 -03001087#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001089 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1090 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001091#endif
1092
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001093#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001094 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1095 {
1096 /* Don't allow truncated MAC for Poly1305 */
1097 if ( tag_len != 16U )
1098 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1099
Hanno Becker18597cd2018-11-09 16:36:33 +00001100 return( mbedtls_chachapoly_finish(
1101 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001102 }
1103#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001104
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001105 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001106}
Paul Bakker9af723c2014-05-01 13:03:14 +02001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001109 const unsigned char *tag, size_t tag_len )
1110{
Daniel King8fe47012016-05-17 20:33:28 -03001111 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001113
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001114 CIPHER_VALIDATE_RET( ctx != NULL );
1115 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1116 if( ctx->cipher_info == NULL )
1117 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1118
1119 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001122 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001123
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001124#if defined(MBEDTLS_USE_PSA_CRYPTO)
1125 if( ctx->psa_enabled == 1 )
1126 {
1127 /* While PSA Crypto has an API for multipart
1128 * operations, we currently don't make it
1129 * accessible through the cipher layer. */
1130 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1131 }
1132#endif /* MBEDTLS_USE_PSA_CRYPTO */
1133
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001134 /* Status to return on a non-authenticated algorithm. It would make sense
1135 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1136 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1137 * unit tests assume 0. */
1138 ret = 0;
1139
Daniel King8fe47012016-05-17 20:33:28 -03001140#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001142 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001143 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001145
Hanno Becker18597cd2018-11-09 16:36:33 +00001146 if( 0 != ( ret = mbedtls_gcm_finish(
1147 (mbedtls_gcm_context *) ctx->cipher_ctx,
1148 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001149 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001150 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001151 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152
1153 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001154 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinef9a05012021-12-13 16:57:47 +01001155 {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001156 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001157 goto exit;
1158 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001159 }
Daniel King8fe47012016-05-17 20:33:28 -03001160#endif /* MBEDTLS_GCM_C */
1161
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001162#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001163 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1164 {
1165 /* Don't allow truncated MAC for Poly1305 */
1166 if ( tag_len != sizeof( check_tag ) )
1167 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1168
Hanno Becker18597cd2018-11-09 16:36:33 +00001169 ret = mbedtls_chachapoly_finish(
1170 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001171 if ( ret != 0 )
1172 {
1173 return( ret );
1174 }
1175
1176 /* Check the tag in "constant-time" */
Gabor Mezei18a44942021-10-20 11:59:27 +02001177 if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskinef9a05012021-12-13 16:57:47 +01001178 {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001179 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001180 goto exit;
1181 }
Daniel King8fe47012016-05-17 20:33:28 -03001182 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001183#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001184
Gilles Peskinef9a05012021-12-13 16:57:47 +01001185exit:
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001186 mbedtls_platform_zeroize( check_tag, tag_len );
1187 return( ret );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001189#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001190
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001191/*
1192 * Packet-oriented wrapper for non-AEAD modes
1193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001195 const unsigned char *iv, size_t iv_len,
1196 const unsigned char *input, size_t ilen,
1197 unsigned char *output, size_t *olen )
1198{
Janos Follath24eed8d2019-11-22 13:21:35 +00001199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001200 size_t finish_olen;
1201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001202 CIPHER_VALIDATE_RET( ctx != NULL );
1203 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1204 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1205 CIPHER_VALIDATE_RET( output != NULL );
1206 CIPHER_VALIDATE_RET( olen != NULL );
1207
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001208#if defined(MBEDTLS_USE_PSA_CRYPTO)
1209 if( ctx->psa_enabled == 1 )
1210 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001211 /* As in the non-PSA case, we don't check that
1212 * a key has been set. If not, the key slot will
1213 * still be in its default state of 0, which is
1214 * guaranteed to be invalid, hence the PSA-call
1215 * below will gracefully fail. */
1216 mbedtls_cipher_context_psa * const cipher_psa =
1217 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1218
1219 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001220 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001221 size_t part_len;
1222
1223 if( ctx->operation == MBEDTLS_DECRYPT )
1224 {
1225 status = psa_cipher_decrypt_setup( &cipher_op,
1226 cipher_psa->slot,
1227 cipher_psa->alg );
1228 }
1229 else if( ctx->operation == MBEDTLS_ENCRYPT )
1230 {
1231 status = psa_cipher_encrypt_setup( &cipher_op,
1232 cipher_psa->slot,
1233 cipher_psa->alg );
1234 }
1235 else
1236 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1237
1238 /* In the following, we can immediately return on an error,
1239 * because the PSA Crypto API guarantees that cipher operations
1240 * are terminated by unsuccessful calls to psa_cipher_update(),
1241 * and by any call to psa_cipher_finish(). */
1242 if( status != PSA_SUCCESS )
1243 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1244
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001245 if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
1246 {
1247 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1248 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6c0ec0e2021-09-30 15:51:05 +02001249 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001250 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001251
1252 status = psa_cipher_update( &cipher_op,
1253 input, ilen,
1254 output, ilen, olen );
1255 if( status != PSA_SUCCESS )
1256 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1257
1258 status = psa_cipher_finish( &cipher_op,
1259 output + *olen, ilen - *olen,
1260 &part_len );
1261 if( status != PSA_SUCCESS )
1262 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1263
1264 *olen += part_len;
1265 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001266 }
1267#endif /* MBEDTLS_USE_PSA_CRYPTO */
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001270 return( ret );
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001273 return( ret );
1274
Hanno Becker18597cd2018-11-09 16:36:33 +00001275 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1276 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001277 return( ret );
1278
Hanno Becker18597cd2018-11-09 16:36:33 +00001279 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1280 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001281 return( ret );
1282
1283 *olen += finish_olen;
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001289/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001290 * Packet-oriented encryption for AEAD modes: internal function shared by
1291 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001292 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001293static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001294 const unsigned char *iv, size_t iv_len,
1295 const unsigned char *ad, size_t ad_len,
1296 const unsigned char *input, size_t ilen,
1297 unsigned char *output, size_t *olen,
1298 unsigned char *tag, size_t tag_len )
1299{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001300#if defined(MBEDTLS_USE_PSA_CRYPTO)
1301 if( ctx->psa_enabled == 1 )
1302 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001303 /* As in the non-PSA case, we don't check that
1304 * a key has been set. If not, the key slot will
1305 * still be in its default state of 0, which is
1306 * guaranteed to be invalid, hence the PSA-call
1307 * below will gracefully fail. */
1308 mbedtls_cipher_context_psa * const cipher_psa =
1309 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1310
1311 psa_status_t status;
1312
1313 /* PSA Crypto API always writes the authentication tag
1314 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001315 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001316 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1317
1318 status = psa_aead_encrypt( cipher_psa->slot,
1319 cipher_psa->alg,
1320 iv, iv_len,
1321 ad, ad_len,
1322 input, ilen,
1323 output, ilen + tag_len, olen );
1324 if( status != PSA_SUCCESS )
1325 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1326
1327 *olen -= tag_len;
1328 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001329 }
1330#endif /* MBEDTLS_USE_PSA_CRYPTO */
1331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_GCM_C)
1333 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001334 {
1335 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001336 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1337 ilen, iv, iv_len, ad, ad_len,
1338 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_GCM_C */
1341#if defined(MBEDTLS_CCM_C)
1342 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001343 {
1344 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001346 iv, iv_len, ad, ad_len, input, output,
1347 tag, tag_len ) );
1348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001350#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001351 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1352 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001353 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001354 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001355 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001356 {
1357 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1358 }
1359
1360 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001361 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001362 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001363 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001364#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001367}
1368
1369/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001370 * Packet-oriented encryption for AEAD modes: internal function shared by
1371 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001372 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001373static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001374 const unsigned char *iv, size_t iv_len,
1375 const unsigned char *ad, size_t ad_len,
1376 const unsigned char *input, size_t ilen,
1377 unsigned char *output, size_t *olen,
1378 const unsigned char *tag, size_t tag_len )
1379{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001380#if defined(MBEDTLS_USE_PSA_CRYPTO)
1381 if( ctx->psa_enabled == 1 )
1382 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001383 /* As in the non-PSA case, we don't check that
1384 * a key has been set. If not, the key slot will
1385 * still be in its default state of 0, which is
1386 * guaranteed to be invalid, hence the PSA-call
1387 * below will gracefully fail. */
1388 mbedtls_cipher_context_psa * const cipher_psa =
1389 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1390
1391 psa_status_t status;
1392
1393 /* PSA Crypto API always writes the authentication tag
1394 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001395 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001396 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1397
1398 status = psa_aead_decrypt( cipher_psa->slot,
1399 cipher_psa->alg,
1400 iv, iv_len,
1401 ad, ad_len,
1402 input, ilen + tag_len,
1403 output, ilen, olen );
1404 if( status == PSA_ERROR_INVALID_SIGNATURE )
1405 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1406 else if( status != PSA_SUCCESS )
1407 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1408
1409 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001410 }
1411#endif /* MBEDTLS_USE_PSA_CRYPTO */
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_GCM_C)
1414 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001415 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001417
1418 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001420 iv, iv_len, ad, ad_len,
1421 tag, tag_len, input, output );
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1424 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001425
1426 return( ret );
1427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_GCM_C */
1429#if defined(MBEDTLS_CCM_C)
1430 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001431 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001433
1434 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001436 iv, iv_len, ad, ad_len,
1437 input, output, tag, tag_len );
1438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1440 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001441
1442 return( ret );
1443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001445#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001446 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1447 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001449
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001450 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001451 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001452 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001453 {
1454 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1455 }
1456
1457 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001458 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1459 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001460
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001461 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1462 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001463
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001464 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001466#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001467
1468 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1469}
1470
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001471#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001472/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001473 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001474 */
1475int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1476 const unsigned char *iv, size_t iv_len,
1477 const unsigned char *ad, size_t ad_len,
1478 const unsigned char *input, size_t ilen,
1479 unsigned char *output, size_t *olen,
1480 unsigned char *tag, size_t tag_len )
1481{
1482 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001483 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001484 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1485 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001486 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001487 CIPHER_VALIDATE_RET( olen != NULL );
1488 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1489
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001490 return( mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1491 input, ilen, output, olen,
1492 tag, tag_len ) );
1493}
1494
1495/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001496 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001497 */
1498int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1499 const unsigned char *iv, size_t iv_len,
1500 const unsigned char *ad, size_t ad_len,
1501 const unsigned char *input, size_t ilen,
1502 unsigned char *output, size_t *olen,
1503 const unsigned char *tag, size_t tag_len )
1504{
1505 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001506 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001507 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1508 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001509 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001510 CIPHER_VALIDATE_RET( olen != NULL );
1511 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1512
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001513 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1514 input, ilen, output, olen,
1515 tag, tag_len ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001517#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001519
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001520#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1521/*
1522 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1523 */
1524int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1525 const unsigned char *iv, size_t iv_len,
1526 const unsigned char *ad, size_t ad_len,
1527 const unsigned char *input, size_t ilen,
1528 unsigned char *output, size_t output_len,
1529 size_t *olen, size_t tag_len )
1530{
1531 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001532 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001533 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1534 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1535 CIPHER_VALIDATE_RET( output != NULL );
1536 CIPHER_VALIDATE_RET( olen != NULL );
1537
1538#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001539 if(
1540#if defined(MBEDTLS_USE_PSA_CRYPTO)
1541 ctx->psa_enabled == 0 &&
1542#endif
1543 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1544 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001545 {
1546 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1547 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1548
1549 /* There is no iv, tag or ad associated with KW and KWP,
1550 * so these length should be 0 as documented. */
1551 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1552 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1553
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001554 (void) iv;
1555 (void) ad;
1556
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001557 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1558 output, olen, output_len ) );
1559 }
1560#endif /* MBEDTLS_NIST_KW_C */
1561
1562#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1563 /* AEAD case: check length before passing on to shared function */
1564 if( output_len < ilen + tag_len )
1565 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1566
1567 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1568 input, ilen, output, olen,
1569 output + ilen, tag_len );
1570 *olen += tag_len;
1571 return( ret );
1572#else
1573 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1574#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1575}
1576
1577/*
1578 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1579 */
1580int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1581 const unsigned char *iv, size_t iv_len,
1582 const unsigned char *ad, size_t ad_len,
1583 const unsigned char *input, size_t ilen,
1584 unsigned char *output, size_t output_len,
1585 size_t *olen, size_t tag_len )
1586{
1587 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001588 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1590 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1591 CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
1592 CIPHER_VALIDATE_RET( olen != NULL );
1593
1594#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001595 if(
1596#if defined(MBEDTLS_USE_PSA_CRYPTO)
1597 ctx->psa_enabled == 0 &&
1598#endif
1599 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1600 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001601 {
1602 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1603 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1604
1605 /* There is no iv, tag or ad associated with KW and KWP,
1606 * so these length should be 0 as documented. */
1607 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1608 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1609
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001610 (void) iv;
1611 (void) ad;
1612
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001613 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1614 output, olen, output_len ) );
1615 }
1616#endif /* MBEDTLS_NIST_KW_C */
1617
1618#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1619 /* AEAD case: check length before passing on to shared function */
1620 if( ilen < tag_len || output_len < ilen - tag_len )
1621 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1622
1623 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1624 input, ilen - tag_len, output, olen,
1625 input + ilen - tag_len, tag_len ) );
1626#else
1627 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1628#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1629}
1630#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#endif /* MBEDTLS_CIPHER_C */