blob: 8c7452b1fd7c825c5ae449a936be16e23c5c48b5 [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é-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
Paul Bakker8123e9d2011-01-06 15:37:30 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000028#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000032
33#if defined(POLARSSL_CIPHER_C)
34
35#include "polarssl/cipher.h"
36#include "polarssl/cipher_wrap.h"
37
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020041#if defined(POLARSSL_GCM_C)
42#include "polarssl/gcm.h"
43#endif
44
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020045#if defined(POLARSSL_CCM_C)
46#include "polarssl/ccm.h"
47#endif
48
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020049#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020050#define POLARSSL_CIPHER_MODE_STREAM
51#endif
52
Paul Bakker6edcd412013-10-29 15:22:54 +010053#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
54 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000055#define strcasecmp _stricmp
56#endif
57
Paul Bakker34617722014-06-13 17:20:13 +020058/* Implementation that should never be optimized out by the compiler */
59static void polarssl_zeroize( void *v, size_t n ) {
60 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
61}
62
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020063static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000064
65const int *cipher_list( void )
66{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067 const cipher_definition_t *def;
68 int *type;
69
70 if( ! supported_init )
71 {
72 def = cipher_definitions;
73 type = supported_ciphers;
74
75 while( def->type != 0 )
76 *type++ = (*def++).type;
77
78 *type = 0;
79
80 supported_init = 1;
81 }
82
Paul Bakkerd8bb8262014-06-17 14:06:49 +020083 return( supported_ciphers );
Paul Bakker72f62662011-01-16 21:27:44 +000084}
85
Paul Bakkerec1b9842012-01-14 18:24:43 +000086const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000087{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020089
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020090 for( def = cipher_definitions; def->info != NULL; def++ )
91 if( def->type == cipher_type )
92 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000093
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000095}
96
97const cipher_info_t *cipher_info_from_string( const char *cipher_name )
98{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020099 const cipher_definition_t *def;
100
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104 for( def = cipher_definitions; def->info != NULL; def++ )
105 if( ! strcasecmp( def->info->name, cipher_name ) )
106 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000107
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200108 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Paul Bakkerf46b6952013-09-09 00:08:26 +0200111const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
112 int key_length,
113 const cipher_mode_t mode )
114{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200116
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117 for( def = cipher_definitions; def->info != NULL; def++ )
118 if( def->info->base->cipher == cipher_id &&
119 def->info->key_length == (unsigned) key_length &&
120 def->info->mode == mode )
121 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200122
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124}
125
Paul Bakker84bbeb52014-07-01 14:53:22 +0200126void cipher_init( cipher_context_t *ctx )
127{
128 memset( ctx, 0, sizeof( cipher_context_t ) );
129}
130
131void cipher_free( cipher_context_t *ctx )
132{
133 if( ctx == NULL )
134 return;
135
136 if( ctx->cipher_ctx )
137 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
138
139 polarssl_zeroize( ctx, sizeof(cipher_context_t) );
140}
141
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
143{
144 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
Paul Bakker279432a2012-04-26 10:09:35 +0000147 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Paul Bakker343a8702011-06-09 14:27:58 +0000149 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200150 return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151
152 ctx->cipher_info = cipher_info;
153
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200154#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200155 /*
156 * Ignore possible errors caused by a cipher mode that doesn't use padding
157 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200158#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200159 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200160#else
161 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
162#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200163#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200164
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200165 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166}
167
Paul Bakker84bbeb52014-07-01 14:53:22 +0200168/* Deprecated, redirects to cipher_free() */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169int cipher_free_ctx( cipher_context_t *ctx )
170{
Paul Bakker84bbeb52014-07-01 14:53:22 +0200171 cipher_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000172
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200173 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174}
175
176int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
177 int key_length, const operation_t operation )
178{
179 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200180 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200182 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
183 (int) ctx->cipher_info->key_length != key_length )
184 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200185 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200186 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200187
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188 ctx->key_length = key_length;
189 ctx->operation = operation;
190
Paul Bakker343a8702011-06-09 14:27:58 +0000191 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000192 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000193 */
194 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000195 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000196 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
197 {
198 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000200 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Paul Bakker343a8702011-06-09 14:27:58 +0000202 if( POLARSSL_DECRYPT == operation )
203 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 ctx->key_length );
205
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200206 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207}
208
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200209int cipher_set_iv( cipher_context_t *ctx,
210 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200212 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200213
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200215 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200217 /* avoid buffer overflow in ctx->iv */
218 if( iv_len > POLARSSL_MAX_IV_LENGTH )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200220
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200221 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200222 actual_iv_size = iv_len;
223 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200224 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200225 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200226
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200227 /* avoid reading past the end of input buffer */
228 if( actual_iv_size > iv_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200230 }
231
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200232 memcpy( ctx->iv, iv, actual_iv_size );
233 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200234
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200235 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200236}
237
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200238int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200239{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200240 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200241 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200242
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243 ctx->unprocessed_len = 0;
244
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200245 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200246}
247
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200248#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200249int cipher_update_ad( cipher_context_t *ctx,
250 const unsigned char *ad, size_t ad_len )
251{
252 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200253 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200254
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200255 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
256 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200258 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200259 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200260
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200263#endif /* POLARSSL_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200265int cipher_update( cipher_context_t *ctx, const unsigned char *input,
266 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267{
Paul Bakkerff61a782011-06-09 15:42:02 +0000268 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
Paul Bakker68884e32013-01-07 18:20:04 +0100270 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000271 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200272 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000273 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274
Paul Bakker6c212762013-12-16 15:24:50 +0100275 *olen = 0;
276
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
278 {
279 if( ilen != cipher_get_block_size( ctx ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200280 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200281
282 *olen = ilen;
283
284 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
285 ctx->operation, input, output ) ) )
286 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200287 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200288 }
289
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200290 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200291 }
292
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200293#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200294 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200295 {
296 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200297 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
298 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200299 }
300#endif
301
Paul Bakker68884e32013-01-07 18:20:04 +0100302 if( input == output &&
303 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
304 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200305 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100306 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200308#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200309 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200311 size_t copy_len = 0;
312
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313 /*
314 * If there is not enough data for a full block, cache it.
315 */
316 if( ( ctx->operation == POLARSSL_DECRYPT &&
317 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
318 ( ctx->operation == POLARSSL_ENCRYPT &&
319 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
320 {
321 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
322 ilen );
323
324 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200325 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326 }
327
328 /*
329 * Process cached data first
330 */
331 if( ctx->unprocessed_len != 0 )
332 {
333 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
334
335 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
336 copy_len );
337
Paul Bakkerff61a782011-06-09 15:42:02 +0000338 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000340 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200342 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343 }
344
345 *olen += cipher_get_block_size( ctx );
346 output += cipher_get_block_size( ctx );
347 ctx->unprocessed_len = 0;
348
349 input += copy_len;
350 ilen -= copy_len;
351 }
352
353 /*
354 * Cache final, incomplete block
355 */
356 if( 0 != ilen )
357 {
358 copy_len = ilen % cipher_get_block_size( ctx );
359 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
Paul Bakker66d5d072014-06-17 16:39:18 +0200360 copy_len = cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361
362 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
363 copy_len );
364
365 ctx->unprocessed_len += copy_len;
366 ilen -= copy_len;
367 }
368
369 /*
370 * Process remaining full blocks
371 */
372 if( ilen )
373 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000374 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
375 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200377 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200379
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 *olen += ilen;
381 }
382
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200385#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
Paul Bakker68884e32013-01-07 18:20:04 +0100387#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000388 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000389 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000390 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000391 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000392 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000393 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200394 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000395 }
396
397 *olen = ilen;
398
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200399 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000400 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200401#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000402
Paul Bakker68884e32013-01-07 18:20:04 +0100403#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000404 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
405 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000406 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000407 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000408 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000409 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200410 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000411 }
412
413 *olen = ilen;
414
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200415 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000416 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200417#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000418
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200419#if defined(POLARSSL_CIPHER_MODE_STREAM)
420 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
421 {
422 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
423 ilen, input, output ) ) )
424 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200425 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200426 }
427
428 *olen = ilen;
429
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200430 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200431 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200432#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200433
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200434 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435}
436
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200437#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200438#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200439/*
440 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
441 */
Paul Bakker23986e52011-04-24 08:57:21 +0000442static void add_pkcs_padding( unsigned char *output, size_t output_len,
443 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444{
Paul Bakker23986e52011-04-24 08:57:21 +0000445 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100446 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447
448 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000449 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450}
451
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200452static int get_pkcs_padding( unsigned char *input, size_t input_len,
453 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100455 size_t i, pad_idx;
456 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457
Paul Bakkera885d682011-01-20 16:35:05 +0000458 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200459 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000460
461 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462 *data_len = input_len - padding_len;
463
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100464 /* Avoid logical || since it results in a branch */
465 bad |= padding_len > input_len;
466 bad |= padding_len == 0;
467
468 /* The number of bytes checked must be independent of padding_len,
469 * so pick input_len, which is usually 8 or 16 (one block) */
470 pad_idx = input_len - padding_len;
471 for( i = 0; i < input_len; i++ )
472 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
473
Paul Bakker66d5d072014-06-17 16:39:18 +0200474 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475}
Paul Bakker48e93c82013-08-14 12:21:18 +0200476#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477
Paul Bakker48e93c82013-08-14 12:21:18 +0200478#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200479/*
480 * One and zeros padding: fill with 80 00 ... 00
481 */
482static void add_one_and_zeros_padding( unsigned char *output,
483 size_t output_len, size_t data_len )
484{
485 size_t padding_len = output_len - data_len;
486 unsigned char i = 0;
487
488 output[data_len] = 0x80;
489 for( i = 1; i < padding_len; i++ )
490 output[data_len + i] = 0x00;
491}
492
493static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
494 size_t *data_len )
495{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100496 size_t i;
497 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200498
499 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200500 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200501
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100502 bad = 0xFF;
503 *data_len = 0;
504 for( i = input_len; i > 0; i-- )
505 {
506 prev_done = done;
507 done |= ( input[i-1] != 0 );
508 *data_len |= ( i - 1 ) * ( done != prev_done );
509 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
510 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200511
Paul Bakker66d5d072014-06-17 16:39:18 +0200512 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200513
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200514}
Paul Bakker48e93c82013-08-14 12:21:18 +0200515#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200516
Paul Bakker48e93c82013-08-14 12:21:18 +0200517#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200518/*
519 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
520 */
521static void add_zeros_and_len_padding( unsigned char *output,
522 size_t output_len, size_t data_len )
523{
524 size_t padding_len = output_len - data_len;
525 unsigned char i = 0;
526
527 for( i = 1; i < padding_len; i++ )
528 output[data_len + i - 1] = 0x00;
529 output[output_len - 1] = (unsigned char) padding_len;
530}
531
532static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
533 size_t *data_len )
534{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100535 size_t i, pad_idx;
536 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200537
538 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200539 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200540
541 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200542 *data_len = input_len - padding_len;
543
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100544 /* Avoid logical || since it results in a branch */
545 bad |= padding_len > input_len;
546 bad |= padding_len == 0;
547
548 /* The number of bytes checked must be independent of padding_len */
549 pad_idx = input_len - padding_len;
550 for( i = 0; i < input_len - 1; i++ )
551 bad |= input[i] * ( i >= pad_idx );
552
Paul Bakker66d5d072014-06-17 16:39:18 +0200553 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200554}
Paul Bakker48e93c82013-08-14 12:21:18 +0200555#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200556
Paul Bakker48e93c82013-08-14 12:21:18 +0200557#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200558/*
559 * Zero padding: fill with 00 ... 00
560 */
561static void add_zeros_padding( unsigned char *output,
562 size_t output_len, size_t data_len )
563{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200564 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200565
566 for( i = data_len; i < output_len; i++ )
567 output[i] = 0x00;
568}
569
570static int get_zeros_padding( unsigned char *input, size_t input_len,
571 size_t *data_len )
572{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100573 size_t i;
574 unsigned char done = 0, prev_done;
575
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200576 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200577 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200578
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100579 *data_len = 0;
580 for( i = input_len; i > 0; i-- )
581 {
582 prev_done = done;
583 done |= ( input[i-1] != 0 );
584 *data_len |= i * ( done != prev_done );
585 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200586
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200587 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200588}
Paul Bakker48e93c82013-08-14 12:21:18 +0200589#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200590
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200591/*
592 * No padding: don't pad :)
593 *
594 * There is no add_padding function (check for NULL in cipher_finish)
595 * but a trivial get_padding function
596 */
597static int get_no_padding( unsigned char *input, size_t input_len,
598 size_t *data_len )
599{
600 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200601 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200602
603 *data_len = input_len;
604
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200605 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200606}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200607#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200608
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200609int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200610 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611{
612 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200613 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000614
615 *olen = 0;
616
Paul Bakker6132d0a2012-07-04 17:10:40 +0000617 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000618 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200619 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200620 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000621 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200622 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000623 }
624
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
626 {
627 if( ctx->unprocessed_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200628 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200629
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200630 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200631 }
632
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200633#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
635 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200636 int ret = 0;
637
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638 if( POLARSSL_ENCRYPT == ctx->operation )
639 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200640 /* check for 'no padding' mode */
641 if( NULL == ctx->add_padding )
642 {
643 if( 0 != ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200644 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200645
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200647 }
648
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200649 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650 ctx->unprocessed_len );
651 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200652 else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200654 /*
655 * For decrypt operations, expect a full block,
656 * or an empty block if no padding
657 */
658 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200659 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200660
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 }
663
664 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000665 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
666 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
667 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200669 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 }
671
672 /* Set output size for decryption */
673 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200674 return ctx->get_padding( output, cipher_get_block_size( ctx ),
675 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
677 /* Set output size for encryption */
678 *olen = cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200679 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200681#else
682 ((void) output);
683#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686}
687
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200688#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200689int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
690{
691 if( NULL == ctx ||
692 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
693 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200694 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200695 }
696
Paul Bakker1a45d912013-08-14 12:04:26 +0200697 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200698 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200699#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200700 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200701 ctx->add_padding = add_pkcs_padding;
702 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200703 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200704#endif
705#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200706 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200707 ctx->add_padding = add_one_and_zeros_padding;
708 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200709 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200710#endif
711#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200712 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200713 ctx->add_padding = add_zeros_and_len_padding;
714 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200715 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200716#endif
717#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200718 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200719 ctx->add_padding = add_zeros_padding;
720 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200721 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200722#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200723 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200724 ctx->add_padding = NULL;
725 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200726 break;
727
728 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200729 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200730 }
731
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200732 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200733}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200734#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200735
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200736#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200737int cipher_write_tag( cipher_context_t *ctx,
738 unsigned char *tag, size_t tag_len )
739{
740 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200741 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200742
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200743 if( POLARSSL_ENCRYPT != ctx->operation )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200744 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200745
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200746 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200747 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200748
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200749 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200750}
Paul Bakker9af723c2014-05-01 13:03:14 +0200751
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200752int cipher_check_tag( cipher_context_t *ctx,
753 const unsigned char *tag, size_t tag_len )
754{
755 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200756
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200757 if( NULL == ctx || NULL == ctx->cipher_info ||
758 POLARSSL_DECRYPT != ctx->operation )
759 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200760 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200761 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200762
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
764 {
765 unsigned char check_tag[16];
766 size_t i;
767 int diff;
768
769 if( tag_len > sizeof( check_tag ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200770 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200771
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200772 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
773 check_tag, tag_len ) ) )
774 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200775 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200776 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200777
778 /* Check the tag in "constant-time" */
779 for( diff = 0, i = 0; i < tag_len; i++ )
780 diff |= tag[i] ^ check_tag[i];
781
782 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200783 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200784
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200785 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200786 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200787
788 return( 0 );
789}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200790#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200791
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200792/*
793 * Packet-oriented wrapper for non-AEAD modes
794 */
795int cipher_crypt( cipher_context_t *ctx,
796 const unsigned char *iv, size_t iv_len,
797 const unsigned char *input, size_t ilen,
798 unsigned char *output, size_t *olen )
799{
800 int ret;
801 size_t finish_olen;
802
803 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
804 return( ret );
805
806 if( ( ret = cipher_reset( ctx ) ) != 0 )
807 return( ret );
808
809 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
810 return( ret );
811
812 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
813 return( ret );
814
815 *olen += finish_olen;
816
817 return( 0 );
818}
819
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200820#if defined(POLARSSL_CIPHER_MODE_AEAD)
821/*
822 * Packet-oriented encryption for AEAD modes
823 */
824int cipher_auth_encrypt( cipher_context_t *ctx,
825 const unsigned char *iv, size_t iv_len,
826 const unsigned char *ad, size_t ad_len,
827 const unsigned char *input, size_t ilen,
828 unsigned char *output, size_t *olen,
829 unsigned char *tag, size_t tag_len )
830{
831#if defined(POLARSSL_GCM_C)
832 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
833 {
834 *olen = ilen;
835 return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
836 iv, iv_len, ad, ad_len, input, output,
837 tag_len, tag ) );
838 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200839#endif /* POLARSSL_GCM_C */
840#if defined(POLARSSL_CCM_C)
841 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
842 {
843 *olen = ilen;
844 return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
845 iv, iv_len, ad, ad_len, input, output,
846 tag, tag_len ) );
847 }
848#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200849
850 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
851}
852
853/*
854 * Packet-oriented decryption for AEAD modes
855 */
856int cipher_auth_decrypt( cipher_context_t *ctx,
857 const unsigned char *iv, size_t iv_len,
858 const unsigned char *ad, size_t ad_len,
859 const unsigned char *input, size_t ilen,
860 unsigned char *output, size_t *olen,
861 const unsigned char *tag, size_t tag_len )
862{
863#if defined(POLARSSL_GCM_C)
864 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
865 {
866 int ret;
867
868 *olen = ilen;
869 ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
870 iv, iv_len, ad, ad_len,
871 tag, tag_len, input, output );
872
873 if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
874 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
875
876 return( ret );
877 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200878#endif /* POLARSSL_GCM_C */
879#if defined(POLARSSL_CCM_C)
880 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
881 {
882 int ret;
883
884 *olen = ilen;
885 ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
886 iv, iv_len, ad, ad_len,
887 input, output, tag, tag_len );
888
889 if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
890 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
891
892 return( ret );
893 }
894#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200895
896 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
897}
898#endif /* POLARSSL_CIPHER_MODE_AEAD */
899
900
Paul Bakker8123e9d2011-01-06 15:37:30 +0000901#if defined(POLARSSL_SELF_TEST)
902
Paul Bakker8123e9d2011-01-06 15:37:30 +0000903/*
904 * Checkup routine
905 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000906int cipher_self_test( int verbose )
907{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000908 ((void) verbose);
909
Paul Bakker8123e9d2011-01-06 15:37:30 +0000910 return( 0 );
911}
912
Paul Bakker9af723c2014-05-01 13:03:14 +0200913#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000914
Paul Bakker9af723c2014-05-01 13:03:14 +0200915#endif /* POLARSSL_CIPHER_C */