blob: 7ae6c4ac5d1ef8cecbb9b716ed9f012f979a9196 [file] [log] [blame] [raw]
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
Paul Bakker8123e9d2011-01-06 15:37:30 +000052
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 */
Paul Bakker34617722014-06-13 17:20:13 +020087
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;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000216
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 );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200234}
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;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200240
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200244 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
246 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200249 actual_iv_size = iv_len;
250 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200251 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200252 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200253
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200254 /* avoid reading past the end of input buffer */
255 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200257 }
258
Daniel Kingbd920622016-05-15 19:56:20 -0300259#if defined(MBEDTLS_CHACHA20_C)
260 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
261 {
262 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
263 iv,
264 0U ) ) /* Initial counter value */
265 {
266 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
267 }
268 }
269#endif
270
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200271 memcpy( ctx->iv, iv, actual_iv_size );
272 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200273
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200274 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200275}
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200278{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200279 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200281
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282 ctx->unprocessed_len = 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200283
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200284 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200285}
286
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200287#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200289 const unsigned char *ad, size_t ad_len )
290{
291 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
Daniel King8fe47012016-05-17 20:33:28 -0300294#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200298 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200299 }
Daniel King8fe47012016-05-17 20:33:28 -0300300#endif
301
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200302#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300303 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
304 {
305 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200306 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300307
308 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200309 ? MBEDTLS_CHACHAPOLY_ENCRYPT
310 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300311
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200312 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300313 ctx->iv,
314 mode );
315 if ( result != 0 )
316 return( result );
317
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200318 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200319 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300320 }
321#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200323 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200325#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200328 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329{
Paul Bakkerff61a782011-06-09 15:42:02 +0000330 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100331 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
Paul Bakker68884e32013-01-07 18:20:04 +0100333 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000336 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337
Paul Bakker6c212762013-12-16 15:24:50 +0100338 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100339 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200342 {
Janos Follath98e28a72016-05-31 14:03:54 +0100343 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200345
346 *olen = ilen;
347
348 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
349 ctx->operation, input, output ) ) )
350 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200352 }
353
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200354 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200355 }
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_GCM_C)
358 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200359 {
360 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200362 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200363 }
364#endif
365
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200366#if defined(MBEDTLS_CHACHAPOLY_C)
367 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
368 {
369 *olen = ilen;
370 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
371 ilen, input, output );
372 }
373#endif
374
Janos Follath98e28a72016-05-31 14:03:54 +0100375 if ( 0 == block_size )
376 {
377 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
378 }
379
Paul Bakker68884e32013-01-07 18:20:04 +0100380 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100381 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100384 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_CIPHER_MODE_CBC)
387 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200389 size_t copy_len = 0;
390
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391 /*
392 * If there is not enough data for a full block, cache it.
393 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700394 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000395 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700396 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
397 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000399 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400 {
401 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
402 ilen );
403
404 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200405 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406 }
407
408 /*
409 * Process cached data first
410 */
Janos Follath98e28a72016-05-31 14:03:54 +0100411 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412 {
Janos Follath98e28a72016-05-31 14:03:54 +0100413 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
415 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
416 copy_len );
417
Paul Bakkerff61a782011-06-09 15:42:02 +0000418 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100419 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000420 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423 }
424
Janos Follath98e28a72016-05-31 14:03:54 +0100425 *olen += block_size;
426 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 ctx->unprocessed_len = 0;
428
429 input += copy_len;
430 ilen -= copy_len;
431 }
432
433 /*
434 * Cache final, incomplete block
435 */
436 if( 0 != ilen )
437 {
Janos Follath98e28a72016-05-31 14:03:54 +0100438 if( 0 == block_size )
439 {
440 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
441 }
442
Andy Leiserson79e77892017-04-28 20:01:49 -0700443 /* Encryption: only cache partial blocks
444 * Decryption w/ padding: always keep at least one whole block
445 * Decryption w/o padding: only cache partial blocks
446 */
Janos Follath98e28a72016-05-31 14:03:54 +0100447 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700448 if( copy_len == 0 &&
449 ctx->operation == MBEDTLS_DECRYPT &&
450 NULL != ctx->add_padding)
451 {
Janos Follath98e28a72016-05-31 14:03:54 +0100452 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700453 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454
455 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
456 copy_len );
457
458 ctx->unprocessed_len += copy_len;
459 ilen -= copy_len;
460 }
461
462 /*
463 * Process remaining full blocks
464 */
465 if( ilen )
466 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000467 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
468 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200470 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200472
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473 *olen += ilen;
474 }
Paul Bakker343a8702011-06-09 14:27:58 +0000475
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200476 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_CIPHER_MODE_CFB)
481 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000482 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000483 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000484 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000485 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000486 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200487 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000488 }
489
490 *olen = ilen;
491
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200492 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000495
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100496#if defined(MBEDTLS_CIPHER_MODE_OFB)
497 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
498 {
499 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
500 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
501 {
502 return( ret );
503 }
504
505 *olen = ilen;
506
507 return( 0 );
508 }
509#endif /* MBEDTLS_CIPHER_MODE_OFB */
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_CIPHER_MODE_CTR)
512 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000513 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000514 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000515 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000516 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000517 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000519 }
520
521 *olen = ilen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200523 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526
Jaeden Ameroc6539902018-04-30 17:17:41 +0100527#if defined(MBEDTLS_CIPHER_MODE_XTS)
528 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
529 {
530 if( ctx->unprocessed_len > 0 ) {
531 /* We can only process an entire data unit at a time. */
532 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
533 }
534
535 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
536 ctx->operation, ilen, ctx->iv, input, output );
537 if( ret != 0 )
538 {
539 return( ret );
540 }
541
542 *olen = ilen;
543
544 return( 0 );
545 }
546#endif /* MBEDTLS_CIPHER_MODE_XTS */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_CIPHER_MODE_STREAM)
549 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200550 {
551 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
552 ilen, input, output ) ) )
553 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200554 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200555 }
556
557 *olen = ilen;
558
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200559 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564}
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
567#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200568/*
569 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
570 */
Paul Bakker23986e52011-04-24 08:57:21 +0000571static void add_pkcs_padding( unsigned char *output, size_t output_len,
572 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573{
Paul Bakker23986e52011-04-24 08:57:21 +0000574 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100575 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576
577 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000578 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579}
580
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200581static int get_pkcs_padding( unsigned char *input, size_t input_len,
582 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100584 size_t i, pad_idx;
585 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
Paul Bakkera885d682011-01-20 16:35:05 +0000587 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589
590 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 *data_len = input_len - padding_len;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200592
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100593 /* Avoid logical || since it results in a branch */
594 bad |= padding_len > input_len;
595 bad |= padding_len == 0;
596
597 /* The number of bytes checked must be independent of padding_len,
598 * so pick input_len, which is usually 8 or 16 (one block) */
599 pad_idx = input_len - padding_len;
600 for( i = 0; i < input_len; i++ )
601 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200608/*
609 * One and zeros padding: fill with 80 00 ... 00
610 */
611static void add_one_and_zeros_padding( unsigned char *output,
612 size_t output_len, size_t data_len )
613{
614 size_t padding_len = output_len - data_len;
615 unsigned char i = 0;
616
617 output[data_len] = 0x80;
618 for( i = 1; i < padding_len; i++ )
619 output[data_len + i] = 0x00;
620}
621
622static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
623 size_t *data_len )
624{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100625 size_t i;
626 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200627
628 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200630
Micha Krausba8316f2017-12-23 23:40:08 +0100631 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100632 *data_len = 0;
633 for( i = input_len; i > 0; i-- )
634 {
635 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100636 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100637 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100638 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100639 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200642
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200647/*
648 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
649 */
650static void add_zeros_and_len_padding( unsigned char *output,
651 size_t output_len, size_t data_len )
652{
653 size_t padding_len = output_len - data_len;
654 unsigned char i = 0;
655
656 for( i = 1; i < padding_len; i++ )
657 output[data_len + i - 1] = 0x00;
658 output[output_len - 1] = (unsigned char) padding_len;
659}
660
661static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
662 size_t *data_len )
663{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100664 size_t i, pad_idx;
665 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200666
667 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200669
670 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200671 *data_len = input_len - padding_len;
672
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100673 /* Avoid logical || since it results in a branch */
674 bad |= padding_len > input_len;
675 bad |= padding_len == 0;
676
677 /* The number of bytes checked must be independent of padding_len */
678 pad_idx = input_len - padding_len;
679 for( i = 0; i < input_len - 1; i++ )
680 bad |= input[i] * ( i >= pad_idx );
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200687/*
688 * Zero padding: fill with 00 ... 00
689 */
690static void add_zeros_padding( unsigned char *output,
691 size_t output_len, size_t data_len )
692{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200693 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200694
695 for( i = data_len; i < output_len; i++ )
696 output[i] = 0x00;
697}
698
699static int get_zeros_padding( unsigned char *input, size_t input_len,
700 size_t *data_len )
701{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100702 size_t i;
703 unsigned char done = 0, prev_done;
704
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200705 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200707
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100708 *data_len = 0;
709 for( i = input_len; i > 0; i-- )
710 {
711 prev_done = done;
712 done |= ( input[i-1] != 0 );
713 *data_len |= i * ( done != prev_done );
714 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200715
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200719
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200720/*
721 * No padding: don't pad :)
722 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200724 * but a trivial get_padding function
725 */
726static int get_no_padding( unsigned char *input, size_t input_len,
727 size_t *data_len )
728{
729 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200731
732 *data_len = input_len;
733
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200739 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740{
741 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743
744 *olen = 0;
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100747 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
749 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100750 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000752 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000754 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200755
Daniel King8fe47012016-05-17 20:33:28 -0300756 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
757 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300758 {
759 return( 0 );
760 }
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200763 {
764 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200766
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200767 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200768 }
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_CIPHER_MODE_CBC)
771 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200773 int ret = 0;
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200777 /* check for 'no padding' mode */
778 if( NULL == ctx->add_padding )
779 {
780 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200782
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200783 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200784 }
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787 ctx->unprocessed_len );
788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200791 /*
792 * For decrypt operations, expect a full block,
793 * or an empty block if no padding
794 */
795 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200796 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799 }
800
801 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000802 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000804 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200806 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000807 }
808
809 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( MBEDTLS_DECRYPT == ctx->operation )
811 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200812 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000813
814 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200816 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000817 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200818#else
819 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000823}
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
826int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200827{
828 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200832 }
833
Paul Bakker1a45d912013-08-14 12:04:26 +0200834 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
837 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200838 ctx->add_padding = add_pkcs_padding;
839 ctx->get_padding = get_pkcs_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#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
843 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200844 ctx->add_padding = add_one_and_zeros_padding;
845 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200846 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
849 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850 ctx->add_padding = add_zeros_and_len_padding;
851 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200852 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
855 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200856 ctx->add_padding = add_zeros_padding;
857 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200858 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200859#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200861 ctx->add_padding = NULL;
862 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200863 break;
864
865 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200867 }
868
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200869 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200872
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200873#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200875 unsigned char *tag, size_t tag_len )
876{
877 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( MBEDTLS_ENCRYPT != ctx->operation )
881 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200882
Daniel King8fe47012016-05-17 20:33:28 -0300883#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
885 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300886#endif
887
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200888#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300889 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
890 {
891 /* Don't allow truncated MAC for Poly1305 */
892 if ( tag_len != 16U )
893 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
894
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200895 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300896 tag );
897 }
898#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200899
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200900 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200901}
Paul Bakker9af723c2014-05-01 13:03:14 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200904 const unsigned char *tag, size_t tag_len )
905{
Daniel King8fe47012016-05-17 20:33:28 -0300906 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200907 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200908
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200909 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200913 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200914
Daniel King8fe47012016-05-17 20:33:28 -0300915#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200917 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200918 if( tag_len > sizeof( check_tag ) )
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200922 check_tag, tag_len ) ) )
923 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200924 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200925 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200926
927 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300928 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200930
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200931 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200932 }
Daniel King8fe47012016-05-17 20:33:28 -0300933#endif /* MBEDTLS_GCM_C */
934
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200935#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300936 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
937 {
938 /* Don't allow truncated MAC for Poly1305 */
939 if ( tag_len != sizeof( check_tag ) )
940 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
941
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200942 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300943 check_tag );
944 if ( ret != 0 )
945 {
946 return( ret );
947 }
948
949 /* Check the tag in "constant-time" */
950 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
951 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
952
953 return( 0 );
954 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200955#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200956
957 return( 0 );
958}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200959#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200960
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200961/*
962 * Packet-oriented wrapper for non-AEAD modes
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200965 const unsigned char *iv, size_t iv_len,
966 const unsigned char *input, size_t ilen,
967 unsigned char *output, size_t *olen )
968{
969 int ret;
970 size_t finish_olen;
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200973 return( ret );
974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200976 return( ret );
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200979 return( ret );
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200982 return( ret );
983
984 *olen += finish_olen;
985
986 return( 0 );
987}
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200990/*
991 * Packet-oriented encryption for AEAD modes
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200994 const unsigned char *iv, size_t iv_len,
995 const unsigned char *ad, size_t ad_len,
996 const unsigned char *input, size_t ilen,
997 unsigned char *output, size_t *olen,
998 unsigned char *tag, size_t tag_len )
999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_GCM_C)
1001 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001002 {
1003 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001005 iv, iv_len, ad, ad_len, input, output,
1006 tag_len, tag ) );
1007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#endif /* MBEDTLS_GCM_C */
1009#if defined(MBEDTLS_CCM_C)
1010 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001011 {
1012 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001014 iv, iv_len, ad, ad_len, input, output,
1015 tag, tag_len ) );
1016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001018#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001019 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1020 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001021 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001022 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001023 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001024 {
1025 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1026 }
1027
1028 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001029 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001030 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001031 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001032#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001035}
1036
1037/*
1038 * Packet-oriented decryption for AEAD modes
1039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001041 const unsigned char *iv, size_t iv_len,
1042 const unsigned char *ad, size_t ad_len,
1043 const unsigned char *input, size_t ilen,
1044 unsigned char *output, size_t *olen,
1045 const unsigned char *tag, size_t tag_len )
1046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_GCM_C)
1048 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001049 {
1050 int ret;
1051
1052 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001054 iv, iv_len, ad, ad_len,
1055 tag, tag_len, input, output );
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1058 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001059
1060 return( ret );
1061 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_GCM_C */
1063#if defined(MBEDTLS_CCM_C)
1064 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001065 {
1066 int ret;
1067
1068 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001070 iv, iv_len, ad, ad_len,
1071 input, output, tag, tag_len );
1072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1074 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001075
1076 return( ret );
1077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001079#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001080 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1081 {
Daniel King8fe47012016-05-17 20:33:28 -03001082 int ret;
1083
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001084 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001085 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001086 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001087 {
1088 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1089 }
1090
1091 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001092 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1093 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001094
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001095 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1096 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001097
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001098 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001099 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001100#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_CIPHER_C */