blob: d2c8ab37365cb05b5b2e9beacc4016d1fcaa178f [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
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200371int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
372{
373 if( NULL == ctx ||
374 POLARSSL_MODE_CBC != ctx->cipher_info->mode ||
375 POLARSSL_PADDING_PKCS7 != mode )
376 {
377 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
378 }
379
380 return 0;
381}
382
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
384{
385 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000386 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387
388 ctx->unprocessed_len = 0;
389
390 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
391
392 return 0;
393}
394
Paul Bakker23986e52011-04-24 08:57:21 +0000395int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
396 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397{
Paul Bakkerff61a782011-06-09 15:42:02 +0000398 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000399 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400
Paul Bakker68884e32013-01-07 18:20:04 +0100401 *olen = 0;
402
403 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000404 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000405 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000406 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407
Paul Bakker68884e32013-01-07 18:20:04 +0100408 if( input == output &&
409 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
410 {
411 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
412 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
Paul Bakkerfab5c822012-02-06 16:45:10 +0000414#if defined(POLARSSL_CIPHER_NULL_CIPHER)
415 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
416 {
Paul Bakkerfab5c822012-02-06 16:45:10 +0000417 *olen = ilen;
Paul Bakker68884e32013-01-07 18:20:04 +0100418
419 if( output == input )
420 return( 0 );
421
422 memcpy( output, input, ilen );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000423 return 0;
424 }
425#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
426
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
428 {
429 /*
430 * If there is not enough data for a full block, cache it.
431 */
432 if( ( ctx->operation == POLARSSL_DECRYPT &&
433 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
434 ( ctx->operation == POLARSSL_ENCRYPT &&
435 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
436 {
437 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
438 ilen );
439
440 ctx->unprocessed_len += ilen;
441 return 0;
442 }
443
444 /*
445 * Process cached data first
446 */
447 if( ctx->unprocessed_len != 0 )
448 {
449 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
450
451 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
452 copy_len );
453
Paul Bakkerff61a782011-06-09 15:42:02 +0000454 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000456 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000458 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459 }
460
461 *olen += cipher_get_block_size( ctx );
462 output += cipher_get_block_size( ctx );
463 ctx->unprocessed_len = 0;
464
465 input += copy_len;
466 ilen -= copy_len;
467 }
468
469 /*
470 * Cache final, incomplete block
471 */
472 if( 0 != ilen )
473 {
474 copy_len = ilen % cipher_get_block_size( ctx );
475 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
476 copy_len = cipher_get_block_size(ctx);
477
478 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
479 copy_len );
480
481 ctx->unprocessed_len += copy_len;
482 ilen -= copy_len;
483 }
484
485 /*
486 * Process remaining full blocks
487 */
488 if( ilen )
489 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000490 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
491 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000493 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494 }
495 *olen += ilen;
496 }
497
498 return 0;
499 }
500
Paul Bakker68884e32013-01-07 18:20:04 +0100501#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000502 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000503 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000504 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000505 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000506 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000507 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000508 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000509 }
510
511 *olen = ilen;
512
513 return 0;
514 }
Paul Bakker68884e32013-01-07 18:20:04 +0100515#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000516
Paul Bakker68884e32013-01-07 18:20:04 +0100517#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000518 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
519 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000520 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000521 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000522 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000523 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000524 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000525 }
526
527 *olen = ilen;
528
529 return 0;
530 }
Paul Bakker68884e32013-01-07 18:20:04 +0100531#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000532
Paul Bakkerff61a782011-06-09 15:42:02 +0000533 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534}
535
Paul Bakker23986e52011-04-24 08:57:21 +0000536static void add_pkcs_padding( unsigned char *output, size_t output_len,
537 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538{
Paul Bakker23986e52011-04-24 08:57:21 +0000539 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000540 unsigned char i = 0;
541
542 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000543 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544}
545
Paul Bakkerec1b9842012-01-14 18:24:43 +0000546static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
Paul Bakker23986e52011-04-24 08:57:21 +0000547 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000549 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000550
Paul Bakkera885d682011-01-20 16:35:05 +0000551 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000552 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000553
554 padding_len = input[input_len - 1];
555
Paul Bakkera885d682011-01-20 16:35:05 +0000556 if( padding_len > input_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000557 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558
Paul Bakkera885d682011-01-20 16:35:05 +0000559 for( i = input_len - padding_len; i < input_len; i++ )
560 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000561 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562
563 *data_len = input_len - padding_len;
564
565 return 0;
566}
567
Paul Bakker23986e52011-04-24 08:57:21 +0000568int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569{
Paul Bakkerff61a782011-06-09 15:42:02 +0000570 int ret = 0;
571
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000573 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574
575 *olen = 0;
576
Paul Bakker6132d0a2012-07-04 17:10:40 +0000577 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000578 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
579 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000580 {
581 return 0;
582 }
583
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
585 {
586 if( POLARSSL_ENCRYPT == ctx->operation )
587 {
588 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
589 ctx->unprocessed_len );
590 }
591 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
592 {
593 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000594 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 }
596
597 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000598 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
599 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
600 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000602 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603 }
604
605 /* Set output size for decryption */
606 if( POLARSSL_DECRYPT == ctx->operation )
607 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
608
609 /* Set output size for encryption */
610 *olen = cipher_get_block_size( ctx );
611 return 0;
612 }
613
Paul Bakkerff61a782011-06-09 15:42:02 +0000614 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615}
616
617#if defined(POLARSSL_SELF_TEST)
618
619#include <stdio.h>
620
621#define ASSERT(x) if (!(x)) { \
622 printf( "failed with %i at %s\n", value, (#x) ); \
623 return( 1 ); \
624}
625/*
626 * Checkup routine
627 */
628
629int cipher_self_test( int verbose )
630{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000631 ((void) verbose);
632
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633 return( 0 );
634}
635
636#endif
637
638#endif