blob: 409c3fe67424c6e2bd24460d8f8271e38320928a [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 );
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100530 if ( 0 == block_size )
531 {
532 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
533 }
Paul Bakker6c212762013-12-16 15:24:50 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200536 {
Janos Follath98e28a72016-05-31 14:03:54 +0100537 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200539
540 *olen = ilen;
541
542 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
543 ctx->operation, input, output ) ) )
544 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200545 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200546 }
547
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200548 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200549 }
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#if defined(MBEDTLS_GCM_C)
552 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200553 {
554 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500555 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
556 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200557 }
558#endif
559
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200560#if defined(MBEDTLS_CHACHAPOLY_C)
561 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
562 {
563 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500564 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
565 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200566 }
567#endif
568
Paul Bakker68884e32013-01-07 18:20:04 +0100569 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100570 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100573 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_CIPHER_MODE_CBC)
576 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200578 size_t copy_len = 0;
579
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 /*
581 * If there is not enough data for a full block, cache it.
582 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700583 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000584 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700585 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
586 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000588 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589 {
590 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
591 ilen );
592
593 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200594 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 }
596
597 /*
598 * Process cached data first
599 */
Janos Follath98e28a72016-05-31 14:03:54 +0100600 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 {
Janos Follath98e28a72016-05-31 14:03:54 +0100602 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
604 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
605 copy_len );
606
Paul Bakkerff61a782011-06-09 15:42:02 +0000607 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100608 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000609 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200611 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 }
613
Janos Follath98e28a72016-05-31 14:03:54 +0100614 *olen += block_size;
615 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 ctx->unprocessed_len = 0;
617
618 input += copy_len;
619 ilen -= copy_len;
620 }
621
622 /*
623 * Cache final, incomplete block
624 */
625 if( 0 != ilen )
626 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700627 /* Encryption: only cache partial blocks
628 * Decryption w/ padding: always keep at least one whole block
629 * Decryption w/o padding: only cache partial blocks
630 */
Janos Follath98e28a72016-05-31 14:03:54 +0100631 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700632 if( copy_len == 0 &&
633 ctx->operation == MBEDTLS_DECRYPT &&
634 NULL != ctx->add_padding)
635 {
Janos Follath98e28a72016-05-31 14:03:54 +0100636 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700637 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638
639 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
640 copy_len );
641
642 ctx->unprocessed_len += copy_len;
643 ilen -= copy_len;
644 }
645
646 /*
647 * Process remaining full blocks
648 */
649 if( ilen )
650 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000651 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
652 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200654 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200656
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 *olen += ilen;
658 }
659
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200660 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_CIPHER_MODE_CFB)
665 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000666 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000667 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000668 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000669 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000670 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000672 }
673
674 *olen = ilen;
675
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000679
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100680#if defined(MBEDTLS_CIPHER_MODE_OFB)
681 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
682 {
683 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
684 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
685 {
686 return( ret );
687 }
688
689 *olen = ilen;
690
691 return( 0 );
692 }
693#endif /* MBEDTLS_CIPHER_MODE_OFB */
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_CIPHER_MODE_CTR)
696 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000697 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000698 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000699 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000700 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000701 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200702 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000703 }
704
705 *olen = ilen;
706
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200707 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000710
Jaeden Ameroc6539902018-04-30 17:17:41 +0100711#if defined(MBEDTLS_CIPHER_MODE_XTS)
712 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
713 {
714 if( ctx->unprocessed_len > 0 ) {
715 /* We can only process an entire data unit at a time. */
716 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
717 }
718
719 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
720 ctx->operation, ilen, ctx->iv, input, output );
721 if( ret != 0 )
722 {
723 return( ret );
724 }
725
726 *olen = ilen;
727
728 return( 0 );
729 }
730#endif /* MBEDTLS_CIPHER_MODE_XTS */
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_CIPHER_MODE_STREAM)
733 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200734 {
735 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
736 ilen, input, output ) ) )
737 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200738 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200739 }
740
741 *olen = ilen;
742
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200743 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200744 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748}
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
751#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200752/*
753 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
754 */
Paul Bakker23986e52011-04-24 08:57:21 +0000755static void add_pkcs_padding( unsigned char *output, size_t output_len,
756 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757{
Paul Bakker23986e52011-04-24 08:57:21 +0000758 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100759 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
761 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000762 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763}
764
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200765static int get_pkcs_padding( unsigned char *input, size_t input_len,
766 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100768 size_t i, pad_idx;
769 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
Paul Bakkera885d682011-01-20 16:35:05 +0000771 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773
774 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775 *data_len = input_len - padding_len;
776
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100777 /* Avoid logical || since it results in a branch */
778 bad |= padding_len > input_len;
779 bad |= padding_len == 0;
780
781 /* The number of bytes checked must be independent of padding_len,
782 * so pick input_len, which is usually 8 or 16 (one block) */
783 pad_idx = input_len - padding_len;
784 for( i = 0; i < input_len; i++ )
785 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200792/*
793 * One and zeros padding: fill with 80 00 ... 00
794 */
795static void add_one_and_zeros_padding( unsigned char *output,
796 size_t output_len, size_t data_len )
797{
798 size_t padding_len = output_len - data_len;
799 unsigned char i = 0;
800
801 output[data_len] = 0x80;
802 for( i = 1; i < padding_len; i++ )
803 output[data_len + i] = 0x00;
804}
805
806static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
807 size_t *data_len )
808{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100809 size_t i;
810 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200811
812 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200814
Micha Krausba8316f2017-12-23 23:40:08 +0100815 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100816 *data_len = 0;
817 for( i = input_len; i > 0; i-- )
818 {
819 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100820 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100821 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100822 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100823 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200831/*
832 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
833 */
834static void add_zeros_and_len_padding( unsigned char *output,
835 size_t output_len, size_t data_len )
836{
837 size_t padding_len = output_len - data_len;
838 unsigned char i = 0;
839
840 for( i = 1; i < padding_len; i++ )
841 output[data_len + i - 1] = 0x00;
842 output[output_len - 1] = (unsigned char) padding_len;
843}
844
845static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
846 size_t *data_len )
847{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100848 size_t i, pad_idx;
849 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850
851 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200853
854 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200855 *data_len = input_len - padding_len;
856
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100857 /* Avoid logical || since it results in a branch */
858 bad |= padding_len > input_len;
859 bad |= padding_len == 0;
860
861 /* The number of bytes checked must be independent of padding_len */
862 pad_idx = input_len - padding_len;
863 for( i = 0; i < input_len - 1; i++ )
864 bad |= input[i] * ( i >= pad_idx );
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200871/*
872 * Zero padding: fill with 00 ... 00
873 */
874static void add_zeros_padding( unsigned char *output,
875 size_t output_len, size_t data_len )
876{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200877 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200878
879 for( i = data_len; i < output_len; i++ )
880 output[i] = 0x00;
881}
882
883static int get_zeros_padding( unsigned char *input, size_t input_len,
884 size_t *data_len )
885{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100886 size_t i;
887 unsigned char done = 0, prev_done;
888
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200889 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100892 *data_len = 0;
893 for( i = input_len; i > 0; i-- )
894 {
895 prev_done = done;
896 done |= ( input[i-1] != 0 );
897 *data_len |= i * ( done != prev_done );
898 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200899
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200900 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200903
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200904/*
905 * No padding: don't pad :)
906 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200908 * but a trivial get_padding function
909 */
910static int get_no_padding( unsigned char *input, size_t input_len,
911 size_t *data_len )
912{
913 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915
916 *data_len = input_len;
917
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200918 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200923 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000924{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500925 CIPHER_VALIDATE_RET( ctx != NULL );
926 CIPHER_VALIDATE_RET( output != NULL );
927 CIPHER_VALIDATE_RET( olen != NULL );
928 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000930
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000931#if defined(MBEDTLS_USE_PSA_CRYPTO)
932 if( ctx->psa_enabled == 1 )
933 {
934 /* While PSA Crypto has an API for multipart
935 * operations, we currently don't make it
936 * accessible through the cipher layer. */
937 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
938 }
939#endif /* MBEDTLS_USE_PSA_CRYPTO */
940
Paul Bakker8123e9d2011-01-06 15:37:30 +0000941 *olen = 0;
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100944 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
946 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100947 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000949 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200950 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000951 }
952
Daniel King8fe47012016-05-17 20:33:28 -0300953 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
954 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300955 {
956 return( 0 );
957 }
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200960 {
961 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200963
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200964 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200965 }
966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_CIPHER_MODE_CBC)
968 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000969 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200970 int ret = 0;
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000973 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200974 /* check for 'no padding' mode */
975 if( NULL == ctx->add_padding )
976 {
977 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200979
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200980 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200981 }
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000984 ctx->unprocessed_len );
985 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988 /*
989 * For decrypt operations, expect a full block,
990 * or an empty block if no padding
991 */
992 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200993 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 }
997
998 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000999 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001001 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001004 }
1005
1006 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001008 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1009 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001010
1011 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001013 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001015#else
1016 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020}
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001023int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1024 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001025{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001026 CIPHER_VALIDATE_RET( ctx != NULL );
1027
1028 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001031 }
1032
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001033#if defined(MBEDTLS_USE_PSA_CRYPTO)
1034 if( ctx->psa_enabled == 1 )
1035 {
1036 /* While PSA Crypto knows about CBC padding
1037 * schemes, we currently don't make them
1038 * accessible through the cipher layer. */
1039 if( mode != MBEDTLS_PADDING_NONE )
1040 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1041
1042 return( 0 );
1043 }
1044#endif /* MBEDTLS_USE_PSA_CRYPTO */
1045
Paul Bakker1a45d912013-08-14 12:04:26 +02001046 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1049 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001050 ctx->add_padding = add_pkcs_padding;
1051 ctx->get_padding = get_pkcs_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_ONE_AND_ZEROS)
1055 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001056 ctx->add_padding = add_one_and_zeros_padding;
1057 ctx->get_padding = get_one_and_zeros_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_AND_LEN)
1061 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001062 ctx->add_padding = add_zeros_and_len_padding;
1063 ctx->get_padding = get_zeros_and_len_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)
1067 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001068 ctx->add_padding = add_zeros_padding;
1069 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001073 ctx->add_padding = NULL;
1074 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001075 break;
1076
1077 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001079 }
1080
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001081 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001082}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001084
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001085#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001087 unsigned char *tag, size_t tag_len )
1088{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001089 CIPHER_VALIDATE_RET( ctx != NULL );
1090 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1091 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( MBEDTLS_ENCRYPT != ctx->operation )
1095 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001096
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001097#if defined(MBEDTLS_USE_PSA_CRYPTO)
1098 if( ctx->psa_enabled == 1 )
1099 {
1100 /* While PSA Crypto has an API for multipart
1101 * operations, we currently don't make it
1102 * accessible through the cipher layer. */
1103 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001104 }
1105#endif /* MBEDTLS_USE_PSA_CRYPTO */
1106
Daniel King8fe47012016-05-17 20:33:28 -03001107#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001109 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1110 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001111#endif
1112
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001113#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001114 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1115 {
1116 /* Don't allow truncated MAC for Poly1305 */
1117 if ( tag_len != 16U )
1118 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1119
Hanno Becker18597cd2018-11-09 16:36:33 +00001120 return( mbedtls_chachapoly_finish(
1121 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001122 }
1123#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001124
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001125 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001126}
Paul Bakker9af723c2014-05-01 13:03:14 +02001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001129 const unsigned char *tag, size_t tag_len )
1130{
Daniel King8fe47012016-05-17 20:33:28 -03001131 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001133
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001134 CIPHER_VALIDATE_RET( ctx != NULL );
1135 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1136 if( ctx->cipher_info == NULL )
1137 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1138
1139 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001142 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001143
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001144#if defined(MBEDTLS_USE_PSA_CRYPTO)
1145 if( ctx->psa_enabled == 1 )
1146 {
1147 /* While PSA Crypto has an API for multipart
1148 * operations, we currently don't make it
1149 * accessible through the cipher layer. */
1150 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1151 }
1152#endif /* MBEDTLS_USE_PSA_CRYPTO */
1153
Daniel King8fe47012016-05-17 20:33:28 -03001154#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001156 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001157 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001159
Hanno Becker18597cd2018-11-09 16:36:33 +00001160 if( 0 != ( ret = mbedtls_gcm_finish(
1161 (mbedtls_gcm_context *) ctx->cipher_ctx,
1162 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001163 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001164 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001165 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166
1167 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001168 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001170
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001171 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001172 }
Daniel King8fe47012016-05-17 20:33:28 -03001173#endif /* MBEDTLS_GCM_C */
1174
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001175#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001176 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1177 {
1178 /* Don't allow truncated MAC for Poly1305 */
1179 if ( tag_len != sizeof( check_tag ) )
1180 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1181
Hanno Becker18597cd2018-11-09 16:36:33 +00001182 ret = mbedtls_chachapoly_finish(
1183 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001184 if ( ret != 0 )
1185 {
1186 return( ret );
1187 }
1188
1189 /* Check the tag in "constant-time" */
1190 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1191 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1192
1193 return( 0 );
1194 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001195#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001196
1197 return( 0 );
1198}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001199#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001200
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001201/*
1202 * Packet-oriented wrapper for non-AEAD modes
1203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001205 const unsigned char *iv, size_t iv_len,
1206 const unsigned char *input, size_t ilen,
1207 unsigned char *output, size_t *olen )
1208{
Janos Follath24eed8d2019-11-22 13:21:35 +00001209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001210 size_t finish_olen;
1211
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001212 CIPHER_VALIDATE_RET( ctx != NULL );
1213 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1214 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1215 CIPHER_VALIDATE_RET( output != NULL );
1216 CIPHER_VALIDATE_RET( olen != NULL );
1217
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001218#if defined(MBEDTLS_USE_PSA_CRYPTO)
1219 if( ctx->psa_enabled == 1 )
1220 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001221 /* As in the non-PSA case, we don't check that
1222 * a key has been set. If not, the key slot will
1223 * still be in its default state of 0, which is
1224 * guaranteed to be invalid, hence the PSA-call
1225 * below will gracefully fail. */
1226 mbedtls_cipher_context_psa * const cipher_psa =
1227 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1228
1229 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001230 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001231 size_t part_len;
1232
1233 if( ctx->operation == MBEDTLS_DECRYPT )
1234 {
1235 status = psa_cipher_decrypt_setup( &cipher_op,
1236 cipher_psa->slot,
1237 cipher_psa->alg );
1238 }
1239 else if( ctx->operation == MBEDTLS_ENCRYPT )
1240 {
1241 status = psa_cipher_encrypt_setup( &cipher_op,
1242 cipher_psa->slot,
1243 cipher_psa->alg );
1244 }
1245 else
1246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1247
1248 /* In the following, we can immediately return on an error,
1249 * because the PSA Crypto API guarantees that cipher operations
1250 * are terminated by unsuccessful calls to psa_cipher_update(),
1251 * and by any call to psa_cipher_finish(). */
1252 if( status != PSA_SUCCESS )
1253 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1254
1255 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1256 if( status != PSA_SUCCESS )
1257 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1258
1259 status = psa_cipher_update( &cipher_op,
1260 input, ilen,
1261 output, ilen, olen );
1262 if( status != PSA_SUCCESS )
1263 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1264
1265 status = psa_cipher_finish( &cipher_op,
1266 output + *olen, ilen - *olen,
1267 &part_len );
1268 if( status != PSA_SUCCESS )
1269 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1270
1271 *olen += part_len;
1272 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001273 }
1274#endif /* MBEDTLS_USE_PSA_CRYPTO */
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001277 return( ret );
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001280 return( ret );
1281
Hanno Becker18597cd2018-11-09 16:36:33 +00001282 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1283 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001284 return( ret );
1285
Hanno Becker18597cd2018-11-09 16:36:33 +00001286 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1287 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001288 return( ret );
1289
1290 *olen += finish_olen;
1291
1292 return( 0 );
1293}
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001296/*
1297 * Packet-oriented encryption for AEAD modes
1298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001300 const unsigned char *iv, size_t iv_len,
1301 const unsigned char *ad, size_t ad_len,
1302 const unsigned char *input, size_t ilen,
1303 unsigned char *output, size_t *olen,
1304 unsigned char *tag, size_t tag_len )
1305{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001306 CIPHER_VALIDATE_RET( ctx != NULL );
1307 CIPHER_VALIDATE_RET( iv != NULL );
1308 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1309 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1310 CIPHER_VALIDATE_RET( output != NULL );
1311 CIPHER_VALIDATE_RET( olen != NULL );
1312 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1313
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001314#if defined(MBEDTLS_USE_PSA_CRYPTO)
1315 if( ctx->psa_enabled == 1 )
1316 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001317 /* As in the non-PSA case, we don't check that
1318 * a key has been set. If not, the key slot will
1319 * still be in its default state of 0, which is
1320 * guaranteed to be invalid, hence the PSA-call
1321 * below will gracefully fail. */
1322 mbedtls_cipher_context_psa * const cipher_psa =
1323 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1324
1325 psa_status_t status;
1326
1327 /* PSA Crypto API always writes the authentication tag
1328 * at the end of the encrypted message. */
1329 if( tag != output + ilen )
1330 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1331
1332 status = psa_aead_encrypt( cipher_psa->slot,
1333 cipher_psa->alg,
1334 iv, iv_len,
1335 ad, ad_len,
1336 input, ilen,
1337 output, ilen + tag_len, olen );
1338 if( status != PSA_SUCCESS )
1339 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1340
1341 *olen -= tag_len;
1342 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001343 }
1344#endif /* MBEDTLS_USE_PSA_CRYPTO */
1345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_GCM_C)
1347 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001348 {
1349 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001350 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1351 ilen, iv, iv_len, ad, ad_len,
1352 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_GCM_C */
1355#if defined(MBEDTLS_CCM_C)
1356 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001357 {
1358 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001360 iv, iv_len, ad, ad_len, input, output,
1361 tag, tag_len ) );
1362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001364#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001365 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1366 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001367 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001368 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001369 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001370 {
1371 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1372 }
1373
1374 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001375 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001376 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001377 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001378#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001379#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001380 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1381 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001382 {
1383 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1384 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1385
1386 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1387 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1388 {
1389 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1390 }
1391
1392 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1393 }
1394#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001397}
1398
1399/*
1400 * Packet-oriented decryption for AEAD modes
1401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001403 const unsigned char *iv, size_t iv_len,
1404 const unsigned char *ad, size_t ad_len,
1405 const unsigned char *input, size_t ilen,
1406 unsigned char *output, size_t *olen,
1407 const unsigned char *tag, size_t tag_len )
1408{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001409 CIPHER_VALIDATE_RET( ctx != NULL );
1410 CIPHER_VALIDATE_RET( iv != NULL );
1411 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1412 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1413 CIPHER_VALIDATE_RET( output != NULL );
1414 CIPHER_VALIDATE_RET( olen != NULL );
1415 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001416
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001417#if defined(MBEDTLS_USE_PSA_CRYPTO)
1418 if( ctx->psa_enabled == 1 )
1419 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001420 /* As in the non-PSA case, we don't check that
1421 * a key has been set. If not, the key slot will
1422 * still be in its default state of 0, which is
1423 * guaranteed to be invalid, hence the PSA-call
1424 * below will gracefully fail. */
1425 mbedtls_cipher_context_psa * const cipher_psa =
1426 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1427
1428 psa_status_t status;
1429
1430 /* PSA Crypto API always writes the authentication tag
1431 * at the end of the encrypted message. */
1432 if( tag != input + ilen )
1433 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1434
1435 status = psa_aead_decrypt( cipher_psa->slot,
1436 cipher_psa->alg,
1437 iv, iv_len,
1438 ad, ad_len,
1439 input, ilen + tag_len,
1440 output, ilen, olen );
1441 if( status == PSA_ERROR_INVALID_SIGNATURE )
1442 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1443 else if( status != PSA_SUCCESS )
1444 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1445
1446 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001447 }
1448#endif /* MBEDTLS_USE_PSA_CRYPTO */
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_GCM_C)
1451 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001452 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001454
1455 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001457 iv, iv_len, ad, ad_len,
1458 tag, tag_len, input, output );
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1461 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001462
1463 return( ret );
1464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_GCM_C */
1466#if defined(MBEDTLS_CCM_C)
1467 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001468 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001470
1471 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001473 iv, iv_len, ad, ad_len,
1474 input, output, tag, tag_len );
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1477 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001478
1479 return( ret );
1480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001482#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001483 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1484 {
Janos Follath24eed8d2019-11-22 13:21:35 +00001485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001486
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001487 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001488 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001489 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001490 {
1491 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1492 }
1493
1494 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001495 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1496 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001497
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001498 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1499 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001500
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001501 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001502 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001503#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001504#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001505 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1506 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001507 {
1508 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1509 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1510
1511 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1512 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1513 {
1514 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1515 }
1516
1517 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1518 }
1519#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_CIPHER_C */