blob: b4101823c43676f411334b3c8e02cb1788dda3c0 [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 );
Gilles Peskine0f595f72020-01-21 15:02:14 +0100285 if ( 0 == block_size )
286 {
287 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
288 }
Paul Bakker6c212762013-12-16 15:24:50 +0100289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200291 {
Janos Follath98e28a72016-05-31 14:03:54 +0100292 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200294
295 *olen = ilen;
296
297 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
298 ctx->operation, input, output ) ) )
299 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200300 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200301 }
302
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200303 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_GCM_C)
307 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200308 {
309 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200311 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200312 }
313#endif
314
Paul Bakker68884e32013-01-07 18:20:04 +0100315 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100316 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100319 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_CIPHER_MODE_CBC)
322 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200324 size_t copy_len = 0;
325
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326 /*
327 * If there is not enough data for a full block, cache it.
328 */
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700329 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000330 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700331 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
332 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000334 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 {
336 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
337 ilen );
338
339 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341 }
342
343 /*
344 * Process cached data first
345 */
Janos Follath98e28a72016-05-31 14:03:54 +0100346 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347 {
Janos Follath98e28a72016-05-31 14:03:54 +0100348 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
350 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
351 copy_len );
352
Paul Bakkerff61a782011-06-09 15:42:02 +0000353 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100354 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000355 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200357 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 }
359
Janos Follath98e28a72016-05-31 14:03:54 +0100360 *olen += block_size;
361 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 ctx->unprocessed_len = 0;
363
364 input += copy_len;
365 ilen -= copy_len;
366 }
367
368 /*
369 * Cache final, incomplete block
370 */
371 if( 0 != ilen )
372 {
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700373 /* Encryption: only cache partial blocks
374 * Decryption w/ padding: always keep at least one whole block
375 * Decryption w/o padding: only cache partial blocks
376 */
Janos Follath98e28a72016-05-31 14:03:54 +0100377 copy_len = ilen % block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700378 if( copy_len == 0 &&
379 ctx->operation == MBEDTLS_DECRYPT &&
380 NULL != ctx->add_padding)
381 {
Janos Follath98e28a72016-05-31 14:03:54 +0100382 copy_len = block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700383 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
385 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
386 copy_len );
387
388 ctx->unprocessed_len += copy_len;
389 ilen -= copy_len;
390 }
391
392 /*
393 * Process remaining full blocks
394 */
395 if( ilen )
396 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000397 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
398 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200400 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200402
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403 *olen += ilen;
404 }
405
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_CIPHER_MODE_CFB)
411 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000412 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000413 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000414 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000415 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000416 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000418 }
419
420 *olen = ilen;
421
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_CIPHER_MODE_CTR)
427 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000428 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000429 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000430 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000431 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000432 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200433 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000434 }
435
436 *olen = ilen;
437
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200438 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_CIPHER_MODE_STREAM)
443 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200444 {
445 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
446 ilen, input, output ) ) )
447 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200448 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200449 }
450
451 *olen = ilen;
452
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200453 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458}
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
461#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200462/*
463 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
464 */
Paul Bakker23986e52011-04-24 08:57:21 +0000465static void add_pkcs_padding( unsigned char *output, size_t output_len,
466 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467{
Paul Bakker23986e52011-04-24 08:57:21 +0000468 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100469 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000470
471 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000472 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473}
474
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200475static int get_pkcs_padding( unsigned char *input, size_t input_len,
476 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100478 size_t i, pad_idx;
479 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480
Paul Bakkera885d682011-01-20 16:35:05 +0000481 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483
484 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485 *data_len = input_len - padding_len;
486
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100487 /* Avoid logical || since it results in a branch */
488 bad |= padding_len > input_len;
489 bad |= padding_len == 0;
490
491 /* The number of bytes checked must be independent of padding_len,
492 * so pick input_len, which is usually 8 or 16 (one block) */
493 pad_idx = input_len - padding_len;
494 for( i = 0; i < input_len; i++ )
495 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200502/*
503 * One and zeros padding: fill with 80 00 ... 00
504 */
505static void add_one_and_zeros_padding( unsigned char *output,
506 size_t output_len, size_t data_len )
507{
508 size_t padding_len = output_len - data_len;
509 unsigned char i = 0;
510
511 output[data_len] = 0x80;
512 for( i = 1; i < padding_len; i++ )
513 output[data_len + i] = 0x00;
514}
515
516static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
517 size_t *data_len )
518{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100519 size_t i;
520 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200521
522 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200524
Micha Krausba8316f2017-12-23 23:40:08 +0100525 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100526 *data_len = 0;
527 for( i = input_len; i > 0; i-- )
528 {
529 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100530 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100531 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100532 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100533 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200536
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200541/*
542 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
543 */
544static void add_zeros_and_len_padding( unsigned char *output,
545 size_t output_len, size_t data_len )
546{
547 size_t padding_len = output_len - data_len;
548 unsigned char i = 0;
549
550 for( i = 1; i < padding_len; i++ )
551 output[data_len + i - 1] = 0x00;
552 output[output_len - 1] = (unsigned char) padding_len;
553}
554
555static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
556 size_t *data_len )
557{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100558 size_t i, pad_idx;
559 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200560
561 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200563
564 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200565 *data_len = input_len - padding_len;
566
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100567 /* Avoid logical || since it results in a branch */
568 bad |= padding_len > input_len;
569 bad |= padding_len == 0;
570
571 /* The number of bytes checked must be independent of padding_len */
572 pad_idx = input_len - padding_len;
573 for( i = 0; i < input_len - 1; i++ )
574 bad |= input[i] * ( i >= pad_idx );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200581/*
582 * Zero padding: fill with 00 ... 00
583 */
584static void add_zeros_padding( unsigned char *output,
585 size_t output_len, size_t data_len )
586{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200587 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200588
589 for( i = data_len; i < output_len; i++ )
590 output[i] = 0x00;
591}
592
593static int get_zeros_padding( unsigned char *input, size_t input_len,
594 size_t *data_len )
595{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100596 size_t i;
597 unsigned char done = 0, prev_done;
598
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200599 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200601
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100602 *data_len = 0;
603 for( i = input_len; i > 0; i-- )
604 {
605 prev_done = done;
606 done |= ( input[i-1] != 0 );
607 *data_len |= i * ( done != prev_done );
608 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200609
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200610 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200613
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200614/*
615 * No padding: don't pad :)
616 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200618 * but a trivial get_padding function
619 */
620static int get_no_padding( unsigned char *input, size_t input_len,
621 size_t *data_len )
622{
623 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200625
626 *data_len = input_len;
627
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200628 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200633 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634{
635 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637
638 *olen = 0;
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
641 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
642 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
643 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000644 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200649 {
650 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200652
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200653 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200654 }
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_CIPHER_MODE_CBC)
657 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200659 int ret = 0;
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200663 /* check for 'no padding' mode */
664 if( NULL == ctx->add_padding )
665 {
666 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200668
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200669 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200670 }
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 ctx->unprocessed_len );
674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200677 /*
678 * For decrypt operations, expect a full block,
679 * or an empty block if no padding
680 */
681 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200682 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 }
686
687 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000688 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000690 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200692 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693 }
694
695 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 if( MBEDTLS_DECRYPT == ctx->operation )
697 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200698 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000699
700 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200702 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200704#else
705 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709}
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
712int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200713{
714 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200718 }
719
Paul Bakker1a45d912013-08-14 12:04:26 +0200720 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
723 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200724 ctx->add_padding = add_pkcs_padding;
725 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200726 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
729 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200730 ctx->add_padding = add_one_and_zeros_padding;
731 ctx->get_padding = get_one_and_zeros_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_ZEROS_AND_LEN)
735 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200736 ctx->add_padding = add_zeros_and_len_padding;
737 ctx->get_padding = get_zeros_and_len_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)
741 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200742 ctx->add_padding = add_zeros_padding;
743 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200747 ctx->add_padding = NULL;
748 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200749 break;
750
751 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200753 }
754
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200755 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_GCM_C)
760int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200761 unsigned char *tag, size_t tag_len )
762{
763 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( MBEDTLS_ENCRYPT != ctx->operation )
767 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
770 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200771
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200772 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200773}
Paul Bakker9af723c2014-05-01 13:03:14 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200776 const unsigned char *tag, size_t tag_len )
777{
778 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200780 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200784 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200787 {
788 unsigned char check_tag[16];
789 size_t i;
790 int diff;
791
792 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200796 check_tag, tag_len ) ) )
797 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200798 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200799 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200800
801 /* Check the tag in "constant-time" */
802 for( diff = 0, i = 0; i < tag_len; i++ )
803 diff |= tag[i] ^ check_tag[i];
804
805 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200807
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200808 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200809 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200810
811 return( 0 );
812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200814
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200815/*
816 * Packet-oriented wrapper for non-AEAD modes
817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200819 const unsigned char *iv, size_t iv_len,
820 const unsigned char *input, size_t ilen,
821 unsigned char *output, size_t *olen )
822{
823 int ret;
824 size_t finish_olen;
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200827 return( ret );
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200830 return( ret );
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 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_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200836 return( ret );
837
838 *olen += finish_olen;
839
840 return( 0 );
841}
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200844/*
845 * Packet-oriented encryption for AEAD modes
846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200848 const unsigned char *iv, size_t iv_len,
849 const unsigned char *ad, size_t ad_len,
850 const unsigned char *input, size_t ilen,
851 unsigned char *output, size_t *olen,
852 unsigned char *tag, size_t tag_len )
853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_GCM_C)
855 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200856 {
857 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200859 iv, iv_len, ad, ad_len, input, output,
860 tag_len, tag ) );
861 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_GCM_C */
863#if defined(MBEDTLS_CCM_C)
864 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200865 {
866 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200868 iv, iv_len, ad, ad_len, input, output,
869 tag, tag_len ) );
870 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200874}
875
876/*
877 * Packet-oriented decryption for AEAD modes
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200880 const unsigned char *iv, size_t iv_len,
881 const unsigned char *ad, size_t ad_len,
882 const unsigned char *input, size_t ilen,
883 unsigned char *output, size_t *olen,
884 const unsigned char *tag, size_t tag_len )
885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_GCM_C)
887 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200888 {
889 int ret;
890
891 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200893 iv, iv_len, ad, ad_len,
894 tag, tag_len, input, output );
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
897 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200898
899 return( ret );
900 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#endif /* MBEDTLS_GCM_C */
902#if defined(MBEDTLS_CCM_C)
903 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200904 {
905 int ret;
906
907 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200909 iv, iv_len, ad, ad_len,
910 input, output, tag, tag_len );
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
913 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200914
915 return( ret );
916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_CIPHER_C */