blob: bbde94400d62309c51bdf5520eefd87dfb2b955a [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
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200529/*
530 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
531 */
Paul Bakker23986e52011-04-24 08:57:21 +0000532static void add_pkcs_padding( unsigned char *output, size_t output_len,
533 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534{
Paul Bakker23986e52011-04-24 08:57:21 +0000535 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536 unsigned char i = 0;
537
538 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000539 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000540}
541
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200542static int get_pkcs_padding( unsigned char *input, size_t input_len,
543 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000544{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000545 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000546
Paul Bakkera885d682011-01-20 16:35:05 +0000547 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000548 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000549
550 padding_len = input[input_len - 1];
551
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200552 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000553 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554
Paul Bakkera885d682011-01-20 16:35:05 +0000555 for( i = input_len - padding_len; i < input_len; i++ )
556 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000557 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000558
559 *data_len = input_len - padding_len;
560
561 return 0;
562}
563
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200564/*
565 * One and zeros padding: fill with 80 00 ... 00
566 */
567static void add_one_and_zeros_padding( unsigned char *output,
568 size_t output_len, size_t data_len )
569{
570 size_t padding_len = output_len - data_len;
571 unsigned char i = 0;
572
573 output[data_len] = 0x80;
574 for( i = 1; i < padding_len; i++ )
575 output[data_len + i] = 0x00;
576}
577
578static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
579 size_t *data_len )
580{
581 unsigned char *p = input + input_len - 1;
582
583 if( NULL == input || NULL == data_len )
584 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
585
586 while( *p == 0x00 && p > input )
587 --p;
588
589 if( *p != 0x80 )
590 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
591
592 *data_len = p - input;
593
594 return 0;
595}
596
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200597/*
598 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
599 */
600static void add_zeros_and_len_padding( unsigned char *output,
601 size_t output_len, size_t data_len )
602{
603 size_t padding_len = output_len - data_len;
604 unsigned char i = 0;
605
606 for( i = 1; i < padding_len; i++ )
607 output[data_len + i - 1] = 0x00;
608 output[output_len - 1] = (unsigned char) padding_len;
609}
610
611static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
612 size_t *data_len )
613{
614 unsigned int i, padding_len = 0;
615
616 if( NULL == input || NULL == data_len )
617 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
618
619 padding_len = input[input_len - 1];
620
621 if( padding_len > input_len || padding_len == 0 )
622 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
623
624 for( i = input_len - padding_len; i < input_len - 1; i++ )
625 if( input[i] != 0x00 )
626 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
627
628 *data_len = input_len - padding_len;
629
630 return 0;
631}
632
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200633/*
634 * Zero padding: fill with 00 ... 00
635 */
636static void add_zeros_padding( unsigned char *output,
637 size_t output_len, size_t data_len )
638{
639 unsigned char i;
640
641 for( i = data_len; i < output_len; i++ )
642 output[i] = 0x00;
643}
644
645static int get_zeros_padding( unsigned char *input, size_t input_len,
646 size_t *data_len )
647{
648 unsigned char *p = input + input_len - 1;
649 if( NULL == input || NULL == data_len )
650 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
651
652 while( *p == 0x00 && p > input )
653 --p;
654
655 *data_len = *p == 0x00 ? 0 : p - input + 1;
656
657 return 0;
658}
659
Paul Bakker23986e52011-04-24 08:57:21 +0000660int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661{
Paul Bakkerff61a782011-06-09 15:42:02 +0000662 int ret = 0;
663
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000665 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666
667 *olen = 0;
668
Paul Bakker6132d0a2012-07-04 17:10:40 +0000669 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000670 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
671 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000672 {
673 return 0;
674 }
675
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
677 {
678 if( POLARSSL_ENCRYPT == ctx->operation )
679 {
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200680 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 ctx->unprocessed_len );
682 }
683 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
684 {
685 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000686 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687 }
688
689 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000690 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
691 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
692 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000694 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695 }
696
697 /* Set output size for decryption */
698 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200699 return ctx->get_padding( output, cipher_get_block_size( ctx ),
700 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701
702 /* Set output size for encryption */
703 *olen = cipher_get_block_size( ctx );
704 return 0;
705 }
706
Paul Bakkerff61a782011-06-09 15:42:02 +0000707 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708}
709
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200710int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
711{
712 if( NULL == ctx ||
713 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
714 {
715 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
716 }
717
718 if( POLARSSL_PADDING_PKCS7 == mode )
719 {
720 ctx->add_padding = add_pkcs_padding;
721 ctx->get_padding = get_pkcs_padding;
722 return 0;
723 }
724
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200725 if( POLARSSL_PADDING_ONE_AND_ZEROS == mode )
726 {
727 ctx->add_padding = add_one_and_zeros_padding;
728 ctx->get_padding = get_one_and_zeros_padding;
729 return 0;
730 }
731
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200732 if( POLARSSL_PADDING_ZEROS_AND_LEN == mode )
733 {
734 ctx->add_padding = add_zeros_and_len_padding;
735 ctx->get_padding = get_zeros_and_len_padding;
736 return 0;
737 }
738
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200739 if( POLARSSL_PADDING_ZEROS == mode )
740 {
741 ctx->add_padding = add_zeros_padding;
742 ctx->get_padding = get_zeros_padding;
743 return 0;
744 }
745
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200746 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
747}
748
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749#if defined(POLARSSL_SELF_TEST)
750
751#include <stdio.h>
752
753#define ASSERT(x) if (!(x)) { \
754 printf( "failed with %i at %s\n", value, (#x) ); \
755 return( 1 ); \
756}
757/*
758 * Checkup routine
759 */
760
761int cipher_self_test( int verbose )
762{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000763 ((void) verbose);
764
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765 return( 0 );
766}
767
768#endif
769
770#endif