blob: b8da586cbbd16e88fae4aa6c1851a14349f2d48d [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000028 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000051 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020062#include "mbedtls/cipher_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stdlib.h>
65#include <string.h>
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020073#endif
74
Simon Butcher327398a2016-10-05 14:09:11 +010075#if defined(MBEDTLS_CMAC_C)
76#include "mbedtls/cmac.h"
77#endif
78
79#if defined(MBEDTLS_PLATFORM_C)
80#include "mbedtls/platform.h"
81#else
82#define mbedtls_calloc calloc
83#define mbedtls_free free
84#endif
85
Paul Bakker34617722014-06-13 17:20:13 +020086/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010088 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020089}
90
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020091static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020096 int *type;
97
98 if( ! supported_init )
99 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 def = mbedtls_cipher_definitions;
101 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200102
103 while( def->type != 0 )
104 *type++ = (*def++).type;
105
106 *type = 0;
107
108 supported_init = 1;
109 }
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +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->type == cipher_type )
120 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000121
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200122 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200128
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200130 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200133 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200134 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000135
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139const 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 +0200140 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200146 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200147 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200148 def->info->mode == mode )
149 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200150
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200151 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157}
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200160{
161 if( ctx == NULL )
162 return;
163
Simon Butcher327398a2016-10-05 14:09:11 +0100164#if defined(MBEDTLS_CMAC_C)
165 if( ctx->cmac_ctx )
166 {
167 mbedtls_zeroize( ctx->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
168 mbedtls_free( ctx->cmac_ctx );
169 }
170#endif
171
Paul Bakker84bbeb52014-07-01 14:53:22 +0200172 if( ctx->cipher_ctx )
173 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176}
177
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200178int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179{
180 if( NULL == cipher_info || NULL == ctx )
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 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184
Paul Bakker343a8702011-06-09 14:27:58 +0000185 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
188 ctx->cipher_info = cipher_info;
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200191 /*
192 * Ignore possible errors caused by a cipher mode that doesn't use padding
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
195 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200196#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200200
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200201 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200205 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206{
207 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200211 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200214 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200215
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200216 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 ctx->operation = operation;
218
Paul Bakker343a8702011-06-09 14:27:58 +0000219 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000220 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( MBEDTLS_ENCRYPT == operation ||
223 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
224 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000225 {
226 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200227 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000228 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000231 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200232 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235}
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200238 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200240 size_t actual_iv_size;
Ron Eldorefba4b02017-09-25 18:22:32 +0300241 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Ron Eldorefba4b02017-09-25 18:22:32 +0300243 else if( NULL == iv && iv_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245
Ron Eldorde881c02017-10-01 17:04:54 +0300246 if( NULL == iv && iv_len == 0 )
247 ctx->iv_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200249 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
251 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200254 actual_iv_size = iv_len;
255 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200256 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200257 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200258
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200259 /* avoid reading past the end of input buffer */
260 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200262 }
Ron Eldorde881c02017-10-01 17:04:54 +0300263 if ( actual_iv_size != 0 )
Ron Eldorefba4b02017-09-25 18:22:32 +0300264 {
265 memcpy( ctx->iv, iv, actual_iv_size );
266 ctx->iv_size = actual_iv_size;
267 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200268
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200269 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200273{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200274 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200276
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277 ctx->unprocessed_len = 0;
278
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200280}
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_GCM_C)
283int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200284 const unsigned char *ad, size_t ad_len )
285{
286 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200292 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200293 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200294
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200295 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200300 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301{
Paul Bakkerff61a782011-06-09 15:42:02 +0000302 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100303 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304
Paul Bakker68884e32013-01-07 18:20:04 +0100305 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000308 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309
Paul Bakker6c212762013-12-16 15:24:50 +0100310 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100311 block_size = mbedtls_cipher_get_block_size( ctx );
Gilles Peskine0f595f72020-01-21 15:02:14 +0100312 if ( 0 == block_size )
313 {
314 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
315 }
Paul Bakker6c212762013-12-16 15:24:50 +0100316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200318 {
Janos Follath98e28a72016-05-31 14:03:54 +0100319 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200321
322 *olen = ilen;
323
324 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
325 ctx->operation, input, output ) ) )
326 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200327 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200328 }
329
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200330 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200331 }
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_GCM_C)
334 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200335 {
336 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200338 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200339 }
340#endif
341
Paul Bakker68884e32013-01-07 18:20:04 +0100342 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100343 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100346 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_CIPHER_MODE_CBC)
349 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200351 size_t copy_len = 0;
352
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353 /*
354 * If there is not enough data for a full block, cache it.
355 */
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700356 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000357 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700358 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
359 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000361 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 {
363 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
364 ilen );
365
366 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200367 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368 }
369
370 /*
371 * Process cached data first
372 */
Janos Follath98e28a72016-05-31 14:03:54 +0100373 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 {
Janos Follath98e28a72016-05-31 14:03:54 +0100375 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
377 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
378 copy_len );
379
Paul Bakkerff61a782011-06-09 15:42:02 +0000380 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100381 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000382 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200384 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 }
386
Janos Follath98e28a72016-05-31 14:03:54 +0100387 *olen += block_size;
388 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 ctx->unprocessed_len = 0;
390
391 input += copy_len;
392 ilen -= copy_len;
393 }
394
395 /*
396 * Cache final, incomplete block
397 */
398 if( 0 != ilen )
399 {
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700400 /* Encryption: only cache partial blocks
401 * Decryption w/ padding: always keep at least one whole block
402 * Decryption w/o padding: only cache partial blocks
403 */
Janos Follath98e28a72016-05-31 14:03:54 +0100404 copy_len = ilen % block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700405 if( copy_len == 0 &&
406 ctx->operation == MBEDTLS_DECRYPT &&
407 NULL != ctx->add_padding)
408 {
Janos Follath98e28a72016-05-31 14:03:54 +0100409 copy_len = block_size;
Andy Leiserson38a29ee2017-04-28 20:01:49 -0700410 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
412 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
413 copy_len );
414
415 ctx->unprocessed_len += copy_len;
416 ilen -= copy_len;
417 }
418
419 /*
420 * Process remaining full blocks
421 */
422 if( ilen )
423 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000424 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
425 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200429
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 *olen += ilen;
431 }
432
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200433 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#if defined(MBEDTLS_CIPHER_MODE_CFB)
438 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000439 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000440 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000441 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000443 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200444 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000445 }
446
447 *olen = ilen;
448
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200449 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_CIPHER_MODE_CTR)
454 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000455 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000456 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000457 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000458 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000459 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200460 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000461 }
462
463 *olen = ilen;
464
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_CIPHER_MODE_STREAM)
470 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200471 {
472 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
473 ilen, input, output ) ) )
474 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200476 }
477
478 *olen = ilen;
479
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200480 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485}
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
488#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200489/*
490 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
491 */
Paul Bakker23986e52011-04-24 08:57:21 +0000492static void add_pkcs_padding( unsigned char *output, size_t output_len,
493 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494{
Paul Bakker23986e52011-04-24 08:57:21 +0000495 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100496 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
498 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000499 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500}
501
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200502static int get_pkcs_padding( unsigned char *input, size_t input_len,
503 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100505 size_t i, pad_idx;
506 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507
Paul Bakkera885d682011-01-20 16:35:05 +0000508 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
511 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512 *data_len = input_len - padding_len;
513
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100514 /* Avoid logical || since it results in a branch */
515 bad |= padding_len > input_len;
516 bad |= padding_len == 0;
517
518 /* The number of bytes checked must be independent of padding_len,
519 * so pick input_len, which is usually 8 or 16 (one block) */
520 pad_idx = input_len - padding_len;
521 for( i = 0; i < input_len; i++ )
522 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200529/*
530 * One and zeros padding: fill with 80 00 ... 00
531 */
532static void add_one_and_zeros_padding( unsigned char *output,
533 size_t output_len, size_t data_len )
534{
535 size_t padding_len = output_len - data_len;
536 unsigned char i = 0;
537
538 output[data_len] = 0x80;
539 for( i = 1; i < padding_len; i++ )
540 output[data_len + i] = 0x00;
541}
542
543static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
544 size_t *data_len )
545{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100546 size_t i;
547 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200548
549 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200551
Micha Krausba8316f2017-12-23 23:40:08 +0100552 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100553 *data_len = 0;
554 for( i = input_len; i > 0; i-- )
555 {
556 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100557 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100558 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100559 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100560 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200563
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200568/*
569 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
570 */
571static void add_zeros_and_len_padding( unsigned char *output,
572 size_t output_len, size_t data_len )
573{
574 size_t padding_len = output_len - data_len;
575 unsigned char i = 0;
576
577 for( i = 1; i < padding_len; i++ )
578 output[data_len + i - 1] = 0x00;
579 output[output_len - 1] = (unsigned char) padding_len;
580}
581
582static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
583 size_t *data_len )
584{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100585 size_t i, pad_idx;
586 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200587
588 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200590
591 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200592 *data_len = input_len - padding_len;
593
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100594 /* Avoid logical || since it results in a branch */
595 bad |= padding_len > input_len;
596 bad |= padding_len == 0;
597
598 /* The number of bytes checked must be independent of padding_len */
599 pad_idx = input_len - padding_len;
600 for( i = 0; i < input_len - 1; i++ )
601 bad |= input[i] * ( i >= pad_idx );
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200608/*
609 * Zero padding: fill with 00 ... 00
610 */
611static void add_zeros_padding( unsigned char *output,
612 size_t output_len, size_t data_len )
613{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200614 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200615
616 for( i = data_len; i < output_len; i++ )
617 output[i] = 0x00;
618}
619
620static int get_zeros_padding( unsigned char *input, size_t input_len,
621 size_t *data_len )
622{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100623 size_t i;
624 unsigned char done = 0, prev_done;
625
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200626 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200628
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100629 *data_len = 0;
630 for( i = input_len; i > 0; i-- )
631 {
632 prev_done = done;
633 done |= ( input[i-1] != 0 );
634 *data_len |= i * ( done != prev_done );
635 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200636
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200637 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200640
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641/*
642 * No padding: don't pad :)
643 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200645 * but a trivial get_padding function
646 */
647static int get_no_padding( unsigned char *input, size_t input_len,
648 size_t *data_len )
649{
650 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652
653 *data_len = input_len;
654
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200660 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661{
662 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664
665 *olen = 0;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
668 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
669 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
670 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000671 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000673 }
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200676 {
677 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200679
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200681 }
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_CIPHER_MODE_CBC)
684 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200686 int ret = 0;
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000689 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200690 /* check for 'no padding' mode */
691 if( NULL == ctx->add_padding )
692 {
693 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200695
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200697 }
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700 ctx->unprocessed_len );
701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200704 /*
705 * For decrypt operations, expect a full block,
706 * or an empty block if no padding
707 */
708 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200709 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712 }
713
714 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000715 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000717 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000718 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200719 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720 }
721
722 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 if( MBEDTLS_DECRYPT == ctx->operation )
724 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200725 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726
727 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200729 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200731#else
732 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736}
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
739int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200740{
741 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200745 }
746
Paul Bakker1a45d912013-08-14 12:04:26 +0200747 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
750 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200751 ctx->add_padding = add_pkcs_padding;
752 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200753 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
756 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200757 ctx->add_padding = add_one_and_zeros_padding;
758 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200759 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
762 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200763 ctx->add_padding = add_zeros_and_len_padding;
764 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200765 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
768 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200769 ctx->add_padding = add_zeros_padding;
770 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200771 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200774 ctx->add_padding = NULL;
775 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200776 break;
777
778 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200780 }
781
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200782 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_GCM_C)
787int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200788 unsigned char *tag, size_t tag_len )
789{
790 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( MBEDTLS_ENCRYPT != ctx->operation )
794 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
797 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200798
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200799 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200800}
Paul Bakker9af723c2014-05-01 13:03:14 +0200801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200803 const unsigned char *tag, size_t tag_len )
804{
805 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200806
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200807 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200811 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200814 {
815 unsigned char check_tag[16];
816 size_t i;
817 int diff;
818
819 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200823 check_tag, tag_len ) ) )
824 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200825 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200826 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200827
828 /* Check the tag in "constant-time" */
829 for( diff = 0, i = 0; i < tag_len; i++ )
830 diff |= tag[i] ^ check_tag[i];
831
832 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200834
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200835 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200836 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200837
838 return( 0 );
839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200841
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200842/*
843 * Packet-oriented wrapper for non-AEAD modes
844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200846 const unsigned char *iv, size_t iv_len,
847 const unsigned char *input, size_t ilen,
848 unsigned char *output, size_t *olen )
849{
850 int ret;
851 size_t finish_olen;
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200854 return( ret );
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200857 return( ret );
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200860 return( ret );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200863 return( ret );
864
865 *olen += finish_olen;
866
867 return( 0 );
868}
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200871/*
872 * Packet-oriented encryption for AEAD modes
873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200875 const unsigned char *iv, size_t iv_len,
876 const unsigned char *ad, size_t ad_len,
877 const unsigned char *input, size_t ilen,
878 unsigned char *output, size_t *olen,
879 unsigned char *tag, size_t tag_len )
880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_GCM_C)
882 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200883 {
884 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200886 iv, iv_len, ad, ad_len, input, output,
887 tag_len, tag ) );
888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#endif /* MBEDTLS_GCM_C */
890#if defined(MBEDTLS_CCM_C)
891 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200892 {
893 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200895 iv, iv_len, ad, ad_len, input, output,
896 tag, tag_len ) );
897 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200901}
902
903/*
904 * Packet-oriented decryption for AEAD modes
905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200907 const unsigned char *iv, size_t iv_len,
908 const unsigned char *ad, size_t ad_len,
909 const unsigned char *input, size_t ilen,
910 unsigned char *output, size_t *olen,
911 const unsigned char *tag, size_t tag_len )
912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_GCM_C)
914 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200915 {
916 int ret;
917
918 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200920 iv, iv_len, ad, ad_len,
921 tag, tag_len, input, output );
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
924 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200925
926 return( ret );
927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#endif /* MBEDTLS_GCM_C */
929#if defined(MBEDTLS_CCM_C)
930 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200931 {
932 int ret;
933
934 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200936 iv, iv_len, ad, ad_len,
937 input, output, tag, tag_len );
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
940 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200941
942 return( ret );
943 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_CIPHER_C */