blob: a5cd61cdf34797c353887bbff84dfeb27195563f [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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Simon Butcher327398a2016-10-05 14:09:11 +010049#if defined(MBEDTLS_CMAC_C)
50#include "mbedtls/cmac.h"
51#endif
52
53#if defined(MBEDTLS_PLATFORM_C)
54#include "mbedtls/platform.h"
55#else
56#define mbedtls_calloc calloc
57#define mbedtls_free free
58#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
61#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020062#endif
63
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020064static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020069 int *type;
70
71 if( ! supported_init )
72 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 def = mbedtls_cipher_definitions;
74 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020075
76 while( def->type != 0 )
77 *type++ = (*def++).type;
78
79 *type = 0;
80
81 supported_init = 1;
82 }
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020092 if( def->type == cipher_type )
93 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000094
Paul Bakkerd8bb8262014-06-17 14:06:49 +020095 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200103 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200106 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200107 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000108
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200109 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112const 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 +0200113 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200119 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200120 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200121 def->info->mode == mode )
122 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200123
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200133{
134 if( ctx == NULL )
135 return;
136
Simon Butcher327398a2016-10-05 14:09:11 +0100137#if defined(MBEDTLS_CMAC_C)
138 if( ctx->cmac_ctx )
139 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500140 mbedtls_platform_zeroize( ctx->cmac_ctx,
141 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100142 mbedtls_free( ctx->cmac_ctx );
143 }
144#endif
145
Paul Bakker84bbeb52014-07-01 14:53:22 +0200146 if( ctx->cipher_ctx )
147 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500149 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150}
151
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200152int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153{
154 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
Paul Bakker343a8702011-06-09 14:27:58 +0000159 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
162 ctx->cipher_info = cipher_info;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200165 /*
166 * Ignore possible errors caused by a cipher mode that doesn't use padding
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
169 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200174
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200175 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200179 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180{
181 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200185 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200188 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200189
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200190 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191 ctx->operation = operation;
192
Paul Bakker343a8702011-06-09 14:27:58 +0000193 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000194 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( MBEDTLS_ENCRYPT == operation ||
197 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
198 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000199 {
200 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200201 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000202 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000205 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200206 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200212 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200214 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200215
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200219 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
221 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200224 actual_iv_size = iv_len;
225 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200226 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200227 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200228
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200229 /* avoid reading past the end of input buffer */
230 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200232 }
233
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200234 memcpy( ctx->iv, iv, actual_iv_size );
235 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200236
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200237 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200241{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200242 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200244
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245 ctx->unprocessed_len = 0;
246
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200247 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200248}
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_GCM_C)
251int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200252 const unsigned char *ad, size_t ad_len )
253{
254 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200260 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200261 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200262
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200268 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269{
Paul Bakkerff61a782011-06-09 15:42:02 +0000270 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100271 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272
Paul Bakker68884e32013-01-07 18:20:04 +0100273 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000276 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277
Paul Bakker6c212762013-12-16 15:24:50 +0100278 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100279 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200282 {
Janos Follath98e28a72016-05-31 14:03:54 +0100283 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200285
286 *olen = ilen;
287
288 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
289 ctx->operation, input, output ) ) )
290 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200291 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200292 }
293
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200294 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200295 }
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_GCM_C)
298 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200299 {
300 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200302 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200303 }
304#endif
305
Janos Follath98e28a72016-05-31 14:03:54 +0100306 if ( 0 == block_size )
307 {
308 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
309 }
310
Paul Bakker68884e32013-01-07 18:20:04 +0100311 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100312 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100315 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_CIPHER_MODE_CBC)
318 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200320 size_t copy_len = 0;
321
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322 /*
323 * If there is not enough data for a full block, cache it.
324 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700325 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000326 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700327 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
328 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000330 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 {
332 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
333 ilen );
334
335 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200336 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337 }
338
339 /*
340 * Process cached data first
341 */
Janos Follath98e28a72016-05-31 14:03:54 +0100342 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343 {
Janos Follath98e28a72016-05-31 14:03:54 +0100344 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345
346 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
347 copy_len );
348
Paul Bakkerff61a782011-06-09 15:42:02 +0000349 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100350 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000351 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200353 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354 }
355
Janos Follath98e28a72016-05-31 14:03:54 +0100356 *olen += block_size;
357 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 ctx->unprocessed_len = 0;
359
360 input += copy_len;
361 ilen -= copy_len;
362 }
363
364 /*
365 * Cache final, incomplete block
366 */
367 if( 0 != ilen )
368 {
Janos Follath98e28a72016-05-31 14:03:54 +0100369 if( 0 == block_size )
370 {
371 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
372 }
373
Andy Leiserson79e77892017-04-28 20:01:49 -0700374 /* Encryption: only cache partial blocks
375 * Decryption w/ padding: always keep at least one whole block
376 * Decryption w/o padding: only cache partial blocks
377 */
Janos Follath98e28a72016-05-31 14:03:54 +0100378 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700379 if( copy_len == 0 &&
380 ctx->operation == MBEDTLS_DECRYPT &&
381 NULL != ctx->add_padding)
382 {
Janos Follath98e28a72016-05-31 14:03:54 +0100383 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700384 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
386 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
387 copy_len );
388
389 ctx->unprocessed_len += copy_len;
390 ilen -= copy_len;
391 }
392
393 /*
394 * Process remaining full blocks
395 */
396 if( ilen )
397 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000398 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
399 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200401 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200403
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404 *olen += ilen;
405 }
406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_CIPHER_MODE_CFB)
412 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000413 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000414 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000415 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000416 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000417 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200418 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000419 }
420
421 *olen = ilen;
422
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200423 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#if defined(MBEDTLS_CIPHER_MODE_CTR)
428 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000429 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000430 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000431 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000432 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000433 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200434 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000435 }
436
437 *olen = ilen;
438
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200439 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_CIPHER_MODE_STREAM)
444 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200445 {
446 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
447 ilen, input, output ) ) )
448 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200449 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200450 }
451
452 *olen = ilen;
453
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200454 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459}
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
462#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200463/*
464 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
465 */
Paul Bakker23986e52011-04-24 08:57:21 +0000466static void add_pkcs_padding( unsigned char *output, size_t output_len,
467 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000468{
Paul Bakker23986e52011-04-24 08:57:21 +0000469 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100470 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471
472 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000473 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474}
475
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200476static int get_pkcs_padding( unsigned char *input, size_t input_len,
477 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100479 size_t i, pad_idx;
480 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481
Paul Bakkera885d682011-01-20 16:35:05 +0000482 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
485 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486 *data_len = input_len - padding_len;
487
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100488 /* Avoid logical || since it results in a branch */
489 bad |= padding_len > input_len;
490 bad |= padding_len == 0;
491
492 /* The number of bytes checked must be independent of padding_len,
493 * so pick input_len, which is usually 8 or 16 (one block) */
494 pad_idx = input_len - padding_len;
495 for( i = 0; i < input_len; i++ )
496 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503/*
504 * One and zeros padding: fill with 80 00 ... 00
505 */
506static void add_one_and_zeros_padding( unsigned char *output,
507 size_t output_len, size_t data_len )
508{
509 size_t padding_len = output_len - data_len;
510 unsigned char i = 0;
511
512 output[data_len] = 0x80;
513 for( i = 1; i < padding_len; i++ )
514 output[data_len + i] = 0x00;
515}
516
517static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
518 size_t *data_len )
519{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100520 size_t i;
521 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200522
523 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200525
Micha Krausba8316f2017-12-23 23:40:08 +0100526 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100527 *data_len = 0;
528 for( i = input_len; i > 0; i-- )
529 {
530 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100531 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100532 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100533 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100534 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200537
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200542/*
543 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
544 */
545static void add_zeros_and_len_padding( unsigned char *output,
546 size_t output_len, size_t data_len )
547{
548 size_t padding_len = output_len - data_len;
549 unsigned char i = 0;
550
551 for( i = 1; i < padding_len; i++ )
552 output[data_len + i - 1] = 0x00;
553 output[output_len - 1] = (unsigned char) padding_len;
554}
555
556static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
557 size_t *data_len )
558{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100559 size_t i, pad_idx;
560 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200561
562 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200564
565 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200566 *data_len = input_len - padding_len;
567
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100568 /* Avoid logical || since it results in a branch */
569 bad |= padding_len > input_len;
570 bad |= padding_len == 0;
571
572 /* The number of bytes checked must be independent of padding_len */
573 pad_idx = input_len - padding_len;
574 for( i = 0; i < input_len - 1; i++ )
575 bad |= input[i] * ( i >= pad_idx );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200582/*
583 * Zero padding: fill with 00 ... 00
584 */
585static void add_zeros_padding( unsigned char *output,
586 size_t output_len, size_t data_len )
587{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200588 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200589
590 for( i = data_len; i < output_len; i++ )
591 output[i] = 0x00;
592}
593
594static int get_zeros_padding( unsigned char *input, size_t input_len,
595 size_t *data_len )
596{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100597 size_t i;
598 unsigned char done = 0, prev_done;
599
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200600 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200602
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100603 *data_len = 0;
604 for( i = input_len; i > 0; i-- )
605 {
606 prev_done = done;
607 done |= ( input[i-1] != 0 );
608 *data_len |= i * ( done != prev_done );
609 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200610
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200611 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200614
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200615/*
616 * No padding: don't pad :)
617 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200619 * but a trivial get_padding function
620 */
621static int get_no_padding( unsigned char *input, size_t input_len,
622 size_t *data_len )
623{
624 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200626
627 *data_len = input_len;
628
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200629 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200634 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635{
636 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638
639 *olen = 0;
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
642 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
643 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
644 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000645 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000647 }
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200650 {
651 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200653
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200654 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200655 }
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_CIPHER_MODE_CBC)
658 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200660 int ret = 0;
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200664 /* check for 'no padding' mode */
665 if( NULL == ctx->add_padding )
666 {
667 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200669
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200670 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 ctx->unprocessed_len );
675 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200678 /*
679 * For decrypt operations, expect a full block,
680 * or an empty block if no padding
681 */
682 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200683 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686 }
687
688 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000689 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000691 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000692 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200693 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694 }
695
696 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( MBEDTLS_DECRYPT == ctx->operation )
698 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200699 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700
701 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200703 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000704 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200705#else
706 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000710}
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
713int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200714{
715 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200719 }
720
Paul Bakker1a45d912013-08-14 12:04:26 +0200721 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
724 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200725 ctx->add_padding = add_pkcs_padding;
726 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200727 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
730 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200731 ctx->add_padding = add_one_and_zeros_padding;
732 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200733 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
736 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200737 ctx->add_padding = add_zeros_and_len_padding;
738 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200739 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
742 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200743 ctx->add_padding = add_zeros_padding;
744 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200745 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200748 ctx->add_padding = NULL;
749 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200750 break;
751
752 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200754 }
755
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200756 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_GCM_C)
761int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200762 unsigned char *tag, size_t tag_len )
763{
764 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( MBEDTLS_ENCRYPT != ctx->operation )
768 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
771 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200772
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200773 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200774}
Paul Bakker9af723c2014-05-01 13:03:14 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777 const unsigned char *tag, size_t tag_len )
778{
779 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200780
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200781 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200785 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200788 {
789 unsigned char check_tag[16];
790 size_t i;
791 int diff;
792
793 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200797 check_tag, tag_len ) ) )
798 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200799 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200800 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200801
802 /* Check the tag in "constant-time" */
803 for( diff = 0, i = 0; i < tag_len; i++ )
804 diff |= tag[i] ^ check_tag[i];
805
806 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200808
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200809 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200810 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200811
812 return( 0 );
813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200815
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200816/*
817 * Packet-oriented wrapper for non-AEAD modes
818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200820 const unsigned char *iv, size_t iv_len,
821 const unsigned char *input, size_t ilen,
822 unsigned char *output, size_t *olen )
823{
824 int ret;
825 size_t finish_olen;
826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200828 return( ret );
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200831 return( ret );
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200834 return( ret );
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200837 return( ret );
838
839 *olen += finish_olen;
840
841 return( 0 );
842}
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200845/*
846 * Packet-oriented encryption for AEAD modes
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_cipher_auth_encrypt( 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 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 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200860 iv, iv_len, ad, ad_len, input, output,
861 tag_len, tag ) );
862 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_GCM_C */
864#if defined(MBEDTLS_CCM_C)
865 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200866 {
867 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200869 iv, iv_len, ad, ad_len, input, output,
870 tag, tag_len ) );
871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200875}
876
877/*
878 * Packet-oriented decryption for AEAD modes
879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200881 const unsigned char *iv, size_t iv_len,
882 const unsigned char *ad, size_t ad_len,
883 const unsigned char *input, size_t ilen,
884 unsigned char *output, size_t *olen,
885 const unsigned char *tag, size_t tag_len )
886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_GCM_C)
888 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200889 {
890 int ret;
891
892 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200894 iv, iv_len, ad, ad_len,
895 tag, tag_len, input, output );
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
898 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200899
900 return( ret );
901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_GCM_C */
903#if defined(MBEDTLS_CCM_C)
904 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200905 {
906 int ret;
907
908 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200910 iv, iv_len, ad, ad_len,
911 input, output, tag, tag_len );
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
914 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200915
916 return( ret );
917 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_CIPHER_C */