blob: 0713fee5ecddee769a86caa3f9649d9457ec787e [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
Simon Butcher327398a2016-10-05 14:09:11 +010048#if defined(MBEDTLS_CMAC_C)
49#include "mbedtls/cmac.h"
50#endif
51
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
60#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020061#endif
62
Paul Bakker34617722014-06-13 17:20:13 +020063/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010065 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020066}
67
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073 int *type;
74
75 if( ! supported_init )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 def = mbedtls_cipher_definitions;
78 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079
80 while( def->type != 0 )
81 *type++ = (*def++).type;
82
83 *type = 0;
84
85 supported_init = 1;
86 }
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020096 if( def->type == cipher_type )
97 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000098
Paul Bakkerd8bb8262014-06-17 14:06:49 +020099 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000100}
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200105
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200107 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200110 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200111 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000112
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200113 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116const 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 +0200117 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200123 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200124 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200125 def->info->mode == mode )
126 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200127
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200128 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200137{
138 if( ctx == NULL )
139 return;
140
Simon Butcher327398a2016-10-05 14:09:11 +0100141#if defined(MBEDTLS_CMAC_C)
142 if( ctx->cmac_ctx )
143 {
144 mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
145 mbedtls_free( ctx->cmac_ctx );
146 }
147#endif
148
Paul Bakker84bbeb52014-07-01 14:53:22 +0200149 if( ctx->cipher_ctx )
150 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153}
154
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200155int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156{
157 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
Paul Bakker343a8702011-06-09 14:27:58 +0000162 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
165 ctx->cipher_info = cipher_info;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200168 /*
169 * Ignore possible errors caused by a cipher mode that doesn't use padding
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
172 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200173#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200177
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200182 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183{
184 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200188 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200191 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200192
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200193 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 ctx->operation = operation;
195
Paul Bakker343a8702011-06-09 14:27:58 +0000196 /*
Simon Butcher91e254c2018-04-22 22:58:07 +0100197 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( MBEDTLS_ENCRYPT == operation ||
200 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher91e254c2018-04-22 22:58:07 +0100201 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000203 {
204 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200205 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000206 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000209 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200210 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213}
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200216 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200218 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200219
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200223 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
225 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200228 actual_iv_size = iv_len;
229 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200230 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200231 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200232
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200233 /* avoid reading past the end of input buffer */
234 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200236 }
237
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200238 memcpy( ctx->iv, iv, actual_iv_size );
239 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200240
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200241 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200242}
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200245{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200246 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200248
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249 ctx->unprocessed_len = 0;
250
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200251 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200252}
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_GCM_C)
255int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200256 const unsigned char *ad, size_t ad_len )
257{
258 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200264 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200265 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200266
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200272 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273{
Paul Bakkerff61a782011-06-09 15:42:02 +0000274 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100275 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000276
Paul Bakker68884e32013-01-07 18:20:04 +0100277 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000280 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281
Paul Bakker6c212762013-12-16 15:24:50 +0100282 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100283 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200286 {
Janos Follath98e28a72016-05-31 14:03:54 +0100287 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200289
290 *olen = ilen;
291
292 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
293 ctx->operation, input, output ) ) )
294 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200295 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200296 }
297
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200298 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200299 }
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_GCM_C)
302 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200303 {
304 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200306 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200307 }
308#endif
309
Janos Follath98e28a72016-05-31 14:03:54 +0100310 if ( 0 == block_size )
311 {
312 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
313 }
314
Paul Bakker68884e32013-01-07 18:20:04 +0100315 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100316 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100319 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_CIPHER_MODE_CBC)
322 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200324 size_t copy_len = 0;
325
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326 /*
327 * If there is not enough data for a full block, cache it.
328 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700329 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000330 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700331 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
332 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000334 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 {
336 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
337 ilen );
338
339 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341 }
342
343 /*
344 * Process cached data first
345 */
Janos Follath98e28a72016-05-31 14:03:54 +0100346 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347 {
Janos Follath98e28a72016-05-31 14:03:54 +0100348 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
350 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
351 copy_len );
352
Paul Bakkerff61a782011-06-09 15:42:02 +0000353 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100354 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000355 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200357 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 }
359
Janos Follath98e28a72016-05-31 14:03:54 +0100360 *olen += block_size;
361 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 ctx->unprocessed_len = 0;
363
364 input += copy_len;
365 ilen -= copy_len;
366 }
367
368 /*
369 * Cache final, incomplete block
370 */
371 if( 0 != ilen )
372 {
Janos Follath98e28a72016-05-31 14:03:54 +0100373 if( 0 == block_size )
374 {
375 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
376 }
377
Andy Leiserson79e77892017-04-28 20:01:49 -0700378 /* Encryption: only cache partial blocks
379 * Decryption w/ padding: always keep at least one whole block
380 * Decryption w/o padding: only cache partial blocks
381 */
Janos Follath98e28a72016-05-31 14:03:54 +0100382 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700383 if( copy_len == 0 &&
384 ctx->operation == MBEDTLS_DECRYPT &&
385 NULL != ctx->add_padding)
386 {
Janos Follath98e28a72016-05-31 14:03:54 +0100387 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700388 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389
390 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
391 copy_len );
392
393 ctx->unprocessed_len += copy_len;
394 ilen -= copy_len;
395 }
396
397 /*
398 * Process remaining full blocks
399 */
400 if( ilen )
401 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000402 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
403 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200405 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200407
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408 *olen += ilen;
409 }
410
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_CIPHER_MODE_CFB)
416 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000417 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000418 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000419 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000420 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000421 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000423 }
424
425 *olen = ilen;
426
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000430
Simon Butcher91e254c2018-04-22 22:58:07 +0100431#if defined(MBEDTLS_CIPHER_MODE_OFB)
432 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
433 {
434 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
435 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
436 {
437 return( ret );
438 }
439
440 *olen = ilen;
441
442 return( 0 );
443 }
444#endif /* MBEDTLS_CIPHER_MODE_OFB */
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_CIPHER_MODE_CTR)
447 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000448 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000449 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000450 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000451 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000452 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200453 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000454 }
455
456 *olen = ilen;
457
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200458 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_CIPHER_MODE_STREAM)
463 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200464 {
465 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
466 ilen, input, output ) ) )
467 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200468 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200469 }
470
471 *olen = ilen;
472
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200473 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478}
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
481#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200482/*
483 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
484 */
Paul Bakker23986e52011-04-24 08:57:21 +0000485static void add_pkcs_padding( unsigned char *output, size_t output_len,
486 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487{
Paul Bakker23986e52011-04-24 08:57:21 +0000488 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100489 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490
491 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000492 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493}
494
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200495static int get_pkcs_padding( unsigned char *input, size_t input_len,
496 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100498 size_t i, pad_idx;
499 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500
Paul Bakkera885d682011-01-20 16:35:05 +0000501 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503
504 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505 *data_len = input_len - padding_len;
506
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100507 /* Avoid logical || since it results in a branch */
508 bad |= padding_len > input_len;
509 bad |= padding_len == 0;
510
511 /* The number of bytes checked must be independent of padding_len,
512 * so pick input_len, which is usually 8 or 16 (one block) */
513 pad_idx = input_len - padding_len;
514 for( i = 0; i < input_len; i++ )
515 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200522/*
523 * One and zeros padding: fill with 80 00 ... 00
524 */
525static void add_one_and_zeros_padding( unsigned char *output,
526 size_t output_len, size_t data_len )
527{
528 size_t padding_len = output_len - data_len;
529 unsigned char i = 0;
530
531 output[data_len] = 0x80;
532 for( i = 1; i < padding_len; i++ )
533 output[data_len + i] = 0x00;
534}
535
536static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
537 size_t *data_len )
538{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100539 size_t i;
540 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200541
542 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200544
Micha Krausba8316f2017-12-23 23:40:08 +0100545 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100546 *data_len = 0;
547 for( i = input_len; i > 0; i-- )
548 {
549 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100550 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100551 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100552 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100553 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200556
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200561/*
562 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
563 */
564static void add_zeros_and_len_padding( unsigned char *output,
565 size_t output_len, size_t data_len )
566{
567 size_t padding_len = output_len - data_len;
568 unsigned char i = 0;
569
570 for( i = 1; i < padding_len; i++ )
571 output[data_len + i - 1] = 0x00;
572 output[output_len - 1] = (unsigned char) padding_len;
573}
574
575static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
576 size_t *data_len )
577{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100578 size_t i, pad_idx;
579 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200580
581 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200583
584 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200585 *data_len = input_len - padding_len;
586
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100587 /* Avoid logical || since it results in a branch */
588 bad |= padding_len > input_len;
589 bad |= padding_len == 0;
590
591 /* The number of bytes checked must be independent of padding_len */
592 pad_idx = input_len - padding_len;
593 for( i = 0; i < input_len - 1; i++ )
594 bad |= input[i] * ( i >= pad_idx );
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200601/*
602 * Zero padding: fill with 00 ... 00
603 */
604static void add_zeros_padding( unsigned char *output,
605 size_t output_len, size_t data_len )
606{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200607 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200608
609 for( i = data_len; i < output_len; i++ )
610 output[i] = 0x00;
611}
612
613static int get_zeros_padding( unsigned char *input, size_t input_len,
614 size_t *data_len )
615{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100616 size_t i;
617 unsigned char done = 0, prev_done;
618
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200619 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200621
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100622 *data_len = 0;
623 for( i = input_len; i > 0; i-- )
624 {
625 prev_done = done;
626 done |= ( input[i-1] != 0 );
627 *data_len |= i * ( done != prev_done );
628 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200629
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200630 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200633
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200634/*
635 * No padding: don't pad :)
636 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200638 * but a trivial get_padding function
639 */
640static int get_no_padding( unsigned char *input, size_t input_len,
641 size_t *data_len )
642{
643 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200645
646 *data_len = input_len;
647
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200653 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654{
655 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657
658 *olen = 0;
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher91e254c2018-04-22 22:58:07 +0100661 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
663 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
664 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000665 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000667 }
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200670 {
671 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200673
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200674 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200675 }
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_CIPHER_MODE_CBC)
678 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200680 int ret = 0;
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200684 /* check for 'no padding' mode */
685 if( NULL == ctx->add_padding )
686 {
687 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200689
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200690 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200691 }
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694 ctx->unprocessed_len );
695 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200698 /*
699 * For decrypt operations, expect a full block,
700 * or an empty block if no padding
701 */
702 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200703 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000706 }
707
708 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000709 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000711 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200713 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000714 }
715
716 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( MBEDTLS_DECRYPT == ctx->operation )
718 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200719 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720
721 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200723 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200725#else
726 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730}
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
733int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200734{
735 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200739 }
740
Paul Bakker1a45d912013-08-14 12:04:26 +0200741 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
744 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200745 ctx->add_padding = add_pkcs_padding;
746 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200747 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
750 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200751 ctx->add_padding = add_one_and_zeros_padding;
752 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200753 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
756 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200757 ctx->add_padding = add_zeros_and_len_padding;
758 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200759 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
762 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200763 ctx->add_padding = add_zeros_padding;
764 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200765 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200768 ctx->add_padding = NULL;
769 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200770 break;
771
772 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200774 }
775
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200776 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_GCM_C)
781int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200782 unsigned char *tag, size_t tag_len )
783{
784 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( MBEDTLS_ENCRYPT != ctx->operation )
788 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
791 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200792
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200793 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200794}
Paul Bakker9af723c2014-05-01 13:03:14 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200797 const unsigned char *tag, size_t tag_len )
798{
799 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200800
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200801 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200805 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200808 {
809 unsigned char check_tag[16];
810 size_t i;
811 int diff;
812
813 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200817 check_tag, tag_len ) ) )
818 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200819 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200820 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200821
822 /* Check the tag in "constant-time" */
823 for( diff = 0, i = 0; i < tag_len; i++ )
824 diff |= tag[i] ^ check_tag[i];
825
826 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200828
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200829 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200830 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200831
832 return( 0 );
833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200835
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200836/*
837 * Packet-oriented wrapper for non-AEAD modes
838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200840 const unsigned char *iv, size_t iv_len,
841 const unsigned char *input, size_t ilen,
842 unsigned char *output, size_t *olen )
843{
844 int ret;
845 size_t finish_olen;
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200848 return( ret );
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200851 return( ret );
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200854 return( ret );
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200857 return( ret );
858
859 *olen += finish_olen;
860
861 return( 0 );
862}
863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200865/*
866 * Packet-oriented encryption for AEAD modes
867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200869 const unsigned char *iv, size_t iv_len,
870 const unsigned char *ad, size_t ad_len,
871 const unsigned char *input, size_t ilen,
872 unsigned char *output, size_t *olen,
873 unsigned char *tag, size_t tag_len )
874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_GCM_C)
876 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200877 {
878 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200880 iv, iv_len, ad, ad_len, input, output,
881 tag_len, tag ) );
882 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#endif /* MBEDTLS_GCM_C */
884#if defined(MBEDTLS_CCM_C)
885 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200886 {
887 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200889 iv, iv_len, ad, ad_len, input, output,
890 tag, tag_len ) );
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}
896
897/*
898 * Packet-oriented decryption for AEAD modes
899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200901 const unsigned char *iv, size_t iv_len,
902 const unsigned char *ad, size_t ad_len,
903 const unsigned char *input, size_t ilen,
904 unsigned char *output, size_t *olen,
905 const unsigned char *tag, size_t tag_len )
906{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_GCM_C)
908 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200909 {
910 int ret;
911
912 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200914 iv, iv_len, ad, ad_len,
915 tag, tag_len, input, output );
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
918 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200919
920 return( ret );
921 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_GCM_C */
923#if defined(MBEDTLS_CCM_C)
924 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200925 {
926 int ret;
927
928 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200930 iv, iv_len, ad, ad_len,
931 input, output, tag, tag_len );
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
934 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200935
936 return( ret );
937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_CIPHER_C */