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