1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_BLOWFISH_C
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(POLARSSL_PLATFORM_C)
20 #define polarssl_malloc malloc
21 #define polarssl_free free
26 typedef UINT32 uint32_t;
39 #define GET_UINT32_BE(n,b,i) \
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] ); \
49 #define PUT_UINT32_BE(n,b,i) \
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) ); \
58 static int unhexify(
unsigned char *obuf,
const char *ibuf)
61 int len = strlen(ibuf) / 2;
62 assert(!(strlen(ibuf) %1));
67 if( c >=
'0' && c <=
'9' )
69 else if( c >=
'a' && c <=
'f' )
71 else if( c >=
'A' && c <=
'F' )
77 if( c2 >=
'0' && c2 <=
'9' )
79 else if( c2 >=
'a' && c2 <=
'f' )
81 else if( c2 >=
'A' && c2 <=
'F' )
86 *obuf++ = ( c << 4 ) | c2;
92 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
104 *obuf++ =
'a' + h - 10;
109 *obuf++ =
'a' + l - 10;
126 size_t actual_len = len != 0 ? len : 1;
131 memset( p, 0x00, actual_len );
150 *olen = strlen(ibuf) / 2;
156 assert( obuf != NULL );
172 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
174 #if !defined(__OpenBSD__)
177 if( rng_state != NULL )
180 for( i = 0; i < len; ++i )
183 if( rng_state != NULL )
186 arc4random_buf( output, len );
197 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
199 if( rng_state != NULL )
202 memset( output, 0, len );
229 if( rng_state == NULL )
238 memcpy( output, info->
buf, use_len );
239 info->
buf += use_len;
243 if( len - use_len > 0 )
244 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
273 uint32_t i, *k, sum, delta=0x9E3779B9;
274 unsigned char result[4], *out = output;
276 if( rng_state == NULL )
283 size_t use_len = ( len > 4 ) ? 4 : len;
286 for( i = 0; i < 32; i++ )
288 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
290 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
294 memcpy( out, result, use_len );
306 #if defined(POLARSSL_PLATFORM_C)
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
316 #ifdef POLARSSL_BLOWFISH_C
318 #define TEST_SUITE_ACTIVE
320 static int test_assert(
int correct,
const char *test )
327 printf(
"FAILED\n" );
328 printf(
" %s\n", test );
333 #define TEST_ASSERT( TEST ) \
334 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335 if( test_errors) goto exit; \
340 if( (*str)[0] !=
'"' ||
341 (*str)[strlen( *str ) - 1] !=
'"' )
343 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
348 (*str)[strlen( *str ) - 1] =
'\0';
360 for( i = 0; i < strlen( str ); i++ )
362 if( i == 0 && str[i] ==
'-' )
368 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369 str[i - 1] ==
'0' && str[i] ==
'x' )
375 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
376 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
377 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
387 *value = strtol( str, NULL, 16 );
389 *value = strtol( str, NULL, 10 );
394 if( strcmp( str,
"POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH" ) == 0 )
399 #ifdef POLARSSL_CIPHER_MODE_CBC
400 if( strcmp( str,
"POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH" ) == 0 )
405 #endif // POLARSSL_CIPHER_MODE_CBC
408 printf(
"Expected integer for parameter and got: %s\n", str );
412 void test_suite_blowfish_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
413 char *hex_dst_string,
int setkey_result )
415 unsigned char key_str[100];
416 unsigned char src_str[100];
417 unsigned char dst_str[100];
418 unsigned char output[100];
422 memset(key_str, 0x00, 100);
423 memset(src_str, 0x00, 100);
424 memset(dst_str, 0x00, 100);
425 memset(output, 0x00, 100);
428 key_len =
unhexify( key_str, hex_key_string );
429 unhexify( src_str, hex_src_string );
432 if( setkey_result == 0 )
435 hexify( dst_str, output, 8 );
437 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
444 void test_suite_blowfish_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
445 char *hex_dst_string,
int setkey_result )
447 unsigned char key_str[100];
448 unsigned char src_str[100];
449 unsigned char dst_str[100];
450 unsigned char output[100];
454 memset(key_str, 0x00, 100);
455 memset(src_str, 0x00, 100);
456 memset(dst_str, 0x00, 100);
457 memset(output, 0x00, 100);
460 key_len =
unhexify( key_str, hex_key_string );
461 unhexify( src_str, hex_src_string );
464 if( setkey_result == 0 )
467 hexify( dst_str, output, 8 );
469 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
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,
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];
487 int key_len, data_len;
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);
496 key_len =
unhexify( key_str, hex_key_string );
498 data_len =
unhexify( src_str, hex_src_string );
503 if( cbc_result == 0 )
505 hexify( dst_str, output, data_len );
507 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
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,
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];
526 int key_len, data_len;
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);
535 key_len =
unhexify( key_str, hex_key_string );
537 data_len =
unhexify( src_str, hex_src_string );
543 hexify( dst_str, output, data_len );
545 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
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 )
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];
563 size_t iv_offset = 0;
564 int key_len, src_len;
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);
573 key_len =
unhexify( key_str, hex_key_string );
575 src_len =
unhexify( src_str, hex_src_string );
579 hexify( dst_str, output, src_len );
581 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
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 )
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];
598 size_t iv_offset = 0;
599 int key_len, src_len;
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);
608 key_len =
unhexify( key_str, hex_key_string );
610 src_len =
unhexify( src_str, hex_src_string );
614 hexify( dst_str, output, src_len );
616 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
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 )
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];
634 size_t iv_offset = 0;
635 int key_len, src_len;
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);
645 key_len =
unhexify( key_str, hex_key_string );
647 src_len =
unhexify( src_str, hex_src_string );
651 hexify( dst_str, output, src_len );
653 TEST_ASSERT( strcmp( (
char *) dst_str, hex_dst_string ) == 0 );
680 #if defined(TEST_SUITE_ACTIVE)
681 if( strcmp( params[0],
"blowfish_encrypt_ecb" ) == 0 )
684 char *param1 = params[1];
685 char *param2 = params[2];
686 char *param3 = params[3];
691 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
698 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
700 test_suite_blowfish_encrypt_ecb( param1, param2, param3, param4 );
706 if( strcmp( params[0],
"blowfish_decrypt_ecb" ) == 0 )
709 char *param1 = params[1];
710 char *param2 = params[2];
711 char *param3 = params[3];
716 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
723 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
725 test_suite_blowfish_decrypt_ecb( param1, param2, param3, param4 );
731 if( strcmp( params[0],
"blowfish_encrypt_cbc" ) == 0 )
733 #ifdef POLARSSL_CIPHER_MODE_CBC
735 char *param1 = params[1];
736 char *param2 = params[2];
737 char *param3 = params[3];
738 char *param4 = params[4];
743 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
751 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
753 test_suite_blowfish_encrypt_cbc( param1, param2, param3, param4, param5 );
760 if( strcmp( params[0],
"blowfish_decrypt_cbc" ) == 0 )
762 #ifdef POLARSSL_CIPHER_MODE_CBC
764 char *param1 = params[1];
765 char *param2 = params[2];
766 char *param3 = params[3];
767 char *param4 = params[4];
772 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
780 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
782 test_suite_blowfish_decrypt_cbc( param1, param2, param3, param4, param5 );
789 if( strcmp( params[0],
"blowfish_encrypt_cfb64" ) == 0 )
791 #ifdef POLARSSL_CIPHER_MODE_CFB
793 char *param1 = params[1];
794 char *param2 = params[2];
795 char *param3 = params[3];
796 char *param4 = params[4];
800 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
809 test_suite_blowfish_encrypt_cfb64( param1, param2, param3, param4 );
816 if( strcmp( params[0],
"blowfish_decrypt_cfb64" ) == 0 )
818 #ifdef POLARSSL_CIPHER_MODE_CFB
820 char *param1 = params[1];
821 char *param2 = params[2];
822 char *param3 = params[3];
823 char *param4 = params[4];
827 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
836 test_suite_blowfish_decrypt_cfb64( param1, param2, param3, param4 );
843 if( strcmp( params[0],
"blowfish_encrypt_ctr" ) == 0 )
845 #ifdef POLARSSL_CIPHER_MODE_CTR
847 char *param1 = params[1];
848 char *param2 = params[2];
849 char *param3 = params[3];
850 char *param4 = params[4];
854 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
863 test_suite_blowfish_encrypt_ctr( param1, param2, param3, param4 );
872 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
886 ret = fgets( buf, len, f );
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';
906 while( *p !=
'\0' && p < buf + len )
916 if( p + 1 < buf + len )
928 for( i = 0; i < cnt; i++ )
935 if( *p ==
'\\' && *(p + 1) ==
'n' )
940 else if( *p ==
'\\' && *(p + 1) ==
':' )
945 else if( *p ==
'\\' && *(p + 1) ==
'?' )
961 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
962 const char *filename =
"/tmp/B.1c06a028-9709-4951-a65b-b24142b09746/BUILD/polarssl-1.3.9/tests/suites/test_suite_blowfish.data";
967 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
968 unsigned char alloc_buf[1000000];
972 file = fopen( filename,
"r" );
975 fprintf( stderr,
"Failed to open\n" );
979 while( !feof( file ) )
983 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
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,
" " );
994 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
998 if( strcmp( params[0],
"depends_on" ) == 0 )
1000 for( i = 1; i < cnt; i++ )
1004 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1015 if( skip == 1 || ret == 3 )
1018 fprintf( stdout,
"----\n" );
1023 fprintf( stdout,
"PASS\n" );
1028 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1035 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1037 if( strlen(buf) != 0 )
1039 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1045 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1046 if( total_errors == 0 )
1047 fprintf( stdout,
"PASSED" );
1049 fprintf( stdout,
"FAILED" );
1051 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1052 total_tests - total_errors, total_tests, total_skipped );
1054 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1055 #if defined(POLARSSL_MEMORY_DEBUG)
1056 memory_buffer_alloc_status();
1061 return( total_errors != 0 );
int parse_arguments(char *buf, size_t len, char *params[50])
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)
int get_line(FILE *f, char *buf, size_t len)
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.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
void blowfish_free(blowfish_context *ctx)
Clear Blowfish context.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#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().
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
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 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 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.
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) ...