7 #define POLARSSL_ECP_PF_UNKNOWN -1
11 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 typedef UINT32 uint32_t;
30 #define GET_UINT32_BE(n,b,i) \
32 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
33 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
34 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
35 | ( (uint32_t) (b)[(i) + 3] ); \
40 #define PUT_UINT32_BE(n,b,i) \
42 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
43 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
44 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
45 (b)[(i) + 3] = (unsigned char) ( (n) ); \
49 static int unhexify(
unsigned char *obuf,
const char *ibuf)
52 int len = strlen(ibuf) / 2;
53 assert(!(strlen(ibuf) %1));
58 if( c >=
'0' && c <=
'9' )
60 else if( c >=
'a' && c <=
'f' )
62 else if( c >=
'A' && c <=
'F' )
68 if( c2 >=
'0' && c2 <=
'9' )
70 else if( c2 >=
'a' && c2 <=
'f' )
72 else if( c2 >=
'A' && c2 <=
'F' )
77 *obuf++ = ( c << 4 ) | c2;
83 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
95 *obuf++ =
'a' + h - 10;
100 *obuf++ =
'a' + l - 10;
116 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
120 if( rng_state != NULL )
123 for( i = 0; i < len; ++i )
134 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
136 if( rng_state != NULL )
139 memset( output, 0, len );
166 if( rng_state == NULL )
175 memcpy( output, info->
buf, use_len );
176 info->
buf += use_len;
180 if( len - use_len > 0 )
181 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
210 uint32_t i, *k, sum, delta=0x9E3779B9;
211 unsigned char result[4];
213 if( rng_state == NULL )
220 size_t use_len = ( len > 4 ) ? 4 : len;
223 for( i = 0; i < 32; i++ )
225 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
227 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
231 memcpy( output, result, use_len );
247 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
250 const char *ibuf = in;
252 assert( len == strlen(ibuf) / 2 );
253 assert(!(strlen(ibuf) %1));
255 obuf = out + (len - 1);
259 if( c >=
'0' && c <=
'9' )
261 else if( c >=
'a' && c <=
'f' )
263 else if( c >=
'A' && c <=
'F' )
269 if( c2 >=
'0' && c2 <=
'9' )
271 else if( c2 >=
'a' && c2 <=
'f' )
273 else if( c2 >=
'A' && c2 <=
'F' )
278 *obuf-- = ( c << 4 ) | c2;
290 #ifdef POLARSSL_ECP_C
292 #define TEST_SUITE_ACTIVE
301 printf(
"FAILED\n" );
302 printf(
" %s\n", test );
307 #define TEST_ASSERT( TEST ) \
308 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
309 if( test_errors) return; \
314 if( (*str)[0] !=
'"' ||
315 (*str)[strlen( *str ) - 1] !=
'"' )
317 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
322 (*str)[strlen( *str ) - 1] =
'\0';
334 for( i = 0; i < strlen( str ); i++ )
336 if( i == 0 && str[i] ==
'-' )
342 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
343 str[i - 1] ==
'0' && str[i] ==
'x' )
349 if( str[i] <
'0' || str[i] >
'9' )
359 *value = strtol( str, NULL, 16 );
361 *value = strtol( str, NULL, 10 );
366 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
371 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
376 if( strcmp( str,
"POLARSSL_ECP_PF_UNCOMPRESSED" ) == 0 )
381 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
386 if( strcmp( str,
"POLARSSL_ECP_DP_BP256R1" ) == 0 )
391 if( strcmp( str,
"POLARSSL_ECP_PF_COMPRESSED" ) == 0 )
396 if( strcmp( str,
"POLARSSL_ERR_ECP_BUFFER_TOO_SMALL" ) == 0 )
401 if( strcmp( str,
"POLARSSL_ERR_ECP_INVALID_KEY" ) == 0 )
406 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
411 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
416 if( strcmp( str,
"POLARSSL_ECP_PF_UNKNOWN" ) == 0 )
418 *value = ( POLARSSL_ECP_PF_UNKNOWN );
421 if( strcmp( str,
"POLARSSL_ECP_DP_BP512R1" ) == 0 )
426 if( strcmp( str,
"POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE" ) == 0 )
431 if( strcmp( str,
"POLARSSL_ERR_ECP_BAD_INPUT_DATA" ) == 0 )
436 if( strcmp( str,
"POLARSSL_ECP_DP_BP384R1" ) == 0 )
441 if( strcmp( str,
"-1" ) == 0 )
448 printf(
"Expected integer for parameter and got: %s\n", str );
452 void test_suite_ecp_small_add(
int a_zero,
char *x_a,
char *y_a,
int b_zero,
char *x_b,
453 char *y_b,
int c_zero,
int x_c,
int y_c )
462 "47",
"4",
"17",
"42",
"13" ) == 0 );
498 void test_suite_ecp_small_sub(
int a_zero,
char *x_a,
char *y_a,
int b_zero,
char *x_b,
499 char *y_b,
int c_zero,
int x_c,
int y_c )
508 "47",
"4",
"17",
"42",
"13" ) == 0 );
534 void test_suite_ecp_small_mul(
int m_str,
int r_zero,
int x_r,
int y_r,
int ret )
547 "47",
"4",
"17",
"42",
"13" ) == 0 );
580 void test_suite_ecp_small_check_pub(
int x,
int y,
int z,
int ret )
589 "47",
"4",
"17",
"42",
"13" ) == 0 );
601 void test_suite_ecp_test_vect(
int id,
char *dA_str,
char *xA_str,
char *yA_str,
602 char *dB_str,
char *xB_str,
char *yB_str,
char *xZ_str,
607 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
653 void test_suite_ecp_fast_mod(
int id,
char *N_str )
682 void test_suite_ecp_write_binary(
int id,
char *x,
char *y,
char *z,
int format,
683 char *out,
int blen,
int ret )
687 unsigned char buf[256], str[512];
690 memset( buf, 0,
sizeof( buf ) );
691 memset( str, 0,
sizeof( str ) );
702 &olen, buf, blen ) == ret );
707 TEST_ASSERT( strcasecmp( (
char *) str, out ) == 0 );
713 void test_suite_ecp_read_binary(
int id,
char *input,
char *x,
char *y,
char *z,
720 unsigned char buf[256];
722 memset( buf, 0,
sizeof( buf ) );
748 void test_suite_ecp_tls_read_point(
int id,
char *input,
char *x,
char *y,
char *z,
755 unsigned char buf[256];
756 const unsigned char *vbuf = buf;
758 memset( buf, 0,
sizeof( buf ) );
785 void test_suite_ecp_tls_write_read_point(
int id )
789 unsigned char buf[256];
790 const unsigned char *vbuf;
798 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
805 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
814 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
822 memset( buf, 0x00,
sizeof( buf ) ); vbuf = buf;
834 void test_suite_ecp_tls_read_group(
char *record,
int result,
int bits )
837 unsigned char buf[10];
838 const unsigned char *vbuf = buf;
842 memset( buf, 0x00,
sizeof( buf ) );
858 void test_suite_ecp_tls_write_read_group(
int id )
861 unsigned char buf[10];
862 const unsigned char *vbuf = buf;
868 memset( buf, 0x00,
sizeof( buf ) );
885 void test_suite_ecp_check_privkey(
int id )
905 void test_suite_ecp_gen_keypair(
int id )
930 #ifdef POLARSSL_SELF_TEST
931 void test_suite_ecp_selftest()
946 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
948 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
954 if( strcmp( str,
"POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
956 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
962 if( strcmp( str,
"POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
964 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
970 if( strcmp( str,
"POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
972 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
978 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
980 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
986 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
988 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
994 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
996 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
1002 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
1004 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1021 #if defined(TEST_SUITE_ACTIVE)
1022 if( strcmp( params[0],
"ecp_small_add" ) == 0 )
1026 char *param2 = params[2];
1027 char *param3 = params[3];
1029 char *param5 = params[5];
1030 char *param6 = params[6];
1037 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1041 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1044 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1047 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1048 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1049 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1051 test_suite_ecp_small_add( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1057 if( strcmp( params[0],
"ecp_small_sub" ) == 0 )
1061 char *param2 = params[2];
1062 char *param3 = params[3];
1064 char *param5 = params[5];
1065 char *param6 = params[6];
1072 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1076 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1079 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1082 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1083 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1084 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1086 test_suite_ecp_small_sub( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1092 if( strcmp( params[0],
"ecp_small_mul" ) == 0 )
1103 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1107 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1108 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1109 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1110 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1111 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1113 test_suite_ecp_small_mul( param1, param2, param3, param4, param5 );
1119 if( strcmp( params[0],
"ecp_small_check_pub" ) == 0 )
1129 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1133 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1134 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1135 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1136 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1138 test_suite_ecp_small_check_pub( param1, param2, param3, param4 );
1144 if( strcmp( params[0],
"ecp_test_vect" ) == 0 )
1148 char *param2 = params[2];
1149 char *param3 = params[3];
1150 char *param4 = params[4];
1151 char *param5 = params[5];
1152 char *param6 = params[6];
1153 char *param7 = params[7];
1154 char *param8 = params[8];
1155 char *param9 = params[9];
1159 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1163 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1173 test_suite_ecp_test_vect( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1179 if( strcmp( params[0],
"ecp_fast_mod" ) == 0 )
1183 char *param2 = params[2];
1187 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1191 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1194 test_suite_ecp_fast_mod( param1, param2 );
1200 if( strcmp( params[0],
"ecp_write_binary" ) == 0 )
1204 char *param2 = params[2];
1205 char *param3 = params[3];
1206 char *param4 = params[4];
1208 char *param6 = params[6];
1214 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1218 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1222 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1224 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1225 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1227 test_suite_ecp_write_binary( param1, param2, param3, param4, param5, param6, param7, param8 );
1233 if( strcmp( params[0],
"ecp_read_binary" ) == 0 )
1237 char *param2 = params[2];
1238 char *param3 = params[3];
1239 char *param4 = params[4];
1240 char *param5 = params[5];
1245 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1249 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1254 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1256 test_suite_ecp_read_binary( param1, param2, param3, param4, param5, param6 );
1262 if( strcmp( params[0],
"ecp_tls_read_point" ) == 0 )
1266 char *param2 = params[2];
1267 char *param3 = params[3];
1268 char *param4 = params[4];
1269 char *param5 = params[5];
1274 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1278 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1283 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1285 test_suite_ecp_tls_read_point( param1, param2, param3, param4, param5, param6 );
1291 if( strcmp( params[0],
"ecp_tls_write_read_point" ) == 0 )
1298 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1302 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1304 test_suite_ecp_tls_write_read_point( param1 );
1310 if( strcmp( params[0],
"ecp_tls_read_group" ) == 0 )
1313 char *param1 = params[1];
1319 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1324 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1325 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1327 test_suite_ecp_tls_read_group( param1, param2, param3 );
1333 if( strcmp( params[0],
"ecp_tls_write_read_group" ) == 0 )
1340 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1344 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1346 test_suite_ecp_tls_write_read_group( param1 );
1352 if( strcmp( params[0],
"ecp_check_privkey" ) == 0 )
1359 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1363 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1365 test_suite_ecp_check_privkey( param1 );
1371 if( strcmp( params[0],
"ecp_gen_keypair" ) == 0 )
1378 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1382 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1384 test_suite_ecp_gen_keypair( param1 );
1390 if( strcmp( params[0],
"ecp_selftest" ) == 0 )
1392 #ifdef POLARSSL_SELF_TEST
1397 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1402 test_suite_ecp_selftest( );
1411 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1425 ret = fgets( buf, len, f );
1429 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1430 buf[strlen(buf) - 1] =
'\0';
1431 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1432 buf[strlen(buf) - 1] =
'\0';
1443 params[cnt++] = cur;
1445 while( *p !=
'\0' && p < buf + len )
1455 if( p + 1 < buf + len )
1458 params[cnt++] = cur;
1467 for( i = 0; i < cnt; i++ )
1474 if( *p ==
'\\' && *(p + 1) ==
'n' )
1479 else if( *p ==
'\\' && *(p + 1) ==
':' )
1484 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1500 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1501 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_ecp.data";
1506 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1507 unsigned char alloc_buf[1000000];
1508 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1511 file = fopen( filename,
"r" );
1514 fprintf( stderr,
"Failed to open\n" );
1518 while( !feof( file ) )
1522 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1524 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1525 fprintf( stdout,
" " );
1526 for( i = strlen( buf ) + 1; i < 67; i++ )
1527 fprintf( stdout,
"." );
1528 fprintf( stdout,
" " );
1533 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1537 if( strcmp( params[0],
"depends_on" ) == 0 )
1539 for( i = 1; i < cnt; i++ )
1543 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1554 if( skip == 1 || ret == 3 )
1557 fprintf( stdout,
"----\n" );
1562 fprintf( stdout,
"PASS\n" );
1567 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1574 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1576 if( strlen(buf) != 0 )
1578 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1584 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1585 if( total_errors == 0 )
1586 fprintf( stdout,
"PASSED" );
1588 fprintf( stdout,
"FAILED" );
1590 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1591 total_tests - total_errors, total_tests, total_skipped );
1593 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1594 #if defined(POLARSSL_MEMORY_DEBUG)
1595 memory_buffer_alloc_status();
1597 memory_buffer_alloc_free();
1600 return( total_errors != 0 );