PolarSSL v1.1.5
cipher.c
Go to the documentation of this file.
1 
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 <stdlib.h>
38 
39 #if defined _MSC_VER && !defined strcasecmp
40 #define strcasecmp _stricmp
41 #endif
42 
43 static const int supported_ciphers[] = {
44 
45 #if defined(POLARSSL_AES_C)
49 
50 #if defined(POLARSSL_CIPHER_MODE_CFB)
54 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
55 
56 #if defined(POLARSSL_CIPHER_MODE_CTR)
60 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
61 
62 #endif /* defined(POLARSSL_AES_C) */
63 
64 #if defined(POLARSSL_CAMELLIA_C)
68 
69 #if defined(POLARSSL_CIPHER_MODE_CFB)
73 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
74 
75 #if defined(POLARSSL_CIPHER_MODE_CTR)
79 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
80 
81 #endif /* defined(POLARSSL_CAMELLIA_C) */
82 
83 #if defined(POLARSSL_DES_C)
87 #endif /* defined(POLARSSL_DES_C) */
88 
89  0
90 };
91 
92 const int *cipher_list( void )
93 {
94  return supported_ciphers;
95 }
96 
97 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
98 {
99  /* Find static cipher information */
100  switch ( cipher_type )
101  {
102 #if defined(POLARSSL_AES_C)
104  return &aes_128_cbc_info;
106  return &aes_192_cbc_info;
108  return &aes_256_cbc_info;
109 
110 #if defined(POLARSSL_CIPHER_MODE_CFB)
112  return &aes_128_cfb128_info;
114  return &aes_192_cfb128_info;
116  return &aes_256_cfb128_info;
117 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
118 
119 #if defined(POLARSSL_CIPHER_MODE_CTR)
121  return &aes_128_ctr_info;
123  return &aes_192_ctr_info;
125  return &aes_256_ctr_info;
126 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
127 
128 #endif
129 
130 #if defined(POLARSSL_CAMELLIA_C)
132  return &camellia_128_cbc_info;
134  return &camellia_192_cbc_info;
136  return &camellia_256_cbc_info;
137 
138 #if defined(POLARSSL_CIPHER_MODE_CFB)
140  return &camellia_128_cfb128_info;
142  return &camellia_192_cfb128_info;
144  return &camellia_256_cfb128_info;
145 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
146 
147 #if defined(POLARSSL_CIPHER_MODE_CTR)
149  return &camellia_128_ctr_info;
151  return &camellia_192_ctr_info;
153  return &camellia_256_ctr_info;
154 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
155 
156 #endif
157 
158 #if defined(POLARSSL_DES_C)
160  return &des_cbc_info;
162  return &des_ede_cbc_info;
164  return &des_ede3_cbc_info;
165 #endif
166 
167  default:
168  return NULL;
169  }
170 }
171 
172 const cipher_info_t *cipher_info_from_string( const char *cipher_name )
173 {
174  if( NULL == cipher_name )
175  return NULL;
176 
177  /* Get the appropriate cipher information */
178 #if defined(POLARSSL_CAMELLIA_C)
179  if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
181  if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
183  if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
185 
186 #if defined(POLARSSL_CIPHER_MODE_CFB)
187  if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
189  if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
191  if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
193 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
194 
195 #if defined(POLARSSL_CIPHER_MODE_CTR)
196  if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
198  if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
200  if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
202 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
203 #endif
204 
205 #if defined(POLARSSL_AES_C)
206  if( !strcasecmp( "AES-128-CBC", cipher_name ) )
208  if( !strcasecmp( "AES-192-CBC", cipher_name ) )
210  if( !strcasecmp( "AES-256-CBC", cipher_name ) )
212 
213 #if defined(POLARSSL_CIPHER_MODE_CFB)
214  if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
216  if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
218  if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
220 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
221 
222 #if defined(POLARSSL_CIPHER_MODE_CTR)
223  if( !strcasecmp( "AES-128-CTR", cipher_name ) )
225  if( !strcasecmp( "AES-192-CTR", cipher_name ) )
227  if( !strcasecmp( "AES-256-CTR", cipher_name ) )
229 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
230 #endif
231 
232 #if defined(POLARSSL_DES_C)
233  if( !strcasecmp( "DES-CBC", cipher_name ) )
235  if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
237  if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
239 #endif
240  return NULL;
241 }
242 
243 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
244 {
245  if( NULL == cipher_info || NULL == ctx )
247 
248  memset( ctx, 0, sizeof( cipher_context_t ) );
249 
250  if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
252 
253  ctx->cipher_info = cipher_info;
254 
255  return 0;
256 }
257 
259 {
260  if( ctx == NULL || ctx->cipher_info == NULL )
262 
263  ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
264 
265  return 0;
266 }
267 
268 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
269  int key_length, const operation_t operation )
270 {
271  if( NULL == ctx || NULL == ctx->cipher_info )
273 
274  ctx->key_length = key_length;
275  ctx->operation = operation;
276 
277  /*
278  * For CFB128 and CTR mode always use the encryption key schedule
279  */
280  if( POLARSSL_ENCRYPT == operation ||
283  {
284  return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
285  ctx->key_length );
286  }
287 
288  if( POLARSSL_DECRYPT == operation )
289  return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
290  ctx->key_length );
291 
293 }
294 
295 int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
296 {
297  if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
299 
300  ctx->unprocessed_len = 0;
301 
302  memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
303 
304  return 0;
305 }
306 
307 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
308  unsigned char *output, size_t *olen )
309 {
310  int ret;
311  size_t copy_len = 0;
312 
313  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
314  input == output )
315  {
317  }
318 
319  *olen = 0;
320 
321  if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
322  {
323  /*
324  * If there is not enough data for a full block, cache it.
325  */
326  if( ( ctx->operation == POLARSSL_DECRYPT &&
327  ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
328  ( ctx->operation == POLARSSL_ENCRYPT &&
329  ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
330  {
331  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
332  ilen );
333 
334  ctx->unprocessed_len += ilen;
335  return 0;
336  }
337 
338  /*
339  * Process cached data first
340  */
341  if( ctx->unprocessed_len != 0 )
342  {
343  copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
344 
345  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
346  copy_len );
347 
348  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
349  ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
350  ctx->unprocessed_data, output ) ) )
351  {
352  return ret;
353  }
354 
355  *olen += cipher_get_block_size( ctx );
356  output += cipher_get_block_size( ctx );
357  ctx->unprocessed_len = 0;
358 
359  input += copy_len;
360  ilen -= copy_len;
361  }
362 
363  /*
364  * Cache final, incomplete block
365  */
366  if( 0 != ilen )
367  {
368  copy_len = ilen % cipher_get_block_size( ctx );
369  if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
370  copy_len = cipher_get_block_size(ctx);
371 
372  memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
373  copy_len );
374 
375  ctx->unprocessed_len += copy_len;
376  ilen -= copy_len;
377  }
378 
379  /*
380  * Process remaining full blocks
381  */
382  if( ilen )
383  {
384  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
385  ctx->operation, ilen, ctx->iv, input, output ) ) )
386  {
387  return ret;
388  }
389  *olen += ilen;
390  }
391 
392  return 0;
393  }
394 
395  if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 )
396  {
397  if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx,
398  ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
399  input, output ) ) )
400  {
401  return ret;
402  }
403 
404  *olen = ilen;
405 
406  return 0;
407  }
408 
409  if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
410  {
411  if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
412  ilen, &ctx->unprocessed_len, ctx->iv,
413  ctx->unprocessed_data, input, output ) ) )
414  {
415  return ret;
416  }
417 
418  *olen = ilen;
419 
420  return 0;
421  }
422 
424 }
425 
426 static void add_pkcs_padding( unsigned char *output, size_t output_len,
427  size_t data_len )
428 {
429  size_t padding_len = output_len - data_len;
430  unsigned char i = 0;
431 
432  for( i = 0; i < padding_len; i++ )
433  output[data_len + i] = (unsigned char) padding_len;
434 }
435 
436 static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
437  size_t *data_len)
438 {
439  unsigned int i, padding_len = 0;
440 
441  if( NULL == input || NULL == data_len )
443 
444  padding_len = input[input_len - 1];
445 
446  if( padding_len > input_len )
448 
449  for( i = input_len - padding_len; i < input_len; i++ )
450  if( input[i] != padding_len )
452 
453  *data_len = input_len - padding_len;
454 
455  return 0;
456 }
457 
458 int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
459 {
460  int ret = 0;
461 
462  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
464 
465  *olen = 0;
466 
467  if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
469  {
470  return 0;
471  }
472 
473  if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
474  {
475  if( POLARSSL_ENCRYPT == ctx->operation )
476  {
477  add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
478  ctx->unprocessed_len );
479  }
480  else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
481  {
482  /* For decrypt operations, expect a full block */
484  }
485 
486  /* cipher block */
487  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
488  ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
489  ctx->unprocessed_data, output ) ) )
490  {
491  return ret;
492  }
493 
494  /* Set output size for decryption */
495  if( POLARSSL_DECRYPT == ctx->operation )
496  return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
497 
498  /* Set output size for encryption */
499  *olen = cipher_get_block_size( ctx );
500  return 0;
501  }
502 
504 }
505 
506 #if defined(POLARSSL_SELF_TEST)
507 
508 #include <stdio.h>
509 
510 #define ASSERT(x) if (!(x)) { \
511  printf( "failed with %i at %s\n", value, (#x) ); \
512  return( 1 ); \
513 }
514 /*
515  * Checkup routine
516  */
517 
518 int cipher_self_test( int verbose )
519 {
520  ((void) verbose);
521 
522  return( 0 );
523 }
524 
525 #endif
526 
527 #endif