blob: acbda26b76cc167dd1c0f988befe18520b53e4a2 [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020031#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Hanno Becker4ccfc402018-11-09 16:10:57 +000058#if defined(MBEDTLS_USE_PSA_CRYPTO)
59#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000060#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000061#endif /* MBEDTLS_USE_PSA_CRYPTO */
62
Jack Lloydffdf2882019-03-07 17:00:32 -050063#if defined(MBEDTLS_NIST_KW_C)
64#include "mbedtls/nist_kw.h"
65#endif
66
Simon Butcher327398a2016-10-05 14:09:11 +010067#if defined(MBEDTLS_PLATFORM_C)
68#include "mbedtls/platform.h"
69#else
70#define mbedtls_calloc calloc
71#define mbedtls_free free
72#endif
73
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050074#define CIPHER_VALIDATE_RET( cond ) \
75 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
76#define CIPHER_VALIDATE( cond ) \
77 MBEDTLS_INTERNAL_VALIDATE( cond )
78
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020079#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030080/* Compare the contents of two buffers in constant time.
81 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030082 * a non-zero value.
83 * This is currently only used by GCM and ChaCha20+Poly1305.
84 */
Hanno Becker18597cd2018-11-09 16:36:33 +000085static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
86 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030087{
88 const unsigned char *p1 = (const unsigned char*) v1;
89 const unsigned char *p2 = (const unsigned char*) v2;
90 size_t i;
91 unsigned char diff;
92
93 for( diff = 0, i = 0; i < len; i++ )
94 diff |= p1[i] ^ p2[i];
95
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -030097}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020098#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030099
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200100static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200105 int *type;
106
107 if( ! supported_init )
108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 def = mbedtls_cipher_definitions;
110 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200111
112 while( def->type != 0 )
113 *type++ = (*def++).type;
114
115 *type = 0;
116
117 supported_init = 1;
118 }
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000121}
122
Hanno Becker18597cd2018-11-09 16:36:33 +0000123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
124 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200129 if( def->type == cipher_type )
130 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000131
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200132 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133}
134
Hanno Becker18597cd2018-11-09 16:36:33 +0000135const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
136 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200139
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200141 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200144 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200145 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000146
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200147 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148}
149
Hanno Becker18597cd2018-11-09 16:36:33 +0000150const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
151 const mbedtls_cipher_id_t cipher_id,
152 int key_bitlen,
153 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200158 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200159 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200160 def->info->mode == mode )
161 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200162
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200163 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200167{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200170}
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200173{
174 if( ctx == NULL )
175 return;
176
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000177#if defined(MBEDTLS_USE_PSA_CRYPTO)
178 if( ctx->psa_enabled == 1 )
179 {
Hanno Becker6118e432018-11-09 16:47:20 +0000180 if( ctx->cipher_ctx != NULL )
181 {
182 mbedtls_cipher_context_psa * const cipher_psa =
183 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
184
Hanno Becker19086552018-11-17 22:11:16 +0000185 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000186 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000187 /* xxx_free() doesn't allow to return failures. */
188 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000189 }
190
191 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
192 mbedtls_free( cipher_psa );
193 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000194
195 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
196 return;
197 }
198#endif /* MBEDTLS_USE_PSA_CRYPTO */
199
Simon Butcher327398a2016-10-05 14:09:11 +0100200#if defined(MBEDTLS_CMAC_C)
201 if( ctx->cmac_ctx )
202 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500203 mbedtls_platform_zeroize( ctx->cmac_ctx,
204 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100205 mbedtls_free( ctx->cmac_ctx );
206 }
207#endif
208
Paul Bakker84bbeb52014-07-01 14:53:22 +0200209 if( ctx->cipher_ctx )
210 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
211
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500212 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200213}
214
Hanno Becker18597cd2018-11-09 16:36:33 +0000215int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
216 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 CIPHER_VALIDATE_RET( ctx != NULL );
219 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223
Paul Bakker343a8702011-06-09 14:27:58 +0000224 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
227 ctx->cipher_info = cipher_info;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200230 /*
231 * Ignore possible errors caused by a cipher mode that doesn't use padding
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
234 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200235#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200239
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200240 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241}
242
Hanno Becker4ccfc402018-11-09 16:10:57 +0000243#if defined(MBEDTLS_USE_PSA_CRYPTO)
244int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000245 const mbedtls_cipher_info_t *cipher_info,
246 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000247{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000248 psa_algorithm_t alg;
249 mbedtls_cipher_context_psa *cipher_psa;
250
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000251 if( NULL == cipher_info || NULL == ctx )
252 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
253
Hanno Becker4ee7e762018-11-17 22:00:38 +0000254 /* Check that the underlying cipher mode and cipher type are
255 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000256 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000257 if( alg == 0 )
258 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
259 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000260 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000261
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000262 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
263
Hanno Beckeredda8b82018-11-12 11:59:30 +0000264 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
265 if( cipher_psa == NULL )
266 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
267 cipher_psa->alg = alg;
268 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000269 ctx->cipher_info = cipher_info;
270 ctx->psa_enabled = 1;
271 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000272}
273#endif /* MBEDTLS_USE_PSA_CRYPTO */
274
Hanno Becker18597cd2018-11-09 16:36:33 +0000275int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
276 const unsigned char *key,
277 int key_bitlen,
278 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 CIPHER_VALIDATE_RET( ctx != NULL );
281 CIPHER_VALIDATE_RET( key != NULL );
282 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100283 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000287#if defined(MBEDTLS_USE_PSA_CRYPTO)
288 if( ctx->psa_enabled == 1 )
289 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000290 mbedtls_cipher_context_psa * const cipher_psa =
291 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
292
293 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
294
295 psa_status_t status;
296 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000298
299 /* PSA Crypto API only accepts byte-aligned keys. */
300 if( key_bitlen % 8 != 0 )
301 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
302
303 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000304 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000305 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
306
Andrzej Kurekc7509322019-01-08 09:36:01 -0500307 key_type = mbedtls_psa_translate_cipher_type(
308 ctx->cipher_info->type );
309 if( key_type == 0 )
310 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200311 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000312
313 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500314 * (encrypt vs. decrypt): it is possible to setup a key for encryption
315 * and use it for AEAD decryption. Until tests relying on this
316 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200317 psa_set_key_usage_flags( &attributes,
318 /* mbedtls_psa_translate_cipher_operation( operation ); */
319 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
320 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000321
Gilles Peskined2d45c12019-05-27 14:53:13 +0200322 status = psa_import_key( &attributes, key, key_bytelen,
323 &cipher_psa->slot );
324 switch( status )
325 {
326 case PSA_SUCCESS:
327 break;
328 case PSA_ERROR_INSUFFICIENT_MEMORY:
329 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
330 case PSA_ERROR_NOT_SUPPORTED:
331 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
332 default:
333 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
334 }
335 /* Indicate that we own the key slot and need to
336 * destroy it in mbedtls_cipher_free(). */
337 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000338
339 ctx->key_bitlen = key_bitlen;
340 ctx->operation = operation;
341 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000342 }
343#endif /* MBEDTLS_USE_PSA_CRYPTO */
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200346 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200349 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200350
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200351 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352 ctx->operation = operation;
353
Paul Bakker343a8702011-06-09 14:27:58 +0000354 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100355 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( MBEDTLS_ENCRYPT == operation ||
358 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100359 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000361 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500362 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
363 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000364 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500367 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
368 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371}
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374 const unsigned char *iv,
375 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200377 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500379 CIPHER_VALIDATE_RET( ctx != NULL );
380 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
381 if( ctx->cipher_info == NULL )
382 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000383#if defined(MBEDTLS_USE_PSA_CRYPTO)
384 if( ctx->psa_enabled == 1 )
385 {
386 /* While PSA Crypto has an API for multipart
387 * operations, we currently don't make it
388 * accessible through the cipher layer. */
389 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
390 }
391#endif /* MBEDTLS_USE_PSA_CRYPTO */
392
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200393 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
395 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200398 actual_iv_size = iv_len;
399 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200400 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200401 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200402
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200403 /* avoid reading past the end of input buffer */
404 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200406 }
407
Daniel Kingbd920622016-05-15 19:56:20 -0300408#if defined(MBEDTLS_CHACHA20_C)
409 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
410 {
411 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
412 iv,
413 0U ) ) /* Initial counter value */
414 {
415 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
416 }
417 }
418#endif
419
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300420 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300421 {
422 memcpy( ctx->iv, iv, actual_iv_size );
423 ctx->iv_size = actual_iv_size;
424 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200425
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200430{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500431 CIPHER_VALIDATE_RET( ctx != NULL );
432 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200434
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000435#if defined(MBEDTLS_USE_PSA_CRYPTO)
436 if( ctx->psa_enabled == 1 )
437 {
438 /* We don't support resetting PSA-based
439 * cipher contexts, yet. */
440 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
441 }
442#endif /* MBEDTLS_USE_PSA_CRYPTO */
443
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 ctx->unprocessed_len = 0;
445
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200446 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200447}
448
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200449#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200451 const unsigned char *ad, size_t ad_len )
452{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500453 CIPHER_VALIDATE_RET( ctx != NULL );
454 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
455 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200457
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000458#if defined(MBEDTLS_USE_PSA_CRYPTO)
459 if( ctx->psa_enabled == 1 )
460 {
461 /* While PSA Crypto has an API for multipart
462 * operations, we currently don't make it
463 * accessible through the cipher layer. */
464 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
465 }
466#endif /* MBEDTLS_USE_PSA_CRYPTO */
467
Daniel King8fe47012016-05-17 20:33:28 -0300468#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200470 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500471 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
472 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200473 }
Daniel King8fe47012016-05-17 20:33:28 -0300474#endif
475
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200476#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300477 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
478 {
479 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200480 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300481
482 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200483 ? MBEDTLS_CHACHAPOLY_ENCRYPT
484 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300485
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200486 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300487 ctx->iv,
488 mode );
489 if ( result != 0 )
490 return( result );
491
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
493 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300494 }
495#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200496
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200499#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200502 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503{
Janos Follath24eed8d2019-11-22 13:21:35 +0000504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507 CIPHER_VALIDATE_RET( ctx != NULL );
508 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
509 CIPHER_VALIDATE_RET( output != NULL );
510 CIPHER_VALIDATE_RET( olen != NULL );
511 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000514#if defined(MBEDTLS_USE_PSA_CRYPTO)
515 if( ctx->psa_enabled == 1 )
516 {
517 /* While PSA Crypto has an API for multipart
518 * operations, we currently don't make it
519 * accessible through the cipher layer. */
520 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
521 }
522#endif /* MBEDTLS_USE_PSA_CRYPTO */
523
Paul Bakker6c212762013-12-16 15:24:50 +0100524 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100525 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100526 if ( 0 == block_size )
527 {
528 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
529 }
Paul Bakker6c212762013-12-16 15:24:50 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200532 {
Janos Follath98e28a72016-05-31 14:03:54 +0100533 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200535
536 *olen = ilen;
537
538 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
539 ctx->operation, input, output ) ) )
540 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200541 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200542 }
543
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200544 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200545 }
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_GCM_C)
548 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200549 {
550 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500551 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
552 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200553 }
554#endif
555
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200556#if defined(MBEDTLS_CHACHAPOLY_C)
557 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
558 {
559 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
561 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200562 }
563#endif
564
Paul Bakker68884e32013-01-07 18:20:04 +0100565 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100566 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100569 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_CIPHER_MODE_CBC)
572 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200574 size_t copy_len = 0;
575
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576 /*
577 * If there is not enough data for a full block, cache it.
578 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700579 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000580 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700581 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
582 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000584 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585 {
586 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
587 ilen );
588
589 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 }
592
593 /*
594 * Process cached data first
595 */
Janos Follath98e28a72016-05-31 14:03:54 +0100596 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 {
Janos Follath98e28a72016-05-31 14:03:54 +0100598 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599
600 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
601 copy_len );
602
Paul Bakkerff61a782011-06-09 15:42:02 +0000603 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100604 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000605 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200607 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 }
609
Janos Follath98e28a72016-05-31 14:03:54 +0100610 *olen += block_size;
611 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 ctx->unprocessed_len = 0;
613
614 input += copy_len;
615 ilen -= copy_len;
616 }
617
618 /*
619 * Cache final, incomplete block
620 */
621 if( 0 != ilen )
622 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700623 /* Encryption: only cache partial blocks
624 * Decryption w/ padding: always keep at least one whole block
625 * Decryption w/o padding: only cache partial blocks
626 */
Janos Follath98e28a72016-05-31 14:03:54 +0100627 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700628 if( copy_len == 0 &&
629 ctx->operation == MBEDTLS_DECRYPT &&
630 NULL != ctx->add_padding)
631 {
Janos Follath98e28a72016-05-31 14:03:54 +0100632 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700633 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634
635 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
636 copy_len );
637
638 ctx->unprocessed_len += copy_len;
639 ilen -= copy_len;
640 }
641
642 /*
643 * Process remaining full blocks
644 */
645 if( ilen )
646 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000647 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
648 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200652
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 *olen += ilen;
654 }
655
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200656 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_CIPHER_MODE_CFB)
661 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000662 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000663 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000664 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000665 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000666 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000668 }
669
670 *olen = ilen;
671
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000675
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100676#if defined(MBEDTLS_CIPHER_MODE_OFB)
677 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
678 {
679 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
680 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
681 {
682 return( ret );
683 }
684
685 *olen = ilen;
686
687 return( 0 );
688 }
689#endif /* MBEDTLS_CIPHER_MODE_OFB */
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_MODE_CTR)
692 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000693 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000694 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000695 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000696 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000697 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200698 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000699 }
700
701 *olen = ilen;
702
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200703 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000704 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000706
Jaeden Ameroc6539902018-04-30 17:17:41 +0100707#if defined(MBEDTLS_CIPHER_MODE_XTS)
708 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
709 {
710 if( ctx->unprocessed_len > 0 ) {
711 /* We can only process an entire data unit at a time. */
712 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
713 }
714
715 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
716 ctx->operation, ilen, ctx->iv, input, output );
717 if( ret != 0 )
718 {
719 return( ret );
720 }
721
722 *olen = ilen;
723
724 return( 0 );
725 }
726#endif /* MBEDTLS_CIPHER_MODE_XTS */
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_MODE_STREAM)
729 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200730 {
731 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
732 ilen, input, output ) ) )
733 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200735 }
736
737 *olen = ilen;
738
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200739 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744}
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
747#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200748/*
749 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
750 */
Paul Bakker23986e52011-04-24 08:57:21 +0000751static void add_pkcs_padding( unsigned char *output, size_t output_len,
752 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753{
Paul Bakker23986e52011-04-24 08:57:21 +0000754 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100755 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000756
757 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000758 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000759}
760
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200761static int get_pkcs_padding( unsigned char *input, size_t input_len,
762 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100764 size_t i, pad_idx;
765 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766
Paul Bakkera885d682011-01-20 16:35:05 +0000767 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769
770 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771 *data_len = input_len - padding_len;
772
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100773 /* Avoid logical || since it results in a branch */
774 bad |= padding_len > input_len;
775 bad |= padding_len == 0;
776
777 /* The number of bytes checked must be independent of padding_len,
778 * so pick input_len, which is usually 8 or 16 (one block) */
779 pad_idx = input_len - padding_len;
780 for( i = 0; i < input_len; i++ )
781 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200788/*
789 * One and zeros padding: fill with 80 00 ... 00
790 */
791static void add_one_and_zeros_padding( unsigned char *output,
792 size_t output_len, size_t data_len )
793{
794 size_t padding_len = output_len - data_len;
795 unsigned char i = 0;
796
797 output[data_len] = 0x80;
798 for( i = 1; i < padding_len; i++ )
799 output[data_len + i] = 0x00;
800}
801
802static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
803 size_t *data_len )
804{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100805 size_t i;
806 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200807
808 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810
Micha Krausba8316f2017-12-23 23:40:08 +0100811 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100812 *data_len = 0;
813 for( i = input_len; i > 0; i-- )
814 {
815 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100816 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100817 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100818 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100819 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200822
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200827/*
828 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
829 */
830static void add_zeros_and_len_padding( unsigned char *output,
831 size_t output_len, size_t data_len )
832{
833 size_t padding_len = output_len - data_len;
834 unsigned char i = 0;
835
836 for( i = 1; i < padding_len; i++ )
837 output[data_len + i - 1] = 0x00;
838 output[output_len - 1] = (unsigned char) padding_len;
839}
840
841static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
842 size_t *data_len )
843{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100844 size_t i, pad_idx;
845 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200846
847 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200849
850 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200851 *data_len = input_len - padding_len;
852
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100853 /* Avoid logical || since it results in a branch */
854 bad |= padding_len > input_len;
855 bad |= padding_len == 0;
856
857 /* The number of bytes checked must be independent of padding_len */
858 pad_idx = input_len - padding_len;
859 for( i = 0; i < input_len - 1; i++ )
860 bad |= input[i] * ( i >= pad_idx );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200867/*
868 * Zero padding: fill with 00 ... 00
869 */
870static void add_zeros_padding( unsigned char *output,
871 size_t output_len, size_t data_len )
872{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200873 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200874
875 for( i = data_len; i < output_len; i++ )
876 output[i] = 0x00;
877}
878
879static int get_zeros_padding( unsigned char *input, size_t input_len,
880 size_t *data_len )
881{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100882 size_t i;
883 unsigned char done = 0, prev_done;
884
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100888 *data_len = 0;
889 for( i = input_len; i > 0; i-- )
890 {
891 prev_done = done;
892 done |= ( input[i-1] != 0 );
893 *data_len |= i * ( done != prev_done );
894 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200896 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200899
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200900/*
901 * No padding: don't pad :)
902 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200904 * but a trivial get_padding function
905 */
906static int get_no_padding( unsigned char *input, size_t input_len,
907 size_t *data_len )
908{
909 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200911
912 *data_len = input_len;
913
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200914 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200919 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000920{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500921 CIPHER_VALIDATE_RET( ctx != NULL );
922 CIPHER_VALIDATE_RET( output != NULL );
923 CIPHER_VALIDATE_RET( olen != NULL );
924 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000926
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000927#if defined(MBEDTLS_USE_PSA_CRYPTO)
928 if( ctx->psa_enabled == 1 )
929 {
930 /* While PSA Crypto has an API for multipart
931 * operations, we currently don't make it
932 * accessible through the cipher layer. */
933 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
934 }
935#endif /* MBEDTLS_USE_PSA_CRYPTO */
936
Paul Bakker8123e9d2011-01-06 15:37:30 +0000937 *olen = 0;
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100940 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
942 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100943 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000945 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200946 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000947 }
948
Daniel King8fe47012016-05-17 20:33:28 -0300949 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
950 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300951 {
952 return( 0 );
953 }
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200956 {
957 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200959
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200960 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200961 }
962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_CIPHER_MODE_CBC)
964 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000965 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200966 int ret = 0;
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000969 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200970 /* check for 'no padding' mode */
971 if( NULL == ctx->add_padding )
972 {
973 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200976 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200977 }
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000980 ctx->unprocessed_len );
981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000983 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200984 /*
985 * For decrypt operations, expect a full block,
986 * or an empty block if no padding
987 */
988 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000992 }
993
994 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000995 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000997 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000998 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200999 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001000 }
1001
1002 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001004 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1005 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001006
1007 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001009 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001010 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001011#else
1012 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016}
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001019int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1020 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001021{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001022 CIPHER_VALIDATE_RET( ctx != NULL );
1023
1024 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001027 }
1028
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
1030 if( ctx->psa_enabled == 1 )
1031 {
1032 /* While PSA Crypto knows about CBC padding
1033 * schemes, we currently don't make them
1034 * accessible through the cipher layer. */
1035 if( mode != MBEDTLS_PADDING_NONE )
1036 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1037
1038 return( 0 );
1039 }
1040#endif /* MBEDTLS_USE_PSA_CRYPTO */
1041
Paul Bakker1a45d912013-08-14 12:04:26 +02001042 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1045 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001046 ctx->add_padding = add_pkcs_padding;
1047 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001048 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1051 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001052 ctx->add_padding = add_one_and_zeros_padding;
1053 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001054 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1057 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001058 ctx->add_padding = add_zeros_and_len_padding;
1059 ctx->get_padding = get_zeros_and_len_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_ZEROS)
1063 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001064 ctx->add_padding = add_zeros_padding;
1065 ctx->get_padding = get_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001069 ctx->add_padding = NULL;
1070 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001071 break;
1072
1073 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001075 }
1076
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001077 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001080
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001081#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001083 unsigned char *tag, size_t tag_len )
1084{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001085 CIPHER_VALIDATE_RET( ctx != NULL );
1086 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1087 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( MBEDTLS_ENCRYPT != ctx->operation )
1091 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001092
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001093#if defined(MBEDTLS_USE_PSA_CRYPTO)
1094 if( ctx->psa_enabled == 1 )
1095 {
1096 /* While PSA Crypto has an API for multipart
1097 * operations, we currently don't make it
1098 * accessible through the cipher layer. */
1099 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001100 }
1101#endif /* MBEDTLS_USE_PSA_CRYPTO */
1102
Daniel King8fe47012016-05-17 20:33:28 -03001103#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001105 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1106 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001107#endif
1108
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001109#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001110 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1111 {
1112 /* Don't allow truncated MAC for Poly1305 */
1113 if ( tag_len != 16U )
1114 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1115
Hanno Becker18597cd2018-11-09 16:36:33 +00001116 return( mbedtls_chachapoly_finish(
1117 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001118 }
1119#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001120
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001121 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001122}
Paul Bakker9af723c2014-05-01 13:03:14 +02001123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001125 const unsigned char *tag, size_t tag_len )
1126{
Daniel King8fe47012016-05-17 20:33:28 -03001127 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001129
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001130 CIPHER_VALIDATE_RET( ctx != NULL );
1131 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1132 if( ctx->cipher_info == NULL )
1133 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1134
1135 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001138 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001139
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001140#if defined(MBEDTLS_USE_PSA_CRYPTO)
1141 if( ctx->psa_enabled == 1 )
1142 {
1143 /* While PSA Crypto has an API for multipart
1144 * operations, we currently don't make it
1145 * accessible through the cipher layer. */
1146 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1147 }
1148#endif /* MBEDTLS_USE_PSA_CRYPTO */
1149
Daniel King8fe47012016-05-17 20:33:28 -03001150#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001153 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001155
Hanno Becker18597cd2018-11-09 16:36:33 +00001156 if( 0 != ( ret = mbedtls_gcm_finish(
1157 (mbedtls_gcm_context *) ctx->cipher_ctx,
1158 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001159 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001160 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001161 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001162
1163 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001164 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001167 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001168 }
Daniel King8fe47012016-05-17 20:33:28 -03001169#endif /* MBEDTLS_GCM_C */
1170
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001171#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001172 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1173 {
1174 /* Don't allow truncated MAC for Poly1305 */
1175 if ( tag_len != sizeof( check_tag ) )
1176 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1177
Hanno Becker18597cd2018-11-09 16:36:33 +00001178 ret = mbedtls_chachapoly_finish(
1179 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001180 if ( ret != 0 )
1181 {
1182 return( ret );
1183 }
1184
1185 /* Check the tag in "constant-time" */
1186 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1187 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1188
1189 return( 0 );
1190 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001191#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001192
1193 return( 0 );
1194}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001195#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001196
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001197/*
1198 * Packet-oriented wrapper for non-AEAD modes
1199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001201 const unsigned char *iv, size_t iv_len,
1202 const unsigned char *input, size_t ilen,
1203 unsigned char *output, size_t *olen )
1204{
Janos Follath24eed8d2019-11-22 13:21:35 +00001205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001206 size_t finish_olen;
1207
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001208 CIPHER_VALIDATE_RET( ctx != NULL );
1209 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1210 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1211 CIPHER_VALIDATE_RET( output != NULL );
1212 CIPHER_VALIDATE_RET( olen != NULL );
1213
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001214#if defined(MBEDTLS_USE_PSA_CRYPTO)
1215 if( ctx->psa_enabled == 1 )
1216 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001217 /* As in the non-PSA case, we don't check that
1218 * a key has been set. If not, the key slot will
1219 * still be in its default state of 0, which is
1220 * guaranteed to be invalid, hence the PSA-call
1221 * below will gracefully fail. */
1222 mbedtls_cipher_context_psa * const cipher_psa =
1223 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1224
1225 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001226 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001227 size_t part_len;
1228
1229 if( ctx->operation == MBEDTLS_DECRYPT )
1230 {
1231 status = psa_cipher_decrypt_setup( &cipher_op,
1232 cipher_psa->slot,
1233 cipher_psa->alg );
1234 }
1235 else if( ctx->operation == MBEDTLS_ENCRYPT )
1236 {
1237 status = psa_cipher_encrypt_setup( &cipher_op,
1238 cipher_psa->slot,
1239 cipher_psa->alg );
1240 }
1241 else
1242 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1243
1244 /* In the following, we can immediately return on an error,
1245 * because the PSA Crypto API guarantees that cipher operations
1246 * are terminated by unsuccessful calls to psa_cipher_update(),
1247 * and by any call to psa_cipher_finish(). */
1248 if( status != PSA_SUCCESS )
1249 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1250
1251 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1252 if( status != PSA_SUCCESS )
1253 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1254
1255 status = psa_cipher_update( &cipher_op,
1256 input, ilen,
1257 output, ilen, olen );
1258 if( status != PSA_SUCCESS )
1259 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1260
1261 status = psa_cipher_finish( &cipher_op,
1262 output + *olen, ilen - *olen,
1263 &part_len );
1264 if( status != PSA_SUCCESS )
1265 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1266
1267 *olen += part_len;
1268 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001269 }
1270#endif /* MBEDTLS_USE_PSA_CRYPTO */
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001273 return( ret );
1274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001276 return( ret );
1277
Hanno Becker18597cd2018-11-09 16:36:33 +00001278 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1279 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001280 return( ret );
1281
Hanno Becker18597cd2018-11-09 16:36:33 +00001282 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1283 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001284 return( ret );
1285
1286 *olen += finish_olen;
1287
1288 return( 0 );
1289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001292/*
1293 * Packet-oriented encryption for AEAD modes
1294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001296 const unsigned char *iv, size_t iv_len,
1297 const unsigned char *ad, size_t ad_len,
1298 const unsigned char *input, size_t ilen,
1299 unsigned char *output, size_t *olen,
1300 unsigned char *tag, size_t tag_len )
1301{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001302 CIPHER_VALIDATE_RET( ctx != NULL );
1303 CIPHER_VALIDATE_RET( iv != NULL );
1304 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1305 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1306 CIPHER_VALIDATE_RET( output != NULL );
1307 CIPHER_VALIDATE_RET( olen != NULL );
1308 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1309
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001310#if defined(MBEDTLS_USE_PSA_CRYPTO)
1311 if( ctx->psa_enabled == 1 )
1312 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001313 /* As in the non-PSA case, we don't check that
1314 * a key has been set. If not, the key slot will
1315 * still be in its default state of 0, which is
1316 * guaranteed to be invalid, hence the PSA-call
1317 * below will gracefully fail. */
1318 mbedtls_cipher_context_psa * const cipher_psa =
1319 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1320
1321 psa_status_t status;
1322
1323 /* PSA Crypto API always writes the authentication tag
1324 * at the end of the encrypted message. */
1325 if( tag != output + ilen )
1326 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1327
1328 status = psa_aead_encrypt( cipher_psa->slot,
1329 cipher_psa->alg,
1330 iv, iv_len,
1331 ad, ad_len,
1332 input, ilen,
1333 output, ilen + tag_len, olen );
1334 if( status != PSA_SUCCESS )
1335 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1336
1337 *olen -= tag_len;
1338 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001339 }
1340#endif /* MBEDTLS_USE_PSA_CRYPTO */
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_GCM_C)
1343 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001344 {
1345 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001346 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1347 ilen, iv, iv_len, ad, ad_len,
1348 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_GCM_C */
1351#if defined(MBEDTLS_CCM_C)
1352 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001353 {
1354 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001356 iv, iv_len, ad, ad_len, input, output,
1357 tag, tag_len ) );
1358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001360#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001361 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1362 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001363 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001364 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001365 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001366 {
1367 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1368 }
1369
1370 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001371 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001372 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001373 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001374#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001375#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001376 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1377 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001378 {
1379 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1380 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1381
1382 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1383 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1384 {
1385 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1386 }
1387
1388 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1389 }
1390#endif /* MBEDTLS_NIST_KW_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/*
1396 * Packet-oriented decryption for AEAD modes
1397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001399 const unsigned char *iv, size_t iv_len,
1400 const unsigned char *ad, size_t ad_len,
1401 const unsigned char *input, size_t ilen,
1402 unsigned char *output, size_t *olen,
1403 const unsigned char *tag, size_t tag_len )
1404{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001405 CIPHER_VALIDATE_RET( ctx != NULL );
1406 CIPHER_VALIDATE_RET( iv != NULL );
1407 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1408 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1409 CIPHER_VALIDATE_RET( output != NULL );
1410 CIPHER_VALIDATE_RET( olen != NULL );
1411 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001412
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001413#if defined(MBEDTLS_USE_PSA_CRYPTO)
1414 if( ctx->psa_enabled == 1 )
1415 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001416 /* As in the non-PSA case, we don't check that
1417 * a key has been set. If not, the key slot will
1418 * still be in its default state of 0, which is
1419 * guaranteed to be invalid, hence the PSA-call
1420 * below will gracefully fail. */
1421 mbedtls_cipher_context_psa * const cipher_psa =
1422 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1423
1424 psa_status_t status;
1425
1426 /* PSA Crypto API always writes the authentication tag
1427 * at the end of the encrypted message. */
1428 if( tag != input + ilen )
1429 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1430
1431 status = psa_aead_decrypt( cipher_psa->slot,
1432 cipher_psa->alg,
1433 iv, iv_len,
1434 ad, ad_len,
1435 input, ilen + tag_len,
1436 output, ilen, olen );
1437 if( status == PSA_ERROR_INVALID_SIGNATURE )
1438 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1439 else if( status != PSA_SUCCESS )
1440 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1441
1442 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001443 }
1444#endif /* MBEDTLS_USE_PSA_CRYPTO */
1445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_GCM_C)
1447 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001448 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001450
1451 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001453 iv, iv_len, ad, ad_len,
1454 tag, tag_len, input, output );
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1457 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001458
1459 return( ret );
1460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_GCM_C */
1462#if defined(MBEDTLS_CCM_C)
1463 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001464 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001466
1467 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001469 iv, iv_len, ad, ad_len,
1470 input, output, tag, tag_len );
1471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1473 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001474
1475 return( ret );
1476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001478#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001479 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1480 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001482
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001483 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001484 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001485 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001486 {
1487 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1488 }
1489
1490 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001491 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1492 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001493
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001494 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1495 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001496
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001497 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001498 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001499#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001500#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001501 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1502 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001503 {
1504 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1505 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1506
1507 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1508 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1509 {
1510 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1511 }
1512
1513 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1514 }
1515#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_CIPHER_C */