PolarSSL v1.3.1
test_suite_cipher.blowfish.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_CIPHER_C
4 
5 #include <polarssl/cipher.h>
6 
7 #if defined(POLARSSL_GCM_C)
8 #include <polarssl/gcm.h>
9 #endif
10 #endif /* POLARSSL_CIPHER_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #ifdef _MSC_VER
18 #include <basetsd.h>
19 typedef UINT32 uint32_t;
20 #else
21 #include <inttypes.h>
22 #endif
23 
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 /*
29  * 32-bit integer manipulation macros (big endian)
30  */
31 #ifndef GET_UINT32_BE
32 #define GET_UINT32_BE(n,b,i) \
33 { \
34  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
35  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
36  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
37  | ( (uint32_t) (b)[(i) + 3] ); \
38 }
39 #endif
40 
41 #ifndef PUT_UINT32_BE
42 #define PUT_UINT32_BE(n,b,i) \
43 { \
44  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
45  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
46  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
47  (b)[(i) + 3] = (unsigned char) ( (n) ); \
48 }
49 #endif
50 
51 static int unhexify(unsigned char *obuf, const char *ibuf)
52 {
53  unsigned char c, c2;
54  int len = strlen(ibuf) / 2;
55  assert(!(strlen(ibuf) %1)); // must be even number of bytes
56 
57  while (*ibuf != 0)
58  {
59  c = *ibuf++;
60  if( c >= '0' && c <= '9' )
61  c -= '0';
62  else if( c >= 'a' && c <= 'f' )
63  c -= 'a' - 10;
64  else if( c >= 'A' && c <= 'F' )
65  c -= 'A' - 10;
66  else
67  assert( 0 );
68 
69  c2 = *ibuf++;
70  if( c2 >= '0' && c2 <= '9' )
71  c2 -= '0';
72  else if( c2 >= 'a' && c2 <= 'f' )
73  c2 -= 'a' - 10;
74  else if( c2 >= 'A' && c2 <= 'F' )
75  c2 -= 'A' - 10;
76  else
77  assert( 0 );
78 
79  *obuf++ = ( c << 4 ) | c2;
80  }
81 
82  return len;
83 }
84 
85 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
86 {
87  unsigned char l, h;
88 
89  while (len != 0)
90  {
91  h = (*ibuf) / 16;
92  l = (*ibuf) % 16;
93 
94  if( h < 10 )
95  *obuf++ = '0' + h;
96  else
97  *obuf++ = 'a' + h - 10;
98 
99  if( l < 10 )
100  *obuf++ = '0' + l;
101  else
102  *obuf++ = 'a' + l - 10;
103 
104  ++ibuf;
105  len--;
106  }
107 }
108 
118 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
119 {
120  size_t i;
121 
122  if( rng_state != NULL )
123  rng_state = NULL;
124 
125  for( i = 0; i < len; ++i )
126  output[i] = rand();
127 
128  return( 0 );
129 }
130 
136 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
137 {
138  if( rng_state != NULL )
139  rng_state = NULL;
140 
141  memset( output, 0, len );
142 
143  return( 0 );
144 }
145 
146 typedef struct
147 {
148  unsigned char *buf;
149  size_t length;
150 } rnd_buf_info;
151 
163 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
164 {
165  rnd_buf_info *info = (rnd_buf_info *) rng_state;
166  size_t use_len;
167 
168  if( rng_state == NULL )
169  return( rnd_std_rand( NULL, output, len ) );
170 
171  use_len = len;
172  if( len > info->length )
173  use_len = info->length;
174 
175  if( use_len )
176  {
177  memcpy( output, info->buf, use_len );
178  info->buf += use_len;
179  info->length -= use_len;
180  }
181 
182  if( len - use_len > 0 )
183  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
184 
185  return( 0 );
186 }
187 
195 typedef struct
196 {
197  uint32_t key[16];
198  uint32_t v0, v1;
200 
209 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
210 {
211  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
212  uint32_t i, *k, sum, delta=0x9E3779B9;
213  unsigned char result[4];
214 
215  if( rng_state == NULL )
216  return( rnd_std_rand( NULL, output, len ) );
217 
218  k = info->key;
219 
220  while( len > 0 )
221  {
222  size_t use_len = ( len > 4 ) ? 4 : len;
223  sum = 0;
224 
225  for( i = 0; i < 32; i++ )
226  {
227  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
228  sum += delta;
229  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
230  }
231 
232  PUT_UINT32_BE( info->v0, result, 0 );
233  memcpy( output, result, use_len );
234  len -= use_len;
235  }
236 
237  return( 0 );
238 }
239 
249 static int not_rnd( void *in, unsigned char *out, size_t len )
250 {
251  unsigned char *obuf;
252  const char *ibuf = in;
253  unsigned char c, c2;
254  assert( len == strlen(ibuf) / 2 );
255  assert(!(strlen(ibuf) %1)); // must be even number of bytes
256 
257  obuf = out + (len - 1); // sic
258  while (*ibuf != 0)
259  {
260  c = *ibuf++;
261  if( c >= '0' && c <= '9' )
262  c -= '0';
263  else if( c >= 'a' && c <= 'f' )
264  c -= 'a' - 10;
265  else if( c >= 'A' && c <= 'F' )
266  c -= 'A' - 10;
267  else
268  assert( 0 );
269 
270  c2 = *ibuf++;
271  if( c2 >= '0' && c2 <= '9' )
272  c2 -= '0';
273  else if( c2 >= 'a' && c2 <= 'f' )
274  c2 -= 'a' - 10;
275  else if( c2 >= 'A' && c2 <= 'F' )
276  c2 -= 'A' - 10;
277  else
278  assert( 0 );
279 
280  *obuf-- = ( c << 4 ) | c2; // sic
281  }
282 
283  return( 0 );
284 }
285 
286 
287 #include <stdio.h>
288 #include <string.h>
289 
290 static int test_errors = 0;
291 
292 #ifdef POLARSSL_CIPHER_C
293 
294 #define TEST_SUITE_ACTIVE
295 
296 static int test_assert( int correct, char *test )
297 {
298  if( correct )
299  return( 0 );
300 
301  test_errors++;
302  if( test_errors == 1 )
303  printf( "FAILED\n" );
304  printf( " %s\n", test );
305 
306  return( 1 );
307 }
308 
309 #define TEST_ASSERT( TEST ) \
310  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
311  if( test_errors) return; \
312  } while (0)
313 
314 int verify_string( char **str )
315 {
316  if( (*str)[0] != '"' ||
317  (*str)[strlen( *str ) - 1] != '"' )
318  {
319  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
320  return( -1 );
321  }
322 
323  (*str)++;
324  (*str)[strlen( *str ) - 1] = '\0';
325 
326  return( 0 );
327 }
328 
329 int verify_int( char *str, int *value )
330 {
331  size_t i;
332  int minus = 0;
333  int digits = 1;
334  int hex = 0;
335 
336  for( i = 0; i < strlen( str ); i++ )
337  {
338  if( i == 0 && str[i] == '-' )
339  {
340  minus = 1;
341  continue;
342  }
343 
344  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
345  str[i - 1] == '0' && str[i] == 'x' )
346  {
347  hex = 1;
348  continue;
349  }
350 
351  if( str[i] < '0' || str[i] > '9' )
352  {
353  digits = 0;
354  break;
355  }
356  }
357 
358  if( digits )
359  {
360  if( hex )
361  *value = strtol( str, NULL, 16 );
362  else
363  *value = strtol( str, NULL, 10 );
364 
365  return( 0 );
366  }
367 
368  if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
369  {
370  *value = ( POLARSSL_PADDING_ZEROS );
371  return( 0 );
372  }
373  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
374  {
375  *value = ( POLARSSL_CIPHER_BLOWFISH_CBC );
376  return( 0 );
377  }
378  if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
379  {
380  *value = ( POLARSSL_PADDING_NONE );
381  return( 0 );
382  }
383  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
384  {
385  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
386  return( 0 );
387  }
388  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
389  {
390  *value = ( POLARSSL_CIPHER_BLOWFISH_CFB64 );
391  return( 0 );
392  }
393  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
394  {
395  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
396  return( 0 );
397  }
398  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
399  {
400  *value = ( POLARSSL_CIPHER_BLOWFISH_CTR );
401  return( 0 );
402  }
403  if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
404  {
406  return( 0 );
407  }
408  if( strcmp( str, "-1" ) == 0 )
409  {
410  *value = ( -1 );
411  return( 0 );
412  }
413 
414 
415  printf( "Expected integer for parameter and got: %s\n", str );
416  return( -1 );
417 }
418 
419 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
420  int length_val, int pad_mode )
421 {
422  size_t length = length_val, outlen, total_len, i;
423  unsigned char key[32];
424  unsigned char iv[16];
425  unsigned char ad[13];
426  unsigned char tag[16];
427  unsigned char inbuf[64];
428  unsigned char encbuf[64];
429  unsigned char decbuf[64];
430 
431  const cipher_info_t *cipher_info;
432  cipher_context_t ctx_dec;
433  cipher_context_t ctx_enc;
434 
435  /*
436  * Prepare contexts
437  */
438  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
439  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
440 
441  memset( key, 0x2a, sizeof( key ) );
442 
443  /* Check and get info structures */
444  cipher_info = cipher_info_from_type( cipher_id );
445  TEST_ASSERT( NULL != cipher_info );
446  TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
447 
448  /* Initialise enc and dec contexts */
449  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
450  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
451 
452  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
453  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
454 
455 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
456  if( -1 != pad_mode )
457  {
458  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
459  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
460  }
461 #else
462  (void) pad_mode;
463 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
464 
465  /*
466  * Do a few encode/decode cycles
467  */
468  for( i = 0; i < 3; i++ )
469  {
470  memset( iv , 0x00 + i, sizeof( iv ) );
471  memset( ad, 0x10 + i, sizeof( ad ) );
472  memset( inbuf, 0x20 + i, sizeof( inbuf ) );
473 
474  memset( encbuf, 0, sizeof( encbuf ) );
475  memset( decbuf, 0, sizeof( decbuf ) );
476  memset( tag, 0, sizeof( tag ) );
477 
478  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
479  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
480 
481  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
482  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
483 
484 #if defined(POLARSSL_CIPHER_MODE_AEAD)
485  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
486  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
487 #endif /* POLARSSL_CIPHER_MODE_AEAD */
488 
489  /* encode length number of bytes from inbuf */
490  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
491  total_len = outlen;
492 
493  TEST_ASSERT( total_len == length ||
494  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
495  total_len < length &&
496  total_len + cipher_get_block_size( &ctx_enc ) > length ) );
497 
498  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
499  total_len += outlen;
500 
501 #if defined(POLARSSL_CIPHER_MODE_AEAD)
502  TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
503 #endif /* POLARSSL_CIPHER_MODE_AEAD */
504 
505  TEST_ASSERT( total_len == length ||
506  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
507  total_len > length &&
508  total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
509 
510  /* decode the previously encoded string */
511  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
512  total_len = outlen;
513 
514  TEST_ASSERT( total_len == length ||
515  ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
516  total_len < length &&
517  total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
518 
519  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
520  total_len += outlen;
521 
522 #if defined(POLARSSL_CIPHER_MODE_AEAD)
523  TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
524 #endif /* POLARSSL_CIPHER_MODE_AEAD */
525 
526  /* check result */
527  TEST_ASSERT( total_len == length );
528  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
529  }
530 
531  /*
532  * Done
533  */
534  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
535  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
536 }
537 
538 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
539  int length_val, int ret )
540 {
541  size_t length = length_val;
542  unsigned char key[32];
543  unsigned char iv[16];
544 
545  const cipher_info_t *cipher_info;
546  cipher_context_t ctx;
547 
548  unsigned char inbuf[64];
549  unsigned char encbuf[64];
550 
551  size_t outlen = 0;
552 
553  memset( key, 0, 32 );
554  memset( iv , 0, 16 );
555 
556  memset( &ctx, 0, sizeof( ctx ) );
557 
558  memset( inbuf, 5, 64 );
559  memset( encbuf, 0, 64 );
560 
561  /* Check and get info structures */
562  cipher_info = cipher_info_from_type( cipher_id );
563  TEST_ASSERT( NULL != cipher_info );
564 
565  /* Initialise context */
566  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
567  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
568 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
569  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
570 #else
571  (void) pad_mode;
572 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
573  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
574  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
575 #if defined(POLARSSL_CIPHER_MODE_AEAD)
576  TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
577 #endif /* POLARSSL_CIPHER_MODE_AEAD */
578 
579  /* encode length number of bytes from inbuf */
580  TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
581  TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
582 
583  /* done */
584  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
585 }
586 
587 void test_suite_dec_empty_buf()
588 {
589  unsigned char key[32];
590  unsigned char iv[16];
591 
592  cipher_context_t ctx_dec;
593  const cipher_info_t *cipher_info;
594 
595  unsigned char encbuf[64];
596  unsigned char decbuf[64];
597 
598  size_t outlen = 0;
599 
600  memset( key, 0, 32 );
601  memset( iv , 0, 16 );
602 
603  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
604 
605  memset( encbuf, 0, 64 );
606  memset( decbuf, 0, 64 );
607 
608  /* Initialise context */
610  TEST_ASSERT( NULL != cipher_info);
611 
612  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
613 
614  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
615 
616  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
617 
618  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
619 
620 #if defined(POLARSSL_CIPHER_MODE_AEAD)
621  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
622 #endif /* POLARSSL_CIPHER_MODE_AEAD */
623 
624  /* decode 0-byte string */
625  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
626  TEST_ASSERT( 0 == outlen );
628  &ctx_dec, decbuf + outlen, &outlen ) );
629  TEST_ASSERT( 0 == outlen );
630 
631  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
632 }
633 
634 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
635  int second_length_val )
636 {
637  size_t first_length = first_length_val;
638  size_t second_length = second_length_val;
639  size_t length = first_length + second_length;
640  unsigned char key[32];
641  unsigned char iv[16];
642 
643  cipher_context_t ctx_dec;
644  cipher_context_t ctx_enc;
645  const cipher_info_t *cipher_info;
646 
647  unsigned char inbuf[64];
648  unsigned char encbuf[64];
649  unsigned char decbuf[64];
650 
651  size_t outlen = 0;
652  size_t totaloutlen = 0;
653 
654  memset( key, 0, 32 );
655  memset( iv , 0, 16 );
656 
657  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
658  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
659 
660  memset( inbuf, 5, 64 );
661  memset( encbuf, 0, 64 );
662  memset( decbuf, 0, 64 );
663 
664  /* Initialise enc and dec contexts */
665  cipher_info = cipher_info_from_type( cipher_id );
666  TEST_ASSERT( NULL != cipher_info);
667 
668  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
669  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
670 
671  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
672  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
673 
674  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
675  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
676 
677  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
678  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
679 
680 #if defined(POLARSSL_CIPHER_MODE_AEAD)
681  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
682  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
683 #endif /* POLARSSL_CIPHER_MODE_AEAD */
684 
685  /* encode length number of bytes from inbuf */
686  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
687  totaloutlen = outlen;
688  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
689  totaloutlen += outlen;
690  TEST_ASSERT( totaloutlen == length ||
691  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
692  totaloutlen < length &&
693  totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
694 
695  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
696  totaloutlen += outlen;
697  TEST_ASSERT( totaloutlen == length ||
698  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
699  totaloutlen > length &&
700  totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
701 
702  /* decode the previously encoded string */
703  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
704  totaloutlen = outlen;
705 
706  TEST_ASSERT( totaloutlen == length ||
707  ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
708  totaloutlen < length &&
709  totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
710 
711  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
712  totaloutlen += outlen;
713 
714  TEST_ASSERT( totaloutlen == length );
715 
716  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
717 
718  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
719  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
720 }
721 
722 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
723  char *hex_key, char *hex_iv,
724  char *hex_cipher, char *hex_clear,
725  char *hex_ad, char *hex_tag,
726  int finish_result, int tag_result )
727 {
728  unsigned char key[50];
729  unsigned char iv[50];
730  unsigned char cipher[200];
731  unsigned char clear[200];
732  unsigned char ad[200];
733  unsigned char tag[20];
734  size_t key_len, iv_len, cipher_len, clear_len;
735 #if defined(POLARSSL_CIPHER_MODE_AEAD)
736  size_t ad_len, tag_len;
737 #endif
738  cipher_context_t ctx;
739  unsigned char output[200];
740  size_t outlen, total_len;
741 
742  memset( key, 0x00, sizeof( key ) );
743  memset( iv, 0x00, sizeof( iv ) );
744  memset( cipher, 0x00, sizeof( cipher ) );
745  memset( clear, 0x00, sizeof( clear ) );
746  memset( ad, 0x00, sizeof( ad ) );
747  memset( tag, 0x00, sizeof( tag ) );
748  memset( output, 0x00, sizeof( output ) );
749 
750  key_len = unhexify( key, hex_key );
751  iv_len = unhexify( iv, hex_iv );
752  cipher_len = unhexify( cipher, hex_cipher );
753  clear_len = unhexify( clear, hex_clear );
754 #if defined(POLARSSL_CIPHER_MODE_AEAD)
755  ad_len = unhexify( ad, hex_ad );
756  tag_len = unhexify( tag, hex_tag );
757 #else
758  ((void) hex_ad);
759  ((void) hex_tag);
760 #endif
761 
762  /* Prepare context */
763  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
764  cipher_info_from_type( cipher_id ) ) );
765  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
766 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
767  if( pad_mode != -1 )
768  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
769 #else
770  (void) pad_mode;
771 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
772  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
773  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
774 #if defined(POLARSSL_CIPHER_MODE_AEAD)
775  TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
776 #endif /* POLARSSL_CIPHER_MODE_AEAD */
777 
778  /* decode buffer and check tag */
779  total_len = 0;
780  TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
781  total_len += outlen;
782  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
783  &outlen ) );
784  total_len += outlen;
785 #if defined(POLARSSL_CIPHER_MODE_AEAD)
786  TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
787 #endif /* POLARSSL_CIPHER_MODE_AEAD */
788 
789  /* check plaintext only if everything went fine */
790  if( 0 == finish_result && 0 == tag_result )
791  {
792  TEST_ASSERT( total_len == clear_len );
793  TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
794  }
795 
796  cipher_free_ctx( &ctx );
797 }
798 
799 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
800  char *hex_input, char *hex_result,
801  int finish_result )
802 {
803  unsigned char key[50];
804  unsigned char input[16];
805  unsigned char result[16];
806  size_t key_len;
807  cipher_context_t ctx;
808  unsigned char output[32];
809  size_t outlen;
810 
811  memset( key, 0x00, sizeof( key ) );
812  memset( input, 0x00, sizeof( input ) );
813  memset( result, 0x00, sizeof( result ) );
814  memset( output, 0x00, sizeof( output ) );
815 
816  /* Prepare context */
817  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
818  cipher_info_from_type( cipher_id ) ) );
819 
820  key_len = unhexify( key, hex_key );
821  TEST_ASSERT( unhexify( input, hex_input ) ==
822  (int) cipher_get_block_size( &ctx ) );
823  TEST_ASSERT( unhexify( result, hex_result ) ==
824  (int) cipher_get_block_size( &ctx ) );
825 
826  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
827 
828  TEST_ASSERT( 0 == cipher_update( &ctx, input,
829  cipher_get_block_size( &ctx ),
830  output, &outlen ) );
831  TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
832  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
833  &outlen ) );
834  TEST_ASSERT( 0 == outlen );
835 
836  /* check plaintext only if everything went fine */
837  if( 0 == finish_result )
838  TEST_ASSERT( 0 == memcmp( output, result,
839  cipher_get_block_size( &ctx ) ) );
840 
841  cipher_free_ctx( &ctx );
842 }
843 
844 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
845 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
846 {
847  const cipher_info_t *cipher_info;
848  cipher_context_t ctx;
849 
850  cipher_info = cipher_info_from_type( cipher_id );
851  TEST_ASSERT( NULL != cipher_info );
852  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
853 
854  TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
855 
856  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
857 }
858 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
859 
860 #ifdef POLARSSL_CIPHER_MODE_CBC
861 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
862 {
863  cipher_info_t cipher_info;
864  cipher_context_t ctx;
865  unsigned char input[16];
866  size_t ilen, dlen;
867 
868  /* build a fake context just for getting access to get_padding */
869  memset( &ctx, 0, sizeof( ctx ) );
870  cipher_info.mode = POLARSSL_MODE_CBC;
871  ctx.cipher_info = &cipher_info;
872 
873  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
874 
875  ilen = unhexify( input, input_str );
876 
877  TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
878  if( 0 == ret )
879  TEST_ASSERT( dlen == (size_t) dlen_check );
880 }
881 #endif /* POLARSSL_CIPHER_MODE_CBC */
882 
883 #ifdef POLARSSL_SELF_TEST
884 void test_suite_cipher_selftest()
885 {
886  TEST_ASSERT( cipher_self_test( 0 ) == 0 );
887 }
888 #endif /* POLARSSL_SELF_TEST */
889 
890 
891 #endif /* POLARSSL_CIPHER_C */
892 
893 
894 int dep_check( char *str )
895 {
896  if( str == NULL )
897  return( 1 );
898 
899  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
900  {
901 #if defined(POLARSSL_CIPHER_MODE_CFB)
902  return( 0 );
903 #else
904  return( 1 );
905 #endif
906  }
907  if( strcmp( str, "POLARSSL_BLOWFISH_C" ) == 0 )
908  {
909 #if defined(POLARSSL_BLOWFISH_C)
910  return( 0 );
911 #else
912  return( 1 );
913 #endif
914  }
915  if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
916  {
917 #if defined(POLARSSL_CIPHER_MODE_CTR)
918  return( 0 );
919 #else
920  return( 1 );
921 #endif
922  }
923  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
924  {
925 #if defined(POLARSSL_CIPHER_MODE_CBC)
926  return( 0 );
927 #else
928  return( 1 );
929 #endif
930  }
931  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
932  {
933 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
934  return( 0 );
935 #else
936  return( 1 );
937 #endif
938  }
939 
940 
941  return( 1 );
942 }
943 
944 int dispatch_test(int cnt, char *params[50])
945 {
946  int ret;
947  ((void) cnt);
948  ((void) params);
949 
950 #if defined(TEST_SUITE_ACTIVE)
951  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
952  {
953 
954  int param1;
955  char *param2 = params[2];
956  int param3;
957  int param4;
958  int param5;
959 
960  if( cnt != 6 )
961  {
962  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
963  return( 2 );
964  }
965 
966  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
967  if( verify_string( &param2 ) != 0 ) return( 2 );
968  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
969  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
970  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
971 
972  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
973  return ( 0 );
974 
975  return ( 3 );
976  }
977  else
978  if( strcmp( params[0], "enc_fail" ) == 0 )
979  {
980 
981  int param1;
982  int param2;
983  int param3;
984  int param4;
985  int param5;
986 
987  if( cnt != 6 )
988  {
989  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
990  return( 2 );
991  }
992 
993  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
994  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
995  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
996  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
997  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
998 
999  test_suite_enc_fail( param1, param2, param3, param4, param5 );
1000  return ( 0 );
1001 
1002  return ( 3 );
1003  }
1004  else
1005  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1006  {
1007 
1008 
1009  if( cnt != 1 )
1010  {
1011  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1012  return( 2 );
1013  }
1014 
1015 
1016  test_suite_dec_empty_buf( );
1017  return ( 0 );
1018 
1019  return ( 3 );
1020  }
1021  else
1022  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1023  {
1024 
1025  int param1;
1026  int param2;
1027  int param3;
1028  int param4;
1029 
1030  if( cnt != 5 )
1031  {
1032  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1033  return( 2 );
1034  }
1035 
1036  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1037  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1038  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1039  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1040 
1041  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1042  return ( 0 );
1043 
1044  return ( 3 );
1045  }
1046  else
1047  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1048  {
1049 
1050  int param1;
1051  int param2;
1052  char *param3 = params[3];
1053  char *param4 = params[4];
1054  char *param5 = params[5];
1055  char *param6 = params[6];
1056  char *param7 = params[7];
1057  char *param8 = params[8];
1058  int param9;
1059  int param10;
1060 
1061  if( cnt != 11 )
1062  {
1063  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1064  return( 2 );
1065  }
1066 
1067  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1068  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1069  if( verify_string( &param3 ) != 0 ) return( 2 );
1070  if( verify_string( &param4 ) != 0 ) return( 2 );
1071  if( verify_string( &param5 ) != 0 ) return( 2 );
1072  if( verify_string( &param6 ) != 0 ) return( 2 );
1073  if( verify_string( &param7 ) != 0 ) return( 2 );
1074  if( verify_string( &param8 ) != 0 ) return( 2 );
1075  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1076  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1077 
1078  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1079  return ( 0 );
1080 
1081  return ( 3 );
1082  }
1083  else
1084  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1085  {
1086 
1087  int param1;
1088  int param2;
1089  char *param3 = params[3];
1090  char *param4 = params[4];
1091  char *param5 = params[5];
1092  int param6;
1093 
1094  if( cnt != 7 )
1095  {
1096  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1097  return( 2 );
1098  }
1099 
1100  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1101  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1102  if( verify_string( &param3 ) != 0 ) return( 2 );
1103  if( verify_string( &param4 ) != 0 ) return( 2 );
1104  if( verify_string( &param5 ) != 0 ) return( 2 );
1105  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1106 
1107  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1108  return ( 0 );
1109 
1110  return ( 3 );
1111  }
1112  else
1113  if( strcmp( params[0], "set_padding" ) == 0 )
1114  {
1115  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1116 
1117  int param1;
1118  int param2;
1119  int param3;
1120 
1121  if( cnt != 4 )
1122  {
1123  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1124  return( 2 );
1125  }
1126 
1127  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1128  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1129  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1130 
1131  test_suite_set_padding( param1, param2, param3 );
1132  return ( 0 );
1133  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1134 
1135  return ( 3 );
1136  }
1137  else
1138  if( strcmp( params[0], "check_padding" ) == 0 )
1139  {
1140  #ifdef POLARSSL_CIPHER_MODE_CBC
1141 
1142  int param1;
1143  char *param2 = params[2];
1144  int param3;
1145  int param4;
1146 
1147  if( cnt != 5 )
1148  {
1149  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1150  return( 2 );
1151  }
1152 
1153  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1154  if( verify_string( &param2 ) != 0 ) return( 2 );
1155  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1156  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1157 
1158  test_suite_check_padding( param1, param2, param3, param4 );
1159  return ( 0 );
1160  #endif /* POLARSSL_CIPHER_MODE_CBC */
1161 
1162  return ( 3 );
1163  }
1164  else
1165  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1166  {
1167  #ifdef POLARSSL_SELF_TEST
1168 
1169 
1170  if( cnt != 1 )
1171  {
1172  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1173  return( 2 );
1174  }
1175 
1176 
1177  test_suite_cipher_selftest( );
1178  return ( 0 );
1179  #endif /* POLARSSL_SELF_TEST */
1180 
1181  return ( 3 );
1182  }
1183  else
1184 
1185  {
1186  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1187  fflush( stdout );
1188  return( 1 );
1189  }
1190 #else
1191  return( 3 );
1192 #endif
1193  return( ret );
1194 }
1195 
1196 int get_line( FILE *f, char *buf, size_t len )
1197 {
1198  char *ret;
1199 
1200  ret = fgets( buf, len, f );
1201  if( ret == NULL )
1202  return( -1 );
1203 
1204  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1205  buf[strlen(buf) - 1] = '\0';
1206  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1207  buf[strlen(buf) - 1] = '\0';
1208 
1209  return( 0 );
1210 }
1211 
1212 int parse_arguments( char *buf, size_t len, char *params[50] )
1213 {
1214  int cnt = 0, i;
1215  char *cur = buf;
1216  char *p = buf, *q;
1217 
1218  params[cnt++] = cur;
1219 
1220  while( *p != '\0' && p < buf + len )
1221  {
1222  if( *p == '\\' )
1223  {
1224  *p++;
1225  *p++;
1226  continue;
1227  }
1228  if( *p == ':' )
1229  {
1230  if( p + 1 < buf + len )
1231  {
1232  cur = p + 1;
1233  params[cnt++] = cur;
1234  }
1235  *p = '\0';
1236  }
1237 
1238  *p++;
1239  }
1240 
1241  // Replace newlines, question marks and colons in strings
1242  for( i = 0; i < cnt; i++ )
1243  {
1244  p = params[i];
1245  q = params[i];
1246 
1247  while( *p != '\0' )
1248  {
1249  if( *p == '\\' && *(p + 1) == 'n' )
1250  {
1251  p += 2;
1252  *(q++) = '\n';
1253  }
1254  else if( *p == '\\' && *(p + 1) == ':' )
1255  {
1256  p += 2;
1257  *(q++) = ':';
1258  }
1259  else if( *p == '\\' && *(p + 1) == '?' )
1260  {
1261  p += 2;
1262  *(q++) = '?';
1263  }
1264  else
1265  *(q++) = *(p++);
1266  }
1267  *q = '\0';
1268  }
1269 
1270  return( cnt );
1271 }
1272 
1273 int main()
1274 {
1275  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1276  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.blowfish.data";
1277  FILE *file;
1278  char buf[5000];
1279  char *params[50];
1280 
1281 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1282  unsigned char alloc_buf[1000000];
1283  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1284 #endif
1285 
1286  file = fopen( filename, "r" );
1287  if( file == NULL )
1288  {
1289  fprintf( stderr, "Failed to open\n" );
1290  return( 1 );
1291  }
1292 
1293  while( !feof( file ) )
1294  {
1295  int skip = 0;
1296 
1297  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1298  break;
1299  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1300  fprintf( stdout, " " );
1301  for( i = strlen( buf ) + 1; i < 67; i++ )
1302  fprintf( stdout, "." );
1303  fprintf( stdout, " " );
1304  fflush( stdout );
1305 
1306  total_tests++;
1307 
1308  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1309  break;
1310  cnt = parse_arguments( buf, strlen(buf), params );
1311 
1312  if( strcmp( params[0], "depends_on" ) == 0 )
1313  {
1314  for( i = 1; i < cnt; i++ )
1315  if( dep_check( params[i] ) != 0 )
1316  skip = 1;
1317 
1318  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1319  break;
1320  cnt = parse_arguments( buf, strlen(buf), params );
1321  }
1322 
1323  if( skip == 0 )
1324  {
1325  test_errors = 0;
1326  ret = dispatch_test( cnt, params );
1327  }
1328 
1329  if( skip == 1 || ret == 3 )
1330  {
1331  total_skipped++;
1332  fprintf( stdout, "----\n" );
1333  fflush( stdout );
1334  }
1335  else if( ret == 0 && test_errors == 0 )
1336  {
1337  fprintf( stdout, "PASS\n" );
1338  fflush( stdout );
1339  }
1340  else if( ret == 2 )
1341  {
1342  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1343  fclose(file);
1344  exit( 2 );
1345  }
1346  else
1347  total_errors++;
1348 
1349  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1350  break;
1351  if( strlen(buf) != 0 )
1352  {
1353  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1354  return( 1 );
1355  }
1356  }
1357  fclose(file);
1358 
1359  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1360  if( total_errors == 0 )
1361  fprintf( stdout, "PASSED" );
1362  else
1363  fprintf( stdout, "FAILED" );
1364 
1365  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1366  total_tests - total_errors, total_tests, total_skipped );
1367 
1368 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1369 #if defined(POLARSSL_MEMORY_DEBUG)
1370  memory_buffer_alloc_status();
1371 #endif
1372  memory_buffer_alloc_free();
1373 #endif
1374 
1375  return( total_errors != 0 );
1376 }
1377 
1378