blob: fe34929c28039d5573f62e4a7accdb45a838195c [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;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200195
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200199 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
201 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200204 actual_iv_size = iv_len;
205 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200206 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200207 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200208
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200209 /* avoid reading past the end of input buffer */
210 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200212 }
213
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200214 memcpy( ctx->iv, iv, actual_iv_size );
215 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200216
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200217 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200218}
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200221{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200222 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200224
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225 ctx->unprocessed_len = 0;
226
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200227 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_GCM_C)
231int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200232 const unsigned char *ad, size_t ad_len )
233{
234 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200240 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200241 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200242
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200243 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200248 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249{
Paul Bakkerff61a782011-06-09 15:42:02 +0000250 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Paul Bakker68884e32013-01-07 18:20:04 +0100252 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000255 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Paul Bakker6c212762013-12-16 15:24:50 +0100257 *olen = 0;
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
262 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200263
264 *olen = ilen;
265
266 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
267 ctx->operation, input, output ) ) )
268 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200269 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200270 }
271
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200272 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200273 }
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_GCM_C)
276 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200277 {
278 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200280 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200281 }
282#endif
283
Paul Bakker68884e32013-01-07 18:20:04 +0100284 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100288 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#if defined(MBEDTLS_CIPHER_MODE_CBC)
291 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000292 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200293 size_t copy_len = 0;
294
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295 /*
296 * If there is not enough data for a full block, cache it.
297 */
Andrzej Kurek944adb92018-03-30 04:58:13 -0400298 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000299 ilen <= mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Andrzej Kurek944adb92018-03-30 04:58:13 -0400300 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
301 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garciaef1329e2017-01-17 23:24:02 +0000303 ilen < mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304 {
305 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
306 ilen );
307
308 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200309 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310 }
311
312 /*
313 * Process cached data first
314 */
315 if( ctx->unprocessed_len != 0 )
316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000318
319 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
320 copy_len );
321
Paul Bakkerff61a782011-06-09 15:42:02 +0000322 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000324 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200326 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 }
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 *olen += mbedtls_cipher_get_block_size( ctx );
330 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 ctx->unprocessed_len = 0;
332
333 input += copy_len;
334 ilen -= copy_len;
335 }
336
337 /*
338 * Cache final, incomplete block
339 */
340 if( 0 != ilen )
341 {
Andrzej Kurek944adb92018-03-30 04:58:13 -0400342 /* Encryption: only cache partial blocks
343 * Decryption w/ padding: always keep at least one whole block
344 * Decryption w/o padding: only cache partial blocks
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400347 if( copy_len == 0 &&
348 ctx->operation == MBEDTLS_DECRYPT &&
349 NULL != ctx->add_padding)
350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 copy_len = mbedtls_cipher_get_block_size( ctx );
Andrzej Kurek944adb92018-03-30 04:58:13 -0400352 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353
354 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
355 copy_len );
356
357 ctx->unprocessed_len += copy_len;
358 ilen -= copy_len;
359 }
360
361 /*
362 * Process remaining full blocks
363 */
364 if( ilen )
365 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000366 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
367 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200371
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 *olen += ilen;
373 }
374
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200375 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_CIPHER_MODE_CFB)
380 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000381 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000382 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000383 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000384 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000385 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200386 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000387 }
388
389 *olen = ilen;
390
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200391 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000392 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_CIPHER_MODE_CTR)
396 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000397 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000398 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000399 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000400 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000401 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200402 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000403 }
404
405 *olen = ilen;
406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000408 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_CIPHER_MODE_STREAM)
412 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200413 {
414 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
415 ilen, input, output ) ) )
416 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200418 }
419
420 *olen = ilen;
421
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
430#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200431/*
432 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
433 */
Paul Bakker23986e52011-04-24 08:57:21 +0000434static void add_pkcs_padding( unsigned char *output, size_t output_len,
435 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436{
Paul Bakker23986e52011-04-24 08:57:21 +0000437 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100438 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439
440 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000441 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442}
443
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200444static int get_pkcs_padding( unsigned char *input, size_t input_len,
445 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100447 size_t i, pad_idx;
448 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449
Paul Bakkera885d682011-01-20 16:35:05 +0000450 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452
453 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 *data_len = input_len - padding_len;
455
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100456 /* Avoid logical || since it results in a branch */
457 bad |= padding_len > input_len;
458 bad |= padding_len == 0;
459
460 /* The number of bytes checked must be independent of padding_len,
461 * so pick input_len, which is usually 8 or 16 (one block) */
462 pad_idx = input_len - padding_len;
463 for( i = 0; i < input_len; i++ )
464 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200471/*
472 * One and zeros padding: fill with 80 00 ... 00
473 */
474static void add_one_and_zeros_padding( unsigned char *output,
475 size_t output_len, size_t data_len )
476{
477 size_t padding_len = output_len - data_len;
478 unsigned char i = 0;
479
480 output[data_len] = 0x80;
481 for( i = 1; i < padding_len; i++ )
482 output[data_len + i] = 0x00;
483}
484
485static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
486 size_t *data_len )
487{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100488 size_t i;
489 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200490
491 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200493
Micha Kraus1741db92017-12-23 23:40:08 +0100494 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100495 *data_len = 0;
496 for( i = input_len; i > 0; i-- )
497 {
498 prev_done = done;
Micha Kraus1741db92017-12-23 23:40:08 +0100499 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100500 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Kraus1741db92017-12-23 23:40:08 +0100501 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100502 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200505
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200510/*
511 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
512 */
513static void add_zeros_and_len_padding( unsigned char *output,
514 size_t output_len, size_t data_len )
515{
516 size_t padding_len = output_len - data_len;
517 unsigned char i = 0;
518
519 for( i = 1; i < padding_len; i++ )
520 output[data_len + i - 1] = 0x00;
521 output[output_len - 1] = (unsigned char) padding_len;
522}
523
524static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
525 size_t *data_len )
526{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100527 size_t i, pad_idx;
528 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200529
530 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200532
533 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200534 *data_len = input_len - padding_len;
535
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100536 /* Avoid logical || since it results in a branch */
537 bad |= padding_len > input_len;
538 bad |= padding_len == 0;
539
540 /* The number of bytes checked must be independent of padding_len */
541 pad_idx = input_len - padding_len;
542 for( i = 0; i < input_len - 1; i++ )
543 bad |= input[i] * ( i >= pad_idx );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200550/*
551 * Zero padding: fill with 00 ... 00
552 */
553static void add_zeros_padding( unsigned char *output,
554 size_t output_len, size_t data_len )
555{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200556 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200557
558 for( i = data_len; i < output_len; i++ )
559 output[i] = 0x00;
560}
561
562static int get_zeros_padding( unsigned char *input, size_t input_len,
563 size_t *data_len )
564{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100565 size_t i;
566 unsigned char done = 0, prev_done;
567
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200568 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200570
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100571 *data_len = 0;
572 for( i = input_len; i > 0; i-- )
573 {
574 prev_done = done;
575 done |= ( input[i-1] != 0 );
576 *data_len |= i * ( done != prev_done );
577 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200578
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200582
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200583/*
584 * No padding: don't pad :)
585 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200587 * but a trivial get_padding function
588 */
589static int get_no_padding( unsigned char *input, size_t input_len,
590 size_t *data_len )
591{
592 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200594
595 *data_len = input_len;
596
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200597 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200602 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603{
604 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
607 *olen = 0;
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
610 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
611 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
612 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000613 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200614 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000615 }
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200618 {
619 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200621
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200622 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200623 }
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_CIPHER_MODE_CBC)
626 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200628 int ret = 0;
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200632 /* check for 'no padding' mode */
633 if( NULL == ctx->add_padding )
634 {
635 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200637
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200638 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200639 }
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642 ctx->unprocessed_len );
643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646 /*
647 * For decrypt operations, expect a full block,
648 * or an empty block if no padding
649 */
650 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 }
655
656 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000657 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000659 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 }
663
664 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( MBEDTLS_DECRYPT == ctx->operation )
666 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200667 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668
669 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200673#else
674 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
681int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200682{
683 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200687 }
688
Paul Bakker1a45d912013-08-14 12:04:26 +0200689 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
692 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200693 ctx->add_padding = add_pkcs_padding;
694 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
698 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200699 ctx->add_padding = add_one_and_zeros_padding;
700 ctx->get_padding = get_one_and_zeros_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_ZEROS_AND_LEN)
704 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200705 ctx->add_padding = add_zeros_and_len_padding;
706 ctx->get_padding = get_zeros_and_len_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)
710 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200711 ctx->add_padding = add_zeros_padding;
712 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200716 ctx->add_padding = NULL;
717 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200718 break;
719
720 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200722 }
723
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200724 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_GCM_C)
729int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200730 unsigned char *tag, size_t tag_len )
731{
732 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 if( MBEDTLS_ENCRYPT != ctx->operation )
736 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
739 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200740
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200741 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200742}
Paul Bakker9af723c2014-05-01 13:03:14 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200745 const unsigned char *tag, size_t tag_len )
746{
747 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200748
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200749 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200753 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200756 {
757 unsigned char check_tag[16];
758 size_t i;
759 int diff;
760
761 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200765 check_tag, tag_len ) ) )
766 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200767 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200768 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200769
770 /* Check the tag in "constant-time" */
771 for( diff = 0, i = 0; i < tag_len; i++ )
772 diff |= tag[i] ^ check_tag[i];
773
774 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200778 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779
780 return( 0 );
781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200783
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200784/*
785 * Packet-oriented wrapper for non-AEAD modes
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200788 const unsigned char *iv, size_t iv_len,
789 const unsigned char *input, size_t ilen,
790 unsigned char *output, size_t *olen )
791{
792 int ret;
793 size_t finish_olen;
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200796 return( ret );
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200799 return( ret );
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 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_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200805 return( ret );
806
807 *olen += finish_olen;
808
809 return( 0 );
810}
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200813/*
814 * Packet-oriented encryption for AEAD modes
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200817 const unsigned char *iv, size_t iv_len,
818 const unsigned char *ad, size_t ad_len,
819 const unsigned char *input, size_t ilen,
820 unsigned char *output, size_t *olen,
821 unsigned char *tag, size_t tag_len )
822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_GCM_C)
824 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200825 {
826 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200828 iv, iv_len, ad, ad_len, input, output,
829 tag_len, tag ) );
830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_GCM_C */
832#if defined(MBEDTLS_CCM_C)
833 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200834 {
835 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200837 iv, iv_len, ad, ad_len, input, output,
838 tag, tag_len ) );
839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200843}
844
845/*
846 * Packet-oriented decryption for AEAD modes
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200849 const unsigned char *iv, size_t iv_len,
850 const unsigned char *ad, size_t ad_len,
851 const unsigned char *input, size_t ilen,
852 unsigned char *output, size_t *olen,
853 const unsigned char *tag, size_t tag_len )
854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_GCM_C)
856 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200857 {
858 int ret;
859
860 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200862 iv, iv_len, ad, ad_len,
863 tag, tag_len, input, output );
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
866 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200867
868 return( ret );
869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_GCM_C */
871#if defined(MBEDTLS_CCM_C)
872 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200873 {
874 int ret;
875
876 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200878 iv, iv_len, ad, ad_len,
879 input, output, tag, tag_len );
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
882 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200883
884 return( ret );
885 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#endif /* MBEDTLS_CIPHER_C */