1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
11 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len );
13 #define RSA_KEY_SIZE 512
14 #define RSA_KEY_LEN 64
20 #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
24 #if defined(POLARSSL_ECP_C)
41 #if defined(POLARSSL_RSA_C)
43 const unsigned char *input,
unsigned char *output,
44 size_t output_max_len )
47 input, output, output_max_len ) );
50 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng,
51 int mode,
md_type_t md_alg,
unsigned int hashlen,
52 const unsigned char *hash,
unsigned char *sig )
55 md_alg, hashlen, hash, sig ) );
65 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
69 #if defined(POLARSSL_PLATFORM_C)
72 #define polarssl_malloc malloc
73 #define polarssl_free free
78 typedef UINT32 uint32_t;
91 #define GET_UINT32_BE(n,b,i) \
93 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
94 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
95 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
96 | ( (uint32_t) (b)[(i) + 3] ); \
100 #ifndef PUT_UINT32_BE
101 #define PUT_UINT32_BE(n,b,i) \
103 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
104 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
105 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
106 (b)[(i) + 3] = (unsigned char) ( (n) ); \
110 static int unhexify(
unsigned char *obuf,
const char *ibuf)
113 int len = strlen(ibuf) / 2;
114 assert(!(strlen(ibuf) %1));
119 if( c >=
'0' && c <=
'9' )
121 else if( c >=
'a' && c <=
'f' )
123 else if( c >=
'A' && c <=
'F' )
129 if( c2 >=
'0' && c2 <=
'9' )
131 else if( c2 >=
'a' && c2 <=
'f' )
133 else if( c2 >=
'A' && c2 <=
'F' )
138 *obuf++ = ( c << 4 ) | c2;
144 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
156 *obuf++ =
'a' + h - 10;
161 *obuf++ =
'a' + l - 10;
178 size_t actual_len = len != 0 ? len : 1;
183 memset( p, 0x00, actual_len );
202 *olen = strlen(ibuf) / 2;
208 assert( obuf != NULL );
224 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
226 #if !defined(__OpenBSD__)
229 if( rng_state != NULL )
232 for( i = 0; i < len; ++i )
235 if( rng_state != NULL )
238 arc4random_buf( output, len );
249 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
251 if( rng_state != NULL )
254 memset( output, 0, len );
281 if( rng_state == NULL )
290 memcpy( output, info->
buf, use_len );
291 info->
buf += use_len;
295 if( len - use_len > 0 )
296 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
325 uint32_t i, *k, sum, delta=0x9E3779B9;
326 unsigned char result[4], *out = output;
328 if( rng_state == NULL )
335 size_t use_len = ( len > 4 ) ? 4 : len;
338 for( i = 0; i < 32; i++ )
340 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
342 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
346 memcpy( out, result, use_len );
358 #if defined(POLARSSL_PLATFORM_C)
361 #define polarssl_printf printf
362 #define polarssl_malloc malloc
363 #define polarssl_free free
370 #define TEST_SUITE_ACTIVE
372 static int test_assert(
int correct,
const char *test )
378 if( test_errors == 1 )
379 printf(
"FAILED\n" );
380 printf(
" %s\n", test );
385 #define TEST_ASSERT( TEST ) \
386 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
387 if( test_errors) goto exit; \
392 if( (*str)[0] !=
'"' ||
393 (*str)[strlen( *str ) - 1] !=
'"' )
395 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
400 (*str)[strlen( *str ) - 1] =
'\0';
412 for( i = 0; i < strlen( str ); i++ )
414 if( i == 0 && str[i] ==
'-' )
420 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
421 str[i - 1] ==
'0' && str[i] ==
'x' )
427 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
428 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
429 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
439 *value = strtol( str, NULL, 16 );
441 *value = strtol( str, NULL, 10 );
446 #ifdef POLARSSL_RSA_C
447 if( strcmp( str,
"RSA_SALT_LEN_ANY" ) == 0 )
452 #endif // POLARSSL_RSA_C
453 #ifdef POLARSSL_RSA_C
454 if( strcmp( str,
"POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
459 #endif // POLARSSL_RSA_C
460 if( strcmp( str,
"POLARSSL_PK_ECKEY_DH" ) == 0 )
465 if( strcmp( str,
"POLARSSL_PK_ECKEY" ) == 0 )
470 #ifdef POLARSSL_ECDSA_C
471 if( strcmp( str,
"POLARSSL_ERR_ECP_VERIFY_FAILED" ) == 0 )
476 #endif // POLARSSL_ECDSA_C
477 #ifdef POLARSSL_RSA_C
478 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
483 #endif // POLARSSL_RSA_C
484 #ifdef POLARSSL_RSA_C
485 if( strcmp( str,
"POLARSSL_MD_NONE" ) == 0 )
490 #endif // POLARSSL_RSA_C
491 #ifdef POLARSSL_RSA_C
492 if( strcmp( str,
"POLARSSL_PK_RSASSA_PSS" ) == 0 )
497 #endif // POLARSSL_RSA_C
498 #ifdef POLARSSL_RSA_C
499 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
504 #endif // POLARSSL_RSA_C
505 #ifdef POLARSSL_RSA_C
506 if( strcmp( str,
"POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
511 #endif // POLARSSL_RSA_C
512 #ifdef POLARSSL_RSA_C
513 if( strcmp( str,
"-1" ) == 0 )
518 #endif // POLARSSL_RSA_C
519 #ifdef POLARSSL_ECDSA_C
520 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
525 #endif // POLARSSL_ECDSA_C
526 if( strcmp( str,
"POLARSSL_PK_RSA" ) == 0 )
531 #ifdef POLARSSL_RSA_C
532 if( strcmp( str,
"POLARSSL_ERR_PK_BAD_INPUT_DATA" ) == 0 )
537 #endif // POLARSSL_RSA_C
538 if( strcmp( str,
"POLARSSL_ERR_PK_TYPE_MISMATCH" ) == 0 )
543 if( strcmp( str,
"POLARSSL_PK_ECDSA" ) == 0 )
550 printf(
"Expected integer for parameter and got: %s\n", str );
554 void test_suite_pk_utils(
int type,
int size,
int len,
char *name )
573 #ifdef POLARSSL_RSA_C
574 void test_suite_pk_rsa_verify_test_vec(
char *message_hex_string,
int digest,
575 int mod,
int radix_N,
char *input_N,
int radix_E,
576 char *input_E,
char *result_hex_str,
int result )
578 unsigned char message_str[1000];
579 unsigned char hash_result[1000];
580 unsigned char result_str[1000];
587 memset( message_str, 0x00, 1000 );
588 memset( hash_result, 0x00, 1000 );
589 memset( result_str, 0x00, 1000 );
598 msg_len =
unhexify( message_str, message_hex_string );
599 unhexify( result_str, result_hex_str );
612 #ifdef POLARSSL_RSA_C
613 void test_suite_pk_rsa_verify_ext_test_vec(
char *message_hex_string,
int digest,
614 int mod,
int radix_N,
char *input_N,
int radix_E,
615 char *input_E,
char *result_hex_str,
616 int pk_type,
int mgf1_hash_id,
int salt_len,
619 unsigned char message_str[1000];
620 unsigned char hash_result[1000];
621 unsigned char result_str[1000];
631 memset( message_str, 0x00, 1000 );
632 memset( hash_result, 0x00, 1000 );
633 memset( result_str, 0x00, 1000 );
642 msg_len =
unhexify( message_str, message_hex_string );
643 unhexify( result_str, result_hex_str );
648 message_str, msg_len, hash_result ) == 0 );
653 memcpy( hash_result, message_str, msg_len );
657 if( mgf1_hash_id < 0 )
670 digest, hash_result, hash_len,
678 #ifdef POLARSSL_ECDSA_C
679 void test_suite_pk_ec_test_vec(
int type,
int id,
char *key_str,
680 char *hash_str,
char * sig_str,
int ret )
684 unsigned char hash[100], sig[500], key[500];
685 size_t hash_len, sig_len, key_len;
689 memset( hash, 0,
sizeof( hash ) ); hash_len =
unhexify(hash, hash_str);
690 memset( sig, 0,
sizeof( sig ) ); sig_len =
unhexify(sig, sig_str);
691 memset( key, 0,
sizeof( key ) ); key_len =
unhexify(key, key_str);
700 key, key_len ) == 0 );
703 hash, hash_len, sig, sig_len ) == ret );
710 void test_suite_pk_sign_verify(
int type,
int sign_ret,
int verify_ret )
713 unsigned char hash[50], sig[5000];
718 memset( hash, 0x2a,
sizeof hash );
719 memset( sig, 0,
sizeof sig );
728 hash,
sizeof hash, sig, sig_len ) == verify_ret );
734 #ifdef POLARSSL_RSA_C
735 void test_suite_pk_rsa_encrypt_test_vec(
char *message_hex,
int mod,
736 int radix_N,
char *input_N,
737 int radix_E,
char *input_E,
738 char *result_hex,
int ret )
740 unsigned char message[1000];
741 unsigned char output[1000];
742 unsigned char result[1000];
743 size_t msg_len, olen, res_len;
749 memset( message, 0,
sizeof( message ) );
750 memset( output, 0,
sizeof( output ) );
751 memset( result, 0,
sizeof( result ) );
753 msg_len =
unhexify( message, message_hex );
754 res_len =
unhexify( result, result_hex );
765 output, &olen,
sizeof( output ),
768 TEST_ASSERT( memcmp( output, result, olen ) == 0 );
775 #ifdef POLARSSL_RSA_C
776 void test_suite_pk_rsa_decrypt_test_vec(
char *cipher_hex,
int mod,
777 int radix_P,
char *input_P,
778 int radix_Q,
char *input_Q,
779 int radix_N,
char *input_N,
780 int radix_E,
char *input_E,
781 char *clear_hex,
int ret )
783 unsigned char clear[1000];
784 unsigned char output[1000];
785 unsigned char cipher[1000];
786 size_t clear_len, olen, cipher_len;
796 memset( clear, 0,
sizeof( clear ) );
797 memset( cipher, 0,
sizeof( cipher ) );
799 clear_len =
unhexify( clear, clear_hex );
800 cipher_len =
unhexify( cipher, cipher_hex );
824 memset( output, 0,
sizeof( output ) );
827 output, &olen,
sizeof( output ),
841 void test_suite_pk_ec_nocrypt(
int type )
844 unsigned char output[100];
845 unsigned char input[100];
853 memset( output, 0,
sizeof( output ) );
854 memset( input, 0,
sizeof( input ) );
859 output, &olen,
sizeof( output ),
863 output, &olen,
sizeof( output ),
870 #ifdef POLARSSL_RSA_C
871 void test_suite_pk_rsa_alt( )
881 unsigned char hash[50], sig[1000];
882 unsigned char msg[50], ciph[1000], test[1000];
883 size_t sig_len, ciph_len, test_len;
889 memset( hash, 0x2a,
sizeof hash );
890 memset( sig, 0,
sizeof sig );
891 memset( msg, 0x2a,
sizeof msg );
892 memset( ciph, 0,
sizeof ciph );
893 memset( test, 0,
sizeof test );
919 hash,
sizeof hash, sig, sig_len ) == 0 );
923 ciph, &ciph_len,
sizeof ciph,
926 test, &test_len,
sizeof test,
933 ciph, &ciph_len,
sizeof ciph,
936 hash,
sizeof hash, sig, sig_len ) == ret );
954 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
956 #if defined(POLARSSL_SHA1_C)
962 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
964 #if defined(POLARSSL_ECP_C)
970 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
972 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
978 if( strcmp( str,
"POLARSSL_ECDSA_C" ) == 0 )
980 #if defined(POLARSSL_ECDSA_C)
986 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
988 #if defined(POLARSSL_RSA_C)
994 if( strcmp( str,
"POLARSSL_PKCS1_V21" ) == 0 )
996 #if defined(POLARSSL_PKCS1_V21)
1002 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
1004 #if defined(POLARSSL_SHA256_C)
1010 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
1012 #if defined(POLARSSL_PKCS1_V15)
1018 if( strcmp( str,
"POLARSSL_GENPRIME" ) == 0 )
1020 #if defined(POLARSSL_GENPRIME)
1037 #if defined(TEST_SUITE_ACTIVE)
1038 if( strcmp( params[0],
"pk_utils" ) == 0 )
1044 char *param4 = params[4];
1048 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1052 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1053 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1054 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1057 test_suite_pk_utils( param1, param2, param3, param4 );
1063 if( strcmp( params[0],
"pk_rsa_verify_test_vec" ) == 0 )
1065 #ifdef POLARSSL_RSA_C
1067 char *param1 = params[1];
1071 char *param5 = params[5];
1073 char *param7 = params[7];
1074 char *param8 = params[8];
1079 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1084 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1085 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1086 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1088 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1091 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1093 test_suite_pk_rsa_verify_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1100 if( strcmp( params[0],
"pk_rsa_verify_ext_test_vec" ) == 0 )
1102 #ifdef POLARSSL_RSA_C
1104 char *param1 = params[1];
1108 char *param5 = params[5];
1110 char *param7 = params[7];
1111 char *param8 = params[8];
1119 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1124 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1125 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1126 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1128 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1131 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1132 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1133 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1134 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1136 test_suite_pk_rsa_verify_ext_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1143 if( strcmp( params[0],
"pk_ec_test_vec" ) == 0 )
1145 #ifdef POLARSSL_ECDSA_C
1149 char *param3 = params[3];
1150 char *param4 = params[4];
1151 char *param5 = params[5];
1156 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1160 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1161 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1165 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1167 test_suite_pk_ec_test_vec( param1, param2, param3, param4, param5, param6 );
1174 if( strcmp( params[0],
"pk_sign_verify" ) == 0 )
1183 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1187 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1188 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1189 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1191 test_suite_pk_sign_verify( param1, param2, param3 );
1197 if( strcmp( params[0],
"pk_rsa_encrypt_test_vec" ) == 0 )
1199 #ifdef POLARSSL_RSA_C
1201 char *param1 = params[1];
1204 char *param4 = params[4];
1206 char *param6 = params[6];
1207 char *param7 = params[7];
1212 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1217 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1218 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1220 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1223 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1225 test_suite_pk_rsa_encrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8 );
1232 if( strcmp( params[0],
"pk_rsa_decrypt_test_vec" ) == 0 )
1234 #ifdef POLARSSL_RSA_C
1236 char *param1 = params[1];
1239 char *param4 = params[4];
1241 char *param6 = params[6];
1243 char *param8 = params[8];
1245 char *param10 = params[10];
1246 char *param11 = params[11];
1251 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1256 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1257 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1259 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1261 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1263 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1266 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1268 test_suite_pk_rsa_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1275 if( strcmp( params[0],
"pk_ec_nocrypt" ) == 0 )
1282 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1286 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1288 test_suite_pk_ec_nocrypt( param1 );
1294 if( strcmp( params[0],
"pk_rsa_alt" ) == 0 )
1296 #ifdef POLARSSL_RSA_C
1301 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1306 test_suite_pk_rsa_alt( );
1315 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1329 ret = fgets( buf, len, f );
1333 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1334 buf[strlen(buf) - 1] =
'\0';
1335 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1336 buf[strlen(buf) - 1] =
'\0';
1347 params[cnt++] = cur;
1349 while( *p !=
'\0' && p < buf + len )
1359 if( p + 1 < buf + len )
1362 params[cnt++] = cur;
1371 for( i = 0; i < cnt; i++ )
1378 if( *p ==
'\\' && *(p + 1) ==
'n' )
1383 else if( *p ==
'\\' && *(p + 1) ==
':' )
1388 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1404 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1405 const char *filename =
"/home/users/builder/rpm/BUILD/polarssl-1.3.9/tests/suites/test_suite_pk.data";
1410 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1411 unsigned char alloc_buf[1000000];
1415 file = fopen( filename,
"r" );
1418 fprintf( stderr,
"Failed to open\n" );
1422 while( !feof( file ) )
1426 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1428 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1429 fprintf( stdout,
" " );
1430 for( i = strlen( buf ) + 1; i < 67; i++ )
1431 fprintf( stdout,
"." );
1432 fprintf( stdout,
" " );
1437 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1441 if( strcmp( params[0],
"depends_on" ) == 0 )
1443 for( i = 1; i < cnt; i++ )
1447 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1458 if( skip == 1 || ret == 3 )
1461 fprintf( stdout,
"----\n" );
1464 else if( ret == 0 && test_errors == 0 )
1466 fprintf( stdout,
"PASS\n" );
1471 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1478 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1480 if( strlen(buf) != 0 )
1482 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1488 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1489 if( total_errors == 0 )
1490 fprintf( stdout,
"PASSED" );
1492 fprintf( stdout,
"FAILED" );
1494 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1495 total_tests - total_errors, total_tests, total_skipped );
1497 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1498 #if defined(POLARSSL_MEMORY_DEBUG)
1499 memory_buffer_alloc_status();
1504 return( total_errors != 0 );