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