blob: 85c74aba1f8690924783161c6fc0167f02674e60 [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 Bakkerfab5c822012-02-06 16:45:10 +00008 * Copyright (C) 2006-2012, 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
Paul Bakker8123e9d2011-01-06 15:37:30 +000037#include <stdlib.h>
38
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000039#if defined _MSC_VER && !defined strcasecmp
40#define strcasecmp _stricmp
41#endif
42
Paul Bakker72f62662011-01-16 21:27:44 +000043static const int supported_ciphers[] = {
44
45#if defined(POLARSSL_AES_C)
46 POLARSSL_CIPHER_AES_128_CBC,
47 POLARSSL_CIPHER_AES_192_CBC,
48 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000049
50#if defined(POLARSSL_CIPHER_MODE_CFB)
51 POLARSSL_CIPHER_AES_128_CFB128,
52 POLARSSL_CIPHER_AES_192_CFB128,
53 POLARSSL_CIPHER_AES_256_CFB128,
54#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
55
56#if defined(POLARSSL_CIPHER_MODE_CTR)
57 POLARSSL_CIPHER_AES_128_CTR,
58 POLARSSL_CIPHER_AES_192_CTR,
59 POLARSSL_CIPHER_AES_256_CTR,
60#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
61
Paul Bakker72f62662011-01-16 21:27:44 +000062#endif /* defined(POLARSSL_AES_C) */
63
64#if defined(POLARSSL_CAMELLIA_C)
65 POLARSSL_CIPHER_CAMELLIA_128_CBC,
66 POLARSSL_CIPHER_CAMELLIA_192_CBC,
67 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000068
69#if defined(POLARSSL_CIPHER_MODE_CFB)
70 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
71 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
72 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
73#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
74
75#if defined(POLARSSL_CIPHER_MODE_CTR)
76 POLARSSL_CIPHER_CAMELLIA_128_CTR,
77 POLARSSL_CIPHER_CAMELLIA_192_CTR,
78 POLARSSL_CIPHER_CAMELLIA_256_CTR,
79#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
80
Paul Bakker72f62662011-01-16 21:27:44 +000081#endif /* defined(POLARSSL_CAMELLIA_C) */
82
83#if defined(POLARSSL_DES_C)
84 POLARSSL_CIPHER_DES_CBC,
85 POLARSSL_CIPHER_DES_EDE_CBC,
86 POLARSSL_CIPHER_DES_EDE3_CBC,
87#endif /* defined(POLARSSL_DES_C) */
88
Paul Bakkerfab5c822012-02-06 16:45:10 +000089#if defined(POLARSSL_CIPHER_NULL_CIPHER)
90 POLARSSL_CIPHER_NULL,
91#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
92
Paul Bakker72f62662011-01-16 21:27:44 +000093 0
94};
95
96const int *cipher_list( void )
97{
98 return supported_ciphers;
99}
100
Paul Bakkerec1b9842012-01-14 18:24:43 +0000101const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102{
103 /* Find static cipher information */
104 switch ( cipher_type )
105 {
106#if defined(POLARSSL_AES_C)
107 case POLARSSL_CIPHER_AES_128_CBC:
108 return &aes_128_cbc_info;
109 case POLARSSL_CIPHER_AES_192_CBC:
110 return &aes_192_cbc_info;
111 case POLARSSL_CIPHER_AES_256_CBC:
112 return &aes_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000113
114#if defined(POLARSSL_CIPHER_MODE_CFB)
115 case POLARSSL_CIPHER_AES_128_CFB128:
116 return &aes_128_cfb128_info;
117 case POLARSSL_CIPHER_AES_192_CFB128:
118 return &aes_192_cfb128_info;
119 case POLARSSL_CIPHER_AES_256_CFB128:
120 return &aes_256_cfb128_info;
121#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
122
123#if defined(POLARSSL_CIPHER_MODE_CTR)
124 case POLARSSL_CIPHER_AES_128_CTR:
125 return &aes_128_ctr_info;
126 case POLARSSL_CIPHER_AES_192_CTR:
127 return &aes_192_ctr_info;
128 case POLARSSL_CIPHER_AES_256_CTR:
129 return &aes_256_ctr_info;
130#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
131
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132#endif
133
134#if defined(POLARSSL_CAMELLIA_C)
135 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
136 return &camellia_128_cbc_info;
137 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
138 return &camellia_192_cbc_info;
139 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
140 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000141
142#if defined(POLARSSL_CIPHER_MODE_CFB)
143 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
144 return &camellia_128_cfb128_info;
145 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
146 return &camellia_192_cfb128_info;
147 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
148 return &camellia_256_cfb128_info;
149#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
150
151#if defined(POLARSSL_CIPHER_MODE_CTR)
152 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
153 return &camellia_128_ctr_info;
154 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
155 return &camellia_192_ctr_info;
156 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
157 return &camellia_256_ctr_info;
158#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
159
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160#endif
161
162#if defined(POLARSSL_DES_C)
163 case POLARSSL_CIPHER_DES_CBC:
164 return &des_cbc_info;
165 case POLARSSL_CIPHER_DES_EDE_CBC:
166 return &des_ede_cbc_info;
167 case POLARSSL_CIPHER_DES_EDE3_CBC:
168 return &des_ede3_cbc_info;
169#endif
170
Paul Bakkerfab5c822012-02-06 16:45:10 +0000171#if defined(POLARSSL_CIPHER_NULL_CIPHER)
172 case POLARSSL_CIPHER_NULL:
173 return &null_cipher_info;
174#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
175
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 default:
177 return NULL;
178 }
179}
180
181const cipher_info_t *cipher_info_from_string( const char *cipher_name )
182{
183 if( NULL == cipher_name )
184 return NULL;
185
Paul Bakker343a8702011-06-09 14:27:58 +0000186 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187#if defined(POLARSSL_CAMELLIA_C)
188 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
189 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
190 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
191 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
192 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
193 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000194
195#if defined(POLARSSL_CIPHER_MODE_CFB)
196 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
197 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
198 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
199 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
200 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
201 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
202#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
203
204#if defined(POLARSSL_CIPHER_MODE_CTR)
205 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
206 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
207 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
208 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
209 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
210 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
211#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000213
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214#if defined(POLARSSL_AES_C)
215 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
216 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
217 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
218 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
219 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
220 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000221
222#if defined(POLARSSL_CIPHER_MODE_CFB)
223 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
224 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
225 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
226 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
227 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
228 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
229#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
230
231#if defined(POLARSSL_CIPHER_MODE_CTR)
232 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
233 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
234 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
235 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
236 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
237 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
238#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000240
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241#if defined(POLARSSL_DES_C)
242 if( !strcasecmp( "DES-CBC", cipher_name ) )
243 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
244 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
245 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
246 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
247 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
248#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000249
250#if defined(POLARSSL_CIPHER_NULL_CIPHER)
251 if( !strcasecmp( "NULL", cipher_name ) )
252 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
253#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
254
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255 return NULL;
256}
257
258int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
259{
260 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000261 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262
263 memset( ctx, 0, sizeof( ctx ) );
264
Paul Bakker343a8702011-06-09 14:27:58 +0000265 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000266 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
268 ctx->cipher_info = cipher_info;
269
270 return 0;
271}
272
273int cipher_free_ctx( cipher_context_t *ctx )
274{
275 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000276 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277
Paul Bakker343a8702011-06-09 14:27:58 +0000278 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279
280 return 0;
281}
282
283int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
284 int key_length, const operation_t operation )
285{
286 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000287 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288
289 ctx->key_length = key_length;
290 ctx->operation = operation;
291
Paul Bakkerfab5c822012-02-06 16:45:10 +0000292#if defined(POLARSSL_CIPHER_NULL_CIPHER)
293 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
294 return 0;
295#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
296
Paul Bakker343a8702011-06-09 14:27:58 +0000297 /*
298 * For CFB128 and CTR mode always use the encryption key schedule
299 */
300 if( POLARSSL_ENCRYPT == operation ||
301 POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
302 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
303 {
304 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000306 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307
Paul Bakker343a8702011-06-09 14:27:58 +0000308 if( POLARSSL_DECRYPT == operation )
309 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310 ctx->key_length );
311
Paul Bakkerff61a782011-06-09 15:42:02 +0000312 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313}
314
315int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
316{
317 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000318 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319
320 ctx->unprocessed_len = 0;
321
322 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
323
324 return 0;
325}
326
Paul Bakker23986e52011-04-24 08:57:21 +0000327int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
328 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329{
Paul Bakkerff61a782011-06-09 15:42:02 +0000330 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000331 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
Paul Bakkera885d682011-01-20 16:35:05 +0000333 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
334 input == output )
335 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000336 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000337 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
339 *olen = 0;
340
Paul Bakkerfab5c822012-02-06 16:45:10 +0000341#if defined(POLARSSL_CIPHER_NULL_CIPHER)
342 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
343 {
344 memcpy( output, input, ilen );
345 *olen = ilen;
346 return 0;
347 }
348#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
349
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
351 {
352 /*
353 * If there is not enough data for a full block, cache it.
354 */
355 if( ( ctx->operation == POLARSSL_DECRYPT &&
356 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
357 ( ctx->operation == POLARSSL_ENCRYPT &&
358 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
359 {
360 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
361 ilen );
362
363 ctx->unprocessed_len += ilen;
364 return 0;
365 }
366
367 /*
368 * Process cached data first
369 */
370 if( ctx->unprocessed_len != 0 )
371 {
372 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
373
374 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
375 copy_len );
376
Paul Bakkerff61a782011-06-09 15:42:02 +0000377 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000379 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000381 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382 }
383
384 *olen += cipher_get_block_size( ctx );
385 output += cipher_get_block_size( ctx );
386 ctx->unprocessed_len = 0;
387
388 input += copy_len;
389 ilen -= copy_len;
390 }
391
392 /*
393 * Cache final, incomplete block
394 */
395 if( 0 != ilen )
396 {
397 copy_len = ilen % cipher_get_block_size( ctx );
398 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
399 copy_len = cipher_get_block_size(ctx);
400
401 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
402 copy_len );
403
404 ctx->unprocessed_len += copy_len;
405 ilen -= copy_len;
406 }
407
408 /*
409 * Process remaining full blocks
410 */
411 if( ilen )
412 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000413 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
414 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000416 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417 }
418 *olen += ilen;
419 }
420
421 return 0;
422 }
423
Paul Bakker343a8702011-06-09 14:27:58 +0000424 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 )
425 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000426 if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000427 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000428 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000429 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000430 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000431 }
432
433 *olen = ilen;
434
435 return 0;
436 }
437
438 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
439 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000440 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000441 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000443 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000444 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000445 }
446
447 *olen = ilen;
448
449 return 0;
450 }
451
Paul Bakkerff61a782011-06-09 15:42:02 +0000452 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453}
454
Paul Bakker23986e52011-04-24 08:57:21 +0000455static void add_pkcs_padding( unsigned char *output, size_t output_len,
456 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457{
Paul Bakker23986e52011-04-24 08:57:21 +0000458 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459 unsigned char i = 0;
460
461 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000462 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463}
464
Paul Bakkerec1b9842012-01-14 18:24:43 +0000465static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
Paul Bakker23986e52011-04-24 08:57:21 +0000466 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000468 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469
Paul Bakkera885d682011-01-20 16:35:05 +0000470 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000471 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472
473 padding_len = input[input_len - 1];
474
Paul Bakkera885d682011-01-20 16:35:05 +0000475 if( padding_len > input_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000476 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477
Paul Bakkera885d682011-01-20 16:35:05 +0000478 for( i = input_len - padding_len; i < input_len; i++ )
479 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000480 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481
482 *data_len = input_len - padding_len;
483
484 return 0;
485}
486
Paul Bakker23986e52011-04-24 08:57:21 +0000487int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000488{
Paul Bakkerff61a782011-06-09 15:42:02 +0000489 int ret = 0;
490
Paul Bakker8123e9d2011-01-06 15:37:30 +0000491 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000492 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493
494 *olen = 0;
495
Paul Bakker343a8702011-06-09 14:27:58 +0000496 if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000497 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
498 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000499 {
500 return 0;
501 }
502
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
504 {
505 if( POLARSSL_ENCRYPT == ctx->operation )
506 {
507 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
508 ctx->unprocessed_len );
509 }
510 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
511 {
512 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000513 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514 }
515
516 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000517 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
518 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
519 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000521 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522 }
523
524 /* Set output size for decryption */
525 if( POLARSSL_DECRYPT == ctx->operation )
526 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
527
528 /* Set output size for encryption */
529 *olen = cipher_get_block_size( ctx );
530 return 0;
531 }
532
Paul Bakkerff61a782011-06-09 15:42:02 +0000533 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534}
535
536#if defined(POLARSSL_SELF_TEST)
537
538#include <stdio.h>
539
540#define ASSERT(x) if (!(x)) { \
541 printf( "failed with %i at %s\n", value, (#x) ); \
542 return( 1 ); \
543}
544/*
545 * Checkup routine
546 */
547
548int cipher_self_test( int verbose )
549{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000550 ((void) verbose);
551
Paul Bakker8123e9d2011-01-06 15:37:30 +0000552 return( 0 );
553}
554
555#endif
556
557#endif