4 * \brief Generic cipher wrapper for PolarSSL
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2012, Brainspark B.V.
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
13 * All rights reserved.
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.
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.
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.
32 #if defined(POLARSSL_CIPHER_C)
35 #include "cipher_wrap.h"
39 #if defined _MSC_VER && !defined strcasecmp
40 #define strcasecmp _stricmp
43 static const int supported_ciphers
[] = {
45 #if defined(POLARSSL_AES_C)
46 POLARSSL_CIPHER_AES_128_CBC
,
47 POLARSSL_CIPHER_AES_192_CBC
,
48 POLARSSL_CIPHER_AES_256_CBC
,
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) */
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) */
62 #endif /* defined(POLARSSL_AES_C) */
64 #if defined(POLARSSL_CAMELLIA_C)
65 POLARSSL_CIPHER_CAMELLIA_128_CBC
,
66 POLARSSL_CIPHER_CAMELLIA_192_CBC
,
67 POLARSSL_CIPHER_CAMELLIA_256_CBC
,
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) */
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) */
81 #endif /* defined(POLARSSL_CAMELLIA_C) */
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) */
89 #if defined(POLARSSL_BLOWFISH_C)
90 POLARSSL_CIPHER_BLOWFISH_CBC
,
92 #if defined(POLARSSL_CIPHER_MODE_CFB)
93 POLARSSL_CIPHER_BLOWFISH_CFB64
,
94 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
96 #if defined(POLARSSL_CIPHER_MODE_CTR)
97 POLARSSL_CIPHER_BLOWFISH_CTR
,
98 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
100 #endif /* defined(POLARSSL_BLOWFISH_C) */
102 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
103 POLARSSL_CIPHER_NULL
,
104 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
109 const int *cipher_list( void )
111 return supported_ciphers
;
114 const cipher_info_t
*cipher_info_from_type( const cipher_type_t cipher_type
)
116 /* Find static cipher information */
117 switch ( cipher_type
)
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
;
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) */
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) */
147 #if defined(POLARSSL_CAMELLIA_C)
148 case POLARSSL_CIPHER_CAMELLIA_128_CBC
:
149 return &camellia_128_cbc_info
;
150 case POLARSSL_CIPHER_CAMELLIA_192_CBC
:
151 return &camellia_192_cbc_info
;
152 case POLARSSL_CIPHER_CAMELLIA_256_CBC
:
153 return &camellia_256_cbc_info
;
155 #if defined(POLARSSL_CIPHER_MODE_CFB)
156 case POLARSSL_CIPHER_CAMELLIA_128_CFB128
:
157 return &camellia_128_cfb128_info
;
158 case POLARSSL_CIPHER_CAMELLIA_192_CFB128
:
159 return &camellia_192_cfb128_info
;
160 case POLARSSL_CIPHER_CAMELLIA_256_CFB128
:
161 return &camellia_256_cfb128_info
;
162 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
164 #if defined(POLARSSL_CIPHER_MODE_CTR)
165 case POLARSSL_CIPHER_CAMELLIA_128_CTR
:
166 return &camellia_128_ctr_info
;
167 case POLARSSL_CIPHER_CAMELLIA_192_CTR
:
168 return &camellia_192_ctr_info
;
169 case POLARSSL_CIPHER_CAMELLIA_256_CTR
:
170 return &camellia_256_ctr_info
;
171 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
175 #if defined(POLARSSL_DES_C)
176 case POLARSSL_CIPHER_DES_CBC
:
177 return &des_cbc_info
;
178 case POLARSSL_CIPHER_DES_EDE_CBC
:
179 return &des_ede_cbc_info
;
180 case POLARSSL_CIPHER_DES_EDE3_CBC
:
181 return &des_ede3_cbc_info
;
184 #if defined(POLARSSL_BLOWFISH_C)
185 case POLARSSL_CIPHER_BLOWFISH_CBC
:
186 return &blowfish_cbc_info
;
188 #if defined(POLARSSL_CIPHER_MODE_CFB)
189 case POLARSSL_CIPHER_BLOWFISH_CFB64
:
190 return &blowfish_cfb64_info
;
191 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
194 case POLARSSL_CIPHER_BLOWFISH_CTR
:
195 return &blowfish_ctr_info
;
196 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
200 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
201 case POLARSSL_CIPHER_NULL
:
202 return &null_cipher_info
;
203 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
210 const cipher_info_t
*cipher_info_from_string( const char *cipher_name
)
212 if( NULL
== cipher_name
)
215 /* Get the appropriate cipher information */
216 #if defined(POLARSSL_CAMELLIA_C)
217 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name
) )
218 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC
);
219 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name
) )
220 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC
);
221 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name
) )
222 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC
);
224 #if defined(POLARSSL_CIPHER_MODE_CFB)
225 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name
) )
226 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128
);
227 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name
) )
228 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128
);
229 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name
) )
230 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128
);
231 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
233 #if defined(POLARSSL_CIPHER_MODE_CTR)
234 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name
) )
235 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR
);
236 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name
) )
237 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR
);
238 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name
) )
239 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR
);
240 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
243 #if defined(POLARSSL_AES_C)
244 if( !strcasecmp( "AES-128-CBC", cipher_name
) )
245 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC
);
246 if( !strcasecmp( "AES-192-CBC", cipher_name
) )
247 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC
);
248 if( !strcasecmp( "AES-256-CBC", cipher_name
) )
249 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC
);
251 #if defined(POLARSSL_CIPHER_MODE_CFB)
252 if( !strcasecmp( "AES-128-CFB128", cipher_name
) )
253 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128
);
254 if( !strcasecmp( "AES-192-CFB128", cipher_name
) )
255 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128
);
256 if( !strcasecmp( "AES-256-CFB128", cipher_name
) )
257 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128
);
258 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
260 #if defined(POLARSSL_CIPHER_MODE_CTR)
261 if( !strcasecmp( "AES-128-CTR", cipher_name
) )
262 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR
);
263 if( !strcasecmp( "AES-192-CTR", cipher_name
) )
264 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR
);
265 if( !strcasecmp( "AES-256-CTR", cipher_name
) )
266 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR
);
267 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
270 #if defined(POLARSSL_DES_C)
271 if( !strcasecmp( "DES-CBC", cipher_name
) )
272 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC
);
273 if( !strcasecmp( "DES-EDE-CBC", cipher_name
) )
274 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC
);
275 if( !strcasecmp( "DES-EDE3-CBC", cipher_name
) )
276 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC
);
279 #if defined(POLARSSL_BLOWFISH_C)
280 if( !strcasecmp( "BLOWFISH-CBC", cipher_name
) )
281 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC
);
283 #if defined(POLARSSL_CIPHER_MODE_CFB)
284 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name
) )
285 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64
);
286 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
288 #if defined(POLARSSL_CIPHER_MODE_CTR)
289 if( !strcasecmp( "BLOWFISH-CTR", cipher_name
) )
290 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR
);
291 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
294 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
295 if( !strcasecmp( "NULL", cipher_name
) )
296 return cipher_info_from_type( POLARSSL_CIPHER_NULL
);
297 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
302 int cipher_init_ctx( cipher_context_t
*ctx
, const cipher_info_t
*cipher_info
)
304 if( NULL
== cipher_info
|| NULL
== ctx
)
305 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
307 memset( ctx
, 0, sizeof( cipher_context_t
) );
309 if( NULL
== ( ctx
->cipher_ctx
= cipher_info
->base
->ctx_alloc_func() ) )
310 return POLARSSL_ERR_CIPHER_ALLOC_FAILED
;
312 ctx
->cipher_info
= cipher_info
;
317 int cipher_free_ctx( cipher_context_t
*ctx
)
319 if( ctx
== NULL
|| ctx
->cipher_info
== NULL
)
320 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
322 ctx
->cipher_info
->base
->ctx_free_func( ctx
->cipher_ctx
);
327 int cipher_setkey( cipher_context_t
*ctx
, const unsigned char *key
,
328 int key_length
, const operation_t operation
)
330 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
)
331 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
333 ctx
->key_length
= key_length
;
334 ctx
->operation
= operation
;
336 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
337 if( ctx
->cipher_info
->mode
== POLARSSL_MODE_NULL
)
339 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
342 * For CFB and CTR mode always use the encryption key schedule
344 if( POLARSSL_ENCRYPT
== operation
||
345 POLARSSL_MODE_CFB
== ctx
->cipher_info
->mode
||
346 POLARSSL_MODE_CTR
== ctx
->cipher_info
->mode
)
348 return ctx
->cipher_info
->base
->setkey_enc_func( ctx
->cipher_ctx
, key
,
352 if( POLARSSL_DECRYPT
== operation
)
353 return ctx
->cipher_info
->base
->setkey_dec_func( ctx
->cipher_ctx
, key
,
356 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
359 int cipher_reset( cipher_context_t
*ctx
, const unsigned char *iv
)
361 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== iv
)
362 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
364 ctx
->unprocessed_len
= 0;
366 memcpy( ctx
->iv
, iv
, cipher_get_iv_size( ctx
) );
371 int cipher_update( cipher_context_t
*ctx
, const unsigned char *input
, size_t ilen
,
372 unsigned char *output
, size_t *olen
)
377 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== olen
||
380 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
385 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
386 if( ctx
->cipher_info
->mode
== POLARSSL_MODE_NULL
)
388 memcpy( output
, input
, ilen
);
392 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
394 if( ctx
->cipher_info
->mode
== POLARSSL_MODE_CBC
)
397 * If there is not enough data for a full block, cache it.
399 if( ( ctx
->operation
== POLARSSL_DECRYPT
&&
400 ilen
+ ctx
->unprocessed_len
<= cipher_get_block_size( ctx
) ) ||
401 ( ctx
->operation
== POLARSSL_ENCRYPT
&&
402 ilen
+ ctx
->unprocessed_len
< cipher_get_block_size( ctx
) ) )
404 memcpy( &( ctx
->unprocessed_data
[ctx
->unprocessed_len
] ), input
,
407 ctx
->unprocessed_len
+= ilen
;
412 * Process cached data first
414 if( ctx
->unprocessed_len
!= 0 )
416 copy_len
= cipher_get_block_size( ctx
) - ctx
->unprocessed_len
;
418 memcpy( &( ctx
->unprocessed_data
[ctx
->unprocessed_len
] ), input
,
421 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
422 ctx
->operation
, cipher_get_block_size( ctx
), ctx
->iv
,
423 ctx
->unprocessed_data
, output
) ) )
428 *olen
+= cipher_get_block_size( ctx
);
429 output
+= cipher_get_block_size( ctx
);
430 ctx
->unprocessed_len
= 0;
437 * Cache final, incomplete block
441 copy_len
= ilen
% cipher_get_block_size( ctx
);
442 if( copy_len
== 0 && ctx
->operation
== POLARSSL_DECRYPT
)
443 copy_len
= cipher_get_block_size(ctx
);
445 memcpy( ctx
->unprocessed_data
, &( input
[ilen
- copy_len
] ),
448 ctx
->unprocessed_len
+= copy_len
;
453 * Process remaining full blocks
457 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
458 ctx
->operation
, ilen
, ctx
->iv
, input
, output
) ) )
468 if( ctx
->cipher_info
->mode
== POLARSSL_MODE_CFB
)
470 if( 0 != ( ret
= ctx
->cipher_info
->base
->cfb_func( ctx
->cipher_ctx
,
471 ctx
->operation
, ilen
, &ctx
->unprocessed_len
, ctx
->iv
,
482 if( ctx
->cipher_info
->mode
== POLARSSL_MODE_CTR
)
484 if( 0 != ( ret
= ctx
->cipher_info
->base
->ctr_func( ctx
->cipher_ctx
,
485 ilen
, &ctx
->unprocessed_len
, ctx
->iv
,
486 ctx
->unprocessed_data
, input
, output
) ) )
496 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
499 static void add_pkcs_padding( unsigned char *output
, size_t output_len
,
502 size_t padding_len
= output_len
- data_len
;
505 for( i
= 0; i
< padding_len
; i
++ )
506 output
[data_len
+ i
] = (unsigned char) padding_len
;
509 static int get_pkcs_padding( unsigned char *input
, unsigned int input_len
,
512 unsigned int i
, padding_len
= 0;
514 if( NULL
== input
|| NULL
== data_len
)
515 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
517 padding_len
= input
[input_len
- 1];
519 if( padding_len
> input_len
)
520 return POLARSSL_ERR_CIPHER_INVALID_PADDING
;
522 for( i
= input_len
- padding_len
; i
< input_len
; i
++ )
523 if( input
[i
] != padding_len
)
524 return POLARSSL_ERR_CIPHER_INVALID_PADDING
;
526 *data_len
= input_len
- padding_len
;
531 int cipher_finish( cipher_context_t
*ctx
, unsigned char *output
, size_t *olen
)
535 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== olen
)
536 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
;
540 if( POLARSSL_MODE_CFB
== ctx
->cipher_info
->mode
||
541 POLARSSL_MODE_CTR
== ctx
->cipher_info
->mode
||
542 POLARSSL_MODE_NULL
== ctx
->cipher_info
->mode
)
547 if( POLARSSL_MODE_CBC
== ctx
->cipher_info
->mode
)
549 if( POLARSSL_ENCRYPT
== ctx
->operation
)
551 add_pkcs_padding( ctx
->unprocessed_data
, cipher_get_iv_size( ctx
),
552 ctx
->unprocessed_len
);
554 else if ( cipher_get_block_size( ctx
) != ctx
->unprocessed_len
)
556 /* For decrypt operations, expect a full block */
557 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
;
561 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
562 ctx
->operation
, cipher_get_block_size( ctx
), ctx
->iv
,
563 ctx
->unprocessed_data
, output
) ) )
568 /* Set output size for decryption */
569 if( POLARSSL_DECRYPT
== ctx
->operation
)
570 return get_pkcs_padding( output
, cipher_get_block_size( ctx
), olen
);
572 /* Set output size for encryption */
573 *olen
= cipher_get_block_size( ctx
);
577 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
580 #if defined(POLARSSL_SELF_TEST)
584 #define ASSERT(x) if (!(x)) { \
585 printf( "failed with %i at %s\n", value, (#x) ); \
592 int cipher_self_test( int verbose
)