blob: 7054f4bca5b54dc5b935a630ad6a905631f7a472 [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 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000028 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Paul Bakker8123e9d2011-01-06 15:37:30 +000049 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020060#include "mbedtls/cipher_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stdlib.h>
63#include <string.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020071#endif
72
Simon Butcher327398a2016-10-05 14:09:11 +010073#if defined(MBEDTLS_CMAC_C)
74#include "mbedtls/cmac.h"
75#endif
76
77#if defined(MBEDTLS_PLATFORM_C)
78#include "mbedtls/platform.h"
79#else
80#define mbedtls_calloc calloc
81#define mbedtls_free free
82#endif
83
Paul Bakker34617722014-06-13 17:20:13 +020084/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010086 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020087}
88
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020089static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020094 int *type;
95
96 if( ! supported_init )
97 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 def = mbedtls_cipher_definitions;
99 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200100
101 while( def->type != 0 )
102 *type++ = (*def++).type;
103
104 *type = 0;
105
106 supported_init = 1;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117 if( def->type == cipher_type )
118 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000119
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200120 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121}
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200126
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200128 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200131 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200132 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000133
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200134 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135}
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137const 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 +0200138 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200144 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200145 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200146 def->info->mode == mode )
147 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200148
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200149 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200150}
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200155}
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200158{
159 if( ctx == NULL )
160 return;
161
Simon Butcher327398a2016-10-05 14:09:11 +0100162#if defined(MBEDTLS_CMAC_C)
163 if( ctx->cmac_ctx )
164 {
165 mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
166 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_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 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000218 * For 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 ||
222 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000223 {
224 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200225 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000226 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000229 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200230 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200236 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200238 size_t actual_iv_size;
Ron Eldorefba4b02017-09-25 18:22:32 +0300239 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Ron Eldorefba4b02017-09-25 18:22:32 +0300241 else if( NULL == iv && iv_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
Ron Eldorde881c02017-10-01 17:04:54 +0300244 if( NULL == iv && iv_len == 0 )
245 ctx->iv_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200247 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
249 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200252 actual_iv_size = iv_len;
253 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200254 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200255 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200256
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200257 /* avoid reading past the end of input buffer */
258 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200260 }
Ron Eldorde881c02017-10-01 17:04:54 +0300261 if ( actual_iv_size != 0 )
Ron Eldorefba4b02017-09-25 18:22:32 +0300262 {
263 memcpy( ctx->iv, iv, actual_iv_size );
264 ctx->iv_size = actual_iv_size;
265 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200266
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200271{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200272 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200274
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275 ctx->unprocessed_len = 0;
276
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200277 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_GCM_C)
281int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200282 const unsigned char *ad, size_t ad_len )
283{
284 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200290 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200291 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200292
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200293 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200298 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299{
Paul Bakkerff61a782011-06-09 15:42:02 +0000300 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100301 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302
Paul Bakker68884e32013-01-07 18:20:04 +0100303 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000306 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307
Paul Bakker6c212762013-12-16 15:24:50 +0100308 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100309 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskine0f595f72020-01-21 15:02:14 +0100310 if ( 0 == block_size )
311 {
312 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
313 }
Paul Bakker6c212762013-12-16 15:24:50 +0100314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200316 {
Janos Follath98e28a72016-05-31 14:03:54 +0100317 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200319
320 *olen = ilen;
321
322 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
323 ctx->operation, input, output ) ) )
324 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200325 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200326 }
327
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200328 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200329 }
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_GCM_C)
332 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200333 {
334 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200336 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200337 }
338#endif
339
Paul Bakker68884e32013-01-07 18:20:04 +0100340 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100341 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100344 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_CIPHER_MODE_CBC)
347 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200349 size_t copy_len = 0;
350
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351 /*
352 * If there is not enough data for a full block, cache it.
353 */
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700354 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000355 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700356 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
357 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000359 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360 {
361 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
362 ilen );
363
364 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200365 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 }
367
368 /*
369 * Process cached data first
370 */
Janos Follath98e28a72016-05-31 14:03:54 +0100371 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 {
Janos Follath98e28a72016-05-31 14:03:54 +0100373 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374
375 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
376 copy_len );
377
Paul Bakkerff61a782011-06-09 15:42:02 +0000378 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100379 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000380 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200382 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 }
384
Janos Follath98e28a72016-05-31 14:03:54 +0100385 *olen += block_size;
386 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387 ctx->unprocessed_len = 0;
388
389 input += copy_len;
390 ilen -= copy_len;
391 }
392
393 /*
394 * Cache final, incomplete block
395 */
396 if( 0 != ilen )
397 {
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700398 /* Encryption: only cache partial blocks
399 * Decryption w/ padding: always keep at least one whole block
400 * Decryption w/o padding: only cache partial blocks
401 */
Janos Follath98e28a72016-05-31 14:03:54 +0100402 copy_len = ilen % block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700403 if( copy_len == 0 &&
404 ctx->operation == MBEDTLS_DECRYPT &&
405 NULL != ctx->add_padding)
406 {
Janos Follath98e28a72016-05-31 14:03:54 +0100407 copy_len = block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700408 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
410 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
411 copy_len );
412
413 ctx->unprocessed_len += copy_len;
414 ilen -= copy_len;
415 }
416
417 /*
418 * Process remaining full blocks
419 */
420 if( ilen )
421 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000422 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
423 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200425 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200427
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 *olen += ilen;
429 }
430
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200431 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_CIPHER_MODE_CFB)
436 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000437 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000438 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000439 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000440 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000441 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200442 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000443 }
444
445 *olen = ilen;
446
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200447 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000448 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_CIPHER_MODE_CTR)
452 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000453 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000454 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000455 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000456 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000457 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200458 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000459 }
460
461 *olen = ilen;
462
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200463 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_CIPHER_MODE_STREAM)
468 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200469 {
470 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
471 ilen, input, output ) ) )
472 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200473 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200474 }
475
476 *olen = ilen;
477
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200478 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483}
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
486#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200487/*
488 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
489 */
Paul Bakker23986e52011-04-24 08:57:21 +0000490static void add_pkcs_padding( unsigned char *output, size_t output_len,
491 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100494 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495
496 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000497 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498}
499
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200500static int get_pkcs_padding( unsigned char *input, size_t input_len,
501 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100503 size_t i, pad_idx;
504 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505
Paul Bakkera885d682011-01-20 16:35:05 +0000506 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508
509 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510 *data_len = input_len - padding_len;
511
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100512 /* Avoid logical || since it results in a branch */
513 bad |= padding_len > input_len;
514 bad |= padding_len == 0;
515
516 /* The number of bytes checked must be independent of padding_len,
517 * so pick input_len, which is usually 8 or 16 (one block) */
518 pad_idx = input_len - padding_len;
519 for( i = 0; i < input_len; i++ )
520 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200527/*
528 * One and zeros padding: fill with 80 00 ... 00
529 */
530static void add_one_and_zeros_padding( unsigned char *output,
531 size_t output_len, size_t data_len )
532{
533 size_t padding_len = output_len - data_len;
534 unsigned char i = 0;
535
536 output[data_len] = 0x80;
537 for( i = 1; i < padding_len; i++ )
538 output[data_len + i] = 0x00;
539}
540
541static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
542 size_t *data_len )
543{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100544 size_t i;
545 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200546
547 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200549
Micha Krausba8316f2017-12-23 23:40:08 +0100550 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100551 *data_len = 0;
552 for( i = input_len; i > 0; i-- )
553 {
554 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100555 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100556 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100557 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100558 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200561
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200566/*
567 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
568 */
569static void add_zeros_and_len_padding( unsigned char *output,
570 size_t output_len, size_t data_len )
571{
572 size_t padding_len = output_len - data_len;
573 unsigned char i = 0;
574
575 for( i = 1; i < padding_len; i++ )
576 output[data_len + i - 1] = 0x00;
577 output[output_len - 1] = (unsigned char) padding_len;
578}
579
580static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
581 size_t *data_len )
582{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100583 size_t i, pad_idx;
584 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200585
586 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200588
589 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200590 *data_len = input_len - padding_len;
591
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100592 /* Avoid logical || since it results in a branch */
593 bad |= padding_len > input_len;
594 bad |= padding_len == 0;
595
596 /* The number of bytes checked must be independent of padding_len */
597 pad_idx = input_len - padding_len;
598 for( i = 0; i < input_len - 1; i++ )
599 bad |= input[i] * ( i >= pad_idx );
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200606/*
607 * Zero padding: fill with 00 ... 00
608 */
609static void add_zeros_padding( unsigned char *output,
610 size_t output_len, size_t data_len )
611{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200612 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200613
614 for( i = data_len; i < output_len; i++ )
615 output[i] = 0x00;
616}
617
618static int get_zeros_padding( unsigned char *input, size_t input_len,
619 size_t *data_len )
620{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100621 size_t i;
622 unsigned char done = 0, prev_done;
623
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200624 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200626
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100627 *data_len = 0;
628 for( i = input_len; i > 0; i-- )
629 {
630 prev_done = done;
631 done |= ( input[i-1] != 0 );
632 *data_len |= i * ( done != prev_done );
633 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200634
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200635 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200638
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200639/*
640 * No padding: don't pad :)
641 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200643 * but a trivial get_padding function
644 */
645static int get_no_padding( unsigned char *input, size_t input_len,
646 size_t *data_len )
647{
648 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200650
651 *data_len = input_len;
652
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200653 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200658 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659{
660 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
663 *olen = 0;
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
666 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
667 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
668 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000669 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200670 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200674 {
675 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200677
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200678 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200679 }
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_CIPHER_MODE_CBC)
682 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200684 int ret = 0;
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200688 /* check for 'no padding' mode */
689 if( NULL == ctx->add_padding )
690 {
691 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200693
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200694 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200695 }
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698 ctx->unprocessed_len );
699 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200702 /*
703 * For decrypt operations, expect a full block,
704 * or an empty block if no padding
705 */
706 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200707 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000710 }
711
712 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000713 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000715 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200717 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000718 }
719
720 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( MBEDTLS_DECRYPT == ctx->operation )
722 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200723 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724
725 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200727 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200729#else
730 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734}
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
737int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200738{
739 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200743 }
744
Paul Bakker1a45d912013-08-14 12:04:26 +0200745 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
748 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200749 ctx->add_padding = add_pkcs_padding;
750 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200751 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
754 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200755 ctx->add_padding = add_one_and_zeros_padding;
756 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200757 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
760 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200761 ctx->add_padding = add_zeros_and_len_padding;
762 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200763 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
766 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200767 ctx->add_padding = add_zeros_padding;
768 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200769 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200772 ctx->add_padding = NULL;
773 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200774 break;
775
776 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200778 }
779
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200780 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_GCM_C)
785int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786 unsigned char *tag, size_t tag_len )
787{
788 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 if( MBEDTLS_ENCRYPT != ctx->operation )
792 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
795 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200796
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200797 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200798}
Paul Bakker9af723c2014-05-01 13:03:14 +0200799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200801 const unsigned char *tag, size_t tag_len )
802{
803 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200804
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200805 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200809 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200812 {
813 unsigned char check_tag[16];
814 size_t i;
815 int diff;
816
817 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200821 check_tag, tag_len ) ) )
822 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200823 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200824 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200825
826 /* Check the tag in "constant-time" */
827 for( diff = 0, i = 0; i < tag_len; i++ )
828 diff |= tag[i] ^ check_tag[i];
829
830 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200832
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200833 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200834 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200835
836 return( 0 );
837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200839
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200840/*
841 * Packet-oriented wrapper for non-AEAD modes
842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200844 const unsigned char *iv, size_t iv_len,
845 const unsigned char *input, size_t ilen,
846 unsigned char *output, size_t *olen )
847{
848 int ret;
849 size_t finish_olen;
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200852 return( ret );
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200855 return( ret );
856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200858 return( ret );
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200861 return( ret );
862
863 *olen += finish_olen;
864
865 return( 0 );
866}
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200869/*
870 * Packet-oriented encryption for AEAD modes
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200873 const unsigned char *iv, size_t iv_len,
874 const unsigned char *ad, size_t ad_len,
875 const unsigned char *input, size_t ilen,
876 unsigned char *output, size_t *olen,
877 unsigned char *tag, size_t tag_len )
878{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_GCM_C)
880 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200881 {
882 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200884 iv, iv_len, ad, ad_len, input, output,
885 tag_len, tag ) );
886 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_GCM_C */
888#if defined(MBEDTLS_CCM_C)
889 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200890 {
891 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200893 iv, iv_len, ad, ad_len, input, output,
894 tag, tag_len ) );
895 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200899}
900
901/*
902 * Packet-oriented decryption for AEAD modes
903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200905 const unsigned char *iv, size_t iv_len,
906 const unsigned char *ad, size_t ad_len,
907 const unsigned char *input, size_t ilen,
908 unsigned char *output, size_t *olen,
909 const unsigned char *tag, size_t tag_len )
910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_GCM_C)
912 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200913 {
914 int ret;
915
916 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200918 iv, iv_len, ad, ad_len,
919 tag, tag_len, input, output );
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
922 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200923
924 return( ret );
925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_GCM_C */
927#if defined(MBEDTLS_CCM_C)
928 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200929 {
930 int ret;
931
932 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200934 iv, iv_len, ad, ad_len,
935 input, output, tag, tag_len );
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
938 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200939
940 return( ret );
941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_CIPHER_C */