PolarSSL v1.3.9
test_suite_arc4.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_ARC4_C
8 
9 #include <polarssl/arc4.h>
10 #endif /* POLARSSL_ARC4_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_ARC4_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_arc4_crypt( char *hex_src_string, char *hex_key_string,
401  char *hex_dst_string )
402 {
403  unsigned char src_str[1000];
404  unsigned char key_str[1000];
405  unsigned char dst_str[1000];
406  unsigned char dst_hexstr[2000];
407  int src_len, key_len;
408  arc4_context ctx;
409 
410  memset(src_str, 0x00, 1000);
411  memset(key_str, 0x00, 1000);
412  memset(dst_str, 0x00, 1000);
413  memset(dst_hexstr, 0x00, 2000);
414  arc4_init( &ctx );
415 
416  src_len = unhexify( src_str, hex_src_string );
417  key_len = unhexify( key_str, hex_key_string );
418 
419  arc4_setup(&ctx, key_str, key_len);
420  TEST_ASSERT( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
421  hexify( dst_hexstr, dst_str, src_len );
422 
423  TEST_ASSERT( strcmp( (char *) dst_hexstr, hex_dst_string ) == 0 );
424 
425 exit:
426  arc4_free( &ctx );
427 }
428 
429 #ifdef POLARSSL_SELF_TEST
430 void test_suite_arc4_selftest()
431 {
432  TEST_ASSERT( arc4_self_test( 0 ) == 0 );
433 
434 exit:
435  return;
436 }
437 #endif /* POLARSSL_SELF_TEST */
438 
439 
440 #endif /* POLARSSL_ARC4_C */
441 
442 
443 int dep_check( char *str )
444 {
445  if( str == NULL )
446  return( 1 );
447 
448  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
449  {
450 #if defined(POLARSSL_SELF_TEST)
451  return( 0 );
452 #else
453  return( 1 );
454 #endif
455  }
456 
457 
458  return( 1 );
459 }
460 
461 int dispatch_test(int cnt, char *params[50])
462 {
463  int ret;
464  ((void) cnt);
465  ((void) params);
466 
467 #if defined(TEST_SUITE_ACTIVE)
468  if( strcmp( params[0], "arc4_crypt" ) == 0 )
469  {
470 
471  char *param1 = params[1];
472  char *param2 = params[2];
473  char *param3 = params[3];
474 
475  if( cnt != 4 )
476  {
477  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
478  return( 2 );
479  }
480 
481  if( verify_string( &param1 ) != 0 ) return( 2 );
482  if( verify_string( &param2 ) != 0 ) return( 2 );
483  if( verify_string( &param3 ) != 0 ) return( 2 );
484 
485  test_suite_arc4_crypt( param1, param2, param3 );
486  return ( 0 );
487 
488  return ( 3 );
489  }
490  else
491  if( strcmp( params[0], "arc4_selftest" ) == 0 )
492  {
493  #ifdef POLARSSL_SELF_TEST
494 
495 
496  if( cnt != 1 )
497  {
498  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
499  return( 2 );
500  }
501 
502 
503  test_suite_arc4_selftest( );
504  return ( 0 );
505  #endif /* POLARSSL_SELF_TEST */
506 
507  return ( 3 );
508  }
509  else
510 
511  {
512  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
513  fflush( stdout );
514  return( 1 );
515  }
516 #else
517  return( 3 );
518 #endif
519  return( ret );
520 }
521 
522 int get_line( FILE *f, char *buf, size_t len )
523 {
524  char *ret;
525 
526  ret = fgets( buf, len, f );
527  if( ret == NULL )
528  return( -1 );
529 
530  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
531  buf[strlen(buf) - 1] = '\0';
532  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
533  buf[strlen(buf) - 1] = '\0';
534 
535  return( 0 );
536 }
537 
538 int parse_arguments( char *buf, size_t len, char *params[50] )
539 {
540  int cnt = 0, i;
541  char *cur = buf;
542  char *p = buf, *q;
543 
544  params[cnt++] = cur;
545 
546  while( *p != '\0' && p < buf + len )
547  {
548  if( *p == '\\' )
549  {
550  p++;
551  p++;
552  continue;
553  }
554  if( *p == ':' )
555  {
556  if( p + 1 < buf + len )
557  {
558  cur = p + 1;
559  params[cnt++] = cur;
560  }
561  *p = '\0';
562  }
563 
564  p++;
565  }
566 
567  // Replace newlines, question marks and colons in strings
568  for( i = 0; i < cnt; i++ )
569  {
570  p = params[i];
571  q = params[i];
572 
573  while( *p != '\0' )
574  {
575  if( *p == '\\' && *(p + 1) == 'n' )
576  {
577  p += 2;
578  *(q++) = '\n';
579  }
580  else if( *p == '\\' && *(p + 1) == ':' )
581  {
582  p += 2;
583  *(q++) = ':';
584  }
585  else if( *p == '\\' && *(p + 1) == '?' )
586  {
587  p += 2;
588  *(q++) = '?';
589  }
590  else
591  *(q++) = *(p++);
592  }
593  *q = '\0';
594  }
595 
596  return( cnt );
597 }
598 
599 int main()
600 {
601  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
602  const char *filename = "/tmp/B.9tfrh6rj/BUILD/polarssl-1.3.9/tests/suites/test_suite_arc4.data";
603  FILE *file;
604  char buf[5000];
605  char *params[50];
606 
607 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
608  unsigned char alloc_buf[1000000];
609  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
610 #endif
611 
612  file = fopen( filename, "r" );
613  if( file == NULL )
614  {
615  fprintf( stderr, "Failed to open\n" );
616  return( 1 );
617  }
618 
619  while( !feof( file ) )
620  {
621  int skip = 0;
622 
623  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
624  break;
625  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
626  fprintf( stdout, " " );
627  for( i = strlen( buf ) + 1; i < 67; i++ )
628  fprintf( stdout, "." );
629  fprintf( stdout, " " );
630  fflush( stdout );
631 
632  total_tests++;
633 
634  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
635  break;
636  cnt = parse_arguments( buf, strlen(buf), params );
637 
638  if( strcmp( params[0], "depends_on" ) == 0 )
639  {
640  for( i = 1; i < cnt; i++ )
641  if( dep_check( params[i] ) != 0 )
642  skip = 1;
643 
644  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
645  break;
646  cnt = parse_arguments( buf, strlen(buf), params );
647  }
648 
649  if( skip == 0 )
650  {
651  test_errors = 0;
652  ret = dispatch_test( cnt, params );
653  }
654 
655  if( skip == 1 || ret == 3 )
656  {
657  total_skipped++;
658  fprintf( stdout, "----\n" );
659  fflush( stdout );
660  }
661  else if( ret == 0 && test_errors == 0 )
662  {
663  fprintf( stdout, "PASS\n" );
664  fflush( stdout );
665  }
666  else if( ret == 2 )
667  {
668  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
669  fclose(file);
670  exit( 2 );
671  }
672  else
673  total_errors++;
674 
675  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
676  break;
677  if( strlen(buf) != 0 )
678  {
679  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
680  return( 1 );
681  }
682  }
683  fclose(file);
684 
685  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
686  if( total_errors == 0 )
687  fprintf( stdout, "PASSED" );
688  else
689  fprintf( stdout, "FAILED" );
690 
691  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
692  total_tests - total_errors, total_tests, total_skipped );
693 
694 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
695 #if defined(POLARSSL_MEMORY_DEBUG)
696  memory_buffer_alloc_status();
697 #endif
699 #endif
700 
701  return( total_errors != 0 );
702 }
703 
704 
#define polarssl_malloc
Memory allocation layer (Deprecated to platform layer)
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
void arc4_init(arc4_context *ctx)
Initialize ARC4 context.
void arc4_setup(arc4_context *ctx, const unsigned char *key, unsigned int keylen)
ARC4 key schedule.
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int dispatch_test(int cnt, char *params[50])
int arc4_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
void arc4_free(arc4_context *ctx)
Clear ARC4 context.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
static int test_errors
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int verify_string(char **str)
int dep_check(char *str)
ARC4 context structure.
Definition: arc4.h:49
unsigned char * buf
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 main()
int get_line(FILE *f, char *buf, size_t len)
int verify_int(char *str, int *value)
The ARCFOUR stream cipher.
int parse_arguments(char *buf, size_t len, char *params[50])