PolarSSL v1.3.9
test_suite_blowfish.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_BLOWFISH_C
8 
9 #include "polarssl/blowfish.h"
10 #endif /* POLARSSL_BLOWFISH_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_BLOWFISH_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 #ifdef POLARSSL_CIPHER_MODE_CBC
395  if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH" ) == 0 )
396  {
398  return( 0 );
399  }
400 #endif // POLARSSL_CIPHER_MODE_CBC
401  if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH" ) == 0 )
402  {
404  return( 0 );
405  }
406 
407 
408  printf( "Expected integer for parameter and got: %s\n", str );
409  return( -1 );
410 }
411 
412 void test_suite_blowfish_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  blowfish_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  blowfish_init( &ctx );
427 
428  key_len = unhexify( key_str, hex_key_string );
429  unhexify( src_str, hex_src_string );
430 
431  TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
432  if( setkey_result == 0 )
433  {
434  TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_ENCRYPT, src_str, output ) == 0 );
435  hexify( dst_str, output, 8 );
436 
437  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
438  }
439 
440 exit:
441  blowfish_free( &ctx );
442 }
443 
444 void test_suite_blowfish_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  blowfish_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  blowfish_init( &ctx );
459 
460  key_len = unhexify( key_str, hex_key_string );
461  unhexify( src_str, hex_src_string );
462 
463  TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
464  if( setkey_result == 0 )
465  {
466  TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_DECRYPT, src_str, output ) == 0 );
467  hexify( dst_str, output, 8 );
468 
469  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
470  }
471 
472 exit:
473  blowfish_free( &ctx );
474 }
475 
476 #ifdef POLARSSL_CIPHER_MODE_CBC
477 void test_suite_blowfish_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  blowfish_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  blowfish_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  blowfish_setkey( &ctx, key_str, key_len * 8 );
501 
502  TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
503  if( cbc_result == 0 )
504  {
505  hexify( dst_str, output, data_len );
506 
507  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
508  }
509 
510 exit:
511  blowfish_free( &ctx );
512 }
513 #endif /* POLARSSL_CIPHER_MODE_CBC */
514 
515 #ifdef POLARSSL_CIPHER_MODE_CBC
516 void test_suite_blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
517  char *hex_src_string, char *hex_dst_string,
518  int cbc_result )
519 {
520  unsigned char key_str[100];
521  unsigned char iv_str[100];
522  unsigned char src_str[100];
523  unsigned char dst_str[100];
524  unsigned char output[100];
525  blowfish_context ctx;
526  int key_len, data_len;
527 
528  memset(key_str, 0x00, 100);
529  memset(iv_str, 0x00, 100);
530  memset(src_str, 0x00, 100);
531  memset(dst_str, 0x00, 100);
532  memset(output, 0x00, 100);
533  blowfish_init( &ctx );
534 
535  key_len = unhexify( key_str, hex_key_string );
536  unhexify( iv_str, hex_iv_string );
537  data_len = unhexify( src_str, hex_src_string );
538 
539  blowfish_setkey( &ctx, key_str, key_len * 8 );
540  TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
541  if( cbc_result == 0)
542  {
543  hexify( dst_str, output, data_len );
544 
545  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
546  }
547 
548 exit:
549  blowfish_free( &ctx );
550 }
551 #endif /* POLARSSL_CIPHER_MODE_CBC */
552 
553 #ifdef POLARSSL_CIPHER_MODE_CFB
554 void test_suite_blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
555  char *hex_src_string, char *hex_dst_string )
556 {
557  unsigned char key_str[100];
558  unsigned char iv_str[100];
559  unsigned char src_str[100];
560  unsigned char dst_str[100];
561  unsigned char output[100];
562  blowfish_context ctx;
563  size_t iv_offset = 0;
564  int key_len, src_len;
565 
566  memset(key_str, 0x00, 100);
567  memset(iv_str, 0x00, 100);
568  memset(src_str, 0x00, 100);
569  memset(dst_str, 0x00, 100);
570  memset(output, 0x00, 100);
571  blowfish_init( &ctx );
572 
573  key_len = unhexify( key_str, hex_key_string );
574  unhexify( iv_str, hex_iv_string );
575  src_len = unhexify( src_str, hex_src_string );
576 
577  blowfish_setkey( &ctx, key_str, key_len * 8 );
578  TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
579  hexify( dst_str, output, src_len );
580 
581  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
582 
583 exit:
584  blowfish_free( &ctx );
585 }
586 #endif /* POLARSSL_CIPHER_MODE_CFB */
587 
588 #ifdef POLARSSL_CIPHER_MODE_CFB
589 void test_suite_blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
590  char *hex_src_string, char *hex_dst_string )
591 {
592  unsigned char key_str[100];
593  unsigned char iv_str[100];
594  unsigned char src_str[100];
595  unsigned char dst_str[100];
596  unsigned char output[100];
597  blowfish_context ctx;
598  size_t iv_offset = 0;
599  int key_len, src_len;
600 
601  memset(key_str, 0x00, 100);
602  memset(iv_str, 0x00, 100);
603  memset(src_str, 0x00, 100);
604  memset(dst_str, 0x00, 100);
605  memset(output, 0x00, 100);
606  blowfish_init( &ctx );
607 
608  key_len = unhexify( key_str, hex_key_string );
609  unhexify( iv_str, hex_iv_string );
610  src_len = unhexify( src_str, hex_src_string );
611 
612  blowfish_setkey( &ctx, key_str, key_len * 8 );
613  TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
614  hexify( dst_str, output, src_len );
615 
616  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
617 
618 exit:
619  blowfish_free( &ctx );
620 }
621 #endif /* POLARSSL_CIPHER_MODE_CFB */
622 
623 #ifdef POLARSSL_CIPHER_MODE_CTR
624 void test_suite_blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
625  char *hex_src_string, char *hex_dst_string )
626 {
627  unsigned char key_str[100];
628  unsigned char iv_str[100];
629  unsigned char stream_str[100];
630  unsigned char src_str[100];
631  unsigned char dst_str[100];
632  unsigned char output[100];
633  blowfish_context ctx;
634  size_t iv_offset = 0;
635  int key_len, src_len;
636 
637  memset(key_str, 0x00, 100);
638  memset(iv_str, 0x00, 100);
639  memset(stream_str, 0x00, 100);
640  memset(src_str, 0x00, 100);
641  memset(dst_str, 0x00, 100);
642  memset(output, 0x00, 100);
643  blowfish_init( &ctx );
644 
645  key_len = unhexify( key_str, hex_key_string );
646  unhexify( iv_str, hex_iv_string );
647  src_len = unhexify( src_str, hex_src_string );
648 
649  blowfish_setkey( &ctx, key_str, key_len * 8 );
650  TEST_ASSERT( blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
651  hexify( dst_str, output, src_len );
652 
653  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
654 
655 exit:
656  blowfish_free( &ctx );
657 }
658 #endif /* POLARSSL_CIPHER_MODE_CTR */
659 
660 
661 #endif /* POLARSSL_BLOWFISH_C */
662 
663 
664 int dep_check( char *str )
665 {
666  if( str == NULL )
667  return( 1 );
668 
669 
670 
671  return( 1 );
672 }
673 
674 int dispatch_test(int cnt, char *params[50])
675 {
676  int ret;
677  ((void) cnt);
678  ((void) params);
679 
680 #if defined(TEST_SUITE_ACTIVE)
681  if( strcmp( params[0], "blowfish_encrypt_ecb" ) == 0 )
682  {
683 
684  char *param1 = params[1];
685  char *param2 = params[2];
686  char *param3 = params[3];
687  int param4;
688 
689  if( cnt != 5 )
690  {
691  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
692  return( 2 );
693  }
694 
695  if( verify_string( &param1 ) != 0 ) return( 2 );
696  if( verify_string( &param2 ) != 0 ) return( 2 );
697  if( verify_string( &param3 ) != 0 ) return( 2 );
698  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
699 
700  test_suite_blowfish_encrypt_ecb( param1, param2, param3, param4 );
701  return ( 0 );
702 
703  return ( 3 );
704  }
705  else
706  if( strcmp( params[0], "blowfish_decrypt_ecb" ) == 0 )
707  {
708 
709  char *param1 = params[1];
710  char *param2 = params[2];
711  char *param3 = params[3];
712  int param4;
713 
714  if( cnt != 5 )
715  {
716  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
717  return( 2 );
718  }
719 
720  if( verify_string( &param1 ) != 0 ) return( 2 );
721  if( verify_string( &param2 ) != 0 ) return( 2 );
722  if( verify_string( &param3 ) != 0 ) return( 2 );
723  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
724 
725  test_suite_blowfish_decrypt_ecb( param1, param2, param3, param4 );
726  return ( 0 );
727 
728  return ( 3 );
729  }
730  else
731  if( strcmp( params[0], "blowfish_encrypt_cbc" ) == 0 )
732  {
733  #ifdef POLARSSL_CIPHER_MODE_CBC
734 
735  char *param1 = params[1];
736  char *param2 = params[2];
737  char *param3 = params[3];
738  char *param4 = params[4];
739  int param5;
740 
741  if( cnt != 6 )
742  {
743  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
744  return( 2 );
745  }
746 
747  if( verify_string( &param1 ) != 0 ) return( 2 );
748  if( verify_string( &param2 ) != 0 ) return( 2 );
749  if( verify_string( &param3 ) != 0 ) return( 2 );
750  if( verify_string( &param4 ) != 0 ) return( 2 );
751  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
752 
753  test_suite_blowfish_encrypt_cbc( param1, param2, param3, param4, param5 );
754  return ( 0 );
755  #endif /* POLARSSL_CIPHER_MODE_CBC */
756 
757  return ( 3 );
758  }
759  else
760  if( strcmp( params[0], "blowfish_decrypt_cbc" ) == 0 )
761  {
762  #ifdef POLARSSL_CIPHER_MODE_CBC
763 
764  char *param1 = params[1];
765  char *param2 = params[2];
766  char *param3 = params[3];
767  char *param4 = params[4];
768  int param5;
769 
770  if( cnt != 6 )
771  {
772  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
773  return( 2 );
774  }
775 
776  if( verify_string( &param1 ) != 0 ) return( 2 );
777  if( verify_string( &param2 ) != 0 ) return( 2 );
778  if( verify_string( &param3 ) != 0 ) return( 2 );
779  if( verify_string( &param4 ) != 0 ) return( 2 );
780  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
781 
782  test_suite_blowfish_decrypt_cbc( param1, param2, param3, param4, param5 );
783  return ( 0 );
784  #endif /* POLARSSL_CIPHER_MODE_CBC */
785 
786  return ( 3 );
787  }
788  else
789  if( strcmp( params[0], "blowfish_encrypt_cfb64" ) == 0 )
790  {
791  #ifdef POLARSSL_CIPHER_MODE_CFB
792 
793  char *param1 = params[1];
794  char *param2 = params[2];
795  char *param3 = params[3];
796  char *param4 = params[4];
797 
798  if( cnt != 5 )
799  {
800  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
801  return( 2 );
802  }
803 
804  if( verify_string( &param1 ) != 0 ) return( 2 );
805  if( verify_string( &param2 ) != 0 ) return( 2 );
806  if( verify_string( &param3 ) != 0 ) return( 2 );
807  if( verify_string( &param4 ) != 0 ) return( 2 );
808 
809  test_suite_blowfish_encrypt_cfb64( param1, param2, param3, param4 );
810  return ( 0 );
811  #endif /* POLARSSL_CIPHER_MODE_CFB */
812 
813  return ( 3 );
814  }
815  else
816  if( strcmp( params[0], "blowfish_decrypt_cfb64" ) == 0 )
817  {
818  #ifdef POLARSSL_CIPHER_MODE_CFB
819 
820  char *param1 = params[1];
821  char *param2 = params[2];
822  char *param3 = params[3];
823  char *param4 = params[4];
824 
825  if( cnt != 5 )
826  {
827  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
828  return( 2 );
829  }
830 
831  if( verify_string( &param1 ) != 0 ) return( 2 );
832  if( verify_string( &param2 ) != 0 ) return( 2 );
833  if( verify_string( &param3 ) != 0 ) return( 2 );
834  if( verify_string( &param4 ) != 0 ) return( 2 );
835 
836  test_suite_blowfish_decrypt_cfb64( param1, param2, param3, param4 );
837  return ( 0 );
838  #endif /* POLARSSL_CIPHER_MODE_CFB */
839 
840  return ( 3 );
841  }
842  else
843  if( strcmp( params[0], "blowfish_encrypt_ctr" ) == 0 )
844  {
845  #ifdef POLARSSL_CIPHER_MODE_CTR
846 
847  char *param1 = params[1];
848  char *param2 = params[2];
849  char *param3 = params[3];
850  char *param4 = params[4];
851 
852  if( cnt != 5 )
853  {
854  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
855  return( 2 );
856  }
857 
858  if( verify_string( &param1 ) != 0 ) return( 2 );
859  if( verify_string( &param2 ) != 0 ) return( 2 );
860  if( verify_string( &param3 ) != 0 ) return( 2 );
861  if( verify_string( &param4 ) != 0 ) return( 2 );
862 
863  test_suite_blowfish_encrypt_ctr( param1, param2, param3, param4 );
864  return ( 0 );
865  #endif /* POLARSSL_CIPHER_MODE_CTR */
866 
867  return ( 3 );
868  }
869  else
870 
871  {
872  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
873  fflush( stdout );
874  return( 1 );
875  }
876 #else
877  return( 3 );
878 #endif
879  return( ret );
880 }
881 
882 int get_line( FILE *f, char *buf, size_t len )
883 {
884  char *ret;
885 
886  ret = fgets( buf, len, f );
887  if( ret == NULL )
888  return( -1 );
889 
890  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
891  buf[strlen(buf) - 1] = '\0';
892  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
893  buf[strlen(buf) - 1] = '\0';
894 
895  return( 0 );
896 }
897 
898 int parse_arguments( char *buf, size_t len, char *params[50] )
899 {
900  int cnt = 0, i;
901  char *cur = buf;
902  char *p = buf, *q;
903 
904  params[cnt++] = cur;
905 
906  while( *p != '\0' && p < buf + len )
907  {
908  if( *p == '\\' )
909  {
910  p++;
911  p++;
912  continue;
913  }
914  if( *p == ':' )
915  {
916  if( p + 1 < buf + len )
917  {
918  cur = p + 1;
919  params[cnt++] = cur;
920  }
921  *p = '\0';
922  }
923 
924  p++;
925  }
926 
927  // Replace newlines, question marks and colons in strings
928  for( i = 0; i < cnt; i++ )
929  {
930  p = params[i];
931  q = params[i];
932 
933  while( *p != '\0' )
934  {
935  if( *p == '\\' && *(p + 1) == 'n' )
936  {
937  p += 2;
938  *(q++) = '\n';
939  }
940  else if( *p == '\\' && *(p + 1) == ':' )
941  {
942  p += 2;
943  *(q++) = ':';
944  }
945  else if( *p == '\\' && *(p + 1) == '?' )
946  {
947  p += 2;
948  *(q++) = '?';
949  }
950  else
951  *(q++) = *(p++);
952  }
953  *q = '\0';
954  }
955 
956  return( cnt );
957 }
958 
959 int main()
960 {
961  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
962  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_blowfish.data";
963  FILE *file;
964  char buf[5000];
965  char *params[50];
966 
967 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
968  unsigned char alloc_buf[1000000];
969  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
970 #endif
971 
972  file = fopen( filename, "r" );
973  if( file == NULL )
974  {
975  fprintf( stderr, "Failed to open\n" );
976  return( 1 );
977  }
978 
979  while( !feof( file ) )
980  {
981  int skip = 0;
982 
983  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
984  break;
985  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
986  fprintf( stdout, " " );
987  for( i = strlen( buf ) + 1; i < 67; i++ )
988  fprintf( stdout, "." );
989  fprintf( stdout, " " );
990  fflush( stdout );
991 
992  total_tests++;
993 
994  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
995  break;
996  cnt = parse_arguments( buf, strlen(buf), params );
997 
998  if( strcmp( params[0], "depends_on" ) == 0 )
999  {
1000  for( i = 1; i < cnt; i++ )
1001  if( dep_check( params[i] ) != 0 )
1002  skip = 1;
1003 
1004  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1005  break;
1006  cnt = parse_arguments( buf, strlen(buf), params );
1007  }
1008 
1009  if( skip == 0 )
1010  {
1011  test_errors = 0;
1012  ret = dispatch_test( cnt, params );
1013  }
1014 
1015  if( skip == 1 || ret == 3 )
1016  {
1017  total_skipped++;
1018  fprintf( stdout, "----\n" );
1019  fflush( stdout );
1020  }
1021  else if( ret == 0 && test_errors == 0 )
1022  {
1023  fprintf( stdout, "PASS\n" );
1024  fflush( stdout );
1025  }
1026  else if( ret == 2 )
1027  {
1028  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1029  fclose(file);
1030  exit( 2 );
1031  }
1032  else
1033  total_errors++;
1034 
1035  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1036  break;
1037  if( strlen(buf) != 0 )
1038  {
1039  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1040  return( 1 );
1041  }
1042  }
1043  fclose(file);
1044 
1045  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1046  if( total_errors == 0 )
1047  fprintf( stdout, "PASSED" );
1048  else
1049  fprintf( stdout, "FAILED" );
1050 
1051  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1052  total_tests - total_errors, total_tests, total_skipped );
1053 
1054 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1055 #if defined(POLARSSL_MEMORY_DEBUG)
1056  memory_buffer_alloc_status();
1057 #endif
1059 #endif
1060 
1061  return( total_errors != 0 );
1062 }
1063 
1064 
static int test_errors
static int unhexify(unsigned char *obuf, const char *ibuf)
int blowfish_setkey(blowfish_context *ctx, const unsigned char *key, unsigned int keysize)
Blowfish key schedule.
void blowfish_init(blowfish_context *ctx)
Initialize Blowfish context.
Memory allocation layer (Deprecated to platform layer)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
#define polarssl_malloc
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
Configuration options (set of defines)
#define BLOWFISH_ENCRYPT
Definition: blowfish.h:45
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
int main(int argc, char *argv[])
void blowfish_free(blowfish_context *ctx)
Clear Blowfish context.
int dep_check(char *str)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
Blowfish block cipher.
int blowfish_crypt_ecb(blowfish_context *ctx, int mode, const unsigned char input[BLOWFISH_BLOCKSIZE], unsigned char output[BLOWFISH_BLOCKSIZE])
Blowfish-ECB block encryption/decryption.
#define PUT_UINT32_BE(n, b, i)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int parse_arguments(char *buf, size_t len, char *params[50])
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define BLOWFISH_DECRYPT
Definition: blowfish.h:46
int verify_string(char **str)
#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
Invalid key length.
Definition: blowfish.h:52
#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Invalid data input length.
Definition: blowfish.h:53
int dispatch_test(int cnt, char *params[50])
int blowfish_crypt_ctr(blowfish_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], unsigned char stream_block[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish-CTR buffer encryption/decryption.
unsigned char * buf
int blowfish_crypt_cfb64(blowfish_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish CFB buffer encryption/decryption.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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 verify_int(char *str, int *value)
Blowfish context structure.
Definition: blowfish.h:66
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int blowfish_crypt_cbc(blowfish_context *ctx, int mode, size_t length, unsigned char iv[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish-CBC buffer encryption/decryption Length should be a multiple of the block size (8 bytes) ...
int get_line(FILE *f, char *buf, size_t len)