PolarSSL v1.3.2
test_suite_aes.cbc.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 
569 
570  return( 1 );
571 }
572 
573 int dispatch_test(int cnt, char *params[50])
574 {
575  int ret;
576  ((void) cnt);
577  ((void) params);
578 
579 #if defined(TEST_SUITE_ACTIVE)
580  if( strcmp( params[0], "aes_encrypt_ecb" ) == 0 )
581  {
582 
583  char *param1 = params[1];
584  char *param2 = params[2];
585  char *param3 = params[3];
586  int param4;
587 
588  if( cnt != 5 )
589  {
590  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
591  return( 2 );
592  }
593 
594  if( verify_string( &param1 ) != 0 ) return( 2 );
595  if( verify_string( &param2 ) != 0 ) return( 2 );
596  if( verify_string( &param3 ) != 0 ) return( 2 );
597  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
598 
599  test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
600  return ( 0 );
601 
602  return ( 3 );
603  }
604  else
605  if( strcmp( params[0], "aes_decrypt_ecb" ) == 0 )
606  {
607 
608  char *param1 = params[1];
609  char *param2 = params[2];
610  char *param3 = params[3];
611  int param4;
612 
613  if( cnt != 5 )
614  {
615  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
616  return( 2 );
617  }
618 
619  if( verify_string( &param1 ) != 0 ) return( 2 );
620  if( verify_string( &param2 ) != 0 ) return( 2 );
621  if( verify_string( &param3 ) != 0 ) return( 2 );
622  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
623 
624  test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
625  return ( 0 );
626 
627  return ( 3 );
628  }
629  else
630  if( strcmp( params[0], "aes_encrypt_cbc" ) == 0 )
631  {
632  #ifdef POLARSSL_CIPHER_MODE_CBC
633 
634  char *param1 = params[1];
635  char *param2 = params[2];
636  char *param3 = params[3];
637  char *param4 = params[4];
638  int param5;
639 
640  if( cnt != 6 )
641  {
642  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
643  return( 2 );
644  }
645 
646  if( verify_string( &param1 ) != 0 ) return( 2 );
647  if( verify_string( &param2 ) != 0 ) return( 2 );
648  if( verify_string( &param3 ) != 0 ) return( 2 );
649  if( verify_string( &param4 ) != 0 ) return( 2 );
650  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
651 
652  test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
653  return ( 0 );
654  #endif /* POLARSSL_CIPHER_MODE_CBC */
655 
656  return ( 3 );
657  }
658  else
659  if( strcmp( params[0], "aes_decrypt_cbc" ) == 0 )
660  {
661  #ifdef POLARSSL_CIPHER_MODE_CBC
662 
663  char *param1 = params[1];
664  char *param2 = params[2];
665  char *param3 = params[3];
666  char *param4 = params[4];
667  int param5;
668 
669  if( cnt != 6 )
670  {
671  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
672  return( 2 );
673  }
674 
675  if( verify_string( &param1 ) != 0 ) return( 2 );
676  if( verify_string( &param2 ) != 0 ) return( 2 );
677  if( verify_string( &param3 ) != 0 ) return( 2 );
678  if( verify_string( &param4 ) != 0 ) return( 2 );
679  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
680 
681  test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
682  return ( 0 );
683  #endif /* POLARSSL_CIPHER_MODE_CBC */
684 
685  return ( 3 );
686  }
687  else
688  if( strcmp( params[0], "aes_encrypt_cfb128" ) == 0 )
689  {
690  #ifdef POLARSSL_CIPHER_MODE_CFB
691 
692  char *param1 = params[1];
693  char *param2 = params[2];
694  char *param3 = params[3];
695  char *param4 = params[4];
696 
697  if( cnt != 5 )
698  {
699  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
700  return( 2 );
701  }
702 
703  if( verify_string( &param1 ) != 0 ) return( 2 );
704  if( verify_string( &param2 ) != 0 ) return( 2 );
705  if( verify_string( &param3 ) != 0 ) return( 2 );
706  if( verify_string( &param4 ) != 0 ) return( 2 );
707 
708  test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
709  return ( 0 );
710  #endif /* POLARSSL_CIPHER_MODE_CFB */
711 
712  return ( 3 );
713  }
714  else
715  if( strcmp( params[0], "aes_decrypt_cfb128" ) == 0 )
716  {
717  #ifdef POLARSSL_CIPHER_MODE_CFB
718 
719  char *param1 = params[1];
720  char *param2 = params[2];
721  char *param3 = params[3];
722  char *param4 = params[4];
723 
724  if( cnt != 5 )
725  {
726  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
727  return( 2 );
728  }
729 
730  if( verify_string( &param1 ) != 0 ) return( 2 );
731  if( verify_string( &param2 ) != 0 ) return( 2 );
732  if( verify_string( &param3 ) != 0 ) return( 2 );
733  if( verify_string( &param4 ) != 0 ) return( 2 );
734 
735  test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
736  return ( 0 );
737  #endif /* POLARSSL_CIPHER_MODE_CFB */
738 
739  return ( 3 );
740  }
741  else
742  if( strcmp( params[0], "aes_selftest" ) == 0 )
743  {
744  #ifdef POLARSSL_SELF_TEST
745 
746 
747  if( cnt != 1 )
748  {
749  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
750  return( 2 );
751  }
752 
753 
754  test_suite_aes_selftest( );
755  return ( 0 );
756  #endif /* POLARSSL_SELF_TEST */
757 
758  return ( 3 );
759  }
760  else
761 
762  {
763  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
764  fflush( stdout );
765  return( 1 );
766  }
767 #else
768  return( 3 );
769 #endif
770  return( ret );
771 }
772 
773 int get_line( FILE *f, char *buf, size_t len )
774 {
775  char *ret;
776 
777  ret = fgets( buf, len, f );
778  if( ret == NULL )
779  return( -1 );
780 
781  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
782  buf[strlen(buf) - 1] = '\0';
783  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
784  buf[strlen(buf) - 1] = '\0';
785 
786  return( 0 );
787 }
788 
789 int parse_arguments( char *buf, size_t len, char *params[50] )
790 {
791  int cnt = 0, i;
792  char *cur = buf;
793  char *p = buf, *q;
794 
795  params[cnt++] = cur;
796 
797  while( *p != '\0' && p < buf + len )
798  {
799  if( *p == '\\' )
800  {
801  *p++;
802  *p++;
803  continue;
804  }
805  if( *p == ':' )
806  {
807  if( p + 1 < buf + len )
808  {
809  cur = p + 1;
810  params[cnt++] = cur;
811  }
812  *p = '\0';
813  }
814 
815  *p++;
816  }
817 
818  // Replace newlines, question marks and colons in strings
819  for( i = 0; i < cnt; i++ )
820  {
821  p = params[i];
822  q = params[i];
823 
824  while( *p != '\0' )
825  {
826  if( *p == '\\' && *(p + 1) == 'n' )
827  {
828  p += 2;
829  *(q++) = '\n';
830  }
831  else if( *p == '\\' && *(p + 1) == ':' )
832  {
833  p += 2;
834  *(q++) = ':';
835  }
836  else if( *p == '\\' && *(p + 1) == '?' )
837  {
838  p += 2;
839  *(q++) = '?';
840  }
841  else
842  *(q++) = *(p++);
843  }
844  *q = '\0';
845  }
846 
847  return( cnt );
848 }
849 
850 int main()
851 {
852  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
853  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_aes.cbc.data";
854  FILE *file;
855  char buf[5000];
856  char *params[50];
857 
858 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
859  unsigned char alloc_buf[1000000];
860  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
861 #endif
862 
863  file = fopen( filename, "r" );
864  if( file == NULL )
865  {
866  fprintf( stderr, "Failed to open\n" );
867  return( 1 );
868  }
869 
870  while( !feof( file ) )
871  {
872  int skip = 0;
873 
874  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
875  break;
876  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
877  fprintf( stdout, " " );
878  for( i = strlen( buf ) + 1; i < 67; i++ )
879  fprintf( stdout, "." );
880  fprintf( stdout, " " );
881  fflush( stdout );
882 
883  total_tests++;
884 
885  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
886  break;
887  cnt = parse_arguments( buf, strlen(buf), params );
888 
889  if( strcmp( params[0], "depends_on" ) == 0 )
890  {
891  for( i = 1; i < cnt; i++ )
892  if( dep_check( params[i] ) != 0 )
893  skip = 1;
894 
895  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
896  break;
897  cnt = parse_arguments( buf, strlen(buf), params );
898  }
899 
900  if( skip == 0 )
901  {
902  test_errors = 0;
903  ret = dispatch_test( cnt, params );
904  }
905 
906  if( skip == 1 || ret == 3 )
907  {
908  total_skipped++;
909  fprintf( stdout, "----\n" );
910  fflush( stdout );
911  }
912  else if( ret == 0 && test_errors == 0 )
913  {
914  fprintf( stdout, "PASS\n" );
915  fflush( stdout );
916  }
917  else if( ret == 2 )
918  {
919  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
920  fclose(file);
921  exit( 2 );
922  }
923  else
924  total_errors++;
925 
926  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
927  break;
928  if( strlen(buf) != 0 )
929  {
930  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
931  return( 1 );
932  }
933  }
934  fclose(file);
935 
936  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
937  if( total_errors == 0 )
938  fprintf( stdout, "PASSED" );
939  else
940  fprintf( stdout, "FAILED" );
941 
942  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
943  total_tests - total_errors, total_tests, total_skipped );
944 
945 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
946 #if defined(POLARSSL_MEMORY_DEBUG)
947  memory_buffer_alloc_status();
948 #endif
949  memory_buffer_alloc_free();
950 #endif
951 
952  return( total_errors != 0 );
953 }
954 
955 
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
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_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
#define AES_DECRYPT
Definition: aes.h:42
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[])
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
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.
static int test_errors
int parse_arguments(char *buf, size_t len, char *params[50])
#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
int verify_string(char **str)
static int unhexify(unsigned char *obuf, const char *ibuf)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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_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)