PolarSSL v1.3.1
test_suite_gcm.encrypt_256.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_GCM_C
4 
5 #include <polarssl/gcm.h>
6 #endif /* POLARSSL_GCM_C */
7 
8 
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
10 #include "polarssl/memory.h"
11 #endif
12 
13 #ifdef _MSC_VER
14 #include <basetsd.h>
15 typedef UINT32 uint32_t;
16 #else
17 #include <inttypes.h>
18 #endif
19 
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 /*
25  * 32-bit integer manipulation macros (big endian)
26  */
27 #ifndef GET_UINT32_BE
28 #define GET_UINT32_BE(n,b,i) \
29 { \
30  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33  | ( (uint32_t) (b)[(i) + 3] ); \
34 }
35 #endif
36 
37 #ifndef PUT_UINT32_BE
38 #define PUT_UINT32_BE(n,b,i) \
39 { \
40  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43  (b)[(i) + 3] = (unsigned char) ( (n) ); \
44 }
45 #endif
46 
47 static int unhexify(unsigned char *obuf, const char *ibuf)
48 {
49  unsigned char c, c2;
50  int len = strlen(ibuf) / 2;
51  assert(!(strlen(ibuf) %1)); // must be even number of bytes
52 
53  while (*ibuf != 0)
54  {
55  c = *ibuf++;
56  if( c >= '0' && c <= '9' )
57  c -= '0';
58  else if( c >= 'a' && c <= 'f' )
59  c -= 'a' - 10;
60  else if( c >= 'A' && c <= 'F' )
61  c -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  c2 = *ibuf++;
66  if( c2 >= '0' && c2 <= '9' )
67  c2 -= '0';
68  else if( c2 >= 'a' && c2 <= 'f' )
69  c2 -= 'a' - 10;
70  else if( c2 >= 'A' && c2 <= 'F' )
71  c2 -= 'A' - 10;
72  else
73  assert( 0 );
74 
75  *obuf++ = ( c << 4 ) | c2;
76  }
77 
78  return len;
79 }
80 
81 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
82 {
83  unsigned char l, h;
84 
85  while (len != 0)
86  {
87  h = (*ibuf) / 16;
88  l = (*ibuf) % 16;
89 
90  if( h < 10 )
91  *obuf++ = '0' + h;
92  else
93  *obuf++ = 'a' + h - 10;
94 
95  if( l < 10 )
96  *obuf++ = '0' + l;
97  else
98  *obuf++ = 'a' + l - 10;
99 
100  ++ibuf;
101  len--;
102  }
103 }
104 
114 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
115 {
116  size_t i;
117 
118  if( rng_state != NULL )
119  rng_state = NULL;
120 
121  for( i = 0; i < len; ++i )
122  output[i] = rand();
123 
124  return( 0 );
125 }
126 
132 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
133 {
134  if( rng_state != NULL )
135  rng_state = NULL;
136 
137  memset( output, 0, len );
138 
139  return( 0 );
140 }
141 
142 typedef struct
143 {
144  unsigned char *buf;
145  size_t length;
146 } rnd_buf_info;
147 
159 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
160 {
161  rnd_buf_info *info = (rnd_buf_info *) rng_state;
162  size_t use_len;
163 
164  if( rng_state == NULL )
165  return( rnd_std_rand( NULL, output, len ) );
166 
167  use_len = len;
168  if( len > info->length )
169  use_len = info->length;
170 
171  if( use_len )
172  {
173  memcpy( output, info->buf, use_len );
174  info->buf += use_len;
175  info->length -= use_len;
176  }
177 
178  if( len - use_len > 0 )
179  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
180 
181  return( 0 );
182 }
183 
191 typedef struct
192 {
193  uint32_t key[16];
194  uint32_t v0, v1;
196 
205 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
206 {
207  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
208  uint32_t i, *k, sum, delta=0x9E3779B9;
209  unsigned char result[4];
210 
211  if( rng_state == NULL )
212  return( rnd_std_rand( NULL, output, len ) );
213 
214  k = info->key;
215 
216  while( len > 0 )
217  {
218  size_t use_len = ( len > 4 ) ? 4 : len;
219  sum = 0;
220 
221  for( i = 0; i < 32; i++ )
222  {
223  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
224  sum += delta;
225  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
226  }
227 
228  PUT_UINT32_BE( info->v0, result, 0 );
229  memcpy( output, result, use_len );
230  len -= use_len;
231  }
232 
233  return( 0 );
234 }
235 
245 static int not_rnd( void *in, unsigned char *out, size_t len )
246 {
247  unsigned char *obuf;
248  const char *ibuf = in;
249  unsigned char c, c2;
250  assert( len == strlen(ibuf) / 2 );
251  assert(!(strlen(ibuf) %1)); // must be even number of bytes
252 
253  obuf = out + (len - 1); // sic
254  while (*ibuf != 0)
255  {
256  c = *ibuf++;
257  if( c >= '0' && c <= '9' )
258  c -= '0';
259  else if( c >= 'a' && c <= 'f' )
260  c -= 'a' - 10;
261  else if( c >= 'A' && c <= 'F' )
262  c -= 'A' - 10;
263  else
264  assert( 0 );
265 
266  c2 = *ibuf++;
267  if( c2 >= '0' && c2 <= '9' )
268  c2 -= '0';
269  else if( c2 >= 'a' && c2 <= 'f' )
270  c2 -= 'a' - 10;
271  else if( c2 >= 'A' && c2 <= 'F' )
272  c2 -= 'A' - 10;
273  else
274  assert( 0 );
275 
276  *obuf-- = ( c << 4 ) | c2; // sic
277  }
278 
279  return( 0 );
280 }
281 
282 
283 #include <stdio.h>
284 #include <string.h>
285 
286 static int test_errors = 0;
287 
288 #ifdef POLARSSL_GCM_C
289 
290 #define TEST_SUITE_ACTIVE
291 
292 static int test_assert( int correct, char *test )
293 {
294  if( correct )
295  return( 0 );
296 
297  test_errors++;
298  if( test_errors == 1 )
299  printf( "FAILED\n" );
300  printf( " %s\n", test );
301 
302  return( 1 );
303 }
304 
305 #define TEST_ASSERT( TEST ) \
306  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307  if( test_errors) return; \
308  } while (0)
309 
310 int verify_string( char **str )
311 {
312  if( (*str)[0] != '"' ||
313  (*str)[strlen( *str ) - 1] != '"' )
314  {
315  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
316  return( -1 );
317  }
318 
319  (*str)++;
320  (*str)[strlen( *str ) - 1] = '\0';
321 
322  return( 0 );
323 }
324 
325 int verify_int( char *str, int *value )
326 {
327  size_t i;
328  int minus = 0;
329  int digits = 1;
330  int hex = 0;
331 
332  for( i = 0; i < strlen( str ); i++ )
333  {
334  if( i == 0 && str[i] == '-' )
335  {
336  minus = 1;
337  continue;
338  }
339 
340  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341  str[i - 1] == '0' && str[i] == 'x' )
342  {
343  hex = 1;
344  continue;
345  }
346 
347  if( str[i] < '0' || str[i] > '9' )
348  {
349  digits = 0;
350  break;
351  }
352  }
353 
354  if( digits )
355  {
356  if( hex )
357  *value = strtol( str, NULL, 16 );
358  else
359  *value = strtol( str, NULL, 10 );
360 
361  return( 0 );
362  }
363 
364 
365 
366  printf( "Expected integer for parameter and got: %s\n", str );
367  return( -1 );
368 }
369 
370 void test_suite_gcm_encrypt_and_tag( char *hex_key_string, char *hex_src_string,
371  char *hex_iv_string, char *hex_add_string,
372  char *hex_dst_string, int tag_len_bits,
373  char *hex_tag_string, int init_result )
374 {
375  unsigned char key_str[128];
376  unsigned char src_str[128];
377  unsigned char dst_str[257];
378  unsigned char iv_str[128];
379  unsigned char add_str[128];
380  unsigned char tag_str[128];
381  unsigned char output[128];
382  unsigned char tag_output[16];
383  gcm_context ctx;
384  unsigned int key_len;
385  size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
386 
387  memset(key_str, 0x00, 128);
388  memset(src_str, 0x00, 128);
389  memset(dst_str, 0x00, 257);
390  memset(iv_str, 0x00, 128);
391  memset(add_str, 0x00, 128);
392  memset(tag_str, 0x00, 128);
393  memset(output, 0x00, 128);
394  memset(tag_output, 0x00, 16);
395 
396  key_len = unhexify( key_str, hex_key_string );
397  pt_len = unhexify( src_str, hex_src_string );
398  iv_len = unhexify( iv_str, hex_iv_string );
399  add_len = unhexify( add_str, hex_add_string );
400 
401  TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result );
402  if( init_result == 0 )
403  {
404  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 );
405  hexify( dst_str, output, pt_len );
406  hexify( tag_str, tag_output, tag_len );
407 
408  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
409  TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
410  }
411 
412  gcm_free( &ctx );
413 }
414 
415 void test_suite_gcm_decrypt_and_verify( char *hex_key_string, char *hex_src_string,
416  char *hex_iv_string, char *hex_add_string,
417  int tag_len_bits, char *hex_tag_string,
418  char *pt_result, int init_result )
419 {
420  unsigned char key_str[128];
421  unsigned char src_str[128];
422  unsigned char dst_str[257];
423  unsigned char iv_str[128];
424  unsigned char add_str[128];
425  unsigned char tag_str[128];
426  unsigned char output[128];
427  gcm_context ctx;
428  unsigned int key_len;
429  size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
430  int ret;
431 
432  memset(key_str, 0x00, 128);
433  memset(src_str, 0x00, 128);
434  memset(dst_str, 0x00, 257);
435  memset(iv_str, 0x00, 128);
436  memset(add_str, 0x00, 128);
437  memset(tag_str, 0x00, 128);
438  memset(output, 0x00, 128);
439 
440  key_len = unhexify( key_str, hex_key_string );
441  pt_len = unhexify( src_str, hex_src_string );
442  iv_len = unhexify( iv_str, hex_iv_string );
443  add_len = unhexify( add_str, hex_add_string );
444  unhexify( tag_str, hex_tag_string );
445 
446  TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result );
447  if( init_result == 0 )
448  {
449  ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
450 
451  if( strcmp( "FAIL", pt_result ) == 0 )
452  {
454  }
455  else
456  {
457  TEST_ASSERT( ret == 0 );
458  hexify( dst_str, output, pt_len );
459 
460  TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
461  }
462  }
463 
464  gcm_free( &ctx );
465 }
466 
467 #ifdef POLARSSL_SELF_TEST
468 void test_suite_gcm_selftest()
469 {
470  TEST_ASSERT( gcm_self_test( 0 ) == 0 );
471 }
472 #endif /* POLARSSL_SELF_TEST */
473 
474 
475 #endif /* POLARSSL_GCM_C */
476 
477 
478 int dep_check( char *str )
479 {
480  if( str == NULL )
481  return( 1 );
482 
483 
484 
485  return( 1 );
486 }
487 
488 int dispatch_test(int cnt, char *params[50])
489 {
490  int ret;
491  ((void) cnt);
492  ((void) params);
493 
494 #if defined(TEST_SUITE_ACTIVE)
495  if( strcmp( params[0], "gcm_encrypt_and_tag" ) == 0 )
496  {
497 
498  char *param1 = params[1];
499  char *param2 = params[2];
500  char *param3 = params[3];
501  char *param4 = params[4];
502  char *param5 = params[5];
503  int param6;
504  char *param7 = params[7];
505  int param8;
506 
507  if( cnt != 9 )
508  {
509  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
510  return( 2 );
511  }
512 
513  if( verify_string( &param1 ) != 0 ) return( 2 );
514  if( verify_string( &param2 ) != 0 ) return( 2 );
515  if( verify_string( &param3 ) != 0 ) return( 2 );
516  if( verify_string( &param4 ) != 0 ) return( 2 );
517  if( verify_string( &param5 ) != 0 ) return( 2 );
518  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
519  if( verify_string( &param7 ) != 0 ) return( 2 );
520  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
521 
522  test_suite_gcm_encrypt_and_tag( param1, param2, param3, param4, param5, param6, param7, param8 );
523  return ( 0 );
524 
525  return ( 3 );
526  }
527  else
528  if( strcmp( params[0], "gcm_decrypt_and_verify" ) == 0 )
529  {
530 
531  char *param1 = params[1];
532  char *param2 = params[2];
533  char *param3 = params[3];
534  char *param4 = params[4];
535  int param5;
536  char *param6 = params[6];
537  char *param7 = params[7];
538  int param8;
539 
540  if( cnt != 9 )
541  {
542  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
543  return( 2 );
544  }
545 
546  if( verify_string( &param1 ) != 0 ) return( 2 );
547  if( verify_string( &param2 ) != 0 ) return( 2 );
548  if( verify_string( &param3 ) != 0 ) return( 2 );
549  if( verify_string( &param4 ) != 0 ) return( 2 );
550  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
551  if( verify_string( &param6 ) != 0 ) return( 2 );
552  if( verify_string( &param7 ) != 0 ) return( 2 );
553  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
554 
555  test_suite_gcm_decrypt_and_verify( param1, param2, param3, param4, param5, param6, param7, param8 );
556  return ( 0 );
557 
558  return ( 3 );
559  }
560  else
561  if( strcmp( params[0], "gcm_selftest" ) == 0 )
562  {
563  #ifdef POLARSSL_SELF_TEST
564 
565 
566  if( cnt != 1 )
567  {
568  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
569  return( 2 );
570  }
571 
572 
573  test_suite_gcm_selftest( );
574  return ( 0 );
575  #endif /* POLARSSL_SELF_TEST */
576 
577  return ( 3 );
578  }
579  else
580 
581  {
582  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
583  fflush( stdout );
584  return( 1 );
585  }
586 #else
587  return( 3 );
588 #endif
589  return( ret );
590 }
591 
592 int get_line( FILE *f, char *buf, size_t len )
593 {
594  char *ret;
595 
596  ret = fgets( buf, len, f );
597  if( ret == NULL )
598  return( -1 );
599 
600  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
601  buf[strlen(buf) - 1] = '\0';
602  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
603  buf[strlen(buf) - 1] = '\0';
604 
605  return( 0 );
606 }
607 
608 int parse_arguments( char *buf, size_t len, char *params[50] )
609 {
610  int cnt = 0, i;
611  char *cur = buf;
612  char *p = buf, *q;
613 
614  params[cnt++] = cur;
615 
616  while( *p != '\0' && p < buf + len )
617  {
618  if( *p == '\\' )
619  {
620  *p++;
621  *p++;
622  continue;
623  }
624  if( *p == ':' )
625  {
626  if( p + 1 < buf + len )
627  {
628  cur = p + 1;
629  params[cnt++] = cur;
630  }
631  *p = '\0';
632  }
633 
634  *p++;
635  }
636 
637  // Replace newlines, question marks and colons in strings
638  for( i = 0; i < cnt; i++ )
639  {
640  p = params[i];
641  q = params[i];
642 
643  while( *p != '\0' )
644  {
645  if( *p == '\\' && *(p + 1) == 'n' )
646  {
647  p += 2;
648  *(q++) = '\n';
649  }
650  else if( *p == '\\' && *(p + 1) == ':' )
651  {
652  p += 2;
653  *(q++) = ':';
654  }
655  else if( *p == '\\' && *(p + 1) == '?' )
656  {
657  p += 2;
658  *(q++) = '?';
659  }
660  else
661  *(q++) = *(p++);
662  }
663  *q = '\0';
664  }
665 
666  return( cnt );
667 }
668 
669 int main()
670 {
671  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
672  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_gcm.encrypt_256.data";
673  FILE *file;
674  char buf[5000];
675  char *params[50];
676 
677 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
678  unsigned char alloc_buf[1000000];
679  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
680 #endif
681 
682  file = fopen( filename, "r" );
683  if( file == NULL )
684  {
685  fprintf( stderr, "Failed to open\n" );
686  return( 1 );
687  }
688 
689  while( !feof( file ) )
690  {
691  int skip = 0;
692 
693  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
694  break;
695  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
696  fprintf( stdout, " " );
697  for( i = strlen( buf ) + 1; i < 67; i++ )
698  fprintf( stdout, "." );
699  fprintf( stdout, " " );
700  fflush( stdout );
701 
702  total_tests++;
703 
704  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
705  break;
706  cnt = parse_arguments( buf, strlen(buf), params );
707 
708  if( strcmp( params[0], "depends_on" ) == 0 )
709  {
710  for( i = 1; i < cnt; i++ )
711  if( dep_check( params[i] ) != 0 )
712  skip = 1;
713 
714  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
715  break;
716  cnt = parse_arguments( buf, strlen(buf), params );
717  }
718 
719  if( skip == 0 )
720  {
721  test_errors = 0;
722  ret = dispatch_test( cnt, params );
723  }
724 
725  if( skip == 1 || ret == 3 )
726  {
727  total_skipped++;
728  fprintf( stdout, "----\n" );
729  fflush( stdout );
730  }
731  else if( ret == 0 && test_errors == 0 )
732  {
733  fprintf( stdout, "PASS\n" );
734  fflush( stdout );
735  }
736  else if( ret == 2 )
737  {
738  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
739  fclose(file);
740  exit( 2 );
741  }
742  else
743  total_errors++;
744 
745  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
746  break;
747  if( strlen(buf) != 0 )
748  {
749  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
750  return( 1 );
751  }
752  }
753  fclose(file);
754 
755  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
756  if( total_errors == 0 )
757  fprintf( stdout, "PASSED" );
758  else
759  fprintf( stdout, "FAILED" );
760 
761  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
762  total_tests - total_errors, total_tests, total_skipped );
763 
764 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
765 #if defined(POLARSSL_MEMORY_DEBUG)
766  memory_buffer_alloc_status();
767 #endif
768  memory_buffer_alloc_free();
769 #endif
770 
771  return( total_errors != 0 );
772 }
773 
774