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