blob: e9e0b223e52a13bffc1150036747a67f8ec3fcfc [file] [log] [blame] [raw]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stdlib.h>
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020046#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000047
Simon Butcher327398a2016-10-05 14:09:11 +010048#if defined(MBEDTLS_CMAC_C)
49#include "mbedtls/cmac.h"
50#endif
51
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
60#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020061#endif
62
Paul Bakker34617722014-06-13 17:20:13 +020063/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010065 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020066}
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 {
144 mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
145 mbedtls_free( ctx->cmac_ctx );
146 }
147#endif
148
Paul Bakker84bbeb52014-07-01 14:53:22 +0200149 if( ctx->cipher_ctx )
150 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153}
154
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200155int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156{
157 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
Paul Bakker343a8702011-06-09 14:27:58 +0000162 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
165 ctx->cipher_info = cipher_info;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200168 /*
169 * Ignore possible errors caused by a cipher mode that doesn't use padding
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
172 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200173#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200177
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200182 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183{
184 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200188 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200191 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200192
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200193 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 ctx->operation = operation;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000195
Paul Bakker343a8702011-06-09 14:27:58 +0000196 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000197 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( MBEDTLS_ENCRYPT == operation ||
200 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
201 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000202 {
203 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200204 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000205 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000208 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200209 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200212}
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200215 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200217 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200218
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200222 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
224 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200227 actual_iv_size = iv_len;
228 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200229 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200230 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200231
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200232 /* avoid reading past the end of input buffer */
233 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200235 }
236
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200237 memcpy( ctx->iv, iv, actual_iv_size );
238 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200239
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200240 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200241}
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200244{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200245 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200247
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248 ctx->unprocessed_len = 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200249
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200250 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200251}
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_GCM_C)
254int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200255 const unsigned char *ad, size_t ad_len )
256{
257 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200263 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200264 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200266 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200271 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272{
Paul Bakkerff61a782011-06-09 15:42:02 +0000273 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100274 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275
Paul Bakker68884e32013-01-07 18:20:04 +0100276 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000279 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000280
Paul Bakker6c212762013-12-16 15:24:50 +0100281 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100282 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200285 {
Janos Follath98e28a72016-05-31 14:03:54 +0100286 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200288
289 *olen = ilen;
290
291 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
292 ctx->operation, input, output ) ) )
293 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200294 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200295 }
296
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200297 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200298 }
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_GCM_C)
301 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200302 {
303 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200305 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200306 }
307#endif
308
Janos Follath98e28a72016-05-31 14:03:54 +0100309 if ( 0 == block_size )
310 {
311 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
312 }
313
Paul Bakker68884e32013-01-07 18:20:04 +0100314 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100315 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100318 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_CIPHER_MODE_CBC)
321 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200323 size_t copy_len = 0;
324
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 /*
326 * If there is not enough data for a full block, cache it.
327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ctx->operation == MBEDTLS_DECRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000329 ilen <= block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000331 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332 {
333 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
334 ilen );
335
336 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200337 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338 }
339
340 /*
341 * Process cached data first
342 */
Janos Follath98e28a72016-05-31 14:03:54 +0100343 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 {
Janos Follath98e28a72016-05-31 14:03:54 +0100345 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346
347 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
348 copy_len );
349
Paul Bakkerff61a782011-06-09 15:42:02 +0000350 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100351 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000352 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200354 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355 }
356
Janos Follath98e28a72016-05-31 14:03:54 +0100357 *olen += block_size;
358 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 ctx->unprocessed_len = 0;
360
361 input += copy_len;
362 ilen -= copy_len;
363 }
364
365 /*
366 * Cache final, incomplete block
367 */
368 if( 0 != ilen )
369 {
Janos Follath98e28a72016-05-31 14:03:54 +0100370 if( 0 == block_size )
371 {
372 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
373 }
374
375 copy_len = ilen % block_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
Janos Follath98e28a72016-05-31 14:03:54 +0100377 copy_len = block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378
379 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
380 copy_len );
381
382 ctx->unprocessed_len += copy_len;
383 ilen -= copy_len;
384 }
385
386 /*
387 * Process remaining full blocks
388 */
389 if( ilen )
390 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000391 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
392 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000393 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200394 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200396
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 *olen += ilen;
398 }
Paul Bakker343a8702011-06-09 14:27:58 +0000399
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200400 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_CIPHER_MODE_CFB)
405 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000406 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000407 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000408 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000409 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000410 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000412 }
413
414 *olen = ilen;
415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_CIPHER_MODE_CTR)
421 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000422 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000423 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000424 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000425 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000426 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000428 }
429
430 *olen = ilen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200432 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_CIPHER_MODE_STREAM)
437 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200438 {
439 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
440 ilen, input, output ) ) )
441 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200442 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200443 }
444
445 *olen = ilen;
446
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200447 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200448 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452}
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
455#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200456/*
457 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
458 */
Paul Bakker23986e52011-04-24 08:57:21 +0000459static void add_pkcs_padding( unsigned char *output, size_t output_len,
460 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461{
Paul Bakker23986e52011-04-24 08:57:21 +0000462 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100463 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464
465 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000466 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467}
468
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200469static int get_pkcs_padding( unsigned char *input, size_t input_len,
470 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100472 size_t i, pad_idx;
473 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474
Paul Bakkera885d682011-01-20 16:35:05 +0000475 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477
478 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479 *data_len = input_len - padding_len;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200480
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100481 /* Avoid logical || since it results in a branch */
482 bad |= padding_len > input_len;
483 bad |= padding_len == 0;
484
485 /* The number of bytes checked must be independent of padding_len,
486 * so pick input_len, which is usually 8 or 16 (one block) */
487 pad_idx = input_len - padding_len;
488 for( i = 0; i < input_len; i++ )
489 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200496/*
497 * One and zeros padding: fill with 80 00 ... 00
498 */
499static void add_one_and_zeros_padding( unsigned char *output,
500 size_t output_len, size_t data_len )
501{
502 size_t padding_len = output_len - data_len;
503 unsigned char i = 0;
504
505 output[data_len] = 0x80;
506 for( i = 1; i < padding_len; i++ )
507 output[data_len + i] = 0x00;
508}
509
510static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
511 size_t *data_len )
512{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100513 size_t i;
514 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200515
516 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200518
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100519 bad = 0xFF;
520 *data_len = 0;
521 for( i = input_len; i > 0; i-- )
522 {
523 prev_done = done;
524 done |= ( input[i-1] != 0 );
525 *data_len |= ( i - 1 ) * ( done != prev_done );
526 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
527 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200530
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200535/*
536 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
537 */
538static void add_zeros_and_len_padding( unsigned char *output,
539 size_t output_len, size_t data_len )
540{
541 size_t padding_len = output_len - data_len;
542 unsigned char i = 0;
543
544 for( i = 1; i < padding_len; i++ )
545 output[data_len + i - 1] = 0x00;
546 output[output_len - 1] = (unsigned char) padding_len;
547}
548
549static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
550 size_t *data_len )
551{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100552 size_t i, pad_idx;
553 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200554
555 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200557
558 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200559 *data_len = input_len - padding_len;
560
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100561 /* Avoid logical || since it results in a branch */
562 bad |= padding_len > input_len;
563 bad |= padding_len == 0;
564
565 /* The number of bytes checked must be independent of padding_len */
566 pad_idx = input_len - padding_len;
567 for( i = 0; i < input_len - 1; i++ )
568 bad |= input[i] * ( i >= pad_idx );
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200575/*
576 * Zero padding: fill with 00 ... 00
577 */
578static void add_zeros_padding( unsigned char *output,
579 size_t output_len, size_t data_len )
580{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200581 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200582
583 for( i = data_len; i < output_len; i++ )
584 output[i] = 0x00;
585}
586
587static int get_zeros_padding( unsigned char *input, size_t input_len,
588 size_t *data_len )
589{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100590 size_t i;
591 unsigned char done = 0, prev_done;
592
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200593 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200595
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100596 *data_len = 0;
597 for( i = input_len; i > 0; i-- )
598 {
599 prev_done = done;
600 done |= ( input[i-1] != 0 );
601 *data_len |= i * ( done != prev_done );
602 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200603
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200604 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200607
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200608/*
609 * No padding: don't pad :)
610 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200612 * but a trivial get_padding function
613 */
614static int get_no_padding( unsigned char *input, size_t input_len,
615 size_t *data_len )
616{
617 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200619
620 *data_len = input_len;
621
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200622 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200627 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628{
629 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631
632 *olen = 0;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
635 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
636 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
637 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000638 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200639 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000640 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200643 {
644 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200646
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200647 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_CIPHER_MODE_CBC)
651 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200653 int ret = 0;
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200657 /* check for 'no padding' mode */
658 if( NULL == ctx->add_padding )
659 {
660 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200662
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200663 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200664 }
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 ctx->unprocessed_len );
668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200671 /*
672 * For decrypt operations, expect a full block,
673 * or an empty block if no padding
674 */
675 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 }
680
681 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000682 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000684 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200686 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687 }
688
689 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( MBEDTLS_DECRYPT == ctx->operation )
691 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200692 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693
694 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200698#else
699 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703}
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
706int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200707{
708 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200712 }
713
Paul Bakker1a45d912013-08-14 12:04:26 +0200714 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
717 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200718 ctx->add_padding = add_pkcs_padding;
719 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200720 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
723 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200724 ctx->add_padding = add_one_and_zeros_padding;
725 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200726 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
729 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200730 ctx->add_padding = add_zeros_and_len_padding;
731 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200732 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
735 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200736 ctx->add_padding = add_zeros_padding;
737 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200738 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200741 ctx->add_padding = NULL;
742 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200743 break;
744
745 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200747 }
748
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200749 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_GCM_C)
754int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200755 unsigned char *tag, size_t tag_len )
756{
757 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 if( MBEDTLS_ENCRYPT != ctx->operation )
761 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
764 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200765
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200766 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200767}
Paul Bakker9af723c2014-05-01 13:03:14 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200770 const unsigned char *tag, size_t tag_len )
771{
772 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200773
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200774 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200778 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200781 {
782 unsigned char check_tag[16];
783 size_t i;
784 int diff;
785
786 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200790 check_tag, tag_len ) ) )
791 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200792 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200793 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200794
795 /* Check the tag in "constant-time" */
796 for( diff = 0, i = 0; i < tag_len; i++ )
797 diff |= tag[i] ^ check_tag[i];
798
799 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200801
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200802 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200803 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200804
805 return( 0 );
806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200808
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200809/*
810 * Packet-oriented wrapper for non-AEAD modes
811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200813 const unsigned char *iv, size_t iv_len,
814 const unsigned char *input, size_t ilen,
815 unsigned char *output, size_t *olen )
816{
817 int ret;
818 size_t finish_olen;
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200821 return( ret );
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200824 return( ret );
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200827 return( ret );
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200830 return( ret );
831
832 *olen += finish_olen;
833
834 return( 0 );
835}
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200838/*
839 * Packet-oriented encryption for AEAD modes
840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200842 const unsigned char *iv, size_t iv_len,
843 const unsigned char *ad, size_t ad_len,
844 const unsigned char *input, size_t ilen,
845 unsigned char *output, size_t *olen,
846 unsigned char *tag, size_t tag_len )
847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_GCM_C)
849 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200850 {
851 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200853 iv, iv_len, ad, ad_len, input, output,
854 tag_len, tag ) );
855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_GCM_C */
857#if defined(MBEDTLS_CCM_C)
858 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200859 {
860 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200862 iv, iv_len, ad, ad_len, input, output,
863 tag, tag_len ) );
864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200868}
869
870/*
871 * Packet-oriented decryption for AEAD modes
872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200874 const unsigned char *iv, size_t iv_len,
875 const unsigned char *ad, size_t ad_len,
876 const unsigned char *input, size_t ilen,
877 unsigned char *output, size_t *olen,
878 const unsigned char *tag, size_t tag_len )
879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_GCM_C)
881 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200882 {
883 int ret;
884
885 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200887 iv, iv_len, ad, ad_len,
888 tag, tag_len, input, output );
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
891 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200892
893 return( ret );
894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_GCM_C */
896#if defined(MBEDTLS_CCM_C)
897 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200898 {
899 int ret;
900
901 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200903 iv, iv_len, ad, ad_len,
904 input, output, tag, tag_len );
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
907 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200908
909 return( ret );
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#endif /* MBEDTLS_CIPHER_C */