blob: b204bcbb8abb7c6aac27e731769caf5a67c3b050 [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é-Gonnardd4bd8562018-05-07 10:43:27 +020041#if defined(MBEDTLS_CHACHAPOLY_C)
42#include "mbedtls/chachapoly.h"
Daniel Kingb437a982016-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 King0fe7b5b2016-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
69#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020070#endif
71
Daniel Kingb9a069d2016-05-18 13:38:22 -030072
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +020073#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-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 Kingb9a069d2016-05-18 13:38:22 -030076 * a non-zero value.
77 * This is currently only used by GCM and ChaCha20+Poly1305.
78 */
Daniel Kingb437a982016-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é-Gonnardd4bd8562018-05-07 10:43:27 +020091#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel Kingb437a982016-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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200159}
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200162{
163 if( ctx == NULL )
164 return;
165
Simon Butcher327398a2016-10-05 14:09:11 +0100166#if defined(MBEDTLS_CMAC_C)
167 if( ctx->cmac_ctx )
168 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500169 mbedtls_platform_zeroize( ctx->cmac_ctx,
170 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100171 mbedtls_free( ctx->cmac_ctx );
172 }
173#endif
174
Paul Bakker84bbeb52014-07-01 14:53:22 +0200175 if( ctx->cipher_ctx )
176 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
177
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500178 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200179}
180
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200181int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182{
183 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Paul Bakker343a8702011-06-09 14:27:58 +0000188 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190
191 ctx->cipher_info = cipher_info;
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200194 /*
195 * Ignore possible errors caused by a cipher mode that doesn't use padding
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
198 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200199#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200203
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200208 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209{
210 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200214 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200217 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200218
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200219 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220 ctx->operation = operation;
221
Paul Bakker343a8702011-06-09 14:27:58 +0000222 /*
Simon Butcher91e254c2018-04-22 22:58:07 +0100223 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( MBEDTLS_ENCRYPT == operation ||
226 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher91e254c2018-04-22 22:58:07 +0100227 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000229 {
230 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200231 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000232 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000235 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200236 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200242 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200244 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200245
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
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
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200249 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
251 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200254 actual_iv_size = iv_len;
255 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200256 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200257 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200258
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200259 /* avoid reading past the end of input buffer */
260 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200262 }
263
Daniel King0fe7b5b2016-05-15 19:56:20 -0300264#if defined(MBEDTLS_CHACHA20_C)
265 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
266 {
267 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
268 iv,
269 0U ) ) /* Initial counter value */
270 {
271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
272 }
273 }
274#endif
275
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200276 memcpy( ctx->iv, iv, actual_iv_size );
277 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200278
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200280}
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200283{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200284 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200286
Paul Bakker8123e9d2011-01-06 15:37:30 +0000287 ctx->unprocessed_len = 0;
288
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200289 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200290}
291
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200292#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200294 const unsigned char *ad, size_t ad_len )
295{
296 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200298
Daniel Kingb437a982016-05-17 20:33:28 -0300299#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200303 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200304 }
Daniel Kingb437a982016-05-17 20:33:28 -0300305#endif
306
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200307#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -0300308 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
309 {
310 int result;
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200311 mbedtls_chachapoly_mode_t mode;
Daniel Kingb437a982016-05-17 20:33:28 -0300312
313 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200314 ? MBEDTLS_CHACHAPOLY_ENCRYPT
315 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel Kingb437a982016-05-17 20:33:28 -0300316
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200317 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel Kingb437a982016-05-17 20:33:28 -0300318 ctx->iv,
319 mode );
320 if ( result != 0 )
321 return( result );
322
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200323 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard799b3902018-05-09 09:34:25 +0200324 ad, ad_len );
Daniel Kingb437a982016-05-17 20:33:28 -0300325 }
326#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200327
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200328 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329}
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200330#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200333 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334{
Paul Bakkerff61a782011-06-09 15:42:02 +0000335 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100336 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337
Paul Bakker68884e32013-01-07 18:20:04 +0100338 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000341 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342
Paul Bakker6c212762013-12-16 15:24:50 +0100343 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100344 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200347 {
Janos Follath98e28a72016-05-31 14:03:54 +0100348 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200350
351 *olen = ilen;
352
353 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
354 ctx->operation, input, output ) ) )
355 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200356 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200357 }
358
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200359 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_GCM_C)
363 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200364 {
365 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200367 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200368 }
369#endif
370
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +0200371#if defined(MBEDTLS_CHACHAPOLY_C)
372 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
373 {
374 *olen = ilen;
375 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
376 ilen, input, output );
377 }
378#endif
379
Janos Follath98e28a72016-05-31 14:03:54 +0100380 if ( 0 == block_size )
381 {
382 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
383 }
384
Paul Bakker68884e32013-01-07 18:20:04 +0100385 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100386 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100389 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_CIPHER_MODE_CBC)
392 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000393 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200394 size_t copy_len = 0;
395
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396 /*
397 * If there is not enough data for a full block, cache it.
398 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700399 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000400 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700401 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
402 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000404 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 {
406 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
407 ilen );
408
409 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411 }
412
413 /*
414 * Process cached data first
415 */
Janos Follath98e28a72016-05-31 14:03:54 +0100416 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417 {
Janos Follath98e28a72016-05-31 14:03:54 +0100418 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000419
420 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
421 copy_len );
422
Paul Bakkerff61a782011-06-09 15:42:02 +0000423 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100424 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000425 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 }
429
Janos Follath98e28a72016-05-31 14:03:54 +0100430 *olen += block_size;
431 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 ctx->unprocessed_len = 0;
433
434 input += copy_len;
435 ilen -= copy_len;
436 }
437
438 /*
439 * Cache final, incomplete block
440 */
441 if( 0 != ilen )
442 {
Janos Follath98e28a72016-05-31 14:03:54 +0100443 if( 0 == block_size )
444 {
445 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
446 }
447
Andy Leiserson79e77892017-04-28 20:01:49 -0700448 /* Encryption: only cache partial blocks
449 * Decryption w/ padding: always keep at least one whole block
450 * Decryption w/o padding: only cache partial blocks
451 */
Janos Follath98e28a72016-05-31 14:03:54 +0100452 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700453 if( copy_len == 0 &&
454 ctx->operation == MBEDTLS_DECRYPT &&
455 NULL != ctx->add_padding)
456 {
Janos Follath98e28a72016-05-31 14:03:54 +0100457 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700458 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
460 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
461 copy_len );
462
463 ctx->unprocessed_len += copy_len;
464 ilen -= copy_len;
465 }
466
467 /*
468 * Process remaining full blocks
469 */
470 if( ilen )
471 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000472 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
473 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200477
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478 *olen += ilen;
479 }
480
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200481 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#if defined(MBEDTLS_CIPHER_MODE_CFB)
486 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000487 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000488 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000489 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000490 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000491 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200492 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000493 }
494
495 *olen = ilen;
496
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000498 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000500
Simon Butcher91e254c2018-04-22 22:58:07 +0100501#if defined(MBEDTLS_CIPHER_MODE_OFB)
502 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
503 {
504 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
505 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
506 {
507 return( ret );
508 }
509
510 *olen = ilen;
511
512 return( 0 );
513 }
514#endif /* MBEDTLS_CIPHER_MODE_OFB */
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_CIPHER_MODE_CTR)
517 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000518 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000519 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000520 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000521 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000522 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200523 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000524 }
525
526 *olen = ilen;
527
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200528 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000531
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100532#if defined(MBEDTLS_CIPHER_MODE_XTS)
533 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
534 {
535 if ( ctx->unprocessed_len ) {
536 /* We can only process an entire sector at a time. */
537 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
538 }
539
540 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
541 ctx->operation, ilen, ctx->iv, input, output );
542 if( ret != 0 )
543 {
544 return( ret );
545 }
546
547 *olen = ilen;
548
549 return( 0 );
550 }
551#endif /* MBEDTLS_CIPHER_MODE_XTS */
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_CIPHER_MODE_STREAM)
554 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200555 {
556 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
557 ilen, input, output ) ) )
558 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200559 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200560 }
561
562 *olen = ilen;
563
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200564 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569}
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
572#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200573/*
574 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
575 */
Paul Bakker23986e52011-04-24 08:57:21 +0000576static void add_pkcs_padding( unsigned char *output, size_t output_len,
577 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100580 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581
582 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000583 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584}
585
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200586static int get_pkcs_padding( unsigned char *input, size_t input_len,
587 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100589 size_t i, pad_idx;
590 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Paul Bakkera885d682011-01-20 16:35:05 +0000592 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
595 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 *data_len = input_len - padding_len;
597
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100598 /* Avoid logical || since it results in a branch */
599 bad |= padding_len > input_len;
600 bad |= padding_len == 0;
601
602 /* The number of bytes checked must be independent of padding_len,
603 * so pick input_len, which is usually 8 or 16 (one block) */
604 pad_idx = input_len - padding_len;
605 for( i = 0; i < input_len; i++ )
606 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200613/*
614 * One and zeros padding: fill with 80 00 ... 00
615 */
616static void add_one_and_zeros_padding( unsigned char *output,
617 size_t output_len, size_t data_len )
618{
619 size_t padding_len = output_len - data_len;
620 unsigned char i = 0;
621
622 output[data_len] = 0x80;
623 for( i = 1; i < padding_len; i++ )
624 output[data_len + i] = 0x00;
625}
626
627static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
628 size_t *data_len )
629{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100630 size_t i;
631 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200632
633 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200635
Micha Krausba8316f2017-12-23 23:40:08 +0100636 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100637 *data_len = 0;
638 for( i = input_len; i > 0; i-- )
639 {
640 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100641 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100642 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100643 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100644 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200647
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200652/*
653 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
654 */
655static void add_zeros_and_len_padding( unsigned char *output,
656 size_t output_len, size_t data_len )
657{
658 size_t padding_len = output_len - data_len;
659 unsigned char i = 0;
660
661 for( i = 1; i < padding_len; i++ )
662 output[data_len + i - 1] = 0x00;
663 output[output_len - 1] = (unsigned char) padding_len;
664}
665
666static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
667 size_t *data_len )
668{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100669 size_t i, pad_idx;
670 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200671
672 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200674
675 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200676 *data_len = input_len - padding_len;
677
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100678 /* Avoid logical || since it results in a branch */
679 bad |= padding_len > input_len;
680 bad |= padding_len == 0;
681
682 /* The number of bytes checked must be independent of padding_len */
683 pad_idx = input_len - padding_len;
684 for( i = 0; i < input_len - 1; i++ )
685 bad |= input[i] * ( i >= pad_idx );
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200692/*
693 * Zero padding: fill with 00 ... 00
694 */
695static void add_zeros_padding( unsigned char *output,
696 size_t output_len, size_t data_len )
697{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200698 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200699
700 for( i = data_len; i < output_len; i++ )
701 output[i] = 0x00;
702}
703
704static int get_zeros_padding( unsigned char *input, size_t input_len,
705 size_t *data_len )
706{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100707 size_t i;
708 unsigned char done = 0, prev_done;
709
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200710 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200712
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100713 *data_len = 0;
714 for( i = input_len; i > 0; i-- )
715 {
716 prev_done = done;
717 done |= ( input[i-1] != 0 );
718 *data_len |= i * ( done != prev_done );
719 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200720
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200721 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200724
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200725/*
726 * No padding: don't pad :)
727 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200729 * but a trivial get_padding function
730 */
731static int get_no_padding( unsigned char *input, size_t input_len,
732 size_t *data_len )
733{
734 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200736
737 *data_len = input_len;
738
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200739 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200744 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000745{
746 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748
749 *olen = 0;
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher91e254c2018-04-22 22:58:07 +0100752 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
754 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100755 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000757 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200758 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000759 }
760
Daniel Kingb437a982016-05-17 20:33:28 -0300761 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
762 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel King0fe7b5b2016-05-15 19:56:20 -0300763 {
764 return( 0 );
765 }
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200768 {
769 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200771
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200772 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200773 }
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_CIPHER_MODE_CBC)
776 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000777 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200778 int ret = 0;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200782 /* check for 'no padding' mode */
783 if( NULL == ctx->add_padding )
784 {
785 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200787
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200788 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200789 }
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 ctx->unprocessed_len );
793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000795 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200796 /*
797 * For decrypt operations, expect a full block,
798 * or an empty block if no padding
799 */
800 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200801 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000804 }
805
806 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000807 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000809 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000810 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200811 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812 }
813
814 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 if( MBEDTLS_DECRYPT == ctx->operation )
816 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200817 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000818
819 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200821 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000822 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200823#else
824 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000828}
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
831int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200832{
833 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200837 }
838
Paul Bakker1a45d912013-08-14 12:04:26 +0200839 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
842 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200843 ctx->add_padding = add_pkcs_padding;
844 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200845 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
848 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200849 ctx->add_padding = add_one_and_zeros_padding;
850 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200851 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
854 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200855 ctx->add_padding = add_zeros_and_len_padding;
856 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200857 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
860 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200861 ctx->add_padding = add_zeros_padding;
862 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200863 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200866 ctx->add_padding = NULL;
867 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200868 break;
869
870 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200872 }
873
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200874 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200877
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200878#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200880 unsigned char *tag, size_t tag_len )
881{
882 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( MBEDTLS_ENCRYPT != ctx->operation )
886 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200887
Daniel Kingb437a982016-05-17 20:33:28 -0300888#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
890 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel Kingb437a982016-05-17 20:33:28 -0300891#endif
892
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200893#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -0300894 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
895 {
896 /* Don't allow truncated MAC for Poly1305 */
897 if ( tag_len != 16U )
898 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
899
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200900 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel Kingb437a982016-05-17 20:33:28 -0300901 tag );
902 }
903#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200904
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200905 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200906}
Paul Bakker9af723c2014-05-01 13:03:14 +0200907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200909 const unsigned char *tag, size_t tag_len )
910{
Daniel Kingb437a982016-05-17 20:33:28 -0300911 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200912 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200913
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200914 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200918 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200919
Daniel Kingb437a982016-05-17 20:33:28 -0300920#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200922 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200923 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200927 check_tag, tag_len ) ) )
928 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200929 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200930 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200931
932 /* Check the tag in "constant-time" */
Daniel Kingb437a982016-05-17 20:33:28 -0300933 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200935
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200936 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200937 }
Daniel Kingb437a982016-05-17 20:33:28 -0300938#endif /* MBEDTLS_GCM_C */
939
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200940#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -0300941 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
942 {
943 /* Don't allow truncated MAC for Poly1305 */
944 if ( tag_len != sizeof( check_tag ) )
945 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
946
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200947 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel Kingb437a982016-05-17 20:33:28 -0300948 check_tag );
949 if ( ret != 0 )
950 {
951 return( ret );
952 }
953
954 /* Check the tag in "constant-time" */
955 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
956 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
957
958 return( 0 );
959 }
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200960#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200961
962 return( 0 );
963}
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +0200964#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200965
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200966/*
967 * Packet-oriented wrapper for non-AEAD modes
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200970 const unsigned char *iv, size_t iv_len,
971 const unsigned char *input, size_t ilen,
972 unsigned char *output, size_t *olen )
973{
974 int ret;
975 size_t finish_olen;
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200978 return( ret );
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200981 return( ret );
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200984 return( ret );
985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200987 return( ret );
988
989 *olen += finish_olen;
990
991 return( 0 );
992}
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200995/*
996 * Packet-oriented encryption for AEAD modes
997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200999 const unsigned char *iv, size_t iv_len,
1000 const unsigned char *ad, size_t ad_len,
1001 const unsigned char *input, size_t ilen,
1002 unsigned char *output, size_t *olen,
1003 unsigned char *tag, size_t tag_len )
1004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_GCM_C)
1006 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001007 {
1008 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001010 iv, iv_len, ad, ad_len, input, output,
1011 tag_len, tag ) );
1012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_GCM_C */
1014#if defined(MBEDTLS_CCM_C)
1015 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001016 {
1017 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001019 iv, iv_len, ad, ad_len, input, output,
1020 tag, tag_len ) );
1021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001023#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -03001024 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1025 {
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001026 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel Kingb437a982016-05-17 20:33:28 -03001027 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001028 ( tag_len != 16U ) )
Daniel Kingb437a982016-05-17 20:33:28 -03001029 {
1030 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1031 }
1032
1033 *olen = ilen;
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001034 return( mbedtls_chachapoly_crypt_and_tag( ctx->cipher_ctx,
1035 MBEDTLS_CHACHAPOLY_ENCRYPT,
1036 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel Kingb437a982016-05-17 20:33:28 -03001037 }
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001038#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001041}
1042
1043/*
1044 * Packet-oriented decryption for AEAD modes
1045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001047 const unsigned char *iv, size_t iv_len,
1048 const unsigned char *ad, size_t ad_len,
1049 const unsigned char *input, size_t ilen,
1050 unsigned char *output, size_t *olen,
1051 const unsigned char *tag, size_t tag_len )
1052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_GCM_C)
1054 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001055 {
1056 int ret;
1057
1058 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001060 iv, iv_len, ad, ad_len,
1061 tag, tag_len, input, output );
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1064 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001065
1066 return( ret );
1067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_GCM_C */
1069#if defined(MBEDTLS_CCM_C)
1070 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001071 {
1072 int ret;
1073
1074 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001076 iv, iv_len, ad, ad_len,
1077 input, output, tag, tag_len );
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1080 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001081
1082 return( ret );
1083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001085#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -03001086 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1087 {
Daniel Kingb437a982016-05-17 20:33:28 -03001088 int ret;
1089
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001090 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel Kingb437a982016-05-17 20:33:28 -03001091 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001092 ( tag_len != 16U ) )
Daniel Kingb437a982016-05-17 20:33:28 -03001093 {
1094 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1095 }
1096
1097 *olen = ilen;
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001098 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1099 iv, ad, ad_len, tag, input, output );
Daniel Kingb437a982016-05-17 20:33:28 -03001100
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001101 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1102 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel Kingb437a982016-05-17 20:33:28 -03001103
Manuel Pégourié-Gonnard4f522632018-05-08 09:38:09 +02001104 return( ret );
Daniel Kingb437a982016-05-17 20:33:28 -03001105 }
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001106#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_CIPHER_C */