blob: 5784f065f1c9e174f2f6cb0a6a09de06e5b78b95 [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
Paul Bakker34617722014-06-13 17:20:13 +020048/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020050 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020053static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020058 int *type;
59
60 if( ! supported_init )
61 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 def = mbedtls_cipher_definitions;
63 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020064
65 while( def->type != 0 )
66 *type++ = (*def++).type;
67
68 *type = 0;
69
70 supported_init = 1;
71 }
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000074}
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020081 if( def->type == cipher_type )
82 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000083
Paul Bakkerd8bb8262014-06-17 14:06:49 +020084 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020090
Paul Bakker8123e9d2011-01-06 15:37:30 +000091 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020092 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +020095 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020096 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +000097
Paul Bakkerd8bb8262014-06-17 14:06:49 +020098 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101const 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 +0200102 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200108 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200109 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200110 def->info->mode == mode )
111 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200112
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200113 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200122{
123 if( ctx == NULL )
124 return;
125
126 if( ctx->cipher_ctx )
127 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200130}
131
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200132int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133{
134 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138
Paul Bakker343a8702011-06-09 14:27:58 +0000139 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141
142 ctx->cipher_info = cipher_info;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200145 /*
146 * Ignore possible errors caused by a cipher mode that doesn't use padding
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
149 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200150#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200154
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200155 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156}
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200159 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160{
161 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200165 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200168 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200169
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200170 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171 ctx->operation = operation;
172
Paul Bakker343a8702011-06-09 14:27:58 +0000173 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000174 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( MBEDTLS_ENCRYPT == operation ||
177 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
178 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000179 {
180 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200181 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000182 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000185 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200186 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189}
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200192 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200194 size_t actual_iv_size;
Ron Eldor80d7b7c2017-09-25 18:22:32 +0300195 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Ron Eldor80d7b7c2017-09-25 18:22:32 +0300197 else if( NULL == iv && iv_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200198 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
Ron Eldor8a1a43b2017-10-01 17:04:54 +0300200 if( NULL == iv && iv_len == 0 )
201 ctx->iv_size = 0;
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 }
Ron Eldor8a1a43b2017-10-01 17:04:54 +0300217 if ( actual_iv_size != 0 )
Ron Eldor80d7b7c2017-09-25 18:22:32 +0300218 {
219 memcpy( ctx->iv, iv, actual_iv_size );
220 ctx->iv_size = actual_iv_size;
221 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200222
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200223 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200224}
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200227{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200228 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200230
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231 ctx->unprocessed_len = 0;
232
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200233 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200234}
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_GCM_C)
237int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200238 const unsigned char *ad, size_t ad_len )
239{
240 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200246 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200247 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200248
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200249 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200254 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255{
Paul Bakkerff61a782011-06-09 15:42:02 +0000256 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Paul Bakker68884e32013-01-07 18:20:04 +0100258 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000261 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262
Paul Bakker6c212762013-12-16 15:24:50 +0100263 *olen = 0;
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
268 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200269
270 *olen = ilen;
271
272 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
273 ctx->operation, input, output ) ) )
274 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200275 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200276 }
277
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200278 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#if defined(MBEDTLS_GCM_C)
282 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200283 {
284 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200287 }
288#endif
289
Paul Bakker68884e32013-01-07 18:20:04 +0100290 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100294 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_CIPHER_MODE_CBC)
297 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200299 size_t copy_len = 0;
300
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301 /*
302 * If there is not enough data for a full block, cache it.
303 */
Andrzej Kurek944adb92018-03-30 04:58:13 -0400304 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000305 ilen <= mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Andrzej Kurek944adb92018-03-30 04:58:13 -0400306 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
307 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000309 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310 {
311 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
312 ilen );
313
314 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200315 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316 }
317
318 /*
319 * Process cached data first
320 */
321 if( ctx->unprocessed_len != 0 )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324
325 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
326 copy_len );
327
Paul Bakkerff61a782011-06-09 15:42:02 +0000328 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000330 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200332 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333 }
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 *olen += mbedtls_cipher_get_block_size( ctx );
336 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337 ctx->unprocessed_len = 0;
338
339 input += copy_len;
340 ilen -= copy_len;
341 }
342
343 /*
344 * Cache final, incomplete block
345 */
346 if( 0 != ilen )
347 {
Andrzej Kurek944adb92018-03-30 04:58:13 -0400348 /* Encryption: only cache partial blocks
349 * Decryption w/ padding: always keep at least one whole block
350 * Decryption w/o padding: only cache partial blocks
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400353 if( copy_len == 0 &&
354 ctx->operation == MBEDTLS_DECRYPT &&
355 NULL != ctx->add_padding)
356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 copy_len = mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400358 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359
360 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
361 copy_len );
362
363 ctx->unprocessed_len += copy_len;
364 ilen -= copy_len;
365 }
366
367 /*
368 * Process remaining full blocks
369 */
370 if( ilen )
371 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000372 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
373 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200375 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200377
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 *olen += ilen;
379 }
380
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200381 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_CIPHER_MODE_CFB)
386 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000387 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000388 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000389 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000390 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000391 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200392 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000393 }
394
395 *olen = ilen;
396
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200397 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_CIPHER_MODE_CTR)
402 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000403 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000404 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000405 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000406 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000407 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200408 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000409 }
410
411 *olen = ilen;
412
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200413 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000414 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_CIPHER_MODE_STREAM)
418 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200419 {
420 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
421 ilen, input, output ) ) )
422 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200423 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200424 }
425
426 *olen = ilen;
427
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200428 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433}
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
436#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200437/*
438 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
439 */
Paul Bakker23986e52011-04-24 08:57:21 +0000440static void add_pkcs_padding( unsigned char *output, size_t output_len,
441 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442{
Paul Bakker23986e52011-04-24 08:57:21 +0000443 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100444 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445
446 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000447 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448}
449
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200450static int get_pkcs_padding( unsigned char *input, size_t input_len,
451 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100453 size_t i, pad_idx;
454 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455
Paul Bakkera885d682011-01-20 16:35:05 +0000456 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
459 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000460 *data_len = input_len - padding_len;
461
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100462 /* Avoid logical || since it results in a branch */
463 bad |= padding_len > input_len;
464 bad |= padding_len == 0;
465
466 /* The number of bytes checked must be independent of padding_len,
467 * so pick input_len, which is usually 8 or 16 (one block) */
468 pad_idx = input_len - padding_len;
469 for( i = 0; i < input_len; i++ )
470 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200477/*
478 * One and zeros padding: fill with 80 00 ... 00
479 */
480static void add_one_and_zeros_padding( unsigned char *output,
481 size_t output_len, size_t data_len )
482{
483 size_t padding_len = output_len - data_len;
484 unsigned char i = 0;
485
486 output[data_len] = 0x80;
487 for( i = 1; i < padding_len; i++ )
488 output[data_len + i] = 0x00;
489}
490
491static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
492 size_t *data_len )
493{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100494 size_t i;
495 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200496
497 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200499
Micha Kraus1741db92017-12-23 23:40:08 +0100500 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100501 *data_len = 0;
502 for( i = input_len; i > 0; i-- )
503 {
504 prev_done = done;
Micha Kraus1741db92017-12-23 23:40:08 +0100505 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100506 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Kraus1741db92017-12-23 23:40:08 +0100507 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100508 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200511
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200516/*
517 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
518 */
519static void add_zeros_and_len_padding( unsigned char *output,
520 size_t output_len, size_t data_len )
521{
522 size_t padding_len = output_len - data_len;
523 unsigned char i = 0;
524
525 for( i = 1; i < padding_len; i++ )
526 output[data_len + i - 1] = 0x00;
527 output[output_len - 1] = (unsigned char) padding_len;
528}
529
530static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
531 size_t *data_len )
532{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100533 size_t i, pad_idx;
534 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200535
536 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200538
539 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200540 *data_len = input_len - padding_len;
541
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100542 /* Avoid logical || since it results in a branch */
543 bad |= padding_len > input_len;
544 bad |= padding_len == 0;
545
546 /* The number of bytes checked must be independent of padding_len */
547 pad_idx = input_len - padding_len;
548 for( i = 0; i < input_len - 1; i++ )
549 bad |= input[i] * ( i >= pad_idx );
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200556/*
557 * Zero padding: fill with 00 ... 00
558 */
559static void add_zeros_padding( unsigned char *output,
560 size_t output_len, size_t data_len )
561{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200562 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200563
564 for( i = data_len; i < output_len; i++ )
565 output[i] = 0x00;
566}
567
568static int get_zeros_padding( unsigned char *input, size_t input_len,
569 size_t *data_len )
570{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100571 size_t i;
572 unsigned char done = 0, prev_done;
573
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200574 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200576
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100577 *data_len = 0;
578 for( i = input_len; i > 0; i-- )
579 {
580 prev_done = done;
581 done |= ( input[i-1] != 0 );
582 *data_len |= i * ( done != prev_done );
583 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200584
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200588
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200589/*
590 * No padding: don't pad :)
591 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200593 * but a trivial get_padding function
594 */
595static int get_no_padding( unsigned char *input, size_t input_len,
596 size_t *data_len )
597{
598 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200600
601 *data_len = input_len;
602
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200603 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200608 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609{
610 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612
613 *olen = 0;
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
616 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
617 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
618 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000619 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200620 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000621 }
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200624 {
625 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200627
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200628 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_CIPHER_MODE_CBC)
632 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200634 int ret = 0;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200638 /* check for 'no padding' mode */
639 if( NULL == ctx->add_padding )
640 {
641 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200643
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200644 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200645 }
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648 ctx->unprocessed_len );
649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652 /*
653 * For decrypt operations, expect a full block,
654 * or an empty block if no padding
655 */
656 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200657 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 }
661
662 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000663 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000665 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 }
669
670 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( MBEDTLS_DECRYPT == ctx->operation )
672 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200673 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674
675 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200677 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200679#else
680 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684}
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
687int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200688{
689 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200693 }
694
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
698 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200699 ctx->add_padding = add_pkcs_padding;
700 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
704 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200705 ctx->add_padding = add_one_and_zeros_padding;
706 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200707 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
710 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200711 ctx->add_padding = add_zeros_and_len_padding;
712 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200713 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
716 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200717 ctx->add_padding = add_zeros_padding;
718 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200719 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200722 ctx->add_padding = NULL;
723 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200724 break;
725
726 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200728 }
729
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200730 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_GCM_C)
735int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200736 unsigned char *tag, size_t tag_len )
737{
738 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( MBEDTLS_ENCRYPT != ctx->operation )
742 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_MODE_GCM == ctx->cipher_info->mode )
745 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200746
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200747 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200748}
Paul Bakker9af723c2014-05-01 13:03:14 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200751 const unsigned char *tag, size_t tag_len )
752{
753 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200754
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200755 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200759 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200762 {
763 unsigned char check_tag[16];
764 size_t i;
765 int diff;
766
767 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200771 check_tag, tag_len ) ) )
772 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200773 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200774 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200775
776 /* Check the tag in "constant-time" */
777 for( diff = 0, i = 0; i < tag_len; i++ )
778 diff |= tag[i] ^ check_tag[i];
779
780 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200782
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200783 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200784 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200785
786 return( 0 );
787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200789
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200790/*
791 * Packet-oriented wrapper for non-AEAD modes
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200794 const unsigned char *iv, size_t iv_len,
795 const unsigned char *input, size_t ilen,
796 unsigned char *output, size_t *olen )
797{
798 int ret;
799 size_t finish_olen;
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200802 return( ret );
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 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_update( ctx, input, ilen, output, olen ) ) != 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_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200811 return( ret );
812
813 *olen += finish_olen;
814
815 return( 0 );
816}
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200819/*
820 * Packet-oriented encryption for AEAD modes
821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200823 const unsigned char *iv, size_t iv_len,
824 const unsigned char *ad, size_t ad_len,
825 const unsigned char *input, size_t ilen,
826 unsigned char *output, size_t *olen,
827 unsigned char *tag, size_t tag_len )
828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_GCM_C)
830 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200831 {
832 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200834 iv, iv_len, ad, ad_len, input, output,
835 tag_len, tag ) );
836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_GCM_C */
838#if defined(MBEDTLS_CCM_C)
839 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200840 {
841 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200843 iv, iv_len, ad, ad_len, input, output,
844 tag, tag_len ) );
845 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200849}
850
851/*
852 * Packet-oriented decryption for AEAD modes
853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200855 const unsigned char *iv, size_t iv_len,
856 const unsigned char *ad, size_t ad_len,
857 const unsigned char *input, size_t ilen,
858 unsigned char *output, size_t *olen,
859 const unsigned char *tag, size_t tag_len )
860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_GCM_C)
862 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200863 {
864 int ret;
865
866 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200868 iv, iv_len, ad, ad_len,
869 tag, tag_len, input, output );
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
872 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200873
874 return( ret );
875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_GCM_C */
877#if defined(MBEDTLS_CCM_C)
878 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200879 {
880 int ret;
881
882 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200884 iv, iv_len, ad, ad_len,
885 input, output, tag, tag_len );
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
888 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200889
890 return( ret );
891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#endif /* MBEDTLS_CIPHER_C */