PolarSSL v1.3.1
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 #if defined(POLARSSL_GCM_C)
38 #include "polarssl/gcm.h"
39 #endif
40 
41 #include <stdlib.h>
42 
43 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
44 #define POLARSSL_CIPHER_MODE_STREAM
45 #endif
46 
47 #if defined _MSC_VER && !defined strcasecmp
48 #define strcasecmp _stricmp
49 #endif
50 
51 static int supported_init = 0;
52 
53 const int *cipher_list( void )
54 {
55  const cipher_definition_t *def;
56  int *type;
57 
58  if( ! supported_init )
59  {
60  def = cipher_definitions;
61  type = supported_ciphers;
62 
63  while( def->type != 0 )
64  *type++ = (*def++).type;
65 
66  *type = 0;
67 
68  supported_init = 1;
69  }
70 
71  return supported_ciphers;
72 }
73 
74 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
75 {
76  const cipher_definition_t *def;
77 
78  for( def = cipher_definitions; def->info != NULL; def++ )
79  if( def->type == cipher_type )
80  return( def->info );
81 
82  return NULL;
83 }
84 
85 const cipher_info_t *cipher_info_from_string( const char *cipher_name )
86 {
87  const cipher_definition_t *def;
88 
89  if( NULL == cipher_name )
90  return NULL;
91 
92  for( def = cipher_definitions; def->info != NULL; def++ )
93  if( ! strcasecmp( def->info->name, cipher_name ) )
94  return( def->info );
95 
96  return NULL;
97 }
98 
99 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
100  int key_length,
101  const cipher_mode_t mode )
102 {
103  const cipher_definition_t *def;
104 
105  for( def = cipher_definitions; def->info != NULL; def++ )
106  if( def->info->base->cipher == cipher_id &&
107  def->info->key_length == (unsigned) key_length &&
108  def->info->mode == mode )
109  return( def->info );
110 
111  return NULL;
112 }
113 
114 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
115 {
116  if( NULL == cipher_info || NULL == ctx )
118 
119  memset( ctx, 0, sizeof( cipher_context_t ) );
120 
121  if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
123 
124  ctx->cipher_info = cipher_info;
125 
126 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
127  /*
128  * Ignore possible errors caused by a cipher mode that doesn't use padding
129  */
130 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
132 #else
134 #endif
135 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
136 
137  return 0;
138 }
139 
141 {
142  if( ctx == NULL || ctx->cipher_info == NULL )
144 
145  ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
146 
147  return 0;
148 }
149 
150 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
151  int key_length, const operation_t operation )
152 {
153  if( NULL == ctx || NULL == ctx->cipher_info )
155 
156  if( (int) ctx->cipher_info->key_length != key_length )
158 
159  ctx->key_length = key_length;
160  ctx->operation = operation;
161 
162  /*
163  * For CFB and CTR mode always use the encryption key schedule
164  */
165  if( POLARSSL_ENCRYPT == operation ||
168  {
169  return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
170  ctx->key_length );
171  }
172 
173  if( POLARSSL_DECRYPT == operation )
174  return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
175  ctx->key_length );
176 
178 }
179 
181  const unsigned char *iv, size_t iv_len )
182 {
183  size_t actual_iv_size;
184 
185  if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
187 
189  actual_iv_size = iv_len;
190  else
191  actual_iv_size = ctx->cipher_info->iv_size;
192 
193  memcpy( ctx->iv, iv, actual_iv_size );
194  ctx->iv_size = actual_iv_size;
195 
196  return 0;
197 }
198 
199 int cipher_reset( cipher_context_t *ctx )
200 {
201  if( NULL == ctx || NULL == ctx->cipher_info )
203 
204  ctx->unprocessed_len = 0;
205 
206  return 0;
207 }
208 
209 #if defined(POLARSSL_CIPHER_MODE_AEAD)
211  const unsigned char *ad, size_t ad_len )
212 {
213  if( NULL == ctx || NULL == ctx->cipher_info )
215 
216 #if defined(POLARSSL_GCM_C)
217  if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
218  {
219  return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
220  ctx->iv, ctx->iv_size, ad, ad_len );
221  }
222 #endif
223 
224  return 0;
225 }
226 #endif /* POLARSSL_CIPHER_MODE_AEAD */
227 
228 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
229  unsigned char *output, size_t *olen )
230 {
231  int ret;
232 
233  *olen = 0;
234 
235  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
236  {
238  }
239 
240  if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
241  {
242  if( ilen != cipher_get_block_size( ctx ) )
244 
245  *olen = ilen;
246 
247  if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
248  ctx->operation, input, output ) ) )
249  {
250  return ret;
251  }
252 
253  return 0;
254  }
255 
256 #if defined(POLARSSL_GCM_C)
257  if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
258  {
259  *olen = ilen;
260  return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
261  output );
262  }
263 #endif
264 
265  if( input == output &&
266  ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
267  {
269  }
270 
271 #if defined(POLARSSL_CIPHER_MODE_CBC)
272  if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
273  {
274  size_t copy_len = 0;
275 
276  /*
277  * If there is not enough data for a full block, cache it.
278  */
279  if( ( ctx->operation == POLARSSL_DECRYPT &&
280  ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
281  ( ctx->operation == POLARSSL_ENCRYPT &&
282  ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
283  {
284  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
285  ilen );
286 
287  ctx->unprocessed_len += ilen;
288  return 0;
289  }
290 
291  /*
292  * Process cached data first
293  */
294  if( ctx->unprocessed_len != 0 )
295  {
296  copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
297 
298  memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
299  copy_len );
300 
301  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
302  ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
303  ctx->unprocessed_data, output ) ) )
304  {
305  return ret;
306  }
307 
308  *olen += cipher_get_block_size( ctx );
309  output += cipher_get_block_size( ctx );
310  ctx->unprocessed_len = 0;
311 
312  input += copy_len;
313  ilen -= copy_len;
314  }
315 
316  /*
317  * Cache final, incomplete block
318  */
319  if( 0 != ilen )
320  {
321  copy_len = ilen % cipher_get_block_size( ctx );
322  if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
323  copy_len = cipher_get_block_size(ctx);
324 
325  memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
326  copy_len );
327 
328  ctx->unprocessed_len += copy_len;
329  ilen -= copy_len;
330  }
331 
332  /*
333  * Process remaining full blocks
334  */
335  if( ilen )
336  {
337  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
338  ctx->operation, ilen, ctx->iv, input, output ) ) )
339  {
340  return ret;
341  }
342 
343  *olen += ilen;
344  }
345 
346  return 0;
347  }
348 #endif /* POLARSSL_CIPHER_MODE_CBC */
349 
350 #if defined(POLARSSL_CIPHER_MODE_CFB)
351  if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
352  {
353  if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
354  ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
355  input, output ) ) )
356  {
357  return ret;
358  }
359 
360  *olen = ilen;
361 
362  return 0;
363  }
364 #endif
365 
366 #if defined(POLARSSL_CIPHER_MODE_CTR)
367  if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
368  {
369  if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
370  ilen, &ctx->unprocessed_len, ctx->iv,
371  ctx->unprocessed_data, input, output ) ) )
372  {
373  return ret;
374  }
375 
376  *olen = ilen;
377 
378  return 0;
379  }
380 #endif
381 
382 #if defined(POLARSSL_CIPHER_MODE_STREAM)
383  if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
384  {
385  if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
386  ilen, input, output ) ) )
387  {
388  return ret;
389  }
390 
391  *olen = ilen;
392 
393  return 0;
394  }
395 #endif
396 
398 }
399 
400 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
401 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
402 /*
403  * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
404  */
405 static void add_pkcs_padding( unsigned char *output, size_t output_len,
406  size_t data_len )
407 {
408  size_t padding_len = output_len - data_len;
409  unsigned char i = 0;
410 
411  for( i = 0; i < padding_len; i++ )
412  output[data_len + i] = (unsigned char) padding_len;
413 }
414 
415 static int get_pkcs_padding( unsigned char *input, size_t input_len,
416  size_t *data_len )
417 {
418  size_t i, padding_len = 0;
419 
420  if( NULL == input || NULL == data_len )
422 
423  padding_len = input[input_len - 1];
424 
425  if( padding_len > input_len || padding_len == 0 )
427 
428  for( i = input_len - padding_len; i < input_len; i++ )
429  if( input[i] != padding_len )
431 
432  *data_len = input_len - padding_len;
433 
434  return 0;
435 }
436 #endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
437 
438 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
439 /*
440  * One and zeros padding: fill with 80 00 ... 00
441  */
442 static void add_one_and_zeros_padding( unsigned char *output,
443  size_t output_len, size_t data_len )
444 {
445  size_t padding_len = output_len - data_len;
446  unsigned char i = 0;
447 
448  output[data_len] = 0x80;
449  for( i = 1; i < padding_len; i++ )
450  output[data_len + i] = 0x00;
451 }
452 
453 static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
454  size_t *data_len )
455 {
456  unsigned char *p = input + input_len - 1;
457 
458  if( NULL == input || NULL == data_len )
460 
461  while( *p == 0x00 && p > input )
462  --p;
463 
464  if( *p != 0x80 )
466 
467  *data_len = p - input;
468 
469  return 0;
470 }
471 #endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
472 
473 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
474 /*
475  * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
476  */
477 static void add_zeros_and_len_padding( unsigned char *output,
478  size_t output_len, size_t data_len )
479 {
480  size_t padding_len = output_len - data_len;
481  unsigned char i = 0;
482 
483  for( i = 1; i < padding_len; i++ )
484  output[data_len + i - 1] = 0x00;
485  output[output_len - 1] = (unsigned char) padding_len;
486 }
487 
488 static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
489  size_t *data_len )
490 {
491  size_t i, padding_len = 0;
492 
493  if( NULL == input || NULL == data_len )
495 
496  padding_len = input[input_len - 1];
497 
498  if( padding_len > input_len || padding_len == 0 )
500 
501  for( i = input_len - padding_len; i < input_len - 1; i++ )
502  if( input[i] != 0x00 )
504 
505  *data_len = input_len - padding_len;
506 
507  return 0;
508 }
509 #endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
510 
511 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
512 /*
513  * Zero padding: fill with 00 ... 00
514  */
515 static void add_zeros_padding( unsigned char *output,
516  size_t output_len, size_t data_len )
517 {
518  size_t i;
519 
520  for( i = data_len; i < output_len; i++ )
521  output[i] = 0x00;
522 }
523 
524 static int get_zeros_padding( unsigned char *input, size_t input_len,
525  size_t *data_len )
526 {
527  unsigned char *p = input + input_len - 1;
528  if( NULL == input || NULL == data_len )
530 
531  while( *p == 0x00 && p > input )
532  --p;
533 
534  *data_len = *p == 0x00 ? 0 : p - input + 1;
535 
536  return 0;
537 }
538 #endif /* POLARSSL_CIPHER_PADDING_ZEROS */
539 
540 /*
541  * No padding: don't pad :)
542  *
543  * There is no add_padding function (check for NULL in cipher_finish)
544  * but a trivial get_padding function
545  */
546 static int get_no_padding( unsigned char *input, size_t input_len,
547  size_t *data_len )
548 {
549  if( NULL == input || NULL == data_len )
551 
552  *data_len = input_len;
553 
554  return 0;
555 }
556 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
557 
559  unsigned char *output, size_t *olen )
560 {
561  if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
563 
564  *olen = 0;
565 
566  if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
570  {
571  return 0;
572  }
573 
574  if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
575  {
576  if( ctx->unprocessed_len != 0 )
578 
579  return 0;
580  }
581 
582 #if defined(POLARSSL_CIPHER_MODE_CBC)
583  if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
584  {
585  int ret = 0;
586 
587  if( POLARSSL_ENCRYPT == ctx->operation )
588  {
589  /* check for 'no padding' mode */
590  if( NULL == ctx->add_padding )
591  {
592  if( 0 != ctx->unprocessed_len )
594 
595  return 0;
596  }
597 
599  ctx->unprocessed_len );
600  }
601  else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
602  {
603  /*
604  * For decrypt operations, expect a full block,
605  * or an empty block if no padding
606  */
607  if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
608  return 0;
609 
611  }
612 
613  /* cipher block */
614  if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
615  ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
616  ctx->unprocessed_data, output ) ) )
617  {
618  return ret;
619  }
620 
621  /* Set output size for decryption */
622  if( POLARSSL_DECRYPT == ctx->operation )
623  return ctx->get_padding( output, cipher_get_block_size( ctx ),
624  olen );
625 
626  /* Set output size for encryption */
627  *olen = cipher_get_block_size( ctx );
628  return 0;
629  }
630 #else
631  ((void) output);
632 #endif /* POLARSSL_CIPHER_MODE_CBC */
633 
635 }
636 
637 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
639 {
640  if( NULL == ctx ||
642  {
644  }
645 
646  switch( mode )
647  {
648 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
650  ctx->add_padding = add_pkcs_padding;
651  ctx->get_padding = get_pkcs_padding;
652  break;
653 #endif
654 #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
656  ctx->add_padding = add_one_and_zeros_padding;
657  ctx->get_padding = get_one_and_zeros_padding;
658  break;
659 #endif
660 #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
662  ctx->add_padding = add_zeros_and_len_padding;
663  ctx->get_padding = get_zeros_and_len_padding;
664  break;
665 #endif
666 #if defined(POLARSSL_CIPHER_PADDING_ZEROS)
668  ctx->add_padding = add_zeros_padding;
669  ctx->get_padding = get_zeros_padding;
670  break;
671 #endif
673  ctx->add_padding = NULL;
674  ctx->get_padding = get_no_padding;
675  break;
676 
677  default:
679  }
680 
681  return 0;
682 }
683 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
684 
685 #if defined(POLARSSL_CIPHER_MODE_AEAD)
687  unsigned char *tag, size_t tag_len )
688 {
689  if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
691 
692  if( POLARSSL_ENCRYPT != ctx->operation )
694 
695 #if defined(POLARSSL_GCM_C)
696  if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
697  return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
698 #endif
699 
700  return 0;
701 }
702 
704  const unsigned char *tag, size_t tag_len )
705 {
706  int ret;
707 
708  if( NULL == ctx || NULL == ctx->cipher_info ||
709  POLARSSL_DECRYPT != ctx->operation )
710  {
712  }
713 
714 #if defined(POLARSSL_GCM_C)
715  if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
716  {
717  unsigned char check_tag[16];
718  size_t i;
719  int diff;
720 
721  if( tag_len > sizeof( check_tag ) )
723 
724  if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
725  check_tag, tag_len ) ) )
726  {
727  return( ret );
728  }
729 
730  /* Check the tag in "constant-time" */
731  for( diff = 0, i = 0; i < tag_len; i++ )
732  diff |= tag[i] ^ check_tag[i];
733 
734  if( diff != 0 )
736 
737  return( 0 );
738  }
739 #endif
740 
741  return( 0 );
742 }
743 #endif /* POLARSSL_CIPHER_MODE_AEAD */
744 
745 #if defined(POLARSSL_SELF_TEST)
746 
747 #include <stdio.h>
748 
749 #define ASSERT(x) if (!(x)) { \
750  printf( "failed with %i at %s\n", value, (#x) ); \
751  return( 1 ); \
752 }
753 /*
754  * Checkup routine
755  */
756 
757 int cipher_self_test( int verbose )
758 {
759  ((void) verbose);
760 
761  return( 0 );
762 }
763 
764 #endif
765 
766 #endif