PolarSSL v1.3.2
test_suite_aes.cfb.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_AES_C
4 
5 #include <polarssl/aes.h>
6 #endif /* POLARSSL_AES_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_AES_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 
361 
362  printf( "Expected integer for parameter and got: %s\n", str );
363  return( -1 );
364 }
365 
366 void test_suite_aes_encrypt_ecb( char *hex_key_string, char *hex_src_string,
367  char *hex_dst_string, int setkey_result )
368 {
369  unsigned char key_str[100];
370  unsigned char src_str[100];
371  unsigned char dst_str[100];
372  unsigned char output[100];
373  aes_context ctx;
374  int key_len;
375 
376  memset(key_str, 0x00, 100);
377  memset(src_str, 0x00, 100);
378  memset(dst_str, 0x00, 100);
379  memset(output, 0x00, 100);
380 
381  key_len = unhexify( key_str, hex_key_string );
382  unhexify( src_str, hex_src_string );
383 
384  TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
385  if( setkey_result == 0 )
386  {
387  TEST_ASSERT( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 );
388  hexify( dst_str, output, 16 );
389 
390  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
391  }
392 }
393 
394 void test_suite_aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
395  char *hex_dst_string, int setkey_result )
396 {
397  unsigned char key_str[100];
398  unsigned char src_str[100];
399  unsigned char dst_str[100];
400  unsigned char output[100];
401  aes_context ctx;
402  int key_len;
403 
404  memset(key_str, 0x00, 100);
405  memset(src_str, 0x00, 100);
406  memset(dst_str, 0x00, 100);
407  memset(output, 0x00, 100);
408 
409  key_len = unhexify( key_str, hex_key_string );
410  unhexify( src_str, hex_src_string );
411 
412  TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
413  if( setkey_result == 0 )
414  {
415  TEST_ASSERT( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 );
416  hexify( dst_str, output, 16 );
417 
418  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
419  }
420 }
421 
422 #ifdef POLARSSL_CIPHER_MODE_CBC
423 void test_suite_aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
424  char *hex_src_string, char *hex_dst_string,
425  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  aes_context ctx;
433  int key_len, data_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  key_len = unhexify( key_str, hex_key_string );
442  unhexify( iv_str, hex_iv_string );
443  data_len = unhexify( src_str, hex_src_string );
444 
445  aes_setkey_enc( &ctx, key_str, key_len * 8 );
446  TEST_ASSERT( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
447  if( cbc_result == 0 )
448  {
449  hexify( dst_str, output, data_len );
450 
451  TEST_ASSERT( strcmp( (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_aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
458  char *hex_src_string, char *hex_dst_string,
459  int cbc_result )
460 {
461  unsigned char key_str[100];
462  unsigned char iv_str[100];
463  unsigned char src_str[100];
464  unsigned char dst_str[100];
465  unsigned char output[100];
466  aes_context ctx;
467  int key_len, data_len;
468 
469  memset(key_str, 0x00, 100);
470  memset(iv_str, 0x00, 100);
471  memset(src_str, 0x00, 100);
472  memset(dst_str, 0x00, 100);
473  memset(output, 0x00, 100);
474 
475  key_len = unhexify( key_str, hex_key_string );
476  unhexify( iv_str, hex_iv_string );
477  data_len = unhexify( src_str, hex_src_string );
478 
479  aes_setkey_dec( &ctx, key_str, key_len * 8 );
480  TEST_ASSERT( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
481  if( cbc_result == 0)
482  {
483  hexify( dst_str, output, data_len );
484 
485  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
486  }
487 }
488 #endif /* POLARSSL_CIPHER_MODE_CBC */
489 
490 #ifdef POLARSSL_CIPHER_MODE_CFB
491 void test_suite_aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
492  char *hex_src_string, char *hex_dst_string )
493 {
494  unsigned char key_str[100];
495  unsigned char iv_str[100];
496  unsigned char src_str[100];
497  unsigned char dst_str[100];
498  unsigned char output[100];
499  aes_context ctx;
500  size_t iv_offset = 0;
501  int key_len;
502 
503  memset(key_str, 0x00, 100);
504  memset(iv_str, 0x00, 100);
505  memset(src_str, 0x00, 100);
506  memset(dst_str, 0x00, 100);
507  memset(output, 0x00, 100);
508 
509  key_len = unhexify( key_str, hex_key_string );
510  unhexify( iv_str, hex_iv_string );
511  unhexify( src_str, hex_src_string );
512 
513  aes_setkey_enc( &ctx, key_str, key_len * 8 );
514  TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
515  hexify( dst_str, output, 16 );
516 
517  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
518 }
519 #endif /* POLARSSL_CIPHER_MODE_CFB */
520 
521 #ifdef POLARSSL_CIPHER_MODE_CFB
522 void test_suite_aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
523  char *hex_src_string, char *hex_dst_string )
524 {
525  unsigned char key_str[100];
526  unsigned char iv_str[100];
527  unsigned char src_str[100];
528  unsigned char dst_str[100];
529  unsigned char output[100];
530  aes_context ctx;
531  size_t iv_offset = 0;
532  int key_len;
533 
534  memset(key_str, 0x00, 100);
535  memset(iv_str, 0x00, 100);
536  memset(src_str, 0x00, 100);
537  memset(dst_str, 0x00, 100);
538  memset(output, 0x00, 100);
539 
540  key_len = unhexify( key_str, hex_key_string );
541  unhexify( iv_str, hex_iv_string );
542  unhexify( src_str, hex_src_string );
543 
544  aes_setkey_enc( &ctx, key_str, key_len * 8 );
545  TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
546  hexify( dst_str, output, 16 );
547 
548  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
549 }
550 #endif /* POLARSSL_CIPHER_MODE_CFB */
551 
552 #ifdef POLARSSL_SELF_TEST
553 void test_suite_aes_selftest()
554 {
555  TEST_ASSERT( aes_self_test( 0 ) == 0 );
556 }
557 #endif /* POLARSSL_SELF_TEST */
558 
559 
560 #endif /* POLARSSL_AES_C */
561 
562 
563 int dep_check( char *str )
564 {
565  if( str == NULL )
566  return( 1 );
567 
568  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
569  {
570 #if defined(POLARSSL_CIPHER_MODE_CFB)
571  return( 0 );
572 #else
573  return( 1 );
574 #endif
575  }
576 
577 
578  return( 1 );
579 }
580 
581 int dispatch_test(int cnt, char *params[50])
582 {
583  int ret;
584  ((void) cnt);
585  ((void) params);
586 
587 #if defined(TEST_SUITE_ACTIVE)
588  if( strcmp( params[0], "aes_encrypt_ecb" ) == 0 )
589  {
590 
591  char *param1 = params[1];
592  char *param2 = params[2];
593  char *param3 = params[3];
594  int param4;
595 
596  if( cnt != 5 )
597  {
598  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
599  return( 2 );
600  }
601 
602  if( verify_string( &param1 ) != 0 ) return( 2 );
603  if( verify_string( &param2 ) != 0 ) return( 2 );
604  if( verify_string( &param3 ) != 0 ) return( 2 );
605  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
606 
607  test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
608  return ( 0 );
609 
610  return ( 3 );
611  }
612  else
613  if( strcmp( params[0], "aes_decrypt_ecb" ) == 0 )
614  {
615 
616  char *param1 = params[1];
617  char *param2 = params[2];
618  char *param3 = params[3];
619  int param4;
620 
621  if( cnt != 5 )
622  {
623  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
624  return( 2 );
625  }
626 
627  if( verify_string( &param1 ) != 0 ) return( 2 );
628  if( verify_string( &param2 ) != 0 ) return( 2 );
629  if( verify_string( &param3 ) != 0 ) return( 2 );
630  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
631 
632  test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
633  return ( 0 );
634 
635  return ( 3 );
636  }
637  else
638  if( strcmp( params[0], "aes_encrypt_cbc" ) == 0 )
639  {
640  #ifdef POLARSSL_CIPHER_MODE_CBC
641 
642  char *param1 = params[1];
643  char *param2 = params[2];
644  char *param3 = params[3];
645  char *param4 = params[4];
646  int param5;
647 
648  if( cnt != 6 )
649  {
650  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
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_string( &param4 ) != 0 ) return( 2 );
658  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
659 
660  test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
661  return ( 0 );
662  #endif /* POLARSSL_CIPHER_MODE_CBC */
663 
664  return ( 3 );
665  }
666  else
667  if( strcmp( params[0], "aes_decrypt_cbc" ) == 0 )
668  {
669  #ifdef POLARSSL_CIPHER_MODE_CBC
670 
671  char *param1 = params[1];
672  char *param2 = params[2];
673  char *param3 = params[3];
674  char *param4 = params[4];
675  int param5;
676 
677  if( cnt != 6 )
678  {
679  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
680  return( 2 );
681  }
682 
683  if( verify_string( &param1 ) != 0 ) return( 2 );
684  if( verify_string( &param2 ) != 0 ) return( 2 );
685  if( verify_string( &param3 ) != 0 ) return( 2 );
686  if( verify_string( &param4 ) != 0 ) return( 2 );
687  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
688 
689  test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
690  return ( 0 );
691  #endif /* POLARSSL_CIPHER_MODE_CBC */
692 
693  return ( 3 );
694  }
695  else
696  if( strcmp( params[0], "aes_encrypt_cfb128" ) == 0 )
697  {
698  #ifdef POLARSSL_CIPHER_MODE_CFB
699 
700  char *param1 = params[1];
701  char *param2 = params[2];
702  char *param3 = params[3];
703  char *param4 = params[4];
704 
705  if( cnt != 5 )
706  {
707  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
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  if( verify_string( &param4 ) != 0 ) return( 2 );
715 
716  test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
717  return ( 0 );
718  #endif /* POLARSSL_CIPHER_MODE_CFB */
719 
720  return ( 3 );
721  }
722  else
723  if( strcmp( params[0], "aes_decrypt_cfb128" ) == 0 )
724  {
725  #ifdef POLARSSL_CIPHER_MODE_CFB
726 
727  char *param1 = params[1];
728  char *param2 = params[2];
729  char *param3 = params[3];
730  char *param4 = params[4];
731 
732  if( cnt != 5 )
733  {
734  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
735  return( 2 );
736  }
737 
738  if( verify_string( &param1 ) != 0 ) return( 2 );
739  if( verify_string( &param2 ) != 0 ) return( 2 );
740  if( verify_string( &param3 ) != 0 ) return( 2 );
741  if( verify_string( &param4 ) != 0 ) return( 2 );
742 
743  test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
744  return ( 0 );
745  #endif /* POLARSSL_CIPHER_MODE_CFB */
746 
747  return ( 3 );
748  }
749  else
750  if( strcmp( params[0], "aes_selftest" ) == 0 )
751  {
752  #ifdef POLARSSL_SELF_TEST
753 
754 
755  if( cnt != 1 )
756  {
757  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
758  return( 2 );
759  }
760 
761 
762  test_suite_aes_selftest( );
763  return ( 0 );
764  #endif /* POLARSSL_SELF_TEST */
765 
766  return ( 3 );
767  }
768  else
769 
770  {
771  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
772  fflush( stdout );
773  return( 1 );
774  }
775 #else
776  return( 3 );
777 #endif
778  return( ret );
779 }
780 
781 int get_line( FILE *f, char *buf, size_t len )
782 {
783  char *ret;
784 
785  ret = fgets( buf, len, f );
786  if( ret == NULL )
787  return( -1 );
788 
789  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
790  buf[strlen(buf) - 1] = '\0';
791  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
792  buf[strlen(buf) - 1] = '\0';
793 
794  return( 0 );
795 }
796 
797 int parse_arguments( char *buf, size_t len, char *params[50] )
798 {
799  int cnt = 0, i;
800  char *cur = buf;
801  char *p = buf, *q;
802 
803  params[cnt++] = cur;
804 
805  while( *p != '\0' && p < buf + len )
806  {
807  if( *p == '\\' )
808  {
809  *p++;
810  *p++;
811  continue;
812  }
813  if( *p == ':' )
814  {
815  if( p + 1 < buf + len )
816  {
817  cur = p + 1;
818  params[cnt++] = cur;
819  }
820  *p = '\0';
821  }
822 
823  *p++;
824  }
825 
826  // Replace newlines, question marks and colons in strings
827  for( i = 0; i < cnt; i++ )
828  {
829  p = params[i];
830  q = params[i];
831 
832  while( *p != '\0' )
833  {
834  if( *p == '\\' && *(p + 1) == 'n' )
835  {
836  p += 2;
837  *(q++) = '\n';
838  }
839  else if( *p == '\\' && *(p + 1) == ':' )
840  {
841  p += 2;
842  *(q++) = ':';
843  }
844  else if( *p == '\\' && *(p + 1) == '?' )
845  {
846  p += 2;
847  *(q++) = '?';
848  }
849  else
850  *(q++) = *(p++);
851  }
852  *q = '\0';
853  }
854 
855  return( cnt );
856 }
857 
858 int main()
859 {
860  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
861  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_aes.cfb.data";
862  FILE *file;
863  char buf[5000];
864  char *params[50];
865 
866 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
867  unsigned char alloc_buf[1000000];
868  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
869 #endif
870 
871  file = fopen( filename, "r" );
872  if( file == NULL )
873  {
874  fprintf( stderr, "Failed to open\n" );
875  return( 1 );
876  }
877 
878  while( !feof( file ) )
879  {
880  int skip = 0;
881 
882  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
883  break;
884  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
885  fprintf( stdout, " " );
886  for( i = strlen( buf ) + 1; i < 67; i++ )
887  fprintf( stdout, "." );
888  fprintf( stdout, " " );
889  fflush( stdout );
890 
891  total_tests++;
892 
893  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
894  break;
895  cnt = parse_arguments( buf, strlen(buf), params );
896 
897  if( strcmp( params[0], "depends_on" ) == 0 )
898  {
899  for( i = 1; i < cnt; i++ )
900  if( dep_check( params[i] ) != 0 )
901  skip = 1;
902 
903  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
904  break;
905  cnt = parse_arguments( buf, strlen(buf), params );
906  }
907 
908  if( skip == 0 )
909  {
910  test_errors = 0;
911  ret = dispatch_test( cnt, params );
912  }
913 
914  if( skip == 1 || ret == 3 )
915  {
916  total_skipped++;
917  fprintf( stdout, "----\n" );
918  fflush( stdout );
919  }
920  else if( ret == 0 && test_errors == 0 )
921  {
922  fprintf( stdout, "PASS\n" );
923  fflush( stdout );
924  }
925  else if( ret == 2 )
926  {
927  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
928  fclose(file);
929  exit( 2 );
930  }
931  else
932  total_errors++;
933 
934  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
935  break;
936  if( strlen(buf) != 0 )
937  {
938  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
939  return( 1 );
940  }
941  }
942  fclose(file);
943 
944  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
945  if( total_errors == 0 )
946  fprintf( stdout, "PASSED" );
947  else
948  fprintf( stdout, "FAILED" );
949 
950  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
951  total_tests - total_errors, total_tests, total_skipped );
952 
953 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
954 #if defined(POLARSSL_MEMORY_DEBUG)
955  memory_buffer_alloc_status();
956 #endif
957  memory_buffer_alloc_free();
958 #endif
959 
960  return( total_errors != 0 );
961 }
962 
963 
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define PUT_UINT32_BE(n, b, i)
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
int aes_crypt_cfb128(aes_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CFB128 buffer encryption/decryption.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
#define AES_DECRYPT
Definition: aes.h:42
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
AES context structure.
Definition: aes.h:58
Configuration options (set of defines)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
int aes_crypt_cbc(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
AES block cipher.
int aes_self_test(int verbose)
Checkup routine.
int parse_arguments(char *buf, size_t len, char *params[50])
static int test_errors
#define AES_ENCRYPT
Definition: aes.h:41
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 unhexify(unsigned char *obuf, const char *ibuf)
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
int verify_int(char *str, int *value)
int aes_setkey_enc(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (encryption)
int aes_crypt_ecb(aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
AES-ECB block encryption/decryption.
int get_line(FILE *f, char *buf, size_t len)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.