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