blob: 2a2d78299301eb20ae5f2be1624d06b5ff1ec6d5 [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
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 Bakker68884e32013-01-07 18:20:04 +0100145#if defined(POLARSSL_GCM_C)
146 case POLARSSL_CIPHER_AES_128_GCM:
147 return &aes_128_gcm_info;
148 case POLARSSL_CIPHER_AES_256_GCM:
149 return &aes_256_gcm_info;
150#endif /* defined(POLARSSL_GCM_C) */
151
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152#endif
153
154#if defined(POLARSSL_CAMELLIA_C)
155 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
156 return &camellia_128_cbc_info;
157 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
158 return &camellia_192_cbc_info;
159 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
160 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000161
162#if defined(POLARSSL_CIPHER_MODE_CFB)
163 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
164 return &camellia_128_cfb128_info;
165 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
166 return &camellia_192_cfb128_info;
167 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
168 return &camellia_256_cfb128_info;
169#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
170
171#if defined(POLARSSL_CIPHER_MODE_CTR)
172 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
173 return &camellia_128_ctr_info;
174 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
175 return &camellia_192_ctr_info;
176 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
177 return &camellia_256_ctr_info;
178#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
179
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180#endif
181
182#if defined(POLARSSL_DES_C)
183 case POLARSSL_CIPHER_DES_CBC:
184 return &des_cbc_info;
185 case POLARSSL_CIPHER_DES_EDE_CBC:
186 return &des_ede_cbc_info;
187 case POLARSSL_CIPHER_DES_EDE3_CBC:
188 return &des_ede3_cbc_info;
189#endif
190
Paul Bakker68884e32013-01-07 18:20:04 +0100191#if defined(POLARSSL_ARC4_C)
192 case POLARSSL_CIPHER_ARC4_128:
193 return &arc4_128_info;
194#endif
195
Paul Bakker6132d0a2012-07-04 17:10:40 +0000196#if defined(POLARSSL_BLOWFISH_C)
197 case POLARSSL_CIPHER_BLOWFISH_CBC:
198 return &blowfish_cbc_info;
199
200#if defined(POLARSSL_CIPHER_MODE_CFB)
201 case POLARSSL_CIPHER_BLOWFISH_CFB64:
202 return &blowfish_cfb64_info;
203#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
204
205#if defined(POLARSSL_CIPHER_MODE_CTR)
206 case POLARSSL_CIPHER_BLOWFISH_CTR:
207 return &blowfish_ctr_info;
208#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
209
210#endif
211
Paul Bakkerfab5c822012-02-06 16:45:10 +0000212#if defined(POLARSSL_CIPHER_NULL_CIPHER)
213 case POLARSSL_CIPHER_NULL:
214 return &null_cipher_info;
215#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
216
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 default:
218 return NULL;
219 }
220}
221
222const cipher_info_t *cipher_info_from_string( const char *cipher_name )
223{
224 if( NULL == cipher_name )
225 return NULL;
226
Paul Bakker343a8702011-06-09 14:27:58 +0000227 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228#if defined(POLARSSL_CAMELLIA_C)
229 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
230 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
231 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
232 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
233 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
234 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000235
236#if defined(POLARSSL_CIPHER_MODE_CFB)
237 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
238 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
239 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
240 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
241 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
242 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
243#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
244
245#if defined(POLARSSL_CIPHER_MODE_CTR)
246 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
247 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
248 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
249 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
250 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
251 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
252#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000254
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255#if defined(POLARSSL_AES_C)
256 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
257 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
258 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
259 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
260 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
261 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000262
263#if defined(POLARSSL_CIPHER_MODE_CFB)
264 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
265 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
266 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
267 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
268 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
269 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
270#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
271
272#if defined(POLARSSL_CIPHER_MODE_CTR)
273 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
274 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
275 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
276 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
277 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
278 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
279#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000280#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000281
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282#if defined(POLARSSL_DES_C)
283 if( !strcasecmp( "DES-CBC", cipher_name ) )
284 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
285 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
286 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
287 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
288 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
289#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000290
Paul Bakker6132d0a2012-07-04 17:10:40 +0000291#if defined(POLARSSL_BLOWFISH_C)
292 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
293 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
294
295#if defined(POLARSSL_CIPHER_MODE_CFB)
296 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
297 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
298#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
299
300#if defined(POLARSSL_CIPHER_MODE_CTR)
301 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
302 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
303#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
304#endif
305
Paul Bakkerfab5c822012-02-06 16:45:10 +0000306#if defined(POLARSSL_CIPHER_NULL_CIPHER)
307 if( !strcasecmp( "NULL", cipher_name ) )
308 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
309#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
310
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 return NULL;
312}
313
314int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
315{
316 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000317 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000318
Paul Bakker279432a2012-04-26 10:09:35 +0000319 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Paul Bakker343a8702011-06-09 14:27:58 +0000321 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000322 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323
324 ctx->cipher_info = cipher_info;
325
326 return 0;
327}
328
329int cipher_free_ctx( cipher_context_t *ctx )
330{
331 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000332 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Paul Bakker343a8702011-06-09 14:27:58 +0000334 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335
336 return 0;
337}
338
339int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
340 int key_length, const operation_t operation )
341{
342 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000343 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
345 ctx->key_length = key_length;
346 ctx->operation = operation;
347
Paul Bakkerfab5c822012-02-06 16:45:10 +0000348#if defined(POLARSSL_CIPHER_NULL_CIPHER)
349 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
350 return 0;
351#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
352
Paul Bakker343a8702011-06-09 14:27:58 +0000353 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000354 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000355 */
356 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000357 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000358 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
359 {
360 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000362 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
Paul Bakker343a8702011-06-09 14:27:58 +0000364 if( POLARSSL_DECRYPT == operation )
365 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 ctx->key_length );
367
Paul Bakkerff61a782011-06-09 15:42:02 +0000368 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369}
370
371int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
372{
373 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000374 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375
376 ctx->unprocessed_len = 0;
377
378 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
379
380 return 0;
381}
382
Paul Bakker23986e52011-04-24 08:57:21 +0000383int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
384 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385{
Paul Bakkerff61a782011-06-09 15:42:02 +0000386 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000387 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388
Paul Bakker68884e32013-01-07 18:20:04 +0100389 *olen = 0;
390
391 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000392 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000393 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000394 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395
Paul Bakker68884e32013-01-07 18:20:04 +0100396 if( input == output &&
397 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
398 {
399 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
400 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401
Paul Bakkerfab5c822012-02-06 16:45:10 +0000402#if defined(POLARSSL_CIPHER_NULL_CIPHER)
403 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
404 {
Paul Bakkerfab5c822012-02-06 16:45:10 +0000405 *olen = ilen;
Paul Bakker68884e32013-01-07 18:20:04 +0100406
407 if( output == input )
408 return( 0 );
409
410 memcpy( output, input, ilen );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000411 return 0;
412 }
413#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
414
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
416 {
417 /*
418 * If there is not enough data for a full block, cache it.
419 */
420 if( ( ctx->operation == POLARSSL_DECRYPT &&
421 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
422 ( ctx->operation == POLARSSL_ENCRYPT &&
423 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
424 {
425 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
426 ilen );
427
428 ctx->unprocessed_len += ilen;
429 return 0;
430 }
431
432 /*
433 * Process cached data first
434 */
435 if( ctx->unprocessed_len != 0 )
436 {
437 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
438
439 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
440 copy_len );
441
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000444 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000446 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 }
448
449 *olen += cipher_get_block_size( ctx );
450 output += cipher_get_block_size( ctx );
451 ctx->unprocessed_len = 0;
452
453 input += copy_len;
454 ilen -= copy_len;
455 }
456
457 /*
458 * Cache final, incomplete block
459 */
460 if( 0 != ilen )
461 {
462 copy_len = ilen % cipher_get_block_size( ctx );
463 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
464 copy_len = cipher_get_block_size(ctx);
465
466 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
467 copy_len );
468
469 ctx->unprocessed_len += copy_len;
470 ilen -= copy_len;
471 }
472
473 /*
474 * Process remaining full blocks
475 */
476 if( ilen )
477 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000478 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
479 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000481 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482 }
483 *olen += ilen;
484 }
485
486 return 0;
487 }
488
Paul Bakker68884e32013-01-07 18:20:04 +0100489#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000490 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000491 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000492 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000493 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000494 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000495 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000496 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000497 }
498
499 *olen = ilen;
500
501 return 0;
502 }
Paul Bakker68884e32013-01-07 18:20:04 +0100503#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000504
Paul Bakker68884e32013-01-07 18:20:04 +0100505#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000506 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
507 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000508 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000509 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000510 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000511 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000512 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000513 }
514
515 *olen = ilen;
516
517 return 0;
518 }
Paul Bakker68884e32013-01-07 18:20:04 +0100519#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000520
Paul Bakkerff61a782011-06-09 15:42:02 +0000521 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522}
523
Paul Bakker23986e52011-04-24 08:57:21 +0000524static void add_pkcs_padding( unsigned char *output, size_t output_len,
525 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526{
Paul Bakker23986e52011-04-24 08:57:21 +0000527 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528 unsigned char i = 0;
529
530 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000531 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532}
533
Paul Bakkerec1b9842012-01-14 18:24:43 +0000534static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
Paul Bakker23986e52011-04-24 08:57:21 +0000535 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000537 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538
Paul Bakkera885d682011-01-20 16:35:05 +0000539 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000540 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541
542 padding_len = input[input_len - 1];
543
Paul Bakkera885d682011-01-20 16:35:05 +0000544 if( padding_len > input_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000545 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000546
Paul Bakkera885d682011-01-20 16:35:05 +0000547 for( i = input_len - padding_len; i < input_len; i++ )
548 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000549 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000550
551 *data_len = input_len - padding_len;
552
553 return 0;
554}
555
Paul Bakker23986e52011-04-24 08:57:21 +0000556int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000557{
Paul Bakkerff61a782011-06-09 15:42:02 +0000558 int ret = 0;
559
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000561 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562
563 *olen = 0;
564
Paul Bakker6132d0a2012-07-04 17:10:40 +0000565 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000566 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
567 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000568 {
569 return 0;
570 }
571
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
573 {
574 if( POLARSSL_ENCRYPT == ctx->operation )
575 {
576 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
577 ctx->unprocessed_len );
578 }
579 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
580 {
581 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000582 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583 }
584
585 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000586 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
587 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
588 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000590 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 }
592
593 /* Set output size for decryption */
594 if( POLARSSL_DECRYPT == ctx->operation )
595 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
596
597 /* Set output size for encryption */
598 *olen = cipher_get_block_size( ctx );
599 return 0;
600 }
601
Paul Bakkerff61a782011-06-09 15:42:02 +0000602 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603}
604
605#if defined(POLARSSL_SELF_TEST)
606
607#include <stdio.h>
608
609#define ASSERT(x) if (!(x)) { \
610 printf( "failed with %i at %s\n", value, (#x) ); \
611 return( 1 ); \
612}
613/*
614 * Checkup routine
615 */
616
617int cipher_self_test( int verbose )
618{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000619 ((void) verbose);
620
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 return( 0 );
622}
623
624#endif
625
626#endif