blob: d7acf34ee5fc2142d9d536ecf3d69ea765f5dcd0 [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é-Gonnarddca3a5d2018-05-07 10:43:27 +020068#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030069/* Compare the contents of two buffers in constant time.
70 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030071 * a non-zero value.
72 * This is currently only used by GCM and ChaCha20+Poly1305.
73 */
Daniel King8fe47012016-05-17 20:33:28 -030074static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
75{
76 const unsigned char *p1 = (const unsigned char*) v1;
77 const unsigned char *p2 = (const unsigned char*) v2;
78 size_t i;
79 unsigned char diff;
80
81 for( diff = 0, i = 0; i < len; i++ )
82 diff |= p1[i] ^ p2[i];
83
84 return (int)diff;
85}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020086#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030087
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020093 int *type;
94
95 if( ! supported_init )
96 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 def = mbedtls_cipher_definitions;
98 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020099
100 while( def->type != 0 )
101 *type++ = (*def++).type;
102
103 *type = 0;
104
105 supported_init = 1;
106 }
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200116 if( def->type == cipher_type )
117 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000118
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200119 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200125
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200127 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200130 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200131 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000132
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200133 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136const 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 +0200137 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200143 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200144 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200145 def->info->mode == mode )
146 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200147
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200148 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200149}
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157{
158 if( ctx == NULL )
159 return;
160
Simon Butcher327398a2016-10-05 14:09:11 +0100161#if defined(MBEDTLS_CMAC_C)
162 if( ctx->cmac_ctx )
163 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500164 mbedtls_platform_zeroize( ctx->cmac_ctx,
165 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100166 mbedtls_free( ctx->cmac_ctx );
167 }
168#endif
169
Paul Bakker84bbeb52014-07-01 14:53:22 +0200170 if( ctx->cipher_ctx )
171 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
172
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500173 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200174}
175
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200176int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177{
178 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182
Paul Bakker343a8702011-06-09 14:27:58 +0000183 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185
186 ctx->cipher_info = cipher_info;
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200189 /*
190 * Ignore possible errors caused by a cipher mode that doesn't use padding
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
193 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200194#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200198
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200199 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200}
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200203 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204{
205 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200209 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200212 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200213
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200214 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 ctx->operation = operation;
216
Paul Bakker343a8702011-06-09 14:27:58 +0000217 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100218 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 if( MBEDTLS_ENCRYPT == operation ||
221 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100222 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000224 {
225 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200226 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000227 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000230 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200231 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234}
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200237 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200239 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300240 if( NULL == ctx || NULL == ctx->cipher_info )
241 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
242 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300245 if( NULL == iv && iv_len == 0 )
246 ctx->iv_size = 0;
247
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
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300275 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300276 {
277 memcpy( ctx->iv, iv, actual_iv_size );
278 ctx->iv_size = actual_iv_size;
279 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200280
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200281 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200282}
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200285{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200286 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200288
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289 ctx->unprocessed_len = 0;
290
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200291 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200292}
293
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200294#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200296 const unsigned char *ad, size_t ad_len )
297{
298 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200300
Daniel King8fe47012016-05-17 20:33:28 -0300301#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200305 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200306 }
Daniel King8fe47012016-05-17 20:33:28 -0300307#endif
308
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200309#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300310 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
311 {
312 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200313 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300314
315 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200316 ? MBEDTLS_CHACHAPOLY_ENCRYPT
317 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300318
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200319 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300320 ctx->iv,
321 mode );
322 if ( result != 0 )
323 return( result );
324
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200325 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200326 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300327 }
328#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200329
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200330 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200332#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200335 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336{
Paul Bakkerff61a782011-06-09 15:42:02 +0000337 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100338 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339
Paul Bakker68884e32013-01-07 18:20:04 +0100340 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000343 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
Paul Bakker6c212762013-12-16 15:24:50 +0100345 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100346 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200349 {
Janos Follath98e28a72016-05-31 14:03:54 +0100350 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200352
353 *olen = ilen;
354
355 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
356 ctx->operation, input, output ) ) )
357 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200359 }
360
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_GCM_C)
365 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200366 {
367 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200369 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200370 }
371#endif
372
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200373#if defined(MBEDTLS_CHACHAPOLY_C)
374 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
375 {
376 *olen = ilen;
377 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
378 ilen, input, output );
379 }
380#endif
381
Janos Follath98e28a72016-05-31 14:03:54 +0100382 if ( 0 == block_size )
383 {
384 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
385 }
386
Paul Bakker68884e32013-01-07 18:20:04 +0100387 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100388 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100391 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_CIPHER_MODE_CBC)
394 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200396 size_t copy_len = 0;
397
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398 /*
399 * If there is not enough data for a full block, cache it.
400 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700401 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000402 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700403 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
404 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000406 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 {
408 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
409 ilen );
410
411 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413 }
414
415 /*
416 * Process cached data first
417 */
Janos Follath98e28a72016-05-31 14:03:54 +0100418 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000419 {
Janos Follath98e28a72016-05-31 14:03:54 +0100420 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421
422 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
423 copy_len );
424
Paul Bakkerff61a782011-06-09 15:42:02 +0000425 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100426 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000427 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200429 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 }
431
Janos Follath98e28a72016-05-31 14:03:54 +0100432 *olen += block_size;
433 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434 ctx->unprocessed_len = 0;
435
436 input += copy_len;
437 ilen -= copy_len;
438 }
439
440 /*
441 * Cache final, incomplete block
442 */
443 if( 0 != ilen )
444 {
Janos Follath98e28a72016-05-31 14:03:54 +0100445 if( 0 == block_size )
446 {
447 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
448 }
449
Andy Leiserson79e77892017-04-28 20:01:49 -0700450 /* Encryption: only cache partial blocks
451 * Decryption w/ padding: always keep at least one whole block
452 * Decryption w/o padding: only cache partial blocks
453 */
Janos Follath98e28a72016-05-31 14:03:54 +0100454 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700455 if( copy_len == 0 &&
456 ctx->operation == MBEDTLS_DECRYPT &&
457 NULL != ctx->add_padding)
458 {
Janos Follath98e28a72016-05-31 14:03:54 +0100459 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700460 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461
462 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
463 copy_len );
464
465 ctx->unprocessed_len += copy_len;
466 ilen -= copy_len;
467 }
468
469 /*
470 * Process remaining full blocks
471 */
472 if( ilen )
473 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000474 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
475 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200477 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200479
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480 *olen += ilen;
481 }
482
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200483 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_CIPHER_MODE_CFB)
488 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000489 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000490 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000491 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000492 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000493 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000495 }
496
497 *olen = ilen;
498
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200499 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000502
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100503#if defined(MBEDTLS_CIPHER_MODE_OFB)
504 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
505 {
506 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
507 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
508 {
509 return( ret );
510 }
511
512 *olen = ilen;
513
514 return( 0 );
515 }
516#endif /* MBEDTLS_CIPHER_MODE_OFB */
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_CIPHER_MODE_CTR)
519 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000520 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000521 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000522 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000523 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000524 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200525 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000526 }
527
528 *olen = ilen;
529
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200530 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000533
Jaeden Ameroc6539902018-04-30 17:17:41 +0100534#if defined(MBEDTLS_CIPHER_MODE_XTS)
535 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
536 {
537 if( ctx->unprocessed_len > 0 ) {
538 /* We can only process an entire data unit at a time. */
539 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
540 }
541
542 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
543 ctx->operation, ilen, ctx->iv, input, output );
544 if( ret != 0 )
545 {
546 return( ret );
547 }
548
549 *olen = ilen;
550
551 return( 0 );
552 }
553#endif /* MBEDTLS_CIPHER_MODE_XTS */
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_CIPHER_MODE_STREAM)
556 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200557 {
558 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
559 ilen, input, output ) ) )
560 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200561 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200562 }
563
564 *olen = ilen;
565
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200566 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
574#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200575/*
576 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
577 */
Paul Bakker23986e52011-04-24 08:57:21 +0000578static void add_pkcs_padding( unsigned char *output, size_t output_len,
579 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580{
Paul Bakker23986e52011-04-24 08:57:21 +0000581 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100582 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
584 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000585 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586}
587
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200588static int get_pkcs_padding( unsigned char *input, size_t input_len,
589 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100591 size_t i, pad_idx;
592 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593
Paul Bakkera885d682011-01-20 16:35:05 +0000594 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596
597 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598 *data_len = input_len - padding_len;
599
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100600 /* Avoid logical || since it results in a branch */
601 bad |= padding_len > input_len;
602 bad |= padding_len == 0;
603
604 /* The number of bytes checked must be independent of padding_len,
605 * so pick input_len, which is usually 8 or 16 (one block) */
606 pad_idx = input_len - padding_len;
607 for( i = 0; i < input_len; i++ )
608 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200615/*
616 * One and zeros padding: fill with 80 00 ... 00
617 */
618static void add_one_and_zeros_padding( unsigned char *output,
619 size_t output_len, size_t data_len )
620{
621 size_t padding_len = output_len - data_len;
622 unsigned char i = 0;
623
624 output[data_len] = 0x80;
625 for( i = 1; i < padding_len; i++ )
626 output[data_len + i] = 0x00;
627}
628
629static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
630 size_t *data_len )
631{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100632 size_t i;
633 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200634
635 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200637
Micha Krausba8316f2017-12-23 23:40:08 +0100638 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100639 *data_len = 0;
640 for( i = input_len; i > 0; i-- )
641 {
642 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100643 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100644 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100645 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100646 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200649
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200654/*
655 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
656 */
657static void add_zeros_and_len_padding( unsigned char *output,
658 size_t output_len, size_t data_len )
659{
660 size_t padding_len = output_len - data_len;
661 unsigned char i = 0;
662
663 for( i = 1; i < padding_len; i++ )
664 output[data_len + i - 1] = 0x00;
665 output[output_len - 1] = (unsigned char) padding_len;
666}
667
668static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
669 size_t *data_len )
670{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100671 size_t i, pad_idx;
672 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200673
674 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200676
677 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200678 *data_len = input_len - padding_len;
679
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100680 /* Avoid logical || since it results in a branch */
681 bad |= padding_len > input_len;
682 bad |= padding_len == 0;
683
684 /* The number of bytes checked must be independent of padding_len */
685 pad_idx = input_len - padding_len;
686 for( i = 0; i < input_len - 1; i++ )
687 bad |= input[i] * ( i >= pad_idx );
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200694/*
695 * Zero padding: fill with 00 ... 00
696 */
697static void add_zeros_padding( unsigned char *output,
698 size_t output_len, size_t data_len )
699{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200700 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200701
702 for( i = data_len; i < output_len; i++ )
703 output[i] = 0x00;
704}
705
706static int get_zeros_padding( unsigned char *input, size_t input_len,
707 size_t *data_len )
708{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100709 size_t i;
710 unsigned char done = 0, prev_done;
711
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200712 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200714
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100715 *data_len = 0;
716 for( i = input_len; i > 0; i-- )
717 {
718 prev_done = done;
719 done |= ( input[i-1] != 0 );
720 *data_len |= i * ( done != prev_done );
721 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200722
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200723 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200726
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200727/*
728 * No padding: don't pad :)
729 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200731 * but a trivial get_padding function
732 */
733static int get_no_padding( unsigned char *input, size_t input_len,
734 size_t *data_len )
735{
736 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200738
739 *data_len = input_len;
740
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200741 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200746 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747{
748 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750
751 *olen = 0;
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100754 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
756 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100757 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000759 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200760 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000761 }
762
Daniel King8fe47012016-05-17 20:33:28 -0300763 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
764 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300765 {
766 return( 0 );
767 }
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200770 {
771 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200773
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200774 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200775 }
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_CIPHER_MODE_CBC)
778 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000779 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200780 int ret = 0;
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200784 /* check for 'no padding' mode */
785 if( NULL == ctx->add_padding )
786 {
787 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200789
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200790 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200791 }
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794 ctx->unprocessed_len );
795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200798 /*
799 * For decrypt operations, expect a full block,
800 * or an empty block if no padding
801 */
802 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200803 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000806 }
807
808 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000809 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000811 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200813 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000814 }
815
816 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 if( MBEDTLS_DECRYPT == ctx->operation )
818 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200819 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000820
821 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200823 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000824 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200825#else
826 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830}
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
833int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200834{
835 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200839 }
840
Paul Bakker1a45d912013-08-14 12:04:26 +0200841 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
844 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200845 ctx->add_padding = add_pkcs_padding;
846 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200847 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
850 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200851 ctx->add_padding = add_one_and_zeros_padding;
852 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200853 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
856 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857 ctx->add_padding = add_zeros_and_len_padding;
858 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200859 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
862 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200863 ctx->add_padding = add_zeros_padding;
864 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200865 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200868 ctx->add_padding = NULL;
869 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200870 break;
871
872 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200874 }
875
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200876 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200879
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200880#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200882 unsigned char *tag, size_t tag_len )
883{
884 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 if( MBEDTLS_ENCRYPT != ctx->operation )
888 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200889
Daniel King8fe47012016-05-17 20:33:28 -0300890#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
892 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300893#endif
894
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200895#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300896 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
897 {
898 /* Don't allow truncated MAC for Poly1305 */
899 if ( tag_len != 16U )
900 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
901
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200902 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300903 tag );
904 }
905#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200906
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200907 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200908}
Paul Bakker9af723c2014-05-01 13:03:14 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200911 const unsigned char *tag, size_t tag_len )
912{
Daniel King8fe47012016-05-17 20:33:28 -0300913 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200914 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200915
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200916 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200920 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200921
Daniel King8fe47012016-05-17 20:33:28 -0300922#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200924 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200925 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200929 check_tag, tag_len ) ) )
930 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200931 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200932 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200933
934 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300935 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200937
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200938 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200939 }
Daniel King8fe47012016-05-17 20:33:28 -0300940#endif /* MBEDTLS_GCM_C */
941
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200942#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300943 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
944 {
945 /* Don't allow truncated MAC for Poly1305 */
946 if ( tag_len != sizeof( check_tag ) )
947 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
948
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200949 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300950 check_tag );
951 if ( ret != 0 )
952 {
953 return( ret );
954 }
955
956 /* Check the tag in "constant-time" */
957 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
958 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
959
960 return( 0 );
961 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200962#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200963
964 return( 0 );
965}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200966#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200967
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200968/*
969 * Packet-oriented wrapper for non-AEAD modes
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200972 const unsigned char *iv, size_t iv_len,
973 const unsigned char *input, size_t ilen,
974 unsigned char *output, size_t *olen )
975{
976 int ret;
977 size_t finish_olen;
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200980 return( ret );
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200983 return( ret );
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200986 return( ret );
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200989 return( ret );
990
991 *olen += finish_olen;
992
993 return( 0 );
994}
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200997/*
998 * Packet-oriented encryption for AEAD modes
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001001 const unsigned char *iv, size_t iv_len,
1002 const unsigned char *ad, size_t ad_len,
1003 const unsigned char *input, size_t ilen,
1004 unsigned char *output, size_t *olen,
1005 unsigned char *tag, size_t tag_len )
1006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_GCM_C)
1008 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001009 {
1010 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001012 iv, iv_len, ad, ad_len, input, output,
1013 tag_len, tag ) );
1014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_GCM_C */
1016#if defined(MBEDTLS_CCM_C)
1017 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001018 {
1019 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001021 iv, iv_len, ad, ad_len, input, output,
1022 tag, tag_len ) );
1023 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001025#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001026 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1027 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001028 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001029 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001030 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001031 {
1032 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1033 }
1034
1035 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001036 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001037 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001038 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001039#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001042}
1043
1044/*
1045 * Packet-oriented decryption for AEAD modes
1046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001048 const unsigned char *iv, size_t iv_len,
1049 const unsigned char *ad, size_t ad_len,
1050 const unsigned char *input, size_t ilen,
1051 unsigned char *output, size_t *olen,
1052 const unsigned char *tag, size_t tag_len )
1053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_GCM_C)
1055 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001056 {
1057 int ret;
1058
1059 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001061 iv, iv_len, ad, ad_len,
1062 tag, tag_len, input, output );
1063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1065 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001066
1067 return( ret );
1068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_GCM_C */
1070#if defined(MBEDTLS_CCM_C)
1071 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001072 {
1073 int ret;
1074
1075 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001077 iv, iv_len, ad, ad_len,
1078 input, output, tag, tag_len );
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1081 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001082
1083 return( ret );
1084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001086#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001087 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1088 {
Daniel King8fe47012016-05-17 20:33:28 -03001089 int ret;
1090
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001091 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001092 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001093 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001094 {
1095 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1096 }
1097
1098 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001099 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1100 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001101
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001102 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1103 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001104
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001105 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001106 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001107#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_CIPHER_C */