PolarSSL v1.3.1
test_suite_des.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_DES_C
4 
5 #include <polarssl/des.h>
6 #endif /* POLARSSL_DES_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_DES_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_DES_INVALID_INPUT_LENGTH" ) == 0 )
365  {
367  return( 0 );
368  }
369 
370 
371  printf( "Expected integer for parameter and got: %s\n", str );
372  return( -1 );
373 }
374 
375 void test_suite_des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
376  char *hex_dst_string )
377 {
378  unsigned char key_str[100];
379  unsigned char src_str[100];
380  unsigned char dst_str[100];
381  unsigned char output[100];
382  des_context ctx;
383 
384  memset(key_str, 0x00, 100);
385  memset(src_str, 0x00, 100);
386  memset(dst_str, 0x00, 100);
387  memset(output, 0x00, 100);
388 
389  unhexify( key_str, hex_key_string );
390  unhexify( src_str, hex_src_string );
391 
392  des_setkey_enc( &ctx, key_str );
393  TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
394  hexify( dst_str, output, 8 );
395 
396  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
397 }
398 
399 void test_suite_des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
400  char *hex_dst_string )
401 {
402  unsigned char key_str[100];
403  unsigned char src_str[100];
404  unsigned char dst_str[100];
405  unsigned char output[100];
406  des_context ctx;
407 
408  memset(key_str, 0x00, 100);
409  memset(src_str, 0x00, 100);
410  memset(dst_str, 0x00, 100);
411  memset(output, 0x00, 100);
412 
413  unhexify( key_str, hex_key_string );
414  unhexify( src_str, hex_src_string );
415 
416  des_setkey_dec( &ctx, key_str );
417  TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
418  hexify( dst_str, output, 8 );
419 
420  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
421 }
422 
423 #ifdef POLARSSL_CIPHER_MODE_CBC
424 void test_suite_des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
425  char *hex_src_string, char *hex_dst_string, int cbc_result )
426 {
427  unsigned char key_str[100];
428  unsigned char iv_str[100];
429  unsigned char src_str[100];
430  unsigned char dst_str[100];
431  unsigned char output[100];
432  des_context ctx;
433  int src_len;
434 
435  memset(key_str, 0x00, 100);
436  memset(iv_str, 0x00, 100);
437  memset(src_str, 0x00, 100);
438  memset(dst_str, 0x00, 100);
439  memset(output, 0x00, 100);
440 
441  unhexify( key_str, hex_key_string );
442  unhexify( iv_str, hex_iv_string );
443  src_len = unhexify( src_str, hex_src_string );
444 
445  des_setkey_enc( &ctx, key_str );
446  TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
447  if( cbc_result == 0 )
448  {
449  hexify( dst_str, output, src_len );
450 
451  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
452  }
453 }
454 #endif /* POLARSSL_CIPHER_MODE_CBC */
455 
456 #ifdef POLARSSL_CIPHER_MODE_CBC
457 void test_suite_des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
458  char *hex_src_string, char *hex_dst_string, int cbc_result )
459 {
460  unsigned char key_str[100];
461  unsigned char iv_str[100];
462  unsigned char src_str[100];
463  unsigned char dst_str[100];
464  unsigned char output[100];
465  des_context ctx;
466  int src_len;
467 
468  memset(key_str, 0x00, 100);
469  memset(iv_str, 0x00, 100);
470  memset(src_str, 0x00, 100);
471  memset(dst_str, 0x00, 100);
472  memset(output, 0x00, 100);
473 
474  unhexify( key_str, hex_key_string );
475  unhexify( iv_str, hex_iv_string );
476  src_len = unhexify( src_str, hex_src_string );
477 
478  des_setkey_dec( &ctx, key_str );
479  TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
480  if( cbc_result == 0 )
481  {
482  hexify( dst_str, output, src_len );
483 
484  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
485  }
486 }
487 #endif /* POLARSSL_CIPHER_MODE_CBC */
488 
489 void test_suite_des3_encrypt_ecb( int key_count, char *hex_key_string,
490  char *hex_src_string, char *hex_dst_string )
491 {
492  unsigned char key_str[100];
493  unsigned char src_str[100];
494  unsigned char dst_str[100];
495  unsigned char output[100];
496  des3_context ctx;
497 
498  memset(key_str, 0x00, 100);
499  memset(src_str, 0x00, 100);
500  memset(dst_str, 0x00, 100);
501  memset(output, 0x00, 100);
502 
503  unhexify( key_str, hex_key_string );
504  unhexify( src_str, hex_src_string );
505 
506  if( key_count == 2 )
507  des3_set2key_enc( &ctx, key_str );
508  else if( key_count == 3 )
509  des3_set3key_enc( &ctx, key_str );
510  else
511  TEST_ASSERT( 0 );
512 
513  TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
514  hexify( dst_str, output, 8 );
515 
516  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
517 }
518 
519 void test_suite_des3_decrypt_ecb( int key_count, char *hex_key_string,
520  char *hex_src_string, char *hex_dst_string )
521 {
522  unsigned char key_str[100];
523  unsigned char src_str[100];
524  unsigned char dst_str[100];
525  unsigned char output[100];
526  des3_context ctx;
527 
528  memset(key_str, 0x00, 100);
529  memset(src_str, 0x00, 100);
530  memset(dst_str, 0x00, 100);
531  memset(output, 0x00, 100);
532 
533  unhexify( key_str, hex_key_string );
534  unhexify( src_str, hex_src_string );
535 
536  if( key_count == 2 )
537  des3_set2key_dec( &ctx, key_str );
538  else if( key_count == 3 )
539  des3_set3key_dec( &ctx, key_str );
540  else
541  TEST_ASSERT( 0 );
542 
543  TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
544  hexify( dst_str, output, 8 );
545 
546  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
547 }
548 
549 #ifdef POLARSSL_CIPHER_MODE_CBC
550 void test_suite_des3_encrypt_cbc( int key_count, char *hex_key_string,
551  char *hex_iv_string, char *hex_src_string,
552  char *hex_dst_string, int cbc_result )
553 {
554  unsigned char key_str[100];
555  unsigned char iv_str[100];
556  unsigned char src_str[100];
557  unsigned char dst_str[100];
558  unsigned char output[100];
559  des3_context ctx;
560  int src_len;
561 
562  memset(key_str, 0x00, 100);
563  memset(iv_str, 0x00, 100);
564  memset(src_str, 0x00, 100);
565  memset(dst_str, 0x00, 100);
566  memset(output, 0x00, 100);
567 
568  unhexify( key_str, hex_key_string );
569  unhexify( iv_str, hex_iv_string );
570  src_len = unhexify( src_str, hex_src_string );
571 
572  if( key_count == 2 )
573  des3_set2key_enc( &ctx, key_str );
574  else if( key_count == 3 )
575  des3_set3key_enc( &ctx, key_str );
576  else
577  TEST_ASSERT( 0 );
578 
579  TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
580 
581  if( cbc_result == 0 )
582  {
583  hexify( dst_str, output, src_len );
584 
585  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
586  }
587 }
588 #endif /* POLARSSL_CIPHER_MODE_CBC */
589 
590 #ifdef POLARSSL_CIPHER_MODE_CBC
591 void test_suite_des3_decrypt_cbc( int key_count, char *hex_key_string,
592  char *hex_iv_string, char *hex_src_string,
593  char *hex_dst_string, int cbc_result )
594 {
595  unsigned char key_str[100];
596  unsigned char iv_str[100];
597  unsigned char src_str[100];
598  unsigned char dst_str[100];
599  unsigned char output[100];
600  des3_context ctx;
601  int src_len;
602 
603  memset(key_str, 0x00, 100);
604  memset(iv_str, 0x00, 100);
605  memset(src_str, 0x00, 100);
606  memset(dst_str, 0x00, 100);
607  memset(output, 0x00, 100);
608 
609  unhexify( key_str, hex_key_string );
610  unhexify( iv_str, hex_iv_string );
611  src_len = unhexify( src_str, hex_src_string );
612 
613  if( key_count == 2 )
614  des3_set2key_dec( &ctx, key_str );
615  else if( key_count == 3 )
616  des3_set3key_dec( &ctx, key_str );
617  else
618  TEST_ASSERT( 0 );
619 
620  TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
621 
622  if( cbc_result == 0 )
623  {
624  hexify( dst_str, output, src_len );
625 
626  TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
627  }
628 }
629 #endif /* POLARSSL_CIPHER_MODE_CBC */
630 
631 void test_suite_des_key_parity_run()
632 {
633  int i, j, cnt;
634  unsigned char key[DES_KEY_SIZE];
635  unsigned int parity;
636 
637  memset( key, 0, DES_KEY_SIZE );
638  cnt = 0;
639 
640  // Iterate through all possible byte values
641  //
642  for( i = 0; i < 32; i++ )
643  {
644  for( j = 0; j < 8; j++ )
645  key[j] = cnt++;
646 
647  // Set the key parity according to the table
648  //
649  des_key_set_parity( key );
650 
651  // Check the parity with a function
652  //
653  for( j = 0; j < 8; j++ )
654  {
655  parity = key[j] ^ ( key[j] >> 4 );
656  parity = parity ^
657  ( parity >> 1 ) ^
658  ( parity >> 2 ) ^
659  ( parity >> 3 );
660  parity &= 1;
661 
662  if( parity != 1 )
663  TEST_ASSERT( 0 );
664  }
665 
666  // Check the parity with the table
667  //
668  TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
669  }
670 }
671 
672 #ifdef POLARSSL_SELF_TEST
673 void test_suite_des_selftest()
674 {
675  TEST_ASSERT( des_self_test( 0 ) == 0 );
676 }
677 #endif /* POLARSSL_SELF_TEST */
678 
679 
680 #endif /* POLARSSL_DES_C */
681 
682 
683 int dep_check( char *str )
684 {
685  if( str == NULL )
686  return( 1 );
687 
688 
689 
690  return( 1 );
691 }
692 
693 int dispatch_test(int cnt, char *params[50])
694 {
695  int ret;
696  ((void) cnt);
697  ((void) params);
698 
699 #if defined(TEST_SUITE_ACTIVE)
700  if( strcmp( params[0], "des_encrypt_ecb" ) == 0 )
701  {
702 
703  char *param1 = params[1];
704  char *param2 = params[2];
705  char *param3 = params[3];
706 
707  if( cnt != 4 )
708  {
709  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
710  return( 2 );
711  }
712 
713  if( verify_string( &param1 ) != 0 ) return( 2 );
714  if( verify_string( &param2 ) != 0 ) return( 2 );
715  if( verify_string( &param3 ) != 0 ) return( 2 );
716 
717  test_suite_des_encrypt_ecb( param1, param2, param3 );
718  return ( 0 );
719 
720  return ( 3 );
721  }
722  else
723  if( strcmp( params[0], "des_decrypt_ecb" ) == 0 )
724  {
725 
726  char *param1 = params[1];
727  char *param2 = params[2];
728  char *param3 = params[3];
729 
730  if( cnt != 4 )
731  {
732  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
733  return( 2 );
734  }
735 
736  if( verify_string( &param1 ) != 0 ) return( 2 );
737  if( verify_string( &param2 ) != 0 ) return( 2 );
738  if( verify_string( &param3 ) != 0 ) return( 2 );
739 
740  test_suite_des_decrypt_ecb( param1, param2, param3 );
741  return ( 0 );
742 
743  return ( 3 );
744  }
745  else
746  if( strcmp( params[0], "des_encrypt_cbc" ) == 0 )
747  {
748  #ifdef POLARSSL_CIPHER_MODE_CBC
749 
750  char *param1 = params[1];
751  char *param2 = params[2];
752  char *param3 = params[3];
753  char *param4 = params[4];
754  int param5;
755 
756  if( cnt != 6 )
757  {
758  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
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  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
767 
768  test_suite_des_encrypt_cbc( param1, param2, param3, param4, param5 );
769  return ( 0 );
770  #endif /* POLARSSL_CIPHER_MODE_CBC */
771 
772  return ( 3 );
773  }
774  else
775  if( strcmp( params[0], "des_decrypt_cbc" ) == 0 )
776  {
777  #ifdef POLARSSL_CIPHER_MODE_CBC
778 
779  char *param1 = params[1];
780  char *param2 = params[2];
781  char *param3 = params[3];
782  char *param4 = params[4];
783  int param5;
784 
785  if( cnt != 6 )
786  {
787  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
788  return( 2 );
789  }
790 
791  if( verify_string( &param1 ) != 0 ) return( 2 );
792  if( verify_string( &param2 ) != 0 ) return( 2 );
793  if( verify_string( &param3 ) != 0 ) return( 2 );
794  if( verify_string( &param4 ) != 0 ) return( 2 );
795  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
796 
797  test_suite_des_decrypt_cbc( param1, param2, param3, param4, param5 );
798  return ( 0 );
799  #endif /* POLARSSL_CIPHER_MODE_CBC */
800 
801  return ( 3 );
802  }
803  else
804  if( strcmp( params[0], "des3_encrypt_ecb" ) == 0 )
805  {
806 
807  int param1;
808  char *param2 = params[2];
809  char *param3 = params[3];
810  char *param4 = params[4];
811 
812  if( cnt != 5 )
813  {
814  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
815  return( 2 );
816  }
817 
818  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
819  if( verify_string( &param2 ) != 0 ) return( 2 );
820  if( verify_string( &param3 ) != 0 ) return( 2 );
821  if( verify_string( &param4 ) != 0 ) return( 2 );
822 
823  test_suite_des3_encrypt_ecb( param1, param2, param3, param4 );
824  return ( 0 );
825 
826  return ( 3 );
827  }
828  else
829  if( strcmp( params[0], "des3_decrypt_ecb" ) == 0 )
830  {
831 
832  int param1;
833  char *param2 = params[2];
834  char *param3 = params[3];
835  char *param4 = params[4];
836 
837  if( cnt != 5 )
838  {
839  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
840  return( 2 );
841  }
842 
843  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
844  if( verify_string( &param2 ) != 0 ) return( 2 );
845  if( verify_string( &param3 ) != 0 ) return( 2 );
846  if( verify_string( &param4 ) != 0 ) return( 2 );
847 
848  test_suite_des3_decrypt_ecb( param1, param2, param3, param4 );
849  return ( 0 );
850 
851  return ( 3 );
852  }
853  else
854  if( strcmp( params[0], "des3_encrypt_cbc" ) == 0 )
855  {
856  #ifdef POLARSSL_CIPHER_MODE_CBC
857 
858  int param1;
859  char *param2 = params[2];
860  char *param3 = params[3];
861  char *param4 = params[4];
862  char *param5 = params[5];
863  int param6;
864 
865  if( cnt != 7 )
866  {
867  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
868  return( 2 );
869  }
870 
871  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
872  if( verify_string( &param2 ) != 0 ) return( 2 );
873  if( verify_string( &param3 ) != 0 ) return( 2 );
874  if( verify_string( &param4 ) != 0 ) return( 2 );
875  if( verify_string( &param5 ) != 0 ) return( 2 );
876  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
877 
878  test_suite_des3_encrypt_cbc( param1, param2, param3, param4, param5, param6 );
879  return ( 0 );
880  #endif /* POLARSSL_CIPHER_MODE_CBC */
881 
882  return ( 3 );
883  }
884  else
885  if( strcmp( params[0], "des3_decrypt_cbc" ) == 0 )
886  {
887  #ifdef POLARSSL_CIPHER_MODE_CBC
888 
889  int param1;
890  char *param2 = params[2];
891  char *param3 = params[3];
892  char *param4 = params[4];
893  char *param5 = params[5];
894  int param6;
895 
896  if( cnt != 7 )
897  {
898  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
899  return( 2 );
900  }
901 
902  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
903  if( verify_string( &param2 ) != 0 ) return( 2 );
904  if( verify_string( &param3 ) != 0 ) return( 2 );
905  if( verify_string( &param4 ) != 0 ) return( 2 );
906  if( verify_string( &param5 ) != 0 ) return( 2 );
907  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
908 
909  test_suite_des3_decrypt_cbc( param1, param2, param3, param4, param5, param6 );
910  return ( 0 );
911  #endif /* POLARSSL_CIPHER_MODE_CBC */
912 
913  return ( 3 );
914  }
915  else
916  if( strcmp( params[0], "des_key_parity_run" ) == 0 )
917  {
918 
919 
920  if( cnt != 1 )
921  {
922  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
923  return( 2 );
924  }
925 
926 
927  test_suite_des_key_parity_run( );
928  return ( 0 );
929 
930  return ( 3 );
931  }
932  else
933  if( strcmp( params[0], "des_selftest" ) == 0 )
934  {
935  #ifdef POLARSSL_SELF_TEST
936 
937 
938  if( cnt != 1 )
939  {
940  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
941  return( 2 );
942  }
943 
944 
945  test_suite_des_selftest( );
946  return ( 0 );
947  #endif /* POLARSSL_SELF_TEST */
948 
949  return ( 3 );
950  }
951  else
952 
953  {
954  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
955  fflush( stdout );
956  return( 1 );
957  }
958 #else
959  return( 3 );
960 #endif
961  return( ret );
962 }
963 
964 int get_line( FILE *f, char *buf, size_t len )
965 {
966  char *ret;
967 
968  ret = fgets( buf, len, f );
969  if( ret == NULL )
970  return( -1 );
971 
972  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
973  buf[strlen(buf) - 1] = '\0';
974  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
975  buf[strlen(buf) - 1] = '\0';
976 
977  return( 0 );
978 }
979 
980 int parse_arguments( char *buf, size_t len, char *params[50] )
981 {
982  int cnt = 0, i;
983  char *cur = buf;
984  char *p = buf, *q;
985 
986  params[cnt++] = cur;
987 
988  while( *p != '\0' && p < buf + len )
989  {
990  if( *p == '\\' )
991  {
992  *p++;
993  *p++;
994  continue;
995  }
996  if( *p == ':' )
997  {
998  if( p + 1 < buf + len )
999  {
1000  cur = p + 1;
1001  params[cnt++] = cur;
1002  }
1003  *p = '\0';
1004  }
1005 
1006  *p++;
1007  }
1008 
1009  // Replace newlines, question marks and colons in strings
1010  for( i = 0; i < cnt; i++ )
1011  {
1012  p = params[i];
1013  q = params[i];
1014 
1015  while( *p != '\0' )
1016  {
1017  if( *p == '\\' && *(p + 1) == 'n' )
1018  {
1019  p += 2;
1020  *(q++) = '\n';
1021  }
1022  else if( *p == '\\' && *(p + 1) == ':' )
1023  {
1024  p += 2;
1025  *(q++) = ':';
1026  }
1027  else if( *p == '\\' && *(p + 1) == '?' )
1028  {
1029  p += 2;
1030  *(q++) = '?';
1031  }
1032  else
1033  *(q++) = *(p++);
1034  }
1035  *q = '\0';
1036  }
1037 
1038  return( cnt );
1039 }
1040 
1041 int main()
1042 {
1043  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1044  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_des.data";
1045  FILE *file;
1046  char buf[5000];
1047  char *params[50];
1048 
1049 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1050  unsigned char alloc_buf[1000000];
1051  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1052 #endif
1053 
1054  file = fopen( filename, "r" );
1055  if( file == NULL )
1056  {
1057  fprintf( stderr, "Failed to open\n" );
1058  return( 1 );
1059  }
1060 
1061  while( !feof( file ) )
1062  {
1063  int skip = 0;
1064 
1065  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1066  break;
1067  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1068  fprintf( stdout, " " );
1069  for( i = strlen( buf ) + 1; i < 67; i++ )
1070  fprintf( stdout, "." );
1071  fprintf( stdout, " " );
1072  fflush( stdout );
1073 
1074  total_tests++;
1075 
1076  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1077  break;
1078  cnt = parse_arguments( buf, strlen(buf), params );
1079 
1080  if( strcmp( params[0], "depends_on" ) == 0 )
1081  {
1082  for( i = 1; i < cnt; i++ )
1083  if( dep_check( params[i] ) != 0 )
1084  skip = 1;
1085 
1086  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1087  break;
1088  cnt = parse_arguments( buf, strlen(buf), params );
1089  }
1090 
1091  if( skip == 0 )
1092  {
1093  test_errors = 0;
1094  ret = dispatch_test( cnt, params );
1095  }
1096 
1097  if( skip == 1 || ret == 3 )
1098  {
1099  total_skipped++;
1100  fprintf( stdout, "----\n" );
1101  fflush( stdout );
1102  }
1103  else if( ret == 0 && test_errors == 0 )
1104  {
1105  fprintf( stdout, "PASS\n" );
1106  fflush( stdout );
1107  }
1108  else if( ret == 2 )
1109  {
1110  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1111  fclose(file);
1112  exit( 2 );
1113  }
1114  else
1115  total_errors++;
1116 
1117  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1118  break;
1119  if( strlen(buf) != 0 )
1120  {
1121  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1122  return( 1 );
1123  }
1124  }
1125  fclose(file);
1126 
1127  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1128  if( total_errors == 0 )
1129  fprintf( stdout, "PASSED" );
1130  else
1131  fprintf( stdout, "FAILED" );
1132 
1133  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1134  total_tests - total_errors, total_tests, total_skipped );
1135 
1136 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1137 #if defined(POLARSSL_MEMORY_DEBUG)
1138  memory_buffer_alloc_status();
1139 #endif
1140  memory_buffer_alloc_free();
1141 #endif
1142 
1143  return( total_errors != 0 );
1144 }
1145 
1146