PolarSSL v1.3.9
test_suite_ccm.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_CCM_C
8 
9 #include <polarssl/ccm.h>
10 #endif /* POLARSSL_CCM_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_CCM_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_CAMELLIA" ) == 0 )
395  {
396  *value = ( POLARSSL_CIPHER_ID_CAMELLIA );
397  return( 0 );
398  }
399  if( strcmp( str, "POLARSSL_CIPHER_ID_AES" ) == 0 )
400  {
401  *value = ( POLARSSL_CIPHER_ID_AES );
402  return( 0 );
403  }
404  if( strcmp( str, "POLARSSL_CIPHER_ID_BLOWFISH" ) == 0 )
405  {
406  *value = ( POLARSSL_CIPHER_ID_BLOWFISH );
407  return( 0 );
408  }
409  if( strcmp( str, "POLARSSL_ERR_CCM_BAD_INPUT" ) == 0 )
410  {
411  *value = ( POLARSSL_ERR_CCM_BAD_INPUT );
412  return( 0 );
413  }
414 
415 
416  printf( "Expected integer for parameter and got: %s\n", str );
417  return( -1 );
418 }
419 
420 #ifdef POLARSSL_SELF_TEST
421 #ifdef POLARSSL_AES_C
422 void test_suite_ccm_self_test( )
423 {
424  TEST_ASSERT( ccm_self_test( 0 ) == 0 );
425 
426 exit:
427  return;
428 }
429 #endif /* POLARSSL_SELF_TEST */
430 #endif /* POLARSSL_AES_C */
431 
432 void test_suite_ccm_init( int cipher_id, int key_size, int result )
433 {
434  ccm_context ctx;
435  unsigned char key[32];
436  int ret;
437 
438  memset( key, 0x2A, sizeof( key ) );
439  TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
440 
441  ret = ccm_init( &ctx, cipher_id, key, key_size );
442  TEST_ASSERT( ret == result );
443 
444 exit:
445  ccm_free( &ctx );
446 }
447 
448 #ifdef POLARSSL_AES_C
449 void test_suite_ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res )
450 {
451  ccm_context ctx;
452  unsigned char key[16];
453  unsigned char msg[10];
454  unsigned char iv[14];
455  unsigned char add[10];
456  unsigned char out[10];
457  unsigned char tag[18];
458  int decrypt_ret;
459 
460  memset( key, 0, sizeof( key ) );
461  memset( msg, 0, sizeof( msg ) );
462  memset( iv, 0, sizeof( iv ) );
463  memset( add, 0, sizeof( add ) );
464  memset( out, 0, sizeof( out ) );
465  memset( tag, 0, sizeof( tag ) );
466 
468  key, 8 * sizeof( key ) ) == 0 );
469 
470  TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
471  msg, out, tag, tag_len ) == res );
472 
473  decrypt_ret = ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
474  msg, out, tag, tag_len );
475 
476  if( res == 0 )
477  TEST_ASSERT( decrypt_ret == POLARSSL_ERR_CCM_AUTH_FAILED );
478  else
479  TEST_ASSERT( decrypt_ret == res );
480 
481 exit:
482  ccm_free( &ctx );
483 }
484 #endif /* POLARSSL_AES_C */
485 
486 void test_suite_ccm_encrypt_and_tag( int cipher_id,
487  char *key_hex, char *msg_hex,
488  char *iv_hex, char *add_hex,
489  char *result_hex )
490 {
491  unsigned char key[32];
492  unsigned char msg[50];
493  unsigned char iv[13];
494  unsigned char add[32];
495  unsigned char result[50];
496  ccm_context ctx;
497  size_t key_len, msg_len, iv_len, add_len, tag_len, result_len;
498 
499  memset( key, 0x00, sizeof( key ) );
500  memset( msg, 0x00, sizeof( msg ) );
501  memset( iv, 0x00, sizeof( iv ) );
502  memset( add, 0x00, sizeof( add ) );
503  memset( result, 0x00, sizeof( result ) );
504 
505  key_len = unhexify( key, key_hex );
506  msg_len = unhexify( msg, msg_hex );
507  iv_len = unhexify( iv, iv_hex );
508  add_len = unhexify( add, add_hex );
509  result_len = unhexify( result, result_hex );
510  tag_len = result_len - msg_len;
511 
512  TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 );
513 
514  /* Test with input == output */
515  TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
516  msg, msg, msg + msg_len, tag_len ) == 0 );
517 
518  TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
519 
520  /* Check we didn't write past the end */
521  TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
522 
523 exit:
524  ccm_free( &ctx );
525 }
526 
527 void test_suite_ccm_auth_decrypt( int cipher_id,
528  char *key_hex, char *msg_hex,
529  char *iv_hex, char *add_hex,
530  int tag_len, char *result_hex )
531 {
532  unsigned char key[32];
533  unsigned char msg[50];
534  unsigned char iv[13];
535  unsigned char add[32];
536  unsigned char tag[16];
537  unsigned char result[50];
538  ccm_context ctx;
539  size_t key_len, msg_len, iv_len, add_len, result_len;
540  int ret;
541 
542  memset( key, 0x00, sizeof( key ) );
543  memset( msg, 0x00, sizeof( msg ) );
544  memset( iv, 0x00, sizeof( iv ) );
545  memset( add, 0x00, sizeof( add ) );
546  memset( tag, 0x00, sizeof( tag ) );
547  memset( result, 0x00, sizeof( result ) );
548 
549  key_len = unhexify( key, key_hex );
550  msg_len = unhexify( msg, msg_hex );
551  iv_len = unhexify( iv, iv_hex );
552  add_len = unhexify( add, add_hex );
553  msg_len -= tag_len;
554  memcpy( tag, msg + msg_len, tag_len );
555 
556  if( strcmp( "FAIL", result_hex ) == 0 )
557  {
559  }
560  else
561  {
562  ret = 0;
563  result_len = unhexify( result, result_hex );
564  }
565 
566  TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 );
567 
568  /* Test with input == output */
569  TEST_ASSERT( ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
570  msg, msg, msg + msg_len, tag_len ) == ret );
571 
572  if( ret == 0 )
573  {
574  TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
575  }
576  else
577  {
578  size_t i;
579 
580  for( i = 0; i < msg_len; i++ )
581  TEST_ASSERT( msg[i] == 0 );
582  }
583 
584  /* Check we didn't write past the end (where the original tag is) */
585  TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
586 
587 exit:
588  ccm_free( &ctx );
589 }
590 
591 
592 #endif /* POLARSSL_CCM_C */
593 
594 
595 int dep_check( char *str )
596 {
597  if( str == NULL )
598  return( 1 );
599 
600  if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
601  {
602 #if defined(POLARSSL_AES_C)
603  return( 0 );
604 #else
605  return( 1 );
606 #endif
607  }
608  if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
609  {
610 #if defined(POLARSSL_CAMELLIA_C)
611  return( 0 );
612 #else
613  return( 1 );
614 #endif
615  }
616  if( strcmp( str, "POLARSSL_BLOWFISH_C" ) == 0 )
617  {
618 #if defined(POLARSSL_BLOWFISH_C)
619  return( 0 );
620 #else
621  return( 1 );
622 #endif
623  }
624 
625 
626  return( 1 );
627 }
628 
629 int dispatch_test(int cnt, char *params[50])
630 {
631  int ret;
632  ((void) cnt);
633  ((void) params);
634 
635 #if defined(TEST_SUITE_ACTIVE)
636  if( strcmp( params[0], "ccm_self_test" ) == 0 )
637  {
638  #ifdef POLARSSL_SELF_TEST
639  #ifdef POLARSSL_AES_C
640 
641 
642  if( cnt != 1 )
643  {
644  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
645  return( 2 );
646  }
647 
648 
649  test_suite_ccm_self_test( );
650  return ( 0 );
651  #endif /* POLARSSL_SELF_TEST */
652  #endif /* POLARSSL_AES_C */
653 
654  return ( 3 );
655  }
656  else
657  if( strcmp( params[0], "ccm_init" ) == 0 )
658  {
659 
660  int param1;
661  int param2;
662  int param3;
663 
664  if( cnt != 4 )
665  {
666  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
667  return( 2 );
668  }
669 
670  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
671  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
672  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
673 
674  test_suite_ccm_init( param1, param2, param3 );
675  return ( 0 );
676 
677  return ( 3 );
678  }
679  else
680  if( strcmp( params[0], "ccm_lengths" ) == 0 )
681  {
682  #ifdef POLARSSL_AES_C
683 
684  int param1;
685  int param2;
686  int param3;
687  int param4;
688  int param5;
689 
690  if( cnt != 6 )
691  {
692  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
693  return( 2 );
694  }
695 
696  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
697  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
698  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
699  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
700  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
701 
702  test_suite_ccm_lengths( param1, param2, param3, param4, param5 );
703  return ( 0 );
704  #endif /* POLARSSL_AES_C */
705 
706  return ( 3 );
707  }
708  else
709  if( strcmp( params[0], "ccm_encrypt_and_tag" ) == 0 )
710  {
711 
712  int param1;
713  char *param2 = params[2];
714  char *param3 = params[3];
715  char *param4 = params[4];
716  char *param5 = params[5];
717  char *param6 = params[6];
718 
719  if( cnt != 7 )
720  {
721  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
722  return( 2 );
723  }
724 
725  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
726  if( verify_string( &param2 ) != 0 ) return( 2 );
727  if( verify_string( &param3 ) != 0 ) return( 2 );
728  if( verify_string( &param4 ) != 0 ) return( 2 );
729  if( verify_string( &param5 ) != 0 ) return( 2 );
730  if( verify_string( &param6 ) != 0 ) return( 2 );
731 
732  test_suite_ccm_encrypt_and_tag( param1, param2, param3, param4, param5, param6 );
733  return ( 0 );
734 
735  return ( 3 );
736  }
737  else
738  if( strcmp( params[0], "ccm_auth_decrypt" ) == 0 )
739  {
740 
741  int param1;
742  char *param2 = params[2];
743  char *param3 = params[3];
744  char *param4 = params[4];
745  char *param5 = params[5];
746  int param6;
747  char *param7 = params[7];
748 
749  if( cnt != 8 )
750  {
751  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
752  return( 2 );
753  }
754 
755  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
756  if( verify_string( &param2 ) != 0 ) return( 2 );
757  if( verify_string( &param3 ) != 0 ) return( 2 );
758  if( verify_string( &param4 ) != 0 ) return( 2 );
759  if( verify_string( &param5 ) != 0 ) return( 2 );
760  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
761  if( verify_string( &param7 ) != 0 ) return( 2 );
762 
763  test_suite_ccm_auth_decrypt( param1, param2, param3, param4, param5, param6, param7 );
764  return ( 0 );
765 
766  return ( 3 );
767  }
768  else
769 
770  {
771  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
772  fflush( stdout );
773  return( 1 );
774  }
775 #else
776  return( 3 );
777 #endif
778  return( ret );
779 }
780 
781 int get_line( FILE *f, char *buf, size_t len )
782 {
783  char *ret;
784 
785  ret = fgets( buf, len, f );
786  if( ret == NULL )
787  return( -1 );
788 
789  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
790  buf[strlen(buf) - 1] = '\0';
791  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
792  buf[strlen(buf) - 1] = '\0';
793 
794  return( 0 );
795 }
796 
797 int parse_arguments( char *buf, size_t len, char *params[50] )
798 {
799  int cnt = 0, i;
800  char *cur = buf;
801  char *p = buf, *q;
802 
803  params[cnt++] = cur;
804 
805  while( *p != '\0' && p < buf + len )
806  {
807  if( *p == '\\' )
808  {
809  p++;
810  p++;
811  continue;
812  }
813  if( *p == ':' )
814  {
815  if( p + 1 < buf + len )
816  {
817  cur = p + 1;
818  params[cnt++] = cur;
819  }
820  *p = '\0';
821  }
822 
823  p++;
824  }
825 
826  // Replace newlines, question marks and colons in strings
827  for( i = 0; i < cnt; i++ )
828  {
829  p = params[i];
830  q = params[i];
831 
832  while( *p != '\0' )
833  {
834  if( *p == '\\' && *(p + 1) == 'n' )
835  {
836  p += 2;
837  *(q++) = '\n';
838  }
839  else if( *p == '\\' && *(p + 1) == ':' )
840  {
841  p += 2;
842  *(q++) = ':';
843  }
844  else if( *p == '\\' && *(p + 1) == '?' )
845  {
846  p += 2;
847  *(q++) = '?';
848  }
849  else
850  *(q++) = *(p++);
851  }
852  *q = '\0';
853  }
854 
855  return( cnt );
856 }
857 
858 int main()
859 {
860  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
861  const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_ccm.data";
862  FILE *file;
863  char buf[5000];
864  char *params[50];
865 
866 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
867  unsigned char alloc_buf[1000000];
868  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
869 #endif
870 
871  file = fopen( filename, "r" );
872  if( file == NULL )
873  {
874  fprintf( stderr, "Failed to open\n" );
875  return( 1 );
876  }
877 
878  while( !feof( file ) )
879  {
880  int skip = 0;
881 
882  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
883  break;
884  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
885  fprintf( stdout, " " );
886  for( i = strlen( buf ) + 1; i < 67; i++ )
887  fprintf( stdout, "." );
888  fprintf( stdout, " " );
889  fflush( stdout );
890 
891  total_tests++;
892 
893  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
894  break;
895  cnt = parse_arguments( buf, strlen(buf), params );
896 
897  if( strcmp( params[0], "depends_on" ) == 0 )
898  {
899  for( i = 1; i < cnt; i++ )
900  if( dep_check( params[i] ) != 0 )
901  skip = 1;
902 
903  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
904  break;
905  cnt = parse_arguments( buf, strlen(buf), params );
906  }
907 
908  if( skip == 0 )
909  {
910  test_errors = 0;
911  ret = dispatch_test( cnt, params );
912  }
913 
914  if( skip == 1 || ret == 3 )
915  {
916  total_skipped++;
917  fprintf( stdout, "----\n" );
918  fflush( stdout );
919  }
920  else if( ret == 0 && test_errors == 0 )
921  {
922  fprintf( stdout, "PASS\n" );
923  fflush( stdout );
924  }
925  else if( ret == 2 )
926  {
927  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
928  fclose(file);
929  exit( 2 );
930  }
931  else
932  total_errors++;
933 
934  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
935  break;
936  if( strlen(buf) != 0 )
937  {
938  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
939  return( 1 );
940  }
941  }
942  fclose(file);
943 
944  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
945  if( total_errors == 0 )
946  fprintf( stdout, "PASSED" );
947  else
948  fprintf( stdout, "FAILED" );
949 
950  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
951  total_tests - total_errors, total_tests, total_skipped );
952 
953 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
954 #if defined(POLARSSL_MEMORY_DEBUG)
955  memory_buffer_alloc_status();
956 #endif
958 #endif
959 
960  return( total_errors != 0 );
961 }
962 
963 
int get_line(FILE *f, char *buf, size_t len)
#define POLARSSL_ERR_CCM_BAD_INPUT
Bad input parameters to function.
Definition: ccm.h:32
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int parse_arguments(char *buf, size_t len, char *params[50])
Memory allocation layer (Deprecated to platform layer)
int dispatch_test(int cnt, char *params[50])
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
Configuration options (set of defines)
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 ccm_auth_decrypt(ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, const unsigned char *tag, size_t tag_len)
CCM buffer authenticated decryption.
CCM context structure.
Definition: ccm.h:42
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define polarssl_malloc
int dep_check(char *str)
int ccm_init(ccm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
CCM initialization (encryption and decryption)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int ccm_self_test(int verbose)
Checkup routine.
static int test_errors
#define PUT_UINT32_BE(n, b, i)
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void ccm_free(ccm_context *ctx)
Free a CCM context and underlying cipher sub-context.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int ccm_encrypt_and_tag(ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, unsigned char *tag, size_t tag_len)
CCM buffer encryption.
int main()
static int unhexify(unsigned char *obuf, const char *ibuf)
unsigned char * buf
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int verify_int(char *str, int *value)
#define POLARSSL_ERR_CCM_AUTH_FAILED
Authenticated decryption failed.
Definition: ccm.h:33
Counter with CBC-MAC (CCM) for 128-bit block ciphers.