blob: 697ff54306fb558f35e2d79eb7861679b5f43ebf [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 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
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
37#include <string.h>
38#include <stdlib.h>
39
Paul Bakker72f62662011-01-16 21:27:44 +000040static const int supported_ciphers[] = {
41
42#if defined(POLARSSL_AES_C)
43 POLARSSL_CIPHER_AES_128_CBC,
44 POLARSSL_CIPHER_AES_192_CBC,
45 POLARSSL_CIPHER_AES_256_CBC,
46#endif /* defined(POLARSSL_AES_C) */
47
48#if defined(POLARSSL_CAMELLIA_C)
49 POLARSSL_CIPHER_CAMELLIA_128_CBC,
50 POLARSSL_CIPHER_CAMELLIA_192_CBC,
51 POLARSSL_CIPHER_CAMELLIA_256_CBC,
52#endif /* defined(POLARSSL_CAMELLIA_C) */
53
54#if defined(POLARSSL_DES_C)
55 POLARSSL_CIPHER_DES_CBC,
56 POLARSSL_CIPHER_DES_EDE_CBC,
57 POLARSSL_CIPHER_DES_EDE3_CBC,
58#endif /* defined(POLARSSL_DES_C) */
59
60 0
61};
62
63const int *cipher_list( void )
64{
65 return supported_ciphers;
66}
67
Paul Bakker8123e9d2011-01-06 15:37:30 +000068const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
69{
70 /* Find static cipher information */
71 switch ( cipher_type )
72 {
73#if defined(POLARSSL_AES_C)
74 case POLARSSL_CIPHER_AES_128_CBC:
75 return &aes_128_cbc_info;
76 case POLARSSL_CIPHER_AES_192_CBC:
77 return &aes_192_cbc_info;
78 case POLARSSL_CIPHER_AES_256_CBC:
79 return &aes_256_cbc_info;
80#endif
81
82#if defined(POLARSSL_CAMELLIA_C)
83 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
84 return &camellia_128_cbc_info;
85 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
86 return &camellia_192_cbc_info;
87 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
88 return &camellia_256_cbc_info;
89#endif
90
91#if defined(POLARSSL_DES_C)
92 case POLARSSL_CIPHER_DES_CBC:
93 return &des_cbc_info;
94 case POLARSSL_CIPHER_DES_EDE_CBC:
95 return &des_ede_cbc_info;
96 case POLARSSL_CIPHER_DES_EDE3_CBC:
97 return &des_ede3_cbc_info;
98#endif
99
100 default:
101 return NULL;
102 }
103}
104
105const cipher_info_t *cipher_info_from_string( const char *cipher_name )
106{
107 if( NULL == cipher_name )
108 return NULL;
109
110 /* Get the appropriate digest information */
111#if defined(POLARSSL_CAMELLIA_C)
112 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
113 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
114 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
115 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
116 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
117 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
118#endif
119#if defined(POLARSSL_AES_C)
120 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
121 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
122 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
123 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
124 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
125 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
126#endif
127#if defined(POLARSSL_DES_C)
128 if( !strcasecmp( "DES-CBC", cipher_name ) )
129 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
130 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
131 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
132 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
133 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
134#endif
135 return NULL;
136}
137
138int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
139{
140 if( NULL == cipher_info || NULL == ctx )
141 return 1;
142
143 memset( ctx, 0, sizeof( ctx ) );
144
145 if( NULL == ( ctx->cipher_ctx = cipher_info->ctx_alloc_func() ) )
146 return 2;
147
148 ctx->cipher_info = cipher_info;
149
150 return 0;
151}
152
153int cipher_free_ctx( cipher_context_t *ctx )
154{
155 if( ctx == NULL || ctx->cipher_info == NULL )
156 return 1;
157
158 ctx->cipher_info->ctx_free_func( ctx->cipher_ctx );
159
160 return 0;
161}
162
163int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
164 int key_length, const operation_t operation )
165{
166 if( NULL == ctx || NULL == ctx->cipher_info )
167 return 1;
168
169 ctx->key_length = key_length;
170 ctx->operation = operation;
171
172 if (POLARSSL_ENCRYPT == operation)
173 return ctx->cipher_info->setkey_enc_func( ctx->cipher_ctx, key,
174 ctx->key_length );
175
176 if (POLARSSL_DECRYPT == operation)
177 return ctx->cipher_info->setkey_dec_func( ctx->cipher_ctx, key,
178 ctx->key_length );
179
180 return 1;
181}
182
183int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
184{
185 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
186 return 1;
187
188 ctx->unprocessed_len = 0;
189
190 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
191
192 return 0;
193}
194
195int cipher_update( cipher_context_t *ctx, const unsigned char *input, int ilen,
196 unsigned char *output, int *olen )
197{
198 int copy_len = 0;
199
Paul Bakkera885d682011-01-20 16:35:05 +0000200 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
201 input == output )
202 {
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203 return 1;
Paul Bakkera885d682011-01-20 16:35:05 +0000204 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205
206 *olen = 0;
207
208 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
209 {
210 /*
211 * If there is not enough data for a full block, cache it.
212 */
213 if( ( ctx->operation == POLARSSL_DECRYPT &&
214 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
215 ( ctx->operation == POLARSSL_ENCRYPT &&
216 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
217 {
218 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
219 ilen );
220
221 ctx->unprocessed_len += ilen;
222 return 0;
223 }
224
225 /*
226 * Process cached data first
227 */
228 if( ctx->unprocessed_len != 0 )
229 {
230 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
231
232 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
233 copy_len );
234
235 if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
236 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
237 ctx->unprocessed_data, output) )
238 {
239 return 1;
240 }
241
242 *olen += cipher_get_block_size( ctx );
243 output += cipher_get_block_size( ctx );
244 ctx->unprocessed_len = 0;
245
246 input += copy_len;
247 ilen -= copy_len;
248 }
249
250 /*
251 * Cache final, incomplete block
252 */
253 if( 0 != ilen )
254 {
255 copy_len = ilen % cipher_get_block_size( ctx );
256 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
257 copy_len = cipher_get_block_size(ctx);
258
259 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
260 copy_len );
261
262 ctx->unprocessed_len += copy_len;
263 ilen -= copy_len;
264 }
265
266 /*
267 * Process remaining full blocks
268 */
269 if( ilen )
270 {
271 if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
272 ctx->operation, ilen, ctx->iv, input, output ) )
273 {
274 return 1;
275 }
276 *olen += ilen;
277 }
278
279 return 0;
280 }
281
282 return 1;
283}
284
285static void add_pkcs_padding( unsigned char *output, unsigned char output_len,
286 int data_len )
287{
288 unsigned char padding_len = output_len - data_len;
289 unsigned char i = 0;
290
291 for( i = 0; i < padding_len; i++ )
292 output[data_len + i] = padding_len;
293}
294
295static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
296 int *data_len)
297{
298 int i = 0;
299 unsigned char padding_len = 0;
300
Paul Bakkera885d682011-01-20 16:35:05 +0000301 if( NULL == input || NULL == data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 return 1;
303
304 padding_len = input[input_len - 1];
305
Paul Bakkera885d682011-01-20 16:35:05 +0000306 if( padding_len > input_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307 return 2;
308
Paul Bakkera885d682011-01-20 16:35:05 +0000309 for( i = input_len - padding_len; i < input_len; i++ )
310 if( input[i] != padding_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 return 2;
312
313 *data_len = input_len - padding_len;
314
315 return 0;
316}
317
318int cipher_finish( cipher_context_t *ctx, unsigned char *output, int *olen)
319{
320 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
321 return 1;
322
323 *olen = 0;
324
325 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
326 {
327 if( POLARSSL_ENCRYPT == ctx->operation )
328 {
329 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
330 ctx->unprocessed_len );
331 }
332 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
333 {
334 /* For decrypt operations, expect a full block */
335 return 1;
336 }
337
338 /* cipher block */
339 if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, ctx->operation,
340 cipher_get_block_size( ctx ), ctx->iv, ctx->unprocessed_data,
341 output ) )
342 {
343 return 1;
344 }
345
346 /* Set output size for decryption */
347 if( POLARSSL_DECRYPT == ctx->operation )
348 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
349
350 /* Set output size for encryption */
351 *olen = cipher_get_block_size( ctx );
352 return 0;
353 }
354
355 return 1;
356}
357
358#if defined(POLARSSL_SELF_TEST)
359
360#include <stdio.h>
361
362#define ASSERT(x) if (!(x)) { \
363 printf( "failed with %i at %s\n", value, (#x) ); \
364 return( 1 ); \
365}
366/*
367 * Checkup routine
368 */
369
370int cipher_self_test( int verbose )
371{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000372 ((void) verbose);
373
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 return( 0 );
375}
376
377#endif
378
379#endif