blob: b62f1d5939b029248ca2bf60d2137fe1270ef49f [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdlib.h>
40#include <string.h>
41
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020042#if defined(MBEDTLS_CHACHAPOLY_C)
43#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020052#endif
53
Daniel Kingbd920622016-05-15 19:56:20 -030054#if defined(MBEDTLS_CHACHA20_C)
55#include "mbedtls/chacha20.h"
56#endif
57
Simon Butcher327398a2016-10-05 14:09:11 +010058#if defined(MBEDTLS_CMAC_C)
59#include "mbedtls/cmac.h"
60#endif
61
Hanno Becker4ccfc402018-11-09 16:10:57 +000062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000064#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000065#endif /* MBEDTLS_USE_PSA_CRYPTO */
66
Jack Lloydffdf2882019-03-07 17:00:32 -050067#if defined(MBEDTLS_NIST_KW_C)
68#include "mbedtls/nist_kw.h"
69#endif
70
Simon Butcher327398a2016-10-05 14:09:11 +010071#if defined(MBEDTLS_PLATFORM_C)
72#include "mbedtls/platform.h"
73#else
74#define mbedtls_calloc calloc
75#define mbedtls_free free
76#endif
77
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078#define CIPHER_VALIDATE_RET( cond ) \
79 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
80#define CIPHER_VALIDATE( cond ) \
81 MBEDTLS_INTERNAL_VALIDATE( cond )
82
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020083#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030084/* Compare the contents of two buffers in constant time.
85 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030086 * a non-zero value.
87 * This is currently only used by GCM and ChaCha20+Poly1305.
88 */
Hanno Becker18597cd2018-11-09 16:36:33 +000089static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
90 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030091{
92 const unsigned char *p1 = (const unsigned char*) v1;
93 const unsigned char *p2 = (const unsigned char*) v2;
94 size_t i;
95 unsigned char diff;
96
97 for( diff = 0, i = 0; i < len; i++ )
98 diff |= p1[i] ^ p2[i];
99
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -0300101}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200102#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -0300103
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200109 int *type;
110
111 if( ! supported_init )
112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 def = mbedtls_cipher_definitions;
114 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
116 while( def->type != 0 )
117 *type++ = (*def++).type;
118
119 *type = 0;
120
121 supported_init = 1;
122 }
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000125}
126
Hanno Becker18597cd2018-11-09 16:36:33 +0000127const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
128 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200133 if( def->type == cipher_type )
134 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000135
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137}
138
Hanno Becker18597cd2018-11-09 16:36:33 +0000139const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
140 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200143
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200148 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200149 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000150
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200151 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152}
153
Hanno Becker18597cd2018-11-09 16:36:33 +0000154const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
155 const mbedtls_cipher_id_t cipher_id,
156 int key_bitlen,
157 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200162 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200163 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200164 def->info->mode == mode )
165 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200166
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200167 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200168}
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200171{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200177{
178 if( ctx == NULL )
179 return;
180
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000181#if defined(MBEDTLS_USE_PSA_CRYPTO)
182 if( ctx->psa_enabled == 1 )
183 {
Hanno Becker6118e432018-11-09 16:47:20 +0000184 if( ctx->cipher_ctx != NULL )
185 {
186 mbedtls_cipher_context_psa * const cipher_psa =
187 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
188
Hanno Becker19086552018-11-17 22:11:16 +0000189 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000190 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000191 /* xxx_free() doesn't allow to return failures. */
192 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000193 }
194
195 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
196 mbedtls_free( cipher_psa );
197 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000198
199 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
200 return;
201 }
202#endif /* MBEDTLS_USE_PSA_CRYPTO */
203
Simon Butcher327398a2016-10-05 14:09:11 +0100204#if defined(MBEDTLS_CMAC_C)
205 if( ctx->cmac_ctx )
206 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500207 mbedtls_platform_zeroize( ctx->cmac_ctx,
208 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100209 mbedtls_free( ctx->cmac_ctx );
210 }
211#endif
212
Paul Bakker84bbeb52014-07-01 14:53:22 +0200213 if( ctx->cipher_ctx )
214 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
215
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500216 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200217}
218
Hanno Becker18597cd2018-11-09 16:36:33 +0000219int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
220 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500222 CIPHER_VALIDATE_RET( ctx != NULL );
223 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227
Paul Bakker343a8702011-06-09 14:27:58 +0000228 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230
231 ctx->cipher_info = cipher_info;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200234 /*
235 * Ignore possible errors caused by a cipher mode that doesn't use padding
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
238 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200239#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200243
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200244 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245}
246
Hanno Becker4ccfc402018-11-09 16:10:57 +0000247#if defined(MBEDTLS_USE_PSA_CRYPTO)
248int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000249 const mbedtls_cipher_info_t *cipher_info,
250 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000251{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000252 psa_algorithm_t alg;
253 mbedtls_cipher_context_psa *cipher_psa;
254
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000255 if( NULL == cipher_info || NULL == ctx )
256 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
257
Hanno Becker4ee7e762018-11-17 22:00:38 +0000258 /* Check that the underlying cipher mode and cipher type are
259 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000260 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000261 if( alg == 0 )
262 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
263 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000264 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000265
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000266 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
267
Hanno Beckeredda8b82018-11-12 11:59:30 +0000268 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
269 if( cipher_psa == NULL )
270 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
271 cipher_psa->alg = alg;
272 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000273 ctx->cipher_info = cipher_info;
274 ctx->psa_enabled = 1;
275 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000276}
277#endif /* MBEDTLS_USE_PSA_CRYPTO */
278
Hanno Becker18597cd2018-11-09 16:36:33 +0000279int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
280 const unsigned char *key,
281 int key_bitlen,
282 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000283{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 CIPHER_VALIDATE_RET( ctx != NULL );
285 CIPHER_VALIDATE_RET( key != NULL );
286 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100287 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000290
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000291#if defined(MBEDTLS_USE_PSA_CRYPTO)
292 if( ctx->psa_enabled == 1 )
293 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000294 mbedtls_cipher_context_psa * const cipher_psa =
295 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
296
297 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
298
299 psa_status_t status;
300 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000302
303 /* PSA Crypto API only accepts byte-aligned keys. */
304 if( key_bitlen % 8 != 0 )
305 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
306
307 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000308 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000309 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
310
Andrzej Kurekc7509322019-01-08 09:36:01 -0500311 key_type = mbedtls_psa_translate_cipher_type(
312 ctx->cipher_info->type );
313 if( key_type == 0 )
314 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200315 psa_set_key_type( &attributes, key_type );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000316
317 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500318 * (encrypt vs. decrypt): it is possible to setup a key for encryption
319 * and use it for AEAD decryption. Until tests relying on this
320 * are changed, allow any usage in PSA. */
Gilles Peskined2d45c12019-05-27 14:53:13 +0200321 psa_set_key_usage_flags( &attributes,
322 /* mbedtls_psa_translate_cipher_operation( operation ); */
323 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
324 psa_set_key_algorithm( &attributes, cipher_psa->alg );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000325
Gilles Peskined2d45c12019-05-27 14:53:13 +0200326 status = psa_import_key( &attributes, key, key_bytelen,
327 &cipher_psa->slot );
328 switch( status )
329 {
330 case PSA_SUCCESS:
331 break;
332 case PSA_ERROR_INSUFFICIENT_MEMORY:
333 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
334 case PSA_ERROR_NOT_SUPPORTED:
335 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
336 default:
337 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
338 }
339 /* Indicate that we own the key slot and need to
340 * destroy it in mbedtls_cipher_free(). */
341 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000342
343 ctx->key_bitlen = key_bitlen;
344 ctx->operation = operation;
345 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000346 }
347#endif /* MBEDTLS_USE_PSA_CRYPTO */
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200350 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200353 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200354
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200355 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 ctx->operation = operation;
357
Paul Bakker343a8702011-06-09 14:27:58 +0000358 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100359 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( MBEDTLS_ENCRYPT == operation ||
362 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100363 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000365 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500366 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
367 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000368 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500371 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
372 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375}
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 const unsigned char *iv,
379 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200381 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 CIPHER_VALIDATE_RET( ctx != NULL );
384 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
385 if( ctx->cipher_info == NULL )
386 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000387#if defined(MBEDTLS_USE_PSA_CRYPTO)
388 if( ctx->psa_enabled == 1 )
389 {
390 /* While PSA Crypto has an API for multipart
391 * operations, we currently don't make it
392 * accessible through the cipher layer. */
393 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
394 }
395#endif /* MBEDTLS_USE_PSA_CRYPTO */
396
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200397 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
399 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200402 actual_iv_size = iv_len;
403 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200404 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200405 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200406
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200407 /* avoid reading past the end of input buffer */
408 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200410 }
411
Daniel Kingbd920622016-05-15 19:56:20 -0300412#if defined(MBEDTLS_CHACHA20_C)
413 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
414 {
415 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
416 iv,
417 0U ) ) /* Initial counter value */
418 {
419 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
420 }
421 }
422#endif
423
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300424 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300425 {
426 memcpy( ctx->iv, iv, actual_iv_size );
427 ctx->iv_size = actual_iv_size;
428 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200429
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200430 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200431}
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200434{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500435 CIPHER_VALIDATE_RET( ctx != NULL );
436 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200438
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000439#if defined(MBEDTLS_USE_PSA_CRYPTO)
440 if( ctx->psa_enabled == 1 )
441 {
442 /* We don't support resetting PSA-based
443 * cipher contexts, yet. */
444 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
445 }
446#endif /* MBEDTLS_USE_PSA_CRYPTO */
447
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448 ctx->unprocessed_len = 0;
449
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200450 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200451}
452
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200453#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200455 const unsigned char *ad, size_t ad_len )
456{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457 CIPHER_VALIDATE_RET( ctx != NULL );
458 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
459 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200461
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000462#if defined(MBEDTLS_USE_PSA_CRYPTO)
463 if( ctx->psa_enabled == 1 )
464 {
465 /* While PSA Crypto has an API for multipart
466 * operations, we currently don't make it
467 * accessible through the cipher layer. */
468 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
469 }
470#endif /* MBEDTLS_USE_PSA_CRYPTO */
471
Daniel King8fe47012016-05-17 20:33:28 -0300472#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200474 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500475 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
476 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200477 }
Daniel King8fe47012016-05-17 20:33:28 -0300478#endif
479
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200480#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300481 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
482 {
483 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200484 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300485
486 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200487 ? MBEDTLS_CHACHAPOLY_ENCRYPT
488 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300489
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200490 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300491 ctx->iv,
492 mode );
493 if ( result != 0 )
494 return( result );
495
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500496 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
497 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300498 }
499#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200500
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200501 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200503#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200506 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507{
Janos Follath24eed8d2019-11-22 13:21:35 +0000508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500509 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 CIPHER_VALIDATE_RET( ctx != NULL );
512 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
513 CIPHER_VALIDATE_RET( output != NULL );
514 CIPHER_VALIDATE_RET( olen != NULL );
515 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519 if( ctx->psa_enabled == 1 )
520 {
521 /* While PSA Crypto has an API for multipart
522 * operations, we currently don't make it
523 * accessible through the cipher layer. */
524 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
525 }
526#endif /* MBEDTLS_USE_PSA_CRYPTO */
527
Paul Bakker6c212762013-12-16 15:24:50 +0100528 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100529 block_size = mbedtls_cipher_get_block_size( ctx );
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
Janos Follath98e28a72016-05-31 14:03:54 +0100565 if ( 0 == block_size )
566 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100568 }
569
Paul Bakker68884e32013-01-07 18:20:04 +0100570 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100571 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100574 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_CIPHER_MODE_CBC)
577 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200579 size_t copy_len = 0;
580
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581 /*
582 * If there is not enough data for a full block, cache it.
583 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700584 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000585 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700586 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
587 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000589 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590 {
591 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
592 ilen );
593
594 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200595 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 }
597
598 /*
599 * Process cached data first
600 */
Janos Follath98e28a72016-05-31 14:03:54 +0100601 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602 {
Janos Follath98e28a72016-05-31 14:03:54 +0100603 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604
605 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
606 copy_len );
607
Paul Bakkerff61a782011-06-09 15:42:02 +0000608 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100609 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000610 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200612 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613 }
614
Janos Follath98e28a72016-05-31 14:03:54 +0100615 *olen += block_size;
616 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617 ctx->unprocessed_len = 0;
618
619 input += copy_len;
620 ilen -= copy_len;
621 }
622
623 /*
624 * Cache final, incomplete block
625 */
626 if( 0 != ilen )
627 {
Janos Follath98e28a72016-05-31 14:03:54 +0100628 if( 0 == block_size )
629 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500630 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100631 }
632
Andy Leiserson79e77892017-04-28 20:01:49 -0700633 /* Encryption: only cache partial blocks
634 * Decryption w/ padding: always keep at least one whole block
635 * Decryption w/o padding: only cache partial blocks
636 */
Janos Follath98e28a72016-05-31 14:03:54 +0100637 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700638 if( copy_len == 0 &&
639 ctx->operation == MBEDTLS_DECRYPT &&
640 NULL != ctx->add_padding)
641 {
Janos Follath98e28a72016-05-31 14:03:54 +0100642 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700643 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644
645 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
646 copy_len );
647
648 ctx->unprocessed_len += copy_len;
649 ilen -= copy_len;
650 }
651
652 /*
653 * Process remaining full blocks
654 */
655 if( ilen )
656 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000657 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
658 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200660 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200662
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 *olen += ilen;
664 }
665
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_MODE_CFB)
671 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000672 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000673 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000674 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000675 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000676 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200677 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000678 }
679
680 *olen = ilen;
681
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200682 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000685
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100686#if defined(MBEDTLS_CIPHER_MODE_OFB)
687 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
688 {
689 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
690 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
691 {
692 return( ret );
693 }
694
695 *olen = ilen;
696
697 return( 0 );
698 }
699#endif /* MBEDTLS_CIPHER_MODE_OFB */
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_CIPHER_MODE_CTR)
702 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000703 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000704 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000705 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000706 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000707 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200708 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000709 }
710
711 *olen = ilen;
712
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200713 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000716
Jaeden Ameroc6539902018-04-30 17:17:41 +0100717#if defined(MBEDTLS_CIPHER_MODE_XTS)
718 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
719 {
720 if( ctx->unprocessed_len > 0 ) {
721 /* We can only process an entire data unit at a time. */
722 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
723 }
724
725 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
726 ctx->operation, ilen, ctx->iv, input, output );
727 if( ret != 0 )
728 {
729 return( ret );
730 }
731
732 *olen = ilen;
733
734 return( 0 );
735 }
736#endif /* MBEDTLS_CIPHER_MODE_XTS */
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#if defined(MBEDTLS_CIPHER_MODE_STREAM)
739 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740 {
741 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
742 ilen, input, output ) ) )
743 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200744 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200745 }
746
747 *olen = ilen;
748
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200749 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754}
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
757#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200758/*
759 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
760 */
Paul Bakker23986e52011-04-24 08:57:21 +0000761static void add_pkcs_padding( unsigned char *output, size_t output_len,
762 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763{
Paul Bakker23986e52011-04-24 08:57:21 +0000764 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100765 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766
767 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000768 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769}
770
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200771static int get_pkcs_padding( unsigned char *input, size_t input_len,
772 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100774 size_t i, pad_idx;
775 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776
Paul Bakkera885d682011-01-20 16:35:05 +0000777 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000779
780 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781 *data_len = input_len - padding_len;
782
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100783 /* Avoid logical || since it results in a branch */
784 bad |= padding_len > input_len;
785 bad |= padding_len == 0;
786
787 /* The number of bytes checked must be independent of padding_len,
788 * so pick input_len, which is usually 8 or 16 (one block) */
789 pad_idx = input_len - padding_len;
790 for( i = 0; i < input_len; i++ )
791 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200798/*
799 * One and zeros padding: fill with 80 00 ... 00
800 */
801static void add_one_and_zeros_padding( unsigned char *output,
802 size_t output_len, size_t data_len )
803{
804 size_t padding_len = output_len - data_len;
805 unsigned char i = 0;
806
807 output[data_len] = 0x80;
808 for( i = 1; i < padding_len; i++ )
809 output[data_len + i] = 0x00;
810}
811
812static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
813 size_t *data_len )
814{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100815 size_t i;
816 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200817
818 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200820
Micha Krausba8316f2017-12-23 23:40:08 +0100821 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100822 *data_len = 0;
823 for( i = input_len; i > 0; i-- )
824 {
825 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100826 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100827 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100828 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100829 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200832
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200837/*
838 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
839 */
840static void add_zeros_and_len_padding( unsigned char *output,
841 size_t output_len, size_t data_len )
842{
843 size_t padding_len = output_len - data_len;
844 unsigned char i = 0;
845
846 for( i = 1; i < padding_len; i++ )
847 output[data_len + i - 1] = 0x00;
848 output[output_len - 1] = (unsigned char) padding_len;
849}
850
851static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
852 size_t *data_len )
853{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100854 size_t i, pad_idx;
855 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200856
857 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200859
860 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200861 *data_len = input_len - padding_len;
862
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100863 /* Avoid logical || since it results in a branch */
864 bad |= padding_len > input_len;
865 bad |= padding_len == 0;
866
867 /* The number of bytes checked must be independent of padding_len */
868 pad_idx = input_len - padding_len;
869 for( i = 0; i < input_len - 1; i++ )
870 bad |= input[i] * ( i >= pad_idx );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200877/*
878 * Zero padding: fill with 00 ... 00
879 */
880static void add_zeros_padding( unsigned char *output,
881 size_t output_len, size_t data_len )
882{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200883 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200884
885 for( i = data_len; i < output_len; i++ )
886 output[i] = 0x00;
887}
888
889static int get_zeros_padding( unsigned char *input, size_t input_len,
890 size_t *data_len )
891{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100892 size_t i;
893 unsigned char done = 0, prev_done;
894
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200897
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100898 *data_len = 0;
899 for( i = input_len; i > 0; i-- )
900 {
901 prev_done = done;
902 done |= ( input[i-1] != 0 );
903 *data_len |= i * ( done != prev_done );
904 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200905
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200906 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200909
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200910/*
911 * No padding: don't pad :)
912 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200914 * but a trivial get_padding function
915 */
916static int get_no_padding( unsigned char *input, size_t input_len,
917 size_t *data_len )
918{
919 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200921
922 *data_len = input_len;
923
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200924 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200925}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200929 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000930{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500931 CIPHER_VALIDATE_RET( ctx != NULL );
932 CIPHER_VALIDATE_RET( output != NULL );
933 CIPHER_VALIDATE_RET( olen != NULL );
934 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000936
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000937#if defined(MBEDTLS_USE_PSA_CRYPTO)
938 if( ctx->psa_enabled == 1 )
939 {
940 /* While PSA Crypto has an API for multipart
941 * operations, we currently don't make it
942 * accessible through the cipher layer. */
943 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
944 }
945#endif /* MBEDTLS_USE_PSA_CRYPTO */
946
Paul Bakker8123e9d2011-01-06 15:37:30 +0000947 *olen = 0;
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100950 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
952 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100953 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000955 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200956 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000957 }
958
Daniel King8fe47012016-05-17 20:33:28 -0300959 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
960 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300961 {
962 return( 0 );
963 }
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200966 {
967 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200969
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200970 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200971 }
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_CIPHER_MODE_CBC)
974 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000975 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200976 int ret = 0;
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000979 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200980 /* check for 'no padding' mode */
981 if( NULL == ctx->add_padding )
982 {
983 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200985
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200986 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200987 }
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000990 ctx->unprocessed_len );
991 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000993 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994 /*
995 * For decrypt operations, expect a full block,
996 * or an empty block if no padding
997 */
998 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200999 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002 }
1003
1004 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001005 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001007 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001008 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001009 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001010 }
1011
1012 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001014 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1015 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016
1017 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001019 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001021#else
1022 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001026}
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001029int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1030 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001031{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001032 CIPHER_VALIDATE_RET( ctx != NULL );
1033
1034 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001037 }
1038
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001039#if defined(MBEDTLS_USE_PSA_CRYPTO)
1040 if( ctx->psa_enabled == 1 )
1041 {
1042 /* While PSA Crypto knows about CBC padding
1043 * schemes, we currently don't make them
1044 * accessible through the cipher layer. */
1045 if( mode != MBEDTLS_PADDING_NONE )
1046 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1047
1048 return( 0 );
1049 }
1050#endif /* MBEDTLS_USE_PSA_CRYPTO */
1051
Paul Bakker1a45d912013-08-14 12:04:26 +02001052 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1055 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001056 ctx->add_padding = add_pkcs_padding;
1057 ctx->get_padding = get_pkcs_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_ONE_AND_ZEROS)
1061 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001062 ctx->add_padding = add_one_and_zeros_padding;
1063 ctx->get_padding = get_one_and_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#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1067 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001068 ctx->add_padding = add_zeros_and_len_padding;
1069 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001070 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1073 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001074 ctx->add_padding = add_zeros_padding;
1075 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001076 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001079 ctx->add_padding = NULL;
1080 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001081 break;
1082
1083 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001085 }
1086
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001087 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001090
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001091#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001093 unsigned char *tag, size_t tag_len )
1094{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001095 CIPHER_VALIDATE_RET( ctx != NULL );
1096 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1097 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( MBEDTLS_ENCRYPT != ctx->operation )
1101 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001102
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001103#if defined(MBEDTLS_USE_PSA_CRYPTO)
1104 if( ctx->psa_enabled == 1 )
1105 {
1106 /* While PSA Crypto has an API for multipart
1107 * operations, we currently don't make it
1108 * accessible through the cipher layer. */
1109 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001110 }
1111#endif /* MBEDTLS_USE_PSA_CRYPTO */
1112
Daniel King8fe47012016-05-17 20:33:28 -03001113#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001115 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1116 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001117#endif
1118
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001119#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001120 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1121 {
1122 /* Don't allow truncated MAC for Poly1305 */
1123 if ( tag_len != 16U )
1124 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1125
Hanno Becker18597cd2018-11-09 16:36:33 +00001126 return( mbedtls_chachapoly_finish(
1127 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001128 }
1129#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001130
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001131 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001132}
Paul Bakker9af723c2014-05-01 13:03:14 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001135 const unsigned char *tag, size_t tag_len )
1136{
Daniel King8fe47012016-05-17 20:33:28 -03001137 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001139
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001140 CIPHER_VALIDATE_RET( ctx != NULL );
1141 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1142 if( ctx->cipher_info == NULL )
1143 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1144
1145 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001148 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001149
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001150#if defined(MBEDTLS_USE_PSA_CRYPTO)
1151 if( ctx->psa_enabled == 1 )
1152 {
1153 /* While PSA Crypto has an API for multipart
1154 * operations, we currently don't make it
1155 * accessible through the cipher layer. */
1156 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1157 }
1158#endif /* MBEDTLS_USE_PSA_CRYPTO */
1159
Daniel King8fe47012016-05-17 20:33:28 -03001160#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001162 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001163 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001165
Hanno Becker18597cd2018-11-09 16:36:33 +00001166 if( 0 != ( ret = mbedtls_gcm_finish(
1167 (mbedtls_gcm_context *) ctx->cipher_ctx,
1168 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001169 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001170 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001171 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001172
1173 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001174 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001176
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001177 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001178 }
Daniel King8fe47012016-05-17 20:33:28 -03001179#endif /* MBEDTLS_GCM_C */
1180
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001182 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1183 {
1184 /* Don't allow truncated MAC for Poly1305 */
1185 if ( tag_len != sizeof( check_tag ) )
1186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1187
Hanno Becker18597cd2018-11-09 16:36:33 +00001188 ret = mbedtls_chachapoly_finish(
1189 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001190 if ( ret != 0 )
1191 {
1192 return( ret );
1193 }
1194
1195 /* Check the tag in "constant-time" */
1196 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1197 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1198
1199 return( 0 );
1200 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001201#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001202
1203 return( 0 );
1204}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001205#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001206
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001207/*
1208 * Packet-oriented wrapper for non-AEAD modes
1209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001211 const unsigned char *iv, size_t iv_len,
1212 const unsigned char *input, size_t ilen,
1213 unsigned char *output, size_t *olen )
1214{
Janos Follath24eed8d2019-11-22 13:21:35 +00001215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001216 size_t finish_olen;
1217
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001218 CIPHER_VALIDATE_RET( ctx != NULL );
1219 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1220 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1221 CIPHER_VALIDATE_RET( output != NULL );
1222 CIPHER_VALIDATE_RET( olen != NULL );
1223
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001224#if defined(MBEDTLS_USE_PSA_CRYPTO)
1225 if( ctx->psa_enabled == 1 )
1226 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001227 /* As in the non-PSA case, we don't check that
1228 * a key has been set. If not, the key slot will
1229 * still be in its default state of 0, which is
1230 * guaranteed to be invalid, hence the PSA-call
1231 * below will gracefully fail. */
1232 mbedtls_cipher_context_psa * const cipher_psa =
1233 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1234
1235 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001236 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001237 size_t part_len;
1238
1239 if( ctx->operation == MBEDTLS_DECRYPT )
1240 {
1241 status = psa_cipher_decrypt_setup( &cipher_op,
1242 cipher_psa->slot,
1243 cipher_psa->alg );
1244 }
1245 else if( ctx->operation == MBEDTLS_ENCRYPT )
1246 {
1247 status = psa_cipher_encrypt_setup( &cipher_op,
1248 cipher_psa->slot,
1249 cipher_psa->alg );
1250 }
1251 else
1252 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1253
1254 /* In the following, we can immediately return on an error,
1255 * because the PSA Crypto API guarantees that cipher operations
1256 * are terminated by unsuccessful calls to psa_cipher_update(),
1257 * and by any call to psa_cipher_finish(). */
1258 if( status != PSA_SUCCESS )
1259 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1260
1261 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1262 if( status != PSA_SUCCESS )
1263 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1264
1265 status = psa_cipher_update( &cipher_op,
1266 input, ilen,
1267 output, ilen, olen );
1268 if( status != PSA_SUCCESS )
1269 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1270
1271 status = psa_cipher_finish( &cipher_op,
1272 output + *olen, ilen - *olen,
1273 &part_len );
1274 if( status != PSA_SUCCESS )
1275 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1276
1277 *olen += part_len;
1278 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001279 }
1280#endif /* MBEDTLS_USE_PSA_CRYPTO */
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001283 return( ret );
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001286 return( ret );
1287
Hanno Becker18597cd2018-11-09 16:36:33 +00001288 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1289 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001290 return( ret );
1291
Hanno Becker18597cd2018-11-09 16:36:33 +00001292 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1293 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001294 return( ret );
1295
1296 *olen += finish_olen;
1297
1298 return( 0 );
1299}
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001302/*
1303 * Packet-oriented encryption for AEAD modes
1304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001306 const unsigned char *iv, size_t iv_len,
1307 const unsigned char *ad, size_t ad_len,
1308 const unsigned char *input, size_t ilen,
1309 unsigned char *output, size_t *olen,
1310 unsigned char *tag, size_t tag_len )
1311{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001312 CIPHER_VALIDATE_RET( ctx != NULL );
1313 CIPHER_VALIDATE_RET( iv != NULL );
1314 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1315 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1316 CIPHER_VALIDATE_RET( output != NULL );
1317 CIPHER_VALIDATE_RET( olen != NULL );
1318 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1319
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001320#if defined(MBEDTLS_USE_PSA_CRYPTO)
1321 if( ctx->psa_enabled == 1 )
1322 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001323 /* As in the non-PSA case, we don't check that
1324 * a key has been set. If not, the key slot will
1325 * still be in its default state of 0, which is
1326 * guaranteed to be invalid, hence the PSA-call
1327 * below will gracefully fail. */
1328 mbedtls_cipher_context_psa * const cipher_psa =
1329 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1330
1331 psa_status_t status;
1332
1333 /* PSA Crypto API always writes the authentication tag
1334 * at the end of the encrypted message. */
1335 if( tag != output + ilen )
1336 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1337
1338 status = psa_aead_encrypt( cipher_psa->slot,
1339 cipher_psa->alg,
1340 iv, iv_len,
1341 ad, ad_len,
1342 input, ilen,
1343 output, ilen + tag_len, olen );
1344 if( status != PSA_SUCCESS )
1345 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1346
1347 *olen -= tag_len;
1348 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001349 }
1350#endif /* MBEDTLS_USE_PSA_CRYPTO */
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_GCM_C)
1353 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001354 {
1355 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001356 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1357 ilen, iv, iv_len, ad, ad_len,
1358 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_GCM_C */
1361#if defined(MBEDTLS_CCM_C)
1362 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001363 {
1364 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001366 iv, iv_len, ad, ad_len, input, output,
1367 tag, tag_len ) );
1368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001370#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001371 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1372 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001373 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001374 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001375 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001376 {
1377 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1378 }
1379
1380 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001381 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001382 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001383 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001384#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001385#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001386 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1387 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001388 {
1389 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1390 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1391
1392 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1393 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1394 {
1395 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1396 }
1397
1398 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1399 }
1400#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001403}
1404
1405/*
1406 * Packet-oriented decryption for AEAD modes
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001409 const unsigned char *iv, size_t iv_len,
1410 const unsigned char *ad, size_t ad_len,
1411 const unsigned char *input, size_t ilen,
1412 unsigned char *output, size_t *olen,
1413 const unsigned char *tag, size_t tag_len )
1414{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001415 CIPHER_VALIDATE_RET( ctx != NULL );
1416 CIPHER_VALIDATE_RET( iv != NULL );
1417 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1418 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1419 CIPHER_VALIDATE_RET( output != NULL );
1420 CIPHER_VALIDATE_RET( olen != NULL );
1421 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001422
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001423#if defined(MBEDTLS_USE_PSA_CRYPTO)
1424 if( ctx->psa_enabled == 1 )
1425 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001426 /* As in the non-PSA case, we don't check that
1427 * a key has been set. If not, the key slot will
1428 * still be in its default state of 0, which is
1429 * guaranteed to be invalid, hence the PSA-call
1430 * below will gracefully fail. */
1431 mbedtls_cipher_context_psa * const cipher_psa =
1432 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1433
1434 psa_status_t status;
1435
1436 /* PSA Crypto API always writes the authentication tag
1437 * at the end of the encrypted message. */
1438 if( tag != input + ilen )
1439 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1440
1441 status = psa_aead_decrypt( cipher_psa->slot,
1442 cipher_psa->alg,
1443 iv, iv_len,
1444 ad, ad_len,
1445 input, ilen + tag_len,
1446 output, ilen, olen );
1447 if( status == PSA_ERROR_INVALID_SIGNATURE )
1448 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1449 else if( status != PSA_SUCCESS )
1450 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1451
1452 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001453 }
1454#endif /* MBEDTLS_USE_PSA_CRYPTO */
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_GCM_C)
1457 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001458 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001460
1461 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001463 iv, iv_len, ad, ad_len,
1464 tag, tag_len, input, output );
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1467 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001468
1469 return( ret );
1470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_GCM_C */
1472#if defined(MBEDTLS_CCM_C)
1473 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001474 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001476
1477 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001479 iv, iv_len, ad, ad_len,
1480 input, output, tag, tag_len );
1481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1483 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001484
1485 return( ret );
1486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001488#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001489 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1490 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001492
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001493 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001494 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001495 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001496 {
1497 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1498 }
1499
1500 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001501 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1502 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001503
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001504 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1505 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001506
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001507 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001508 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001509#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001510#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001511 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1512 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001513 {
1514 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1515 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1516
1517 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1518 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1519 {
1520 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1521 }
1522
1523 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1524 }
1525#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_CIPHER_C */