blob: 2463a6148caa83512ce387d0708a48df1c034957 [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
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 King16b04ce2016-05-18 13:38:22 -030072
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{
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 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000223 * For 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 ||
227 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000228 {
229 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200230 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000231 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000234 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200235 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200241 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200243 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200244
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200248 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
250 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200253 actual_iv_size = iv_len;
254 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200255 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200256 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200257
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200258 /* avoid reading past the end of input buffer */
259 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200261 }
262
Daniel Kingbd920622016-05-15 19:56:20 -0300263#if defined(MBEDTLS_CHACHA20_C)
264 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
265 {
266 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
267 iv,
268 0U ) ) /* Initial counter value */
269 {
270 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
271 }
272 }
273#endif
274
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200275 memcpy( ctx->iv, iv, actual_iv_size );
276 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200277
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200278 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200279}
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200282{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200283 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200285
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286 ctx->unprocessed_len = 0;
287
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200288 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200289}
290
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200291#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200293 const unsigned char *ad, size_t ad_len )
294{
295 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200297
Daniel King8fe47012016-05-17 20:33:28 -0300298#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200302 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200303 }
Daniel King8fe47012016-05-17 20:33:28 -0300304#endif
305
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200306#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300307 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
308 {
309 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200310 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300311
312 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200313 ? MBEDTLS_CHACHAPOLY_ENCRYPT
314 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300315
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200316 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300317 ctx->iv,
318 mode );
319 if ( result != 0 )
320 return( result );
321
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200322 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200323 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300324 }
325#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200326
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200327 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200329#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200332 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333{
Paul Bakkerff61a782011-06-09 15:42:02 +0000334 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100335 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336
Paul Bakker68884e32013-01-07 18:20:04 +0100337 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000340 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341
Paul Bakker6c212762013-12-16 15:24:50 +0100342 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100343 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200346 {
Janos Follath98e28a72016-05-31 14:03:54 +0100347 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200349
350 *olen = ilen;
351
352 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
353 ctx->operation, input, output ) ) )
354 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200355 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200356 }
357
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200359 }
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_GCM_C)
362 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200363 {
364 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200366 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200367 }
368#endif
369
Janos Follath98e28a72016-05-31 14:03:54 +0100370 if ( 0 == block_size )
371 {
372 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
373 }
374
Paul Bakker68884e32013-01-07 18:20:04 +0100375 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100376 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100379 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Daniel Kingbd920622016-05-15 19:56:20 -0300381
382#if defined(MBEDTLS_CHACHA20_C)
383 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
384 {
385 *olen = ilen;
386 return mbedtls_chacha20_update( (mbedtls_chacha20_context*) ctx->cipher_ctx,
387 ilen, input, output );
388 }
389#endif
390
Daniel King8fe47012016-05-17 20:33:28 -0300391 if( input == output &&
392 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
393 {
394 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
395 }
396
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200397#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300398 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
399 {
400 *olen = ilen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200401 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300402 ilen, input, output );
403 }
404#endif
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_CIPHER_MODE_CBC)
407 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200409 size_t copy_len = 0;
410
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411 /*
412 * If there is not enough data for a full block, cache it.
413 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700414 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000415 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700416 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
417 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000419 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000420 {
421 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
422 ilen );
423
424 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200425 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 }
427
428 /*
429 * Process cached data first
430 */
Janos Follath98e28a72016-05-31 14:03:54 +0100431 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 {
Janos Follath98e28a72016-05-31 14:03:54 +0100433 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
435 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
436 copy_len );
437
Paul Bakkerff61a782011-06-09 15:42:02 +0000438 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100439 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000440 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200442 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443 }
444
Janos Follath98e28a72016-05-31 14:03:54 +0100445 *olen += block_size;
446 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 ctx->unprocessed_len = 0;
448
449 input += copy_len;
450 ilen -= copy_len;
451 }
452
453 /*
454 * Cache final, incomplete block
455 */
456 if( 0 != ilen )
457 {
Janos Follath98e28a72016-05-31 14:03:54 +0100458 if( 0 == block_size )
459 {
460 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
461 }
462
Andy Leiserson79e77892017-04-28 20:01:49 -0700463 /* Encryption: only cache partial blocks
464 * Decryption w/ padding: always keep at least one whole block
465 * Decryption w/o padding: only cache partial blocks
466 */
Janos Follath98e28a72016-05-31 14:03:54 +0100467 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700468 if( copy_len == 0 &&
469 ctx->operation == MBEDTLS_DECRYPT &&
470 NULL != ctx->add_padding)
471 {
Janos Follath98e28a72016-05-31 14:03:54 +0100472 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700473 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474
475 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
476 copy_len );
477
478 ctx->unprocessed_len += copy_len;
479 ilen -= copy_len;
480 }
481
482 /*
483 * Process remaining full blocks
484 */
485 if( ilen )
486 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000487 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
488 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200490 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000491 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200492
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493 *olen += ilen;
494 }
495
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_CIPHER_MODE_CFB)
501 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000502 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000503 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000504 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000505 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000506 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200507 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000508 }
509
510 *olen = ilen;
511
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200512 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000513 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000515
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_CIPHER_MODE_STREAM)
533 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200534 {
535 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
536 ilen, input, output ) ) )
537 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200538 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200539 }
540
541 *olen = ilen;
542
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200543 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200544 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548}
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
551#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200552/*
553 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
554 */
Paul Bakker23986e52011-04-24 08:57:21 +0000555static void add_pkcs_padding( unsigned char *output, size_t output_len,
556 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000557{
Paul Bakker23986e52011-04-24 08:57:21 +0000558 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100559 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560
561 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000562 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000563}
564
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200565static int get_pkcs_padding( unsigned char *input, size_t input_len,
566 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100568 size_t i, pad_idx;
569 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570
Paul Bakkera885d682011-01-20 16:35:05 +0000571 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573
574 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 *data_len = input_len - padding_len;
576
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100577 /* Avoid logical || since it results in a branch */
578 bad |= padding_len > input_len;
579 bad |= padding_len == 0;
580
581 /* The number of bytes checked must be independent of padding_len,
582 * so pick input_len, which is usually 8 or 16 (one block) */
583 pad_idx = input_len - padding_len;
584 for( i = 0; i < input_len; i++ )
585 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200592/*
593 * One and zeros padding: fill with 80 00 ... 00
594 */
595static void add_one_and_zeros_padding( unsigned char *output,
596 size_t output_len, size_t data_len )
597{
598 size_t padding_len = output_len - data_len;
599 unsigned char i = 0;
600
601 output[data_len] = 0x80;
602 for( i = 1; i < padding_len; i++ )
603 output[data_len + i] = 0x00;
604}
605
606static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
607 size_t *data_len )
608{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100609 size_t i;
610 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200611
612 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200614
Micha Krausba8316f2017-12-23 23:40:08 +0100615 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100616 *data_len = 0;
617 for( i = input_len; i > 0; i-- )
618 {
619 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100620 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100621 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100622 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100623 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200626
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200627}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200631/*
632 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
633 */
634static void add_zeros_and_len_padding( unsigned char *output,
635 size_t output_len, size_t data_len )
636{
637 size_t padding_len = output_len - data_len;
638 unsigned char i = 0;
639
640 for( i = 1; i < padding_len; i++ )
641 output[data_len + i - 1] = 0x00;
642 output[output_len - 1] = (unsigned char) padding_len;
643}
644
645static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
646 size_t *data_len )
647{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100648 size_t i, pad_idx;
649 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200650
651 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200653
654 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200655 *data_len = input_len - padding_len;
656
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100657 /* Avoid logical || since it results in a branch */
658 bad |= padding_len > input_len;
659 bad |= padding_len == 0;
660
661 /* The number of bytes checked must be independent of padding_len */
662 pad_idx = input_len - padding_len;
663 for( i = 0; i < input_len - 1; i++ )
664 bad |= input[i] * ( i >= pad_idx );
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200671/*
672 * Zero padding: fill with 00 ... 00
673 */
674static void add_zeros_padding( unsigned char *output,
675 size_t output_len, size_t data_len )
676{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200677 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200678
679 for( i = data_len; i < output_len; i++ )
680 output[i] = 0x00;
681}
682
683static int get_zeros_padding( unsigned char *input, size_t input_len,
684 size_t *data_len )
685{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100686 size_t i;
687 unsigned char done = 0, prev_done;
688
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200689 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200691
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100692 *data_len = 0;
693 for( i = input_len; i > 0; i-- )
694 {
695 prev_done = done;
696 done |= ( input[i-1] != 0 );
697 *data_len |= i * ( done != prev_done );
698 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200699
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200700 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200703
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200704/*
705 * No padding: don't pad :)
706 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200708 * but a trivial get_padding function
709 */
710static int get_no_padding( unsigned char *input, size_t input_len,
711 size_t *data_len )
712{
713 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200715
716 *data_len = input_len;
717
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200718 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200723 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724{
725 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000727
728 *olen = 0;
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
731 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
732 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
733 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000734 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000736 }
737
Daniel King8fe47012016-05-17 20:33:28 -0300738 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
739 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300740 {
741 return( 0 );
742 }
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200745 {
746 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200748
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200749 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_CIPHER_MODE_CBC)
753 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200755 int ret = 0;
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200759 /* check for 'no padding' mode */
760 if( NULL == ctx->add_padding )
761 {
762 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200764
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200765 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200766 }
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769 ctx->unprocessed_len );
770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200773 /*
774 * For decrypt operations, expect a full block,
775 * or an empty block if no padding
776 */
777 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200778 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781 }
782
783 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000784 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000786 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200788 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000789 }
790
791 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( MBEDTLS_DECRYPT == ctx->operation )
793 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200794 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000795
796 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200798 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200800#else
801 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805}
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
808int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200809{
810 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200814 }
815
Paul Bakker1a45d912013-08-14 12:04:26 +0200816 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
819 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200820 ctx->add_padding = add_pkcs_padding;
821 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200822 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200823#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
825 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826 ctx->add_padding = add_one_and_zeros_padding;
827 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200828 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
831 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200832 ctx->add_padding = add_zeros_and_len_padding;
833 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200834 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200835#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
837 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200838 ctx->add_padding = add_zeros_padding;
839 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200840 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200841#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200843 ctx->add_padding = NULL;
844 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200845 break;
846
847 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200849 }
850
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200851 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200854
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200855#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200857 unsigned char *tag, size_t tag_len )
858{
859 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( MBEDTLS_ENCRYPT != ctx->operation )
863 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200864
Daniel King8fe47012016-05-17 20:33:28 -0300865#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
867 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300868#endif
869
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200870#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300871 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
872 {
873 /* Don't allow truncated MAC for Poly1305 */
874 if ( tag_len != 16U )
875 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
876
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200877 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300878 tag );
879 }
880#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200881
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200882 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200883}
Paul Bakker9af723c2014-05-01 13:03:14 +0200884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200886 const unsigned char *tag, size_t tag_len )
887{
Daniel King8fe47012016-05-17 20:33:28 -0300888 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200889 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200890
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200891 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200895 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200896
Daniel King8fe47012016-05-17 20:33:28 -0300897#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200899 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200900 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200904 check_tag, tag_len ) ) )
905 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200906 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200907 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200908
909 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300910 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200912
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200913 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200914 }
Daniel King8fe47012016-05-17 20:33:28 -0300915#endif /* MBEDTLS_GCM_C */
916
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200917#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300918 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
919 {
920 /* Don't allow truncated MAC for Poly1305 */
921 if ( tag_len != sizeof( check_tag ) )
922 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
923
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200924 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300925 check_tag );
926 if ( ret != 0 )
927 {
928 return( ret );
929 }
930
931 /* Check the tag in "constant-time" */
932 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
933 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
934
935 return( 0 );
936 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200937#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200938
939 return( 0 );
940}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200941#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200942
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200943/*
944 * Packet-oriented wrapper for non-AEAD modes
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200947 const unsigned char *iv, size_t iv_len,
948 const unsigned char *input, size_t ilen,
949 unsigned char *output, size_t *olen )
950{
951 int ret;
952 size_t finish_olen;
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200955 return( ret );
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200958 return( ret );
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200961 return( ret );
962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200964 return( ret );
965
966 *olen += finish_olen;
967
968 return( 0 );
969}
970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200972/*
973 * Packet-oriented encryption for AEAD modes
974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200976 const unsigned char *iv, size_t iv_len,
977 const unsigned char *ad, size_t ad_len,
978 const unsigned char *input, size_t ilen,
979 unsigned char *output, size_t *olen,
980 unsigned char *tag, size_t tag_len )
981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_GCM_C)
983 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200984 {
985 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200987 iv, iv_len, ad, ad_len, input, output,
988 tag_len, tag ) );
989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_GCM_C */
991#if defined(MBEDTLS_CCM_C)
992 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200993 {
994 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200996 iv, iv_len, ad, ad_len, input, output,
997 tag, tag_len ) );
998 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001000#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001001 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1002 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001003 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001004 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001005 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001006 {
1007 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1008 }
1009
1010 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001011 return( mbedtls_chachapoly_crypt_and_tag( ctx->cipher_ctx,
1012 MBEDTLS_CHACHAPOLY_ENCRYPT,
1013 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001014 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001015#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001018}
1019
1020/*
1021 * Packet-oriented decryption for AEAD modes
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001024 const unsigned char *iv, size_t iv_len,
1025 const unsigned char *ad, size_t ad_len,
1026 const unsigned char *input, size_t ilen,
1027 unsigned char *output, size_t *olen,
1028 const unsigned char *tag, size_t tag_len )
1029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_GCM_C)
1031 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001032 {
1033 int ret;
1034
1035 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001037 iv, iv_len, ad, ad_len,
1038 tag, tag_len, input, output );
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1041 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001042
1043 return( ret );
1044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#endif /* MBEDTLS_GCM_C */
1046#if defined(MBEDTLS_CCM_C)
1047 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001048 {
1049 int ret;
1050
1051 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001053 iv, iv_len, ad, ad_len,
1054 input, output, tag, tag_len );
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1057 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001058
1059 return( ret );
1060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001062#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001063 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1064 {
Daniel King8fe47012016-05-17 20:33:28 -03001065 int ret;
1066
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001067 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001068 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001069 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001070 {
1071 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1072 }
1073
1074 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001075 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1076 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001077
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001078 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1079 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001080
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001081 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001082 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001083#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_CIPHER_C */