blob: f20cc73b483f9dce031262f81261241f6f0a6c43 [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 Bakker6132d0a2012-07-04 17:10:40 +000089#if defined(POLARSSL_BLOWFISH_C)
90 POLARSSL_CIPHER_BLOWFISH_CBC,
91
92#if defined(POLARSSL_CIPHER_MODE_CFB)
93 POLARSSL_CIPHER_BLOWFISH_CFB64,
94#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
95
96#if defined(POLARSSL_CIPHER_MODE_CTR)
97 POLARSSL_CIPHER_BLOWFISH_CTR,
98#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
99
100#endif /* defined(POLARSSL_BLOWFISH_C) */
101
Paul Bakkerfab5c822012-02-06 16:45:10 +0000102#if defined(POLARSSL_CIPHER_NULL_CIPHER)
103 POLARSSL_CIPHER_NULL,
104#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
105
Paul Bakker72f62662011-01-16 21:27:44 +0000106 0
107};
108
109const int *cipher_list( void )
110{
111 return supported_ciphers;
112}
113
Paul Bakkerec1b9842012-01-14 18:24:43 +0000114const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115{
116 /* Find static cipher information */
117 switch ( cipher_type )
118 {
119#if defined(POLARSSL_AES_C)
120 case POLARSSL_CIPHER_AES_128_CBC:
121 return &aes_128_cbc_info;
122 case POLARSSL_CIPHER_AES_192_CBC:
123 return &aes_192_cbc_info;
124 case POLARSSL_CIPHER_AES_256_CBC:
125 return &aes_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000126
127#if defined(POLARSSL_CIPHER_MODE_CFB)
128 case POLARSSL_CIPHER_AES_128_CFB128:
129 return &aes_128_cfb128_info;
130 case POLARSSL_CIPHER_AES_192_CFB128:
131 return &aes_192_cfb128_info;
132 case POLARSSL_CIPHER_AES_256_CFB128:
133 return &aes_256_cfb128_info;
134#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
135
136#if defined(POLARSSL_CIPHER_MODE_CTR)
137 case POLARSSL_CIPHER_AES_128_CTR:
138 return &aes_128_ctr_info;
139 case POLARSSL_CIPHER_AES_192_CTR:
140 return &aes_192_ctr_info;
141 case POLARSSL_CIPHER_AES_256_CTR:
142 return &aes_256_ctr_info;
143#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
144
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145#endif
146
147#if defined(POLARSSL_CAMELLIA_C)
148 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
149 return &camellia_128_cbc_info;
150 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
151 return &camellia_192_cbc_info;
152 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
153 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000154
155#if defined(POLARSSL_CIPHER_MODE_CFB)
156 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
157 return &camellia_128_cfb128_info;
158 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
159 return &camellia_192_cfb128_info;
160 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
161 return &camellia_256_cfb128_info;
162#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
163
164#if defined(POLARSSL_CIPHER_MODE_CTR)
165 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
166 return &camellia_128_ctr_info;
167 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
168 return &camellia_192_ctr_info;
169 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
170 return &camellia_256_ctr_info;
171#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
172
Paul Bakker8123e9d2011-01-06 15:37:30 +0000173#endif
174
175#if defined(POLARSSL_DES_C)
176 case POLARSSL_CIPHER_DES_CBC:
177 return &des_cbc_info;
178 case POLARSSL_CIPHER_DES_EDE_CBC:
179 return &des_ede_cbc_info;
180 case POLARSSL_CIPHER_DES_EDE3_CBC:
181 return &des_ede3_cbc_info;
182#endif
183
Paul Bakker6132d0a2012-07-04 17:10:40 +0000184#if defined(POLARSSL_BLOWFISH_C)
185 case POLARSSL_CIPHER_BLOWFISH_CBC:
186 return &blowfish_cbc_info;
187
188#if defined(POLARSSL_CIPHER_MODE_CFB)
189 case POLARSSL_CIPHER_BLOWFISH_CFB64:
190 return &blowfish_cfb64_info;
191#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
192
193#if defined(POLARSSL_CIPHER_MODE_CTR)
194 case POLARSSL_CIPHER_BLOWFISH_CTR:
195 return &blowfish_ctr_info;
196#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
197
198#endif
199
Paul Bakkerfab5c822012-02-06 16:45:10 +0000200#if defined(POLARSSL_CIPHER_NULL_CIPHER)
201 case POLARSSL_CIPHER_NULL:
202 return &null_cipher_info;
203#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
204
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205 default:
206 return NULL;
207 }
208}
209
210const cipher_info_t *cipher_info_from_string( const char *cipher_name )
211{
212 if( NULL == cipher_name )
213 return NULL;
214
Paul Bakker343a8702011-06-09 14:27:58 +0000215 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216#if defined(POLARSSL_CAMELLIA_C)
217 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
218 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
219 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
220 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
221 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
222 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000223
224#if defined(POLARSSL_CIPHER_MODE_CFB)
225 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
226 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
227 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
228 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
229 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
230 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
231#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
232
233#if defined(POLARSSL_CIPHER_MODE_CTR)
234 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
235 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
236 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
237 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
238 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
239 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
240#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000242
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243#if defined(POLARSSL_AES_C)
244 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
245 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
246 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
247 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
248 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
249 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000250
251#if defined(POLARSSL_CIPHER_MODE_CFB)
252 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
253 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
254 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
255 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
256 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
257 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
258#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
259
260#if defined(POLARSSL_CIPHER_MODE_CTR)
261 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
262 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
263 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
264 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
265 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
266 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
267#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000269
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270#if defined(POLARSSL_DES_C)
271 if( !strcasecmp( "DES-CBC", cipher_name ) )
272 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
273 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
274 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
275 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
276 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
277#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000278
Paul Bakker6132d0a2012-07-04 17:10:40 +0000279#if defined(POLARSSL_BLOWFISH_C)
280 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
281 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
282
283#if defined(POLARSSL_CIPHER_MODE_CFB)
284 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
285 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
286#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
287
288#if defined(POLARSSL_CIPHER_MODE_CTR)
289 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
290 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
291#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
292#endif
293
Paul Bakkerfab5c822012-02-06 16:45:10 +0000294#if defined(POLARSSL_CIPHER_NULL_CIPHER)
295 if( !strcasecmp( "NULL", cipher_name ) )
296 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
297#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
298
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299 return NULL;
300}
301
302int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
303{
304 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000305 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306
Paul Bakker279432a2012-04-26 10:09:35 +0000307 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
Paul Bakker343a8702011-06-09 14:27:58 +0000309 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000310 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311
312 ctx->cipher_info = cipher_info;
313
314 return 0;
315}
316
317int cipher_free_ctx( cipher_context_t *ctx )
318{
319 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000320 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321
Paul Bakker343a8702011-06-09 14:27:58 +0000322 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323
324 return 0;
325}
326
327int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
328 int key_length, const operation_t operation )
329{
330 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000331 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
333 ctx->key_length = key_length;
334 ctx->operation = operation;
335
Paul Bakkerfab5c822012-02-06 16:45:10 +0000336#if defined(POLARSSL_CIPHER_NULL_CIPHER)
337 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
338 return 0;
339#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
340
Paul Bakker343a8702011-06-09 14:27:58 +0000341 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000342 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000343 */
344 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000345 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000346 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
347 {
348 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000350 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351
Paul Bakker343a8702011-06-09 14:27:58 +0000352 if( POLARSSL_DECRYPT == operation )
353 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354 ctx->key_length );
355
Paul Bakkerff61a782011-06-09 15:42:02 +0000356 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357}
358
359int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
360{
361 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000362 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
364 ctx->unprocessed_len = 0;
365
366 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
367
368 return 0;
369}
370
Paul Bakker23986e52011-04-24 08:57:21 +0000371int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
372 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373{
Paul Bakkerff61a782011-06-09 15:42:02 +0000374 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000375 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Paul Bakkera885d682011-01-20 16:35:05 +0000377 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
378 input == output )
379 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000380 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000381 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
383 *olen = 0;
384
Paul Bakkerfab5c822012-02-06 16:45:10 +0000385#if defined(POLARSSL_CIPHER_NULL_CIPHER)
386 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
387 {
388 memcpy( output, input, ilen );
389 *olen = ilen;
390 return 0;
391 }
392#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
393
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
395 {
396 /*
397 * If there is not enough data for a full block, cache it.
398 */
399 if( ( ctx->operation == POLARSSL_DECRYPT &&
400 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
401 ( ctx->operation == POLARSSL_ENCRYPT &&
402 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
403 {
404 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
405 ilen );
406
407 ctx->unprocessed_len += ilen;
408 return 0;
409 }
410
411 /*
412 * Process cached data first
413 */
414 if( ctx->unprocessed_len != 0 )
415 {
416 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
417
418 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
419 copy_len );
420
Paul Bakkerff61a782011-06-09 15:42:02 +0000421 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000423 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000425 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426 }
427
428 *olen += cipher_get_block_size( ctx );
429 output += cipher_get_block_size( ctx );
430 ctx->unprocessed_len = 0;
431
432 input += copy_len;
433 ilen -= copy_len;
434 }
435
436 /*
437 * Cache final, incomplete block
438 */
439 if( 0 != ilen )
440 {
441 copy_len = ilen % cipher_get_block_size( ctx );
442 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
443 copy_len = cipher_get_block_size(ctx);
444
445 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
446 copy_len );
447
448 ctx->unprocessed_len += copy_len;
449 ilen -= copy_len;
450 }
451
452 /*
453 * Process remaining full blocks
454 */
455 if( ilen )
456 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000457 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
458 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000460 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461 }
462 *olen += ilen;
463 }
464
465 return 0;
466 }
467
Paul Bakker6132d0a2012-07-04 17:10:40 +0000468 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000469 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000470 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000471 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000472 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000473 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000474 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000475 }
476
477 *olen = ilen;
478
479 return 0;
480 }
481
482 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
483 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000484 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000485 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000486 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000487 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000488 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000489 }
490
491 *olen = ilen;
492
493 return 0;
494 }
495
Paul Bakkerff61a782011-06-09 15:42:02 +0000496 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497}
498
Paul Bakker23986e52011-04-24 08:57:21 +0000499static void add_pkcs_padding( unsigned char *output, size_t output_len,
500 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501{
Paul Bakker23986e52011-04-24 08:57:21 +0000502 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503 unsigned char i = 0;
504
505 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000506 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507}
508
Paul Bakkerec1b9842012-01-14 18:24:43 +0000509static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
Paul Bakker23986e52011-04-24 08:57:21 +0000510 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000512 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513
Paul Bakkera885d682011-01-20 16:35:05 +0000514 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000515 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516
517 padding_len = input[input_len - 1];
518
Paul Bakkera885d682011-01-20 16:35:05 +0000519 if( padding_len > input_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000520 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521
Paul Bakkera885d682011-01-20 16:35:05 +0000522 for( i = input_len - padding_len; i < input_len; i++ )
523 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000524 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525
526 *data_len = input_len - padding_len;
527
528 return 0;
529}
530
Paul Bakker23986e52011-04-24 08:57:21 +0000531int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532{
Paul Bakkerff61a782011-06-09 15:42:02 +0000533 int ret = 0;
534
Paul Bakker8123e9d2011-01-06 15:37:30 +0000535 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000536 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000537
538 *olen = 0;
539
Paul Bakker6132d0a2012-07-04 17:10:40 +0000540 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000541 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
542 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000543 {
544 return 0;
545 }
546
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
548 {
549 if( POLARSSL_ENCRYPT == ctx->operation )
550 {
551 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
552 ctx->unprocessed_len );
553 }
554 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
555 {
556 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000557 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558 }
559
560 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000561 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
562 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
563 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000565 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000566 }
567
568 /* Set output size for decryption */
569 if( POLARSSL_DECRYPT == ctx->operation )
570 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
571
572 /* Set output size for encryption */
573 *olen = cipher_get_block_size( ctx );
574 return 0;
575 }
576
Paul Bakkerff61a782011-06-09 15:42:02 +0000577 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578}
579
580#if defined(POLARSSL_SELF_TEST)
581
582#include <stdio.h>
583
584#define ASSERT(x) if (!(x)) { \
585 printf( "failed with %i at %s\n", value, (#x) ); \
586 return( 1 ); \
587}
588/*
589 * Checkup routine
590 */
591
592int cipher_self_test( int verbose )
593{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000594 ((void) verbose);
595
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 return( 0 );
597}
598
599#endif
600
601#endif