blob: 896ec8ec661aecf657f63acf34bc993113aff08a [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
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 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000051 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020062#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <stdlib.h>
66#include <string.h>
67
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020068#if defined(MBEDTLS_CHACHAPOLY_C)
69#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020078#endif
79
Daniel Kingbd920622016-05-15 19:56:20 -030080#if defined(MBEDTLS_CHACHA20_C)
81#include "mbedtls/chacha20.h"
82#endif
83
Simon Butcher327398a2016-10-05 14:09:11 +010084#if defined(MBEDTLS_CMAC_C)
85#include "mbedtls/cmac.h"
86#endif
87
88#if defined(MBEDTLS_PLATFORM_C)
89#include "mbedtls/platform.h"
90#else
91#define mbedtls_calloc calloc
92#define mbedtls_free free
93#endif
94
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010095#define CIPHER_VALIDATE_RET( cond ) \
96 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
97#define CIPHER_VALIDATE( cond ) \
98 MBEDTLS_INTERNAL_VALIDATE( cond )
99
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200100#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300101/* Compare the contents of two buffers in constant time.
102 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -0300103 * a non-zero value.
104 * This is currently only used by GCM and ChaCha20+Poly1305.
105 */
Daniel King8fe47012016-05-17 20:33:28 -0300106static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
107{
108 const unsigned char *p1 = (const unsigned char*) v1;
109 const unsigned char *p2 = (const unsigned char*) v2;
110 size_t i;
111 unsigned char diff;
112
113 for( diff = 0, i = 0; i < len; i++ )
114 diff |= p1[i] ^ p2[i];
115
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100116 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -0300117}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200118#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -0300119
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200120static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200125 int *type;
126
127 if( ! supported_init )
128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 def = mbedtls_cipher_definitions;
130 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200131
132 while( def->type != 0 )
133 *type++ = (*def++).type;
134
135 *type = 0;
136
137 supported_init = 1;
138 }
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200148 if( def->type == cipher_type )
149 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000150
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200151 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200157
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200159 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200162 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200163 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000164
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200165 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166}
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168const 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 +0200169 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200175 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200176 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200177 def->info->mode == mode )
178 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200179
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200180 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200184{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100185 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187}
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190{
191 if( ctx == NULL )
192 return;
193
Simon Butcher327398a2016-10-05 14:09:11 +0100194#if defined(MBEDTLS_CMAC_C)
195 if( ctx->cmac_ctx )
196 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500197 mbedtls_platform_zeroize( ctx->cmac_ctx,
198 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100199 mbedtls_free( ctx->cmac_ctx );
200 }
201#endif
202
Paul Bakker84bbeb52014-07-01 14:53:22 +0200203 if( ctx->cipher_ctx )
204 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
205
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500206 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200207}
208
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200209int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210{
k-stachowiaka5390702018-12-17 11:27:03 +0100211 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100212 if( cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100213 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216
Paul Bakker343a8702011-06-09 14:27:58 +0000217 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219
220 ctx->cipher_info = cipher_info;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200223 /*
224 * Ignore possible errors caused by a cipher mode that doesn't use padding
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
227 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200228#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200232
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200233 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234}
235
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100236int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
237 const unsigned char *key,
238 int key_bitlen,
239 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000240{
k-stachowiaka5390702018-12-17 11:27:03 +0100241 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100242 CIPHER_VALIDATE_RET( key != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100243 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
244 operation == MBEDTLS_DECRYPT );
k-stachowiak95070a82018-12-19 14:48:37 +0100245 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200249 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200252 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200253
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200254 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255 ctx->operation = operation;
256
Paul Bakker343a8702011-06-09 14:27:58 +0000257 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100258 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( MBEDTLS_ENCRYPT == operation ||
261 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100262 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000264 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100265 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
266 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000267 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( MBEDTLS_DECRYPT == operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100270 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
271 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274}
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100277 const unsigned char *iv,
278 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200280 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100281
k-stachowiaka5390702018-12-17 11:27:03 +0100282 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100283 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100284 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100285 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200287 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
289 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200292 actual_iv_size = iv_len;
293 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200294 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200295 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200296
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200297 /* avoid reading past the end of input buffer */
298 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200300 }
301
Daniel Kingbd920622016-05-15 19:56:20 -0300302#if defined(MBEDTLS_CHACHA20_C)
303 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
304 {
305 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
306 iv,
307 0U ) ) /* Initial counter value */
308 {
309 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
310 }
311 }
312#endif
313
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300314 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300315 {
316 memcpy( ctx->iv, iv, actual_iv_size );
317 ctx->iv_size = actual_iv_size;
318 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200319
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200320 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200321}
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200324{
k-stachowiaka5390702018-12-17 11:27:03 +0100325 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100326 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100327 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200328
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329 ctx->unprocessed_len = 0;
330
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200331 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200332}
333
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200334#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200336 const unsigned char *ad, size_t ad_len )
337{
k-stachowiaka5390702018-12-17 11:27:03 +0100338 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100339 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100340 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100341 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100342
Daniel King8fe47012016-05-17 20:33:28 -0300343#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200345 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100346 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
347 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200348 }
Daniel King8fe47012016-05-17 20:33:28 -0300349#endif
350
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200351#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300352 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
353 {
354 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200355 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300356
357 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200358 ? MBEDTLS_CHACHAPOLY_ENCRYPT
359 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300360
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200361 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300362 ctx->iv,
363 mode );
364 if ( result != 0 )
365 return( result );
366
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100367 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
368 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300369 }
370#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200371
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200374#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200377 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378{
Paul Bakkerff61a782011-06-09 15:42:02 +0000379 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100380 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381
k-stachowiaka5390702018-12-17 11:27:03 +0100382 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100383 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100384 CIPHER_VALIDATE_RET( output != NULL );
385 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100386 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100387 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100388
Paul Bakker6c212762013-12-16 15:24:50 +0100389 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100390 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskine83a56722020-01-21 15:02:14 +0100391 if ( 0 == block_size )
392 {
393 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
394 }
Paul Bakker6c212762013-12-16 15:24:50 +0100395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200397 {
Janos Follath98e28a72016-05-31 14:03:54 +0100398 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200400
401 *olen = ilen;
402
403 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
404 ctx->operation, input, output ) ) )
405 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200407 }
408
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200409 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200410 }
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_GCM_C)
413 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200414 {
415 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100416 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
417 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200418 }
419#endif
420
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200421#if defined(MBEDTLS_CHACHAPOLY_C)
422 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
423 {
424 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100425 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
426 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200427 }
428#endif
429
Paul Bakker68884e32013-01-07 18:20:04 +0100430 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100431 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100434 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_CIPHER_MODE_CBC)
437 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200439 size_t copy_len = 0;
440
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 /*
442 * If there is not enough data for a full block, cache it.
443 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700444 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000445 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700446 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
447 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000449 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 {
451 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
452 ilen );
453
454 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200455 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456 }
457
458 /*
459 * Process cached data first
460 */
Janos Follath98e28a72016-05-31 14:03:54 +0100461 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462 {
Janos Follath98e28a72016-05-31 14:03:54 +0100463 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464
465 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
466 copy_len );
467
Paul Bakkerff61a782011-06-09 15:42:02 +0000468 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100469 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000470 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200472 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473 }
474
Janos Follath98e28a72016-05-31 14:03:54 +0100475 *olen += block_size;
476 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477 ctx->unprocessed_len = 0;
478
479 input += copy_len;
480 ilen -= copy_len;
481 }
482
483 /*
484 * Cache final, incomplete block
485 */
486 if( 0 != ilen )
487 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700488 /* Encryption: only cache partial blocks
489 * Decryption w/ padding: always keep at least one whole block
490 * Decryption w/o padding: only cache partial blocks
491 */
Janos Follath98e28a72016-05-31 14:03:54 +0100492 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700493 if( copy_len == 0 &&
494 ctx->operation == MBEDTLS_DECRYPT &&
495 NULL != ctx->add_padding)
496 {
Janos Follath98e28a72016-05-31 14:03:54 +0100497 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700498 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499
500 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
501 copy_len );
502
503 ctx->unprocessed_len += copy_len;
504 ilen -= copy_len;
505 }
506
507 /*
508 * Process remaining full blocks
509 */
510 if( ilen )
511 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000512 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
513 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200515 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200517
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518 *olen += ilen;
519 }
520
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200521 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_CIPHER_MODE_CFB)
526 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000527 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000528 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000529 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000530 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000531 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200532 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000533 }
534
535 *olen = ilen;
536
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200537 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000538 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000540
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100541#if defined(MBEDTLS_CIPHER_MODE_OFB)
542 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
543 {
544 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
545 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
546 {
547 return( ret );
548 }
549
550 *olen = ilen;
551
552 return( 0 );
553 }
554#endif /* MBEDTLS_CIPHER_MODE_OFB */
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_CIPHER_MODE_CTR)
557 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000558 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000559 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000560 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000561 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000562 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200563 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000564 }
565
566 *olen = ilen;
567
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200568 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000571
Jaeden Ameroc6539902018-04-30 17:17:41 +0100572#if defined(MBEDTLS_CIPHER_MODE_XTS)
573 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
574 {
575 if( ctx->unprocessed_len > 0 ) {
576 /* We can only process an entire data unit at a time. */
577 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
578 }
579
580 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
581 ctx->operation, ilen, ctx->iv, input, output );
582 if( ret != 0 )
583 {
584 return( ret );
585 }
586
587 *olen = ilen;
588
589 return( 0 );
590 }
591#endif /* MBEDTLS_CIPHER_MODE_XTS */
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_CIPHER_MODE_STREAM)
594 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200595 {
596 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
597 ilen, input, output ) ) )
598 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200599 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200600 }
601
602 *olen = ilen;
603
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200604 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609}
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
612#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200613/*
614 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
615 */
Paul Bakker23986e52011-04-24 08:57:21 +0000616static void add_pkcs_padding( unsigned char *output, size_t output_len,
617 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618{
Paul Bakker23986e52011-04-24 08:57:21 +0000619 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100620 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621
622 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000623 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624}
625
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200626static int get_pkcs_padding( unsigned char *input, size_t input_len,
627 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100629 size_t i, pad_idx;
630 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631
Paul Bakkera885d682011-01-20 16:35:05 +0000632 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634
635 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 *data_len = input_len - padding_len;
637
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100638 /* Avoid logical || since it results in a branch */
639 bad |= padding_len > input_len;
640 bad |= padding_len == 0;
641
642 /* The number of bytes checked must be independent of padding_len,
643 * so pick input_len, which is usually 8 or 16 (one block) */
644 pad_idx = input_len - padding_len;
645 for( i = 0; i < input_len; i++ )
646 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200653/*
654 * One and zeros padding: fill with 80 00 ... 00
655 */
656static void add_one_and_zeros_padding( unsigned char *output,
657 size_t output_len, size_t data_len )
658{
659 size_t padding_len = output_len - data_len;
660 unsigned char i = 0;
661
662 output[data_len] = 0x80;
663 for( i = 1; i < padding_len; i++ )
664 output[data_len + i] = 0x00;
665}
666
667static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
668 size_t *data_len )
669{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100670 size_t i;
671 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200672
673 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200675
Micha Krausba8316f2017-12-23 23:40:08 +0100676 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100677 *data_len = 0;
678 for( i = input_len; i > 0; i-- )
679 {
680 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100681 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100682 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100683 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100684 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200687
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200692/*
693 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
694 */
695static void add_zeros_and_len_padding( unsigned char *output,
696 size_t output_len, size_t data_len )
697{
698 size_t padding_len = output_len - data_len;
699 unsigned char i = 0;
700
701 for( i = 1; i < padding_len; i++ )
702 output[data_len + i - 1] = 0x00;
703 output[output_len - 1] = (unsigned char) padding_len;
704}
705
706static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
707 size_t *data_len )
708{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100709 size_t i, pad_idx;
710 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200711
712 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200714
715 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200716 *data_len = input_len - padding_len;
717
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100718 /* Avoid logical || since it results in a branch */
719 bad |= padding_len > input_len;
720 bad |= padding_len == 0;
721
722 /* The number of bytes checked must be independent of padding_len */
723 pad_idx = input_len - padding_len;
724 for( i = 0; i < input_len - 1; i++ )
725 bad |= input[i] * ( i >= pad_idx );
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200728}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200732/*
733 * Zero padding: fill with 00 ... 00
734 */
735static void add_zeros_padding( unsigned char *output,
736 size_t output_len, size_t data_len )
737{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200739
740 for( i = data_len; i < output_len; i++ )
741 output[i] = 0x00;
742}
743
744static int get_zeros_padding( unsigned char *input, size_t input_len,
745 size_t *data_len )
746{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100747 size_t i;
748 unsigned char done = 0, prev_done;
749
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200750 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200752
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100753 *data_len = 0;
754 for( i = input_len; i > 0; i-- )
755 {
756 prev_done = done;
757 done |= ( input[i-1] != 0 );
758 *data_len |= i * ( done != prev_done );
759 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200760
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200761 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200764
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200765/*
766 * No padding: don't pad :)
767 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200769 * but a trivial get_padding function
770 */
771static int get_no_padding( unsigned char *input, size_t input_len,
772 size_t *data_len )
773{
774 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200776
777 *data_len = input_len;
778
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200779 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200784 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785{
k-stachowiaka5390702018-12-17 11:27:03 +0100786 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100787 CIPHER_VALIDATE_RET( output != NULL );
788 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100789 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100790 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100791
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 *olen = 0;
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100795 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
797 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100798 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000800 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200801 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000802 }
803
Daniel King8fe47012016-05-17 20:33:28 -0300804 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
805 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300806 {
807 return( 0 );
808 }
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200811 {
812 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200814
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200815 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200816 }
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_CIPHER_MODE_CBC)
819 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000820 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200821 int ret = 0;
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000824 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200825 /* check for 'no padding' mode */
826 if( NULL == ctx->add_padding )
827 {
828 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200830
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200831 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200832 }
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000835 ctx->unprocessed_len );
836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000838 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200839 /*
840 * For decrypt operations, expect a full block,
841 * or an empty block if no padding
842 */
843 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200844 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847 }
848
849 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000850 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000852 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000853 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200854 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000855 }
856
857 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( MBEDTLS_DECRYPT == ctx->operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100859 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
860 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000861
862 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200864 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000865 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200866#else
867 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000871}
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100874int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
875 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200876{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100877 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100878
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100879 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200882 }
883
Paul Bakker1a45d912013-08-14 12:04:26 +0200884 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
887 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200888 ctx->add_padding = add_pkcs_padding;
889 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200890 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200891#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
893 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200894 ctx->add_padding = add_one_and_zeros_padding;
895 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200896 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
899 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200900 ctx->add_padding = add_zeros_and_len_padding;
901 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200902 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
905 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200906 ctx->add_padding = add_zeros_padding;
907 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200908 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200911 ctx->add_padding = NULL;
912 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200913 break;
914
915 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200917 }
918
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200919 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200922
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200923#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200925 unsigned char *tag, size_t tag_len )
926{
k-stachowiaka5390702018-12-17 11:27:03 +0100927 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100928 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100929 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100930 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( MBEDTLS_ENCRYPT != ctx->operation )
933 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200934
Daniel King8fe47012016-05-17 20:33:28 -0300935#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100937 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
938 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300939#endif
940
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200941#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300942 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
943 {
944 /* Don't allow truncated MAC for Poly1305 */
945 if ( tag_len != 16U )
946 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
947
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100948 return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
949 tag ) );
Daniel King8fe47012016-05-17 20:33:28 -0300950 }
951#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200952
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200953 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200954}
Paul Bakker9af723c2014-05-01 13:03:14 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200957 const unsigned char *tag, size_t tag_len )
958{
Daniel King8fe47012016-05-17 20:33:28 -0300959 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200960 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200961
k-stachowiaka5390702018-12-17 11:27:03 +0100962 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100963 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100964 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100965 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100966
k-stachowiaka5390702018-12-17 11:27:03 +0100967 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200970 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200971
Daniel King8fe47012016-05-17 20:33:28 -0300972#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200974 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200975 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200979 check_tag, tag_len ) ) )
980 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200981 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200983
984 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300985 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200987
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200988 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200989 }
Daniel King8fe47012016-05-17 20:33:28 -0300990#endif /* MBEDTLS_GCM_C */
991
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200992#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300993 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
994 {
995 /* Don't allow truncated MAC for Poly1305 */
996 if ( tag_len != sizeof( check_tag ) )
997 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
998
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200999 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -03001000 check_tag );
1001 if ( ret != 0 )
1002 {
1003 return( ret );
1004 }
1005
1006 /* Check the tag in "constant-time" */
1007 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1008 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1009
1010 return( 0 );
1011 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001012#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001013
1014 return( 0 );
1015}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001016#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001017
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001018/*
1019 * Packet-oriented wrapper for non-AEAD modes
1020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001022 const unsigned char *iv, size_t iv_len,
1023 const unsigned char *input, size_t ilen,
1024 unsigned char *output, size_t *olen )
1025{
1026 int ret;
1027 size_t finish_olen;
1028
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001029 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001030 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001031 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001032 CIPHER_VALIDATE_RET( output != NULL );
1033 CIPHER_VALIDATE_RET( olen != NULL );
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001036 return( ret );
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001039 return( ret );
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001042 return( ret );
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001045 return( ret );
1046
1047 *olen += finish_olen;
1048
1049 return( 0 );
1050}
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001053/*
1054 * Packet-oriented encryption for AEAD modes
1055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001057 const unsigned char *iv, size_t iv_len,
1058 const unsigned char *ad, size_t ad_len,
1059 const unsigned char *input, size_t ilen,
1060 unsigned char *output, size_t *olen,
1061 unsigned char *tag, size_t tag_len )
1062{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001063 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001064 CIPHER_VALIDATE_RET( iv != NULL );
1065 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001066 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001067 CIPHER_VALIDATE_RET( output != NULL );
1068 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001069 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_GCM_C)
1072 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001073 {
1074 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001076 iv, iv_len, ad, ad_len, input, output,
1077 tag_len, tag ) );
1078 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#endif /* MBEDTLS_GCM_C */
1080#if defined(MBEDTLS_CCM_C)
1081 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001082 {
1083 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001085 iv, iv_len, ad, ad_len, input, output,
1086 tag, tag_len ) );
1087 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001089#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001090 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1091 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001092 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001093 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001094 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001095 {
1096 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1097 }
1098
1099 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001100 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001101 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001102 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001103#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001106}
1107
1108/*
1109 * Packet-oriented decryption for AEAD modes
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001112 const unsigned char *iv, size_t iv_len,
1113 const unsigned char *ad, size_t ad_len,
1114 const unsigned char *input, size_t ilen,
1115 unsigned char *output, size_t *olen,
1116 const unsigned char *tag, size_t tag_len )
1117{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001118 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001119 CIPHER_VALIDATE_RET( iv != NULL );
1120 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001121 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001122 CIPHER_VALIDATE_RET( output != NULL );
1123 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001124 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_GCM_C)
1127 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001128 {
1129 int ret;
1130
1131 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001133 iv, iv_len, ad, ad_len,
1134 tag, tag_len, input, output );
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1137 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001138
1139 return( ret );
1140 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#endif /* MBEDTLS_GCM_C */
1142#if defined(MBEDTLS_CCM_C)
1143 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001144 {
1145 int ret;
1146
1147 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001149 iv, iv_len, ad, ad_len,
1150 input, output, tag, tag_len );
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1153 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001154
1155 return( ret );
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001158#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001159 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1160 {
Daniel King8fe47012016-05-17 20:33:28 -03001161 int ret;
1162
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001163 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001164 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001165 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001166 {
1167 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1168 }
1169
1170 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001171 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1172 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001173
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001174 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1175 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001176
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001177 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001178 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001179#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#endif /* MBEDTLS_CIPHER_C */