blob: da55206514c6e1738c29bf64d50b3725296fa818 [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
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200245 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
247 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200250 actual_iv_size = iv_len;
251 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200252 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200253 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200254
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200255 /* avoid reading past the end of input buffer */
256 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200258 }
259
Daniel Kingbd920622016-05-15 19:56:20 -0300260#if defined(MBEDTLS_CHACHA20_C)
261 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
262 {
263 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
264 iv,
265 0U ) ) /* Initial counter value */
266 {
267 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
268 }
269 }
270#endif
271
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300272 if ( actual_iv_size )
273 {
274 memcpy( ctx->iv, iv, actual_iv_size );
275 ctx->iv_size = actual_iv_size;
276 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200277
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200278 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200279}
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200282{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200283 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200285
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286 ctx->unprocessed_len = 0;
287
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200288 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200289}
290
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200291#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200293 const unsigned char *ad, size_t ad_len )
294{
295 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200297
Daniel King8fe47012016-05-17 20:33:28 -0300298#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200302 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200303 }
Daniel King8fe47012016-05-17 20:33:28 -0300304#endif
305
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200306#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300307 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
308 {
309 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200310 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300311
312 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200313 ? MBEDTLS_CHACHAPOLY_ENCRYPT
314 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300315
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200316 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300317 ctx->iv,
318 mode );
319 if ( result != 0 )
320 return( result );
321
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200322 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200323 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300324 }
325#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200326
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200327 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200329#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200332 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333{
Paul Bakkerff61a782011-06-09 15:42:02 +0000334 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100335 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336
Paul Bakker68884e32013-01-07 18:20:04 +0100337 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000340 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341
Paul Bakker6c212762013-12-16 15:24:50 +0100342 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100343 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200346 {
Janos Follath98e28a72016-05-31 14:03:54 +0100347 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200349
350 *olen = ilen;
351
352 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
353 ctx->operation, input, output ) ) )
354 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200355 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200356 }
357
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200359 }
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_GCM_C)
362 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200363 {
364 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200366 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200367 }
368#endif
369
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200370#if defined(MBEDTLS_CHACHAPOLY_C)
371 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
372 {
373 *olen = ilen;
374 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
375 ilen, input, output );
376 }
377#endif
378
Janos Follath98e28a72016-05-31 14:03:54 +0100379 if ( 0 == block_size )
380 {
381 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
382 }
383
Paul Bakker68884e32013-01-07 18:20:04 +0100384 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100385 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100388 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_CIPHER_MODE_CBC)
391 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200393 size_t copy_len = 0;
394
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 /*
396 * If there is not enough data for a full block, cache it.
397 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700398 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000399 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700400 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
401 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000403 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404 {
405 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
406 ilen );
407
408 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200409 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410 }
411
412 /*
413 * Process cached data first
414 */
Janos Follath98e28a72016-05-31 14:03:54 +0100415 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416 {
Janos Follath98e28a72016-05-31 14:03:54 +0100417 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418
419 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
420 copy_len );
421
Paul Bakkerff61a782011-06-09 15:42:02 +0000422 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100423 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000424 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 }
428
Janos Follath98e28a72016-05-31 14:03:54 +0100429 *olen += block_size;
430 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431 ctx->unprocessed_len = 0;
432
433 input += copy_len;
434 ilen -= copy_len;
435 }
436
437 /*
438 * Cache final, incomplete block
439 */
440 if( 0 != ilen )
441 {
Janos Follath98e28a72016-05-31 14:03:54 +0100442 if( 0 == block_size )
443 {
444 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
445 }
446
Andy Leiserson79e77892017-04-28 20:01:49 -0700447 /* Encryption: only cache partial blocks
448 * Decryption w/ padding: always keep at least one whole block
449 * Decryption w/o padding: only cache partial blocks
450 */
Janos Follath98e28a72016-05-31 14:03:54 +0100451 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700452 if( copy_len == 0 &&
453 ctx->operation == MBEDTLS_DECRYPT &&
454 NULL != ctx->add_padding)
455 {
Janos Follath98e28a72016-05-31 14:03:54 +0100456 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700457 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
459 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
460 copy_len );
461
462 ctx->unprocessed_len += copy_len;
463 ilen -= copy_len;
464 }
465
466 /*
467 * Process remaining full blocks
468 */
469 if( ilen )
470 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000471 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
472 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200474 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200476
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477 *olen += ilen;
478 }
479
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200480 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_CIPHER_MODE_CFB)
485 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000486 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000487 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000488 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000489 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000490 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200491 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000492 }
493
494 *olen = ilen;
495
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000499
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100500#if defined(MBEDTLS_CIPHER_MODE_OFB)
501 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
502 {
503 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
504 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
505 {
506 return( ret );
507 }
508
509 *olen = ilen;
510
511 return( 0 );
512 }
513#endif /* MBEDTLS_CIPHER_MODE_OFB */
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_CIPHER_MODE_CTR)
516 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000517 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000518 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000519 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000520 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000521 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000523 }
524
525 *olen = ilen;
526
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200527 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000530
Jaeden Ameroc6539902018-04-30 17:17:41 +0100531#if defined(MBEDTLS_CIPHER_MODE_XTS)
532 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
533 {
534 if( ctx->unprocessed_len > 0 ) {
535 /* We can only process an entire data unit at a time. */
536 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
537 }
538
539 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
540 ctx->operation, ilen, ctx->iv, input, output );
541 if( ret != 0 )
542 {
543 return( ret );
544 }
545
546 *olen = ilen;
547
548 return( 0 );
549 }
550#endif /* MBEDTLS_CIPHER_MODE_XTS */
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_CIPHER_MODE_STREAM)
553 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200554 {
555 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
556 ilen, input, output ) ) )
557 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200558 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200559 }
560
561 *olen = ilen;
562
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200563 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568}
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
571#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200572/*
573 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
574 */
Paul Bakker23986e52011-04-24 08:57:21 +0000575static void add_pkcs_padding( unsigned char *output, size_t output_len,
576 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577{
Paul Bakker23986e52011-04-24 08:57:21 +0000578 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100579 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580
581 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000582 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583}
584
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200585static int get_pkcs_padding( unsigned char *input, size_t input_len,
586 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100588 size_t i, pad_idx;
589 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Paul Bakkera885d682011-01-20 16:35:05 +0000591 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593
594 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 *data_len = input_len - padding_len;
596
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100597 /* Avoid logical || since it results in a branch */
598 bad |= padding_len > input_len;
599 bad |= padding_len == 0;
600
601 /* The number of bytes checked must be independent of padding_len,
602 * so pick input_len, which is usually 8 or 16 (one block) */
603 pad_idx = input_len - padding_len;
604 for( i = 0; i < input_len; i++ )
605 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200612/*
613 * One and zeros padding: fill with 80 00 ... 00
614 */
615static void add_one_and_zeros_padding( unsigned char *output,
616 size_t output_len, size_t data_len )
617{
618 size_t padding_len = output_len - data_len;
619 unsigned char i = 0;
620
621 output[data_len] = 0x80;
622 for( i = 1; i < padding_len; i++ )
623 output[data_len + i] = 0x00;
624}
625
626static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
627 size_t *data_len )
628{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100629 size_t i;
630 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200631
632 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200634
Micha Krausba8316f2017-12-23 23:40:08 +0100635 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100636 *data_len = 0;
637 for( i = input_len; i > 0; i-- )
638 {
639 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100640 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100641 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100642 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100643 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200646
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200651/*
652 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
653 */
654static void add_zeros_and_len_padding( unsigned char *output,
655 size_t output_len, size_t data_len )
656{
657 size_t padding_len = output_len - data_len;
658 unsigned char i = 0;
659
660 for( i = 1; i < padding_len; i++ )
661 output[data_len + i - 1] = 0x00;
662 output[output_len - 1] = (unsigned char) padding_len;
663}
664
665static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
666 size_t *data_len )
667{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100668 size_t i, pad_idx;
669 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200670
671 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200673
674 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200675 *data_len = input_len - padding_len;
676
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100677 /* Avoid logical || since it results in a branch */
678 bad |= padding_len > input_len;
679 bad |= padding_len == 0;
680
681 /* The number of bytes checked must be independent of padding_len */
682 pad_idx = input_len - padding_len;
683 for( i = 0; i < input_len - 1; i++ )
684 bad |= input[i] * ( i >= pad_idx );
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200691/*
692 * Zero padding: fill with 00 ... 00
693 */
694static void add_zeros_padding( unsigned char *output,
695 size_t output_len, size_t data_len )
696{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200697 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200698
699 for( i = data_len; i < output_len; i++ )
700 output[i] = 0x00;
701}
702
703static int get_zeros_padding( unsigned char *input, size_t input_len,
704 size_t *data_len )
705{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100706 size_t i;
707 unsigned char done = 0, prev_done;
708
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200709 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200711
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100712 *data_len = 0;
713 for( i = input_len; i > 0; i-- )
714 {
715 prev_done = done;
716 done |= ( input[i-1] != 0 );
717 *data_len |= i * ( done != prev_done );
718 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200719
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200720 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200723
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200724/*
725 * No padding: don't pad :)
726 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200728 * but a trivial get_padding function
729 */
730static int get_no_padding( unsigned char *input, size_t input_len,
731 size_t *data_len )
732{
733 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200735
736 *data_len = input_len;
737
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200738 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200743 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744{
745 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
748 *olen = 0;
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100751 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
753 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100754 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000756 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200757 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000758 }
759
Daniel King8fe47012016-05-17 20:33:28 -0300760 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
761 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300762 {
763 return( 0 );
764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200767 {
768 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200770
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200771 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200772 }
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_CIPHER_MODE_CBC)
775 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200777 int ret = 0;
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200781 /* check for 'no padding' mode */
782 if( NULL == ctx->add_padding )
783 {
784 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200786
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200787 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200788 }
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000791 ctx->unprocessed_len );
792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200795 /*
796 * For decrypt operations, expect a full block,
797 * or an empty block if no padding
798 */
799 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200800 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000803 }
804
805 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000806 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000808 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000809 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200810 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811 }
812
813 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( MBEDTLS_DECRYPT == ctx->operation )
815 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200816 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000817
818 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200820 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000821 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200822#else
823 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827}
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
830int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200831{
832 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200836 }
837
Paul Bakker1a45d912013-08-14 12:04:26 +0200838 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
841 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200842 ctx->add_padding = add_pkcs_padding;
843 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200844 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
847 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200848 ctx->add_padding = add_one_and_zeros_padding;
849 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200850 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
853 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200854 ctx->add_padding = add_zeros_and_len_padding;
855 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200856 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
859 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200860 ctx->add_padding = add_zeros_padding;
861 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200862 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200865 ctx->add_padding = NULL;
866 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200867 break;
868
869 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200871 }
872
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200873 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200876
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200877#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200879 unsigned char *tag, size_t tag_len )
880{
881 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( MBEDTLS_ENCRYPT != ctx->operation )
885 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200886
Daniel King8fe47012016-05-17 20:33:28 -0300887#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
889 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300890#endif
891
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200892#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300893 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
894 {
895 /* Don't allow truncated MAC for Poly1305 */
896 if ( tag_len != 16U )
897 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
898
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200899 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300900 tag );
901 }
902#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200903
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200904 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200905}
Paul Bakker9af723c2014-05-01 13:03:14 +0200906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200908 const unsigned char *tag, size_t tag_len )
909{
Daniel King8fe47012016-05-17 20:33:28 -0300910 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200911 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200912
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200913 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200917 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200918
Daniel King8fe47012016-05-17 20:33:28 -0300919#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200921 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200922 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200926 check_tag, tag_len ) ) )
927 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200928 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200929 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200930
931 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300932 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200934
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200935 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200936 }
Daniel King8fe47012016-05-17 20:33:28 -0300937#endif /* MBEDTLS_GCM_C */
938
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200939#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300940 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
941 {
942 /* Don't allow truncated MAC for Poly1305 */
943 if ( tag_len != sizeof( check_tag ) )
944 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
945
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200946 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300947 check_tag );
948 if ( ret != 0 )
949 {
950 return( ret );
951 }
952
953 /* Check the tag in "constant-time" */
954 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
955 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
956
957 return( 0 );
958 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200959#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200960
961 return( 0 );
962}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200963#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200964
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200965/*
966 * Packet-oriented wrapper for non-AEAD modes
967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200969 const unsigned char *iv, size_t iv_len,
970 const unsigned char *input, size_t ilen,
971 unsigned char *output, size_t *olen )
972{
973 int ret;
974 size_t finish_olen;
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200977 return( ret );
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 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_update( ctx, input, ilen, output, olen ) ) != 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_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200986 return( ret );
987
988 *olen += finish_olen;
989
990 return( 0 );
991}
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200994/*
995 * Packet-oriented encryption for AEAD modes
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200998 const unsigned char *iv, size_t iv_len,
999 const unsigned char *ad, size_t ad_len,
1000 const unsigned char *input, size_t ilen,
1001 unsigned char *output, size_t *olen,
1002 unsigned char *tag, size_t tag_len )
1003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_GCM_C)
1005 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001006 {
1007 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001009 iv, iv_len, ad, ad_len, input, output,
1010 tag_len, tag ) );
1011 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_GCM_C */
1013#if defined(MBEDTLS_CCM_C)
1014 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001015 {
1016 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001018 iv, iv_len, ad, ad_len, input, output,
1019 tag, tag_len ) );
1020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001022#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001023 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1024 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001025 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001026 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001027 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001028 {
1029 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1030 }
1031
1032 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001033 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001034 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001035 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001036#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001039}
1040
1041/*
1042 * Packet-oriented decryption for AEAD modes
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001045 const unsigned char *iv, size_t iv_len,
1046 const unsigned char *ad, size_t ad_len,
1047 const unsigned char *input, size_t ilen,
1048 unsigned char *output, size_t *olen,
1049 const unsigned char *tag, size_t tag_len )
1050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_GCM_C)
1052 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001053 {
1054 int ret;
1055
1056 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001058 iv, iv_len, ad, ad_len,
1059 tag, tag_len, input, output );
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1062 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001063
1064 return( ret );
1065 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#endif /* MBEDTLS_GCM_C */
1067#if defined(MBEDTLS_CCM_C)
1068 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001069 {
1070 int ret;
1071
1072 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001074 iv, iv_len, ad, ad_len,
1075 input, output, tag, tag_len );
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1078 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001079
1080 return( ret );
1081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001083#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001084 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1085 {
Daniel King8fe47012016-05-17 20:33:28 -03001086 int ret;
1087
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001088 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001089 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001090 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001091 {
1092 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1093 }
1094
1095 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001096 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1097 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001098
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001099 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1100 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001101
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001102 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001103 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001104#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_CIPHER_C */