blob: e0fd39dc931e7adc15760b102a8187202600c643 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020041#if defined(MBEDTLS_CHACHAPOLY_C)
42#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020051#endif
52
Daniel Kingbd920622016-05-15 19:56:20 -030053#if defined(MBEDTLS_CHACHA20_C)
54#include "mbedtls/chacha20.h"
55#endif
56
Simon Butcher327398a2016-10-05 14:09:11 +010057#if defined(MBEDTLS_CMAC_C)
58#include "mbedtls/cmac.h"
59#endif
60
61#if defined(MBEDTLS_PLATFORM_C)
62#include "mbedtls/platform.h"
63#else
64#define mbedtls_calloc calloc
65#define mbedtls_free free
66#endif
67
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010068#define CIPHER_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
70#define CIPHER_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020073#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030074/* Compare the contents of two buffers in constant time.
75 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030076 * a non-zero value.
77 * This is currently only used by GCM and ChaCha20+Poly1305.
78 */
Daniel King8fe47012016-05-17 20:33:28 -030079static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
80{
81 const unsigned char *p1 = (const unsigned char*) v1;
82 const unsigned char *p2 = (const unsigned char*) v2;
83 size_t i;
84 unsigned char diff;
85
86 for( diff = 0, i = 0; i < len; i++ )
87 diff |= p1[i] ^ p2[i];
88
k-stachowiak1a9df6b2018-12-19 16:52:45 +010089 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -030090}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020091#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030092
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020093static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020098 int *type;
99
100 if( ! supported_init )
101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 def = mbedtls_cipher_definitions;
103 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104
105 while( def->type != 0 )
106 *type++ = (*def++).type;
107
108 *type = 0;
109
110 supported_init = 1;
111 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200121 if( def->type == cipher_type )
122 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000123
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200130
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200132 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200135 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200136 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000137
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200138 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141const 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 +0200142 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +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->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200149 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200150 def->info->mode == mode )
151 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200152
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200153 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100158 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +0200159 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200163{
164 if( ctx == NULL )
165 return;
166
Simon Butcher327398a2016-10-05 14:09:11 +0100167#if defined(MBEDTLS_CMAC_C)
168 if( ctx->cmac_ctx )
169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500170 mbedtls_platform_zeroize( ctx->cmac_ctx,
171 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100172 mbedtls_free( ctx->cmac_ctx );
173 }
174#endif
175
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176 if( ctx->cipher_ctx )
177 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
178
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500179 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180}
181
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200182int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183{
k-stachowiaka5390702018-12-17 11:27:03 +0100184 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100185 if( cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200188 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Andrzej Kurekd81351b2020-09-18 13:14:31 +0200189 ctx->operation = MBEDTLS_OPERATION_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190
Paul Bakker343a8702011-06-09 14:27:58 +0000191 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193
194 ctx->cipher_info = cipher_info;
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200197 /*
198 * Ignore possible errors caused by a cipher mode that doesn't use padding
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
201 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200202#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200206
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200207 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208}
209
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100210int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
211 const unsigned char *key,
212 int key_bitlen,
213 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214{
k-stachowiaka5390702018-12-17 11:27:03 +0100215 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100216 CIPHER_VALIDATE_RET( key != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100217 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
218 operation == MBEDTLS_DECRYPT );
k-stachowiak95070a82018-12-19 14:48:37 +0100219 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100220 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200223 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200226 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200227
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200228 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 ctx->operation = operation;
230
Paul Bakker343a8702011-06-09 14:27:58 +0000231 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100232 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( MBEDTLS_ENCRYPT == operation ||
235 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100236 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000238 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100239 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
240 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000241 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( MBEDTLS_DECRYPT == operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100244 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
245 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248}
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100251 const unsigned char *iv,
252 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200254 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100255
k-stachowiaka5390702018-12-17 11:27:03 +0100256 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100257 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100258 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200261 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
263 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200266 actual_iv_size = iv_len;
267 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200268 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200269 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200270
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200271 /* avoid reading past the end of input buffer */
272 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200274 }
275
Daniel Kingbd920622016-05-15 19:56:20 -0300276#if defined(MBEDTLS_CHACHA20_C)
277 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
278 {
279 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
280 iv,
281 0U ) ) /* Initial counter value */
282 {
283 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
284 }
285 }
286#endif
287
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300288 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300289 {
Teppo Järvelin91d79382019-10-02 09:09:31 +0300290 mbedtls_platform_memcpy( ctx->iv, iv, actual_iv_size );
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300291 ctx->iv_size = actual_iv_size;
292 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200293
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200294 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200298{
k-stachowiaka5390702018-12-17 11:27:03 +0100299 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100300 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100301 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200302
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303 ctx->unprocessed_len = 0;
304
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200305 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200306}
307
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200308#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200310 const unsigned char *ad, size_t ad_len )
311{
k-stachowiaka5390702018-12-17 11:27:03 +0100312 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100313 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100314 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100315 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100316
Daniel King8fe47012016-05-17 20:33:28 -0300317#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200319 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100320 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
321 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200322 }
Daniel King8fe47012016-05-17 20:33:28 -0300323#endif
324
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200325#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300326 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
327 {
328 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200329 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300330
331 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200332 ? MBEDTLS_CHACHAPOLY_ENCRYPT
333 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300334
Hanno Becker484caf02019-05-29 14:41:44 +0100335 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context *) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300336 ctx->iv,
337 mode );
338 if ( result != 0 )
339 return( result );
340
Hanno Becker484caf02019-05-29 14:41:44 +0100341 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context *) ctx->cipher_ctx,
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100342 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300343 }
344#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200345
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200346 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200348#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200351 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352{
Paul Bakkerff61a782011-06-09 15:42:02 +0000353 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100354 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355
k-stachowiaka5390702018-12-17 11:27:03 +0100356 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100357 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100358 CIPHER_VALIDATE_RET( output != NULL );
359 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100360 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100361 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100362
Paul Bakker6c212762013-12-16 15:24:50 +0100363 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100364 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskine010efeb2020-01-21 15:02:14 +0100365 if ( 0 == block_size )
366 {
367 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
368 }
Paul Bakker6c212762013-12-16 15:24:50 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200371 {
Janos Follath98e28a72016-05-31 14:03:54 +0100372 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200374
375 *olen = ilen;
376
377 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
378 ctx->operation, input, output ) ) )
379 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200380 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200381 }
382
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200384 }
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_GCM_C)
387 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200388 {
389 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100390 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
391 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200392 }
393#endif
394
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200395#if defined(MBEDTLS_CHACHAPOLY_C)
396 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
397 {
398 *olen = ilen;
Hanno Becker484caf02019-05-29 14:41:44 +0100399 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context *) ctx->cipher_ctx,
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100400 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200401 }
402#endif
403
Paul Bakker68884e32013-01-07 18:20:04 +0100404 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100405 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100408 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_CIPHER_MODE_CBC)
411 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200413 size_t copy_len = 0;
414
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 /*
416 * If there is not enough data for a full block, cache it.
417 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700418 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000419 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700420 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
421 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000423 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 {
Teppo Järvelin91d79382019-10-02 09:09:31 +0300425 mbedtls_platform_memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 ilen );
427
428 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200429 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 }
431
432 /*
433 * Process cached data first
434 */
Janos Follath98e28a72016-05-31 14:03:54 +0100435 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 {
Janos Follath98e28a72016-05-31 14:03:54 +0100437 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438
Teppo Järvelin91d79382019-10-02 09:09:31 +0300439 mbedtls_platform_memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440 copy_len );
441
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100443 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000444 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200446 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 }
448
Janos Follath98e28a72016-05-31 14:03:54 +0100449 *olen += block_size;
450 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 ctx->unprocessed_len = 0;
452
453 input += copy_len;
454 ilen -= copy_len;
455 }
456
457 /*
458 * Cache final, incomplete block
459 */
460 if( 0 != ilen )
461 {
Andy Leiserson79e77892017-04-28 20:01:49 -0700462 /* Encryption: only cache partial blocks
463 * Decryption w/ padding: always keep at least one whole block
464 * Decryption w/o padding: only cache partial blocks
465 */
Janos Follath98e28a72016-05-31 14:03:54 +0100466 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700467 if( copy_len == 0 &&
468 ctx->operation == MBEDTLS_DECRYPT &&
469 NULL != ctx->add_padding)
470 {
Janos Follath98e28a72016-05-31 14:03:54 +0100471 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700472 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473
Teppo Järvelin91d79382019-10-02 09:09:31 +0300474 mbedtls_platform_memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475 copy_len );
476
477 ctx->unprocessed_len += copy_len;
478 ilen -= copy_len;
479 }
480
481 /*
482 * Process remaining full blocks
483 */
484 if( ilen )
485 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000486 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
487 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000488 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200489 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200491
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492 *olen += ilen;
493 }
494
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200495 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#if defined(MBEDTLS_CIPHER_MODE_CFB)
500 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000501 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000502 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000503 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000504 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000505 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200506 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000507 }
508
509 *olen = ilen;
510
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200511 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000514
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100515#if defined(MBEDTLS_CIPHER_MODE_OFB)
516 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
517 {
518 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
519 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
520 {
521 return( ret );
522 }
523
524 *olen = ilen;
525
526 return( 0 );
527 }
528#endif /* MBEDTLS_CIPHER_MODE_OFB */
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_CIPHER_MODE_CTR)
531 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000532 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000533 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000534 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000535 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000536 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200537 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000538 }
539
540 *olen = ilen;
541
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200542 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000545
Jaeden Ameroc6539902018-04-30 17:17:41 +0100546#if defined(MBEDTLS_CIPHER_MODE_XTS)
547 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
548 {
549 if( ctx->unprocessed_len > 0 ) {
550 /* We can only process an entire data unit at a time. */
551 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
552 }
553
554 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
555 ctx->operation, ilen, ctx->iv, input, output );
556 if( ret != 0 )
557 {
558 return( ret );
559 }
560
561 *olen = ilen;
562
563 return( 0 );
564 }
565#endif /* MBEDTLS_CIPHER_MODE_XTS */
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_CIPHER_MODE_STREAM)
568 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200569 {
570 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
571 ilen, input, output ) ) )
572 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200573 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200574 }
575
576 *olen = ilen;
577
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200578 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583}
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
586#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200587/*
588 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
589 */
Paul Bakker23986e52011-04-24 08:57:21 +0000590static void add_pkcs_padding( unsigned char *output, size_t output_len,
591 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592{
Paul Bakker23986e52011-04-24 08:57:21 +0000593 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100594 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
596 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000597 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598}
599
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200600static int get_pkcs_padding( unsigned char *input, size_t input_len,
601 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100603 size_t i, pad_idx;
604 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Paul Bakkera885d682011-01-20 16:35:05 +0000606 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608
609 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 *data_len = input_len - padding_len;
611
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100612 /* Avoid logical || since it results in a branch */
613 bad |= padding_len > input_len;
614 bad |= padding_len == 0;
615
616 /* The number of bytes checked must be independent of padding_len,
617 * so pick input_len, which is usually 8 or 16 (one block) */
618 pad_idx = input_len - padding_len;
619 for( i = 0; i < input_len; i++ )
620 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200627/*
628 * One and zeros padding: fill with 80 00 ... 00
629 */
630static void add_one_and_zeros_padding( unsigned char *output,
631 size_t output_len, size_t data_len )
632{
633 size_t padding_len = output_len - data_len;
634 unsigned char i = 0;
635
636 output[data_len] = 0x80;
637 for( i = 1; i < padding_len; i++ )
638 output[data_len + i] = 0x00;
639}
640
641static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
642 size_t *data_len )
643{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100644 size_t i;
645 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200646
647 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200649
Micha Krausba8316f2017-12-23 23:40:08 +0100650 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100651 *data_len = 0;
652 for( i = input_len; i > 0; i-- )
653 {
654 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100655 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100656 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100657 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100658 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200661
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200666/*
667 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
668 */
669static void add_zeros_and_len_padding( unsigned char *output,
670 size_t output_len, size_t data_len )
671{
672 size_t padding_len = output_len - data_len;
673 unsigned char i = 0;
674
675 for( i = 1; i < padding_len; i++ )
676 output[data_len + i - 1] = 0x00;
677 output[output_len - 1] = (unsigned char) padding_len;
678}
679
680static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
681 size_t *data_len )
682{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100683 size_t i, pad_idx;
684 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200685
686 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200688
689 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200690 *data_len = input_len - padding_len;
691
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100692 /* Avoid logical || since it results in a branch */
693 bad |= padding_len > input_len;
694 bad |= padding_len == 0;
695
696 /* The number of bytes checked must be independent of padding_len */
697 pad_idx = input_len - padding_len;
698 for( i = 0; i < input_len - 1; i++ )
699 bad |= input[i] * ( i >= pad_idx );
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200706/*
707 * Zero padding: fill with 00 ... 00
708 */
709static void add_zeros_padding( unsigned char *output,
710 size_t output_len, size_t data_len )
711{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200712 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200713
714 for( i = data_len; i < output_len; i++ )
715 output[i] = 0x00;
716}
717
718static int get_zeros_padding( unsigned char *input, size_t input_len,
719 size_t *data_len )
720{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100721 size_t i;
722 unsigned char done = 0, prev_done;
723
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200724 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200726
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100727 *data_len = 0;
728 for( i = input_len; i > 0; i-- )
729 {
730 prev_done = done;
731 done |= ( input[i-1] != 0 );
732 *data_len |= i * ( done != prev_done );
733 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200734
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200738
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200739/*
740 * No padding: don't pad :)
741 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200743 * but a trivial get_padding function
744 */
745static int get_no_padding( unsigned char *input, size_t input_len,
746 size_t *data_len )
747{
748 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é-Gonnardebdc4132013-07-26 16:50:44 +0200750
751 *data_len = input_len;
752
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200758 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000759{
k-stachowiaka5390702018-12-17 11:27:03 +0100760 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100761 CIPHER_VALIDATE_RET( output != NULL );
762 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100763 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100764 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100765
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766 *olen = 0;
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100769 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
771 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100772 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000774 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200775 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000776 }
777
Daniel King8fe47012016-05-17 20:33:28 -0300778 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
779 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300780 {
781 return( 0 );
782 }
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200785 {
786 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200788
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200789 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200790 }
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_MODE_CBC)
793 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200795 int ret = 0;
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000798 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200799 /* check for 'no padding' mode */
800 if( NULL == ctx->add_padding )
801 {
802 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200804
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200805 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200806 }
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000809 ctx->unprocessed_len );
810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200813 /*
814 * For decrypt operations, expect a full block,
815 * or an empty block if no padding
816 */
817 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200818 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000821 }
822
823 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000824 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000826 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200828 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000829 }
830
831 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( MBEDTLS_DECRYPT == ctx->operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100833 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
834 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000835
836 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200838 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000839 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200840#else
841 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845}
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100848int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
849 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200850{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100851 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100852
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100853 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200856 }
857
Paul Bakker1a45d912013-08-14 12:04:26 +0200858 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
861 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200862 ctx->add_padding = add_pkcs_padding;
863 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200864 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
867 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200868 ctx->add_padding = add_one_and_zeros_padding;
869 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200870 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
873 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200874 ctx->add_padding = add_zeros_and_len_padding;
875 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200876 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
879 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200880 ctx->add_padding = add_zeros_padding;
881 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200882 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200885 ctx->add_padding = NULL;
886 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200887 break;
888
889 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200891 }
892
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200893 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200896
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200897#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200899 unsigned char *tag, size_t tag_len )
900{
k-stachowiaka5390702018-12-17 11:27:03 +0100901 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100902 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100903 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100904 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( MBEDTLS_ENCRYPT != ctx->operation )
907 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200908
Daniel King8fe47012016-05-17 20:33:28 -0300909#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100911 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
912 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300913#endif
914
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200915#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300916 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
917 {
918 /* Don't allow truncated MAC for Poly1305 */
919 if ( tag_len != 16U )
920 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
921
Hanno Becker484caf02019-05-29 14:41:44 +0100922 return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context *) ctx->cipher_ctx,
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100923 tag ) );
Daniel King8fe47012016-05-17 20:33:28 -0300924 }
925#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200926
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200927 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200928}
Paul Bakker9af723c2014-05-01 13:03:14 +0200929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200931 const unsigned char *tag, size_t tag_len )
932{
Daniel King8fe47012016-05-17 20:33:28 -0300933 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200934 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200935
k-stachowiaka5390702018-12-17 11:27:03 +0100936 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100937 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100938 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100939 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100940
k-stachowiaka5390702018-12-17 11:27:03 +0100941 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200944 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200945
Daniel King8fe47012016-05-17 20:33:28 -0300946#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200948 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200949 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200953 check_tag, tag_len ) ) )
954 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200955 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200956 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200957
958 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300959 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200961
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200962 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200963 }
Daniel King8fe47012016-05-17 20:33:28 -0300964#endif /* MBEDTLS_GCM_C */
965
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200966#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300967 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
968 {
969 /* Don't allow truncated MAC for Poly1305 */
970 if ( tag_len != sizeof( check_tag ) )
971 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
972
Hanno Becker484caf02019-05-29 14:41:44 +0100973 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context *) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300974 check_tag );
975 if ( ret != 0 )
976 {
977 return( ret );
978 }
979
980 /* Check the tag in "constant-time" */
981 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
982 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
983
984 return( 0 );
985 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200986#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200987
988 return( 0 );
989}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200990#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200991
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200992/*
993 * Packet-oriented wrapper for non-AEAD modes
994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200996 const unsigned char *iv, size_t iv_len,
997 const unsigned char *input, size_t ilen,
998 unsigned char *output, size_t *olen )
999{
1000 int ret;
1001 size_t finish_olen;
1002
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001003 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001004 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001005 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001006 CIPHER_VALIDATE_RET( output != NULL );
1007 CIPHER_VALIDATE_RET( olen != NULL );
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001010 return( ret );
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001013 return( ret );
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001016 return( ret );
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001019 return( ret );
1020
1021 *olen += finish_olen;
1022
1023 return( 0 );
1024}
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001027/*
1028 * Packet-oriented encryption for AEAD modes
1029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001031 const unsigned char *iv, size_t iv_len,
1032 const unsigned char *ad, size_t ad_len,
1033 const unsigned char *input, size_t ilen,
1034 unsigned char *output, size_t *olen,
1035 unsigned char *tag, size_t tag_len )
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 != NULL );
1039 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001040 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001041 CIPHER_VALIDATE_RET( output != NULL );
1042 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001043 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_GCM_C)
1046 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001047 {
1048 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001050 iv, iv_len, ad, ad_len, input, output,
1051 tag_len, tag ) );
1052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#endif /* MBEDTLS_GCM_C */
1054#if defined(MBEDTLS_CCM_C)
1055 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001056 {
1057 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001059 iv, iv_len, ad, ad_len, input, output,
1060 tag, tag_len ) );
1061 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001063#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001064 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1065 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001066 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001067 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001068 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001069 {
1070 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1071 }
1072
1073 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001074 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001075 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001076 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001077#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001080}
1081
1082/*
1083 * Packet-oriented decryption for AEAD modes
1084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001086 const unsigned char *iv, size_t iv_len,
1087 const unsigned char *ad, size_t ad_len,
1088 const unsigned char *input, size_t ilen,
1089 unsigned char *output, size_t *olen,
1090 const unsigned char *tag, size_t tag_len )
1091{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001092 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001093 CIPHER_VALIDATE_RET( iv != NULL );
1094 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001095 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001096 CIPHER_VALIDATE_RET( output != NULL );
1097 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001098 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#if defined(MBEDTLS_GCM_C)
1101 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001102 {
1103 int ret;
1104
1105 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001107 iv, iv_len, ad, ad_len,
1108 tag, tag_len, input, output );
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1111 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001112
1113 return( ret );
1114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_GCM_C */
1116#if defined(MBEDTLS_CCM_C)
1117 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001118 {
1119 int ret;
1120
1121 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001123 iv, iv_len, ad, ad_len,
1124 input, output, tag, tag_len );
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1127 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001128
1129 return( ret );
1130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001132#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001133 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1134 {
Daniel King8fe47012016-05-17 20:33:28 -03001135 int ret;
1136
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001137 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001138 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001139 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001140 {
1141 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1142 }
1143
1144 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001145 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1146 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001147
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001148 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1149 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001150
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001151 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001152 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001153#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_CIPHER_C */