blob: ee365a185eec82ef0b29803fad518566d2ec51e1 [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"
Paul Bakker8123e9d2011-01-06 15:37:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stdlib.h>
34#include <string.h>
35
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020036#if defined(MBEDTLS_CHACHAPOLY_C)
37#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020046#endif
47
Daniel Kingbd920622016-05-15 19:56:20 -030048#if defined(MBEDTLS_CHACHA20_C)
49#include "mbedtls/chacha20.h"
50#endif
51
Simon Butcher327398a2016-10-05 14:09:11 +010052#if defined(MBEDTLS_CMAC_C)
53#include "mbedtls/cmac.h"
54#endif
55
Hanno Becker4ccfc402018-11-09 16:10:57 +000056#if defined(MBEDTLS_USE_PSA_CRYPTO)
57#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000058#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#endif /* MBEDTLS_USE_PSA_CRYPTO */
60
Jack Lloydffdf2882019-03-07 17:00:32 -050061#if defined(MBEDTLS_NIST_KW_C)
62#include "mbedtls/nist_kw.h"
63#endif
64
Simon Butcher327398a2016-10-05 14:09:11 +010065#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#define mbedtls_calloc calloc
69#define mbedtls_free free
70#endif
71
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072#define CIPHER_VALIDATE_RET( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
74#define CIPHER_VALIDATE( cond ) \
75 MBEDTLS_INTERNAL_VALIDATE( cond )
76
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020077#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030078/* Compare the contents of two buffers in constant time.
79 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030080 * a non-zero value.
81 * This is currently only used by GCM and ChaCha20+Poly1305.
82 */
Hanno Becker18597cd2018-11-09 16:36:33 +000083static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
84 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030085{
86 const unsigned char *p1 = (const unsigned char*) v1;
87 const unsigned char *p2 = (const unsigned char*) v2;
88 size_t i;
89 unsigned char diff;
90
91 for( diff = 0, i = 0; i < len; i++ )
92 diff |= p1[i] ^ p2[i];
93
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -030095}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020096#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030097
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020098static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200103 int *type;
104
105 if( ! supported_init )
106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 def = mbedtls_cipher_definitions;
108 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200109
110 while( def->type != 0 )
111 *type++ = (*def++).type;
112
113 *type = 0;
114
115 supported_init = 1;
116 }
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000119}
120
Hanno Becker18597cd2018-11-09 16:36:33 +0000121const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
122 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200127 if( def->type == cipher_type )
128 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000129
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200130 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131}
132
Hanno Becker18597cd2018-11-09 16:36:33 +0000133const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
134 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200137
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200139 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200142 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200143 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000144
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146}
147
Hanno Becker18597cd2018-11-09 16:36:33 +0000148const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
149 const mbedtls_cipher_id_t cipher_id,
150 int key_bitlen,
151 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200156 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200157 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200158 def->info->mode == mode )
159 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200160
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200161 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200165{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200168}
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200171{
172 if( ctx == NULL )
173 return;
174
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000175#if defined(MBEDTLS_USE_PSA_CRYPTO)
176 if( ctx->psa_enabled == 1 )
177 {
Hanno Becker6118e432018-11-09 16:47:20 +0000178 if( ctx->cipher_ctx != NULL )
179 {
180 mbedtls_cipher_context_psa * const cipher_psa =
181 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
182
Hanno Becker19086552018-11-17 22:11:16 +0000183 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000184 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000185 /* xxx_free() doesn't allow to return failures. */
186 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000187 }
188
189 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
190 mbedtls_free( cipher_psa );
191 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000192
193 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
194 return;
195 }
196#endif /* MBEDTLS_USE_PSA_CRYPTO */
197
Simon Butcher327398a2016-10-05 14:09:11 +0100198#if defined(MBEDTLS_CMAC_C)
199 if( ctx->cmac_ctx )
200 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500201 mbedtls_platform_zeroize( ctx->cmac_ctx,
202 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100203 mbedtls_free( ctx->cmac_ctx );
204 }
205#endif
206
Paul Bakker84bbeb52014-07-01 14:53:22 +0200207 if( ctx->cipher_ctx )
208 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
209
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500210 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200211}
212
Hanno Becker18597cd2018-11-09 16:36:33 +0000213int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
214 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 CIPHER_VALIDATE_RET( ctx != NULL );
217 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221
Paul Bakker343a8702011-06-09 14:27:58 +0000222 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224
225 ctx->cipher_info = cipher_info;
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200228 /*
229 * Ignore possible errors caused by a cipher mode that doesn't use padding
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
232 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200233#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200237
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239}
240
Hanno Becker4ccfc402018-11-09 16:10:57 +0000241#if defined(MBEDTLS_USE_PSA_CRYPTO)
242int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000243 const mbedtls_cipher_info_t *cipher_info,
244 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000245{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000246 psa_algorithm_t alg;
247 mbedtls_cipher_context_psa *cipher_psa;
248
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000249 if( NULL == cipher_info || NULL == ctx )
250 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
251
Hanno Becker4ee7e762018-11-17 22:00:38 +0000252 /* Check that the underlying cipher mode and cipher type are
253 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000254 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000255 if( alg == 0 )
256 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
257 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000258 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000259
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000260 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
261
Hanno Beckeredda8b82018-11-12 11:59:30 +0000262 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
263 if( cipher_psa == NULL )
264 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
265 cipher_psa->alg = alg;
266 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000267 ctx->cipher_info = cipher_info;
268 ctx->psa_enabled = 1;
269 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000270}
271#endif /* MBEDTLS_USE_PSA_CRYPTO */
272
Hanno Becker18597cd2018-11-09 16:36:33 +0000273int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
274 const unsigned char *key,
275 int key_bitlen,
276 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 CIPHER_VALIDATE_RET( ctx != NULL );
279 CIPHER_VALIDATE_RET( key != NULL );
280 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100281 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000285#if defined(MBEDTLS_USE_PSA_CRYPTO)
286 if( ctx->psa_enabled == 1 )
287 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000288 mbedtls_cipher_context_psa * const cipher_psa =
289 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
290
291 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
292
293 psa_status_t status;
294 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000296
297 /* PSA Crypto API only accepts byte-aligned keys. */
298 if( key_bitlen % 8 != 0 )
299 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
300
301 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000302 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000303 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
304
Andrzej Kurekc7509322019-01-08 09:36:01 -0500305 key_type = mbedtls_psa_translate_cipher_type(
306 ctx->cipher_info->type );
307 if( key_type == 0 )
308 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200309 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000310
311 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500312 * (encrypt vs. decrypt): it is possible to setup a key for encryption
313 * and use it for AEAD decryption. Until tests relying on this
314 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200315 psa_set_key_usage_flags( &attributes,
316 /* mbedtls_psa_translate_cipher_operation( operation ); */
317 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
318 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000319
Gilles Peskined2d45c12019-05-27 14:53:13 +0200320 status = psa_import_key( &attributes, key, key_bytelen,
321 &cipher_psa->slot );
322 switch( status )
323 {
324 case PSA_SUCCESS:
325 break;
326 case PSA_ERROR_INSUFFICIENT_MEMORY:
327 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
328 case PSA_ERROR_NOT_SUPPORTED:
329 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
330 default:
331 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
332 }
333 /* Indicate that we own the key slot and need to
334 * destroy it in mbedtls_cipher_free(). */
335 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000336
337 ctx->key_bitlen = key_bitlen;
338 ctx->operation = operation;
339 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000340 }
341#endif /* MBEDTLS_USE_PSA_CRYPTO */
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200344 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200347 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200348
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200349 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350 ctx->operation = operation;
351
Paul Bakker343a8702011-06-09 14:27:58 +0000352 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100353 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( MBEDTLS_ENCRYPT == operation ||
356 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100357 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000359 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500360 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
361 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000362 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500365 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
366 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369}
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372 const unsigned char *iv,
373 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200375 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 CIPHER_VALIDATE_RET( ctx != NULL );
378 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
379 if( ctx->cipher_info == NULL )
380 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000381#if defined(MBEDTLS_USE_PSA_CRYPTO)
382 if( ctx->psa_enabled == 1 )
383 {
384 /* While PSA Crypto has an API for multipart
385 * operations, we currently don't make it
386 * accessible through the cipher layer. */
387 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
388 }
389#endif /* MBEDTLS_USE_PSA_CRYPTO */
390
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200391 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
393 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200396 actual_iv_size = iv_len;
397 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200398 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200399 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200400
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200401 /* avoid reading past the end of input buffer */
402 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200404 }
405
Daniel Kingbd920622016-05-15 19:56:20 -0300406#if defined(MBEDTLS_CHACHA20_C)
407 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
408 {
409 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
410 iv,
411 0U ) ) /* Initial counter value */
412 {
413 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
414 }
415 }
416#endif
417
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300418 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300419 {
420 memcpy( ctx->iv, iv, actual_iv_size );
421 ctx->iv_size = actual_iv_size;
422 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200423
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200424 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200425}
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200428{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 CIPHER_VALIDATE_RET( ctx != NULL );
430 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200432
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000433#if defined(MBEDTLS_USE_PSA_CRYPTO)
434 if( ctx->psa_enabled == 1 )
435 {
436 /* We don't support resetting PSA-based
437 * cipher contexts, yet. */
438 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
439 }
440#endif /* MBEDTLS_USE_PSA_CRYPTO */
441
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442 ctx->unprocessed_len = 0;
443
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200444 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200445}
446
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200447#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200449 const unsigned char *ad, size_t ad_len )
450{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500451 CIPHER_VALIDATE_RET( ctx != NULL );
452 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
453 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200455
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000456#if defined(MBEDTLS_USE_PSA_CRYPTO)
457 if( ctx->psa_enabled == 1 )
458 {
459 /* While PSA Crypto has an API for multipart
460 * operations, we currently don't make it
461 * accessible through the cipher layer. */
462 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
463 }
464#endif /* MBEDTLS_USE_PSA_CRYPTO */
465
Daniel King8fe47012016-05-17 20:33:28 -0300466#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200468 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500469 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
470 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200471 }
Daniel King8fe47012016-05-17 20:33:28 -0300472#endif
473
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200474#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300475 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
476 {
477 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200478 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300479
480 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200481 ? MBEDTLS_CHACHAPOLY_ENCRYPT
482 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300483
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200484 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300485 ctx->iv,
486 mode );
487 if ( result != 0 )
488 return( result );
489
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
491 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300492 }
493#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200494
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200495 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200497#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200500 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 CIPHER_VALIDATE_RET( ctx != NULL );
506 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
507 CIPHER_VALIDATE_RET( output != NULL );
508 CIPHER_VALIDATE_RET( olen != NULL );
509 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000512#if defined(MBEDTLS_USE_PSA_CRYPTO)
513 if( ctx->psa_enabled == 1 )
514 {
515 /* While PSA Crypto has an API for multipart
516 * operations, we currently don't make it
517 * accessible through the cipher layer. */
518 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
519 }
520#endif /* MBEDTLS_USE_PSA_CRYPTO */
521
Paul Bakker6c212762013-12-16 15:24:50 +0100522 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100523 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100524 if ( 0 == block_size )
525 {
526 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
527 }
Paul Bakker6c212762013-12-16 15:24:50 +0100528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200530 {
Janos Follath98e28a72016-05-31 14:03:54 +0100531 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200533
534 *olen = ilen;
535
536 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
537 ctx->operation, input, output ) ) )
538 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200539 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200540 }
541
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200542 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_GCM_C)
546 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200547 {
548 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
550 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200551 }
552#endif
553
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200554#if defined(MBEDTLS_CHACHAPOLY_C)
555 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
556 {
557 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
559 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200560 }
561#endif
562
Paul Bakker68884e32013-01-07 18:20:04 +0100563 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100564 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100567 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_CIPHER_MODE_CBC)
570 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200572 size_t copy_len = 0;
573
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574 /*
575 * If there is not enough data for a full block, cache it.
576 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700577 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000578 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700579 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
580 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000582 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583 {
584 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
585 ilen );
586
587 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200588 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589 }
590
591 /*
592 * Process cached data first
593 */
Janos Follath98e28a72016-05-31 14:03:54 +0100594 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 {
Janos Follath98e28a72016-05-31 14:03:54 +0100596 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597
598 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
599 copy_len );
600
Paul Bakkerff61a782011-06-09 15:42:02 +0000601 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100602 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000603 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200605 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 }
607
Janos Follath98e28a72016-05-31 14:03:54 +0100608 *olen += block_size;
609 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 ctx->unprocessed_len = 0;
611
612 input += copy_len;
613 ilen -= copy_len;
614 }
615
616 /*
617 * Cache final, incomplete block
618 */
619 if( 0 != ilen )
620 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700621 /* Encryption: only cache partial blocks
622 * Decryption w/ padding: always keep at least one whole block
623 * Decryption w/o padding: only cache partial blocks
624 */
Janos Follath98e28a72016-05-31 14:03:54 +0100625 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700626 if( copy_len == 0 &&
627 ctx->operation == MBEDTLS_DECRYPT &&
628 NULL != ctx->add_padding)
629 {
Janos Follath98e28a72016-05-31 14:03:54 +0100630 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700631 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
633 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
634 copy_len );
635
636 ctx->unprocessed_len += copy_len;
637 ilen -= copy_len;
638 }
639
640 /*
641 * Process remaining full blocks
642 */
643 if( ilen )
644 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000645 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
646 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200650
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 *olen += ilen;
652 }
653
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200654 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_CIPHER_MODE_CFB)
659 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000660 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000661 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000662 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000663 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000664 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200665 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000666 }
667
668 *olen = ilen;
669
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200670 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000673
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100674#if defined(MBEDTLS_CIPHER_MODE_OFB)
675 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
676 {
677 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
678 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
679 {
680 return( ret );
681 }
682
683 *olen = ilen;
684
685 return( 0 );
686 }
687#endif /* MBEDTLS_CIPHER_MODE_OFB */
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_CIPHER_MODE_CTR)
690 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000691 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000692 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000693 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000694 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000695 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000697 }
698
699 *olen = ilen;
700
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200701 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000702 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000704
Jaeden Ameroc6539902018-04-30 17:17:41 +0100705#if defined(MBEDTLS_CIPHER_MODE_XTS)
706 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
707 {
708 if( ctx->unprocessed_len > 0 ) {
709 /* We can only process an entire data unit at a time. */
710 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
711 }
712
713 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
714 ctx->operation, ilen, ctx->iv, input, output );
715 if( ret != 0 )
716 {
717 return( ret );
718 }
719
720 *olen = ilen;
721
722 return( 0 );
723 }
724#endif /* MBEDTLS_CIPHER_MODE_XTS */
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_CIPHER_MODE_STREAM)
727 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200728 {
729 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
730 ilen, input, output ) ) )
731 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200732 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200733 }
734
735 *olen = ilen;
736
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200737 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200738 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742}
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
745#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200746/*
747 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
748 */
Paul Bakker23986e52011-04-24 08:57:21 +0000749static void add_pkcs_padding( unsigned char *output, size_t output_len,
750 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751{
Paul Bakker23986e52011-04-24 08:57:21 +0000752 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100753 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754
755 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000756 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757}
758
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200759static int get_pkcs_padding( unsigned char *input, size_t input_len,
760 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000761{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100762 size_t i, pad_idx;
763 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764
Paul Bakkera885d682011-01-20 16:35:05 +0000765 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767
768 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769 *data_len = input_len - padding_len;
770
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100771 /* Avoid logical || since it results in a branch */
772 bad |= padding_len > input_len;
773 bad |= padding_len == 0;
774
775 /* The number of bytes checked must be independent of padding_len,
776 * so pick input_len, which is usually 8 or 16 (one block) */
777 pad_idx = input_len - padding_len;
778 for( i = 0; i < input_len; i++ )
779 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200786/*
787 * One and zeros padding: fill with 80 00 ... 00
788 */
789static void add_one_and_zeros_padding( unsigned char *output,
790 size_t output_len, size_t data_len )
791{
792 size_t padding_len = output_len - data_len;
793 unsigned char i = 0;
794
795 output[data_len] = 0x80;
796 for( i = 1; i < padding_len; i++ )
797 output[data_len + i] = 0x00;
798}
799
800static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
801 size_t *data_len )
802{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100803 size_t i;
804 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200805
806 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200808
Micha Krausba8316f2017-12-23 23:40:08 +0100809 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100810 *data_len = 0;
811 for( i = input_len; i > 0; i-- )
812 {
813 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100814 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100815 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100816 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100817 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200820
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200825/*
826 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
827 */
828static void add_zeros_and_len_padding( unsigned char *output,
829 size_t output_len, size_t data_len )
830{
831 size_t padding_len = output_len - data_len;
832 unsigned char i = 0;
833
834 for( i = 1; i < padding_len; i++ )
835 output[data_len + i - 1] = 0x00;
836 output[output_len - 1] = (unsigned char) padding_len;
837}
838
839static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
840 size_t *data_len )
841{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100842 size_t i, pad_idx;
843 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200844
845 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200847
848 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200849 *data_len = input_len - padding_len;
850
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100851 /* Avoid logical || since it results in a branch */
852 bad |= padding_len > input_len;
853 bad |= padding_len == 0;
854
855 /* The number of bytes checked must be independent of padding_len */
856 pad_idx = input_len - padding_len;
857 for( i = 0; i < input_len - 1; i++ )
858 bad |= input[i] * ( i >= pad_idx );
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200865/*
866 * Zero padding: fill with 00 ... 00
867 */
868static void add_zeros_padding( unsigned char *output,
869 size_t output_len, size_t data_len )
870{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200871 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200872
873 for( i = data_len; i < output_len; i++ )
874 output[i] = 0x00;
875}
876
877static int get_zeros_padding( unsigned char *input, size_t input_len,
878 size_t *data_len )
879{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100880 size_t i;
881 unsigned char done = 0, prev_done;
882
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200883 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100886 *data_len = 0;
887 for( i = input_len; i > 0; i-- )
888 {
889 prev_done = done;
890 done |= ( input[i-1] != 0 );
891 *data_len |= i * ( done != prev_done );
892 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200893
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200894 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200897
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200898/*
899 * No padding: don't pad :)
900 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200902 * but a trivial get_padding function
903 */
904static int get_no_padding( unsigned char *input, size_t input_len,
905 size_t *data_len )
906{
907 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200909
910 *data_len = input_len;
911
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200912 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200917 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000918{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500919 CIPHER_VALIDATE_RET( ctx != NULL );
920 CIPHER_VALIDATE_RET( output != NULL );
921 CIPHER_VALIDATE_RET( olen != NULL );
922 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000924
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000925#if defined(MBEDTLS_USE_PSA_CRYPTO)
926 if( ctx->psa_enabled == 1 )
927 {
928 /* While PSA Crypto has an API for multipart
929 * operations, we currently don't make it
930 * accessible through the cipher layer. */
931 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
932 }
933#endif /* MBEDTLS_USE_PSA_CRYPTO */
934
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935 *olen = 0;
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100938 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
940 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100941 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000943 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200944 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000945 }
946
Daniel King8fe47012016-05-17 20:33:28 -0300947 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
948 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300949 {
950 return( 0 );
951 }
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200954 {
955 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200957
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200958 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200959 }
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_CIPHER_MODE_CBC)
962 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000963 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200964 int ret = 0;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000967 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200968 /* check for 'no padding' mode */
969 if( NULL == ctx->add_padding )
970 {
971 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200973
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200974 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975 }
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 ctx->unprocessed_len );
979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000981 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200982 /*
983 * For decrypt operations, expect a full block,
984 * or an empty block if no padding
985 */
986 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000990 }
991
992 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000993 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000995 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200997 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000998 }
999
1000 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001002 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1003 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001004
1005 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001007 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001008 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001009#else
1010 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014}
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001017int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1018 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001019{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001020 CIPHER_VALIDATE_RET( ctx != NULL );
1021
1022 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001025 }
1026
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
1028 if( ctx->psa_enabled == 1 )
1029 {
1030 /* While PSA Crypto knows about CBC padding
1031 * schemes, we currently don't make them
1032 * accessible through the cipher layer. */
1033 if( mode != MBEDTLS_PADDING_NONE )
1034 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1035
1036 return( 0 );
1037 }
1038#endif /* MBEDTLS_USE_PSA_CRYPTO */
1039
Paul Bakker1a45d912013-08-14 12:04:26 +02001040 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1043 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001044 ctx->add_padding = add_pkcs_padding;
1045 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001046 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1049 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001050 ctx->add_padding = add_one_and_zeros_padding;
1051 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001052 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1055 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001056 ctx->add_padding = add_zeros_and_len_padding;
1057 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001058 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1061 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001062 ctx->add_padding = add_zeros_padding;
1063 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001064 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001067 ctx->add_padding = NULL;
1068 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001069 break;
1070
1071 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001073 }
1074
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001075 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001078
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001079#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001081 unsigned char *tag, size_t tag_len )
1082{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001083 CIPHER_VALIDATE_RET( ctx != NULL );
1084 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1085 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( MBEDTLS_ENCRYPT != ctx->operation )
1089 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001090
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001091#if defined(MBEDTLS_USE_PSA_CRYPTO)
1092 if( ctx->psa_enabled == 1 )
1093 {
1094 /* While PSA Crypto has an API for multipart
1095 * operations, we currently don't make it
1096 * accessible through the cipher layer. */
1097 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001098 }
1099#endif /* MBEDTLS_USE_PSA_CRYPTO */
1100
Daniel King8fe47012016-05-17 20:33:28 -03001101#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001103 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1104 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001105#endif
1106
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001107#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001108 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1109 {
1110 /* Don't allow truncated MAC for Poly1305 */
1111 if ( tag_len != 16U )
1112 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1113
Hanno Becker18597cd2018-11-09 16:36:33 +00001114 return( mbedtls_chachapoly_finish(
1115 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001116 }
1117#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001118
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001120}
Paul Bakker9af723c2014-05-01 13:03:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001123 const unsigned char *tag, size_t tag_len )
1124{
Daniel King8fe47012016-05-17 20:33:28 -03001125 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001127
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001128 CIPHER_VALIDATE_RET( ctx != NULL );
1129 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1130 if( ctx->cipher_info == NULL )
1131 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1132
1133 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001136 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001137
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001138#if defined(MBEDTLS_USE_PSA_CRYPTO)
1139 if( ctx->psa_enabled == 1 )
1140 {
1141 /* While PSA Crypto has an API for multipart
1142 * operations, we currently don't make it
1143 * accessible through the cipher layer. */
1144 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1145 }
1146#endif /* MBEDTLS_USE_PSA_CRYPTO */
1147
Daniel King8fe47012016-05-17 20:33:28 -03001148#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001150 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001151 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001153
Hanno Becker18597cd2018-11-09 16:36:33 +00001154 if( 0 != ( ret = mbedtls_gcm_finish(
1155 (mbedtls_gcm_context *) ctx->cipher_ctx,
1156 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001157 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001158 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001159 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001160
1161 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001162 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001164
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001165 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166 }
Daniel King8fe47012016-05-17 20:33:28 -03001167#endif /* MBEDTLS_GCM_C */
1168
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001169#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001170 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1171 {
1172 /* Don't allow truncated MAC for Poly1305 */
1173 if ( tag_len != sizeof( check_tag ) )
1174 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1175
Hanno Becker18597cd2018-11-09 16:36:33 +00001176 ret = mbedtls_chachapoly_finish(
1177 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001178 if ( ret != 0 )
1179 {
1180 return( ret );
1181 }
1182
1183 /* Check the tag in "constant-time" */
1184 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1185 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1186
1187 return( 0 );
1188 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001189#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001190
1191 return( 0 );
1192}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001193#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001194
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001195/*
1196 * Packet-oriented wrapper for non-AEAD modes
1197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001199 const unsigned char *iv, size_t iv_len,
1200 const unsigned char *input, size_t ilen,
1201 unsigned char *output, size_t *olen )
1202{
Janos Follath24eed8d2019-11-22 13:21:35 +00001203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001204 size_t finish_olen;
1205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001206 CIPHER_VALIDATE_RET( ctx != NULL );
1207 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1208 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1209 CIPHER_VALIDATE_RET( output != NULL );
1210 CIPHER_VALIDATE_RET( olen != NULL );
1211
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001212#if defined(MBEDTLS_USE_PSA_CRYPTO)
1213 if( ctx->psa_enabled == 1 )
1214 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001215 /* As in the non-PSA case, we don't check that
1216 * a key has been set. If not, the key slot will
1217 * still be in its default state of 0, which is
1218 * guaranteed to be invalid, hence the PSA-call
1219 * below will gracefully fail. */
1220 mbedtls_cipher_context_psa * const cipher_psa =
1221 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1222
1223 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001224 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001225 size_t part_len;
1226
1227 if( ctx->operation == MBEDTLS_DECRYPT )
1228 {
1229 status = psa_cipher_decrypt_setup( &cipher_op,
1230 cipher_psa->slot,
1231 cipher_psa->alg );
1232 }
1233 else if( ctx->operation == MBEDTLS_ENCRYPT )
1234 {
1235 status = psa_cipher_encrypt_setup( &cipher_op,
1236 cipher_psa->slot,
1237 cipher_psa->alg );
1238 }
1239 else
1240 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1241
1242 /* In the following, we can immediately return on an error,
1243 * because the PSA Crypto API guarantees that cipher operations
1244 * are terminated by unsuccessful calls to psa_cipher_update(),
1245 * and by any call to psa_cipher_finish(). */
1246 if( status != PSA_SUCCESS )
1247 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1248
1249 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1250 if( status != PSA_SUCCESS )
1251 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1252
1253 status = psa_cipher_update( &cipher_op,
1254 input, ilen,
1255 output, ilen, olen );
1256 if( status != PSA_SUCCESS )
1257 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1258
1259 status = psa_cipher_finish( &cipher_op,
1260 output + *olen, ilen - *olen,
1261 &part_len );
1262 if( status != PSA_SUCCESS )
1263 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1264
1265 *olen += part_len;
1266 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001267 }
1268#endif /* MBEDTLS_USE_PSA_CRYPTO */
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001271 return( ret );
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001274 return( ret );
1275
Hanno Becker18597cd2018-11-09 16:36:33 +00001276 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1277 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001278 return( ret );
1279
Hanno Becker18597cd2018-11-09 16:36:33 +00001280 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1281 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001282 return( ret );
1283
1284 *olen += finish_olen;
1285
1286 return( 0 );
1287}
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001290/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001291 * Packet-oriented encryption for AEAD modes: internal function shared by
1292 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001293 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001294static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001295 const unsigned char *iv, size_t iv_len,
1296 const unsigned char *ad, size_t ad_len,
1297 const unsigned char *input, size_t ilen,
1298 unsigned char *output, size_t *olen,
1299 unsigned char *tag, size_t tag_len )
1300{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001301#if defined(MBEDTLS_USE_PSA_CRYPTO)
1302 if( ctx->psa_enabled == 1 )
1303 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001304 /* As in the non-PSA case, we don't check that
1305 * a key has been set. If not, the key slot will
1306 * still be in its default state of 0, which is
1307 * guaranteed to be invalid, hence the PSA-call
1308 * below will gracefully fail. */
1309 mbedtls_cipher_context_psa * const cipher_psa =
1310 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1311
1312 psa_status_t status;
1313
1314 /* PSA Crypto API always writes the authentication tag
1315 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001316 if( output == NULL || tag != output + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001317 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1318
1319 status = psa_aead_encrypt( cipher_psa->slot,
1320 cipher_psa->alg,
1321 iv, iv_len,
1322 ad, ad_len,
1323 input, ilen,
1324 output, ilen + tag_len, olen );
1325 if( status != PSA_SUCCESS )
1326 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1327
1328 *olen -= tag_len;
1329 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001330 }
1331#endif /* MBEDTLS_USE_PSA_CRYPTO */
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_GCM_C)
1334 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001335 {
1336 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001337 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1338 ilen, iv, iv_len, ad, ad_len,
1339 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_GCM_C */
1342#if defined(MBEDTLS_CCM_C)
1343 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001344 {
1345 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001347 iv, iv_len, ad, ad_len, input, output,
1348 tag, tag_len ) );
1349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001351#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001352 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1353 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001354 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001355 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001356 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001357 {
1358 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1359 }
1360
1361 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001362 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001363 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001364 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001365#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001368}
1369
1370/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001371 * Packet-oriented encryption for AEAD modes: internal function shared by
1372 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001373 */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001374static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001375 const unsigned char *iv, size_t iv_len,
1376 const unsigned char *ad, size_t ad_len,
1377 const unsigned char *input, size_t ilen,
1378 unsigned char *output, size_t *olen,
1379 const unsigned char *tag, size_t tag_len )
1380{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001381#if defined(MBEDTLS_USE_PSA_CRYPTO)
1382 if( ctx->psa_enabled == 1 )
1383 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001384 /* As in the non-PSA case, we don't check that
1385 * a key has been set. If not, the key slot will
1386 * still be in its default state of 0, which is
1387 * guaranteed to be invalid, hence the PSA-call
1388 * below will gracefully fail. */
1389 mbedtls_cipher_context_psa * const cipher_psa =
1390 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1391
1392 psa_status_t status;
1393
1394 /* PSA Crypto API always writes the authentication tag
1395 * at the end of the encrypted message. */
Gilles Peskine70edd682020-12-03 20:27:27 +01001396 if( input == NULL || tag != input + ilen )
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001397 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1398
1399 status = psa_aead_decrypt( cipher_psa->slot,
1400 cipher_psa->alg,
1401 iv, iv_len,
1402 ad, ad_len,
1403 input, ilen + tag_len,
1404 output, ilen, olen );
1405 if( status == PSA_ERROR_INVALID_SIGNATURE )
1406 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1407 else if( status != PSA_SUCCESS )
1408 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1409
1410 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001411 }
1412#endif /* MBEDTLS_USE_PSA_CRYPTO */
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_GCM_C)
1415 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001416 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001418
1419 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001421 iv, iv_len, ad, ad_len,
1422 tag, tag_len, input, output );
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1425 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001426
1427 return( ret );
1428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_GCM_C */
1430#if defined(MBEDTLS_CCM_C)
1431 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001432 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001434
1435 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001437 iv, iv_len, ad, ad_len,
1438 input, output, tag, tag_len );
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1441 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001442
1443 return( ret );
1444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001446#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001447 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1448 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001450
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001451 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001452 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001453 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001454 {
1455 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1456 }
1457
1458 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001459 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1460 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001461
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001462 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1463 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001464
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001465 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001466 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001467#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001468
1469 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1470}
1471
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001472#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001473/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001474 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001475 */
1476int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1477 const unsigned char *iv, size_t iv_len,
1478 const unsigned char *ad, size_t ad_len,
1479 const unsigned char *input, size_t ilen,
1480 unsigned char *output, size_t *olen,
1481 unsigned char *tag, size_t tag_len )
1482{
1483 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001484 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001485 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1486 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001487 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001488 CIPHER_VALIDATE_RET( olen != NULL );
1489 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1490
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001491 return( mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1492 input, ilen, output, olen,
1493 tag, tag_len ) );
1494}
1495
1496/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001497 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001498 */
1499int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1500 const unsigned char *iv, size_t iv_len,
1501 const unsigned char *ad, size_t ad_len,
1502 const unsigned char *input, size_t ilen,
1503 unsigned char *output, size_t *olen,
1504 const unsigned char *tag, size_t tag_len )
1505{
1506 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001507 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001508 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1509 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001510 CIPHER_VALIDATE_RET( ilen == 0 || output != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001511 CIPHER_VALIDATE_RET( olen != NULL );
1512 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1513
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001514 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1515 input, ilen, output, olen,
1516 tag, tag_len ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001517}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001518#endif /* !MBEDTLS_DEPRECATED_REMOVED */
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é-Gonnardfaddf982020-11-25 13:39:47 +01001521#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1522/*
1523 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1524 */
1525int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1526 const unsigned char *iv, size_t iv_len,
1527 const unsigned char *ad, size_t ad_len,
1528 const unsigned char *input, size_t ilen,
1529 unsigned char *output, size_t output_len,
1530 size_t *olen, size_t tag_len )
1531{
1532 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001533 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001534 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1535 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1536 CIPHER_VALIDATE_RET( output != NULL );
1537 CIPHER_VALIDATE_RET( olen != NULL );
1538
1539#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001540 if(
1541#if defined(MBEDTLS_USE_PSA_CRYPTO)
1542 ctx->psa_enabled == 0 &&
1543#endif
1544 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1545 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001546 {
1547 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1548 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1549
1550 /* There is no iv, tag or ad associated with KW and KWP,
1551 * so these length should be 0 as documented. */
1552 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1553 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1554
1555 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
1556 output, olen, output_len ) );
1557 }
1558#endif /* MBEDTLS_NIST_KW_C */
1559
1560#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1561 /* AEAD case: check length before passing on to shared function */
1562 if( output_len < ilen + tag_len )
1563 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1564
1565 int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
1566 input, ilen, output, olen,
1567 output + ilen, tag_len );
1568 *olen += tag_len;
1569 return( ret );
1570#else
1571 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1572#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1573}
1574
1575/*
1576 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1577 */
1578int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1579 const unsigned char *iv, size_t iv_len,
1580 const unsigned char *ad, size_t ad_len,
1581 const unsigned char *input, size_t ilen,
1582 unsigned char *output, size_t output_len,
1583 size_t *olen, size_t tag_len )
1584{
1585 CIPHER_VALIDATE_RET( ctx != NULL );
Gilles Peskine70edd682020-12-03 20:27:27 +01001586 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001587 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1588 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1589 CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
1590 CIPHER_VALIDATE_RET( olen != NULL );
1591
1592#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001593 if(
1594#if defined(MBEDTLS_USE_PSA_CRYPTO)
1595 ctx->psa_enabled == 0 &&
1596#endif
1597 ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1598 MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001599 {
1600 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1601 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1602
1603 /* There is no iv, tag or ad associated with KW and KWP,
1604 * so these length should be 0 as documented. */
1605 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1606 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1607
1608 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
1609 output, olen, output_len ) );
1610 }
1611#endif /* MBEDTLS_NIST_KW_C */
1612
1613#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1614 /* AEAD case: check length before passing on to shared function */
1615 if( ilen < tag_len || output_len < ilen - tag_len )
1616 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1617
1618 return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
1619 input, ilen - tag_len, output, olen,
1620 input + ilen - tag_len, tag_len ) );
1621#else
1622 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1623#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1624}
1625#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_CIPHER_C */