blob: 090eeeb42038db76d45f166f2043ff2d449c56c2 [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 Eldor0bd06a32017-09-25 17:03:12 +0300199 if( NULL == ctx || NULL == ctx->cipher_info ||
200 ( NULL == iv && ( ctx->cipher_info->mode != MBEDTLS_MODE_ECB ) ) )
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
Ron Eldor0bd06a32017-09-25 17:03:12 +0300203 if ( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
204 {
205 ctx->iv_size = 0;
206 return ( 0 );
207 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200208 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
210 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200213 actual_iv_size = iv_len;
214 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200215 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200216 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200217
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200218 /* avoid reading past the end of input buffer */
219 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200221 }
222
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200223 memcpy( ctx->iv, iv, actual_iv_size );
224 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200225
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200226 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200227}
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200230{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200231 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200233
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234 ctx->unprocessed_len = 0;
235
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200236 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200237}
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_GCM_C)
240int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200241 const unsigned char *ad, size_t ad_len )
242{
243 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200249 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200250 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200251
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200252 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200257 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
Paul Bakkerff61a782011-06-09 15:42:02 +0000259 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
Paul Bakker68884e32013-01-07 18:20:04 +0100261 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000264 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Paul Bakker6c212762013-12-16 15:24:50 +0100266 *olen = 0;
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
271 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200272
273 *olen = ilen;
274
275 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
276 ctx->operation, input, output ) ) )
277 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200278 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200279 }
280
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200281 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200282 }
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_GCM_C)
285 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200286 {
287 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200290 }
291#endif
292
Paul Bakker68884e32013-01-07 18:20:04 +0100293 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100297 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_CIPHER_MODE_CBC)
300 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200302 size_t copy_len = 0;
303
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304 /*
305 * If there is not enough data for a full block, cache it.
306 */
Andrzej Kurek944adb92018-03-30 04:58:13 -0400307 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000308 ilen <= mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Andrzej Kurek944adb92018-03-30 04:58:13 -0400309 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
310 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000312 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313 {
314 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
315 ilen );
316
317 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200318 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319 }
320
321 /*
322 * Process cached data first
323 */
324 if( ctx->unprocessed_len != 0 )
325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327
328 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
329 copy_len );
330
Paul Bakkerff61a782011-06-09 15:42:02 +0000331 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000333 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200335 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336 }
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 *olen += mbedtls_cipher_get_block_size( ctx );
339 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000340 ctx->unprocessed_len = 0;
341
342 input += copy_len;
343 ilen -= copy_len;
344 }
345
346 /*
347 * Cache final, incomplete block
348 */
349 if( 0 != ilen )
350 {
Andrzej Kurek944adb92018-03-30 04:58:13 -0400351 /* Encryption: only cache partial blocks
352 * Decryption w/ padding: always keep at least one whole block
353 * Decryption w/o padding: only cache partial blocks
354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400356 if( copy_len == 0 &&
357 ctx->operation == MBEDTLS_DECRYPT &&
358 NULL != ctx->add_padding)
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 copy_len = mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400361 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362
363 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
364 copy_len );
365
366 ctx->unprocessed_len += copy_len;
367 ilen -= copy_len;
368 }
369
370 /*
371 * Process remaining full blocks
372 */
373 if( ilen )
374 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000375 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
376 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200378 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200380
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 *olen += ilen;
382 }
383
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200384 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_CIPHER_MODE_CFB)
389 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000390 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000391 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000392 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000393 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000394 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200395 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000396 }
397
398 *olen = ilen;
399
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200400 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_CIPHER_MODE_CTR)
405 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000406 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000407 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000408 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000409 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000410 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000412 }
413
414 *olen = ilen;
415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_CIPHER_MODE_STREAM)
421 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200422 {
423 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
424 ilen, input, output ) ) )
425 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200427 }
428
429 *olen = ilen;
430
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200431 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436}
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
439#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200440/*
441 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
442 */
Paul Bakker23986e52011-04-24 08:57:21 +0000443static void add_pkcs_padding( unsigned char *output, size_t output_len,
444 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445{
Paul Bakker23986e52011-04-24 08:57:21 +0000446 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100447 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448
449 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000450 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451}
452
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200453static int get_pkcs_padding( unsigned char *input, size_t input_len,
454 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100456 size_t i, pad_idx;
457 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
Paul Bakkera885d682011-01-20 16:35:05 +0000459 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461
462 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463 *data_len = input_len - padding_len;
464
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100465 /* Avoid logical || since it results in a branch */
466 bad |= padding_len > input_len;
467 bad |= padding_len == 0;
468
469 /* The number of bytes checked must be independent of padding_len,
470 * so pick input_len, which is usually 8 or 16 (one block) */
471 pad_idx = input_len - padding_len;
472 for( i = 0; i < input_len; i++ )
473 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200480/*
481 * One and zeros padding: fill with 80 00 ... 00
482 */
483static void add_one_and_zeros_padding( unsigned char *output,
484 size_t output_len, size_t data_len )
485{
486 size_t padding_len = output_len - data_len;
487 unsigned char i = 0;
488
489 output[data_len] = 0x80;
490 for( i = 1; i < padding_len; i++ )
491 output[data_len + i] = 0x00;
492}
493
494static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
495 size_t *data_len )
496{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100497 size_t i;
498 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200499
500 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200502
Micha Kraus1741db92017-12-23 23:40:08 +0100503 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100504 *data_len = 0;
505 for( i = input_len; i > 0; i-- )
506 {
507 prev_done = done;
Micha Kraus1741db92017-12-23 23:40:08 +0100508 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100509 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Kraus1741db92017-12-23 23:40:08 +0100510 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100511 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200514
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200519/*
520 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
521 */
522static void add_zeros_and_len_padding( unsigned char *output,
523 size_t output_len, size_t data_len )
524{
525 size_t padding_len = output_len - data_len;
526 unsigned char i = 0;
527
528 for( i = 1; i < padding_len; i++ )
529 output[data_len + i - 1] = 0x00;
530 output[output_len - 1] = (unsigned char) padding_len;
531}
532
533static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
534 size_t *data_len )
535{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100536 size_t i, pad_idx;
537 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200538
539 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200541
542 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200543 *data_len = input_len - padding_len;
544
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100545 /* Avoid logical || since it results in a branch */
546 bad |= padding_len > input_len;
547 bad |= padding_len == 0;
548
549 /* The number of bytes checked must be independent of padding_len */
550 pad_idx = input_len - padding_len;
551 for( i = 0; i < input_len - 1; i++ )
552 bad |= input[i] * ( i >= pad_idx );
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200559/*
560 * Zero padding: fill with 00 ... 00
561 */
562static void add_zeros_padding( unsigned char *output,
563 size_t output_len, size_t data_len )
564{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200565 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200566
567 for( i = data_len; i < output_len; i++ )
568 output[i] = 0x00;
569}
570
571static int get_zeros_padding( unsigned char *input, size_t input_len,
572 size_t *data_len )
573{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100574 size_t i;
575 unsigned char done = 0, prev_done;
576
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200577 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200579
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100580 *data_len = 0;
581 for( i = input_len; i > 0; i-- )
582 {
583 prev_done = done;
584 done |= ( input[i-1] != 0 );
585 *data_len |= i * ( done != prev_done );
586 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200587
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200588 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200591
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200592/*
593 * No padding: don't pad :)
594 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200596 * but a trivial get_padding function
597 */
598static int get_no_padding( unsigned char *input, size_t input_len,
599 size_t *data_len )
600{
601 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200603
604 *data_len = input_len;
605
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200606 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200611 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612{
613 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615
616 *olen = 0;
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
619 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
620 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
621 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000622 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200623 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000624 }
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200627 {
628 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200630
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200631 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_CIPHER_MODE_CBC)
635 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200637 int ret = 0;
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641 /* check for 'no padding' mode */
642 if( NULL == ctx->add_padding )
643 {
644 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200647 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 ctx->unprocessed_len );
652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200655 /*
656 * For decrypt operations, expect a full block,
657 * or an empty block if no padding
658 */
659 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200660 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 }
664
665 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000666 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000668 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200670 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671 }
672
673 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( MBEDTLS_DECRYPT == ctx->operation )
675 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200676 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677
678 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200682#else
683 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687}
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
690int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200691{
692 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200696 }
697
Paul Bakker1a45d912013-08-14 12:04:26 +0200698 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
701 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200702 ctx->add_padding = add_pkcs_padding;
703 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200704 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
707 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200708 ctx->add_padding = add_one_and_zeros_padding;
709 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200710 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
713 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200714 ctx->add_padding = add_zeros_and_len_padding;
715 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200716 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
719 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200720 ctx->add_padding = add_zeros_padding;
721 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200722 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200725 ctx->add_padding = NULL;
726 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200727 break;
728
729 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200731 }
732
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_GCM_C)
738int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200739 unsigned char *tag, size_t tag_len )
740{
741 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 if( MBEDTLS_ENCRYPT != ctx->operation )
745 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
748 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200749
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200750 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200751}
Paul Bakker9af723c2014-05-01 13:03:14 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200754 const unsigned char *tag, size_t tag_len )
755{
756 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200757
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200758 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200762 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200765 {
766 unsigned char check_tag[16];
767 size_t i;
768 int diff;
769
770 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200774 check_tag, tag_len ) ) )
775 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200777 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200778
779 /* Check the tag in "constant-time" */
780 for( diff = 0, i = 0; i < tag_len; i++ )
781 diff |= tag[i] ^ check_tag[i];
782
783 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200785
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200787 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200788
789 return( 0 );
790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200792
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200793/*
794 * Packet-oriented wrapper for non-AEAD modes
795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200797 const unsigned char *iv, size_t iv_len,
798 const unsigned char *input, size_t ilen,
799 unsigned char *output, size_t *olen )
800{
801 int ret;
802 size_t finish_olen;
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200805 return( ret );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200808 return( ret );
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200811 return( ret );
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200814 return( ret );
815
816 *olen += finish_olen;
817
818 return( 0 );
819}
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200822/*
823 * Packet-oriented encryption for AEAD modes
824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200826 const unsigned char *iv, size_t iv_len,
827 const unsigned char *ad, size_t ad_len,
828 const unsigned char *input, size_t ilen,
829 unsigned char *output, size_t *olen,
830 unsigned char *tag, size_t tag_len )
831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_GCM_C)
833 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200834 {
835 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200837 iv, iv_len, ad, ad_len, input, output,
838 tag_len, tag ) );
839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_GCM_C */
841#if defined(MBEDTLS_CCM_C)
842 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200843 {
844 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200846 iv, iv_len, ad, ad_len, input, output,
847 tag, tag_len ) );
848 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200852}
853
854/*
855 * Packet-oriented decryption for AEAD modes
856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200858 const unsigned char *iv, size_t iv_len,
859 const unsigned char *ad, size_t ad_len,
860 const unsigned char *input, size_t ilen,
861 unsigned char *output, size_t *olen,
862 const unsigned char *tag, size_t tag_len )
863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_GCM_C)
865 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200866 {
867 int ret;
868
869 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200871 iv, iv_len, ad, ad_len,
872 tag, tag_len, input, output );
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
875 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200876
877 return( ret );
878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_GCM_C */
880#if defined(MBEDTLS_CCM_C)
881 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200882 {
883 int ret;
884
885 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200887 iv, iv_len, ad, ad_len,
888 input, output, tag, tag_len );
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
891 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200892
893 return( ret );
894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#endif /* MBEDTLS_CIPHER_C */