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