blob: f7f3d1a32ea7a5b2b664da724c5b138e89d76dc3 [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
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200326 /*
327 * Ignore possible errors caused by a cipher mode that doesn't use padding
328 */
329 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
330
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 return 0;
332}
333
334int cipher_free_ctx( cipher_context_t *ctx )
335{
336 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000337 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
Paul Bakker343a8702011-06-09 14:27:58 +0000339 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000340
341 return 0;
342}
343
344int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
345 int key_length, const operation_t operation )
346{
347 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000348 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
350 ctx->key_length = key_length;
351 ctx->operation = operation;
352
Paul Bakkerfab5c822012-02-06 16:45:10 +0000353#if defined(POLARSSL_CIPHER_NULL_CIPHER)
354 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
355 return 0;
356#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
357
Paul Bakker343a8702011-06-09 14:27:58 +0000358 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000359 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000360 */
361 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000362 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000363 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
364 {
365 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000367 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368
Paul Bakker343a8702011-06-09 14:27:58 +0000369 if( POLARSSL_DECRYPT == operation )
370 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371 ctx->key_length );
372
Paul Bakkerff61a782011-06-09 15:42:02 +0000373 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374}
375
376int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
377{
378 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000379 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
381 ctx->unprocessed_len = 0;
382
383 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
384
385 return 0;
386}
387
Paul Bakker23986e52011-04-24 08:57:21 +0000388int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
389 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390{
Paul Bakkerff61a782011-06-09 15:42:02 +0000391 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000392 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000393
Paul Bakker68884e32013-01-07 18:20:04 +0100394 *olen = 0;
395
396 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000397 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000398 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000399 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400
Paul Bakker68884e32013-01-07 18:20:04 +0100401 if( input == output &&
402 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
403 {
404 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
405 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
Paul Bakkerfab5c822012-02-06 16:45:10 +0000407#if defined(POLARSSL_CIPHER_NULL_CIPHER)
408 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
409 {
Paul Bakkerfab5c822012-02-06 16:45:10 +0000410 *olen = ilen;
Paul Bakker68884e32013-01-07 18:20:04 +0100411
412 if( output == input )
413 return( 0 );
414
415 memcpy( output, input, ilen );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000416 return 0;
417 }
418#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
419
Paul Bakker8123e9d2011-01-06 15:37:30 +0000420 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
421 {
422 /*
423 * If there is not enough data for a full block, cache it.
424 */
425 if( ( ctx->operation == POLARSSL_DECRYPT &&
426 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
427 ( ctx->operation == POLARSSL_ENCRYPT &&
428 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
429 {
430 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
431 ilen );
432
433 ctx->unprocessed_len += ilen;
434 return 0;
435 }
436
437 /*
438 * Process cached data first
439 */
440 if( ctx->unprocessed_len != 0 )
441 {
442 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
443
444 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
445 copy_len );
446
Paul Bakkerff61a782011-06-09 15:42:02 +0000447 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000449 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000451 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452 }
453
454 *olen += cipher_get_block_size( ctx );
455 output += cipher_get_block_size( ctx );
456 ctx->unprocessed_len = 0;
457
458 input += copy_len;
459 ilen -= copy_len;
460 }
461
462 /*
463 * Cache final, incomplete block
464 */
465 if( 0 != ilen )
466 {
467 copy_len = ilen % cipher_get_block_size( ctx );
468 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
469 copy_len = cipher_get_block_size(ctx);
470
471 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
472 copy_len );
473
474 ctx->unprocessed_len += copy_len;
475 ilen -= copy_len;
476 }
477
478 /*
479 * Process remaining full blocks
480 */
481 if( ilen )
482 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000483 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
484 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000485 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000486 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487 }
488 *olen += ilen;
489 }
490
491 return 0;
492 }
493
Paul Bakker68884e32013-01-07 18:20:04 +0100494#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000495 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000496 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000497 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000498 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000499 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000500 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000501 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000502 }
503
504 *olen = ilen;
505
506 return 0;
507 }
Paul Bakker68884e32013-01-07 18:20:04 +0100508#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000509
Paul Bakker68884e32013-01-07 18:20:04 +0100510#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000511 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
512 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000513 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000514 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000515 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000516 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000517 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000518 }
519
520 *olen = ilen;
521
522 return 0;
523 }
Paul Bakker68884e32013-01-07 18:20:04 +0100524#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000525
Paul Bakkerff61a782011-06-09 15:42:02 +0000526 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527}
528
Paul Bakker23986e52011-04-24 08:57:21 +0000529static void add_pkcs_padding( unsigned char *output, size_t output_len,
530 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531{
Paul Bakker23986e52011-04-24 08:57:21 +0000532 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533 unsigned char i = 0;
534
535 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000536 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000537}
538
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200539static int get_pkcs_padding( unsigned char *input, size_t input_len,
540 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000542 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000543
Paul Bakkera885d682011-01-20 16:35:05 +0000544 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000545 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000546
547 padding_len = input[input_len - 1];
548
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200549 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000550 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551
Paul Bakkera885d682011-01-20 16:35:05 +0000552 for( i = input_len - padding_len; i < input_len; i++ )
553 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000554 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555
556 *data_len = input_len - padding_len;
557
558 return 0;
559}
560
Paul Bakker23986e52011-04-24 08:57:21 +0000561int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562{
Paul Bakkerff61a782011-06-09 15:42:02 +0000563 int ret = 0;
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000566 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567
568 *olen = 0;
569
Paul Bakker6132d0a2012-07-04 17:10:40 +0000570 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000571 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
572 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000573 {
574 return 0;
575 }
576
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
578 {
579 if( POLARSSL_ENCRYPT == ctx->operation )
580 {
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200581 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582 ctx->unprocessed_len );
583 }
584 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
585 {
586 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000587 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588 }
589
590 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000591 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
592 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
593 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000595 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 }
597
598 /* Set output size for decryption */
599 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200600 return ctx->get_padding( output, cipher_get_block_size( ctx ),
601 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
603 /* Set output size for encryption */
604 *olen = cipher_get_block_size( ctx );
605 return 0;
606 }
607
Paul Bakkerff61a782011-06-09 15:42:02 +0000608 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609}
610
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200611int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
612{
613 if( NULL == ctx ||
614 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
615 {
616 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
617 }
618
619 if( POLARSSL_PADDING_PKCS7 == mode )
620 {
621 ctx->add_padding = add_pkcs_padding;
622 ctx->get_padding = get_pkcs_padding;
623 return 0;
624 }
625
626 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
627}
628
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629#if defined(POLARSSL_SELF_TEST)
630
631#include <stdio.h>
632
633#define ASSERT(x) if (!(x)) { \
634 printf( "failed with %i at %s\n", value, (#x) ); \
635 return( 1 ); \
636}
637/*
638 * Checkup routine
639 */
640
641int cipher_self_test( int verbose )
642{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000643 ((void) verbose);
644
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645 return( 0 );
646}
647
648#endif
649
650#endif