blob: 2d85228aa582fcc21ea0cdb138c78fc74d3a2f15 [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
Jaeden Ameroc6539902018-04-30 17:17:41 +0100459#if defined(MBEDTLS_CIPHER_MODE_XTS)
460 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
461 {
462 if( ctx->unprocessed_len > 0 ) {
463 /* We can only process an entire data unit at a time. */
464 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
465 }
466
467 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
468 ctx->operation, ilen, ctx->iv, input, output );
469 if( ret != 0 )
470 {
471 return( ret );
472 }
473
474 *olen = ilen;
475
476 return( 0 );
477 }
478#endif /* MBEDTLS_CIPHER_MODE_XTS */
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_CIPHER_MODE_STREAM)
481 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200482 {
483 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
484 ilen, input, output ) ) )
485 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200486 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200487 }
488
489 *olen = ilen;
490
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200491 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496}
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
499#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500/*
501 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
502 */
Paul Bakker23986e52011-04-24 08:57:21 +0000503static void add_pkcs_padding( unsigned char *output, size_t output_len,
504 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505{
Paul Bakker23986e52011-04-24 08:57:21 +0000506 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100507 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508
509 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000510 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511}
512
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200513static int get_pkcs_padding( unsigned char *input, size_t input_len,
514 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100516 size_t i, pad_idx;
517 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518
Paul Bakkera885d682011-01-20 16:35:05 +0000519 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521
522 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000523 *data_len = input_len - padding_len;
524
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100525 /* Avoid logical || since it results in a branch */
526 bad |= padding_len > input_len;
527 bad |= padding_len == 0;
528
529 /* The number of bytes checked must be independent of padding_len,
530 * so pick input_len, which is usually 8 or 16 (one block) */
531 pad_idx = input_len - padding_len;
532 for( i = 0; i < input_len; i++ )
533 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200540/*
541 * One and zeros padding: fill with 80 00 ... 00
542 */
543static void add_one_and_zeros_padding( unsigned char *output,
544 size_t output_len, size_t data_len )
545{
546 size_t padding_len = output_len - data_len;
547 unsigned char i = 0;
548
549 output[data_len] = 0x80;
550 for( i = 1; i < padding_len; i++ )
551 output[data_len + i] = 0x00;
552}
553
554static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
555 size_t *data_len )
556{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100557 size_t i;
558 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200559
560 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200562
Micha Krausba8316f2017-12-23 23:40:08 +0100563 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100564 *data_len = 0;
565 for( i = input_len; i > 0; i-- )
566 {
567 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100568 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100569 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100570 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100571 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200574
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200579/*
580 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
581 */
582static void add_zeros_and_len_padding( unsigned char *output,
583 size_t output_len, size_t data_len )
584{
585 size_t padding_len = output_len - data_len;
586 unsigned char i = 0;
587
588 for( i = 1; i < padding_len; i++ )
589 output[data_len + i - 1] = 0x00;
590 output[output_len - 1] = (unsigned char) padding_len;
591}
592
593static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
594 size_t *data_len )
595{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100596 size_t i, pad_idx;
597 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200598
599 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200601
602 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200603 *data_len = input_len - padding_len;
604
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100605 /* Avoid logical || since it results in a branch */
606 bad |= padding_len > input_len;
607 bad |= padding_len == 0;
608
609 /* The number of bytes checked must be independent of padding_len */
610 pad_idx = input_len - padding_len;
611 for( i = 0; i < input_len - 1; i++ )
612 bad |= input[i] * ( i >= pad_idx );
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200619/*
620 * Zero padding: fill with 00 ... 00
621 */
622static void add_zeros_padding( unsigned char *output,
623 size_t output_len, size_t data_len )
624{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200625 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200626
627 for( i = data_len; i < output_len; i++ )
628 output[i] = 0x00;
629}
630
631static int get_zeros_padding( unsigned char *input, size_t input_len,
632 size_t *data_len )
633{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100634 size_t i;
635 unsigned char done = 0, prev_done;
636
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200637 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200639
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100640 *data_len = 0;
641 for( i = input_len; i > 0; i-- )
642 {
643 prev_done = done;
644 done |= ( input[i-1] != 0 );
645 *data_len |= i * ( done != prev_done );
646 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200647
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200651
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652/*
653 * No padding: don't pad :)
654 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656 * but a trivial get_padding function
657 */
658static int get_no_padding( unsigned char *input, size_t input_len,
659 size_t *data_len )
660{
661 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200663
664 *data_len = input_len;
665
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200671 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672{
673 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
676 *olen = 0;
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100679 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
681 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100682 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000684 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000686 }
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200689 {
690 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200692
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200693 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200694 }
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_CIPHER_MODE_CBC)
697 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200699 int ret = 0;
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000702 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200703 /* check for 'no padding' mode */
704 if( NULL == ctx->add_padding )
705 {
706 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200708
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200709 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200710 }
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713 ctx->unprocessed_len );
714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200717 /*
718 * For decrypt operations, expect a full block,
719 * or an empty block if no padding
720 */
721 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200722 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000725 }
726
727 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000728 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000730 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200732 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 }
734
735 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if( MBEDTLS_DECRYPT == ctx->operation )
737 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200738 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739
740 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200742 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200744#else
745 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749}
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
752int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200753{
754 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200758 }
759
Paul Bakker1a45d912013-08-14 12:04:26 +0200760 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
763 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200764 ctx->add_padding = add_pkcs_padding;
765 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200766 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
769 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200770 ctx->add_padding = add_one_and_zeros_padding;
771 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200772 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
775 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200776 ctx->add_padding = add_zeros_and_len_padding;
777 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200778 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
781 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200782 ctx->add_padding = add_zeros_padding;
783 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200784 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200787 ctx->add_padding = NULL;
788 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200789 break;
790
791 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200793 }
794
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200795 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_GCM_C)
800int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200801 unsigned char *tag, size_t tag_len )
802{
803 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( MBEDTLS_ENCRYPT != ctx->operation )
807 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
810 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200811
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200812 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200813}
Paul Bakker9af723c2014-05-01 13:03:14 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200816 const unsigned char *tag, size_t tag_len )
817{
818 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200819
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200820 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200824 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200827 {
828 unsigned char check_tag[16];
829 size_t i;
830 int diff;
831
832 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200836 check_tag, tag_len ) ) )
837 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200838 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200839 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200840
841 /* Check the tag in "constant-time" */
842 for( diff = 0, i = 0; i < tag_len; i++ )
843 diff |= tag[i] ^ check_tag[i];
844
845 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200847
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200848 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200849 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200850
851 return( 0 );
852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200854
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200855/*
856 * Packet-oriented wrapper for non-AEAD modes
857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200859 const unsigned char *iv, size_t iv_len,
860 const unsigned char *input, size_t ilen,
861 unsigned char *output, size_t *olen )
862{
863 int ret;
864 size_t finish_olen;
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200867 return( ret );
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200870 return( ret );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200873 return( ret );
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200876 return( ret );
877
878 *olen += finish_olen;
879
880 return( 0 );
881}
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200884/*
885 * Packet-oriented encryption for AEAD modes
886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200888 const unsigned char *iv, size_t iv_len,
889 const unsigned char *ad, size_t ad_len,
890 const unsigned char *input, size_t ilen,
891 unsigned char *output, size_t *olen,
892 unsigned char *tag, size_t tag_len )
893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_GCM_C)
895 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200896 {
897 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200899 iv, iv_len, ad, ad_len, input, output,
900 tag_len, tag ) );
901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_GCM_C */
903#if defined(MBEDTLS_CCM_C)
904 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200905 {
906 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200908 iv, iv_len, ad, ad_len, input, output,
909 tag, tag_len ) );
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200914}
915
916/*
917 * Packet-oriented decryption for AEAD modes
918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200920 const unsigned char *iv, size_t iv_len,
921 const unsigned char *ad, size_t ad_len,
922 const unsigned char *input, size_t ilen,
923 unsigned char *output, size_t *olen,
924 const unsigned char *tag, size_t tag_len )
925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_GCM_C)
927 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200928 {
929 int ret;
930
931 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200933 iv, iv_len, ad, ad_len,
934 tag, tag_len, input, output );
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
937 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200938
939 return( ret );
940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#endif /* MBEDTLS_GCM_C */
942#if defined(MBEDTLS_CCM_C)
943 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200944 {
945 int ret;
946
947 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200949 iv, iv_len, ad, ad_len,
950 input, output, tag, tag_len );
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
953 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200954
955 return( ret );
956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#endif /* MBEDTLS_CIPHER_C */