3 #ifdef POLARSSL_BLOWFISH_C
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_BLOWFISH_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
364 if( strcmp( str,
"POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH" ) == 0 )
369 if( strcmp( str,
"POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH" ) == 0 )
376 printf(
"Expected integer for parameter and got: %s\n", str );
380 void test_suite_blowfish_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
381 char *hex_dst_string,
int setkey_result )
383 unsigned char key_str[100];
384 unsigned char src_str[100];
385 unsigned char dst_str[100];
386 unsigned char output[100];
390 memset(key_str, 0x00, 100);
391 memset(src_str, 0x00, 100);
392 memset(dst_str, 0x00, 100);
393 memset(output, 0x00, 100);
395 key_len =
unhexify( key_str, hex_key_string );
396 unhexify( src_str, hex_src_string );
399 if( setkey_result == 0 )
402 hexify( dst_str, output, 8 );
404 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
408 void test_suite_blowfish_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
409 char *hex_dst_string,
int setkey_result )
411 unsigned char key_str[100];
412 unsigned char src_str[100];
413 unsigned char dst_str[100];
414 unsigned char output[100];
418 memset(key_str, 0x00, 100);
419 memset(src_str, 0x00, 100);
420 memset(dst_str, 0x00, 100);
421 memset(output, 0x00, 100);
423 key_len =
unhexify( key_str, hex_key_string );
424 unhexify( src_str, hex_src_string );
427 if( setkey_result == 0 )
430 hexify( dst_str, output, 8 );
432 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
436 #ifdef POLARSSL_CIPHER_MODE_CBC
437 void test_suite_blowfish_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
438 char *hex_src_string,
char *hex_dst_string,
441 unsigned char key_str[100];
442 unsigned char iv_str[100];
443 unsigned char src_str[100];
444 unsigned char dst_str[100];
445 unsigned char output[100];
447 int key_len, data_len;
449 memset(key_str, 0x00, 100);
450 memset(iv_str, 0x00, 100);
451 memset(src_str, 0x00, 100);
452 memset(dst_str, 0x00, 100);
453 memset(output, 0x00, 100);
455 key_len =
unhexify( key_str, hex_key_string );
457 data_len =
unhexify( src_str, hex_src_string );
462 if( cbc_result == 0 )
464 hexify( dst_str, output, data_len );
466 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
471 #ifdef POLARSSL_CIPHER_MODE_CBC
472 void test_suite_blowfish_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
473 char *hex_src_string,
char *hex_dst_string,
476 unsigned char key_str[100];
477 unsigned char iv_str[100];
478 unsigned char src_str[100];
479 unsigned char dst_str[100];
480 unsigned char output[100];
482 int key_len, data_len;
484 memset(key_str, 0x00, 100);
485 memset(iv_str, 0x00, 100);
486 memset(src_str, 0x00, 100);
487 memset(dst_str, 0x00, 100);
488 memset(output, 0x00, 100);
490 key_len =
unhexify( key_str, hex_key_string );
492 data_len =
unhexify( src_str, hex_src_string );
498 hexify( dst_str, output, data_len );
500 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
505 void test_suite_blowfish_encrypt_cfb64(
char *hex_key_string,
char *hex_iv_string,
506 char *hex_src_string,
char *hex_dst_string )
508 unsigned char key_str[100];
509 unsigned char iv_str[100];
510 unsigned char src_str[100];
511 unsigned char dst_str[100];
512 unsigned char output[100];
514 size_t iv_offset = 0;
515 int key_len, src_len;
517 memset(key_str, 0x00, 100);
518 memset(iv_str, 0x00, 100);
519 memset(src_str, 0x00, 100);
520 memset(dst_str, 0x00, 100);
521 memset(output, 0x00, 100);
523 key_len =
unhexify( key_str, hex_key_string );
525 src_len =
unhexify( src_str, hex_src_string );
529 hexify( dst_str, output, src_len );
531 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
534 void test_suite_blowfish_decrypt_cfb64(
char *hex_key_string,
char *hex_iv_string,
535 char *hex_src_string,
char *hex_dst_string )
537 unsigned char key_str[100];
538 unsigned char iv_str[100];
539 unsigned char src_str[100];
540 unsigned char dst_str[100];
541 unsigned char output[100];
543 size_t iv_offset = 0;
544 int key_len, src_len;
546 memset(key_str, 0x00, 100);
547 memset(iv_str, 0x00, 100);
548 memset(src_str, 0x00, 100);
549 memset(dst_str, 0x00, 100);
550 memset(output, 0x00, 100);
552 key_len =
unhexify( key_str, hex_key_string );
554 src_len =
unhexify( src_str, hex_src_string );
558 hexify( dst_str, output, src_len );
560 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
563 void test_suite_blowfish_encrypt_ctr(
char *hex_key_string,
char *hex_iv_string,
564 char *hex_src_string,
char *hex_dst_string )
566 unsigned char key_str[100];
567 unsigned char iv_str[100];
568 unsigned char stream_str[100];
569 unsigned char src_str[100];
570 unsigned char dst_str[100];
571 unsigned char output[100];
573 size_t iv_offset = 0;
574 int key_len, src_len;
576 memset(key_str, 0x00, 100);
577 memset(iv_str, 0x00, 100);
578 memset(stream_str, 0x00, 100);
579 memset(src_str, 0x00, 100);
580 memset(dst_str, 0x00, 100);
581 memset(output, 0x00, 100);
583 key_len =
unhexify( key_str, hex_key_string );
585 src_len =
unhexify( src_str, hex_src_string );
589 hexify( dst_str, output, src_len );
591 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
614 #if defined(TEST_SUITE_ACTIVE)
615 if( strcmp( params[0],
"blowfish_encrypt_ecb" ) == 0 )
618 char *param1 = params[1];
619 char *param2 = params[2];
620 char *param3 = params[3];
625 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
632 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
634 test_suite_blowfish_encrypt_ecb( param1, param2, param3, param4 );
640 if( strcmp( params[0],
"blowfish_decrypt_ecb" ) == 0 )
643 char *param1 = params[1];
644 char *param2 = params[2];
645 char *param3 = params[3];
650 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
657 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
659 test_suite_blowfish_decrypt_ecb( param1, param2, param3, param4 );
665 if( strcmp( params[0],
"blowfish_encrypt_cbc" ) == 0 )
667 #ifdef POLARSSL_CIPHER_MODE_CBC
669 char *param1 = params[1];
670 char *param2 = params[2];
671 char *param3 = params[3];
672 char *param4 = params[4];
677 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
685 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
687 test_suite_blowfish_encrypt_cbc( param1, param2, param3, param4, param5 );
694 if( strcmp( params[0],
"blowfish_decrypt_cbc" ) == 0 )
696 #ifdef POLARSSL_CIPHER_MODE_CBC
698 char *param1 = params[1];
699 char *param2 = params[2];
700 char *param3 = params[3];
701 char *param4 = params[4];
706 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
714 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
716 test_suite_blowfish_decrypt_cbc( param1, param2, param3, param4, param5 );
723 if( strcmp( params[0],
"blowfish_encrypt_cfb64" ) == 0 )
726 char *param1 = params[1];
727 char *param2 = params[2];
728 char *param3 = params[3];
729 char *param4 = params[4];
733 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
742 test_suite_blowfish_encrypt_cfb64( param1, param2, param3, param4 );
748 if( strcmp( params[0],
"blowfish_decrypt_cfb64" ) == 0 )
751 char *param1 = params[1];
752 char *param2 = params[2];
753 char *param3 = params[3];
754 char *param4 = params[4];
758 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
767 test_suite_blowfish_decrypt_cfb64( param1, param2, param3, param4 );
773 if( strcmp( params[0],
"blowfish_encrypt_ctr" ) == 0 )
776 char *param1 = params[1];
777 char *param2 = params[2];
778 char *param3 = params[3];
779 char *param4 = params[4];
783 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
792 test_suite_blowfish_encrypt_ctr( param1, param2, param3, param4 );
800 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
814 ret = fgets( buf, len, f );
818 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
819 buf[strlen(buf) - 1] =
'\0';
820 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
821 buf[strlen(buf) - 1] =
'\0';
834 while( *p !=
'\0' && p < buf + len )
844 if( p + 1 < buf + len )
856 for( i = 0; i < cnt; i++ )
863 if( *p ==
'\\' && *(p + 1) ==
'n' )
868 else if( *p ==
'\\' && *(p + 1) ==
':' )
873 else if( *p ==
'\\' && *(p + 1) ==
'?' )
889 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
890 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_blowfish.data";
895 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
896 unsigned char alloc_buf[1000000];
897 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
900 file = fopen( filename,
"r" );
903 fprintf( stderr,
"Failed to open\n" );
907 while( !feof( file ) )
911 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
913 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
914 fprintf( stdout,
" " );
915 for( i = strlen( buf ) + 1; i < 67; i++ )
916 fprintf( stdout,
"." );
917 fprintf( stdout,
" " );
922 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
926 if( strcmp( params[0],
"depends_on" ) == 0 )
928 for( i = 1; i < cnt; i++ )
932 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
943 if( skip == 1 || ret == 3 )
946 fprintf( stdout,
"----\n" );
951 fprintf( stdout,
"PASS\n" );
956 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
963 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
965 if( strlen(buf) != 0 )
967 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
973 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
974 if( total_errors == 0 )
975 fprintf( stdout,
"PASSED" );
977 fprintf( stdout,
"FAILED" );
979 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
980 total_tests - total_errors, total_tests, total_skipped );
982 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
983 #if defined(POLARSSL_MEMORY_DEBUG)
984 memory_buffer_alloc_status();
986 memory_buffer_alloc_free();
989 return( total_errors != 0 );
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.
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.
Configuration options (set of defines)
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
#define TEST_ASSERT(TEST)
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)
int verify_string(char **str)
#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
Invalid key length.
#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Invalid data input length.
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.
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 not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
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.
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)