1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
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_DES_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 #ifdef POLARSSL_CIPHER_MODE_CBC
395 if( strcmp( str,
"POLARSSL_ERR_DES_INVALID_INPUT_LENGTH" ) == 0 )
400 #endif // POLARSSL_CIPHER_MODE_CBC
403 printf(
"Expected integer for parameter and got: %s\n", str );
407 void test_suite_des_check_weak(
char *key_hex,
int ret )
411 memset( key, 0,
sizeof key );
421 void test_suite_des_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
422 char *hex_dst_string )
424 unsigned char key_str[100];
425 unsigned char src_str[100];
426 unsigned char dst_str[100];
427 unsigned char output[100];
430 memset(key_str, 0x00, 100);
431 memset(src_str, 0x00, 100);
432 memset(dst_str, 0x00, 100);
433 memset(output, 0x00, 100);
436 unhexify( key_str, hex_key_string );
437 unhexify( src_str, hex_src_string );
441 hexify( dst_str, output, 8 );
443 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
449 void test_suite_des_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
450 char *hex_dst_string )
452 unsigned char key_str[100];
453 unsigned char src_str[100];
454 unsigned char dst_str[100];
455 unsigned char output[100];
458 memset(key_str, 0x00, 100);
459 memset(src_str, 0x00, 100);
460 memset(dst_str, 0x00, 100);
461 memset(output, 0x00, 100);
464 unhexify( key_str, hex_key_string );
465 unhexify( src_str, hex_src_string );
469 hexify( dst_str, output, 8 );
471 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
477 #ifdef POLARSSL_CIPHER_MODE_CBC
478 void test_suite_des_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
479 char *hex_src_string,
char *hex_dst_string,
int cbc_result )
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];
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 unhexify( key_str, hex_key_string );
498 src_len =
unhexify( src_str, hex_src_string );
502 if( cbc_result == 0 )
504 hexify( dst_str, output, src_len );
506 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
514 #ifdef POLARSSL_CIPHER_MODE_CBC
515 void test_suite_des_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
516 char *hex_src_string,
char *hex_dst_string,
int cbc_result )
518 unsigned char key_str[100];
519 unsigned char iv_str[100];
520 unsigned char src_str[100];
521 unsigned char dst_str[100];
522 unsigned char output[100];
526 memset(key_str, 0x00, 100);
527 memset(iv_str, 0x00, 100);
528 memset(src_str, 0x00, 100);
529 memset(dst_str, 0x00, 100);
530 memset(output, 0x00, 100);
533 unhexify( key_str, hex_key_string );
535 src_len =
unhexify( src_str, hex_src_string );
539 if( cbc_result == 0 )
541 hexify( dst_str, output, src_len );
543 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
551 void test_suite_des3_encrypt_ecb(
int key_count,
char *hex_key_string,
552 char *hex_src_string,
char *hex_dst_string )
554 unsigned char key_str[100];
555 unsigned char src_str[100];
556 unsigned char dst_str[100];
557 unsigned char output[100];
560 memset(key_str, 0x00, 100);
561 memset(src_str, 0x00, 100);
562 memset(dst_str, 0x00, 100);
563 memset(output, 0x00, 100);
566 unhexify( key_str, hex_key_string );
567 unhexify( src_str, hex_src_string );
571 else if( key_count == 3 )
577 hexify( dst_str, output, 8 );
579 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
585 void test_suite_des3_decrypt_ecb(
int key_count,
char *hex_key_string,
586 char *hex_src_string,
char *hex_dst_string )
588 unsigned char key_str[100];
589 unsigned char src_str[100];
590 unsigned char dst_str[100];
591 unsigned char output[100];
594 memset(key_str, 0x00, 100);
595 memset(src_str, 0x00, 100);
596 memset(dst_str, 0x00, 100);
597 memset(output, 0x00, 100);
600 unhexify( key_str, hex_key_string );
601 unhexify( src_str, hex_src_string );
605 else if( key_count == 3 )
611 hexify( dst_str, output, 8 );
613 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
619 #ifdef POLARSSL_CIPHER_MODE_CBC
620 void test_suite_des3_encrypt_cbc(
int key_count,
char *hex_key_string,
621 char *hex_iv_string,
char *hex_src_string,
622 char *hex_dst_string,
int cbc_result )
624 unsigned char key_str[100];
625 unsigned char iv_str[100];
626 unsigned char src_str[100];
627 unsigned char dst_str[100];
628 unsigned char output[100];
632 memset(key_str, 0x00, 100);
633 memset(iv_str, 0x00, 100);
634 memset(src_str, 0x00, 100);
635 memset(dst_str, 0x00, 100);
636 memset(output, 0x00, 100);
639 unhexify( key_str, hex_key_string );
641 src_len =
unhexify( src_str, hex_src_string );
645 else if( key_count == 3 )
652 if( cbc_result == 0 )
654 hexify( dst_str, output, src_len );
656 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
664 #ifdef POLARSSL_CIPHER_MODE_CBC
665 void test_suite_des3_decrypt_cbc(
int key_count,
char *hex_key_string,
666 char *hex_iv_string,
char *hex_src_string,
667 char *hex_dst_string,
int cbc_result )
669 unsigned char key_str[100];
670 unsigned char iv_str[100];
671 unsigned char src_str[100];
672 unsigned char dst_str[100];
673 unsigned char output[100];
677 memset(key_str, 0x00, 100);
678 memset(iv_str, 0x00, 100);
679 memset(src_str, 0x00, 100);
680 memset(dst_str, 0x00, 100);
681 memset(output, 0x00, 100);
684 unhexify( key_str, hex_key_string );
686 src_len =
unhexify( src_str, hex_src_string );
690 else if( key_count == 3 )
697 if( cbc_result == 0 )
699 hexify( dst_str, output, src_len );
701 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
709 void test_suite_des_key_parity_run()
720 for( i = 0; i < 32; i++ )
722 for( j = 0; j < 8; j++ )
731 for( j = 0; j < 8; j++ )
733 parity = key[j] ^ ( key[j] >> 4 );
753 #ifdef POLARSSL_SELF_TEST
754 void test_suite_des_selftest()
783 #if defined(TEST_SUITE_ACTIVE)
784 if( strcmp( params[0],
"des_check_weak" ) == 0 )
787 char *param1 = params[1];
792 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
797 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
799 test_suite_des_check_weak( param1, param2 );
805 if( strcmp( params[0],
"des_encrypt_ecb" ) == 0 )
808 char *param1 = params[1];
809 char *param2 = params[2];
810 char *param3 = params[3];
814 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
822 test_suite_des_encrypt_ecb( param1, param2, param3 );
828 if( strcmp( params[0],
"des_decrypt_ecb" ) == 0 )
831 char *param1 = params[1];
832 char *param2 = params[2];
833 char *param3 = params[3];
837 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
845 test_suite_des_decrypt_ecb( param1, param2, param3 );
851 if( strcmp( params[0],
"des_encrypt_cbc" ) == 0 )
853 #ifdef POLARSSL_CIPHER_MODE_CBC
855 char *param1 = params[1];
856 char *param2 = params[2];
857 char *param3 = params[3];
858 char *param4 = params[4];
863 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
871 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
873 test_suite_des_encrypt_cbc( param1, param2, param3, param4, param5 );
880 if( strcmp( params[0],
"des_decrypt_cbc" ) == 0 )
882 #ifdef POLARSSL_CIPHER_MODE_CBC
884 char *param1 = params[1];
885 char *param2 = params[2];
886 char *param3 = params[3];
887 char *param4 = params[4];
892 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
900 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
902 test_suite_des_decrypt_cbc( param1, param2, param3, param4, param5 );
909 if( strcmp( params[0],
"des3_encrypt_ecb" ) == 0 )
913 char *param2 = params[2];
914 char *param3 = params[3];
915 char *param4 = params[4];
919 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
923 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
928 test_suite_des3_encrypt_ecb( param1, param2, param3, param4 );
934 if( strcmp( params[0],
"des3_decrypt_ecb" ) == 0 )
938 char *param2 = params[2];
939 char *param3 = params[3];
940 char *param4 = params[4];
944 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
948 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
953 test_suite_des3_decrypt_ecb( param1, param2, param3, param4 );
959 if( strcmp( params[0],
"des3_encrypt_cbc" ) == 0 )
961 #ifdef POLARSSL_CIPHER_MODE_CBC
964 char *param2 = params[2];
965 char *param3 = params[3];
966 char *param4 = params[4];
967 char *param5 = params[5];
972 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
976 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
981 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
983 test_suite_des3_encrypt_cbc( param1, param2, param3, param4, param5, param6 );
990 if( strcmp( params[0],
"des3_decrypt_cbc" ) == 0 )
992 #ifdef POLARSSL_CIPHER_MODE_CBC
995 char *param2 = params[2];
996 char *param3 = params[3];
997 char *param4 = params[4];
998 char *param5 = params[5];
1003 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1007 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1012 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1014 test_suite_des3_decrypt_cbc( param1, param2, param3, param4, param5, param6 );
1021 if( strcmp( params[0],
"des_key_parity_run" ) == 0 )
1027 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1032 test_suite_des_key_parity_run( );
1038 if( strcmp( params[0],
"des_selftest" ) == 0 )
1040 #ifdef POLARSSL_SELF_TEST
1045 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1050 test_suite_des_selftest( );
1059 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1073 ret = fgets( buf, len, f );
1077 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1078 buf[strlen(buf) - 1] =
'\0';
1079 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1080 buf[strlen(buf) - 1] =
'\0';
1091 params[cnt++] = cur;
1093 while( *p !=
'\0' && p < buf + len )
1103 if( p + 1 < buf + len )
1106 params[cnt++] = cur;
1115 for( i = 0; i < cnt; i++ )
1122 if( *p ==
'\\' && *(p + 1) ==
'n' )
1127 else if( *p ==
'\\' && *(p + 1) ==
':' )
1132 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1148 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1149 const char *filename =
"/tmp/B.HYlHIA/BUILD/polarssl-1.3.9/tests/suites/test_suite_des.data";
1154 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1155 unsigned char alloc_buf[1000000];
1159 file = fopen( filename,
"r" );
1162 fprintf( stderr,
"Failed to open\n" );
1166 while( !feof( file ) )
1170 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1172 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1173 fprintf( stdout,
" " );
1174 for( i = strlen( buf ) + 1; i < 67; i++ )
1175 fprintf( stdout,
"." );
1176 fprintf( stdout,
" " );
1181 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1185 if( strcmp( params[0],
"depends_on" ) == 0 )
1187 for( i = 1; i < cnt; i++ )
1191 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1202 if( skip == 1 || ret == 3 )
1205 fprintf( stdout,
"----\n" );
1210 fprintf( stdout,
"PASS\n" );
1215 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1222 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1224 if( strlen(buf) != 0 )
1226 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1232 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1233 if( total_errors == 0 )
1234 fprintf( stdout,
"PASSED" );
1236 fprintf( stdout,
"FAILED" );
1238 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1239 total_tests - total_errors, total_tests, total_skipped );
1241 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1242 #if defined(POLARSSL_MEMORY_DEBUG)
1243 memory_buffer_alloc_status();
1248 return( total_errors != 0 );