blob: 16acd805ea467b59959d0fe45407c29a7cfb189c [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Paul Bakker8123e9d2011-01-06 15:37:30 +00004 * \brief Generic cipher wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000035
36#if defined(POLARSSL_CIPHER_C)
37
38#include "polarssl/cipher.h"
39#include "polarssl/cipher_wrap.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
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include <stdlib.h>
50
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020051#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020052#define POLARSSL_CIPHER_MODE_STREAM
53#endif
54
Paul Bakker6edcd412013-10-29 15:22:54 +010055#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
56 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000057#define strcasecmp _stricmp
58#endif
59
Paul Bakker34617722014-06-13 17:20:13 +020060/* Implementation that should never be optimized out by the compiler */
61static void polarssl_zeroize( void *v, size_t n ) {
62 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
63}
64
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020065static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000066
67const int *cipher_list( void )
68{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020069 const cipher_definition_t *def;
70 int *type;
71
72 if( ! supported_init )
73 {
74 def = cipher_definitions;
75 type = supported_ciphers;
76
77 while( def->type != 0 )
78 *type++ = (*def++).type;
79
80 *type = 0;
81
82 supported_init = 1;
83 }
84
Paul Bakkerd8bb8262014-06-17 14:06:49 +020085 return( supported_ciphers );
Paul Bakker72f62662011-01-16 21:27:44 +000086}
87
Paul Bakkerec1b9842012-01-14 18:24:43 +000088const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000089{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020090 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020091
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020092 for( def = cipher_definitions; def->info != NULL; def++ )
93 if( def->type == cipher_type )
94 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000095
Paul Bakkerd8bb8262014-06-17 14:06:49 +020096 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000097}
98
99const cipher_info_t *cipher_info_from_string( const char *cipher_name )
100{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101 const cipher_definition_t *def;
102
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200104 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200106 for( def = cipher_definitions; def->info != NULL; def++ )
107 if( ! strcasecmp( def->info->name, cipher_name ) )
108 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000109
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200110 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111}
112
Paul Bakkerf46b6952013-09-09 00:08:26 +0200113const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
114 int key_length,
115 const cipher_mode_t mode )
116{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200118
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200119 for( def = cipher_definitions; def->info != NULL; def++ )
120 if( def->info->base->cipher == cipher_id &&
121 def->info->key_length == (unsigned) key_length &&
122 def->info->mode == mode )
123 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200125 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200126}
127
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
129{
130 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200131 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132
Paul Bakker279432a2012-04-26 10:09:35 +0000133 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134
Paul Bakker343a8702011-06-09 14:27:58 +0000135 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137
138 ctx->cipher_info = cipher_info;
139
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200140#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200141 /*
142 * Ignore possible errors caused by a cipher mode that doesn't use padding
143 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200144#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200145 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200146#else
147 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
148#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200149#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200150
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200151 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152}
153
154int cipher_free_ctx( cipher_context_t *ctx )
155{
156 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200157 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
Paul Bakker343a8702011-06-09 14:27:58 +0000159 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker34617722014-06-13 17:20:13 +0200160 polarssl_zeroize( ctx, sizeof(cipher_context_t) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200162 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163}
164
165int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
166 int key_length, const operation_t operation )
167{
168 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200169 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200171 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
172 (int) ctx->cipher_info->key_length != key_length )
173 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200174 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200175 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200176
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177 ctx->key_length = key_length;
178 ctx->operation = operation;
179
Paul Bakker343a8702011-06-09 14:27:58 +0000180 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000181 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000182 */
183 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000184 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000185 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
186 {
187 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000189 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190
Paul Bakker343a8702011-06-09 14:27:58 +0000191 if( POLARSSL_DECRYPT == operation )
192 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193 ctx->key_length );
194
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200195 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196}
197
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200198int cipher_set_iv( cipher_context_t *ctx,
199 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200201 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200202
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200204 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200206 /* avoid buffer overflow in ctx->iv */
207 if( iv_len > POLARSSL_MAX_IV_LENGTH )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200209
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200210 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200211 actual_iv_size = iv_len;
212 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200213 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200214 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200215
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200216 /* avoid reading past the end of input buffer */
217 if( actual_iv_size > iv_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200218 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200219 }
220
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200221 memcpy( ctx->iv, iv, actual_iv_size );
222 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200223
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200224 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200225}
226
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200227int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200228{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200229 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200231
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232 ctx->unprocessed_len = 0;
233
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200234 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200235}
236
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200237#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200238int cipher_update_ad( cipher_context_t *ctx,
239 const unsigned char *ad, size_t ad_len )
240{
241 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200243
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200244 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
245 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200247 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200248 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200249
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200250 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200252#endif /* POLARSSL_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200254int cipher_update( cipher_context_t *ctx, const unsigned char *input,
255 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256{
Paul Bakkerff61a782011-06-09 15:42:02 +0000257 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258
Paul Bakker68884e32013-01-07 18:20:04 +0100259 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000260 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Paul Bakker6c212762013-12-16 15:24:50 +0100264 *olen = 0;
265
Paul Bakker5e0efa72013-09-08 23:04:04 +0200266 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
267 {
268 if( ilen != cipher_get_block_size( ctx ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200269 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200270
271 *olen = ilen;
272
273 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
274 ctx->operation, input, output ) ) )
275 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200276 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 }
278
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200280 }
281
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200282#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200283 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200284 {
285 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
287 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200288 }
289#endif
290
Paul Bakker68884e32013-01-07 18:20:04 +0100291 if( input == output &&
292 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
293 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200294 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100295 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200297#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200298 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200300 size_t copy_len = 0;
301
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 /*
303 * If there is not enough data for a full block, cache it.
304 */
305 if( ( ctx->operation == POLARSSL_DECRYPT &&
306 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
307 ( ctx->operation == POLARSSL_ENCRYPT &&
308 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
309 {
310 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
311 ilen );
312
313 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200314 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315 }
316
317 /*
318 * Process cached data first
319 */
320 if( ctx->unprocessed_len != 0 )
321 {
322 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
323
324 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
325 copy_len );
326
Paul Bakkerff61a782011-06-09 15:42:02 +0000327 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000329 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200331 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332 }
333
334 *olen += cipher_get_block_size( ctx );
335 output += cipher_get_block_size( ctx );
336 ctx->unprocessed_len = 0;
337
338 input += copy_len;
339 ilen -= copy_len;
340 }
341
342 /*
343 * Cache final, incomplete block
344 */
345 if( 0 != ilen )
346 {
347 copy_len = ilen % cipher_get_block_size( ctx );
348 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
Paul Bakker66d5d072014-06-17 16:39:18 +0200349 copy_len = cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350
351 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
352 copy_len );
353
354 ctx->unprocessed_len += copy_len;
355 ilen -= copy_len;
356 }
357
358 /*
359 * Process remaining full blocks
360 */
361 if( ilen )
362 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000363 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
364 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000365 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200366 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200368
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369 *olen += ilen;
370 }
371
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200374#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375
Paul Bakker68884e32013-01-07 18:20:04 +0100376#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000377 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000378 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000379 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000380 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000381 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000382 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000384 }
385
386 *olen = ilen;
387
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200388 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000389 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200390#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000391
Paul Bakker68884e32013-01-07 18:20:04 +0100392#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000393 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
394 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000395 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000396 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000397 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000398 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200399 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000400 }
401
402 *olen = ilen;
403
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200404 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000405 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200406#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000407
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200408#if defined(POLARSSL_CIPHER_MODE_STREAM)
409 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
410 {
411 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
412 ilen, input, output ) ) )
413 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200414 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200415 }
416
417 *olen = ilen;
418
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200419 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200420 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200421#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200422
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200423 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424}
425
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200426#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200427#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200428/*
429 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
430 */
Paul Bakker23986e52011-04-24 08:57:21 +0000431static void add_pkcs_padding( unsigned char *output, size_t output_len,
432 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433{
Paul Bakker23986e52011-04-24 08:57:21 +0000434 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100435 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436
437 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000438 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439}
440
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200441static int get_pkcs_padding( unsigned char *input, size_t input_len,
442 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100444 size_t i, pad_idx;
445 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446
Paul Bakkera885d682011-01-20 16:35:05 +0000447 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200448 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449
450 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 *data_len = input_len - padding_len;
452
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100453 /* Avoid logical || since it results in a branch */
454 bad |= padding_len > input_len;
455 bad |= padding_len == 0;
456
457 /* The number of bytes checked must be independent of padding_len,
458 * so pick input_len, which is usually 8 or 16 (one block) */
459 pad_idx = input_len - padding_len;
460 for( i = 0; i < input_len; i++ )
461 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
462
Paul Bakker66d5d072014-06-17 16:39:18 +0200463 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464}
Paul Bakker48e93c82013-08-14 12:21:18 +0200465#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466
Paul Bakker48e93c82013-08-14 12:21:18 +0200467#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200468/*
469 * One and zeros padding: fill with 80 00 ... 00
470 */
471static void add_one_and_zeros_padding( unsigned char *output,
472 size_t output_len, size_t data_len )
473{
474 size_t padding_len = output_len - data_len;
475 unsigned char i = 0;
476
477 output[data_len] = 0x80;
478 for( i = 1; i < padding_len; i++ )
479 output[data_len + i] = 0x00;
480}
481
482static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
483 size_t *data_len )
484{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100485 size_t i;
486 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200487
488 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200489 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200490
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100491 bad = 0xFF;
492 *data_len = 0;
493 for( i = input_len; i > 0; i-- )
494 {
495 prev_done = done;
496 done |= ( input[i-1] != 0 );
497 *data_len |= ( i - 1 ) * ( done != prev_done );
498 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
499 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200502
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503}
Paul Bakker48e93c82013-08-14 12:21:18 +0200504#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200505
Paul Bakker48e93c82013-08-14 12:21:18 +0200506#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200507/*
508 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
509 */
510static void add_zeros_and_len_padding( unsigned char *output,
511 size_t output_len, size_t data_len )
512{
513 size_t padding_len = output_len - data_len;
514 unsigned char i = 0;
515
516 for( i = 1; i < padding_len; i++ )
517 output[data_len + i - 1] = 0x00;
518 output[output_len - 1] = (unsigned char) padding_len;
519}
520
521static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
522 size_t *data_len )
523{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100524 size_t i, pad_idx;
525 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200526
527 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200528 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200529
530 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200531 *data_len = input_len - padding_len;
532
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100533 /* Avoid logical || since it results in a branch */
534 bad |= padding_len > input_len;
535 bad |= padding_len == 0;
536
537 /* The number of bytes checked must be independent of padding_len */
538 pad_idx = input_len - padding_len;
539 for( i = 0; i < input_len - 1; i++ )
540 bad |= input[i] * ( i >= pad_idx );
541
Paul Bakker66d5d072014-06-17 16:39:18 +0200542 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200543}
Paul Bakker48e93c82013-08-14 12:21:18 +0200544#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200545
Paul Bakker48e93c82013-08-14 12:21:18 +0200546#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200547/*
548 * Zero padding: fill with 00 ... 00
549 */
550static void add_zeros_padding( unsigned char *output,
551 size_t output_len, size_t data_len )
552{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200553 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200554
555 for( i = data_len; i < output_len; i++ )
556 output[i] = 0x00;
557}
558
559static int get_zeros_padding( unsigned char *input, size_t input_len,
560 size_t *data_len )
561{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100562 size_t i;
563 unsigned char done = 0, prev_done;
564
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200565 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200566 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200567
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100568 *data_len = 0;
569 for( i = input_len; i > 0; i-- )
570 {
571 prev_done = done;
572 done |= ( input[i-1] != 0 );
573 *data_len |= i * ( done != prev_done );
574 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200575
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200576 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200577}
Paul Bakker48e93c82013-08-14 12:21:18 +0200578#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200579
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200580/*
581 * No padding: don't pad :)
582 *
583 * There is no add_padding function (check for NULL in cipher_finish)
584 * but a trivial get_padding function
585 */
586static int get_no_padding( unsigned char *input, size_t input_len,
587 size_t *data_len )
588{
589 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200591
592 *data_len = input_len;
593
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200594 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200595}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200596#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200597
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200598int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200599 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600{
601 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
604 *olen = 0;
605
Paul Bakker6132d0a2012-07-04 17:10:40 +0000606 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000607 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200608 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200609 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000610 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200611 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000612 }
613
Paul Bakker5e0efa72013-09-08 23:04:04 +0200614 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
615 {
616 if( ctx->unprocessed_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200617 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200618
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200619 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200620 }
621
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200622#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
624 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200625 int ret = 0;
626
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627 if( POLARSSL_ENCRYPT == ctx->operation )
628 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200629 /* check for 'no padding' mode */
630 if( NULL == ctx->add_padding )
631 {
632 if( 0 != ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200634
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200635 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200636 }
637
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200638 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000639 ctx->unprocessed_len );
640 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200641 else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200643 /*
644 * For decrypt operations, expect a full block,
645 * or an empty block if no padding
646 */
647 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200649
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 }
652
653 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000654 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
655 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
656 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200658 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659 }
660
661 /* Set output size for decryption */
662 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200663 return ctx->get_padding( output, cipher_get_block_size( ctx ),
664 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000665
666 /* Set output size for encryption */
667 *olen = cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200668 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200670#else
671 ((void) output);
672#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200674 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675}
676
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200677#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200678int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
679{
680 if( NULL == ctx ||
681 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
682 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200683 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200684 }
685
Paul Bakker1a45d912013-08-14 12:04:26 +0200686 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200687 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200688#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200689 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200690 ctx->add_padding = add_pkcs_padding;
691 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200692 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200693#endif
694#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200696 ctx->add_padding = add_one_and_zeros_padding;
697 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200698 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200699#endif
700#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200702 ctx->add_padding = add_zeros_and_len_padding;
703 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200704 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200705#endif
706#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200707 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200708 ctx->add_padding = add_zeros_padding;
709 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200710 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200711#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200712 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200713 ctx->add_padding = NULL;
714 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200715 break;
716
717 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200718 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200719 }
720
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200721 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200722}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200723#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200724
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200725#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200726int cipher_write_tag( cipher_context_t *ctx,
727 unsigned char *tag, size_t tag_len )
728{
729 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200730 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200731
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200732 if( POLARSSL_ENCRYPT != ctx->operation )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200734
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200735 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200737
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200738 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200739}
Paul Bakker9af723c2014-05-01 13:03:14 +0200740
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200741int cipher_check_tag( cipher_context_t *ctx,
742 const unsigned char *tag, size_t tag_len )
743{
744 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200745
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200746 if( NULL == ctx || NULL == ctx->cipher_info ||
747 POLARSSL_DECRYPT != ctx->operation )
748 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200749 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200750 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200751
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200752 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
753 {
754 unsigned char check_tag[16];
755 size_t i;
756 int diff;
757
758 if( tag_len > sizeof( check_tag ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200759 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200760
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200761 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
762 check_tag, tag_len ) ) )
763 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200764 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200765 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200766
767 /* Check the tag in "constant-time" */
768 for( diff = 0, i = 0; i < tag_len; i++ )
769 diff |= tag[i] ^ check_tag[i];
770
771 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200772 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200773
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200774 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200775 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200776
777 return( 0 );
778}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200779#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200780
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200781/*
782 * Packet-oriented wrapper for non-AEAD modes
783 */
784int cipher_crypt( cipher_context_t *ctx,
785 const unsigned char *iv, size_t iv_len,
786 const unsigned char *input, size_t ilen,
787 unsigned char *output, size_t *olen )
788{
789 int ret;
790 size_t finish_olen;
791
792 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
793 return( ret );
794
795 if( ( ret = cipher_reset( ctx ) ) != 0 )
796 return( ret );
797
798 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
799 return( ret );
800
801 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
802 return( ret );
803
804 *olen += finish_olen;
805
806 return( 0 );
807}
808
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200809#if defined(POLARSSL_CIPHER_MODE_AEAD)
810/*
811 * Packet-oriented encryption for AEAD modes
812 */
813int cipher_auth_encrypt( cipher_context_t *ctx,
814 const unsigned char *iv, size_t iv_len,
815 const unsigned char *ad, size_t ad_len,
816 const unsigned char *input, size_t ilen,
817 unsigned char *output, size_t *olen,
818 unsigned char *tag, size_t tag_len )
819{
820#if defined(POLARSSL_GCM_C)
821 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
822 {
823 *olen = ilen;
824 return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
825 iv, iv_len, ad, ad_len, input, output,
826 tag_len, tag ) );
827 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200828#endif /* POLARSSL_GCM_C */
829#if defined(POLARSSL_CCM_C)
830 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
831 {
832 *olen = ilen;
833 return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
834 iv, iv_len, ad, ad_len, input, output,
835 tag, tag_len ) );
836 }
837#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200838
839 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
840}
841
842/*
843 * Packet-oriented decryption for AEAD modes
844 */
845int cipher_auth_decrypt( cipher_context_t *ctx,
846 const unsigned char *iv, size_t iv_len,
847 const unsigned char *ad, size_t ad_len,
848 const unsigned char *input, size_t ilen,
849 unsigned char *output, size_t *olen,
850 const unsigned char *tag, size_t tag_len )
851{
852#if defined(POLARSSL_GCM_C)
853 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
854 {
855 int ret;
856
857 *olen = ilen;
858 ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
859 iv, iv_len, ad, ad_len,
860 tag, tag_len, input, output );
861
862 if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
863 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
864
865 return( ret );
866 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200867#endif /* POLARSSL_GCM_C */
868#if defined(POLARSSL_CCM_C)
869 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
870 {
871 int ret;
872
873 *olen = ilen;
874 ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
875 iv, iv_len, ad, ad_len,
876 input, output, tag, tag_len );
877
878 if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
879 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
880
881 return( ret );
882 }
883#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200884
885 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
886}
887#endif /* POLARSSL_CIPHER_MODE_AEAD */
888
889
Paul Bakker8123e9d2011-01-06 15:37:30 +0000890#if defined(POLARSSL_SELF_TEST)
891
Paul Bakker8123e9d2011-01-06 15:37:30 +0000892/*
893 * Checkup routine
894 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000895int cipher_self_test( int verbose )
896{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000897 ((void) verbose);
898
Paul Bakker8123e9d2011-01-06 15:37:30 +0000899 return( 0 );
900}
901
Paul Bakker9af723c2014-05-01 13:03:14 +0200902#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000903
Paul Bakker9af723c2014-05-01 13:03:14 +0200904#endif /* POLARSSL_CIPHER_C */