blob: 3e89d2bfb37f412f0c3431a8bb9457c106b42a8b [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"
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
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
49#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020050#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020054 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020057static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020062 int *type;
63
64 if( ! supported_init )
65 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 def = mbedtls_cipher_definitions;
67 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068
69 while( def->type != 0 )
70 *type++ = (*def++).type;
71
72 *type = 0;
73
74 supported_init = 1;
75 }
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000078}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 if( def->type == cipher_type )
86 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000087
Paul Bakkerd8bb8262014-06-17 14:06:49 +020088 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020094
Paul Bakker8123e9d2011-01-06 15:37:30 +000095 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020096 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +020099 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200100 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000101
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103}
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105const 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 +0200106 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200112 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200113 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114 def->info->mode == mode )
115 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200116
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200117 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200126{
127 if( ctx == NULL )
128 return;
129
130 if( ctx->cipher_ctx )
131 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200134}
135
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200136int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137{
138 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142
Paul Bakker343a8702011-06-09 14:27:58 +0000143 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145
146 ctx->cipher_info = cipher_info;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200149 /*
150 * Ignore possible errors caused by a cipher mode that doesn't use padding
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
153 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200154#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200158
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200159 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200163 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164{
165 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200169 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200172 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200173
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200174 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 ctx->operation = operation;
176
Paul Bakker343a8702011-06-09 14:27:58 +0000177 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000178 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( MBEDTLS_ENCRYPT == operation ||
181 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
182 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000183 {
184 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200185 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000186 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000189 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200190 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200196 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200198 size_t actual_iv_size;
Ron Eldor80d7b7c2017-09-25 18:22:32 +0300199 if( NULL == ctx || NULL == ctx->cipher_info )
200 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
201 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200204 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
206 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200209 actual_iv_size = iv_len;
210 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200211 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200212 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200213
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200214 /* avoid reading past the end of input buffer */
215 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200217 }
Ron Eldor80d7b7c2017-09-25 18:22:32 +0300218 if ( actual_iv_size )
219 {
220 memcpy( ctx->iv, iv, actual_iv_size );
221 ctx->iv_size = actual_iv_size;
222 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200223
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200224 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200225}
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200228{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200229 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200231
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232 ctx->unprocessed_len = 0;
233
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200234 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200235}
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_GCM_C)
238int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200239 const unsigned char *ad, size_t ad_len )
240{
241 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200247 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200248 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200249
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200250 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200255 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256{
Paul Bakkerff61a782011-06-09 15:42:02 +0000257 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258
Paul Bakker68884e32013-01-07 18:20:04 +0100259 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Paul Bakker6c212762013-12-16 15:24:50 +0100264 *olen = 0;
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
269 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200270
271 *olen = ilen;
272
273 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
274 ctx->operation, input, output ) ) )
275 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200276 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 }
278
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200280 }
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_GCM_C)
283 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200284 {
285 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200287 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200288 }
289#endif
290
Paul Bakker68884e32013-01-07 18:20:04 +0100291 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100295 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_CIPHER_MODE_CBC)
298 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200300 size_t copy_len = 0;
301
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 /*
303 * If there is not enough data for a full block, cache it.
304 */
Andrzej Kurek944adb92018-03-30 04:58:13 -0400305 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000306 ilen <= mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Andrzej Kurek944adb92018-03-30 04:58:13 -0400307 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
308 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000310 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 {
312 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
313 ilen );
314
315 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200316 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317 }
318
319 /*
320 * Process cached data first
321 */
322 if( ctx->unprocessed_len != 0 )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325
326 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
327 copy_len );
328
Paul Bakkerff61a782011-06-09 15:42:02 +0000329 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000331 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200333 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334 }
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 *olen += mbedtls_cipher_get_block_size( ctx );
337 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338 ctx->unprocessed_len = 0;
339
340 input += copy_len;
341 ilen -= copy_len;
342 }
343
344 /*
345 * Cache final, incomplete block
346 */
347 if( 0 != ilen )
348 {
Andrzej Kurek944adb92018-03-30 04:58:13 -0400349 /* Encryption: only cache partial blocks
350 * Decryption w/ padding: always keep at least one whole block
351 * Decryption w/o padding: only cache partial blocks
352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400354 if( copy_len == 0 &&
355 ctx->operation == MBEDTLS_DECRYPT &&
356 NULL != ctx->add_padding)
357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 copy_len = mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400359 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360
361 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
362 copy_len );
363
364 ctx->unprocessed_len += copy_len;
365 ilen -= copy_len;
366 }
367
368 /*
369 * Process remaining full blocks
370 */
371 if( ilen )
372 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000373 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
374 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200376 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200378
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379 *olen += ilen;
380 }
381
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200382 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_CIPHER_MODE_CFB)
387 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000388 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000389 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000390 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000391 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000392 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200393 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000394 }
395
396 *olen = ilen;
397
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200398 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_CIPHER_MODE_CTR)
403 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000404 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000405 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000406 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000407 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000408 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200409 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000410 }
411
412 *olen = ilen;
413
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200414 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_CIPHER_MODE_STREAM)
419 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200420 {
421 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
422 ilen, input, output ) ) )
423 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200424 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200425 }
426
427 *olen = ilen;
428
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200429 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434}
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
437#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200438/*
439 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
440 */
Paul Bakker23986e52011-04-24 08:57:21 +0000441static void add_pkcs_padding( unsigned char *output, size_t output_len,
442 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443{
Paul Bakker23986e52011-04-24 08:57:21 +0000444 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100445 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446
447 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000448 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449}
450
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200451static int get_pkcs_padding( unsigned char *input, size_t input_len,
452 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100454 size_t i, pad_idx;
455 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456
Paul Bakkera885d682011-01-20 16:35:05 +0000457 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
460 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461 *data_len = input_len - padding_len;
462
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100463 /* Avoid logical || since it results in a branch */
464 bad |= padding_len > input_len;
465 bad |= padding_len == 0;
466
467 /* The number of bytes checked must be independent of padding_len,
468 * so pick input_len, which is usually 8 or 16 (one block) */
469 pad_idx = input_len - padding_len;
470 for( i = 0; i < input_len; i++ )
471 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200478/*
479 * One and zeros padding: fill with 80 00 ... 00
480 */
481static void add_one_and_zeros_padding( unsigned char *output,
482 size_t output_len, size_t data_len )
483{
484 size_t padding_len = output_len - data_len;
485 unsigned char i = 0;
486
487 output[data_len] = 0x80;
488 for( i = 1; i < padding_len; i++ )
489 output[data_len + i] = 0x00;
490}
491
492static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
493 size_t *data_len )
494{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100495 size_t i;
496 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200497
498 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500
Micha Kraus1741db92017-12-23 23:40:08 +0100501 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100502 *data_len = 0;
503 for( i = input_len; i > 0; i-- )
504 {
505 prev_done = done;
Micha Kraus1741db92017-12-23 23:40:08 +0100506 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100507 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Kraus1741db92017-12-23 23:40:08 +0100508 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100509 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200512
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200517/*
518 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
519 */
520static void add_zeros_and_len_padding( unsigned char *output,
521 size_t output_len, size_t data_len )
522{
523 size_t padding_len = output_len - data_len;
524 unsigned char i = 0;
525
526 for( i = 1; i < padding_len; i++ )
527 output[data_len + i - 1] = 0x00;
528 output[output_len - 1] = (unsigned char) padding_len;
529}
530
531static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
532 size_t *data_len )
533{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100534 size_t i, pad_idx;
535 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200536
537 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200539
540 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200541 *data_len = input_len - padding_len;
542
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100543 /* Avoid logical || since it results in a branch */
544 bad |= padding_len > input_len;
545 bad |= padding_len == 0;
546
547 /* The number of bytes checked must be independent of padding_len */
548 pad_idx = input_len - padding_len;
549 for( i = 0; i < input_len - 1; i++ )
550 bad |= input[i] * ( i >= pad_idx );
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200557/*
558 * Zero padding: fill with 00 ... 00
559 */
560static void add_zeros_padding( unsigned char *output,
561 size_t output_len, size_t data_len )
562{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200564
565 for( i = data_len; i < output_len; i++ )
566 output[i] = 0x00;
567}
568
569static int get_zeros_padding( unsigned char *input, size_t input_len,
570 size_t *data_len )
571{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100572 size_t i;
573 unsigned char done = 0, prev_done;
574
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200575 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200577
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100578 *data_len = 0;
579 for( i = input_len; i > 0; i-- )
580 {
581 prev_done = done;
582 done |= ( input[i-1] != 0 );
583 *data_len |= i * ( done != prev_done );
584 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200585
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200586 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200589
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200590/*
591 * No padding: don't pad :)
592 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200594 * but a trivial get_padding function
595 */
596static int get_no_padding( unsigned char *input, size_t input_len,
597 size_t *data_len )
598{
599 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200601
602 *data_len = input_len;
603
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200604 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200609 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610{
611 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
614 *olen = 0;
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
617 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
618 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
619 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000620 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200621 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000622 }
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625 {
626 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200628
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200629 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200630 }
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_CIPHER_MODE_CBC)
633 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200635 int ret = 0;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200639 /* check for 'no padding' mode */
640 if( NULL == ctx->add_padding )
641 {
642 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200644
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 ctx->unprocessed_len );
650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200653 /*
654 * For decrypt operations, expect a full block,
655 * or an empty block if no padding
656 */
657 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200658 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 }
662
663 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000664 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000666 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200668 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 }
670
671 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 if( MBEDTLS_DECRYPT == ctx->operation )
673 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200674 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
676 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200678 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200680#else
681 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685}
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
688int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200689{
690 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200694 }
695
Paul Bakker1a45d912013-08-14 12:04:26 +0200696 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
699 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200700 ctx->add_padding = add_pkcs_padding;
701 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200702 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
705 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200706 ctx->add_padding = add_one_and_zeros_padding;
707 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200708 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
711 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200712 ctx->add_padding = add_zeros_and_len_padding;
713 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200714 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
717 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200718 ctx->add_padding = add_zeros_padding;
719 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200723 ctx->add_padding = NULL;
724 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200725 break;
726
727 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200729 }
730
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200731 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_GCM_C)
736int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200737 unsigned char *tag, size_t tag_len )
738{
739 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( MBEDTLS_ENCRYPT != ctx->operation )
743 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
746 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200747
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200748 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200749}
Paul Bakker9af723c2014-05-01 13:03:14 +0200750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200752 const unsigned char *tag, size_t tag_len )
753{
754 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200755
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200756 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200760 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763 {
764 unsigned char check_tag[16];
765 size_t i;
766 int diff;
767
768 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200772 check_tag, tag_len ) ) )
773 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200774 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200775 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776
777 /* Check the tag in "constant-time" */
778 for( diff = 0, i = 0; i < tag_len; i++ )
779 diff |= tag[i] ^ check_tag[i];
780
781 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200783
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200784 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200785 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786
787 return( 0 );
788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200790
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200791/*
792 * Packet-oriented wrapper for non-AEAD modes
793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200795 const unsigned char *iv, size_t iv_len,
796 const unsigned char *input, size_t ilen,
797 unsigned char *output, size_t *olen )
798{
799 int ret;
800 size_t finish_olen;
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200803 return( ret );
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200806 return( ret );
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200809 return( ret );
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200812 return( ret );
813
814 *olen += finish_olen;
815
816 return( 0 );
817}
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200820/*
821 * Packet-oriented encryption for AEAD modes
822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200824 const unsigned char *iv, size_t iv_len,
825 const unsigned char *ad, size_t ad_len,
826 const unsigned char *input, size_t ilen,
827 unsigned char *output, size_t *olen,
828 unsigned char *tag, size_t tag_len )
829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_GCM_C)
831 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200832 {
833 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200835 iv, iv_len, ad, ad_len, input, output,
836 tag_len, tag ) );
837 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_GCM_C */
839#if defined(MBEDTLS_CCM_C)
840 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200841 {
842 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200844 iv, iv_len, ad, ad_len, input, output,
845 tag, tag_len ) );
846 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200850}
851
852/*
853 * Packet-oriented decryption for AEAD modes
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200856 const unsigned char *iv, size_t iv_len,
857 const unsigned char *ad, size_t ad_len,
858 const unsigned char *input, size_t ilen,
859 unsigned char *output, size_t *olen,
860 const unsigned char *tag, size_t tag_len )
861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_GCM_C)
863 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200864 {
865 int ret;
866
867 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200869 iv, iv_len, ad, ad_len,
870 tag, tag_len, input, output );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
873 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200874
875 return( ret );
876 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_GCM_C */
878#if defined(MBEDTLS_CCM_C)
879 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200880 {
881 int ret;
882
883 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200885 iv, iv_len, ad, ad_len,
886 input, output, tag, tag_len );
887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
889 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200890
891 return( ret );
892 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_CIPHER_C */