PolarSSL v1.3.2
test_suite_blowfish.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_BLOWFISH_C
4 
5 #include "polarssl/blowfish.h"
6 #endif /* POLARSSL_BLOWFISH_C */
7 
8 
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
10 #include "polarssl/memory.h"
11 #endif
12 
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
15 #include "polarssl/bignum.h"
16 #else
17 #error "not_rnd_mpi() need bignum.c"
18 #endif
19 #endif
20 
21 #ifdef _MSC_VER
22 #include <basetsd.h>
23 typedef UINT32 uint32_t;
24 #else
25 #include <inttypes.h>
26 #endif
27 
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 /*
33  * 32-bit integer manipulation macros (big endian)
34  */
35 #ifndef GET_UINT32_BE
36 #define GET_UINT32_BE(n,b,i) \
37 { \
38  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41  | ( (uint32_t) (b)[(i) + 3] ); \
42 }
43 #endif
44 
45 #ifndef PUT_UINT32_BE
46 #define PUT_UINT32_BE(n,b,i) \
47 { \
48  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51  (b)[(i) + 3] = (unsigned char) ( (n) ); \
52 }
53 #endif
54 
55 static int unhexify(unsigned char *obuf, const char *ibuf)
56 {
57  unsigned char c, c2;
58  int len = strlen(ibuf) / 2;
59  assert(!(strlen(ibuf) %1)); // must be even number of bytes
60 
61  while (*ibuf != 0)
62  {
63  c = *ibuf++;
64  if( c >= '0' && c <= '9' )
65  c -= '0';
66  else if( c >= 'a' && c <= 'f' )
67  c -= 'a' - 10;
68  else if( c >= 'A' && c <= 'F' )
69  c -= 'A' - 10;
70  else
71  assert( 0 );
72 
73  c2 = *ibuf++;
74  if( c2 >= '0' && c2 <= '9' )
75  c2 -= '0';
76  else if( c2 >= 'a' && c2 <= 'f' )
77  c2 -= 'a' - 10;
78  else if( c2 >= 'A' && c2 <= 'F' )
79  c2 -= 'A' - 10;
80  else
81  assert( 0 );
82 
83  *obuf++ = ( c << 4 ) | c2;
84  }
85 
86  return len;
87 }
88 
89 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
90 {
91  unsigned char l, h;
92 
93  while (len != 0)
94  {
95  h = (*ibuf) / 16;
96  l = (*ibuf) % 16;
97 
98  if( h < 10 )
99  *obuf++ = '0' + h;
100  else
101  *obuf++ = 'a' + h - 10;
102 
103  if( l < 10 )
104  *obuf++ = '0' + l;
105  else
106  *obuf++ = 'a' + l - 10;
107 
108  ++ibuf;
109  len--;
110  }
111 }
112 
122 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
123 {
124  size_t i;
125 
126  if( rng_state != NULL )
127  rng_state = NULL;
128 
129  for( i = 0; i < len; ++i )
130  output[i] = rand();
131 
132  return( 0 );
133 }
134 
140 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
141 {
142  if( rng_state != NULL )
143  rng_state = NULL;
144 
145  memset( output, 0, len );
146 
147  return( 0 );
148 }
149 
150 typedef struct
151 {
152  unsigned char *buf;
153  size_t length;
154 } rnd_buf_info;
155 
167 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
168 {
169  rnd_buf_info *info = (rnd_buf_info *) rng_state;
170  size_t use_len;
171 
172  if( rng_state == NULL )
173  return( rnd_std_rand( NULL, output, len ) );
174 
175  use_len = len;
176  if( len > info->length )
177  use_len = info->length;
178 
179  if( use_len )
180  {
181  memcpy( output, info->buf, use_len );
182  info->buf += use_len;
183  info->length -= use_len;
184  }
185 
186  if( len - use_len > 0 )
187  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
188 
189  return( 0 );
190 }
191 
199 typedef struct
200 {
201  uint32_t key[16];
202  uint32_t v0, v1;
204 
213 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
214 {
215  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
216  uint32_t i, *k, sum, delta=0x9E3779B9;
217  unsigned char result[4];
218 
219  if( rng_state == NULL )
220  return( rnd_std_rand( NULL, output, len ) );
221 
222  k = info->key;
223 
224  while( len > 0 )
225  {
226  size_t use_len = ( len > 4 ) ? 4 : len;
227  sum = 0;
228 
229  for( i = 0; i < 32; i++ )
230  {
231  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
232  sum += delta;
233  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
234  }
235 
236  PUT_UINT32_BE( info->v0, result, 0 );
237  memcpy( output, result, use_len );
238  len -= use_len;
239  }
240 
241  return( 0 );
242 }
243 
244 #if defined(WANT_NOT_RND_MPI)
245 
253 #define ciL (sizeof(t_uint)) /* chars in limb */
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
256 {
257  char *str = (char *) in;
258  mpi X;
259 
260  /*
261  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
262  * just reconstruct the rest in order to be able to call mpi_read_string()
263  */
264  X.s = 1;
265  X.p = (t_uint *) out;
266  X.n = CHARS_TO_LIMBS( len );
267 
268  /*
269  * If str is too long, mpi_read_string() will try to allocate a new buffer
270  * for X.p, which we want to avoid at all costs.
271  */
272  assert( strlen( str ) / 2 == len );
273 
274  return( mpi_read_string( &X, 16, str ) );
275 }
276 #endif /* WANT_NOT_RND_MPI */
277 
278 
279 #include <stdio.h>
280 #include <string.h>
281 
282 static int test_errors = 0;
283 
284 #ifdef POLARSSL_BLOWFISH_C
285 
286 #define TEST_SUITE_ACTIVE
287 
288 static int test_assert( int correct, char *test )
289 {
290  if( correct )
291  return( 0 );
292 
293  test_errors++;
294  if( test_errors == 1 )
295  printf( "FAILED\n" );
296  printf( " %s\n", test );
297 
298  return( 1 );
299 }
300 
301 #define TEST_ASSERT( TEST ) \
302  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
303  if( test_errors) return; \
304  } while (0)
305 
306 int verify_string( char **str )
307 {
308  if( (*str)[0] != '"' ||
309  (*str)[strlen( *str ) - 1] != '"' )
310  {
311  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
312  return( -1 );
313  }
314 
315  (*str)++;
316  (*str)[strlen( *str ) - 1] = '\0';
317 
318  return( 0 );
319 }
320 
321 int verify_int( char *str, int *value )
322 {
323  size_t i;
324  int minus = 0;
325  int digits = 1;
326  int hex = 0;
327 
328  for( i = 0; i < strlen( str ); i++ )
329  {
330  if( i == 0 && str[i] == '-' )
331  {
332  minus = 1;
333  continue;
334  }
335 
336  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
337  str[i - 1] == '0' && str[i] == 'x' )
338  {
339  hex = 1;
340  continue;
341  }
342 
343  if( str[i] < '0' || str[i] > '9' )
344  {
345  digits = 0;
346  break;
347  }
348  }
349 
350  if( digits )
351  {
352  if( hex )
353  *value = strtol( str, NULL, 16 );
354  else
355  *value = strtol( str, NULL, 10 );
356 
357  return( 0 );
358  }
359 
360  if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH" ) == 0 )
361  {
363  return( 0 );
364  }
365 #ifdef POLARSSL_CIPHER_MODE_CBC
366  if( strcmp( str, "POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH" ) == 0 )
367  {
369  return( 0 );
370  }
371 #endif // POLARSSL_CIPHER_MODE_CBC
372 
373 
374  printf( "Expected integer for parameter and got: %s\n", str );
375  return( -1 );
376 }
377 
378 void test_suite_blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string,
379  char *hex_dst_string, int setkey_result )
380 {
381  unsigned char key_str[100];
382  unsigned char src_str[100];
383  unsigned char dst_str[100];
384  unsigned char output[100];
385  blowfish_context ctx;
386  int key_len;
387 
388  memset(key_str, 0x00, 100);
389  memset(src_str, 0x00, 100);
390  memset(dst_str, 0x00, 100);
391  memset(output, 0x00, 100);
392 
393  key_len = unhexify( key_str, hex_key_string );
394  unhexify( src_str, hex_src_string );
395 
396  TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
397  if( setkey_result == 0 )
398  {
399  TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_ENCRYPT, src_str, output ) == 0 );
400  hexify( dst_str, output, 8 );
401 
402  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
403  }
404 }
405 
406 void test_suite_blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string,
407  char *hex_dst_string, int setkey_result )
408 {
409  unsigned char key_str[100];
410  unsigned char src_str[100];
411  unsigned char dst_str[100];
412  unsigned char output[100];
413  blowfish_context ctx;
414  int key_len;
415 
416  memset(key_str, 0x00, 100);
417  memset(src_str, 0x00, 100);
418  memset(dst_str, 0x00, 100);
419  memset(output, 0x00, 100);
420 
421  key_len = unhexify( key_str, hex_key_string );
422  unhexify( src_str, hex_src_string );
423 
424  TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
425  if( setkey_result == 0 )
426  {
427  TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_DECRYPT, src_str, output ) == 0 );
428  hexify( dst_str, output, 8 );
429 
430  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
431  }
432 }
433 
434 #ifdef POLARSSL_CIPHER_MODE_CBC
435 void test_suite_blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
436  char *hex_src_string, char *hex_dst_string,
437  int cbc_result )
438 {
439  unsigned char key_str[100];
440  unsigned char iv_str[100];
441  unsigned char src_str[100];
442  unsigned char dst_str[100];
443  unsigned char output[100];
444  blowfish_context ctx;
445  int key_len, data_len;
446 
447  memset(key_str, 0x00, 100);
448  memset(iv_str, 0x00, 100);
449  memset(src_str, 0x00, 100);
450  memset(dst_str, 0x00, 100);
451  memset(output, 0x00, 100);
452 
453  key_len = unhexify( key_str, hex_key_string );
454  unhexify( iv_str, hex_iv_string );
455  data_len = unhexify( src_str, hex_src_string );
456 
457  blowfish_setkey( &ctx, key_str, key_len * 8 );
458 
459  TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
460  if( cbc_result == 0 )
461  {
462  hexify( dst_str, output, data_len );
463 
464  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
465  }
466 }
467 #endif /* POLARSSL_CIPHER_MODE_CBC */
468 
469 #ifdef POLARSSL_CIPHER_MODE_CBC
470 void test_suite_blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
471  char *hex_src_string, char *hex_dst_string,
472  int cbc_result )
473 {
474  unsigned char key_str[100];
475  unsigned char iv_str[100];
476  unsigned char src_str[100];
477  unsigned char dst_str[100];
478  unsigned char output[100];
479  blowfish_context ctx;
480  int key_len, data_len;
481 
482  memset(key_str, 0x00, 100);
483  memset(iv_str, 0x00, 100);
484  memset(src_str, 0x00, 100);
485  memset(dst_str, 0x00, 100);
486  memset(output, 0x00, 100);
487 
488  key_len = unhexify( key_str, hex_key_string );
489  unhexify( iv_str, hex_iv_string );
490  data_len = unhexify( src_str, hex_src_string );
491 
492  blowfish_setkey( &ctx, key_str, key_len * 8 );
493  TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
494  if( cbc_result == 0)
495  {
496  hexify( dst_str, output, data_len );
497 
498  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
499  }
500 }
501 #endif /* POLARSSL_CIPHER_MODE_CBC */
502 
503 void test_suite_blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
504  char *hex_src_string, char *hex_dst_string )
505 {
506  unsigned char key_str[100];
507  unsigned char iv_str[100];
508  unsigned char src_str[100];
509  unsigned char dst_str[100];
510  unsigned char output[100];
511  blowfish_context ctx;
512  size_t iv_offset = 0;
513  int key_len, src_len;
514 
515  memset(key_str, 0x00, 100);
516  memset(iv_str, 0x00, 100);
517  memset(src_str, 0x00, 100);
518  memset(dst_str, 0x00, 100);
519  memset(output, 0x00, 100);
520 
521  key_len = unhexify( key_str, hex_key_string );
522  unhexify( iv_str, hex_iv_string );
523  src_len = unhexify( src_str, hex_src_string );
524 
525  blowfish_setkey( &ctx, key_str, key_len * 8 );
526  TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
527  hexify( dst_str, output, src_len );
528 
529  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
530 }
531 
532 void test_suite_blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
533  char *hex_src_string, char *hex_dst_string )
534 {
535  unsigned char key_str[100];
536  unsigned char iv_str[100];
537  unsigned char src_str[100];
538  unsigned char dst_str[100];
539  unsigned char output[100];
540  blowfish_context ctx;
541  size_t iv_offset = 0;
542  int key_len, src_len;
543 
544  memset(key_str, 0x00, 100);
545  memset(iv_str, 0x00, 100);
546  memset(src_str, 0x00, 100);
547  memset(dst_str, 0x00, 100);
548  memset(output, 0x00, 100);
549 
550  key_len = unhexify( key_str, hex_key_string );
551  unhexify( iv_str, hex_iv_string );
552  src_len = unhexify( src_str, hex_src_string );
553 
554  blowfish_setkey( &ctx, key_str, key_len * 8 );
555  TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
556  hexify( dst_str, output, src_len );
557 
558  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
559 }
560 
561 void test_suite_blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
562  char *hex_src_string, char *hex_dst_string )
563 {
564  unsigned char key_str[100];
565  unsigned char iv_str[100];
566  unsigned char stream_str[100];
567  unsigned char src_str[100];
568  unsigned char dst_str[100];
569  unsigned char output[100];
570  blowfish_context ctx;
571  size_t iv_offset = 0;
572  int key_len, src_len;
573 
574  memset(key_str, 0x00, 100);
575  memset(iv_str, 0x00, 100);
576  memset(stream_str, 0x00, 100);
577  memset(src_str, 0x00, 100);
578  memset(dst_str, 0x00, 100);
579  memset(output, 0x00, 100);
580 
581  key_len = unhexify( key_str, hex_key_string );
582  unhexify( iv_str, hex_iv_string );
583  src_len = unhexify( src_str, hex_src_string );
584 
585  blowfish_setkey( &ctx, key_str, key_len * 8 );
586  TEST_ASSERT( blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
587  hexify( dst_str, output, src_len );
588 
589  TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
590 }
591 
592 
593 #endif /* POLARSSL_BLOWFISH_C */
594 
595 
596 int dep_check( char *str )
597 {
598  if( str == NULL )
599  return( 1 );
600 
601 
602 
603  return( 1 );
604 }
605 
606 int dispatch_test(int cnt, char *params[50])
607 {
608  int ret;
609  ((void) cnt);
610  ((void) params);
611 
612 #if defined(TEST_SUITE_ACTIVE)
613  if( strcmp( params[0], "blowfish_encrypt_ecb" ) == 0 )
614  {
615 
616  char *param1 = params[1];
617  char *param2 = params[2];
618  char *param3 = params[3];
619  int param4;
620 
621  if( cnt != 5 )
622  {
623  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
624  return( 2 );
625  }
626 
627  if( verify_string( &param1 ) != 0 ) return( 2 );
628  if( verify_string( &param2 ) != 0 ) return( 2 );
629  if( verify_string( &param3 ) != 0 ) return( 2 );
630  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
631 
632  test_suite_blowfish_encrypt_ecb( param1, param2, param3, param4 );
633  return ( 0 );
634 
635  return ( 3 );
636  }
637  else
638  if( strcmp( params[0], "blowfish_decrypt_ecb" ) == 0 )
639  {
640 
641  char *param1 = params[1];
642  char *param2 = params[2];
643  char *param3 = params[3];
644  int param4;
645 
646  if( cnt != 5 )
647  {
648  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
649  return( 2 );
650  }
651 
652  if( verify_string( &param1 ) != 0 ) return( 2 );
653  if( verify_string( &param2 ) != 0 ) return( 2 );
654  if( verify_string( &param3 ) != 0 ) return( 2 );
655  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
656 
657  test_suite_blowfish_decrypt_ecb( param1, param2, param3, param4 );
658  return ( 0 );
659 
660  return ( 3 );
661  }
662  else
663  if( strcmp( params[0], "blowfish_encrypt_cbc" ) == 0 )
664  {
665  #ifdef POLARSSL_CIPHER_MODE_CBC
666 
667  char *param1 = params[1];
668  char *param2 = params[2];
669  char *param3 = params[3];
670  char *param4 = params[4];
671  int param5;
672 
673  if( cnt != 6 )
674  {
675  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
676  return( 2 );
677  }
678 
679  if( verify_string( &param1 ) != 0 ) return( 2 );
680  if( verify_string( &param2 ) != 0 ) return( 2 );
681  if( verify_string( &param3 ) != 0 ) return( 2 );
682  if( verify_string( &param4 ) != 0 ) return( 2 );
683  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
684 
685  test_suite_blowfish_encrypt_cbc( param1, param2, param3, param4, param5 );
686  return ( 0 );
687  #endif /* POLARSSL_CIPHER_MODE_CBC */
688 
689  return ( 3 );
690  }
691  else
692  if( strcmp( params[0], "blowfish_decrypt_cbc" ) == 0 )
693  {
694  #ifdef POLARSSL_CIPHER_MODE_CBC
695 
696  char *param1 = params[1];
697  char *param2 = params[2];
698  char *param3 = params[3];
699  char *param4 = params[4];
700  int param5;
701 
702  if( cnt != 6 )
703  {
704  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
705  return( 2 );
706  }
707 
708  if( verify_string( &param1 ) != 0 ) return( 2 );
709  if( verify_string( &param2 ) != 0 ) return( 2 );
710  if( verify_string( &param3 ) != 0 ) return( 2 );
711  if( verify_string( &param4 ) != 0 ) return( 2 );
712  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
713 
714  test_suite_blowfish_decrypt_cbc( param1, param2, param3, param4, param5 );
715  return ( 0 );
716  #endif /* POLARSSL_CIPHER_MODE_CBC */
717 
718  return ( 3 );
719  }
720  else
721  if( strcmp( params[0], "blowfish_encrypt_cfb64" ) == 0 )
722  {
723 
724  char *param1 = params[1];
725  char *param2 = params[2];
726  char *param3 = params[3];
727  char *param4 = params[4];
728 
729  if( cnt != 5 )
730  {
731  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
732  return( 2 );
733  }
734 
735  if( verify_string( &param1 ) != 0 ) return( 2 );
736  if( verify_string( &param2 ) != 0 ) return( 2 );
737  if( verify_string( &param3 ) != 0 ) return( 2 );
738  if( verify_string( &param4 ) != 0 ) return( 2 );
739 
740  test_suite_blowfish_encrypt_cfb64( param1, param2, param3, param4 );
741  return ( 0 );
742 
743  return ( 3 );
744  }
745  else
746  if( strcmp( params[0], "blowfish_decrypt_cfb64" ) == 0 )
747  {
748 
749  char *param1 = params[1];
750  char *param2 = params[2];
751  char *param3 = params[3];
752  char *param4 = params[4];
753 
754  if( cnt != 5 )
755  {
756  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
757  return( 2 );
758  }
759 
760  if( verify_string( &param1 ) != 0 ) return( 2 );
761  if( verify_string( &param2 ) != 0 ) return( 2 );
762  if( verify_string( &param3 ) != 0 ) return( 2 );
763  if( verify_string( &param4 ) != 0 ) return( 2 );
764 
765  test_suite_blowfish_decrypt_cfb64( param1, param2, param3, param4 );
766  return ( 0 );
767 
768  return ( 3 );
769  }
770  else
771  if( strcmp( params[0], "blowfish_encrypt_ctr" ) == 0 )
772  {
773 
774  char *param1 = params[1];
775  char *param2 = params[2];
776  char *param3 = params[3];
777  char *param4 = params[4];
778 
779  if( cnt != 5 )
780  {
781  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
782  return( 2 );
783  }
784 
785  if( verify_string( &param1 ) != 0 ) return( 2 );
786  if( verify_string( &param2 ) != 0 ) return( 2 );
787  if( verify_string( &param3 ) != 0 ) return( 2 );
788  if( verify_string( &param4 ) != 0 ) return( 2 );
789 
790  test_suite_blowfish_encrypt_ctr( param1, param2, param3, param4 );
791  return ( 0 );
792 
793  return ( 3 );
794  }
795  else
796 
797  {
798  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
799  fflush( stdout );
800  return( 1 );
801  }
802 #else
803  return( 3 );
804 #endif
805  return( ret );
806 }
807 
808 int get_line( FILE *f, char *buf, size_t len )
809 {
810  char *ret;
811 
812  ret = fgets( buf, len, f );
813  if( ret == NULL )
814  return( -1 );
815 
816  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
817  buf[strlen(buf) - 1] = '\0';
818  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
819  buf[strlen(buf) - 1] = '\0';
820 
821  return( 0 );
822 }
823 
824 int parse_arguments( char *buf, size_t len, char *params[50] )
825 {
826  int cnt = 0, i;
827  char *cur = buf;
828  char *p = buf, *q;
829 
830  params[cnt++] = cur;
831 
832  while( *p != '\0' && p < buf + len )
833  {
834  if( *p == '\\' )
835  {
836  *p++;
837  *p++;
838  continue;
839  }
840  if( *p == ':' )
841  {
842  if( p + 1 < buf + len )
843  {
844  cur = p + 1;
845  params[cnt++] = cur;
846  }
847  *p = '\0';
848  }
849 
850  *p++;
851  }
852 
853  // Replace newlines, question marks and colons in strings
854  for( i = 0; i < cnt; i++ )
855  {
856  p = params[i];
857  q = params[i];
858 
859  while( *p != '\0' )
860  {
861  if( *p == '\\' && *(p + 1) == 'n' )
862  {
863  p += 2;
864  *(q++) = '\n';
865  }
866  else if( *p == '\\' && *(p + 1) == ':' )
867  {
868  p += 2;
869  *(q++) = ':';
870  }
871  else if( *p == '\\' && *(p + 1) == '?' )
872  {
873  p += 2;
874  *(q++) = '?';
875  }
876  else
877  *(q++) = *(p++);
878  }
879  *q = '\0';
880  }
881 
882  return( cnt );
883 }
884 
885 int main()
886 {
887  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
888  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_blowfish.data";
889  FILE *file;
890  char buf[5000];
891  char *params[50];
892 
893 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
894  unsigned char alloc_buf[1000000];
895  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
896 #endif
897 
898  file = fopen( filename, "r" );
899  if( file == NULL )
900  {
901  fprintf( stderr, "Failed to open\n" );
902  return( 1 );
903  }
904 
905  while( !feof( file ) )
906  {
907  int skip = 0;
908 
909  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
910  break;
911  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
912  fprintf( stdout, " " );
913  for( i = strlen( buf ) + 1; i < 67; i++ )
914  fprintf( stdout, "." );
915  fprintf( stdout, " " );
916  fflush( stdout );
917 
918  total_tests++;
919 
920  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
921  break;
922  cnt = parse_arguments( buf, strlen(buf), params );
923 
924  if( strcmp( params[0], "depends_on" ) == 0 )
925  {
926  for( i = 1; i < cnt; i++ )
927  if( dep_check( params[i] ) != 0 )
928  skip = 1;
929 
930  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
931  break;
932  cnt = parse_arguments( buf, strlen(buf), params );
933  }
934 
935  if( skip == 0 )
936  {
937  test_errors = 0;
938  ret = dispatch_test( cnt, params );
939  }
940 
941  if( skip == 1 || ret == 3 )
942  {
943  total_skipped++;
944  fprintf( stdout, "----\n" );
945  fflush( stdout );
946  }
947  else if( ret == 0 && test_errors == 0 )
948  {
949  fprintf( stdout, "PASS\n" );
950  fflush( stdout );
951  }
952  else if( ret == 2 )
953  {
954  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
955  fclose(file);
956  exit( 2 );
957  }
958  else
959  total_errors++;
960 
961  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
962  break;
963  if( strlen(buf) != 0 )
964  {
965  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
966  return( 1 );
967  }
968  }
969  fclose(file);
970 
971  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
972  if( total_errors == 0 )
973  fprintf( stdout, "PASSED" );
974  else
975  fprintf( stdout, "FAILED" );
976 
977  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
978  total_tests - total_errors, total_tests, total_skipped );
979 
980 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
981 #if defined(POLARSSL_MEMORY_DEBUG)
982  memory_buffer_alloc_status();
983 #endif
984  memory_buffer_alloc_free();
985 #endif
986 
987  return( total_errors != 0 );
988 }
989 
990 
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.
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
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.
int s
Definition: bignum.h:173
Configuration options (set of defines)
#define BLOWFISH_ENCRYPT
Definition: blowfish.h:41
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(char *str)
#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:42
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
Invalid key length.
Definition: blowfish.h:48
#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Invalid data input length.
Definition: blowfish.h:49
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
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 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:62
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)