PolarSSL v1.3.9
test_suite_gcm.aes256_de.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_GCM_C
8 
9 #include <polarssl/gcm.h>
10 #endif /* POLARSSL_GCM_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_GCM_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_CIPHER_ID_AES" ) == 0 )
395  {
396  *value = ( POLARSSL_CIPHER_ID_AES );
397  return( 0 );
398  }
399 
400 
401  printf( "Expected integer for parameter and got: %s\n", str );
402  return( -1 );
403 }
404 
405 void test_suite_gcm_encrypt_and_tag( int cipher_id,
406  char *hex_key_string, char *hex_src_string,
407  char *hex_iv_string, char *hex_add_string,
408  char *hex_dst_string, int tag_len_bits,
409  char *hex_tag_string, int init_result )
410 {
411  unsigned char key_str[128];
412  unsigned char src_str[128];
413  unsigned char dst_str[257];
414  unsigned char iv_str[128];
415  unsigned char add_str[128];
416  unsigned char tag_str[128];
417  unsigned char output[128];
418  unsigned char tag_output[16];
419  gcm_context ctx;
420  unsigned int key_len;
421  size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
422 
423  memset(key_str, 0x00, 128);
424  memset(src_str, 0x00, 128);
425  memset(dst_str, 0x00, 257);
426  memset(iv_str, 0x00, 128);
427  memset(add_str, 0x00, 128);
428  memset(tag_str, 0x00, 128);
429  memset(output, 0x00, 128);
430  memset(tag_output, 0x00, 16);
431 
432  key_len = unhexify( key_str, hex_key_string );
433  pt_len = unhexify( src_str, hex_src_string );
434  iv_len = unhexify( iv_str, hex_iv_string );
435  add_len = unhexify( add_str, hex_add_string );
436 
437  TEST_ASSERT( gcm_init( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
438  if( init_result == 0 )
439  {
440  TEST_ASSERT( gcm_crypt_and_tag( &ctx, GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 );
441  hexify( dst_str, output, pt_len );
442  hexify( tag_str, tag_output, tag_len );
443 
444  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
445  TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
446  }
447 
448 exit:
449  gcm_free( &ctx );
450 }
451 
452 void test_suite_gcm_decrypt_and_verify( int cipher_id,
453  char *hex_key_string, char *hex_src_string,
454  char *hex_iv_string, char *hex_add_string,
455  int tag_len_bits, char *hex_tag_string,
456  char *pt_result, int init_result )
457 {
458  unsigned char key_str[128];
459  unsigned char src_str[128];
460  unsigned char dst_str[257];
461  unsigned char iv_str[128];
462  unsigned char add_str[128];
463  unsigned char tag_str[128];
464  unsigned char output[128];
465  gcm_context ctx;
466  unsigned int key_len;
467  size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
468  int ret;
469 
470  memset(key_str, 0x00, 128);
471  memset(src_str, 0x00, 128);
472  memset(dst_str, 0x00, 257);
473  memset(iv_str, 0x00, 128);
474  memset(add_str, 0x00, 128);
475  memset(tag_str, 0x00, 128);
476  memset(output, 0x00, 128);
477 
478  key_len = unhexify( key_str, hex_key_string );
479  pt_len = unhexify( src_str, hex_src_string );
480  iv_len = unhexify( iv_str, hex_iv_string );
481  add_len = unhexify( add_str, hex_add_string );
482  unhexify( tag_str, hex_tag_string );
483 
484  TEST_ASSERT( gcm_init( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
485  if( init_result == 0 )
486  {
487  ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
488 
489  if( strcmp( "FAIL", pt_result ) == 0 )
490  {
492  }
493  else
494  {
495  TEST_ASSERT( ret == 0 );
496  hexify( dst_str, output, pt_len );
497 
498  TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
499  }
500  }
501 
502 exit:
503  gcm_free( &ctx );
504 }
505 
506 #ifdef POLARSSL_SELF_TEST
507 void test_suite_gcm_selftest()
508 {
509  TEST_ASSERT( gcm_self_test( 0 ) == 0 );
510 
511 exit:
512  return;
513 }
514 #endif /* POLARSSL_SELF_TEST */
515 
516 
517 #endif /* POLARSSL_GCM_C */
518 
519 
520 int dep_check( char *str )
521 {
522  if( str == NULL )
523  return( 1 );
524 
525  if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
526  {
527 #if defined(POLARSSL_AES_C)
528  return( 0 );
529 #else
530  return( 1 );
531 #endif
532  }
533 
534 
535  return( 1 );
536 }
537 
538 int dispatch_test(int cnt, char *params[50])
539 {
540  int ret;
541  ((void) cnt);
542  ((void) params);
543 
544 #if defined(TEST_SUITE_ACTIVE)
545  if( strcmp( params[0], "gcm_encrypt_and_tag" ) == 0 )
546  {
547 
548  int param1;
549  char *param2 = params[2];
550  char *param3 = params[3];
551  char *param4 = params[4];
552  char *param5 = params[5];
553  char *param6 = params[6];
554  int param7;
555  char *param8 = params[8];
556  int param9;
557 
558  if( cnt != 10 )
559  {
560  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
561  return( 2 );
562  }
563 
564  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
565  if( verify_string( &param2 ) != 0 ) return( 2 );
566  if( verify_string( &param3 ) != 0 ) return( 2 );
567  if( verify_string( &param4 ) != 0 ) return( 2 );
568  if( verify_string( &param5 ) != 0 ) return( 2 );
569  if( verify_string( &param6 ) != 0 ) return( 2 );
570  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
571  if( verify_string( &param8 ) != 0 ) return( 2 );
572  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
573 
574  test_suite_gcm_encrypt_and_tag( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
575  return ( 0 );
576 
577  return ( 3 );
578  }
579  else
580  if( strcmp( params[0], "gcm_decrypt_and_verify" ) == 0 )
581  {
582 
583  int param1;
584  char *param2 = params[2];
585  char *param3 = params[3];
586  char *param4 = params[4];
587  char *param5 = params[5];
588  int param6;
589  char *param7 = params[7];
590  char *param8 = params[8];
591  int param9;
592 
593  if( cnt != 10 )
594  {
595  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
596  return( 2 );
597  }
598 
599  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
600  if( verify_string( &param2 ) != 0 ) return( 2 );
601  if( verify_string( &param3 ) != 0 ) return( 2 );
602  if( verify_string( &param4 ) != 0 ) return( 2 );
603  if( verify_string( &param5 ) != 0 ) return( 2 );
604  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
605  if( verify_string( &param7 ) != 0 ) return( 2 );
606  if( verify_string( &param8 ) != 0 ) return( 2 );
607  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
608 
609  test_suite_gcm_decrypt_and_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
610  return ( 0 );
611 
612  return ( 3 );
613  }
614  else
615  if( strcmp( params[0], "gcm_selftest" ) == 0 )
616  {
617  #ifdef POLARSSL_SELF_TEST
618 
619 
620  if( cnt != 1 )
621  {
622  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
623  return( 2 );
624  }
625 
626 
627  test_suite_gcm_selftest( );
628  return ( 0 );
629  #endif /* POLARSSL_SELF_TEST */
630 
631  return ( 3 );
632  }
633  else
634 
635  {
636  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
637  fflush( stdout );
638  return( 1 );
639  }
640 #else
641  return( 3 );
642 #endif
643  return( ret );
644 }
645 
646 int get_line( FILE *f, char *buf, size_t len )
647 {
648  char *ret;
649 
650  ret = fgets( buf, len, f );
651  if( ret == NULL )
652  return( -1 );
653 
654  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
655  buf[strlen(buf) - 1] = '\0';
656  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
657  buf[strlen(buf) - 1] = '\0';
658 
659  return( 0 );
660 }
661 
662 int parse_arguments( char *buf, size_t len, char *params[50] )
663 {
664  int cnt = 0, i;
665  char *cur = buf;
666  char *p = buf, *q;
667 
668  params[cnt++] = cur;
669 
670  while( *p != '\0' && p < buf + len )
671  {
672  if( *p == '\\' )
673  {
674  p++;
675  p++;
676  continue;
677  }
678  if( *p == ':' )
679  {
680  if( p + 1 < buf + len )
681  {
682  cur = p + 1;
683  params[cnt++] = cur;
684  }
685  *p = '\0';
686  }
687 
688  p++;
689  }
690 
691  // Replace newlines, question marks and colons in strings
692  for( i = 0; i < cnt; i++ )
693  {
694  p = params[i];
695  q = params[i];
696 
697  while( *p != '\0' )
698  {
699  if( *p == '\\' && *(p + 1) == 'n' )
700  {
701  p += 2;
702  *(q++) = '\n';
703  }
704  else if( *p == '\\' && *(p + 1) == ':' )
705  {
706  p += 2;
707  *(q++) = ':';
708  }
709  else if( *p == '\\' && *(p + 1) == '?' )
710  {
711  p += 2;
712  *(q++) = '?';
713  }
714  else
715  *(q++) = *(p++);
716  }
717  *q = '\0';
718  }
719 
720  return( cnt );
721 }
722 
723 int main()
724 {
725  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
726  const char *filename = "/tmp/B.HYlHIA/BUILD/polarssl-1.3.9/tests/suites/test_suite_gcm.aes256_de.data";
727  FILE *file;
728  char buf[5000];
729  char *params[50];
730 
731 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
732  unsigned char alloc_buf[1000000];
733  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
734 #endif
735 
736  file = fopen( filename, "r" );
737  if( file == NULL )
738  {
739  fprintf( stderr, "Failed to open\n" );
740  return( 1 );
741  }
742 
743  while( !feof( file ) )
744  {
745  int skip = 0;
746 
747  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
748  break;
749  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
750  fprintf( stdout, " " );
751  for( i = strlen( buf ) + 1; i < 67; i++ )
752  fprintf( stdout, "." );
753  fprintf( stdout, " " );
754  fflush( stdout );
755 
756  total_tests++;
757 
758  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
759  break;
760  cnt = parse_arguments( buf, strlen(buf), params );
761 
762  if( strcmp( params[0], "depends_on" ) == 0 )
763  {
764  for( i = 1; i < cnt; i++ )
765  if( dep_check( params[i] ) != 0 )
766  skip = 1;
767 
768  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
769  break;
770  cnt = parse_arguments( buf, strlen(buf), params );
771  }
772 
773  if( skip == 0 )
774  {
775  test_errors = 0;
776  ret = dispatch_test( cnt, params );
777  }
778 
779  if( skip == 1 || ret == 3 )
780  {
781  total_skipped++;
782  fprintf( stdout, "----\n" );
783  fflush( stdout );
784  }
785  else if( ret == 0 && test_errors == 0 )
786  {
787  fprintf( stdout, "PASS\n" );
788  fflush( stdout );
789  }
790  else if( ret == 2 )
791  {
792  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
793  fclose(file);
794  exit( 2 );
795  }
796  else
797  total_errors++;
798 
799  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
800  break;
801  if( strlen(buf) != 0 )
802  {
803  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
804  return( 1 );
805  }
806  }
807  fclose(file);
808 
809  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
810  if( total_errors == 0 )
811  fprintf( stdout, "PASSED" );
812  else
813  fprintf( stdout, "FAILED" );
814 
815  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
816  total_tests - total_errors, total_tests, total_skipped );
817 
818 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
819 #if defined(POLARSSL_MEMORY_DEBUG)
820  memory_buffer_alloc_status();
821 #endif
823 #endif
824 
825  return( total_errors != 0 );
826 }
827 
828