3 #ifdef POLARSSL_BIGNUM_C
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_BIGNUM_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,
"-13" ) == 0 )
369 if( strcmp( str,
"-34" ) == 0 )
374 if( strcmp( str,
"POLARSSL_ERR_MPI_NOT_ACCEPTABLE" ) == 0 )
379 if( strcmp( str,
"POLARSSL_ERR_MPI_BAD_INPUT_DATA" ) == 0 )
384 if( strcmp( str,
"-1" ) == 0 )
389 if( strcmp( str,
"-3" ) == 0 )
394 if( strcmp( str,
"-9871232" ) == 0 )
396 *value = ( -9871232 );
399 if( strcmp( str,
"POLARSSL_ERR_MPI_INVALID_CHARACTER" ) == 0 )
404 if( strcmp( str,
"POLARSSL_ERR_MPI_DIVISION_BY_ZERO" ) == 0 )
409 if( strcmp( str,
"POLARSSL_ERR_MPI_NEGATIVE_VALUE" ) == 0 )
414 if( strcmp( str,
"POLARSSL_ERR_MPI_BUFFER_TOO_SMALL" ) == 0 )
419 if( strcmp( str,
"POLARSSL_ERR_MPI_FILE_IO_ERROR" ) == 0 )
424 if( strcmp( str,
"-2" ) == 0 )
431 printf(
"Expected integer for parameter and got: %s\n", str );
435 void test_suite_mpi_read_write_string(
int radix_X,
char *input_X,
int radix_A,
436 char *input_A,
int output_size,
int result_read,
441 size_t len = output_size;
446 if( result_read == 0 )
449 if( result_write == 0 )
458 void test_suite_mpi_read_binary(
char *input_X,
int radix_A,
char *input_A )
461 unsigned char str[1000];
462 unsigned char buf[1000];
468 input_len =
unhexify( buf, input_X );
472 TEST_ASSERT( strcmp( (
char *) str, input_A ) == 0 );
477 void test_suite_mpi_write_binary(
int radix_X,
char *input_X,
char *input_A,
478 int output_size,
int result )
481 unsigned char str[1000];
482 unsigned char buf[1000];
485 memset( buf, 0x00, 1000 );
486 memset( str, 0x00, 1000 );
493 if( buflen > (
size_t) output_size )
494 buflen = (size_t) output_size;
499 hexify( str, buf, buflen );
501 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
507 #ifdef POLARSSL_FS_IO
508 void test_suite_mpi_read_file(
int radix_X,
char *input_file,
char *input_A,
512 unsigned char str[1000];
513 unsigned char buf[1000];
517 memset( buf, 0x00, 1000 );
518 memset( str, 0x00, 1000 );
522 file = fopen( input_file,
"r" );
531 hexify( str, buf, buflen );
533 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
540 #ifdef POLARSSL_FS_IO
541 void test_suite_mpi_write_file(
int radix_X,
char *input_X,
int output_radix,
545 FILE *file_out, *file_in;
551 file_out = fopen( output_file,
"w" );
556 file_in = fopen( output_file,
"r" );
567 void test_suite_mpi_get_bit(
int radix_X,
char *input_X,
int pos,
int val )
577 void test_suite_mpi_set_bit(
int radix_X,
char *input_X,
int pos,
int val,
int radix_Y,
591 void test_suite_mpi_lsb(
int radix_X,
char *input_X,
int nr_bits )
602 void test_suite_mpi_msb(
int radix_X,
char *input_X,
int nr_bits )
613 void test_suite_mpi_gcd(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
614 int radix_A,
char *input_A )
628 void test_suite_mpi_cmp_int(
int input_X,
int input_A,
int result_CMP )
639 void test_suite_mpi_cmp_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
652 void test_suite_mpi_cmp_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
665 void test_suite_mpi_copy(
int input_X,
int input_A )
682 void test_suite_mpi_copy_self(
int input_X )
694 void test_suite_mpi_swap(
int input_X,
int input_Y )
711 void test_suite_mpi_add_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
712 int radix_A,
char *input_A )
726 void test_suite_mpi_add_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
727 int radix_A,
char *input_A )
741 void test_suite_mpi_add_abs_add_first(
int radix_X,
char *input_X,
int radix_Y,
742 char *input_Y,
int radix_A,
char *input_A )
756 void test_suite_mpi_add_abs_add_second(
int radix_X,
char *input_X,
int radix_Y,
757 char *input_Y,
int radix_A,
char *input_A )
771 void test_suite_mpi_add_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
785 void test_suite_mpi_sub_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
786 int radix_A,
char *input_A )
800 void test_suite_mpi_sub_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
801 int radix_A,
char *input_A,
int sub_result )
819 void test_suite_mpi_sub_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
833 void test_suite_mpi_mul_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
834 int radix_A,
char *input_A )
848 void test_suite_mpi_mul_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
849 char *input_A,
char *result_comparison )
857 if( strcmp( result_comparison,
"==" ) == 0 )
859 else if( strcmp( result_comparison,
"!=" ) == 0 )
867 void test_suite_mpi_div_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
868 int radix_A,
char *input_A,
int radix_B,
char *input_B,
871 mpi X, Y, Q, R, A, B;
892 void test_suite_mpi_div_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
893 char *input_A,
int radix_B,
char *input_B,
int div_result )
915 void test_suite_mpi_mod_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
916 int radix_A,
char *input_A,
int div_result )
935 void test_suite_mpi_mod_int(
int radix_X,
char *input_X,
int input_Y,
int input_A,
954 void test_suite_mpi_exp_mod(
int radix_A,
char *input_A,
int radix_E,
char *input_E,
955 int radix_N,
char *input_N,
int radix_RR,
char *input_RR,
956 int radix_X,
char *input_X,
int div_result )
958 mpi A, E, N, RR, Z, X;
968 if( strlen( input_RR ) )
982 void test_suite_mpi_inv_mod(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
983 int radix_A,
char *input_A,
int div_result )
1002 #ifdef POLARSSL_GENPRIME
1003 void test_suite_mpi_is_prime(
int radix_X,
char *input_X,
int div_result )
1017 void test_suite_mpi_shift_l(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1031 void test_suite_mpi_shift_r(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1045 #ifdef POLARSSL_SELF_TEST
1046 void test_suite_mpi_selftest()
1061 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
1063 #if defined(POLARSSL_SELF_TEST)
1069 if( strcmp( str,
"POLARSSL_GENPRIME" ) == 0 )
1071 #if defined(POLARSSL_GENPRIME)
1088 #if defined(TEST_SUITE_ACTIVE)
1089 if( strcmp( params[0],
"mpi_read_write_string" ) == 0 )
1093 char *param2 = params[2];
1095 char *param4 = params[4];
1102 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1106 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1108 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1110 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1111 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1112 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1114 test_suite_mpi_read_write_string( param1, param2, param3, param4, param5, param6, param7 );
1120 if( strcmp( params[0],
"mpi_read_binary" ) == 0 )
1123 char *param1 = params[1];
1125 char *param3 = params[3];
1129 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1134 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1137 test_suite_mpi_read_binary( param1, param2, param3 );
1143 if( strcmp( params[0],
"mpi_write_binary" ) == 0 )
1147 char *param2 = params[2];
1148 char *param3 = params[3];
1154 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1158 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1161 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1162 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1164 test_suite_mpi_write_binary( param1, param2, param3, param4, param5 );
1170 if( strcmp( params[0],
"mpi_read_file" ) == 0 )
1172 #ifdef POLARSSL_FS_IO
1175 char *param2 = params[2];
1176 char *param3 = params[3];
1181 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1185 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1188 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1190 test_suite_mpi_read_file( param1, param2, param3, param4 );
1197 if( strcmp( params[0],
"mpi_write_file" ) == 0 )
1199 #ifdef POLARSSL_FS_IO
1202 char *param2 = params[2];
1204 char *param4 = params[4];
1208 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1212 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1214 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1217 test_suite_mpi_write_file( param1, param2, param3, param4 );
1224 if( strcmp( params[0],
"mpi_get_bit" ) == 0 )
1228 char *param2 = params[2];
1234 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1238 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1240 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1241 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1243 test_suite_mpi_get_bit( param1, param2, param3, param4 );
1249 if( strcmp( params[0],
"mpi_set_bit" ) == 0 )
1253 char *param2 = params[2];
1257 char *param6 = params[6];
1261 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1265 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1267 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1268 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1269 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1272 test_suite_mpi_set_bit( param1, param2, param3, param4, param5, param6 );
1278 if( strcmp( params[0],
"mpi_lsb" ) == 0 )
1282 char *param2 = params[2];
1287 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1291 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1293 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1295 test_suite_mpi_lsb( param1, param2, param3 );
1301 if( strcmp( params[0],
"mpi_msb" ) == 0 )
1305 char *param2 = params[2];
1310 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1314 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1316 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1318 test_suite_mpi_msb( param1, param2, param3 );
1324 if( strcmp( params[0],
"mpi_gcd" ) == 0 )
1328 char *param2 = params[2];
1330 char *param4 = params[4];
1332 char *param6 = params[6];
1336 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1340 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1342 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1344 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1347 test_suite_mpi_gcd( param1, param2, param3, param4, param5, param6 );
1353 if( strcmp( params[0],
"mpi_cmp_int" ) == 0 )
1362 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1366 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1367 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1368 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1370 test_suite_mpi_cmp_int( param1, param2, param3 );
1376 if( strcmp( params[0],
"mpi_cmp_mpi" ) == 0 )
1380 char *param2 = params[2];
1382 char *param4 = params[4];
1387 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1391 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1393 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1395 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1397 test_suite_mpi_cmp_mpi( param1, param2, param3, param4, param5 );
1403 if( strcmp( params[0],
"mpi_cmp_abs" ) == 0 )
1407 char *param2 = params[2];
1409 char *param4 = params[4];
1414 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1418 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1420 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1422 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1424 test_suite_mpi_cmp_abs( param1, param2, param3, param4, param5 );
1430 if( strcmp( params[0],
"mpi_copy" ) == 0 )
1438 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1442 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1443 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1445 test_suite_mpi_copy( param1, param2 );
1451 if( strcmp( params[0],
"mpi_copy_self" ) == 0 )
1458 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1462 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1464 test_suite_mpi_copy_self( param1 );
1470 if( strcmp( params[0],
"mpi_swap" ) == 0 )
1478 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1482 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1483 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1485 test_suite_mpi_swap( param1, param2 );
1491 if( strcmp( params[0],
"mpi_add_mpi" ) == 0 )
1495 char *param2 = params[2];
1497 char *param4 = params[4];
1499 char *param6 = params[6];
1503 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1507 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1509 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1511 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1514 test_suite_mpi_add_mpi( param1, param2, param3, param4, param5, param6 );
1520 if( strcmp( params[0],
"mpi_add_abs" ) == 0 )
1524 char *param2 = params[2];
1526 char *param4 = params[4];
1528 char *param6 = params[6];
1532 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1536 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1538 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1540 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1543 test_suite_mpi_add_abs( param1, param2, param3, param4, param5, param6 );
1549 if( strcmp( params[0],
"mpi_add_abs_add_first" ) == 0 )
1553 char *param2 = params[2];
1555 char *param4 = params[4];
1557 char *param6 = params[6];
1561 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1565 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1567 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1569 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1572 test_suite_mpi_add_abs_add_first( param1, param2, param3, param4, param5, param6 );
1578 if( strcmp( params[0],
"mpi_add_abs_add_second" ) == 0 )
1582 char *param2 = params[2];
1584 char *param4 = params[4];
1586 char *param6 = params[6];
1590 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1594 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1596 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1598 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1601 test_suite_mpi_add_abs_add_second( param1, param2, param3, param4, param5, param6 );
1607 if( strcmp( params[0],
"mpi_add_int" ) == 0 )
1611 char *param2 = params[2];
1614 char *param5 = params[5];
1618 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1622 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1624 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1625 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1628 test_suite_mpi_add_int( param1, param2, param3, param4, param5 );
1634 if( strcmp( params[0],
"mpi_sub_mpi" ) == 0 )
1638 char *param2 = params[2];
1640 char *param4 = params[4];
1642 char *param6 = params[6];
1646 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1650 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1652 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1654 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1657 test_suite_mpi_sub_mpi( param1, param2, param3, param4, param5, param6 );
1663 if( strcmp( params[0],
"mpi_sub_abs" ) == 0 )
1667 char *param2 = params[2];
1669 char *param4 = params[4];
1671 char *param6 = params[6];
1676 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1680 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1682 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1684 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1686 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1688 test_suite_mpi_sub_abs( param1, param2, param3, param4, param5, param6, param7 );
1694 if( strcmp( params[0],
"mpi_sub_int" ) == 0 )
1698 char *param2 = params[2];
1701 char *param5 = params[5];
1705 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1709 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1711 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1712 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1715 test_suite_mpi_sub_int( param1, param2, param3, param4, param5 );
1721 if( strcmp( params[0],
"mpi_mul_mpi" ) == 0 )
1725 char *param2 = params[2];
1727 char *param4 = params[4];
1729 char *param6 = params[6];
1733 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1737 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1739 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1741 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1744 test_suite_mpi_mul_mpi( param1, param2, param3, param4, param5, param6 );
1750 if( strcmp( params[0],
"mpi_mul_int" ) == 0 )
1754 char *param2 = params[2];
1757 char *param5 = params[5];
1758 char *param6 = params[6];
1762 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1766 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1768 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1769 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1773 test_suite_mpi_mul_int( param1, param2, param3, param4, param5, param6 );
1779 if( strcmp( params[0],
"mpi_div_mpi" ) == 0 )
1783 char *param2 = params[2];
1785 char *param4 = params[4];
1787 char *param6 = params[6];
1789 char *param8 = params[8];
1794 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1798 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1800 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1802 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1804 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1806 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1808 test_suite_mpi_div_mpi( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1814 if( strcmp( params[0],
"mpi_div_int" ) == 0 )
1818 char *param2 = params[2];
1821 char *param5 = params[5];
1823 char *param7 = params[7];
1828 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1832 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1834 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1835 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1837 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1839 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1841 test_suite_mpi_div_int( param1, param2, param3, param4, param5, param6, param7, param8 );
1847 if( strcmp( params[0],
"mpi_mod_mpi" ) == 0 )
1851 char *param2 = params[2];
1853 char *param4 = params[4];
1855 char *param6 = params[6];
1860 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1864 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1866 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1868 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1870 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1872 test_suite_mpi_mod_mpi( param1, param2, param3, param4, param5, param6, param7 );
1878 if( strcmp( params[0],
"mpi_mod_int" ) == 0 )
1882 char *param2 = params[2];
1889 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1893 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1895 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1896 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1897 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1899 test_suite_mpi_mod_int( param1, param2, param3, param4, param5 );
1905 if( strcmp( params[0],
"mpi_exp_mod" ) == 0 )
1909 char *param2 = params[2];
1911 char *param4 = params[4];
1913 char *param6 = params[6];
1915 char *param8 = params[8];
1917 char *param10 = params[10];
1922 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
1926 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1928 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1930 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1932 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1934 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1936 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1938 test_suite_mpi_exp_mod( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
1944 if( strcmp( params[0],
"mpi_inv_mod" ) == 0 )
1948 char *param2 = params[2];
1950 char *param4 = params[4];
1952 char *param6 = params[6];
1957 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1961 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1963 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1965 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1967 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1969 test_suite_mpi_inv_mod( param1, param2, param3, param4, param5, param6, param7 );
1975 if( strcmp( params[0],
"mpi_is_prime" ) == 0 )
1977 #ifdef POLARSSL_GENPRIME
1980 char *param2 = params[2];
1985 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1989 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1991 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1993 test_suite_mpi_is_prime( param1, param2, param3 );
2000 if( strcmp( params[0],
"mpi_shift_l" ) == 0 )
2004 char *param2 = params[2];
2007 char *param5 = params[5];
2011 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2015 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2017 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2018 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2021 test_suite_mpi_shift_l( param1, param2, param3, param4, param5 );
2027 if( strcmp( params[0],
"mpi_shift_r" ) == 0 )
2031 char *param2 = params[2];
2034 char *param5 = params[5];
2038 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2042 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2044 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2045 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2048 test_suite_mpi_shift_r( param1, param2, param3, param4, param5 );
2054 if( strcmp( params[0],
"mpi_selftest" ) == 0 )
2056 #ifdef POLARSSL_SELF_TEST
2061 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
2066 test_suite_mpi_selftest( );
2075 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
2089 ret = fgets( buf, len, f );
2093 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
2094 buf[strlen(buf) - 1] =
'\0';
2095 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
2096 buf[strlen(buf) - 1] =
'\0';
2107 params[cnt++] = cur;
2109 while( *p !=
'\0' && p < buf + len )
2119 if( p + 1 < buf + len )
2122 params[cnt++] = cur;
2131 for( i = 0; i < cnt; i++ )
2138 if( *p ==
'\\' && *(p + 1) ==
'n' )
2143 else if( *p ==
'\\' && *(p + 1) ==
':' )
2148 else if( *p ==
'\\' && *(p + 1) ==
'?' )
2164 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
2165 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_mpi.data";
2170 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2171 unsigned char alloc_buf[1000000];
2172 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
2175 file = fopen( filename,
"r" );
2178 fprintf( stderr,
"Failed to open\n" );
2182 while( !feof( file ) )
2186 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2188 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
2189 fprintf( stdout,
" " );
2190 for( i = strlen( buf ) + 1; i < 67; i++ )
2191 fprintf( stdout,
"." );
2192 fprintf( stdout,
" " );
2197 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2201 if( strcmp( params[0],
"depends_on" ) == 0 )
2203 for( i = 1; i < cnt; i++ )
2207 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2218 if( skip == 1 || ret == 3 )
2221 fprintf( stdout,
"----\n" );
2226 fprintf( stdout,
"PASS\n" );
2231 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
2238 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2240 if( strlen(buf) != 0 )
2242 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
2248 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
2249 if( total_errors == 0 )
2250 fprintf( stdout,
"PASSED" );
2252 fprintf( stdout,
"FAILED" );
2254 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
2255 total_tests - total_errors, total_tests, total_skipped );
2257 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2258 #if defined(POLARSSL_MEMORY_DEBUG)
2259 memory_buffer_alloc_status();
2261 memory_buffer_alloc_free();
2264 return( total_errors != 0 );
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
#define POLARSSL_ERR_MPI_INVALID_CHARACTER
There is an invalid character in the digit string.
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
int mpi_div_int(mpi *Q, mpi *R, const mpi *A, t_sint b)
Division by int: A = Q * b + R.
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE
The input arguments are negative or result in illegal output.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
Info structure for the pseudo random function.
int mpi_sub_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned substraction: X = |A| - |B|.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int mpi_cmp_abs(const mpi *X, const mpi *Y)
Compare unsigned values.
Configuration options (set of defines)
int mpi_add_int(mpi *X, const mpi *A, t_sint b)
Signed addition: X = A + b.
int mpi_read_file(mpi *X, int radix, FILE *fin)
Read X from an opened file.
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
int mpi_is_prime(mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA
Bad input parameters to function.
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
static int test_assert(int correct, char *test)
static int unhexify(unsigned char *obuf, const char *ibuf)
int mpi_write_file(const char *p, const mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
Multi-precision integer library.
int mpi_shift_r(mpi *X, size_t count)
Right-shift: X >>= count.
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 mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
#define TEST_ASSERT(TEST)
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO
The input argument for division is zero, which is not allowed.
int mpi_write_string(const mpi *X, int radix, char *s, size_t *slen)
Export into an ASCII string.
size_t mpi_lsb(const mpi *X)
Return the number of zero-bits before the least significant '1' bit.
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL
The buffer is too small to write to.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
int mpi_mul_int(mpi *X, const mpi *A, t_sint b)
Baseline multiplication: X = A * b Note: b is an unsigned integer type, thus Negative values of b are...
#define PUT_UINT32_BE(n, b, i)
int mpi_mod_int(t_uint *r, const mpi *A, t_sint b)
Modulo: r = A mod b.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int parse_arguments(char *buf, size_t len, char *params[50])
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
int mpi_add_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned addition: X = |A| + |B|.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
int mpi_self_test(int verbose)
Checkup routine.
int verify_string(char **str)
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
int dispatch_test(int cnt, char *params[50])
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_get_bit(const mpi *X, size_t pos)
Get a specific bit from X.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
#define POLARSSL_ERR_MPI_FILE_IO_ERROR
An error occurred while reading from or writing to a file.
int mpi_shift_l(mpi *X, size_t count)
Left-shift: X <<= count.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int verify_int(char *str, int *value)
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed substraction: X = A - B.
int mpi_set_bit(mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed substraction: X = A - b.
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE
The input arguments are not acceptable.
int get_line(FILE *f, char *buf, size_t len)