blob: bd39e4f09702d63a0c415166f3054b06fd794c19 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stdlib.h>
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020046#endif
47
Simon Butcher327398a2016-10-05 14:09:11 +010048#if defined(MBEDTLS_CMAC_C)
49#include "mbedtls/cmac.h"
50#endif
51
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Paul Bakker34617722014-06-13 17:20:13 +020059/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010061 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020062}
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 {
140 mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
141 mbedtls_free( ctx->cmac_ctx );
142 }
143#endif
144
Paul Bakker84bbeb52014-07-01 14:53:22 +0200145 if( ctx->cipher_ctx )
146 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200149}
150
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200151int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152{
153 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000157
Paul Bakker343a8702011-06-09 14:27:58 +0000158 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
161 ctx->cipher_info = cipher_info;
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200164 /*
165 * Ignore possible errors caused by a cipher mode that doesn't use padding
166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
168 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200169#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200173
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200174 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200178 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179{
180 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200184 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200187 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200188
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200189 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 ctx->operation = operation;
191
Paul Bakker343a8702011-06-09 14:27:58 +0000192 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000193 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( MBEDTLS_ENCRYPT == operation ||
196 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
197 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000198 {
199 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200200 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000201 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000204 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200205 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208}
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200211 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200213 size_t actual_iv_size;
Ron Eldorefba4b02017-09-25 18:22:32 +0300214 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Ron Eldorefba4b02017-09-25 18:22:32 +0300216 else if( NULL == iv && iv_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200217 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Ron Eldorde881c02017-10-01 17:04:54 +0300219 if( NULL == iv && iv_len == 0 )
220 ctx->iv_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200222 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
224 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200227 actual_iv_size = iv_len;
228 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200229 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200230 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200231
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200232 /* avoid reading past the end of input buffer */
233 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200235 }
Ron Eldorde881c02017-10-01 17:04:54 +0300236 if ( actual_iv_size != 0 )
Ron Eldorefba4b02017-09-25 18:22:32 +0300237 {
238 memcpy( ctx->iv, iv, actual_iv_size );
239 ctx->iv_size = actual_iv_size;
240 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200241
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200246{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200247 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200249
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250 ctx->unprocessed_len = 0;
251
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200252 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200253}
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_GCM_C)
256int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200257 const unsigned char *ad, size_t ad_len )
258{
259 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200265 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200266 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200267
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200268 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200273 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274{
Paul Bakkerff61a782011-06-09 15:42:02 +0000275 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100276 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277
Paul Bakker68884e32013-01-07 18:20:04 +0100278 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000281 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282
Paul Bakker6c212762013-12-16 15:24:50 +0100283 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100284 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200287 {
Janos Follath98e28a72016-05-31 14:03:54 +0100288 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200290
291 *olen = ilen;
292
293 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
294 ctx->operation, input, output ) ) )
295 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200296 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200297 }
298
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200299 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200300 }
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_GCM_C)
303 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200304 {
305 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200307 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200308 }
309#endif
310
Janos Follath98e28a72016-05-31 14:03:54 +0100311 if ( 0 == block_size )
312 {
313 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
314 }
315
Paul Bakker68884e32013-01-07 18:20:04 +0100316 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100317 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100320 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_CIPHER_MODE_CBC)
323 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200325 size_t copy_len = 0;
326
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 /*
328 * If there is not enough data for a full block, cache it.
329 */
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700330 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000331 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700332 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
333 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000335 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336 {
337 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
338 ilen );
339
340 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200341 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342 }
343
344 /*
345 * Process cached data first
346 */
Janos Follath98e28a72016-05-31 14:03:54 +0100347 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 {
Janos Follath98e28a72016-05-31 14:03:54 +0100349 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350
351 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
352 copy_len );
353
Paul Bakkerff61a782011-06-09 15:42:02 +0000354 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100355 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000356 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200358 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 }
360
Janos Follath98e28a72016-05-31 14:03:54 +0100361 *olen += block_size;
362 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 ctx->unprocessed_len = 0;
364
365 input += copy_len;
366 ilen -= copy_len;
367 }
368
369 /*
370 * Cache final, incomplete block
371 */
372 if( 0 != ilen )
373 {
Janos Follath98e28a72016-05-31 14:03:54 +0100374 if( 0 == block_size )
375 {
376 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
377 }
378
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700379 /* Encryption: only cache partial blocks
380 * Decryption w/ padding: always keep at least one whole block
381 * Decryption w/o padding: only cache partial blocks
382 */
Janos Follath98e28a72016-05-31 14:03:54 +0100383 copy_len = ilen % block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700384 if( copy_len == 0 &&
385 ctx->operation == MBEDTLS_DECRYPT &&
386 NULL != ctx->add_padding)
387 {
Janos Follath98e28a72016-05-31 14:03:54 +0100388 copy_len = block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700389 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390
391 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
392 copy_len );
393
394 ctx->unprocessed_len += copy_len;
395 ilen -= copy_len;
396 }
397
398 /*
399 * Process remaining full blocks
400 */
401 if( ilen )
402 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000403 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
404 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200408
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409 *olen += ilen;
410 }
411
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#if defined(MBEDTLS_CIPHER_MODE_CFB)
417 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000418 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000419 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000420 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000421 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000422 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200423 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000424 }
425
426 *olen = ilen;
427
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200428 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_CIPHER_MODE_CTR)
433 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000434 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000435 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000436 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000437 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000438 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200439 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000440 }
441
442 *olen = ilen;
443
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200444 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_CIPHER_MODE_STREAM)
449 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200450 {
451 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
452 ilen, input, output ) ) )
453 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200454 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200455 }
456
457 *olen = ilen;
458
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200459 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464}
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
467#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200468/*
469 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
470 */
Paul Bakker23986e52011-04-24 08:57:21 +0000471static void add_pkcs_padding( unsigned char *output, size_t output_len,
472 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473{
Paul Bakker23986e52011-04-24 08:57:21 +0000474 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100475 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476
477 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000478 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479}
480
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200481static int get_pkcs_padding( unsigned char *input, size_t input_len,
482 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100484 size_t i, pad_idx;
485 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486
Paul Bakkera885d682011-01-20 16:35:05 +0000487 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489
490 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000491 *data_len = input_len - padding_len;
492
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100493 /* Avoid logical || since it results in a branch */
494 bad |= padding_len > input_len;
495 bad |= padding_len == 0;
496
497 /* The number of bytes checked must be independent of padding_len,
498 * so pick input_len, which is usually 8 or 16 (one block) */
499 pad_idx = input_len - padding_len;
500 for( i = 0; i < input_len; i++ )
501 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200508/*
509 * One and zeros padding: fill with 80 00 ... 00
510 */
511static void add_one_and_zeros_padding( unsigned char *output,
512 size_t output_len, size_t data_len )
513{
514 size_t padding_len = output_len - data_len;
515 unsigned char i = 0;
516
517 output[data_len] = 0x80;
518 for( i = 1; i < padding_len; i++ )
519 output[data_len + i] = 0x00;
520}
521
522static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
523 size_t *data_len )
524{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100525 size_t i;
526 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200527
528 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200530
Micha Krausba8316f2017-12-23 23:40:08 +0100531 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100532 *data_len = 0;
533 for( i = input_len; i > 0; i-- )
534 {
535 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100536 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100537 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100538 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100539 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200542
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200547/*
548 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
549 */
550static void add_zeros_and_len_padding( unsigned char *output,
551 size_t output_len, size_t data_len )
552{
553 size_t padding_len = output_len - data_len;
554 unsigned char i = 0;
555
556 for( i = 1; i < padding_len; i++ )
557 output[data_len + i - 1] = 0x00;
558 output[output_len - 1] = (unsigned char) padding_len;
559}
560
561static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
562 size_t *data_len )
563{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100564 size_t i, pad_idx;
565 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200566
567 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200569
570 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200571 *data_len = input_len - padding_len;
572
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100573 /* Avoid logical || since it results in a branch */
574 bad |= padding_len > input_len;
575 bad |= padding_len == 0;
576
577 /* The number of bytes checked must be independent of padding_len */
578 pad_idx = input_len - padding_len;
579 for( i = 0; i < input_len - 1; i++ )
580 bad |= input[i] * ( i >= pad_idx );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200587/*
588 * Zero padding: fill with 00 ... 00
589 */
590static void add_zeros_padding( unsigned char *output,
591 size_t output_len, size_t data_len )
592{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200593 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200594
595 for( i = data_len; i < output_len; i++ )
596 output[i] = 0x00;
597}
598
599static int get_zeros_padding( unsigned char *input, size_t input_len,
600 size_t *data_len )
601{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100602 size_t i;
603 unsigned char done = 0, prev_done;
604
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200605 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200607
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100608 *data_len = 0;
609 for( i = input_len; i > 0; i-- )
610 {
611 prev_done = done;
612 done |= ( input[i-1] != 0 );
613 *data_len |= i * ( done != prev_done );
614 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200615
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200616 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200619
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200620/*
621 * No padding: don't pad :)
622 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200624 * but a trivial get_padding function
625 */
626static int get_no_padding( unsigned char *input, size_t input_len,
627 size_t *data_len )
628{
629 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200631
632 *data_len = input_len;
633
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200634 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200639 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640{
641 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
644 *olen = 0;
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
647 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
648 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
649 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000650 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000652 }
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200655 {
656 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200658
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200659 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_CIPHER_MODE_CBC)
663 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200665 int ret = 0;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200669 /* check for 'no padding' mode */
670 if( NULL == ctx->add_padding )
671 {
672 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200674
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200675 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200676 }
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 ctx->unprocessed_len );
680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200683 /*
684 * For decrypt operations, expect a full block,
685 * or an empty block if no padding
686 */
687 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200688 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691 }
692
693 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000694 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000696 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200698 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000699 }
700
701 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( MBEDTLS_DECRYPT == ctx->operation )
703 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200704 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705
706 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200708 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200710#else
711 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715}
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
718int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200719{
720 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200724 }
725
Paul Bakker1a45d912013-08-14 12:04:26 +0200726 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
729 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200730 ctx->add_padding = add_pkcs_padding;
731 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200732 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
735 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200736 ctx->add_padding = add_one_and_zeros_padding;
737 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200738 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
741 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200742 ctx->add_padding = add_zeros_and_len_padding;
743 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200744 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
747 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200748 ctx->add_padding = add_zeros_padding;
749 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200750 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200753 ctx->add_padding = NULL;
754 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200755 break;
756
757 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200759 }
760
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200761 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_GCM_C)
766int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200767 unsigned char *tag, size_t tag_len )
768{
769 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( MBEDTLS_ENCRYPT != ctx->operation )
773 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
776 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200777
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200778 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779}
Paul Bakker9af723c2014-05-01 13:03:14 +0200780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200782 const unsigned char *tag, size_t tag_len )
783{
784 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200785
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200786 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200790 }
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 )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200793 {
794 unsigned char check_tag[16];
795 size_t i;
796 int diff;
797
798 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200802 check_tag, tag_len ) ) )
803 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200804 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200805 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200806
807 /* Check the tag in "constant-time" */
808 for( diff = 0, i = 0; i < tag_len; i++ )
809 diff |= tag[i] ^ check_tag[i];
810
811 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200813
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200814 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200815 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200816
817 return( 0 );
818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200820
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200821/*
822 * Packet-oriented wrapper for non-AEAD modes
823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200825 const unsigned char *iv, size_t iv_len,
826 const unsigned char *input, size_t ilen,
827 unsigned char *output, size_t *olen )
828{
829 int ret;
830 size_t finish_olen;
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200833 return( ret );
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200836 return( ret );
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200839 return( ret );
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200842 return( ret );
843
844 *olen += finish_olen;
845
846 return( 0 );
847}
848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200850/*
851 * Packet-oriented encryption for AEAD modes
852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200854 const unsigned char *iv, size_t iv_len,
855 const unsigned char *ad, size_t ad_len,
856 const unsigned char *input, size_t ilen,
857 unsigned char *output, size_t *olen,
858 unsigned char *tag, size_t tag_len )
859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_GCM_C)
861 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200862 {
863 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200865 iv, iv_len, ad, ad_len, input, output,
866 tag_len, tag ) );
867 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_GCM_C */
869#if defined(MBEDTLS_CCM_C)
870 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200871 {
872 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200874 iv, iv_len, ad, ad_len, input, output,
875 tag, tag_len ) );
876 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200880}
881
882/*
883 * Packet-oriented decryption for AEAD modes
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200886 const unsigned char *iv, size_t iv_len,
887 const unsigned char *ad, size_t ad_len,
888 const unsigned char *input, size_t ilen,
889 unsigned char *output, size_t *olen,
890 const unsigned char *tag, size_t tag_len )
891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_GCM_C)
893 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200894 {
895 int ret;
896
897 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200899 iv, iv_len, ad, ad_len,
900 tag, tag_len, input, output );
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
903 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200904
905 return( ret );
906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#endif /* MBEDTLS_GCM_C */
908#if defined(MBEDTLS_CCM_C)
909 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200910 {
911 int ret;
912
913 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200915 iv, iv_len, ad, ad_len,
916 input, output, tag, tag_len );
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
919 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200920
921 return( ret );
922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_CIPHER_C */