blob: f0a770abaeb4eecac60c7fe146373de84eea9473 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
3 *
4 * \brief Generic cipher wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker68884e32013-01-07 18:20:04 +01008 * Copyright (C) 2006-2013, 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
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_CIPHER_C)
33
34#include "polarssl/cipher.h"
35#include "polarssl/cipher_wrap.h"
36
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020037#if defined(POLARSSL_GCM_C)
38#include "polarssl/gcm.h"
39#endif
40
Paul Bakker8123e9d2011-01-06 15:37:30 +000041#include <stdlib.h>
42
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020043#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020044#define POLARSSL_CIPHER_MODE_STREAM
45#endif
46
Paul Bakker6edcd412013-10-29 15:22:54 +010047#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
48 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000049#define strcasecmp _stricmp
50#endif
51
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020052static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000053
54const int *cipher_list( void )
55{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020056 const cipher_definition_t *def;
57 int *type;
58
59 if( ! supported_init )
60 {
61 def = cipher_definitions;
62 type = supported_ciphers;
63
64 while( def->type != 0 )
65 *type++ = (*def++).type;
66
67 *type = 0;
68
69 supported_init = 1;
70 }
71
Paul Bakker72f62662011-01-16 21:27:44 +000072 return supported_ciphers;
73}
74
Paul Bakkerec1b9842012-01-14 18:24:43 +000075const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000076{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020077 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020078
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 for( def = cipher_definitions; def->info != NULL; def++ )
80 if( def->type == cipher_type )
81 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000082
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000084}
85
86const cipher_info_t *cipher_info_from_string( const char *cipher_name )
87{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088 const cipher_definition_t *def;
89
Paul Bakker8123e9d2011-01-06 15:37:30 +000090 if( NULL == cipher_name )
91 return NULL;
92
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020093 for( def = cipher_definitions; def->info != NULL; def++ )
94 if( ! strcasecmp( def->info->name, cipher_name ) )
95 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +000096
Paul Bakker8123e9d2011-01-06 15:37:30 +000097 return NULL;
98}
99
Paul Bakkerf46b6952013-09-09 00:08:26 +0200100const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
101 int key_length,
102 const cipher_mode_t mode )
103{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200105
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200106 for( def = cipher_definitions; def->info != NULL; def++ )
107 if( def->info->base->cipher == cipher_id &&
108 def->info->key_length == (unsigned) key_length &&
109 def->info->mode == mode )
110 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200111
112 return NULL;
113}
114
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
116{
117 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000118 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Paul Bakker279432a2012-04-26 10:09:35 +0000120 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121
Paul Bakker343a8702011-06-09 14:27:58 +0000122 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000123 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124
125 ctx->cipher_info = cipher_info;
126
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200127#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200128 /*
129 * Ignore possible errors caused by a cipher mode that doesn't use padding
130 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200131#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200132 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200133#else
134 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
135#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200136#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200137
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138 return 0;
139}
140
141int cipher_free_ctx( cipher_context_t *ctx )
142{
143 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000144 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145
Paul Bakker343a8702011-06-09 14:27:58 +0000146 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147
148 return 0;
149}
150
151int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
152 int key_length, const operation_t operation )
153{
154 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000155 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200157 if( (int) ctx->cipher_info->key_length != key_length )
158 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
159
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160 ctx->key_length = key_length;
161 ctx->operation = operation;
162
Paul Bakker343a8702011-06-09 14:27:58 +0000163 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000164 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000165 */
166 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000167 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000168 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
169 {
170 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000172 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000173
Paul Bakker343a8702011-06-09 14:27:58 +0000174 if( POLARSSL_DECRYPT == operation )
175 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 ctx->key_length );
177
Paul Bakkerff61a782011-06-09 15:42:02 +0000178 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179}
180
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200181int cipher_set_iv( cipher_context_t *ctx,
182 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200184 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200185
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000187 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200189 /* avoid buffer overflow in ctx->iv */
190 if( iv_len > POLARSSL_MAX_IV_LENGTH )
191 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
192
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200193 if( ctx->cipher_info->accepts_variable_iv_size )
194 actual_iv_size = iv_len;
195 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200196 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200197 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200198
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200199 /* avoid reading past the end of input buffer */
200 if( actual_iv_size > iv_len )
201 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
202 }
203
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200204 memcpy( ctx->iv, iv, actual_iv_size );
205 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200206
207 return 0;
208}
209
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200210int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200211{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200212 if( NULL == ctx || NULL == ctx->cipher_info )
213 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
214
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 ctx->unprocessed_len = 0;
216
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200217 return 0;
218}
219
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200220#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200221int cipher_update_ad( cipher_context_t *ctx,
222 const unsigned char *ad, size_t ad_len )
223{
224 if( NULL == ctx || NULL == ctx->cipher_info )
225 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
226
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200227#if defined(POLARSSL_GCM_C)
228 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
229 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200230 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200231 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200232 }
233#endif
234
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235 return 0;
236}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200237#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238
Paul Bakker23986e52011-04-24 08:57:21 +0000239int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
240 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241{
Paul Bakkerff61a782011-06-09 15:42:02 +0000242 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
Paul Bakker68884e32013-01-07 18:20:04 +0100244 *olen = 0;
245
246 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000247 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000248 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000249 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250
Paul Bakker5e0efa72013-09-08 23:04:04 +0200251 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
252 {
253 if( ilen != cipher_get_block_size( ctx ) )
254 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
255
256 *olen = ilen;
257
258 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
259 ctx->operation, input, output ) ) )
260 {
261 return ret;
262 }
263
264 return 0;
265 }
266
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200267#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200268 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200269 {
270 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200271 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
272 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200273 }
274#endif
275
Paul Bakker68884e32013-01-07 18:20:04 +0100276 if( input == output &&
277 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
278 {
279 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
280 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200282#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200283 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200285 size_t copy_len = 0;
286
Paul Bakker8123e9d2011-01-06 15:37:30 +0000287 /*
288 * If there is not enough data for a full block, cache it.
289 */
290 if( ( ctx->operation == POLARSSL_DECRYPT &&
291 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
292 ( ctx->operation == POLARSSL_ENCRYPT &&
293 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
294 {
295 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
296 ilen );
297
298 ctx->unprocessed_len += ilen;
299 return 0;
300 }
301
302 /*
303 * Process cached data first
304 */
305 if( ctx->unprocessed_len != 0 )
306 {
307 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
308
309 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
310 copy_len );
311
Paul Bakkerff61a782011-06-09 15:42:02 +0000312 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000314 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000316 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317 }
318
319 *olen += cipher_get_block_size( ctx );
320 output += cipher_get_block_size( ctx );
321 ctx->unprocessed_len = 0;
322
323 input += copy_len;
324 ilen -= copy_len;
325 }
326
327 /*
328 * Cache final, incomplete block
329 */
330 if( 0 != ilen )
331 {
332 copy_len = ilen % cipher_get_block_size( ctx );
333 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
334 copy_len = cipher_get_block_size(ctx);
335
336 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
337 copy_len );
338
339 ctx->unprocessed_len += copy_len;
340 ilen -= copy_len;
341 }
342
343 /*
344 * Process remaining full blocks
345 */
346 if( ilen )
347 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000348 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
349 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000351 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200353
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354 *olen += ilen;
355 }
356
357 return 0;
358 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200359#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360
Paul Bakker68884e32013-01-07 18:20:04 +0100361#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000362 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000363 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000364 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000365 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000366 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000367 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000368 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000369 }
370
371 *olen = ilen;
372
373 return 0;
374 }
Paul Bakker68884e32013-01-07 18:20:04 +0100375#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000376
Paul Bakker68884e32013-01-07 18:20:04 +0100377#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000378 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
379 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000380 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000381 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000382 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000383 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000384 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000385 }
386
387 *olen = ilen;
388
389 return 0;
390 }
Paul Bakker68884e32013-01-07 18:20:04 +0100391#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000392
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200393#if defined(POLARSSL_CIPHER_MODE_STREAM)
394 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
395 {
396 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
397 ilen, input, output ) ) )
398 {
399 return ret;
400 }
401
402 *olen = ilen;
403
404 return 0;
405 }
406#endif
407
Paul Bakkerff61a782011-06-09 15:42:02 +0000408 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409}
410
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200411#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200412#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200413/*
414 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
415 */
Paul Bakker23986e52011-04-24 08:57:21 +0000416static void add_pkcs_padding( unsigned char *output, size_t output_len,
417 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418{
Paul Bakker23986e52011-04-24 08:57:21 +0000419 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100420 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421
422 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000423 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424}
425
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200426static int get_pkcs_padding( unsigned char *input, size_t input_len,
427 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100429 size_t i, pad_idx;
430 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431
Paul Bakkera885d682011-01-20 16:35:05 +0000432 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000433 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
435 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 *data_len = input_len - padding_len;
437
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100438 /* Avoid logical || since it results in a branch */
439 bad |= padding_len > input_len;
440 bad |= padding_len == 0;
441
442 /* The number of bytes checked must be independent of padding_len,
443 * so pick input_len, which is usually 8 or 16 (one block) */
444 pad_idx = input_len - padding_len;
445 for( i = 0; i < input_len; i++ )
446 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
447
448 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449}
Paul Bakker48e93c82013-08-14 12:21:18 +0200450#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451
Paul Bakker48e93c82013-08-14 12:21:18 +0200452#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200453/*
454 * One and zeros padding: fill with 80 00 ... 00
455 */
456static void add_one_and_zeros_padding( unsigned char *output,
457 size_t output_len, size_t data_len )
458{
459 size_t padding_len = output_len - data_len;
460 unsigned char i = 0;
461
462 output[data_len] = 0x80;
463 for( i = 1; i < padding_len; i++ )
464 output[data_len + i] = 0x00;
465}
466
467static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
468 size_t *data_len )
469{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100470 size_t i;
471 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200472
473 if( NULL == input || NULL == data_len )
474 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
475
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100476 bad = 0xFF;
477 *data_len = 0;
478 for( i = input_len; i > 0; i-- )
479 {
480 prev_done = done;
481 done |= ( input[i-1] != 0 );
482 *data_len |= ( i - 1 ) * ( done != prev_done );
483 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
484 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200485
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100486 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200487
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200488}
Paul Bakker48e93c82013-08-14 12:21:18 +0200489#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200490
Paul Bakker48e93c82013-08-14 12:21:18 +0200491#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200492/*
493 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
494 */
495static void add_zeros_and_len_padding( unsigned char *output,
496 size_t output_len, size_t data_len )
497{
498 size_t padding_len = output_len - data_len;
499 unsigned char i = 0;
500
501 for( i = 1; i < padding_len; i++ )
502 output[data_len + i - 1] = 0x00;
503 output[output_len - 1] = (unsigned char) padding_len;
504}
505
506static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
507 size_t *data_len )
508{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100509 size_t i, pad_idx;
510 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200511
512 if( NULL == input || NULL == data_len )
513 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
514
515 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200516 *data_len = input_len - padding_len;
517
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100518 /* Avoid logical || since it results in a branch */
519 bad |= padding_len > input_len;
520 bad |= padding_len == 0;
521
522 /* The number of bytes checked must be independent of padding_len */
523 pad_idx = input_len - padding_len;
524 for( i = 0; i < input_len - 1; i++ )
525 bad |= input[i] * ( i >= pad_idx );
526
527 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200528}
Paul Bakker48e93c82013-08-14 12:21:18 +0200529#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200530
Paul Bakker48e93c82013-08-14 12:21:18 +0200531#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200532/*
533 * Zero padding: fill with 00 ... 00
534 */
535static void add_zeros_padding( unsigned char *output,
536 size_t output_len, size_t data_len )
537{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200538 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200539
540 for( i = data_len; i < output_len; i++ )
541 output[i] = 0x00;
542}
543
544static int get_zeros_padding( unsigned char *input, size_t input_len,
545 size_t *data_len )
546{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100547 size_t i;
548 unsigned char done = 0, prev_done;
549
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200550 if( NULL == input || NULL == data_len )
551 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
552
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100553 *data_len = 0;
554 for( i = input_len; i > 0; i-- )
555 {
556 prev_done = done;
557 done |= ( input[i-1] != 0 );
558 *data_len |= i * ( done != prev_done );
559 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200560
561 return 0;
562}
Paul Bakker48e93c82013-08-14 12:21:18 +0200563#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200564
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200565/*
566 * No padding: don't pad :)
567 *
568 * There is no add_padding function (check for NULL in cipher_finish)
569 * but a trivial get_padding function
570 */
571static int get_no_padding( unsigned char *input, size_t input_len,
572 size_t *data_len )
573{
574 if( NULL == input || NULL == data_len )
575 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
576
577 *data_len = input_len;
578
579 return 0;
580}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200581#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200582
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200583int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200584 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585{
586 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000587 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588
589 *olen = 0;
590
Paul Bakker6132d0a2012-07-04 17:10:40 +0000591 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000592 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200593 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200594 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000595 {
596 return 0;
597 }
598
Paul Bakker5e0efa72013-09-08 23:04:04 +0200599 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
600 {
601 if( ctx->unprocessed_len != 0 )
602 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
603
604 return 0;
605 }
606
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200607#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
609 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200610 int ret = 0;
611
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 if( POLARSSL_ENCRYPT == ctx->operation )
613 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200614 /* check for 'no padding' mode */
615 if( NULL == ctx->add_padding )
616 {
617 if( 0 != ctx->unprocessed_len )
618 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
619
620 return 0;
621 }
622
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200623 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624 ctx->unprocessed_len );
625 }
626 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
627 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200628 /*
629 * For decrypt operations, expect a full block,
630 * or an empty block if no padding
631 */
632 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
633 return 0;
634
Paul Bakkerff61a782011-06-09 15:42:02 +0000635 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 }
637
638 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000639 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
640 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
641 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000643 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644 }
645
646 /* Set output size for decryption */
647 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200648 return ctx->get_padding( output, cipher_get_block_size( ctx ),
649 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
651 /* Set output size for encryption */
652 *olen = cipher_get_block_size( ctx );
653 return 0;
654 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200655#else
656 ((void) output);
657#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
Paul Bakkerff61a782011-06-09 15:42:02 +0000659 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660}
661
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200662#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200663int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
664{
665 if( NULL == ctx ||
666 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
667 {
668 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
669 }
670
Paul Bakker1a45d912013-08-14 12:04:26 +0200671 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200672 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200673#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200674 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200675 ctx->add_padding = add_pkcs_padding;
676 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200677 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200678#endif
679#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200680 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200681 ctx->add_padding = add_one_and_zeros_padding;
682 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200683 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200684#endif
685#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200686 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200687 ctx->add_padding = add_zeros_and_len_padding;
688 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200689 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200690#endif
691#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200692 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200693 ctx->add_padding = add_zeros_padding;
694 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200696#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200697 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200698 ctx->add_padding = NULL;
699 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200700 break;
701
702 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200703 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200704 }
705
Paul Bakker1a45d912013-08-14 12:04:26 +0200706 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200707}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200708#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200709
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200710#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200711int cipher_write_tag( cipher_context_t *ctx,
712 unsigned char *tag, size_t tag_len )
713{
714 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
715 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
716
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200717 if( POLARSSL_ENCRYPT != ctx->operation )
718 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
719
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200720#if defined(POLARSSL_GCM_C)
721 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200722 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200723#endif
724
725 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200726}
727
728int cipher_check_tag( cipher_context_t *ctx,
729 const unsigned char *tag, size_t tag_len )
730{
731 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200732
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200733 if( NULL == ctx || NULL == ctx->cipher_info ||
734 POLARSSL_DECRYPT != ctx->operation )
735 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200736 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200737 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200738
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200739#if defined(POLARSSL_GCM_C)
740 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
741 {
742 unsigned char check_tag[16];
743 size_t i;
744 int diff;
745
746 if( tag_len > sizeof( check_tag ) )
747 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
748
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200749 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
750 check_tag, tag_len ) ) )
751 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200752 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200753 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200754
755 /* Check the tag in "constant-time" */
756 for( diff = 0, i = 0; i < tag_len; i++ )
757 diff |= tag[i] ^ check_tag[i];
758
759 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200760 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200761
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200762 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763 }
764#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200765
766 return( 0 );
767}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200768#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200769
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770#if defined(POLARSSL_SELF_TEST)
771
772#include <stdio.h>
773
774#define ASSERT(x) if (!(x)) { \
775 printf( "failed with %i at %s\n", value, (#x) ); \
776 return( 1 ); \
777}
778/*
779 * Checkup routine
780 */
781
782int cipher_self_test( int verbose )
783{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000784 ((void) verbose);
785
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786 return( 0 );
787}
788
789#endif
790
791#endif