1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_BIGNUM_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_BIGNUM_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_MPI_BAD_INPUT_DATA" ) == 0 )
399 if( strcmp( str,
"-3" ) == 0 )
404 if( strcmp( str,
"-1" ) == 0 )
409 if( strcmp( str,
"POLARSSL_ERR_MPI_DIVISION_BY_ZERO" ) == 0 )
414 if( strcmp( str,
"POLARSSL_ERR_MPI_NEGATIVE_VALUE" ) == 0 )
419 if( strcmp( str,
"+1" ) == 0 )
424 if( strcmp( str,
"-34" ) == 0 )
429 if( strcmp( str,
"POLARSSL_ERR_MPI_INVALID_CHARACTER" ) == 0 )
434 if( strcmp( str,
"POLARSSL_ERR_MPI_BUFFER_TOO_SMALL" ) == 0 )
439 if( strcmp( str,
"POLARSSL_ERR_MPI_NOT_ACCEPTABLE" ) == 0 )
444 if( strcmp( str,
"-2" ) == 0 )
449 if( strcmp( str,
"-13" ) == 0 )
454 if( strcmp( str,
"-9871232" ) == 0 )
456 *value = ( -9871232 );
459 #ifdef POLARSSL_FS_IO
460 if( strcmp( str,
"POLARSSL_ERR_MPI_FILE_IO_ERROR" ) == 0 )
465 #endif // POLARSSL_FS_IO
468 printf(
"Expected integer for parameter and got: %s\n", str );
472 void test_suite_mpi_read_write_string(
int radix_X,
char *input_X,
int radix_A,
473 char *input_A,
int output_size,
int result_read,
478 size_t len = output_size;
483 if( result_read == 0 )
486 if( result_write == 0 )
496 void test_suite_mpi_read_binary(
char *input_X,
int radix_A,
char *input_A )
499 unsigned char str[1000];
500 unsigned char buf[1000];
506 input_len =
unhexify( buf, input_X );
510 TEST_ASSERT( strcmp( (
char *) str, input_A ) == 0 );
516 void test_suite_mpi_write_binary(
int radix_X,
char *input_X,
char *input_A,
517 int output_size,
int result )
520 unsigned char str[1000];
521 unsigned char buf[1000];
524 memset( buf, 0x00, 1000 );
525 memset( str, 0x00, 1000 );
532 if( buflen > (
size_t) output_size )
533 buflen = (size_t) output_size;
538 hexify( str, buf, buflen );
540 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
547 #ifdef POLARSSL_FS_IO
548 void test_suite_mpi_read_file(
int radix_X,
char *input_file,
char *input_A,
552 unsigned char str[1000];
553 unsigned char buf[1000];
557 memset( buf, 0x00, 1000 );
558 memset( str, 0x00, 1000 );
562 file = fopen( input_file,
"r" );
572 hexify( str, buf, buflen );
574 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
582 #ifdef POLARSSL_FS_IO
583 void test_suite_mpi_write_file(
int radix_X,
char *input_X,
int output_radix,
587 FILE *file_out, *file_in;
593 file_out = fopen( output_file,
"w" );
598 file_in = fopen( output_file,
"r" );
610 void test_suite_mpi_get_bit(
int radix_X,
char *input_X,
int pos,
int val )
621 void test_suite_mpi_set_bit(
int radix_X,
char *input_X,
int pos,
int val,
int radix_Y,
636 void test_suite_mpi_lsb(
int radix_X,
char *input_X,
int nr_bits )
648 void test_suite_mpi_msb(
int radix_X,
char *input_X,
int nr_bits )
660 void test_suite_mpi_gcd(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
661 int radix_A,
char *input_A )
676 void test_suite_mpi_cmp_int(
int input_X,
int input_A,
int result_CMP )
688 void test_suite_mpi_cmp_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
702 void test_suite_mpi_cmp_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
716 void test_suite_mpi_copy(
int input_X,
int input_A )
734 void test_suite_mpi_copy_self(
int input_X )
747 void test_suite_mpi_shrink(
int before,
int used,
int min,
int after )
754 memset( X.
p, 0x2a, used *
sizeof(
t_uint ) );
762 void test_suite_mpi_safe_cond_assign(
int x_sign,
char *x_str,
763 int y_sign,
char *y_str )
784 void test_suite_mpi_safe_cond_swap(
int x_sign,
char *x_str,
785 int y_sign,
char *y_str )
813 void test_suite_mpi_swap(
int input_X,
int input_Y )
831 void test_suite_mpi_add_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
832 int radix_A,
char *input_A )
847 void test_suite_mpi_add_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
848 int radix_A,
char *input_A )
863 void test_suite_mpi_add_abs_add_first(
int radix_X,
char *input_X,
int radix_Y,
864 char *input_Y,
int radix_A,
char *input_A )
879 void test_suite_mpi_add_abs_add_second(
int radix_X,
char *input_X,
int radix_Y,
880 char *input_Y,
int radix_A,
char *input_A )
895 void test_suite_mpi_add_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
910 void test_suite_mpi_sub_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
911 int radix_A,
char *input_A )
926 void test_suite_mpi_sub_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
927 int radix_A,
char *input_A,
int sub_result )
946 void test_suite_mpi_sub_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
961 void test_suite_mpi_mul_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
962 int radix_A,
char *input_A )
977 void test_suite_mpi_mul_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
978 char *input_A,
char *result_comparison )
986 if( strcmp( result_comparison,
"==" ) == 0 )
988 else if( strcmp( result_comparison,
"!=" ) == 0 )
997 void test_suite_mpi_div_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
998 int radix_A,
char *input_A,
int radix_B,
char *input_B,
1001 mpi X, Y, Q, R, A, B;
1023 void test_suite_mpi_div_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
1024 char *input_A,
int radix_B,
char *input_B,
int div_result )
1047 void test_suite_mpi_mod_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
1048 int radix_A,
char *input_A,
int div_result )
1068 void test_suite_mpi_mod_int(
int radix_X,
char *input_X,
int input_Y,
int input_A,
1088 void test_suite_mpi_exp_mod(
int radix_A,
char *input_A,
int radix_E,
char *input_E,
1089 int radix_N,
char *input_N,
int radix_RR,
char *input_RR,
1090 int radix_X,
char *input_X,
int div_result )
1092 mpi A, E, N, RR, Z, X;
1102 if( strlen( input_RR ) )
1117 void test_suite_mpi_inv_mod(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
1118 int radix_A,
char *input_A,
int div_result )
1138 #ifdef POLARSSL_GENPRIME
1139 void test_suite_mpi_is_prime(
int radix_X,
char *input_X,
int div_result )
1154 #ifdef POLARSSL_GENPRIME
1155 void test_suite_mpi_gen_prime(
int bits,
int safe,
int ref_ret )
1167 size_t actual_bits =
mpi_msb( &X );
1185 void test_suite_mpi_shift_l(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1200 void test_suite_mpi_shift_r(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1215 #ifdef POLARSSL_SELF_TEST
1216 void test_suite_mpi_selftest()
1234 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
1236 #if defined(POLARSSL_SELF_TEST)
1242 if( strcmp( str,
"POLARSSL_GENPRIME" ) == 0 )
1244 #if defined(POLARSSL_GENPRIME)
1261 #if defined(TEST_SUITE_ACTIVE)
1262 if( strcmp( params[0],
"mpi_read_write_string" ) == 0 )
1266 char *param2 = params[2];
1268 char *param4 = params[4];
1275 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1279 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1281 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1283 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1284 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1285 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1287 test_suite_mpi_read_write_string( param1, param2, param3, param4, param5, param6, param7 );
1293 if( strcmp( params[0],
"mpi_read_binary" ) == 0 )
1296 char *param1 = params[1];
1298 char *param3 = params[3];
1302 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1307 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1310 test_suite_mpi_read_binary( param1, param2, param3 );
1316 if( strcmp( params[0],
"mpi_write_binary" ) == 0 )
1320 char *param2 = params[2];
1321 char *param3 = params[3];
1327 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1331 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1334 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1335 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1337 test_suite_mpi_write_binary( param1, param2, param3, param4, param5 );
1343 if( strcmp( params[0],
"mpi_read_file" ) == 0 )
1345 #ifdef POLARSSL_FS_IO
1348 char *param2 = params[2];
1349 char *param3 = params[3];
1354 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1358 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1361 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1363 test_suite_mpi_read_file( param1, param2, param3, param4 );
1370 if( strcmp( params[0],
"mpi_write_file" ) == 0 )
1372 #ifdef POLARSSL_FS_IO
1375 char *param2 = params[2];
1377 char *param4 = params[4];
1381 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1385 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1387 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1390 test_suite_mpi_write_file( param1, param2, param3, param4 );
1397 if( strcmp( params[0],
"mpi_get_bit" ) == 0 )
1401 char *param2 = params[2];
1407 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1411 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1413 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1414 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1416 test_suite_mpi_get_bit( param1, param2, param3, param4 );
1422 if( strcmp( params[0],
"mpi_set_bit" ) == 0 )
1426 char *param2 = params[2];
1430 char *param6 = params[6];
1434 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1438 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1440 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1441 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1442 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1445 test_suite_mpi_set_bit( param1, param2, param3, param4, param5, param6 );
1451 if( strcmp( params[0],
"mpi_lsb" ) == 0 )
1455 char *param2 = params[2];
1460 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1464 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1466 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1468 test_suite_mpi_lsb( param1, param2, param3 );
1474 if( strcmp( params[0],
"mpi_msb" ) == 0 )
1478 char *param2 = params[2];
1483 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1487 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1489 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1491 test_suite_mpi_msb( param1, param2, param3 );
1497 if( strcmp( params[0],
"mpi_gcd" ) == 0 )
1501 char *param2 = params[2];
1503 char *param4 = params[4];
1505 char *param6 = params[6];
1509 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1513 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1515 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1517 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1520 test_suite_mpi_gcd( param1, param2, param3, param4, param5, param6 );
1526 if( strcmp( params[0],
"mpi_cmp_int" ) == 0 )
1535 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1539 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1540 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1541 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1543 test_suite_mpi_cmp_int( param1, param2, param3 );
1549 if( strcmp( params[0],
"mpi_cmp_mpi" ) == 0 )
1553 char *param2 = params[2];
1555 char *param4 = params[4];
1560 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1564 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1566 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1568 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1570 test_suite_mpi_cmp_mpi( param1, param2, param3, param4, param5 );
1576 if( strcmp( params[0],
"mpi_cmp_abs" ) == 0 )
1580 char *param2 = params[2];
1582 char *param4 = params[4];
1587 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1591 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1593 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1595 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1597 test_suite_mpi_cmp_abs( param1, param2, param3, param4, param5 );
1603 if( strcmp( params[0],
"mpi_copy" ) == 0 )
1611 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1615 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1616 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1618 test_suite_mpi_copy( param1, param2 );
1624 if( strcmp( params[0],
"mpi_copy_self" ) == 0 )
1631 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1635 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1637 test_suite_mpi_copy_self( param1 );
1643 if( strcmp( params[0],
"mpi_shrink" ) == 0 )
1653 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1657 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1658 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1659 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1660 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1662 test_suite_mpi_shrink( param1, param2, param3, param4 );
1668 if( strcmp( params[0],
"mpi_safe_cond_assign" ) == 0 )
1672 char *param2 = params[2];
1674 char *param4 = params[4];
1678 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1682 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1684 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1687 test_suite_mpi_safe_cond_assign( param1, param2, param3, param4 );
1693 if( strcmp( params[0],
"mpi_safe_cond_swap" ) == 0 )
1697 char *param2 = params[2];
1699 char *param4 = params[4];
1703 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1707 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1709 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1712 test_suite_mpi_safe_cond_swap( param1, param2, param3, param4 );
1718 if( strcmp( params[0],
"mpi_swap" ) == 0 )
1726 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1730 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1731 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1733 test_suite_mpi_swap( param1, param2 );
1739 if( strcmp( params[0],
"mpi_add_mpi" ) == 0 )
1743 char *param2 = params[2];
1745 char *param4 = params[4];
1747 char *param6 = params[6];
1751 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1755 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1757 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1759 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1762 test_suite_mpi_add_mpi( param1, param2, param3, param4, param5, param6 );
1768 if( strcmp( params[0],
"mpi_add_abs" ) == 0 )
1772 char *param2 = params[2];
1774 char *param4 = params[4];
1776 char *param6 = params[6];
1780 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1784 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1786 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1788 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1791 test_suite_mpi_add_abs( param1, param2, param3, param4, param5, param6 );
1797 if( strcmp( params[0],
"mpi_add_abs_add_first" ) == 0 )
1801 char *param2 = params[2];
1803 char *param4 = params[4];
1805 char *param6 = params[6];
1809 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1813 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1815 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1817 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1820 test_suite_mpi_add_abs_add_first( param1, param2, param3, param4, param5, param6 );
1826 if( strcmp( params[0],
"mpi_add_abs_add_second" ) == 0 )
1830 char *param2 = params[2];
1832 char *param4 = params[4];
1834 char *param6 = params[6];
1838 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1842 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1844 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1846 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1849 test_suite_mpi_add_abs_add_second( param1, param2, param3, param4, param5, param6 );
1855 if( strcmp( params[0],
"mpi_add_int" ) == 0 )
1859 char *param2 = params[2];
1862 char *param5 = params[5];
1866 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1870 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1872 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1873 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1876 test_suite_mpi_add_int( param1, param2, param3, param4, param5 );
1882 if( strcmp( params[0],
"mpi_sub_mpi" ) == 0 )
1886 char *param2 = params[2];
1888 char *param4 = params[4];
1890 char *param6 = params[6];
1894 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1898 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1900 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1902 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1905 test_suite_mpi_sub_mpi( param1, param2, param3, param4, param5, param6 );
1911 if( strcmp( params[0],
"mpi_sub_abs" ) == 0 )
1915 char *param2 = params[2];
1917 char *param4 = params[4];
1919 char *param6 = params[6];
1924 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1928 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1930 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1932 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1934 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1936 test_suite_mpi_sub_abs( param1, param2, param3, param4, param5, param6, param7 );
1942 if( strcmp( params[0],
"mpi_sub_int" ) == 0 )
1946 char *param2 = params[2];
1949 char *param5 = params[5];
1953 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1957 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1959 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1960 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1963 test_suite_mpi_sub_int( param1, param2, param3, param4, param5 );
1969 if( strcmp( params[0],
"mpi_mul_mpi" ) == 0 )
1973 char *param2 = params[2];
1975 char *param4 = params[4];
1977 char *param6 = params[6];
1981 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1985 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1987 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1989 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1992 test_suite_mpi_mul_mpi( param1, param2, param3, param4, param5, param6 );
1998 if( strcmp( params[0],
"mpi_mul_int" ) == 0 )
2002 char *param2 = params[2];
2005 char *param5 = params[5];
2006 char *param6 = params[6];
2010 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
2014 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2016 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2017 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2021 test_suite_mpi_mul_int( param1, param2, param3, param4, param5, param6 );
2027 if( strcmp( params[0],
"mpi_div_mpi" ) == 0 )
2031 char *param2 = params[2];
2033 char *param4 = params[4];
2035 char *param6 = params[6];
2037 char *param8 = params[8];
2042 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
2046 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2048 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2050 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
2052 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
2054 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
2056 test_suite_mpi_div_mpi( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
2062 if( strcmp( params[0],
"mpi_div_int" ) == 0 )
2066 char *param2 = params[2];
2069 char *param5 = params[5];
2071 char *param7 = params[7];
2076 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
2080 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2082 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2083 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2085 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
2087 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
2089 test_suite_mpi_div_int( param1, param2, param3, param4, param5, param6, param7, param8 );
2095 if( strcmp( params[0],
"mpi_mod_mpi" ) == 0 )
2099 char *param2 = params[2];
2101 char *param4 = params[4];
2103 char *param6 = params[6];
2108 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
2112 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2114 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2116 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
2118 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
2120 test_suite_mpi_mod_mpi( param1, param2, param3, param4, param5, param6, param7 );
2126 if( strcmp( params[0],
"mpi_mod_int" ) == 0 )
2130 char *param2 = params[2];
2137 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2141 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2143 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2144 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2145 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
2147 test_suite_mpi_mod_int( param1, param2, param3, param4, param5 );
2153 if( strcmp( params[0],
"mpi_exp_mod" ) == 0 )
2157 char *param2 = params[2];
2159 char *param4 = params[4];
2161 char *param6 = params[6];
2163 char *param8 = params[8];
2165 char *param10 = params[10];
2170 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
2174 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2176 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2178 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
2180 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
2182 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
2184 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
2186 test_suite_mpi_exp_mod( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
2192 if( strcmp( params[0],
"mpi_inv_mod" ) == 0 )
2196 char *param2 = params[2];
2198 char *param4 = params[4];
2200 char *param6 = params[6];
2205 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
2209 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2211 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2213 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
2215 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
2217 test_suite_mpi_inv_mod( param1, param2, param3, param4, param5, param6, param7 );
2223 if( strcmp( params[0],
"mpi_is_prime" ) == 0 )
2225 #ifdef POLARSSL_GENPRIME
2228 char *param2 = params[2];
2233 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
2237 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2239 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2241 test_suite_mpi_is_prime( param1, param2, param3 );
2248 if( strcmp( params[0],
"mpi_gen_prime" ) == 0 )
2250 #ifdef POLARSSL_GENPRIME
2258 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
2262 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2263 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
2264 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2266 test_suite_mpi_gen_prime( param1, param2, param3 );
2273 if( strcmp( params[0],
"mpi_shift_l" ) == 0 )
2277 char *param2 = params[2];
2280 char *param5 = params[5];
2284 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2288 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2290 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2291 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2294 test_suite_mpi_shift_l( param1, param2, param3, param4, param5 );
2300 if( strcmp( params[0],
"mpi_shift_r" ) == 0 )
2304 char *param2 = params[2];
2307 char *param5 = params[5];
2311 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2315 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2317 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2318 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2321 test_suite_mpi_shift_r( param1, param2, param3, param4, param5 );
2327 if( strcmp( params[0],
"mpi_selftest" ) == 0 )
2329 #ifdef POLARSSL_SELF_TEST
2334 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
2339 test_suite_mpi_selftest( );
2348 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
2362 ret = fgets( buf, len, f );
2366 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
2367 buf[strlen(buf) - 1] =
'\0';
2368 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
2369 buf[strlen(buf) - 1] =
'\0';
2380 params[cnt++] = cur;
2382 while( *p !=
'\0' && p < buf + len )
2392 if( p + 1 < buf + len )
2395 params[cnt++] = cur;
2404 for( i = 0; i < cnt; i++ )
2411 if( *p ==
'\\' && *(p + 1) ==
'n' )
2416 else if( *p ==
'\\' && *(p + 1) ==
':' )
2421 else if( *p ==
'\\' && *(p + 1) ==
'?' )
2437 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
2438 const char *filename =
"/tmp/B.HYlHIA/BUILD/polarssl-1.3.9/tests/suites/test_suite_mpi.data";
2443 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2444 unsigned char alloc_buf[1000000];
2448 file = fopen( filename,
"r" );
2451 fprintf( stderr,
"Failed to open\n" );
2455 while( !feof( file ) )
2459 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2461 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
2462 fprintf( stdout,
" " );
2463 for( i = strlen( buf ) + 1; i < 67; i++ )
2464 fprintf( stdout,
"." );
2465 fprintf( stdout,
" " );
2470 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2474 if( strcmp( params[0],
"depends_on" ) == 0 )
2476 for( i = 1; i < cnt; i++ )
2480 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2491 if( skip == 1 || ret == 3 )
2494 fprintf( stdout,
"----\n" );
2499 fprintf( stdout,
"PASS\n" );
2504 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
2511 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2513 if( strlen(buf) != 0 )
2515 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
2521 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
2522 if( total_errors == 0 )
2523 fprintf( stdout,
"PASSED" );
2525 fprintf( stdout,
"FAILED" );
2527 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
2528 total_tests - total_errors, total_tests, total_skipped );
2530 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2531 #if defined(POLARSSL_MEMORY_DEBUG)
2532 memory_buffer_alloc_status();
2537 return( total_errors != 0 );