blob: 0dc51520fab935b7b8500fcaea774da802d2d0e5 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stdlib.h>
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020046#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
49#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020050#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010054 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020055}
56
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020057static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020062 int *type;
63
64 if( ! supported_init )
65 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 def = mbedtls_cipher_definitions;
67 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068
69 while( def->type != 0 )
70 *type++ = (*def++).type;
71
72 *type = 0;
73
74 supported_init = 1;
75 }
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000078}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 if( def->type == cipher_type )
86 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000087
Paul Bakkerd8bb8262014-06-17 14:06:49 +020088 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000089}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020094
Paul Bakker8123e9d2011-01-06 15:37:30 +000095 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020096 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +020099 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200100 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000101
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103}
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105const 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 +0200106 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200112 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200113 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114 def->info->mode == mode )
115 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200116
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200117 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200118}
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200126{
127 if( ctx == NULL )
128 return;
129
130 if( ctx->cipher_ctx )
131 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200134}
135
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200136int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137{
138 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142
Paul Bakker343a8702011-06-09 14:27:58 +0000143 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145
146 ctx->cipher_info = cipher_info;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200149 /*
150 * Ignore possible errors caused by a cipher mode that doesn't use padding
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
153 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200154#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200158
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200159 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200163 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164{
165 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200169 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200172 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200173
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200174 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 ctx->operation = operation;
176
Paul Bakker343a8702011-06-09 14:27:58 +0000177 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000178 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( MBEDTLS_ENCRYPT == operation ||
181 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
182 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000183 {
184 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200185 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000186 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000189 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200190 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200196 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200198 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200199
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200203 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
205 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200208 actual_iv_size = iv_len;
209 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200210 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200211 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200212
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200213 /* avoid reading past the end of input buffer */
214 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200216 }
217
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200218 memcpy( ctx->iv, iv, actual_iv_size );
219 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200220
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200221 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200225{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200226 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200228
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 ctx->unprocessed_len = 0;
230
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200231 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_GCM_C)
235int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200236 const unsigned char *ad, size_t ad_len )
237{
238 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200244 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200245 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200246
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200247 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200252 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253{
Paul Bakkerff61a782011-06-09 15:42:02 +0000254 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255
Paul Bakker68884e32013-01-07 18:20:04 +0100256 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000259 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
Paul Bakker6c212762013-12-16 15:24:50 +0100261 *olen = 0;
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
266 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200267
268 *olen = ilen;
269
270 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
271 ctx->operation, input, output ) ) )
272 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200273 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200274 }
275
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200276 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_GCM_C)
280 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200281 {
282 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200284 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200285 }
286#endif
287
Paul Bakker68884e32013-01-07 18:20:04 +0100288 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100292 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_CIPHER_MODE_CBC)
295 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200297 size_t copy_len = 0;
298
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299 /*
300 * If there is not enough data for a full block, cache it.
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( ( ctx->operation == MBEDTLS_DECRYPT &&
303 ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) ||
304 ( ctx->operation == MBEDTLS_ENCRYPT &&
305 ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306 {
307 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
308 ilen );
309
310 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200311 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312 }
313
314 /*
315 * Process cached data first
316 */
317 if( ctx->unprocessed_len != 0 )
318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
321 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
322 copy_len );
323
Paul Bakkerff61a782011-06-09 15:42:02 +0000324 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000326 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200328 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329 }
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 *olen += mbedtls_cipher_get_block_size( ctx );
332 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333 ctx->unprocessed_len = 0;
334
335 input += copy_len;
336 ilen -= copy_len;
337 }
338
339 /*
340 * Cache final, incomplete block
341 */
342 if( 0 != ilen )
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
345 if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
346 copy_len = mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
348 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
349 copy_len );
350
351 ctx->unprocessed_len += copy_len;
352 ilen -= copy_len;
353 }
354
355 /*
356 * Process remaining full blocks
357 */
358 if( ilen )
359 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000360 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
361 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200363 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000364 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200365
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 *olen += ilen;
367 }
368
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_CIPHER_MODE_CFB)
374 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000375 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000376 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000377 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000378 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000379 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200380 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000381 }
382
383 *olen = ilen;
384
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200385 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#if defined(MBEDTLS_CIPHER_MODE_CTR)
390 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000391 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000392 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000393 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000394 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000395 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200396 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000397 }
398
399 *olen = ilen;
400
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200401 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000402 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_CIPHER_MODE_STREAM)
406 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200407 {
408 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
409 ilen, input, output ) ) )
410 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200412 }
413
414 *olen = ilen;
415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421}
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
424#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200425/*
426 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
427 */
Paul Bakker23986e52011-04-24 08:57:21 +0000428static void add_pkcs_padding( unsigned char *output, size_t output_len,
429 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430{
Paul Bakker23986e52011-04-24 08:57:21 +0000431 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100432 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433
434 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000435 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436}
437
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200438static int get_pkcs_padding( unsigned char *input, size_t input_len,
439 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100441 size_t i, pad_idx;
442 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443
Paul Bakkera885d682011-01-20 16:35:05 +0000444 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446
447 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448 *data_len = input_len - padding_len;
449
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100450 /* Avoid logical || since it results in a branch */
451 bad |= padding_len > input_len;
452 bad |= padding_len == 0;
453
454 /* The number of bytes checked must be independent of padding_len,
455 * so pick input_len, which is usually 8 or 16 (one block) */
456 pad_idx = input_len - padding_len;
457 for( i = 0; i < input_len; i++ )
458 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200465/*
466 * One and zeros padding: fill with 80 00 ... 00
467 */
468static void add_one_and_zeros_padding( unsigned char *output,
469 size_t output_len, size_t data_len )
470{
471 size_t padding_len = output_len - data_len;
472 unsigned char i = 0;
473
474 output[data_len] = 0x80;
475 for( i = 1; i < padding_len; i++ )
476 output[data_len + i] = 0x00;
477}
478
479static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
480 size_t *data_len )
481{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100482 size_t i;
483 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200484
485 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200487
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100488 bad = 0xFF;
489 *data_len = 0;
490 for( i = input_len; i > 0; i-- )
491 {
492 prev_done = done;
493 done |= ( input[i-1] != 0 );
494 *data_len |= ( i - 1 ) * ( done != prev_done );
495 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
496 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200499
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200504/*
505 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
506 */
507static void add_zeros_and_len_padding( unsigned char *output,
508 size_t output_len, size_t data_len )
509{
510 size_t padding_len = output_len - data_len;
511 unsigned char i = 0;
512
513 for( i = 1; i < padding_len; i++ )
514 output[data_len + i - 1] = 0x00;
515 output[output_len - 1] = (unsigned char) padding_len;
516}
517
518static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
519 size_t *data_len )
520{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100521 size_t i, pad_idx;
522 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200523
524 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200526
527 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200528 *data_len = input_len - padding_len;
529
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100530 /* Avoid logical || since it results in a branch */
531 bad |= padding_len > input_len;
532 bad |= padding_len == 0;
533
534 /* The number of bytes checked must be independent of padding_len */
535 pad_idx = input_len - padding_len;
536 for( i = 0; i < input_len - 1; i++ )
537 bad |= input[i] * ( i >= pad_idx );
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200544/*
545 * Zero padding: fill with 00 ... 00
546 */
547static void add_zeros_padding( unsigned char *output,
548 size_t output_len, size_t data_len )
549{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200550 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200551
552 for( i = data_len; i < output_len; i++ )
553 output[i] = 0x00;
554}
555
556static int get_zeros_padding( unsigned char *input, size_t input_len,
557 size_t *data_len )
558{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100559 size_t i;
560 unsigned char done = 0, prev_done;
561
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200562 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200564
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100565 *data_len = 0;
566 for( i = input_len; i > 0; i-- )
567 {
568 prev_done = done;
569 done |= ( input[i-1] != 0 );
570 *data_len |= i * ( done != prev_done );
571 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200572
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200573 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200576
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200577/*
578 * No padding: don't pad :)
579 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200581 * but a trivial get_padding function
582 */
583static int get_no_padding( unsigned char *input, size_t input_len,
584 size_t *data_len )
585{
586 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200588
589 *data_len = input_len;
590
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200591 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200596 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597{
598 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600
601 *olen = 0;
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
604 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
605 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
606 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000607 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200608 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000609 }
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200612 {
613 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200616 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200617 }
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_CIPHER_MODE_CBC)
620 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200622 int ret = 0;
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200626 /* check for 'no padding' mode */
627 if( NULL == ctx->add_padding )
628 {
629 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200631
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200633 }
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 ctx->unprocessed_len );
637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000639 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200640 /*
641 * For decrypt operations, expect a full block,
642 * or an empty block if no padding
643 */
644 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648 }
649
650 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000651 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000653 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200655 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656 }
657
658 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( MBEDTLS_DECRYPT == ctx->operation )
660 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200661 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
663 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200665 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200667#else
668 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672}
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
675int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200676{
677 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200681 }
682
Paul Bakker1a45d912013-08-14 12:04:26 +0200683 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
686 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200687 ctx->add_padding = add_pkcs_padding;
688 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200689 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
692 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200693 ctx->add_padding = add_one_and_zeros_padding;
694 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
698 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200699 ctx->add_padding = add_zeros_and_len_padding;
700 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
704 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200705 ctx->add_padding = add_zeros_padding;
706 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200707 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200710 ctx->add_padding = NULL;
711 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200712 break;
713
714 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200716 }
717
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200718 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_GCM_C)
723int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200724 unsigned char *tag, size_t tag_len )
725{
726 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( MBEDTLS_ENCRYPT != ctx->operation )
730 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
733 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200734
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200736}
Paul Bakker9af723c2014-05-01 13:03:14 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200739 const unsigned char *tag, size_t tag_len )
740{
741 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200742
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200743 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200747 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200750 {
751 unsigned char check_tag[16];
752 size_t i;
753 int diff;
754
755 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200759 check_tag, tag_len ) ) )
760 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200761 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200762 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763
764 /* Check the tag in "constant-time" */
765 for( diff = 0, i = 0; i < tag_len; i++ )
766 diff |= tag[i] ^ check_tag[i];
767
768 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200770
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200771 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200772 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200773
774 return( 0 );
775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200778/*
779 * Packet-oriented wrapper for non-AEAD modes
780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200782 const unsigned char *iv, size_t iv_len,
783 const unsigned char *input, size_t ilen,
784 unsigned char *output, size_t *olen )
785{
786 int ret;
787 size_t finish_olen;
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200790 return( ret );
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200793 return( ret );
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200796 return( ret );
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200799 return( ret );
800
801 *olen += finish_olen;
802
803 return( 0 );
804}
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200807/*
808 * Packet-oriented encryption for AEAD modes
809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200811 const unsigned char *iv, size_t iv_len,
812 const unsigned char *ad, size_t ad_len,
813 const unsigned char *input, size_t ilen,
814 unsigned char *output, size_t *olen,
815 unsigned char *tag, size_t tag_len )
816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_GCM_C)
818 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200819 {
820 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200822 iv, iv_len, ad, ad_len, input, output,
823 tag_len, tag ) );
824 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_GCM_C */
826#if defined(MBEDTLS_CCM_C)
827 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200828 {
829 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200831 iv, iv_len, ad, ad_len, input, output,
832 tag, tag_len ) );
833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200837}
838
839/*
840 * Packet-oriented decryption for AEAD modes
841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200843 const unsigned char *iv, size_t iv_len,
844 const unsigned char *ad, size_t ad_len,
845 const unsigned char *input, size_t ilen,
846 unsigned char *output, size_t *olen,
847 const unsigned char *tag, size_t tag_len )
848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_GCM_C)
850 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200851 {
852 int ret;
853
854 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200856 iv, iv_len, ad, ad_len,
857 tag, tag_len, input, output );
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
860 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200861
862 return( ret );
863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_GCM_C */
865#if defined(MBEDTLS_CCM_C)
866 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200867 {
868 int ret;
869
870 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200872 iv, iv_len, ad, ad_len,
873 input, output, tag, tag_len );
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
876 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200877
878 return( ret );
879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#endif /* MBEDTLS_CIPHER_C */