PolarSSL v1.3.9
test_suite_camellia.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_CAMELLIA_C
8 
9 #include <polarssl/camellia.h>
10 #endif /* POLARSSL_CAMELLIA_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #if defined(POLARSSL_PLATFORM_C)
18 #include "polarssl/platform.h"
19 #else
20 #define polarssl_malloc malloc
21 #define polarssl_free free
22 #endif
23 
24 #ifdef _MSC_VER
25 #include <basetsd.h>
26 typedef UINT32 uint32_t;
27 #else
28 #include <inttypes.h>
29 #endif
30 
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /*
36  * 32-bit integer manipulation macros (big endian)
37  */
38 #ifndef GET_UINT32_BE
39 #define GET_UINT32_BE(n,b,i) \
40 { \
41  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44  | ( (uint32_t) (b)[(i) + 3] ); \
45 }
46 #endif
47 
48 #ifndef PUT_UINT32_BE
49 #define PUT_UINT32_BE(n,b,i) \
50 { \
51  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54  (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 }
56 #endif
57 
58 static int unhexify(unsigned char *obuf, const char *ibuf)
59 {
60  unsigned char c, c2;
61  int len = strlen(ibuf) / 2;
62  assert(!(strlen(ibuf) %1)); // must be even number of bytes
63 
64  while (*ibuf != 0)
65  {
66  c = *ibuf++;
67  if( c >= '0' && c <= '9' )
68  c -= '0';
69  else if( c >= 'a' && c <= 'f' )
70  c -= 'a' - 10;
71  else if( c >= 'A' && c <= 'F' )
72  c -= 'A' - 10;
73  else
74  assert( 0 );
75 
76  c2 = *ibuf++;
77  if( c2 >= '0' && c2 <= '9' )
78  c2 -= '0';
79  else if( c2 >= 'a' && c2 <= 'f' )
80  c2 -= 'a' - 10;
81  else if( c2 >= 'A' && c2 <= 'F' )
82  c2 -= 'A' - 10;
83  else
84  assert( 0 );
85 
86  *obuf++ = ( c << 4 ) | c2;
87  }
88 
89  return len;
90 }
91 
92 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93 {
94  unsigned char l, h;
95 
96  while (len != 0)
97  {
98  h = (*ibuf) / 16;
99  l = (*ibuf) % 16;
100 
101  if( h < 10 )
102  *obuf++ = '0' + h;
103  else
104  *obuf++ = 'a' + h - 10;
105 
106  if( l < 10 )
107  *obuf++ = '0' + l;
108  else
109  *obuf++ = 'a' + l - 10;
110 
111  ++ibuf;
112  len--;
113  }
114 }
115 
123 static unsigned char *zero_alloc( size_t len )
124 {
125  void *p;
126  size_t actual_len = len != 0 ? len : 1;
127 
128  p = polarssl_malloc( actual_len );
129  assert( p != NULL );
130 
131  memset( p, 0x00, actual_len );
132 
133  return( p );
134 }
135 
146 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147 {
148  unsigned char *obuf;
149 
150  *olen = strlen(ibuf) / 2;
151 
152  if( *olen == 0 )
153  return( zero_alloc( *olen ) );
154 
155  obuf = polarssl_malloc( *olen );
156  assert( obuf != NULL );
157 
158  (void) unhexify( obuf, ibuf );
159 
160  return( obuf );
161 }
162 
172 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173 {
174 #if !defined(__OpenBSD__)
175  size_t i;
176 
177  if( rng_state != NULL )
178  rng_state = NULL;
179 
180  for( i = 0; i < len; ++i )
181  output[i] = rand();
182 #else
183  if( rng_state != NULL )
184  rng_state = NULL;
185 
186  arc4random_buf( output, len );
187 #endif /* !OpenBSD */
188 
189  return( 0 );
190 }
191 
197 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198 {
199  if( rng_state != NULL )
200  rng_state = NULL;
201 
202  memset( output, 0, len );
203 
204  return( 0 );
205 }
206 
207 typedef struct
208 {
209  unsigned char *buf;
210  size_t length;
211 } rnd_buf_info;
212 
224 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225 {
226  rnd_buf_info *info = (rnd_buf_info *) rng_state;
227  size_t use_len;
228 
229  if( rng_state == NULL )
230  return( rnd_std_rand( NULL, output, len ) );
231 
232  use_len = len;
233  if( len > info->length )
234  use_len = info->length;
235 
236  if( use_len )
237  {
238  memcpy( output, info->buf, use_len );
239  info->buf += use_len;
240  info->length -= use_len;
241  }
242 
243  if( len - use_len > 0 )
244  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245 
246  return( 0 );
247 }
248 
256 typedef struct
257 {
258  uint32_t key[16];
259  uint32_t v0, v1;
261 
270 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271 {
272  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273  uint32_t i, *k, sum, delta=0x9E3779B9;
274  unsigned char result[4], *out = output;
275 
276  if( rng_state == NULL )
277  return( rnd_std_rand( NULL, output, len ) );
278 
279  k = info->key;
280 
281  while( len > 0 )
282  {
283  size_t use_len = ( len > 4 ) ? 4 : len;
284  sum = 0;
285 
286  for( i = 0; i < 32; i++ )
287  {
288  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289  sum += delta;
290  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291  }
292 
293  PUT_UINT32_BE( info->v0, result, 0 );
294  memcpy( out, result, use_len );
295  len -= use_len;
296  out += 4;
297  }
298 
299  return( 0 );
300 }
301 
302 
303 #include <stdio.h>
304 #include <string.h>
305 
306 #if defined(POLARSSL_PLATFORM_C)
307 #include "polarssl/platform.h"
308 #else
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
312 #endif
313 
314 static int test_errors = 0;
315 
316 #ifdef POLARSSL_CAMELLIA_C
317 
318 #define TEST_SUITE_ACTIVE
319 
320 static int test_assert( int correct, const char *test )
321 {
322  if( correct )
323  return( 0 );
324 
325  test_errors++;
326  if( test_errors == 1 )
327  printf( "FAILED\n" );
328  printf( " %s\n", test );
329 
330  return( 1 );
331 }
332 
333 #define TEST_ASSERT( TEST ) \
334  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335  if( test_errors) goto exit; \
336  } while (0)
337 
338 int verify_string( char **str )
339 {
340  if( (*str)[0] != '"' ||
341  (*str)[strlen( *str ) - 1] != '"' )
342  {
343  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
344  return( -1 );
345  }
346 
347  (*str)++;
348  (*str)[strlen( *str ) - 1] = '\0';
349 
350  return( 0 );
351 }
352 
353 int verify_int( char *str, int *value )
354 {
355  size_t i;
356  int minus = 0;
357  int digits = 1;
358  int hex = 0;
359 
360  for( i = 0; i < strlen( str ); i++ )
361  {
362  if( i == 0 && str[i] == '-' )
363  {
364  minus = 1;
365  continue;
366  }
367 
368  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369  str[i - 1] == '0' && str[i] == 'x' )
370  {
371  hex = 1;
372  continue;
373  }
374 
375  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
376  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
377  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
378  {
379  digits = 0;
380  break;
381  }
382  }
383 
384  if( digits )
385  {
386  if( hex )
387  *value = strtol( str, NULL, 16 );
388  else
389  *value = strtol( str, NULL, 10 );
390 
391  return( 0 );
392  }
393 
394  if( strcmp( str, "POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH" ) == 0 )
395  {
397  return( 0 );
398  }
399 #ifdef POLARSSL_CIPHER_MODE_CBC
400  if( strcmp( str, "POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH" ) == 0 )
401  {
403  return( 0 );
404  }
405 #endif // POLARSSL_CIPHER_MODE_CBC
406 
407 
408  printf( "Expected integer for parameter and got: %s\n", str );
409  return( -1 );
410 }
411 
412 void test_suite_camellia_encrypt_ecb( char *hex_key_string, char *hex_src_string,
413  char *hex_dst_string, int setkey_result )
414 {
415  unsigned char key_str[100];
416  unsigned char src_str[100];
417  unsigned char dst_str[100];
418  unsigned char output[100];
419  camellia_context ctx;
420  int key_len;
421 
422  memset(key_str, 0x00, 100);
423  memset(src_str, 0x00, 100);
424  memset(dst_str, 0x00, 100);
425  memset(output, 0x00, 100);
426  camellia_init( &ctx );
427 
428  key_len = unhexify( key_str, hex_key_string );
429  unhexify( src_str, hex_src_string );
430 
431  TEST_ASSERT( camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
432  if( setkey_result == 0 )
433  {
434  TEST_ASSERT( camellia_crypt_ecb( &ctx, CAMELLIA_ENCRYPT, src_str, output ) == 0 );
435  hexify( dst_str, output, 16 );
436 
437  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
438  }
439 
440 exit:
441  camellia_free( &ctx );
442 }
443 
444 void test_suite_camellia_decrypt_ecb( char *hex_key_string, char *hex_src_string,
445  char *hex_dst_string, int setkey_result )
446 {
447  unsigned char key_str[100];
448  unsigned char src_str[100];
449  unsigned char dst_str[100];
450  unsigned char output[100];
451  camellia_context ctx;
452  int key_len;
453 
454  memset(key_str, 0x00, 100);
455  memset(src_str, 0x00, 100);
456  memset(dst_str, 0x00, 100);
457  memset(output, 0x00, 100);
458  camellia_init( &ctx );
459 
460  key_len = unhexify( key_str, hex_key_string );
461  unhexify( src_str, hex_src_string );
462 
463  TEST_ASSERT( camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
464  if( setkey_result == 0 )
465  {
466  TEST_ASSERT( camellia_crypt_ecb( &ctx, CAMELLIA_DECRYPT, src_str, output ) == 0 );
467  hexify( dst_str, output, 16 );
468 
469  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
470  }
471 
472 exit:
473  camellia_free( &ctx );
474 }
475 
476 #ifdef POLARSSL_CIPHER_MODE_CBC
477 void test_suite_camellia_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
478  char *hex_src_string, char *hex_dst_string,
479  int cbc_result )
480 {
481  unsigned char key_str[100];
482  unsigned char iv_str[100];
483  unsigned char src_str[100];
484  unsigned char dst_str[100];
485  unsigned char output[100];
486  camellia_context ctx;
487  int key_len, data_len;
488 
489  memset(key_str, 0x00, 100);
490  memset(iv_str, 0x00, 100);
491  memset(src_str, 0x00, 100);
492  memset(dst_str, 0x00, 100);
493  memset(output, 0x00, 100);
494  camellia_init( &ctx );
495 
496  key_len = unhexify( key_str, hex_key_string );
497  unhexify( iv_str, hex_iv_string );
498  data_len = unhexify( src_str, hex_src_string );
499 
500  camellia_setkey_enc( &ctx, key_str, key_len * 8 );
501  TEST_ASSERT( camellia_crypt_cbc( &ctx, CAMELLIA_ENCRYPT, data_len, iv_str, src_str, output) == cbc_result );
502  if( cbc_result == 0 )
503  {
504  hexify( dst_str, output, data_len );
505 
506  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
507  }
508 
509 exit:
510  camellia_free( &ctx );
511 }
512 #endif /* POLARSSL_CIPHER_MODE_CBC */
513 
514 #ifdef POLARSSL_CIPHER_MODE_CBC
515 void test_suite_camellia_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
516  char *hex_src_string, char *hex_dst_string,
517  int cbc_result )
518 {
519  unsigned char key_str[100];
520  unsigned char iv_str[100];
521  unsigned char src_str[100];
522  unsigned char dst_str[100];
523  unsigned char output[100];
524  camellia_context ctx;
525  int key_len, data_len;
526 
527  memset(key_str, 0x00, 100);
528  memset(iv_str, 0x00, 100);
529  memset(src_str, 0x00, 100);
530  memset(dst_str, 0x00, 100);
531  memset(output, 0x00, 100);
532  camellia_init( &ctx );
533 
534  key_len = unhexify( key_str, hex_key_string );
535  unhexify( iv_str, hex_iv_string );
536  data_len = unhexify( src_str, hex_src_string );
537 
538  camellia_setkey_dec( &ctx, key_str, key_len * 8 );
539  TEST_ASSERT( camellia_crypt_cbc( &ctx, CAMELLIA_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
540  if( cbc_result == 0 )
541  {
542  hexify( dst_str, output, data_len );
543 
544  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
545  }
546 
547 exit:
548  camellia_free( &ctx );
549 }
550 #endif /* POLARSSL_CIPHER_MODE_CBC */
551 
552 #ifdef POLARSSL_CIPHER_MODE_CFB
553 void test_suite_camellia_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
554  char *hex_src_string, char *hex_dst_string )
555 {
556  unsigned char key_str[100];
557  unsigned char iv_str[100];
558  unsigned char src_str[100];
559  unsigned char dst_str[100];
560  unsigned char output[100];
561  camellia_context ctx;
562  size_t iv_offset = 0;
563  int key_len;
564 
565  memset(key_str, 0x00, 100);
566  memset(iv_str, 0x00, 100);
567  memset(src_str, 0x00, 100);
568  memset(dst_str, 0x00, 100);
569  memset(output, 0x00, 100);
570  camellia_init( &ctx );
571 
572  key_len = unhexify( key_str, hex_key_string );
573  unhexify( iv_str, hex_iv_string );
574  unhexify( src_str, hex_src_string );
575 
576  camellia_setkey_enc( &ctx, key_str, key_len * 8 );
577  TEST_ASSERT( camellia_crypt_cfb128( &ctx, CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
578  hexify( dst_str, output, 16 );
579 
580  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
581 
582 exit:
583  camellia_free( &ctx );
584 }
585 #endif /* POLARSSL_CIPHER_MODE_CFB */
586 
587 #ifdef POLARSSL_CIPHER_MODE_CFB
588 void test_suite_camellia_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
589  char *hex_src_string, char *hex_dst_string )
590 {
591  unsigned char key_str[100];
592  unsigned char iv_str[100];
593  unsigned char src_str[100];
594  unsigned char dst_str[100];
595  unsigned char output[100];
596  camellia_context ctx;
597  size_t iv_offset = 0;
598  int key_len;
599 
600  memset(key_str, 0x00, 100);
601  memset(iv_str, 0x00, 100);
602  memset(src_str, 0x00, 100);
603  memset(dst_str, 0x00, 100);
604  memset(output, 0x00, 100);
605  camellia_init( &ctx );
606 
607  key_len = unhexify( key_str, hex_key_string );
608  unhexify( iv_str, hex_iv_string );
609  unhexify( src_str, hex_src_string );
610 
611  camellia_setkey_enc( &ctx, key_str, key_len * 8 );
612  TEST_ASSERT( camellia_crypt_cfb128( &ctx, CAMELLIA_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
613  hexify( dst_str, output, 16 );
614 
615  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
616 
617 exit:
618  camellia_free( &ctx );
619 }
620 #endif /* POLARSSL_CIPHER_MODE_CFB */
621 
622 #ifdef POLARSSL_SELF_TEST
623 void test_suite_camellia_selftest()
624 {
625  TEST_ASSERT( camellia_self_test( 0 ) == 0 );
626 
627 exit:
628  return;
629 }
630 #endif /* POLARSSL_SELF_TEST */
631 
632 
633 #endif /* POLARSSL_CAMELLIA_C */
634 
635 
636 int dep_check( char *str )
637 {
638  if( str == NULL )
639  return( 1 );
640 
641  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
642  {
643 #if defined(POLARSSL_CIPHER_MODE_CFB)
644  return( 0 );
645 #else
646  return( 1 );
647 #endif
648  }
649  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
650  {
651 #if defined(POLARSSL_SELF_TEST)
652  return( 0 );
653 #else
654  return( 1 );
655 #endif
656  }
657 
658 
659  return( 1 );
660 }
661 
662 int dispatch_test(int cnt, char *params[50])
663 {
664  int ret;
665  ((void) cnt);
666  ((void) params);
667 
668 #if defined(TEST_SUITE_ACTIVE)
669  if( strcmp( params[0], "camellia_encrypt_ecb" ) == 0 )
670  {
671 
672  char *param1 = params[1];
673  char *param2 = params[2];
674  char *param3 = params[3];
675  int param4;
676 
677  if( cnt != 5 )
678  {
679  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
680  return( 2 );
681  }
682 
683  if( verify_string( &param1 ) != 0 ) return( 2 );
684  if( verify_string( &param2 ) != 0 ) return( 2 );
685  if( verify_string( &param3 ) != 0 ) return( 2 );
686  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
687 
688  test_suite_camellia_encrypt_ecb( param1, param2, param3, param4 );
689  return ( 0 );
690 
691  return ( 3 );
692  }
693  else
694  if( strcmp( params[0], "camellia_decrypt_ecb" ) == 0 )
695  {
696 
697  char *param1 = params[1];
698  char *param2 = params[2];
699  char *param3 = params[3];
700  int param4;
701 
702  if( cnt != 5 )
703  {
704  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
705  return( 2 );
706  }
707 
708  if( verify_string( &param1 ) != 0 ) return( 2 );
709  if( verify_string( &param2 ) != 0 ) return( 2 );
710  if( verify_string( &param3 ) != 0 ) return( 2 );
711  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
712 
713  test_suite_camellia_decrypt_ecb( param1, param2, param3, param4 );
714  return ( 0 );
715 
716  return ( 3 );
717  }
718  else
719  if( strcmp( params[0], "camellia_encrypt_cbc" ) == 0 )
720  {
721  #ifdef POLARSSL_CIPHER_MODE_CBC
722 
723  char *param1 = params[1];
724  char *param2 = params[2];
725  char *param3 = params[3];
726  char *param4 = params[4];
727  int param5;
728 
729  if( cnt != 6 )
730  {
731  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
732  return( 2 );
733  }
734 
735  if( verify_string( &param1 ) != 0 ) return( 2 );
736  if( verify_string( &param2 ) != 0 ) return( 2 );
737  if( verify_string( &param3 ) != 0 ) return( 2 );
738  if( verify_string( &param4 ) != 0 ) return( 2 );
739  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
740 
741  test_suite_camellia_encrypt_cbc( param1, param2, param3, param4, param5 );
742  return ( 0 );
743  #endif /* POLARSSL_CIPHER_MODE_CBC */
744 
745  return ( 3 );
746  }
747  else
748  if( strcmp( params[0], "camellia_decrypt_cbc" ) == 0 )
749  {
750  #ifdef POLARSSL_CIPHER_MODE_CBC
751 
752  char *param1 = params[1];
753  char *param2 = params[2];
754  char *param3 = params[3];
755  char *param4 = params[4];
756  int param5;
757 
758  if( cnt != 6 )
759  {
760  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
761  return( 2 );
762  }
763 
764  if( verify_string( &param1 ) != 0 ) return( 2 );
765  if( verify_string( &param2 ) != 0 ) return( 2 );
766  if( verify_string( &param3 ) != 0 ) return( 2 );
767  if( verify_string( &param4 ) != 0 ) return( 2 );
768  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
769 
770  test_suite_camellia_decrypt_cbc( param1, param2, param3, param4, param5 );
771  return ( 0 );
772  #endif /* POLARSSL_CIPHER_MODE_CBC */
773 
774  return ( 3 );
775  }
776  else
777  if( strcmp( params[0], "camellia_encrypt_cfb128" ) == 0 )
778  {
779  #ifdef POLARSSL_CIPHER_MODE_CFB
780 
781  char *param1 = params[1];
782  char *param2 = params[2];
783  char *param3 = params[3];
784  char *param4 = params[4];
785 
786  if( cnt != 5 )
787  {
788  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
789  return( 2 );
790  }
791 
792  if( verify_string( &param1 ) != 0 ) return( 2 );
793  if( verify_string( &param2 ) != 0 ) return( 2 );
794  if( verify_string( &param3 ) != 0 ) return( 2 );
795  if( verify_string( &param4 ) != 0 ) return( 2 );
796 
797  test_suite_camellia_encrypt_cfb128( param1, param2, param3, param4 );
798  return ( 0 );
799  #endif /* POLARSSL_CIPHER_MODE_CFB */
800 
801  return ( 3 );
802  }
803  else
804  if( strcmp( params[0], "camellia_decrypt_cfb128" ) == 0 )
805  {
806  #ifdef POLARSSL_CIPHER_MODE_CFB
807 
808  char *param1 = params[1];
809  char *param2 = params[2];
810  char *param3 = params[3];
811  char *param4 = params[4];
812 
813  if( cnt != 5 )
814  {
815  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
816  return( 2 );
817  }
818 
819  if( verify_string( &param1 ) != 0 ) return( 2 );
820  if( verify_string( &param2 ) != 0 ) return( 2 );
821  if( verify_string( &param3 ) != 0 ) return( 2 );
822  if( verify_string( &param4 ) != 0 ) return( 2 );
823 
824  test_suite_camellia_decrypt_cfb128( param1, param2, param3, param4 );
825  return ( 0 );
826  #endif /* POLARSSL_CIPHER_MODE_CFB */
827 
828  return ( 3 );
829  }
830  else
831  if( strcmp( params[0], "camellia_selftest" ) == 0 )
832  {
833  #ifdef POLARSSL_SELF_TEST
834 
835 
836  if( cnt != 1 )
837  {
838  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
839  return( 2 );
840  }
841 
842 
843  test_suite_camellia_selftest( );
844  return ( 0 );
845  #endif /* POLARSSL_SELF_TEST */
846 
847  return ( 3 );
848  }
849  else
850 
851  {
852  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
853  fflush( stdout );
854  return( 1 );
855  }
856 #else
857  return( 3 );
858 #endif
859  return( ret );
860 }
861 
862 int get_line( FILE *f, char *buf, size_t len )
863 {
864  char *ret;
865 
866  ret = fgets( buf, len, f );
867  if( ret == NULL )
868  return( -1 );
869 
870  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
871  buf[strlen(buf) - 1] = '\0';
872  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
873  buf[strlen(buf) - 1] = '\0';
874 
875  return( 0 );
876 }
877 
878 int parse_arguments( char *buf, size_t len, char *params[50] )
879 {
880  int cnt = 0, i;
881  char *cur = buf;
882  char *p = buf, *q;
883 
884  params[cnt++] = cur;
885 
886  while( *p != '\0' && p < buf + len )
887  {
888  if( *p == '\\' )
889  {
890  p++;
891  p++;
892  continue;
893  }
894  if( *p == ':' )
895  {
896  if( p + 1 < buf + len )
897  {
898  cur = p + 1;
899  params[cnt++] = cur;
900  }
901  *p = '\0';
902  }
903 
904  p++;
905  }
906 
907  // Replace newlines, question marks and colons in strings
908  for( i = 0; i < cnt; i++ )
909  {
910  p = params[i];
911  q = params[i];
912 
913  while( *p != '\0' )
914  {
915  if( *p == '\\' && *(p + 1) == 'n' )
916  {
917  p += 2;
918  *(q++) = '\n';
919  }
920  else if( *p == '\\' && *(p + 1) == ':' )
921  {
922  p += 2;
923  *(q++) = ':';
924  }
925  else if( *p == '\\' && *(p + 1) == '?' )
926  {
927  p += 2;
928  *(q++) = '?';
929  }
930  else
931  *(q++) = *(p++);
932  }
933  *q = '\0';
934  }
935 
936  return( cnt );
937 }
938 
939 int main()
940 {
941  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
942  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_camellia.data";
943  FILE *file;
944  char buf[5000];
945  char *params[50];
946 
947 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
948  unsigned char alloc_buf[1000000];
949  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
950 #endif
951 
952  file = fopen( filename, "r" );
953  if( file == NULL )
954  {
955  fprintf( stderr, "Failed to open\n" );
956  return( 1 );
957  }
958 
959  while( !feof( file ) )
960  {
961  int skip = 0;
962 
963  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
964  break;
965  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
966  fprintf( stdout, " " );
967  for( i = strlen( buf ) + 1; i < 67; i++ )
968  fprintf( stdout, "." );
969  fprintf( stdout, " " );
970  fflush( stdout );
971 
972  total_tests++;
973 
974  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
975  break;
976  cnt = parse_arguments( buf, strlen(buf), params );
977 
978  if( strcmp( params[0], "depends_on" ) == 0 )
979  {
980  for( i = 1; i < cnt; i++ )
981  if( dep_check( params[i] ) != 0 )
982  skip = 1;
983 
984  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
985  break;
986  cnt = parse_arguments( buf, strlen(buf), params );
987  }
988 
989  if( skip == 0 )
990  {
991  test_errors = 0;
992  ret = dispatch_test( cnt, params );
993  }
994 
995  if( skip == 1 || ret == 3 )
996  {
997  total_skipped++;
998  fprintf( stdout, "----\n" );
999  fflush( stdout );
1000  }
1001  else if( ret == 0 && test_errors == 0 )
1002  {
1003  fprintf( stdout, "PASS\n" );
1004  fflush( stdout );
1005  }
1006  else if( ret == 2 )
1007  {
1008  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1009  fclose(file);
1010  exit( 2 );
1011  }
1012  else
1013  total_errors++;
1014 
1015  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1016  break;
1017  if( strlen(buf) != 0 )
1018  {
1019  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1020  return( 1 );
1021  }
1022  }
1023  fclose(file);
1024 
1025  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1026  if( total_errors == 0 )
1027  fprintf( stdout, "PASSED" );
1028  else
1029  fprintf( stdout, "FAILED" );
1030 
1031  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1032  total_tests - total_errors, total_tests, total_skipped );
1033 
1034 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1035 #if defined(POLARSSL_MEMORY_DEBUG)
1036  memory_buffer_alloc_status();
1037 #endif
1039 #endif
1040 
1041  return( total_errors != 0 );
1042 }
1043 
1044 
Memory allocation layer (Deprecated to platform layer)
int camellia_crypt_cbc(camellia_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
#define polarssl_malloc
Configuration options (set of defines)
Camellia block cipher.
int camellia_setkey_enc(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (encryption)
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int main(int argc, char *argv[])
int camellia_self_test(int verbose)
Checkup routine.
int camellia_crypt_cfb128(camellia_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CFB128 buffer encryption/decryption.
int dep_check(char *str)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
void camellia_init(camellia_context *ctx)
Initialize CAMELLIA context.
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
#define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Invalid data input length.
Definition: camellia.h:49
CAMELLIA context structure.
Definition: camellia.h:62
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int parse_arguments(char *buf, size_t len, char *params[50])
void camellia_free(camellia_context *ctx)
Clear CAMELLIA context.
static int test_errors
int camellia_crypt_ecb(camellia_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
CAMELLIA-ECB block encryption/decryption.
int camellia_setkey_dec(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (decryption)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
static int unhexify(unsigned char *obuf, const char *ibuf)
#define CAMELLIA_DECRYPT
Definition: camellia.h:46
unsigned char * buf
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_int(char *str, int *value)
#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Invalid key length.
Definition: camellia.h:48
#define CAMELLIA_ENCRYPT
Definition: camellia.h:45
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int get_line(FILE *f, char *buf, size_t len)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.