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