blob: cf82a82582ab93d5f68a0a9763a6a64df786f81f [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;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200199
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200203 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
205 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200208 actual_iv_size = iv_len;
209 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200210 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200211 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200212
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200213 /* avoid reading past the end of input buffer */
214 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200216 }
217
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200218 memcpy( ctx->iv, iv, actual_iv_size );
219 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200220
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200221 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200225{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200226 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200228
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 ctx->unprocessed_len = 0;
230
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200231 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_GCM_C)
235int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200236 const unsigned char *ad, size_t ad_len )
237{
238 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200244 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200245 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200246
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200247 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200252 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253{
Paul Bakkerff61a782011-06-09 15:42:02 +0000254 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255
Paul Bakker68884e32013-01-07 18:20:04 +0100256 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000259 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
Paul Bakker6c212762013-12-16 15:24:50 +0100261 *olen = 0;
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
266 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200267
268 *olen = ilen;
269
270 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
271 ctx->operation, input, output ) ) )
272 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200273 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200274 }
275
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200276 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_GCM_C)
280 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200281 {
282 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200284 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200285 }
286#endif
287
Paul Bakker68884e32013-01-07 18:20:04 +0100288 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100292 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_CIPHER_MODE_CBC)
295 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200297 size_t copy_len = 0;
298
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299 /*
300 * If there is not enough data for a full block, cache it.
301 */
Andrzej Kurek944adb92018-03-30 04:58:13 -0400302 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000303 ilen <= mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Andrzej Kurek944adb92018-03-30 04:58:13 -0400304 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
305 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000307 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308 {
309 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
310 ilen );
311
312 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200313 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314 }
315
316 /*
317 * Process cached data first
318 */
319 if( ctx->unprocessed_len != 0 )
320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322
323 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
324 copy_len );
325
Paul Bakkerff61a782011-06-09 15:42:02 +0000326 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000328 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200330 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 }
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 *olen += mbedtls_cipher_get_block_size( ctx );
334 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 ctx->unprocessed_len = 0;
336
337 input += copy_len;
338 ilen -= copy_len;
339 }
340
341 /*
342 * Cache final, incomplete block
343 */
344 if( 0 != ilen )
345 {
Andrzej Kurek944adb92018-03-30 04:58:13 -0400346 /* Encryption: only cache partial blocks
347 * Decryption w/ padding: always keep at least one whole block
348 * Decryption w/o padding: only cache partial blocks
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400351 if( copy_len == 0 &&
352 ctx->operation == MBEDTLS_DECRYPT &&
353 NULL != ctx->add_padding)
354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 copy_len = mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400356 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357
358 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
359 copy_len );
360
361 ctx->unprocessed_len += copy_len;
362 ilen -= copy_len;
363 }
364
365 /*
366 * Process remaining full blocks
367 */
368 if( ilen )
369 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000370 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
371 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200373 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200375
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 *olen += ilen;
377 }
378
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200379 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_CIPHER_MODE_CFB)
384 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000385 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000386 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000387 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000388 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000389 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200390 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000391 }
392
393 *olen = ilen;
394
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200395 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000396 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_CIPHER_MODE_CTR)
400 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000401 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000402 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000403 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000404 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000405 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000407 }
408
409 *olen = ilen;
410
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_CIPHER_MODE_STREAM)
416 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200417 {
418 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
419 ilen, input, output ) ) )
420 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200421 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200422 }
423
424 *olen = ilen;
425
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431}
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
434#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200435/*
436 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
437 */
Paul Bakker23986e52011-04-24 08:57:21 +0000438static void add_pkcs_padding( unsigned char *output, size_t output_len,
439 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440{
Paul Bakker23986e52011-04-24 08:57:21 +0000441 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100442 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443
444 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000445 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446}
447
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200448static int get_pkcs_padding( unsigned char *input, size_t input_len,
449 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100451 size_t i, pad_idx;
452 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453
Paul Bakkera885d682011-01-20 16:35:05 +0000454 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456
457 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458 *data_len = input_len - padding_len;
459
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100460 /* Avoid logical || since it results in a branch */
461 bad |= padding_len > input_len;
462 bad |= padding_len == 0;
463
464 /* The number of bytes checked must be independent of padding_len,
465 * so pick input_len, which is usually 8 or 16 (one block) */
466 pad_idx = input_len - padding_len;
467 for( i = 0; i < input_len; i++ )
468 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200475/*
476 * One and zeros padding: fill with 80 00 ... 00
477 */
478static void add_one_and_zeros_padding( unsigned char *output,
479 size_t output_len, size_t data_len )
480{
481 size_t padding_len = output_len - data_len;
482 unsigned char i = 0;
483
484 output[data_len] = 0x80;
485 for( i = 1; i < padding_len; i++ )
486 output[data_len + i] = 0x00;
487}
488
489static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
490 size_t *data_len )
491{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100492 size_t i;
493 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200494
495 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200497
Micha Kraus1741db92017-12-23 23:40:08 +0100498 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100499 *data_len = 0;
500 for( i = input_len; i > 0; i-- )
501 {
502 prev_done = done;
Micha Kraus1741db92017-12-23 23:40:08 +0100503 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100504 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Kraus1741db92017-12-23 23:40:08 +0100505 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100506 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200509
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200514/*
515 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
516 */
517static void add_zeros_and_len_padding( unsigned char *output,
518 size_t output_len, size_t data_len )
519{
520 size_t padding_len = output_len - data_len;
521 unsigned char i = 0;
522
523 for( i = 1; i < padding_len; i++ )
524 output[data_len + i - 1] = 0x00;
525 output[output_len - 1] = (unsigned char) padding_len;
526}
527
528static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
529 size_t *data_len )
530{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100531 size_t i, pad_idx;
532 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200533
534 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200536
537 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200538 *data_len = input_len - padding_len;
539
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100540 /* Avoid logical || since it results in a branch */
541 bad |= padding_len > input_len;
542 bad |= padding_len == 0;
543
544 /* The number of bytes checked must be independent of padding_len */
545 pad_idx = input_len - padding_len;
546 for( i = 0; i < input_len - 1; i++ )
547 bad |= input[i] * ( i >= pad_idx );
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200554/*
555 * Zero padding: fill with 00 ... 00
556 */
557static void add_zeros_padding( unsigned char *output,
558 size_t output_len, size_t data_len )
559{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200560 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200561
562 for( i = data_len; i < output_len; i++ )
563 output[i] = 0x00;
564}
565
566static int get_zeros_padding( unsigned char *input, size_t input_len,
567 size_t *data_len )
568{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100569 size_t i;
570 unsigned char done = 0, prev_done;
571
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200572 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200574
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100575 *data_len = 0;
576 for( i = input_len; i > 0; i-- )
577 {
578 prev_done = done;
579 done |= ( input[i-1] != 0 );
580 *data_len |= i * ( done != prev_done );
581 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200582
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200583 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200586
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200587/*
588 * No padding: don't pad :)
589 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200591 * but a trivial get_padding function
592 */
593static int get_no_padding( unsigned char *input, size_t input_len,
594 size_t *data_len )
595{
596 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200598
599 *data_len = input_len;
600
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200601 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200606 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607{
608 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
611 *olen = 0;
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
614 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
615 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
616 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000617 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000619 }
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622 {
623 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200626 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_CIPHER_MODE_CBC)
630 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200632 int ret = 0;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200636 /* check for 'no padding' mode */
637 if( NULL == ctx->add_padding )
638 {
639 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200642 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200643 }
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000646 ctx->unprocessed_len );
647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200650 /*
651 * For decrypt operations, expect a full block,
652 * or an empty block if no padding
653 */
654 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658 }
659
660 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000661 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000663 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200665 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666 }
667
668 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 if( MBEDTLS_DECRYPT == ctx->operation )
670 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200671 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
673 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200675 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200677#else
678 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682}
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
685int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200686{
687 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200691 }
692
Paul Bakker1a45d912013-08-14 12:04:26 +0200693 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
696 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200697 ctx->add_padding = add_pkcs_padding;
698 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200699 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
702 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200703 ctx->add_padding = add_one_and_zeros_padding;
704 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200705 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
708 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200709 ctx->add_padding = add_zeros_and_len_padding;
710 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200711 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
714 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200715 ctx->add_padding = add_zeros_padding;
716 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200717 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200720 ctx->add_padding = NULL;
721 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200722 break;
723
724 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200726 }
727
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200728 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_GCM_C)
733int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200734 unsigned char *tag, size_t tag_len )
735{
736 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( MBEDTLS_ENCRYPT != ctx->operation )
740 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_MODE_GCM == ctx->cipher_info->mode )
743 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200744
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200745 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200746}
Paul Bakker9af723c2014-05-01 13:03:14 +0200747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200749 const unsigned char *tag, size_t tag_len )
750{
751 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200752
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200753 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200757 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200760 {
761 unsigned char check_tag[16];
762 size_t i;
763 int diff;
764
765 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200769 check_tag, tag_len ) ) )
770 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200771 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200772 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200773
774 /* Check the tag in "constant-time" */
775 for( diff = 0, i = 0; i < tag_len; i++ )
776 diff |= tag[i] ^ check_tag[i];
777
778 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200780
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200781 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200782 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200783
784 return( 0 );
785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200787
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200788/*
789 * Packet-oriented wrapper for non-AEAD modes
790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200792 const unsigned char *iv, size_t iv_len,
793 const unsigned char *input, size_t ilen,
794 unsigned char *output, size_t *olen )
795{
796 int ret;
797 size_t finish_olen;
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200800 return( ret );
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 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_update( ctx, input, ilen, output, olen ) ) != 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_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200809 return( ret );
810
811 *olen += finish_olen;
812
813 return( 0 );
814}
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200817/*
818 * Packet-oriented encryption for AEAD modes
819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200821 const unsigned char *iv, size_t iv_len,
822 const unsigned char *ad, size_t ad_len,
823 const unsigned char *input, size_t ilen,
824 unsigned char *output, size_t *olen,
825 unsigned char *tag, size_t tag_len )
826{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_GCM_C)
828 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200829 {
830 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200832 iv, iv_len, ad, ad_len, input, output,
833 tag_len, tag ) );
834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_GCM_C */
836#if defined(MBEDTLS_CCM_C)
837 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200838 {
839 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200841 iv, iv_len, ad, ad_len, input, output,
842 tag, tag_len ) );
843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200847}
848
849/*
850 * Packet-oriented decryption for AEAD modes
851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200853 const unsigned char *iv, size_t iv_len,
854 const unsigned char *ad, size_t ad_len,
855 const unsigned char *input, size_t ilen,
856 unsigned char *output, size_t *olen,
857 const unsigned char *tag, size_t tag_len )
858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_GCM_C)
860 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200861 {
862 int ret;
863
864 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200866 iv, iv_len, ad, ad_len,
867 tag, tag_len, input, output );
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
870 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200871
872 return( ret );
873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#endif /* MBEDTLS_GCM_C */
875#if defined(MBEDTLS_CCM_C)
876 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200877 {
878 int ret;
879
880 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200882 iv, iv_len, ad, ad_len,
883 input, output, tag, tag_len );
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
886 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200887
888 return( ret );
889 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_C */