blob: 4ea0221f4d87ee737f8f848727d244cdb8e16a19 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000028 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Paul Bakker8123e9d2011-01-06 15:37:30 +000049 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020060#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdlib.h>
64#include <string.h>
65
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020066#if defined(MBEDTLS_CHACHAPOLY_C)
67#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020072#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020076#endif
77
Daniel Kingbd920622016-05-15 19:56:20 -030078#if defined(MBEDTLS_CHACHA20_C)
79#include "mbedtls/chacha20.h"
80#endif
81
Simon Butcher327398a2016-10-05 14:09:11 +010082#if defined(MBEDTLS_CMAC_C)
83#include "mbedtls/cmac.h"
84#endif
85
86#if defined(MBEDTLS_PLATFORM_C)
87#include "mbedtls/platform.h"
88#else
89#define mbedtls_calloc calloc
90#define mbedtls_free free
91#endif
92
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010093#define CIPHER_VALIDATE_RET( cond ) \
94 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
95#define CIPHER_VALIDATE( cond ) \
96 MBEDTLS_INTERNAL_VALIDATE( cond )
97
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020098#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030099/* Compare the contents of two buffers in constant time.
100 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -0300101 * a non-zero value.
102 * This is currently only used by GCM and ChaCha20+Poly1305.
103 */
Daniel King8fe47012016-05-17 20:33:28 -0300104static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
105{
106 const unsigned char *p1 = (const unsigned char*) v1;
107 const unsigned char *p2 = (const unsigned char*) v2;
108 size_t i;
109 unsigned char diff;
110
111 for( diff = 0, i = 0; i < len; i++ )
112 diff |= p1[i] ^ p2[i];
113
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100114 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -0300115}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200116#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -0300117
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200118static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200123 int *type;
124
125 if( ! supported_init )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 def = mbedtls_cipher_definitions;
128 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200129
130 while( def->type != 0 )
131 *type++ = (*def++).type;
132
133 *type = 0;
134
135 supported_init = 1;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200146 if( def->type == cipher_type )
147 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000148
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200149 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150}
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200155
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200157 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200160 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200161 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000162
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200163 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200167 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200173 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200174 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200175 def->info->mode == mode )
176 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200177
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100183 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185}
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200188{
189 if( ctx == NULL )
190 return;
191
Simon Butcher327398a2016-10-05 14:09:11 +0100192#if defined(MBEDTLS_CMAC_C)
193 if( ctx->cmac_ctx )
194 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500195 mbedtls_platform_zeroize( ctx->cmac_ctx,
196 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100197 mbedtls_free( ctx->cmac_ctx );
198 }
199#endif
200
Paul Bakker84bbeb52014-07-01 14:53:22 +0200201 if( ctx->cipher_ctx )
202 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
203
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500204 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200205}
206
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200207int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208{
k-stachowiaka5390702018-12-17 11:27:03 +0100209 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100210 if( cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100211 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214
Paul Bakker343a8702011-06-09 14:27:58 +0000215 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217
218 ctx->cipher_info = cipher_info;
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200221 /*
222 * Ignore possible errors caused by a cipher mode that doesn't use padding
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
225 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200226#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200230
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200231 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232}
233
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100234int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
235 const unsigned char *key,
236 int key_bitlen,
237 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238{
k-stachowiaka5390702018-12-17 11:27:03 +0100239 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100240 CIPHER_VALIDATE_RET( key != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100241 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
242 operation == MBEDTLS_DECRYPT );
k-stachowiak95070a82018-12-19 14:48:37 +0100243 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200247 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200250 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200251
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200252 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253 ctx->operation = operation;
254
Paul Bakker343a8702011-06-09 14:27:58 +0000255 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100256 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( MBEDTLS_ENCRYPT == operation ||
259 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100260 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000262 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100263 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
264 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000265 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( MBEDTLS_DECRYPT == operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100268 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
269 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272}
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100275 const unsigned char *iv,
276 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200278 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100279
k-stachowiaka5390702018-12-17 11:27:03 +0100280 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100281 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100282 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100283 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200285 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
287 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200290 actual_iv_size = iv_len;
291 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200292 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200293 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200294
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200295 /* avoid reading past the end of input buffer */
296 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200298 }
299
Daniel Kingbd920622016-05-15 19:56:20 -0300300#if defined(MBEDTLS_CHACHA20_C)
301 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
302 {
303 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
304 iv,
305 0U ) ) /* Initial counter value */
306 {
307 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
308 }
309 }
310#endif
311
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300312 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300313 {
314 memcpy( ctx->iv, iv, actual_iv_size );
315 ctx->iv_size = actual_iv_size;
316 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200317
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200318 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200319}
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200322{
k-stachowiaka5390702018-12-17 11:27:03 +0100323 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100324 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100325 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200326
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 ctx->unprocessed_len = 0;
328
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200329 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200330}
331
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200332#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200334 const unsigned char *ad, size_t ad_len )
335{
k-stachowiaka5390702018-12-17 11:27:03 +0100336 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100337 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100338 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100339 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100340
Daniel King8fe47012016-05-17 20:33:28 -0300341#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200343 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100344 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
345 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200346 }
Daniel King8fe47012016-05-17 20:33:28 -0300347#endif
348
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200349#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300350 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
351 {
352 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200353 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300354
355 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200356 ? MBEDTLS_CHACHAPOLY_ENCRYPT
357 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300358
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200359 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300360 ctx->iv,
361 mode );
362 if ( result != 0 )
363 return( result );
364
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100365 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
366 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300367 }
368#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200369
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200370 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200372#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200375 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376{
Paul Bakkerff61a782011-06-09 15:42:02 +0000377 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100378 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379
k-stachowiaka5390702018-12-17 11:27:03 +0100380 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100381 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100382 CIPHER_VALIDATE_RET( output != NULL );
383 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100384 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100385 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100386
Paul Bakker6c212762013-12-16 15:24:50 +0100387 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100388 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskine83a56722020-01-21 15:02:14 +0100389 if ( 0 == block_size )
390 {
391 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
392 }
Paul Bakker6c212762013-12-16 15:24:50 +0100393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200395 {
Janos Follath98e28a72016-05-31 14:03:54 +0100396 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200398
399 *olen = ilen;
400
401 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
402 ctx->operation, input, output ) ) )
403 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200404 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200405 }
406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200408 }
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_GCM_C)
411 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200412 {
413 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100414 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
415 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200416 }
417#endif
418
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200419#if defined(MBEDTLS_CHACHAPOLY_C)
420 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
421 {
422 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100423 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
424 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200425 }
426#endif
427
Paul Bakker68884e32013-01-07 18:20:04 +0100428 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100429 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100432 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#if defined(MBEDTLS_CIPHER_MODE_CBC)
435 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200437 size_t copy_len = 0;
438
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439 /*
440 * If there is not enough data for a full block, cache it.
441 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700442 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000443 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700444 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
445 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000447 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448 {
449 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
450 ilen );
451
452 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200453 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 }
455
456 /*
457 * Process cached data first
458 */
Janos Follath98e28a72016-05-31 14:03:54 +0100459 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000460 {
Janos Follath98e28a72016-05-31 14:03:54 +0100461 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462
463 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
464 copy_len );
465
Paul Bakkerff61a782011-06-09 15:42:02 +0000466 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100467 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000468 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200470 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 }
472
Janos Follath98e28a72016-05-31 14:03:54 +0100473 *olen += block_size;
474 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475 ctx->unprocessed_len = 0;
476
477 input += copy_len;
478 ilen -= copy_len;
479 }
480
481 /*
482 * Cache final, incomplete block
483 */
484 if( 0 != ilen )
485 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700486 /* Encryption: only cache partial blocks
487 * Decryption w/ padding: always keep at least one whole block
488 * Decryption w/o padding: only cache partial blocks
489 */
Janos Follath98e28a72016-05-31 14:03:54 +0100490 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700491 if( copy_len == 0 &&
492 ctx->operation == MBEDTLS_DECRYPT &&
493 NULL != ctx->add_padding)
494 {
Janos Follath98e28a72016-05-31 14:03:54 +0100495 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700496 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
498 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
499 copy_len );
500
501 ctx->unprocessed_len += copy_len;
502 ilen -= copy_len;
503 }
504
505 /*
506 * Process remaining full blocks
507 */
508 if( ilen )
509 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000510 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
511 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200513 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200515
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516 *olen += ilen;
517 }
518
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_CIPHER_MODE_CFB)
524 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000525 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000526 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000527 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000528 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000529 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200530 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000531 }
532
533 *olen = ilen;
534
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000538
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100539#if defined(MBEDTLS_CIPHER_MODE_OFB)
540 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
541 {
542 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
543 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
544 {
545 return( ret );
546 }
547
548 *olen = ilen;
549
550 return( 0 );
551 }
552#endif /* MBEDTLS_CIPHER_MODE_OFB */
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_CIPHER_MODE_CTR)
555 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000556 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000557 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000558 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000559 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000560 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200561 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000562 }
563
564 *olen = ilen;
565
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200566 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000569
Jaeden Ameroc6539902018-04-30 17:17:41 +0100570#if defined(MBEDTLS_CIPHER_MODE_XTS)
571 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
572 {
573 if( ctx->unprocessed_len > 0 ) {
574 /* We can only process an entire data unit at a time. */
575 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
576 }
577
578 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
579 ctx->operation, ilen, ctx->iv, input, output );
580 if( ret != 0 )
581 {
582 return( ret );
583 }
584
585 *olen = ilen;
586
587 return( 0 );
588 }
589#endif /* MBEDTLS_CIPHER_MODE_XTS */
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_CIPHER_MODE_STREAM)
592 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200593 {
594 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
595 ilen, input, output ) ) )
596 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200597 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200598 }
599
600 *olen = ilen;
601
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607}
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
610#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200611/*
612 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
613 */
Paul Bakker23986e52011-04-24 08:57:21 +0000614static void add_pkcs_padding( unsigned char *output, size_t output_len,
615 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616{
Paul Bakker23986e52011-04-24 08:57:21 +0000617 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100618 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000619
620 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000621 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622}
623
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200624static int get_pkcs_padding( unsigned char *input, size_t input_len,
625 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100627 size_t i, pad_idx;
628 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629
Paul Bakkera885d682011-01-20 16:35:05 +0000630 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
633 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634 *data_len = input_len - padding_len;
635
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100636 /* Avoid logical || since it results in a branch */
637 bad |= padding_len > input_len;
638 bad |= padding_len == 0;
639
640 /* The number of bytes checked must be independent of padding_len,
641 * so pick input_len, which is usually 8 or 16 (one block) */
642 pad_idx = input_len - padding_len;
643 for( i = 0; i < input_len; i++ )
644 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200651/*
652 * One and zeros padding: fill with 80 00 ... 00
653 */
654static void add_one_and_zeros_padding( unsigned char *output,
655 size_t output_len, size_t data_len )
656{
657 size_t padding_len = output_len - data_len;
658 unsigned char i = 0;
659
660 output[data_len] = 0x80;
661 for( i = 1; i < padding_len; i++ )
662 output[data_len + i] = 0x00;
663}
664
665static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
666 size_t *data_len )
667{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100668 size_t i;
669 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200670
671 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200673
Micha Krausba8316f2017-12-23 23:40:08 +0100674 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100675 *data_len = 0;
676 for( i = input_len; i > 0; i-- )
677 {
678 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100679 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100680 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100681 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100682 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200685
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200690/*
691 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
692 */
693static void add_zeros_and_len_padding( unsigned char *output,
694 size_t output_len, size_t data_len )
695{
696 size_t padding_len = output_len - data_len;
697 unsigned char i = 0;
698
699 for( i = 1; i < padding_len; i++ )
700 output[data_len + i - 1] = 0x00;
701 output[output_len - 1] = (unsigned char) padding_len;
702}
703
704static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
705 size_t *data_len )
706{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100707 size_t i, pad_idx;
708 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200709
710 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200712
713 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200714 *data_len = input_len - padding_len;
715
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100716 /* Avoid logical || since it results in a branch */
717 bad |= padding_len > input_len;
718 bad |= padding_len == 0;
719
720 /* The number of bytes checked must be independent of padding_len */
721 pad_idx = input_len - padding_len;
722 for( i = 0; i < input_len - 1; i++ )
723 bad |= input[i] * ( i >= pad_idx );
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200730/*
731 * Zero padding: fill with 00 ... 00
732 */
733static void add_zeros_padding( unsigned char *output,
734 size_t output_len, size_t data_len )
735{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200737
738 for( i = data_len; i < output_len; i++ )
739 output[i] = 0x00;
740}
741
742static int get_zeros_padding( unsigned char *input, size_t input_len,
743 size_t *data_len )
744{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100745 size_t i;
746 unsigned char done = 0, prev_done;
747
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200748 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200750
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100751 *data_len = 0;
752 for( i = input_len; i > 0; i-- )
753 {
754 prev_done = done;
755 done |= ( input[i-1] != 0 );
756 *data_len |= i * ( done != prev_done );
757 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200758
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200759 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200762
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200763/*
764 * No padding: don't pad :)
765 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200767 * but a trivial get_padding function
768 */
769static int get_no_padding( unsigned char *input, size_t input_len,
770 size_t *data_len )
771{
772 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200774
775 *data_len = input_len;
776
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200777 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200782 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783{
k-stachowiaka5390702018-12-17 11:27:03 +0100784 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100785 CIPHER_VALIDATE_RET( output != NULL );
786 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100787 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100788 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100789
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790 *olen = 0;
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100793 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
795 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100796 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000798 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200799 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000800 }
801
Daniel King8fe47012016-05-17 20:33:28 -0300802 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
803 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300804 {
805 return( 0 );
806 }
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200809 {
810 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200812
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200813 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200814 }
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#if defined(MBEDTLS_CIPHER_MODE_CBC)
817 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000818 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200819 int ret = 0;
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000822 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200823 /* check for 'no padding' mode */
824 if( NULL == ctx->add_padding )
825 {
826 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200828
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200829 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200830 }
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000833 ctx->unprocessed_len );
834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000836 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200837 /*
838 * For decrypt operations, expect a full block,
839 * or an empty block if no padding
840 */
841 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200842 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845 }
846
847 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000848 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000850 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200852 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000853 }
854
855 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( MBEDTLS_DECRYPT == ctx->operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100857 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
858 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000859
860 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200862 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200864#else
865 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000869}
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100872int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
873 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200874{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100875 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100876
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100877 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200880 }
881
Paul Bakker1a45d912013-08-14 12:04:26 +0200882 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
885 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200886 ctx->add_padding = add_pkcs_padding;
887 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200888 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
891 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200892 ctx->add_padding = add_one_and_zeros_padding;
893 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200894 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
897 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200898 ctx->add_padding = add_zeros_and_len_padding;
899 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200900 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
903 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200904 ctx->add_padding = add_zeros_padding;
905 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200906 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200909 ctx->add_padding = NULL;
910 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200911 break;
912
913 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915 }
916
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200917 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200920
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200921#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200923 unsigned char *tag, size_t tag_len )
924{
k-stachowiaka5390702018-12-17 11:27:03 +0100925 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100926 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100927 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100928 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( MBEDTLS_ENCRYPT != ctx->operation )
931 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200932
Daniel King8fe47012016-05-17 20:33:28 -0300933#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100935 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
936 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300937#endif
938
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200939#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300940 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
941 {
942 /* Don't allow truncated MAC for Poly1305 */
943 if ( tag_len != 16U )
944 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
945
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100946 return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
947 tag ) );
Daniel King8fe47012016-05-17 20:33:28 -0300948 }
949#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200950
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200951 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200952}
Paul Bakker9af723c2014-05-01 13:03:14 +0200953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200955 const unsigned char *tag, size_t tag_len )
956{
Daniel King8fe47012016-05-17 20:33:28 -0300957 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200958 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200959
k-stachowiaka5390702018-12-17 11:27:03 +0100960 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100961 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100962 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100963 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100964
k-stachowiaka5390702018-12-17 11:27:03 +0100965 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200968 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200969
Gilles Peskine8dc7b242021-12-13 12:32:43 +0100970 /* Status to return on a non-authenticated algorithm. It would make sense
971 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
972 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
973 * unit tests assume 0. */
974 ret = 0;
975
Daniel King8fe47012016-05-17 20:33:28 -0300976#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200978 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200979 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200983 check_tag, tag_len ) ) )
984 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200985 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200986 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200987
988 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300989 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskineb3f22732021-12-13 16:57:47 +0100990 {
Gilles Peskine8dc7b242021-12-13 12:32:43 +0100991 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskineb3f22732021-12-13 16:57:47 +0100992 goto exit;
993 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200994 }
Daniel King8fe47012016-05-17 20:33:28 -0300995#endif /* MBEDTLS_GCM_C */
996
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200997#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300998 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
999 {
1000 /* Don't allow truncated MAC for Poly1305 */
1001 if ( tag_len != sizeof( check_tag ) )
1002 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1003
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001004 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -03001005 check_tag );
1006 if ( ret != 0 )
1007 {
1008 return( ret );
1009 }
1010
1011 /* Check the tag in "constant-time" */
1012 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Gilles Peskineb3f22732021-12-13 16:57:47 +01001013 {
Gilles Peskine8dc7b242021-12-13 12:32:43 +01001014 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskineb3f22732021-12-13 16:57:47 +01001015 goto exit;
1016 }
Daniel King8fe47012016-05-17 20:33:28 -03001017 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001018#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001019
Gilles Peskineb3f22732021-12-13 16:57:47 +01001020exit:
Gilles Peskine8dc7b242021-12-13 12:32:43 +01001021 mbedtls_platform_zeroize( check_tag, tag_len );
1022 return( ret );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001023}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001024#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001025
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001026/*
1027 * Packet-oriented wrapper for non-AEAD modes
1028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001030 const unsigned char *iv, size_t iv_len,
1031 const unsigned char *input, size_t ilen,
1032 unsigned char *output, size_t *olen )
1033{
1034 int ret;
1035 size_t finish_olen;
1036
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001037 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001038 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001039 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001040 CIPHER_VALIDATE_RET( output != NULL );
1041 CIPHER_VALIDATE_RET( olen != NULL );
1042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001044 return( ret );
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001047 return( ret );
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001050 return( ret );
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001053 return( ret );
1054
1055 *olen += finish_olen;
1056
1057 return( 0 );
1058}
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001061/*
1062 * Packet-oriented encryption for AEAD modes
1063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001065 const unsigned char *iv, size_t iv_len,
1066 const unsigned char *ad, size_t ad_len,
1067 const unsigned char *input, size_t ilen,
1068 unsigned char *output, size_t *olen,
1069 unsigned char *tag, size_t tag_len )
1070{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001071 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001072 CIPHER_VALIDATE_RET( iv != NULL );
1073 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001074 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001075 CIPHER_VALIDATE_RET( output != NULL );
1076 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001077 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_GCM_C)
1080 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001081 {
1082 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001084 iv, iv_len, ad, ad_len, input, output,
1085 tag_len, tag ) );
1086 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_GCM_C */
1088#if defined(MBEDTLS_CCM_C)
1089 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001090 {
1091 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001093 iv, iv_len, ad, ad_len, input, output,
1094 tag, tag_len ) );
1095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001097#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001098 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1099 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001100 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001101 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001102 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001103 {
1104 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1105 }
1106
1107 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001108 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001109 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001110 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001111#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001114}
1115
1116/*
1117 * Packet-oriented decryption for AEAD modes
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001120 const unsigned char *iv, size_t iv_len,
1121 const unsigned char *ad, size_t ad_len,
1122 const unsigned char *input, size_t ilen,
1123 unsigned char *output, size_t *olen,
1124 const unsigned char *tag, size_t tag_len )
1125{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001126 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001127 CIPHER_VALIDATE_RET( iv != NULL );
1128 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001129 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001130 CIPHER_VALIDATE_RET( output != NULL );
1131 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001132 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_GCM_C)
1135 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001136 {
1137 int ret;
1138
1139 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001141 iv, iv_len, ad, ad_len,
1142 tag, tag_len, input, output );
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1145 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001146
1147 return( ret );
1148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#endif /* MBEDTLS_GCM_C */
1150#if defined(MBEDTLS_CCM_C)
1151 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001152 {
1153 int ret;
1154
1155 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001157 iv, iv_len, ad, ad_len,
1158 input, output, tag, tag_len );
1159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1161 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001162
1163 return( ret );
1164 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001166#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001167 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1168 {
Daniel King8fe47012016-05-17 20:33:28 -03001169 int ret;
1170
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001171 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001172 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001173 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001174 {
1175 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1176 }
1177
1178 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001179 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1180 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001181
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001182 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1183 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001184
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001185 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001186 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001187#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_CIPHER_C */