blob: 68d0c10ff87b1990024798ec7d8c245191212670 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel King0fe7b5b2016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
57#if defined(MBEDTLS_PLATFORM_C)
58#include "mbedtls/platform.h"
59#else
60#define mbedtls_calloc calloc
61#define mbedtls_free free
62#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
65#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020066#endif
67
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073 int *type;
74
75 if( ! supported_init )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 def = mbedtls_cipher_definitions;
78 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079
80 while( def->type != 0 )
81 *type++ = (*def++).type;
82
83 *type = 0;
84
85 supported_init = 1;
86 }
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020096 if( def->type == cipher_type )
97 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000098
Paul Bakkerd8bb8262014-06-17 14:06:49 +020099 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000100}
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200105
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200107 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200110 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200111 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000112
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200113 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116const 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 +0200117 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200123 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200124 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200125 def->info->mode == mode )
126 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200127
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200128 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200137{
138 if( ctx == NULL )
139 return;
140
Simon Butcher327398a2016-10-05 14:09:11 +0100141#if defined(MBEDTLS_CMAC_C)
142 if( ctx->cmac_ctx )
143 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500144 mbedtls_platform_zeroize( ctx->cmac_ctx,
145 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100146 mbedtls_free( ctx->cmac_ctx );
147 }
148#endif
149
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150 if( ctx->cipher_ctx )
151 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500153 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200154}
155
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200156int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000157{
158 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162
Paul Bakker343a8702011-06-09 14:27:58 +0000163 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165
166 ctx->cipher_info = cipher_info;
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200169 /*
170 * Ignore possible errors caused by a cipher mode that doesn't use padding
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
173 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200174#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200178
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200179 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200183 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184{
185 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200189 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200192 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200193
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200194 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195 ctx->operation = operation;
196
Paul Bakker343a8702011-06-09 14:27:58 +0000197 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000198 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( MBEDTLS_ENCRYPT == operation ||
201 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
202 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000203 {
204 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200205 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000206 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000209 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200210 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213}
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200216 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200218 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200219
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200223 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
225 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200228 actual_iv_size = iv_len;
229 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200230 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200231 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200232
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200233 /* avoid reading past the end of input buffer */
234 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200236 }
237
Daniel King0fe7b5b2016-05-15 19:56:20 -0300238#if defined(MBEDTLS_CHACHA20_C)
239 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
240 {
241 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
242 iv,
243 0U ) ) /* Initial counter value */
244 {
245 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
246 }
247 }
248#endif
249
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200250 memcpy( ctx->iv, iv, actual_iv_size );
251 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200252
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200253 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200254}
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200257{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200258 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200260
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261 ctx->unprocessed_len = 0;
262
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200264}
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_GCM_C)
267int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200268 const unsigned char *ad, size_t ad_len )
269{
270 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200276 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200277 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200278
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200284 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285{
Paul Bakkerff61a782011-06-09 15:42:02 +0000286 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100287 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288
Paul Bakker68884e32013-01-07 18:20:04 +0100289 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000292 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
Paul Bakker6c212762013-12-16 15:24:50 +0100294 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100295 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200298 {
Janos Follath98e28a72016-05-31 14:03:54 +0100299 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200301
302 *olen = ilen;
303
304 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
305 ctx->operation, input, output ) ) )
306 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200307 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200308 }
309
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200310 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200311 }
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#if defined(MBEDTLS_GCM_C)
314 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200315 {
316 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200318 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200319 }
320#endif
321
Janos Follath98e28a72016-05-31 14:03:54 +0100322 if ( 0 == block_size )
323 {
324 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
325 }
326
Paul Bakker68884e32013-01-07 18:20:04 +0100327 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100328 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100331 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
Daniel King0fe7b5b2016-05-15 19:56:20 -0300333
334#if defined(MBEDTLS_CHACHA20_C)
335 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
336 {
337 *olen = ilen;
338 return mbedtls_chacha20_update( (mbedtls_chacha20_context*) ctx->cipher_ctx,
339 ilen, input, output );
340 }
341#endif
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_CIPHER_MODE_CBC)
344 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200346 size_t copy_len = 0;
347
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 /*
349 * If there is not enough data for a full block, cache it.
350 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700351 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000352 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700353 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
354 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000356 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357 {
358 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
359 ilen );
360
361 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200362 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 }
364
365 /*
366 * Process cached data first
367 */
Janos Follath98e28a72016-05-31 14:03:54 +0100368 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369 {
Janos Follath98e28a72016-05-31 14:03:54 +0100370 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371
372 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
373 copy_len );
374
Paul Bakkerff61a782011-06-09 15:42:02 +0000375 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100376 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000377 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200379 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 }
381
Janos Follath98e28a72016-05-31 14:03:54 +0100382 *olen += block_size;
383 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384 ctx->unprocessed_len = 0;
385
386 input += copy_len;
387 ilen -= copy_len;
388 }
389
390 /*
391 * Cache final, incomplete block
392 */
393 if( 0 != ilen )
394 {
Janos Follath98e28a72016-05-31 14:03:54 +0100395 if( 0 == block_size )
396 {
397 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
398 }
399
Andy Leiserson79e77892017-04-28 20:01:49 -0700400 /* Encryption: only cache partial blocks
401 * Decryption w/ padding: always keep at least one whole block
402 * Decryption w/o padding: only cache partial blocks
403 */
Janos Follath98e28a72016-05-31 14:03:54 +0100404 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700405 if( copy_len == 0 &&
406 ctx->operation == MBEDTLS_DECRYPT &&
407 NULL != ctx->add_padding)
408 {
Janos Follath98e28a72016-05-31 14:03:54 +0100409 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700410 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
412 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
413 copy_len );
414
415 ctx->unprocessed_len += copy_len;
416 ilen -= copy_len;
417 }
418
419 /*
420 * Process remaining full blocks
421 */
422 if( ilen )
423 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000424 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
425 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200429
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 *olen += ilen;
431 }
432
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200433 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#if defined(MBEDTLS_CIPHER_MODE_CFB)
438 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000439 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000440 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000441 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000443 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200444 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000445 }
446
447 *olen = ilen;
448
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200449 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_CIPHER_MODE_CTR)
454 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000455 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000456 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000457 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000458 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000459 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200460 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000461 }
462
463 *olen = ilen;
464
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_CIPHER_MODE_STREAM)
470 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200471 {
472 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
473 ilen, input, output ) ) )
474 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200476 }
477
478 *olen = ilen;
479
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200480 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485}
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
488#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200489/*
490 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
491 */
Paul Bakker23986e52011-04-24 08:57:21 +0000492static void add_pkcs_padding( unsigned char *output, size_t output_len,
493 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494{
Paul Bakker23986e52011-04-24 08:57:21 +0000495 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100496 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
498 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000499 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500}
501
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200502static int get_pkcs_padding( unsigned char *input, size_t input_len,
503 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100505 size_t i, pad_idx;
506 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507
Paul Bakkera885d682011-01-20 16:35:05 +0000508 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
511 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512 *data_len = input_len - padding_len;
513
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100514 /* Avoid logical || since it results in a branch */
515 bad |= padding_len > input_len;
516 bad |= padding_len == 0;
517
518 /* The number of bytes checked must be independent of padding_len,
519 * so pick input_len, which is usually 8 or 16 (one block) */
520 pad_idx = input_len - padding_len;
521 for( i = 0; i < input_len; i++ )
522 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200529/*
530 * One and zeros padding: fill with 80 00 ... 00
531 */
532static void add_one_and_zeros_padding( unsigned char *output,
533 size_t output_len, size_t data_len )
534{
535 size_t padding_len = output_len - data_len;
536 unsigned char i = 0;
537
538 output[data_len] = 0x80;
539 for( i = 1; i < padding_len; i++ )
540 output[data_len + i] = 0x00;
541}
542
543static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
544 size_t *data_len )
545{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100546 size_t i;
547 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200548
549 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200551
Micha Krausba8316f2017-12-23 23:40:08 +0100552 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100553 *data_len = 0;
554 for( i = input_len; i > 0; i-- )
555 {
556 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100557 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100558 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100559 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100560 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200563
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200568/*
569 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
570 */
571static void add_zeros_and_len_padding( unsigned char *output,
572 size_t output_len, size_t data_len )
573{
574 size_t padding_len = output_len - data_len;
575 unsigned char i = 0;
576
577 for( i = 1; i < padding_len; i++ )
578 output[data_len + i - 1] = 0x00;
579 output[output_len - 1] = (unsigned char) padding_len;
580}
581
582static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
583 size_t *data_len )
584{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100585 size_t i, pad_idx;
586 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200587
588 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200590
591 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200592 *data_len = input_len - padding_len;
593
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100594 /* Avoid logical || since it results in a branch */
595 bad |= padding_len > input_len;
596 bad |= padding_len == 0;
597
598 /* The number of bytes checked must be independent of padding_len */
599 pad_idx = input_len - padding_len;
600 for( i = 0; i < input_len - 1; i++ )
601 bad |= input[i] * ( i >= pad_idx );
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200608/*
609 * Zero padding: fill with 00 ... 00
610 */
611static void add_zeros_padding( unsigned char *output,
612 size_t output_len, size_t data_len )
613{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200614 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200615
616 for( i = data_len; i < output_len; i++ )
617 output[i] = 0x00;
618}
619
620static int get_zeros_padding( unsigned char *input, size_t input_len,
621 size_t *data_len )
622{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100623 size_t i;
624 unsigned char done = 0, prev_done;
625
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200626 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200628
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100629 *data_len = 0;
630 for( i = input_len; i > 0; i-- )
631 {
632 prev_done = done;
633 done |= ( input[i-1] != 0 );
634 *data_len |= i * ( done != prev_done );
635 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200636
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200637 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200640
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641/*
642 * No padding: don't pad :)
643 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200645 * but a trivial get_padding function
646 */
647static int get_no_padding( unsigned char *input, size_t input_len,
648 size_t *data_len )
649{
650 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652
653 *data_len = input_len;
654
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200660 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661{
662 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664
665 *olen = 0;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
668 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
669 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
670 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000671 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000673 }
674
Daniel King0fe7b5b2016-05-15 19:56:20 -0300675 if ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type )
676 {
677 return( 0 );
678 }
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200681 {
682 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200684
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200686 }
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_CIPHER_MODE_CBC)
689 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000690 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200691 int ret = 0;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200695 /* check for 'no padding' mode */
696 if( NULL == ctx->add_padding )
697 {
698 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200700
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200701 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200702 }
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705 ctx->unprocessed_len );
706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200709 /*
710 * For decrypt operations, expect a full block,
711 * or an empty block if no padding
712 */
713 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200714 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717 }
718
719 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000720 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000722 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200724 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000725 }
726
727 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( MBEDTLS_DECRYPT == ctx->operation )
729 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200730 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731
732 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200736#else
737 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741}
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
744int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200745{
746 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200750 }
751
Paul Bakker1a45d912013-08-14 12:04:26 +0200752 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
755 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200756 ctx->add_padding = add_pkcs_padding;
757 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200758 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
761 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200762 ctx->add_padding = add_one_and_zeros_padding;
763 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200764 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
767 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200768 ctx->add_padding = add_zeros_and_len_padding;
769 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200770 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
773 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200774 ctx->add_padding = add_zeros_padding;
775 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200776 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200779 ctx->add_padding = NULL;
780 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200781 break;
782
783 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200785 }
786
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200787 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_GCM_C)
792int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200793 unsigned char *tag, size_t tag_len )
794{
795 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( MBEDTLS_ENCRYPT != ctx->operation )
799 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
802 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200803
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200804 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200805}
Paul Bakker9af723c2014-05-01 13:03:14 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200808 const unsigned char *tag, size_t tag_len )
809{
810 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200811
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200812 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200816 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200819 {
820 unsigned char check_tag[16];
821 size_t i;
822 int diff;
823
824 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200828 check_tag, tag_len ) ) )
829 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200830 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200831 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200832
833 /* Check the tag in "constant-time" */
834 for( diff = 0, i = 0; i < tag_len; i++ )
835 diff |= tag[i] ^ check_tag[i];
836
837 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200839
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200840 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200841 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200842
843 return( 0 );
844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200846
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200847/*
848 * Packet-oriented wrapper for non-AEAD modes
849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200851 const unsigned char *iv, size_t iv_len,
852 const unsigned char *input, size_t ilen,
853 unsigned char *output, size_t *olen )
854{
855 int ret;
856 size_t finish_olen;
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200859 return( ret );
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200862 return( ret );
863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200865 return( ret );
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200868 return( ret );
869
870 *olen += finish_olen;
871
872 return( 0 );
873}
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200876/*
877 * Packet-oriented encryption for AEAD modes
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200880 const unsigned char *iv, size_t iv_len,
881 const unsigned char *ad, size_t ad_len,
882 const unsigned char *input, size_t ilen,
883 unsigned char *output, size_t *olen,
884 unsigned char *tag, size_t tag_len )
885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_GCM_C)
887 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200888 {
889 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200891 iv, iv_len, ad, ad_len, input, output,
892 tag_len, tag ) );
893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#endif /* MBEDTLS_GCM_C */
895#if defined(MBEDTLS_CCM_C)
896 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200897 {
898 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200900 iv, iv_len, ad, ad_len, input, output,
901 tag, tag_len ) );
902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200906}
907
908/*
909 * Packet-oriented decryption for AEAD modes
910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200912 const unsigned char *iv, size_t iv_len,
913 const unsigned char *ad, size_t ad_len,
914 const unsigned char *input, size_t ilen,
915 unsigned char *output, size_t *olen,
916 const unsigned char *tag, size_t tag_len )
917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#if defined(MBEDTLS_GCM_C)
919 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200920 {
921 int ret;
922
923 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200925 iv, iv_len, ad, ad_len,
926 tag, tag_len, input, output );
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
929 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200930
931 return( ret );
932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#endif /* MBEDTLS_GCM_C */
934#if defined(MBEDTLS_CCM_C)
935 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200936 {
937 int ret;
938
939 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200941 iv, iv_len, ad, ad_len,
942 input, output, tag, tag_len );
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
945 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200946
947 return( ret );
948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_CIPHER_C */