PolarSSL v1.3.9
test_suite_aes.cfb.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  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
698  {
699 #if defined(POLARSSL_CIPHER_MODE_CFB)
700  return( 0 );
701 #else
702  return( 1 );
703 #endif
704  }
705 
706 
707  return( 1 );
708 }
709 
710 int dispatch_test(int cnt, char *params[50])
711 {
712  int ret;
713  ((void) cnt);
714  ((void) params);
715 
716 #if defined(TEST_SUITE_ACTIVE)
717  if( strcmp( params[0], "aes_encrypt_ecb" ) == 0 )
718  {
719 
720  char *param1 = params[1];
721  char *param2 = params[2];
722  char *param3 = params[3];
723  int param4;
724 
725  if( cnt != 5 )
726  {
727  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
728  return( 2 );
729  }
730 
731  if( verify_string( &param1 ) != 0 ) return( 2 );
732  if( verify_string( &param2 ) != 0 ) return( 2 );
733  if( verify_string( &param3 ) != 0 ) return( 2 );
734  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
735 
736  test_suite_aes_encrypt_ecb( param1, param2, param3, param4 );
737  return ( 0 );
738 
739  return ( 3 );
740  }
741  else
742  if( strcmp( params[0], "aes_decrypt_ecb" ) == 0 )
743  {
744 
745  char *param1 = params[1];
746  char *param2 = params[2];
747  char *param3 = params[3];
748  int param4;
749 
750  if( cnt != 5 )
751  {
752  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
753  return( 2 );
754  }
755 
756  if( verify_string( &param1 ) != 0 ) return( 2 );
757  if( verify_string( &param2 ) != 0 ) return( 2 );
758  if( verify_string( &param3 ) != 0 ) return( 2 );
759  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
760 
761  test_suite_aes_decrypt_ecb( param1, param2, param3, param4 );
762  return ( 0 );
763 
764  return ( 3 );
765  }
766  else
767  if( strcmp( params[0], "aes_encrypt_cbc" ) == 0 )
768  {
769  #ifdef POLARSSL_CIPHER_MODE_CBC
770 
771  char *param1 = params[1];
772  char *param2 = params[2];
773  char *param3 = params[3];
774  char *param4 = params[4];
775  int param5;
776 
777  if( cnt != 6 )
778  {
779  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
780  return( 2 );
781  }
782 
783  if( verify_string( &param1 ) != 0 ) return( 2 );
784  if( verify_string( &param2 ) != 0 ) return( 2 );
785  if( verify_string( &param3 ) != 0 ) return( 2 );
786  if( verify_string( &param4 ) != 0 ) return( 2 );
787  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
788 
789  test_suite_aes_encrypt_cbc( param1, param2, param3, param4, param5 );
790  return ( 0 );
791  #endif /* POLARSSL_CIPHER_MODE_CBC */
792 
793  return ( 3 );
794  }
795  else
796  if( strcmp( params[0], "aes_decrypt_cbc" ) == 0 )
797  {
798  #ifdef POLARSSL_CIPHER_MODE_CBC
799 
800  char *param1 = params[1];
801  char *param2 = params[2];
802  char *param3 = params[3];
803  char *param4 = params[4];
804  int param5;
805 
806  if( cnt != 6 )
807  {
808  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
809  return( 2 );
810  }
811 
812  if( verify_string( &param1 ) != 0 ) return( 2 );
813  if( verify_string( &param2 ) != 0 ) return( 2 );
814  if( verify_string( &param3 ) != 0 ) return( 2 );
815  if( verify_string( &param4 ) != 0 ) return( 2 );
816  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
817 
818  test_suite_aes_decrypt_cbc( param1, param2, param3, param4, param5 );
819  return ( 0 );
820  #endif /* POLARSSL_CIPHER_MODE_CBC */
821 
822  return ( 3 );
823  }
824  else
825  if( strcmp( params[0], "aes_encrypt_cfb128" ) == 0 )
826  {
827  #ifdef POLARSSL_CIPHER_MODE_CFB
828 
829  char *param1 = params[1];
830  char *param2 = params[2];
831  char *param3 = params[3];
832  char *param4 = params[4];
833 
834  if( cnt != 5 )
835  {
836  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
837  return( 2 );
838  }
839 
840  if( verify_string( &param1 ) != 0 ) return( 2 );
841  if( verify_string( &param2 ) != 0 ) return( 2 );
842  if( verify_string( &param3 ) != 0 ) return( 2 );
843  if( verify_string( &param4 ) != 0 ) return( 2 );
844 
845  test_suite_aes_encrypt_cfb128( param1, param2, param3, param4 );
846  return ( 0 );
847  #endif /* POLARSSL_CIPHER_MODE_CFB */
848 
849  return ( 3 );
850  }
851  else
852  if( strcmp( params[0], "aes_decrypt_cfb128" ) == 0 )
853  {
854  #ifdef POLARSSL_CIPHER_MODE_CFB
855 
856  char *param1 = params[1];
857  char *param2 = params[2];
858  char *param3 = params[3];
859  char *param4 = params[4];
860 
861  if( cnt != 5 )
862  {
863  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
864  return( 2 );
865  }
866 
867  if( verify_string( &param1 ) != 0 ) return( 2 );
868  if( verify_string( &param2 ) != 0 ) return( 2 );
869  if( verify_string( &param3 ) != 0 ) return( 2 );
870  if( verify_string( &param4 ) != 0 ) return( 2 );
871 
872  test_suite_aes_decrypt_cfb128( param1, param2, param3, param4 );
873  return ( 0 );
874  #endif /* POLARSSL_CIPHER_MODE_CFB */
875 
876  return ( 3 );
877  }
878  else
879  if( strcmp( params[0], "aes_encrypt_cfb8" ) == 0 )
880  {
881  #ifdef POLARSSL_CIPHER_MODE_CFB
882 
883  char *param1 = params[1];
884  char *param2 = params[2];
885  char *param3 = params[3];
886  char *param4 = params[4];
887 
888  if( cnt != 5 )
889  {
890  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
891  return( 2 );
892  }
893 
894  if( verify_string( &param1 ) != 0 ) return( 2 );
895  if( verify_string( &param2 ) != 0 ) return( 2 );
896  if( verify_string( &param3 ) != 0 ) return( 2 );
897  if( verify_string( &param4 ) != 0 ) return( 2 );
898 
899  test_suite_aes_encrypt_cfb8( param1, param2, param3, param4 );
900  return ( 0 );
901  #endif /* POLARSSL_CIPHER_MODE_CFB */
902 
903  return ( 3 );
904  }
905  else
906  if( strcmp( params[0], "aes_decrypt_cfb8" ) == 0 )
907  {
908  #ifdef POLARSSL_CIPHER_MODE_CFB
909 
910  char *param1 = params[1];
911  char *param2 = params[2];
912  char *param3 = params[3];
913  char *param4 = params[4];
914 
915  if( cnt != 5 )
916  {
917  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
918  return( 2 );
919  }
920 
921  if( verify_string( &param1 ) != 0 ) return( 2 );
922  if( verify_string( &param2 ) != 0 ) return( 2 );
923  if( verify_string( &param3 ) != 0 ) return( 2 );
924  if( verify_string( &param4 ) != 0 ) return( 2 );
925 
926  test_suite_aes_decrypt_cfb8( param1, param2, param3, param4 );
927  return ( 0 );
928  #endif /* POLARSSL_CIPHER_MODE_CFB */
929 
930  return ( 3 );
931  }
932  else
933  if( strcmp( params[0], "aes_selftest" ) == 0 )
934  {
935  #ifdef POLARSSL_SELF_TEST
936 
937 
938  if( cnt != 1 )
939  {
940  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
941  return( 2 );
942  }
943 
944 
945  test_suite_aes_selftest( );
946  return ( 0 );
947  #endif /* POLARSSL_SELF_TEST */
948 
949  return ( 3 );
950  }
951  else
952 
953  {
954  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
955  fflush( stdout );
956  return( 1 );
957  }
958 #else
959  return( 3 );
960 #endif
961  return( ret );
962 }
963 
964 int get_line( FILE *f, char *buf, size_t len )
965 {
966  char *ret;
967 
968  ret = fgets( buf, len, f );
969  if( ret == NULL )
970  return( -1 );
971 
972  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
973  buf[strlen(buf) - 1] = '\0';
974  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
975  buf[strlen(buf) - 1] = '\0';
976 
977  return( 0 );
978 }
979 
980 int parse_arguments( char *buf, size_t len, char *params[50] )
981 {
982  int cnt = 0, i;
983  char *cur = buf;
984  char *p = buf, *q;
985 
986  params[cnt++] = cur;
987 
988  while( *p != '\0' && p < buf + len )
989  {
990  if( *p == '\\' )
991  {
992  p++;
993  p++;
994  continue;
995  }
996  if( *p == ':' )
997  {
998  if( p + 1 < buf + len )
999  {
1000  cur = p + 1;
1001  params[cnt++] = cur;
1002  }
1003  *p = '\0';
1004  }
1005 
1006  p++;
1007  }
1008 
1009  // Replace newlines, question marks and colons in strings
1010  for( i = 0; i < cnt; i++ )
1011  {
1012  p = params[i];
1013  q = params[i];
1014 
1015  while( *p != '\0' )
1016  {
1017  if( *p == '\\' && *(p + 1) == 'n' )
1018  {
1019  p += 2;
1020  *(q++) = '\n';
1021  }
1022  else if( *p == '\\' && *(p + 1) == ':' )
1023  {
1024  p += 2;
1025  *(q++) = ':';
1026  }
1027  else if( *p == '\\' && *(p + 1) == '?' )
1028  {
1029  p += 2;
1030  *(q++) = '?';
1031  }
1032  else
1033  *(q++) = *(p++);
1034  }
1035  *q = '\0';
1036  }
1037 
1038  return( cnt );
1039 }
1040 
1041 int main()
1042 {
1043  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1044  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_aes.cfb.data";
1045  FILE *file;
1046  char buf[5000];
1047  char *params[50];
1048 
1049 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1050  unsigned char alloc_buf[1000000];
1051  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1052 #endif
1053 
1054  file = fopen( filename, "r" );
1055  if( file == NULL )
1056  {
1057  fprintf( stderr, "Failed to open\n" );
1058  return( 1 );
1059  }
1060 
1061  while( !feof( file ) )
1062  {
1063  int skip = 0;
1064 
1065  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1066  break;
1067  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1068  fprintf( stdout, " " );
1069  for( i = strlen( buf ) + 1; i < 67; i++ )
1070  fprintf( stdout, "." );
1071  fprintf( stdout, " " );
1072  fflush( stdout );
1073 
1074  total_tests++;
1075 
1076  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1077  break;
1078  cnt = parse_arguments( buf, strlen(buf), params );
1079 
1080  if( strcmp( params[0], "depends_on" ) == 0 )
1081  {
1082  for( i = 1; i < cnt; i++ )
1083  if( dep_check( params[i] ) != 0 )
1084  skip = 1;
1085 
1086  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1087  break;
1088  cnt = parse_arguments( buf, strlen(buf), params );
1089  }
1090 
1091  if( skip == 0 )
1092  {
1093  test_errors = 0;
1094  ret = dispatch_test( cnt, params );
1095  }
1096 
1097  if( skip == 1 || ret == 3 )
1098  {
1099  total_skipped++;
1100  fprintf( stdout, "----\n" );
1101  fflush( stdout );
1102  }
1103  else if( ret == 0 && test_errors == 0 )
1104  {
1105  fprintf( stdout, "PASS\n" );
1106  fflush( stdout );
1107  }
1108  else if( ret == 2 )
1109  {
1110  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1111  fclose(file);
1112  exit( 2 );
1113  }
1114  else
1115  total_errors++;
1116 
1117  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1118  break;
1119  if( strlen(buf) != 0 )
1120  {
1121  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1122  return( 1 );
1123  }
1124  }
1125  fclose(file);
1126 
1127  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1128  if( total_errors == 0 )
1129  fprintf( stdout, "PASSED" );
1130  else
1131  fprintf( stdout, "FAILED" );
1132 
1133  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1134  total_tests - total_errors, total_tests, total_skipped );
1135 
1136 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1137 #if defined(POLARSSL_MEMORY_DEBUG)
1138  memory_buffer_alloc_status();
1139 #endif
1141 #endif
1142 
1143  return( total_errors != 0 );
1144 }
1145 
1146 
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int get_line(FILE *f, char *buf, size_t len)
#define PUT_UINT32_BE(n, b, i)
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
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 unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
#define polarssl_malloc
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:47
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:68
Configuration options (set of defines)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
int dep_check(char *str)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#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.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int main()
int aes_crypt_cfb8(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CFB8 buffer encryption/decryption.
static int test_errors
#define AES_ENCRYPT
Definition: aes.h:46
static int unhexify(unsigned char *obuf, const char *ibuf)
int verify_string(char **str)
int parse_arguments(char *buf, size_t len, char *params[50])
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void aes_free(aes_context *ctx)
Clear AES context.
unsigned char * buf
int dispatch_test(int cnt, char *params[50])
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.
void aes_init(aes_context *ctx)
Initialize AES context.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.