blob: d2078f6f631bae71422f01238d68a99129735af9 [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
89 return (int)diff;
90}
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +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{
184 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
Paul Bakker343a8702011-06-09 14:27:58 +0000189 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
192 ctx->cipher_info = cipher_info;
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200195 /*
196 * Ignore possible errors caused by a cipher mode that doesn't use padding
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
199 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200200#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200204
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200205 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206}
207
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100208int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
209 const unsigned char *key,
210 int key_bitlen,
211 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100213 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
214 operation == MBEDTLS_DECRYPT );
215
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200220 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200223 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200224
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200225 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226 ctx->operation = operation;
227
Paul Bakker343a8702011-06-09 14:27:58 +0000228 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100229 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( MBEDTLS_ENCRYPT == operation ||
232 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100233 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000235 {
236 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200237 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000238 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000241 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200242 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245}
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100248 const unsigned char *iv,
249 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200251 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100252
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300253 if( NULL == ctx || NULL == ctx->cipher_info )
254 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
255 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300258 if( NULL == iv && iv_len == 0 )
259 ctx->iv_size = 0;
260
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 {
290 memcpy( ctx->iv, iv, actual_iv_size );
291 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{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200299 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200301
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 ctx->unprocessed_len = 0;
303
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200304 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200305}
306
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200307#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200309 const unsigned char *ad, size_t ad_len )
310{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100311 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
312
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200313 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200315
Daniel King8fe47012016-05-17 20:33:28 -0300316#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200320 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200321 }
Daniel King8fe47012016-05-17 20:33:28 -0300322#endif
323
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200324#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300325 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
326 {
327 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200328 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300329
330 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200331 ? MBEDTLS_CHACHAPOLY_ENCRYPT
332 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300333
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200334 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300335 ctx->iv,
336 mode );
337 if ( result != 0 )
338 return( result );
339
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200340 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200341 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300342 }
343#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200344
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200345 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200347#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200350 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351{
Paul Bakkerff61a782011-06-09 15:42:02 +0000352 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100353 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100355 CIPHER_VALIDATE_RET( input != NULL );
356 CIPHER_VALIDATE_RET( output != NULL );
357 CIPHER_VALIDATE_RET( olen != NULL );
358
359 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkera885d682011-01-20 16:35:05 +0000360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000362 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
Paul Bakker6c212762013-12-16 15:24:50 +0100364 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100365 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200368 {
Janos Follath98e28a72016-05-31 14:03:54 +0100369 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200371
372 *olen = ilen;
373
374 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
375 ctx->operation, input, output ) ) )
376 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200377 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200378 }
379
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200380 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200381 }
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_GCM_C)
384 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200385 {
386 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200389 }
390#endif
391
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200392#if defined(MBEDTLS_CHACHAPOLY_C)
393 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
394 {
395 *olen = ilen;
396 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
397 ilen, input, output );
398 }
399#endif
400
Janos Follath98e28a72016-05-31 14:03:54 +0100401 if ( 0 == block_size )
402 {
403 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
404 }
405
Paul Bakker68884e32013-01-07 18:20:04 +0100406 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100407 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100410 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_CIPHER_MODE_CBC)
413 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200415 size_t copy_len = 0;
416
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417 /*
418 * If there is not enough data for a full block, cache it.
419 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700420 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000421 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700422 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
423 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000425 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 {
427 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
428 ilen );
429
430 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200431 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 }
433
434 /*
435 * Process cached data first
436 */
Janos Follath98e28a72016-05-31 14:03:54 +0100437 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438 {
Janos Follath98e28a72016-05-31 14:03:54 +0100439 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440
441 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
442 copy_len );
443
Paul Bakkerff61a782011-06-09 15:42:02 +0000444 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100445 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000446 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200448 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 }
450
Janos Follath98e28a72016-05-31 14:03:54 +0100451 *olen += block_size;
452 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453 ctx->unprocessed_len = 0;
454
455 input += copy_len;
456 ilen -= copy_len;
457 }
458
459 /*
460 * Cache final, incomplete block
461 */
462 if( 0 != ilen )
463 {
Janos Follath98e28a72016-05-31 14:03:54 +0100464 if( 0 == block_size )
465 {
466 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
467 }
468
Andy Leiserson79e77892017-04-28 20:01:49 -0700469 /* Encryption: only cache partial blocks
470 * Decryption w/ padding: always keep at least one whole block
471 * Decryption w/o padding: only cache partial blocks
472 */
Janos Follath98e28a72016-05-31 14:03:54 +0100473 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700474 if( copy_len == 0 &&
475 ctx->operation == MBEDTLS_DECRYPT &&
476 NULL != ctx->add_padding)
477 {
Janos Follath98e28a72016-05-31 14:03:54 +0100478 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700479 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480
481 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
482 copy_len );
483
484 ctx->unprocessed_len += copy_len;
485 ilen -= copy_len;
486 }
487
488 /*
489 * Process remaining full blocks
490 */
491 if( ilen )
492 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000493 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
494 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200498
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499 *olen += ilen;
500 }
501
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200502 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_CIPHER_MODE_CFB)
507 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000508 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000509 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000510 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000511 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000512 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200513 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000514 }
515
516 *olen = ilen;
517
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000521
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100522#if defined(MBEDTLS_CIPHER_MODE_OFB)
523 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
524 {
525 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
526 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
527 {
528 return( ret );
529 }
530
531 *olen = ilen;
532
533 return( 0 );
534 }
535#endif /* MBEDTLS_CIPHER_MODE_OFB */
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_CIPHER_MODE_CTR)
538 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000539 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000540 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000541 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000542 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000543 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200544 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000545 }
546
547 *olen = ilen;
548
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200549 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000552
Jaeden Ameroc6539902018-04-30 17:17:41 +0100553#if defined(MBEDTLS_CIPHER_MODE_XTS)
554 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
555 {
556 if( ctx->unprocessed_len > 0 ) {
557 /* We can only process an entire data unit at a time. */
558 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
559 }
560
561 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
562 ctx->operation, ilen, ctx->iv, input, output );
563 if( ret != 0 )
564 {
565 return( ret );
566 }
567
568 *olen = ilen;
569
570 return( 0 );
571 }
572#endif /* MBEDTLS_CIPHER_MODE_XTS */
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_CIPHER_MODE_STREAM)
575 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200576 {
577 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
578 ilen, input, output ) ) )
579 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200580 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200581 }
582
583 *olen = ilen;
584
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590}
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
593#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200594/*
595 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
596 */
Paul Bakker23986e52011-04-24 08:57:21 +0000597static void add_pkcs_padding( unsigned char *output, size_t output_len,
598 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599{
Paul Bakker23986e52011-04-24 08:57:21 +0000600 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100601 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
603 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000604 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605}
606
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200607static int get_pkcs_padding( unsigned char *input, size_t input_len,
608 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100610 size_t i, pad_idx;
611 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612
Paul Bakkera885d682011-01-20 16:35:05 +0000613 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615
616 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617 *data_len = input_len - padding_len;
618
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100619 /* Avoid logical || since it results in a branch */
620 bad |= padding_len > input_len;
621 bad |= padding_len == 0;
622
623 /* The number of bytes checked must be independent of padding_len,
624 * so pick input_len, which is usually 8 or 16 (one block) */
625 pad_idx = input_len - padding_len;
626 for( i = 0; i < input_len; i++ )
627 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200634/*
635 * One and zeros padding: fill with 80 00 ... 00
636 */
637static void add_one_and_zeros_padding( unsigned char *output,
638 size_t output_len, size_t data_len )
639{
640 size_t padding_len = output_len - data_len;
641 unsigned char i = 0;
642
643 output[data_len] = 0x80;
644 for( i = 1; i < padding_len; i++ )
645 output[data_len + i] = 0x00;
646}
647
648static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
649 size_t *data_len )
650{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100651 size_t i;
652 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200653
654 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200656
Micha Krausba8316f2017-12-23 23:40:08 +0100657 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100658 *data_len = 0;
659 for( i = input_len; i > 0; i-- )
660 {
661 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100662 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100663 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100664 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100665 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200668
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200673/*
674 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
675 */
676static void add_zeros_and_len_padding( unsigned char *output,
677 size_t output_len, size_t data_len )
678{
679 size_t padding_len = output_len - data_len;
680 unsigned char i = 0;
681
682 for( i = 1; i < padding_len; i++ )
683 output[data_len + i - 1] = 0x00;
684 output[output_len - 1] = (unsigned char) padding_len;
685}
686
687static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
688 size_t *data_len )
689{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100690 size_t i, pad_idx;
691 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200692
693 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200695
696 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200697 *data_len = input_len - padding_len;
698
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100699 /* Avoid logical || since it results in a branch */
700 bad |= padding_len > input_len;
701 bad |= padding_len == 0;
702
703 /* The number of bytes checked must be independent of padding_len */
704 pad_idx = input_len - padding_len;
705 for( i = 0; i < input_len - 1; i++ )
706 bad |= input[i] * ( i >= pad_idx );
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200713/*
714 * Zero padding: fill with 00 ... 00
715 */
716static void add_zeros_padding( unsigned char *output,
717 size_t output_len, size_t data_len )
718{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200719 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200720
721 for( i = data_len; i < output_len; i++ )
722 output[i] = 0x00;
723}
724
725static int get_zeros_padding( unsigned char *input, size_t input_len,
726 size_t *data_len )
727{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100728 size_t i;
729 unsigned char done = 0, prev_done;
730
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200731 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200733
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100734 *data_len = 0;
735 for( i = input_len; i > 0; i-- )
736 {
737 prev_done = done;
738 done |= ( input[i-1] != 0 );
739 *data_len |= i * ( done != prev_done );
740 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200741
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200742 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200745
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200746/*
747 * No padding: don't pad :)
748 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200750 * but a trivial get_padding function
751 */
752static int get_no_padding( unsigned char *input, size_t input_len,
753 size_t *data_len )
754{
755 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200757
758 *data_len = input_len;
759
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200760 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200765 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100767 CIPHER_VALIDATE_RET( output != NULL );
768 CIPHER_VALIDATE_RET( olen != NULL );
769
770 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772
773 *olen = 0;
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100776 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
778 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100779 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000781 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200782 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000783 }
784
Daniel King8fe47012016-05-17 20:33:28 -0300785 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
786 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300787 {
788 return( 0 );
789 }
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200792 {
793 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200795
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200796 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200797 }
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_CIPHER_MODE_CBC)
800 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000801 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200802 int ret = 0;
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200806 /* check for 'no padding' mode */
807 if( NULL == ctx->add_padding )
808 {
809 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200811
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200812 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200813 }
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000816 ctx->unprocessed_len );
817 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200820 /*
821 * For decrypt operations, expect a full block,
822 * or an empty block if no padding
823 */
824 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200825 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000828 }
829
830 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000831 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000833 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200835 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000836 }
837
838 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( MBEDTLS_DECRYPT == ctx->operation )
840 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200841 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842
843 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200845 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200847#else
848 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000852}
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100855int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
856 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200857{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100858 CIPHER_VALIDATE_RET( ctx != NULL );
859 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
860
861 if( MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200864 }
865
Paul Bakker1a45d912013-08-14 12:04:26 +0200866 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
869 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200870 ctx->add_padding = add_pkcs_padding;
871 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200872 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
875 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200876 ctx->add_padding = add_one_and_zeros_padding;
877 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200878 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
881 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200882 ctx->add_padding = add_zeros_and_len_padding;
883 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200884 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
887 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200888 ctx->add_padding = add_zeros_padding;
889 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200893 ctx->add_padding = NULL;
894 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200895 break;
896
897 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200899 }
900
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200901 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200904
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200905#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200907 unsigned char *tag, size_t tag_len )
908{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100909 CIPHER_VALIDATE_RET( tag != NULL );
910
911 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( MBEDTLS_ENCRYPT != ctx->operation )
915 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200916
Daniel King8fe47012016-05-17 20:33:28 -0300917#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
919 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300920#endif
921
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200922#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300923 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
924 {
925 /* Don't allow truncated MAC for Poly1305 */
926 if ( tag_len != 16U )
927 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
928
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200929 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300930 tag );
931 }
932#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200933
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200934 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200935}
Paul Bakker9af723c2014-05-01 13:03:14 +0200936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200938 const unsigned char *tag, size_t tag_len )
939{
Daniel King8fe47012016-05-17 20:33:28 -0300940 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200941 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200942
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100943 CIPHER_VALIDATE_RET( tag != NULL );
944
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200945 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200949 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200950
Daniel King8fe47012016-05-17 20:33:28 -0300951#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200953 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200954 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200958 check_tag, tag_len ) ) )
959 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200960 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200961 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200962
963 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300964 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200966
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200967 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200968 }
Daniel King8fe47012016-05-17 20:33:28 -0300969#endif /* MBEDTLS_GCM_C */
970
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200971#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300972 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
973 {
974 /* Don't allow truncated MAC for Poly1305 */
975 if ( tag_len != sizeof( check_tag ) )
976 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
977
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200978 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300979 check_tag );
980 if ( ret != 0 )
981 {
982 return( ret );
983 }
984
985 /* Check the tag in "constant-time" */
986 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
987 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
988
989 return( 0 );
990 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200991#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200992
993 return( 0 );
994}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200995#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200996
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200997/*
998 * Packet-oriented wrapper for non-AEAD modes
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001001 const unsigned char *iv, size_t iv_len,
1002 const unsigned char *input, size_t ilen,
1003 unsigned char *output, size_t *olen )
1004{
1005 int ret;
1006 size_t finish_olen;
1007
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001008 CIPHER_VALIDATE_RET( ctx != NULL );
1009 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1010 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1011 CIPHER_VALIDATE_RET( input != NULL );
1012 CIPHER_VALIDATE_RET( output != NULL );
1013 CIPHER_VALIDATE_RET( olen != NULL );
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 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_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001019 return( ret );
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001022 return( ret );
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001025 return( ret );
1026
1027 *olen += finish_olen;
1028
1029 return( 0 );
1030}
1031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001033/*
1034 * Packet-oriented encryption for AEAD modes
1035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001037 const unsigned char *iv, size_t iv_len,
1038 const unsigned char *ad, size_t ad_len,
1039 const unsigned char *input, size_t ilen,
1040 unsigned char *output, size_t *olen,
1041 unsigned char *tag, size_t tag_len )
1042{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001043 CIPHER_VALIDATE_RET( ctx != NULL );
1044 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1045 CIPHER_VALIDATE_RET( iv != NULL );
1046 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1047 CIPHER_VALIDATE_RET( input != NULL );
1048 CIPHER_VALIDATE_RET( output != NULL );
1049 CIPHER_VALIDATE_RET( olen != NULL );
1050 CIPHER_VALIDATE_RET( tag != NULL );
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_GCM_C)
1053 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001054 {
1055 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001057 iv, iv_len, ad, ad_len, input, output,
1058 tag_len, tag ) );
1059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_GCM_C */
1061#if defined(MBEDTLS_CCM_C)
1062 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001063 {
1064 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001066 iv, iv_len, ad, ad_len, input, output,
1067 tag, tag_len ) );
1068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001070#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001071 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1072 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001073 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001074 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001075 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001076 {
1077 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1078 }
1079
1080 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001081 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001082 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001083 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001084#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001087}
1088
1089/*
1090 * Packet-oriented decryption for AEAD modes
1091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001093 const unsigned char *iv, size_t iv_len,
1094 const unsigned char *ad, size_t ad_len,
1095 const unsigned char *input, size_t ilen,
1096 unsigned char *output, size_t *olen,
1097 const unsigned char *tag, size_t tag_len )
1098{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001099 CIPHER_VALIDATE_RET( ctx != NULL );
1100 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1101 CIPHER_VALIDATE_RET( iv != NULL );
1102 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1103 CIPHER_VALIDATE_RET( input != NULL );
1104 CIPHER_VALIDATE_RET( output != NULL );
1105 CIPHER_VALIDATE_RET( olen != NULL );
1106 CIPHER_VALIDATE_RET( tag != NULL );
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_GCM_C)
1109 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001110 {
1111 int ret;
1112
1113 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001115 iv, iv_len, ad, ad_len,
1116 tag, tag_len, input, output );
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1119 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001120
1121 return( ret );
1122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#endif /* MBEDTLS_GCM_C */
1124#if defined(MBEDTLS_CCM_C)
1125 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001126 {
1127 int ret;
1128
1129 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001131 iv, iv_len, ad, ad_len,
1132 input, output, tag, tag_len );
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1135 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001136
1137 return( ret );
1138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001140#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001141 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1142 {
Daniel King8fe47012016-05-17 20:33:28 -03001143 int ret;
1144
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001145 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001146 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001147 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001148 {
1149 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1150 }
1151
1152 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001153 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1154 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001155
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001156 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1157 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001158
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001159 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001160 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001161#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_CIPHER_C */