PolarSSL v1.2.9
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 #if defined(POLARSSL_BLOWFISH_C)
91 
92 #if defined(POLARSSL_CIPHER_MODE_CFB)
94 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
95 
96 #if defined(POLARSSL_CIPHER_MODE_CTR)
98 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
99 
100 #endif /* defined(POLARSSL_BLOWFISH_C) */
101 
102 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
104 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
105 
106  0
107 };
108 
109 const int *cipher_list( void )
110 {
111  return supported_ciphers;
112 }
113 
114 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
115 {
116  /* Find static cipher information */
117  switch ( cipher_type )
118  {
119 #if defined(POLARSSL_AES_C)
121  return &aes_128_cbc_info;
123  return &aes_192_cbc_info;
125  return &aes_256_cbc_info;
126 
127 #if defined(POLARSSL_CIPHER_MODE_CFB)
129  return &aes_128_cfb128_info;
131  return &aes_192_cfb128_info;
133  return &aes_256_cfb128_info;
134 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
135 
136 #if defined(POLARSSL_CIPHER_MODE_CTR)
138  return &aes_128_ctr_info;
140  return &aes_192_ctr_info;
142  return &aes_256_ctr_info;
143 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
144 
145 #endif
146 
147 #if defined(POLARSSL_CAMELLIA_C)
149  return &camellia_128_cbc_info;
151  return &camellia_192_cbc_info;
153  return &camellia_256_cbc_info;
154 
155 #if defined(POLARSSL_CIPHER_MODE_CFB)
157  return &camellia_128_cfb128_info;
159  return &camellia_192_cfb128_info;
161  return &camellia_256_cfb128_info;
162 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
163 
164 #if defined(POLARSSL_CIPHER_MODE_CTR)
166  return &camellia_128_ctr_info;
168  return &camellia_192_ctr_info;
170  return &camellia_256_ctr_info;
171 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
172 
173 #endif
174 
175 #if defined(POLARSSL_DES_C)
177  return &des_cbc_info;
179  return &des_ede_cbc_info;
181  return &des_ede3_cbc_info;
182 #endif
183 
184 #if defined(POLARSSL_BLOWFISH_C)
186  return &blowfish_cbc_info;
187 
188 #if defined(POLARSSL_CIPHER_MODE_CFB)
190  return &blowfish_cfb64_info;
191 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
192 
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
195  return &blowfish_ctr_info;
196 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
197 
198 #endif
199 
200 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
202  return &null_cipher_info;
203 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
204 
205  default:
206  return NULL;
207  }
208 }
209 
210 const cipher_info_t *cipher_info_from_string( const char *cipher_name )
211 {
212  if( NULL == cipher_name )
213  return NULL;
214 
215  /* Get the appropriate cipher information */
216 #if defined(POLARSSL_CAMELLIA_C)
217  if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
219  if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
221  if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
223 
224 #if defined(POLARSSL_CIPHER_MODE_CFB)
225  if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
227  if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
229  if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
231 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
232 
233 #if defined(POLARSSL_CIPHER_MODE_CTR)
234  if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
236  if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
238  if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
240 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
241 #endif
242 
243 #if defined(POLARSSL_AES_C)
244  if( !strcasecmp( "AES-128-CBC", cipher_name ) )
246  if( !strcasecmp( "AES-192-CBC", cipher_name ) )
248  if( !strcasecmp( "AES-256-CBC", cipher_name ) )
250 
251 #if defined(POLARSSL_CIPHER_MODE_CFB)
252  if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
254  if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
256  if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
258 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
259 
260 #if defined(POLARSSL_CIPHER_MODE_CTR)
261  if( !strcasecmp( "AES-128-CTR", cipher_name ) )
263  if( !strcasecmp( "AES-192-CTR", cipher_name ) )
265  if( !strcasecmp( "AES-256-CTR", cipher_name ) )
267 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
268 #endif
269 
270 #if defined(POLARSSL_DES_C)
271  if( !strcasecmp( "DES-CBC", cipher_name ) )
273  if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
275  if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
277 #endif
278 
279 #if defined(POLARSSL_BLOWFISH_C)
280  if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
282 
283 #if defined(POLARSSL_CIPHER_MODE_CFB)
284  if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
286 #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
287 
288 #if defined(POLARSSL_CIPHER_MODE_CTR)
289  if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
291 #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
292 #endif
293 
294 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
295  if( !strcasecmp( "NULL", cipher_name ) )
297 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
298 
299  return NULL;
300 }
301 
302 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
303 {
304  if( NULL == cipher_info || NULL == ctx )
306 
307  memset( ctx, 0, sizeof( cipher_context_t ) );
308 
309  if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
311 
312  ctx->cipher_info = cipher_info;
313 
314  return 0;
315 }
316 
318 {
319  if( ctx == NULL || ctx->cipher_info == NULL )
321 
322  ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
323 
324  return 0;
325 }
326 
327 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
328  int key_length, const operation_t operation )
329 {
330  if( NULL == ctx || NULL == ctx->cipher_info )
332 
333  ctx->key_length = key_length;
334  ctx->operation = operation;
335 
336 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
337  if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
338  return 0;
339 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
340 
341  /*
342  * For CFB and CTR mode always use the encryption key schedule
343  */
344  if( POLARSSL_ENCRYPT == operation ||
347  {
348  return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
349  ctx->key_length );
350  }
351 
352  if( POLARSSL_DECRYPT == operation )
353  return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
354  ctx->key_length );
355 
357 }
358 
359 int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
360 {
361  if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
363 
364  ctx->unprocessed_len = 0;
365 
366  memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
367 
368  return 0;
369 }
370 
371 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
372  unsigned char *output, size_t *olen )
373 {
374  int ret;
375  size_t copy_len = 0;
376 
377  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
378  input == output )
379  {
381  }
382 
383  *olen = 0;
384 
385 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
386  if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
387  {
388  memcpy( output, input, ilen );
389  *olen = ilen;
390  return 0;
391  }
392 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
393 
394  if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
395  {
396  /*
397  * If there is not enough data for a full block, cache it.
398  */
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 ) ) )
403  {
404  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
405  ilen );
406 
407  ctx->unprocessed_len += ilen;
408  return 0;
409  }
410 
411  /*
412  * Process cached data first
413  */
414  if( ctx->unprocessed_len != 0 )
415  {
416  copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
417 
418  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
419  copy_len );
420 
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 ) ) )
424  {
425  return ret;
426  }
427 
428  *olen += cipher_get_block_size( ctx );
429  output += cipher_get_block_size( ctx );
430  ctx->unprocessed_len = 0;
431 
432  input += copy_len;
433  ilen -= copy_len;
434  }
435 
436  /*
437  * Cache final, incomplete block
438  */
439  if( 0 != ilen )
440  {
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);
444 
445  memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
446  copy_len );
447 
448  ctx->unprocessed_len += copy_len;
449  ilen -= copy_len;
450  }
451 
452  /*
453  * Process remaining full blocks
454  */
455  if( ilen )
456  {
457  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
458  ctx->operation, ilen, ctx->iv, input, output ) ) )
459  {
460  return ret;
461  }
462  *olen += ilen;
463  }
464 
465  return 0;
466  }
467 
468  if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
469  {
470  if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
471  ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
472  input, output ) ) )
473  {
474  return ret;
475  }
476 
477  *olen = ilen;
478 
479  return 0;
480  }
481 
482  if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
483  {
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 ) ) )
487  {
488  return ret;
489  }
490 
491  *olen = ilen;
492 
493  return 0;
494  }
495 
497 }
498 
499 static void add_pkcs_padding( unsigned char *output, size_t output_len,
500  size_t data_len )
501 {
502  size_t padding_len = output_len - data_len;
503  unsigned char i = 0;
504 
505  for( i = 0; i < padding_len; i++ )
506  output[data_len + i] = (unsigned char) padding_len;
507 }
508 
509 static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
510  size_t *data_len)
511 {
512  unsigned int i, padding_len = 0;
513 
514  if( NULL == input || NULL == data_len )
516 
517  padding_len = input[input_len - 1];
518 
519  if( padding_len > input_len )
521 
522  for( i = input_len - padding_len; i < input_len; i++ )
523  if( input[i] != padding_len )
525 
526  *data_len = input_len - padding_len;
527 
528  return 0;
529 }
530 
531 int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
532 {
533  int ret = 0;
534 
535  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
537 
538  *olen = 0;
539 
540  if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
543  {
544  return 0;
545  }
546 
547  if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
548  {
549  if( POLARSSL_ENCRYPT == ctx->operation )
550  {
551  add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
552  ctx->unprocessed_len );
553  }
554  else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
555  {
556  /* For decrypt operations, expect a full block */
558  }
559 
560  /* cipher block */
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 ) ) )
564  {
565  return ret;
566  }
567 
568  /* Set output size for decryption */
569  if( POLARSSL_DECRYPT == ctx->operation )
570  return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
571 
572  /* Set output size for encryption */
573  *olen = cipher_get_block_size( ctx );
574  return 0;
575  }
576 
578 }
579 
580 #if defined(POLARSSL_SELF_TEST)
581 
582 #include <stdio.h>
583 
584 #define ASSERT(x) if (!(x)) { \
585  printf( "failed with %i at %s\n", value, (#x) ); \
586  return( 1 ); \
587 }
588 /*
589  * Checkup routine
590  */
591 
592 int cipher_self_test( int verbose )
593 {
594  ((void) verbose);
595 
596  return( 0 );
597 }
598 
599 #endif
600 
601 #endif