blob: 2c599e54814b510b2a4d73492ad91ca66c94278b [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Simon Butcher327398a2016-10-05 14:09:11 +010049#if defined(MBEDTLS_CMAC_C)
50#include "mbedtls/cmac.h"
51#endif
52
53#if defined(MBEDTLS_PLATFORM_C)
54#include "mbedtls/platform.h"
55#else
56#define mbedtls_calloc calloc
57#define mbedtls_free free
58#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
61#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020062#endif
63
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020064static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020069 int *type;
70
71 if( ! supported_init )
72 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 def = mbedtls_cipher_definitions;
74 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020075
76 while( def->type != 0 )
77 *type++ = (*def++).type;
78
79 *type = 0;
80
81 supported_init = 1;
82 }
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000085}
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020092 if( def->type == cipher_type )
93 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000094
Paul Bakkerd8bb8262014-06-17 14:06:49 +020095 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200103 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200106 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200107 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000108
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200109 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200113 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200119 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200120 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200121 def->info->mode == mode )
122 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200123
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200133{
134 if( ctx == NULL )
135 return;
136
Simon Butcher327398a2016-10-05 14:09:11 +0100137#if defined(MBEDTLS_CMAC_C)
138 if( ctx->cmac_ctx )
139 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500140 mbedtls_platform_zeroize( ctx->cmac_ctx,
141 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100142 mbedtls_free( ctx->cmac_ctx );
143 }
144#endif
145
Paul Bakker84bbeb52014-07-01 14:53:22 +0200146 if( ctx->cipher_ctx )
147 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500149 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150}
151
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200152int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153{
154 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
Paul Bakker343a8702011-06-09 14:27:58 +0000159 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
162 ctx->cipher_info = cipher_info;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200165 /*
166 * Ignore possible errors caused by a cipher mode that doesn't use padding
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
169 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200174
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200175 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200179 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180{
181 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200185 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200188 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200189
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200190 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191 ctx->operation = operation;
192
Paul Bakker343a8702011-06-09 14:27:58 +0000193 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100194 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( MBEDTLS_ENCRYPT == operation ||
197 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100198 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000200 {
201 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200202 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000203 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000206 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200207 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210}
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200213 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200215 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200216
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200220 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
222 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200225 actual_iv_size = iv_len;
226 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200227 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200228 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200229
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200230 /* avoid reading past the end of input buffer */
231 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200233 }
234
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200235 memcpy( ctx->iv, iv, actual_iv_size );
236 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200237
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200242{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200243 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200245
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246 ctx->unprocessed_len = 0;
247
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200249}
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_GCM_C)
252int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200253 const unsigned char *ad, size_t ad_len )
254{
255 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200261 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200262 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200263
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200264 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200269 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270{
Paul Bakkerff61a782011-06-09 15:42:02 +0000271 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100272 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273
Paul Bakker68884e32013-01-07 18:20:04 +0100274 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000277 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278
Paul Bakker6c212762013-12-16 15:24:50 +0100279 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100280 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200283 {
Janos Follath98e28a72016-05-31 14:03:54 +0100284 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200286
287 *olen = ilen;
288
289 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
290 ctx->operation, input, output ) ) )
291 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200292 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200293 }
294
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200295 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200296 }
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_GCM_C)
299 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200300 {
301 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200303 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200304 }
305#endif
306
Janos Follath98e28a72016-05-31 14:03:54 +0100307 if ( 0 == block_size )
308 {
309 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
310 }
311
Paul Bakker68884e32013-01-07 18:20:04 +0100312 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100313 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100316 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#if defined(MBEDTLS_CIPHER_MODE_CBC)
319 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200321 size_t copy_len = 0;
322
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 /*
324 * If there is not enough data for a full block, cache it.
325 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700326 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000327 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700328 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
329 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000331 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332 {
333 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
334 ilen );
335
336 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200337 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338 }
339
340 /*
341 * Process cached data first
342 */
Janos Follath98e28a72016-05-31 14:03:54 +0100343 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 {
Janos Follath98e28a72016-05-31 14:03:54 +0100345 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346
347 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
348 copy_len );
349
Paul Bakkerff61a782011-06-09 15:42:02 +0000350 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100351 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000352 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200354 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355 }
356
Janos Follath98e28a72016-05-31 14:03:54 +0100357 *olen += block_size;
358 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 ctx->unprocessed_len = 0;
360
361 input += copy_len;
362 ilen -= copy_len;
363 }
364
365 /*
366 * Cache final, incomplete block
367 */
368 if( 0 != ilen )
369 {
Janos Follath98e28a72016-05-31 14:03:54 +0100370 if( 0 == block_size )
371 {
372 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
373 }
374
Andy Leiserson79e77892017-04-28 20:01:49 -0700375 /* Encryption: only cache partial blocks
376 * Decryption w/ padding: always keep at least one whole block
377 * Decryption w/o padding: only cache partial blocks
378 */
Janos Follath98e28a72016-05-31 14:03:54 +0100379 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700380 if( copy_len == 0 &&
381 ctx->operation == MBEDTLS_DECRYPT &&
382 NULL != ctx->add_padding)
383 {
Janos Follath98e28a72016-05-31 14:03:54 +0100384 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700385 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
387 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
388 copy_len );
389
390 ctx->unprocessed_len += copy_len;
391 ilen -= copy_len;
392 }
393
394 /*
395 * Process remaining full blocks
396 */
397 if( ilen )
398 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000399 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
400 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200402 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200404
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 *olen += ilen;
406 }
407
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200408 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_CIPHER_MODE_CFB)
413 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000414 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000415 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000416 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000417 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000418 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200419 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000420 }
421
422 *olen = ilen;
423
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200424 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000427
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100428#if defined(MBEDTLS_CIPHER_MODE_OFB)
429 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
430 {
431 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
432 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
433 {
434 return( ret );
435 }
436
437 *olen = ilen;
438
439 return( 0 );
440 }
441#endif /* MBEDTLS_CIPHER_MODE_OFB */
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_CIPHER_MODE_CTR)
444 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000445 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000446 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000447 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000448 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000449 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200450 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000451 }
452
453 *olen = ilen;
454
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200455 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_CIPHER_MODE_STREAM)
460 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200461 {
462 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
463 ilen, input, output ) ) )
464 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200466 }
467
468 *olen = ilen;
469
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200470 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475}
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
478#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200479/*
480 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
481 */
Paul Bakker23986e52011-04-24 08:57:21 +0000482static void add_pkcs_padding( unsigned char *output, size_t output_len,
483 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484{
Paul Bakker23986e52011-04-24 08:57:21 +0000485 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100486 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487
488 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000489 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490}
491
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200492static int get_pkcs_padding( unsigned char *input, size_t input_len,
493 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100495 size_t i, pad_idx;
496 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
Paul Bakkera885d682011-01-20 16:35:05 +0000498 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500
501 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502 *data_len = input_len - padding_len;
503
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100504 /* Avoid logical || since it results in a branch */
505 bad |= padding_len > input_len;
506 bad |= padding_len == 0;
507
508 /* The number of bytes checked must be independent of padding_len,
509 * so pick input_len, which is usually 8 or 16 (one block) */
510 pad_idx = input_len - padding_len;
511 for( i = 0; i < input_len; i++ )
512 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200519/*
520 * One and zeros padding: fill with 80 00 ... 00
521 */
522static void add_one_and_zeros_padding( unsigned char *output,
523 size_t output_len, size_t data_len )
524{
525 size_t padding_len = output_len - data_len;
526 unsigned char i = 0;
527
528 output[data_len] = 0x80;
529 for( i = 1; i < padding_len; i++ )
530 output[data_len + i] = 0x00;
531}
532
533static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
534 size_t *data_len )
535{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100536 size_t i;
537 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200538
539 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200541
Micha Krausba8316f2017-12-23 23:40:08 +0100542 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100543 *data_len = 0;
544 for( i = input_len; i > 0; i-- )
545 {
546 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100547 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100548 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100549 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100550 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200553
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200554}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200558/*
559 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
560 */
561static void add_zeros_and_len_padding( unsigned char *output,
562 size_t output_len, size_t data_len )
563{
564 size_t padding_len = output_len - data_len;
565 unsigned char i = 0;
566
567 for( i = 1; i < padding_len; i++ )
568 output[data_len + i - 1] = 0x00;
569 output[output_len - 1] = (unsigned char) padding_len;
570}
571
572static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
573 size_t *data_len )
574{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100575 size_t i, pad_idx;
576 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200577
578 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200580
581 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200582 *data_len = input_len - padding_len;
583
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100584 /* Avoid logical || since it results in a branch */
585 bad |= padding_len > input_len;
586 bad |= padding_len == 0;
587
588 /* The number of bytes checked must be independent of padding_len */
589 pad_idx = input_len - padding_len;
590 for( i = 0; i < input_len - 1; i++ )
591 bad |= input[i] * ( i >= pad_idx );
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200598/*
599 * Zero padding: fill with 00 ... 00
600 */
601static void add_zeros_padding( unsigned char *output,
602 size_t output_len, size_t data_len )
603{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200604 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200605
606 for( i = data_len; i < output_len; i++ )
607 output[i] = 0x00;
608}
609
610static int get_zeros_padding( unsigned char *input, size_t input_len,
611 size_t *data_len )
612{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100613 size_t i;
614 unsigned char done = 0, prev_done;
615
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200616 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200618
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100619 *data_len = 0;
620 for( i = input_len; i > 0; i-- )
621 {
622 prev_done = done;
623 done |= ( input[i-1] != 0 );
624 *data_len |= i * ( done != prev_done );
625 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200626
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200627 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200630
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200631/*
632 * No padding: don't pad :)
633 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200635 * but a trivial get_padding function
636 */
637static int get_no_padding( unsigned char *input, size_t input_len,
638 size_t *data_len )
639{
640 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200642
643 *data_len = input_len;
644
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200650 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651{
652 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654
655 *olen = 0;
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100658 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
660 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
661 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000662 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200663 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000664 }
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200667 {
668 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200670
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200672 }
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_CIPHER_MODE_CBC)
675 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200677 int ret = 0;
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200681 /* check for 'no padding' mode */
682 if( NULL == ctx->add_padding )
683 {
684 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200686
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200688 }
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691 ctx->unprocessed_len );
692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200695 /*
696 * For decrypt operations, expect a full block,
697 * or an empty block if no padding
698 */
699 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200700 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703 }
704
705 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000706 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000708 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200710 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711 }
712
713 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( MBEDTLS_DECRYPT == ctx->operation )
715 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200716 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717
718 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200720 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000721 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200722#else
723 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000727}
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
730int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200731{
732 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200736 }
737
Paul Bakker1a45d912013-08-14 12:04:26 +0200738 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
741 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200742 ctx->add_padding = add_pkcs_padding;
743 ctx->get_padding = get_pkcs_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_ONE_AND_ZEROS)
747 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200748 ctx->add_padding = add_one_and_zeros_padding;
749 ctx->get_padding = get_one_and_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#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
753 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200754 ctx->add_padding = add_zeros_and_len_padding;
755 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200756 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
759 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200760 ctx->add_padding = add_zeros_padding;
761 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200762 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200765 ctx->add_padding = NULL;
766 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200767 break;
768
769 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200771 }
772
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200773 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_GCM_C)
778int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779 unsigned char *tag, size_t tag_len )
780{
781 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 if( MBEDTLS_ENCRYPT != ctx->operation )
785 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
788 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200789
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200790 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200791}
Paul Bakker9af723c2014-05-01 13:03:14 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200794 const unsigned char *tag, size_t tag_len )
795{
796 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200797
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200798 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200802 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200805 {
806 unsigned char check_tag[16];
807 size_t i;
808 int diff;
809
810 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200814 check_tag, tag_len ) ) )
815 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200816 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200817 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200818
819 /* Check the tag in "constant-time" */
820 for( diff = 0, i = 0; i < tag_len; i++ )
821 diff |= tag[i] ^ check_tag[i];
822
823 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200825
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200826 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200827 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200828
829 return( 0 );
830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200832
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200833/*
834 * Packet-oriented wrapper for non-AEAD modes
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200837 const unsigned char *iv, size_t iv_len,
838 const unsigned char *input, size_t ilen,
839 unsigned char *output, size_t *olen )
840{
841 int ret;
842 size_t finish_olen;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200845 return( ret );
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200848 return( ret );
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200851 return( ret );
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200854 return( ret );
855
856 *olen += finish_olen;
857
858 return( 0 );
859}
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200862/*
863 * Packet-oriented encryption for AEAD modes
864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200866 const unsigned char *iv, size_t iv_len,
867 const unsigned char *ad, size_t ad_len,
868 const unsigned char *input, size_t ilen,
869 unsigned char *output, size_t *olen,
870 unsigned char *tag, size_t tag_len )
871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_GCM_C)
873 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200874 {
875 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200877 iv, iv_len, ad, ad_len, input, output,
878 tag_len, tag ) );
879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_GCM_C */
881#if defined(MBEDTLS_CCM_C)
882 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200883 {
884 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200886 iv, iv_len, ad, ad_len, input, output,
887 tag, tag_len ) );
888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200892}
893
894/*
895 * Packet-oriented decryption for AEAD modes
896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200898 const unsigned char *iv, size_t iv_len,
899 const unsigned char *ad, size_t ad_len,
900 const unsigned char *input, size_t ilen,
901 unsigned char *output, size_t *olen,
902 const unsigned char *tag, size_t tag_len )
903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_GCM_C)
905 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200906 {
907 int ret;
908
909 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200911 iv, iv_len, ad, ad_len,
912 tag, tag_len, input, output );
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
915 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200916
917 return( ret );
918 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_GCM_C */
920#if defined(MBEDTLS_CCM_C)
921 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200922 {
923 int ret;
924
925 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200927 iv, iv_len, ad, ad_len,
928 input, output, tag, tag_len );
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
931 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200932
933 return( ret );
934 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#endif /* MBEDTLS_CIPHER_C */