blob: 4f12b72eea2fa8fd3ecfb0835ad60f38063af466 [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
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100443#if defined(MBEDTLS_CIPHER_MODE_XTS)
444 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
445 {
446 if ( ctx->unprocessed_len ) {
447 /* We can only process an entire sector at a time. */
448 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
449 }
450
451 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
452 ctx->operation, ilen, ctx->iv, input, output );
453 if( ret != 0 )
454 {
455 return( ret );
456 }
457
458 *olen = ilen;
459
460 return( 0 );
461 }
462#endif /* MBEDTLS_CIPHER_MODE_XTS */
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_CIPHER_MODE_STREAM)
465 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200466 {
467 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
468 ilen, input, output ) ) )
469 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200470 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200471 }
472
473 *olen = ilen;
474
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480}
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
483#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200484/*
485 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
486 */
Paul Bakker23986e52011-04-24 08:57:21 +0000487static void add_pkcs_padding( unsigned char *output, size_t output_len,
488 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489{
Paul Bakker23986e52011-04-24 08:57:21 +0000490 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100491 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492
493 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000494 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495}
496
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200497static int get_pkcs_padding( unsigned char *input, size_t input_len,
498 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100500 size_t i, pad_idx;
501 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Paul Bakkera885d682011-01-20 16:35:05 +0000503 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505
506 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507 *data_len = input_len - padding_len;
508
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100509 /* Avoid logical || since it results in a branch */
510 bad |= padding_len > input_len;
511 bad |= padding_len == 0;
512
513 /* The number of bytes checked must be independent of padding_len,
514 * so pick input_len, which is usually 8 or 16 (one block) */
515 pad_idx = input_len - padding_len;
516 for( i = 0; i < input_len; i++ )
517 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200524/*
525 * One and zeros padding: fill with 80 00 ... 00
526 */
527static void add_one_and_zeros_padding( unsigned char *output,
528 size_t output_len, size_t data_len )
529{
530 size_t padding_len = output_len - data_len;
531 unsigned char i = 0;
532
533 output[data_len] = 0x80;
534 for( i = 1; i < padding_len; i++ )
535 output[data_len + i] = 0x00;
536}
537
538static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
539 size_t *data_len )
540{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100541 size_t i;
542 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200543
544 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200546
Micha Krausba8316f2017-12-23 23:40:08 +0100547 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100548 *data_len = 0;
549 for( i = input_len; i > 0; i-- )
550 {
551 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100552 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100553 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100554 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100555 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200558
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200563/*
564 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
565 */
566static void add_zeros_and_len_padding( unsigned char *output,
567 size_t output_len, size_t data_len )
568{
569 size_t padding_len = output_len - data_len;
570 unsigned char i = 0;
571
572 for( i = 1; i < padding_len; i++ )
573 output[data_len + i - 1] = 0x00;
574 output[output_len - 1] = (unsigned char) padding_len;
575}
576
577static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
578 size_t *data_len )
579{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100580 size_t i, pad_idx;
581 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200582
583 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200585
586 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200587 *data_len = input_len - padding_len;
588
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100589 /* Avoid logical || since it results in a branch */
590 bad |= padding_len > input_len;
591 bad |= padding_len == 0;
592
593 /* The number of bytes checked must be independent of padding_len */
594 pad_idx = input_len - padding_len;
595 for( i = 0; i < input_len - 1; i++ )
596 bad |= input[i] * ( i >= pad_idx );
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200603/*
604 * Zero padding: fill with 00 ... 00
605 */
606static void add_zeros_padding( unsigned char *output,
607 size_t output_len, size_t data_len )
608{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200609 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200610
611 for( i = data_len; i < output_len; i++ )
612 output[i] = 0x00;
613}
614
615static int get_zeros_padding( unsigned char *input, size_t input_len,
616 size_t *data_len )
617{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100618 size_t i;
619 unsigned char done = 0, prev_done;
620
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200621 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200623
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100624 *data_len = 0;
625 for( i = input_len; i > 0; i-- )
626 {
627 prev_done = done;
628 done |= ( input[i-1] != 0 );
629 *data_len |= i * ( done != prev_done );
630 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200631
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200635
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200636/*
637 * No padding: don't pad :)
638 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200640 * but a trivial get_padding function
641 */
642static int get_no_padding( unsigned char *input, size_t input_len,
643 size_t *data_len )
644{
645 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200647
648 *data_len = input_len;
649
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200655 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656{
657 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
660 *olen = 0;
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
663 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
664 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100665 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000667 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200668 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200672 {
673 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200675
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200677 }
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_CIPHER_MODE_CBC)
680 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200682 int ret = 0;
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200686 /* check for 'no padding' mode */
687 if( NULL == ctx->add_padding )
688 {
689 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200691
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200692 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200693 }
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696 ctx->unprocessed_len );
697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000699 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200700 /*
701 * For decrypt operations, expect a full block,
702 * or an empty block if no padding
703 */
704 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200705 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708 }
709
710 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000711 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000713 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000714 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200715 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716 }
717
718 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( MBEDTLS_DECRYPT == ctx->operation )
720 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200721 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722
723 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200725 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200727#else
728 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000732}
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
735int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200736{
737 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200741 }
742
Paul Bakker1a45d912013-08-14 12:04:26 +0200743 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
746 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200747 ctx->add_padding = add_pkcs_padding;
748 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200749 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
752 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200753 ctx->add_padding = add_one_and_zeros_padding;
754 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200755 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
758 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200759 ctx->add_padding = add_zeros_and_len_padding;
760 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200761 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
764 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200765 ctx->add_padding = add_zeros_padding;
766 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200767 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200770 ctx->add_padding = NULL;
771 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200772 break;
773
774 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200776 }
777
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200778 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_GCM_C)
783int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200784 unsigned char *tag, size_t tag_len )
785{
786 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 if( MBEDTLS_ENCRYPT != ctx->operation )
790 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
793 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200794
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200795 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200796}
Paul Bakker9af723c2014-05-01 13:03:14 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200799 const unsigned char *tag, size_t tag_len )
800{
801 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200802
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200803 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200807 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200810 {
811 unsigned char check_tag[16];
812 size_t i;
813 int diff;
814
815 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200819 check_tag, tag_len ) ) )
820 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200821 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200822 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200823
824 /* Check the tag in "constant-time" */
825 for( diff = 0, i = 0; i < tag_len; i++ )
826 diff |= tag[i] ^ check_tag[i];
827
828 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200830
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200831 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200832 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200833
834 return( 0 );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200837
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200838/*
839 * Packet-oriented wrapper for non-AEAD modes
840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200842 const unsigned char *iv, size_t iv_len,
843 const unsigned char *input, size_t ilen,
844 unsigned char *output, size_t *olen )
845{
846 int ret;
847 size_t finish_olen;
848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200850 return( ret );
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200853 return( ret );
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200856 return( ret );
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200859 return( ret );
860
861 *olen += finish_olen;
862
863 return( 0 );
864}
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200867/*
868 * Packet-oriented encryption for AEAD modes
869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200871 const unsigned char *iv, size_t iv_len,
872 const unsigned char *ad, size_t ad_len,
873 const unsigned char *input, size_t ilen,
874 unsigned char *output, size_t *olen,
875 unsigned char *tag, size_t tag_len )
876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_GCM_C)
878 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200879 {
880 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200882 iv, iv_len, ad, ad_len, input, output,
883 tag_len, tag ) );
884 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#endif /* MBEDTLS_GCM_C */
886#if defined(MBEDTLS_CCM_C)
887 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200888 {
889 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200891 iv, iv_len, ad, ad_len, input, output,
892 tag, tag_len ) );
893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200897}
898
899/*
900 * Packet-oriented decryption for AEAD modes
901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200903 const unsigned char *iv, size_t iv_len,
904 const unsigned char *ad, size_t ad_len,
905 const unsigned char *input, size_t ilen,
906 unsigned char *output, size_t *olen,
907 const unsigned char *tag, size_t tag_len )
908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_GCM_C)
910 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200911 {
912 int ret;
913
914 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200916 iv, iv_len, ad, ad_len,
917 tag, tag_len, input, output );
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
920 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200921
922 return( ret );
923 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_GCM_C */
925#if defined(MBEDTLS_CCM_C)
926 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200927 {
928 int ret;
929
930 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200932 iv, iv_len, ad, ad_len,
933 input, output, tag, tag_len );
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
936 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200937
938 return( ret );
939 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_CIPHER_C */