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_DES_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_DES_INVALID_INPUT_LENGTH" ) == 0 )
371 printf(
"Expected integer for parameter and got: %s\n", str );
375 void test_suite_des_encrypt_ecb(
char *hex_key_string,
char *hex_src_string,
376 char *hex_dst_string )
378 unsigned char key_str[100];
379 unsigned char src_str[100];
380 unsigned char dst_str[100];
381 unsigned char output[100];
384 memset(key_str, 0x00, 100);
385 memset(src_str, 0x00, 100);
386 memset(dst_str, 0x00, 100);
387 memset(output, 0x00, 100);
389 unhexify( key_str, hex_key_string );
390 unhexify( src_str, hex_src_string );
394 hexify( dst_str, output, 8 );
396 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
399 void test_suite_des_decrypt_ecb(
char *hex_key_string,
char *hex_src_string,
400 char *hex_dst_string )
402 unsigned char key_str[100];
403 unsigned char src_str[100];
404 unsigned char dst_str[100];
405 unsigned char output[100];
408 memset(key_str, 0x00, 100);
409 memset(src_str, 0x00, 100);
410 memset(dst_str, 0x00, 100);
411 memset(output, 0x00, 100);
413 unhexify( key_str, hex_key_string );
414 unhexify( src_str, hex_src_string );
418 hexify( dst_str, output, 8 );
420 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
423 #ifdef POLARSSL_CIPHER_MODE_CBC
424 void test_suite_des_encrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
425 char *hex_src_string,
char *hex_dst_string,
int cbc_result )
427 unsigned char key_str[100];
428 unsigned char iv_str[100];
429 unsigned char src_str[100];
430 unsigned char dst_str[100];
431 unsigned char output[100];
435 memset(key_str, 0x00, 100);
436 memset(iv_str, 0x00, 100);
437 memset(src_str, 0x00, 100);
438 memset(dst_str, 0x00, 100);
439 memset(output, 0x00, 100);
441 unhexify( key_str, hex_key_string );
443 src_len =
unhexify( src_str, hex_src_string );
447 if( cbc_result == 0 )
449 hexify( dst_str, output, src_len );
451 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
456 #ifdef POLARSSL_CIPHER_MODE_CBC
457 void test_suite_des_decrypt_cbc(
char *hex_key_string,
char *hex_iv_string,
458 char *hex_src_string,
char *hex_dst_string,
int cbc_result )
460 unsigned char key_str[100];
461 unsigned char iv_str[100];
462 unsigned char src_str[100];
463 unsigned char dst_str[100];
464 unsigned char output[100];
468 memset(key_str, 0x00, 100);
469 memset(iv_str, 0x00, 100);
470 memset(src_str, 0x00, 100);
471 memset(dst_str, 0x00, 100);
472 memset(output, 0x00, 100);
474 unhexify( key_str, hex_key_string );
476 src_len =
unhexify( src_str, hex_src_string );
480 if( cbc_result == 0 )
482 hexify( dst_str, output, src_len );
484 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
489 void test_suite_des3_encrypt_ecb(
int key_count,
char *hex_key_string,
490 char *hex_src_string,
char *hex_dst_string )
492 unsigned char key_str[100];
493 unsigned char src_str[100];
494 unsigned char dst_str[100];
495 unsigned char output[100];
498 memset(key_str, 0x00, 100);
499 memset(src_str, 0x00, 100);
500 memset(dst_str, 0x00, 100);
501 memset(output, 0x00, 100);
503 unhexify( key_str, hex_key_string );
504 unhexify( src_str, hex_src_string );
508 else if( key_count == 3 )
514 hexify( dst_str, output, 8 );
516 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
519 void test_suite_des3_decrypt_ecb(
int key_count,
char *hex_key_string,
520 char *hex_src_string,
char *hex_dst_string )
522 unsigned char key_str[100];
523 unsigned char src_str[100];
524 unsigned char dst_str[100];
525 unsigned char output[100];
528 memset(key_str, 0x00, 100);
529 memset(src_str, 0x00, 100);
530 memset(dst_str, 0x00, 100);
531 memset(output, 0x00, 100);
533 unhexify( key_str, hex_key_string );
534 unhexify( src_str, hex_src_string );
538 else if( key_count == 3 )
544 hexify( dst_str, output, 8 );
546 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
549 #ifdef POLARSSL_CIPHER_MODE_CBC
550 void test_suite_des3_encrypt_cbc(
int key_count,
char *hex_key_string,
551 char *hex_iv_string,
char *hex_src_string,
552 char *hex_dst_string,
int cbc_result )
554 unsigned char key_str[100];
555 unsigned char iv_str[100];
556 unsigned char src_str[100];
557 unsigned char dst_str[100];
558 unsigned char output[100];
562 memset(key_str, 0x00, 100);
563 memset(iv_str, 0x00, 100);
564 memset(src_str, 0x00, 100);
565 memset(dst_str, 0x00, 100);
566 memset(output, 0x00, 100);
568 unhexify( key_str, hex_key_string );
570 src_len =
unhexify( src_str, hex_src_string );
574 else if( key_count == 3 )
581 if( cbc_result == 0 )
583 hexify( dst_str, output, src_len );
585 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
590 #ifdef POLARSSL_CIPHER_MODE_CBC
591 void test_suite_des3_decrypt_cbc(
int key_count,
char *hex_key_string,
592 char *hex_iv_string,
char *hex_src_string,
593 char *hex_dst_string,
int cbc_result )
595 unsigned char key_str[100];
596 unsigned char iv_str[100];
597 unsigned char src_str[100];
598 unsigned char dst_str[100];
599 unsigned char output[100];
603 memset(key_str, 0x00, 100);
604 memset(iv_str, 0x00, 100);
605 memset(src_str, 0x00, 100);
606 memset(dst_str, 0x00, 100);
607 memset(output, 0x00, 100);
609 unhexify( key_str, hex_key_string );
611 src_len =
unhexify( src_str, hex_src_string );
615 else if( key_count == 3 )
622 if( cbc_result == 0 )
624 hexify( dst_str, output, src_len );
626 TEST_ASSERT( strcasecmp( (
char *) dst_str, hex_dst_string ) == 0 );
631 void test_suite_des_key_parity_run()
642 for( i = 0; i < 32; i++ )
644 for( j = 0; j < 8; j++ )
653 for( j = 0; j < 8; j++ )
655 parity = key[j] ^ ( key[j] >> 4 );
672 #ifdef POLARSSL_SELF_TEST
673 void test_suite_des_selftest()
699 #if defined(TEST_SUITE_ACTIVE)
700 if( strcmp( params[0],
"des_encrypt_ecb" ) == 0 )
703 char *param1 = params[1];
704 char *param2 = params[2];
705 char *param3 = params[3];
709 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
717 test_suite_des_encrypt_ecb( param1, param2, param3 );
723 if( strcmp( params[0],
"des_decrypt_ecb" ) == 0 )
726 char *param1 = params[1];
727 char *param2 = params[2];
728 char *param3 = params[3];
732 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
740 test_suite_des_decrypt_ecb( param1, param2, param3 );
746 if( strcmp( params[0],
"des_encrypt_cbc" ) == 0 )
748 #ifdef POLARSSL_CIPHER_MODE_CBC
750 char *param1 = params[1];
751 char *param2 = params[2];
752 char *param3 = params[3];
753 char *param4 = params[4];
758 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
766 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
768 test_suite_des_encrypt_cbc( param1, param2, param3, param4, param5 );
775 if( strcmp( params[0],
"des_decrypt_cbc" ) == 0 )
777 #ifdef POLARSSL_CIPHER_MODE_CBC
779 char *param1 = params[1];
780 char *param2 = params[2];
781 char *param3 = params[3];
782 char *param4 = params[4];
787 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
795 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
797 test_suite_des_decrypt_cbc( param1, param2, param3, param4, param5 );
804 if( strcmp( params[0],
"des3_encrypt_ecb" ) == 0 )
808 char *param2 = params[2];
809 char *param3 = params[3];
810 char *param4 = params[4];
814 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
818 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
823 test_suite_des3_encrypt_ecb( param1, param2, param3, param4 );
829 if( strcmp( params[0],
"des3_decrypt_ecb" ) == 0 )
833 char *param2 = params[2];
834 char *param3 = params[3];
835 char *param4 = params[4];
839 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
843 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
848 test_suite_des3_decrypt_ecb( param1, param2, param3, param4 );
854 if( strcmp( params[0],
"des3_encrypt_cbc" ) == 0 )
856 #ifdef POLARSSL_CIPHER_MODE_CBC
859 char *param2 = params[2];
860 char *param3 = params[3];
861 char *param4 = params[4];
862 char *param5 = params[5];
867 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
871 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
876 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
878 test_suite_des3_encrypt_cbc( param1, param2, param3, param4, param5, param6 );
885 if( strcmp( params[0],
"des3_decrypt_cbc" ) == 0 )
887 #ifdef POLARSSL_CIPHER_MODE_CBC
890 char *param2 = params[2];
891 char *param3 = params[3];
892 char *param4 = params[4];
893 char *param5 = params[5];
898 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
902 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
907 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
909 test_suite_des3_decrypt_cbc( param1, param2, param3, param4, param5, param6 );
916 if( strcmp( params[0],
"des_key_parity_run" ) == 0 )
922 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
927 test_suite_des_key_parity_run( );
933 if( strcmp( params[0],
"des_selftest" ) == 0 )
935 #ifdef POLARSSL_SELF_TEST
940 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
945 test_suite_des_selftest( );
954 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
968 ret = fgets( buf, len, f );
972 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
973 buf[strlen(buf) - 1] =
'\0';
974 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
975 buf[strlen(buf) - 1] =
'\0';
988 while( *p !=
'\0' && p < buf + len )
998 if( p + 1 < buf + len )
1001 params[cnt++] = cur;
1010 for( i = 0; i < cnt; i++ )
1017 if( *p ==
'\\' && *(p + 1) ==
'n' )
1022 else if( *p ==
'\\' && *(p + 1) ==
':' )
1027 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1043 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1044 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_des.data";
1049 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1050 unsigned char alloc_buf[1000000];
1051 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1054 file = fopen( filename,
"r" );
1057 fprintf( stderr,
"Failed to open\n" );
1061 while( !feof( file ) )
1065 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1067 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1068 fprintf( stdout,
" " );
1069 for( i = strlen( buf ) + 1; i < 67; i++ )
1070 fprintf( stdout,
"." );
1071 fprintf( stdout,
" " );
1076 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1080 if( strcmp( params[0],
"depends_on" ) == 0 )
1082 for( i = 1; i < cnt; i++ )
1086 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1097 if( skip == 1 || ret == 3 )
1100 fprintf( stdout,
"----\n" );
1105 fprintf( stdout,
"PASS\n" );
1110 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1117 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1119 if( strlen(buf) != 0 )
1121 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1127 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1128 if( total_errors == 0 )
1129 fprintf( stdout,
"PASSED" );
1131 fprintf( stdout,
"FAILED" );
1133 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1134 total_tests - total_errors, total_tests, total_skipped );
1136 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1137 #if defined(POLARSSL_MEMORY_DEBUG)
1138 memory_buffer_alloc_status();
1140 memory_buffer_alloc_free();
1143 return( total_errors != 0 );
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Info structure for the pseudo random function.
int des_self_test(int verbose)
Checkup routine.
int des_crypt_ecb(des_context *ctx, const unsigned char input[8], unsigned char output[8])
DES-ECB block 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.
Configuration options (set of defines)
static int test_assert(int correct, char *test)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int main(int argc, char *argv[])
int des3_set3key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, encryption)
int des3_set3key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, decryption)
#define TEST_ASSERT(TEST)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Triple-DES context structure.
int des3_set2key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, encryption)
static int unhexify(unsigned char *obuf, const char *ibuf)
void des_key_set_parity(unsigned char key[DES_KEY_SIZE])
Set key parity on the given key to odd.
int parse_arguments(char *buf, size_t len, char *params[50])
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
int des_crypt_cbc(des_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
DES-CBC buffer encryption/decryption.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int verify_string(char **str)
int des_setkey_enc(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, encryption)
int des_setkey_dec(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, decryption)
int dispatch_test(int cnt, char *params[50])
int des3_crypt_ecb(des3_context *ctx, const unsigned char input[8], unsigned char output[8])
3DES-ECB block encryption/decryption
#define PUT_UINT32_BE(n, b, i)
int verify_int(char *str, int *value)
int des_key_check_key_parity(const unsigned char key[DES_KEY_SIZE])
Check that key parity on the given key is odd.
#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
The data input has an invalid length.
int get_line(FILE *f, char *buf, size_t len)
int des3_set2key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, decryption)
int des3_crypt_cbc(des3_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
3DES-CBC buffer encryption/decryption