3 #ifdef POLARSSL_BIGNUM_C
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
17 #error "not_rnd_mpi() need bignum.c"
23 typedef UINT32 uint32_t;
36 #define GET_UINT32_BE(n,b,i) \
38 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41 | ( (uint32_t) (b)[(i) + 3] ); \
46 #define PUT_UINT32_BE(n,b,i) \
48 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 static int unhexify(
unsigned char *obuf,
const char *ibuf)
58 int len = strlen(ibuf) / 2;
59 assert(!(strlen(ibuf) %1));
64 if( c >=
'0' && c <=
'9' )
66 else if( c >=
'a' && c <=
'f' )
68 else if( c >=
'A' && c <=
'F' )
74 if( c2 >=
'0' && c2 <=
'9' )
76 else if( c2 >=
'a' && c2 <=
'f' )
78 else if( c2 >=
'A' && c2 <=
'F' )
83 *obuf++ = ( c << 4 ) | c2;
89 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
101 *obuf++ =
'a' + h - 10;
106 *obuf++ =
'a' + l - 10;
122 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
126 if( rng_state != NULL )
129 for( i = 0; i < len; ++i )
140 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
142 if( rng_state != NULL )
145 memset( output, 0, len );
172 if( rng_state == NULL )
181 memcpy( output, info->
buf, use_len );
182 info->
buf += use_len;
186 if( len - use_len > 0 )
187 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
216 uint32_t i, *k, sum, delta=0x9E3779B9;
217 unsigned char result[4];
219 if( rng_state == NULL )
226 size_t use_len = ( len > 4 ) ? 4 : len;
229 for( i = 0; i < 32; i++ )
231 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
233 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
237 memcpy( output, result, use_len );
244 #if defined(WANT_NOT_RND_MPI)
253 #define ciL (sizeof(t_uint))
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
257 char *str = (
char *) in;
266 X.
n = CHARS_TO_LIMBS( len );
272 assert( strlen( str ) / 2 == len );
284 #ifdef POLARSSL_BIGNUM_C
286 #define TEST_SUITE_ACTIVE
294 if( test_errors == 1 )
295 printf(
"FAILED\n" );
296 printf(
" %s\n", test );
301 #define TEST_ASSERT( TEST ) \
302 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
303 if( test_errors) return; \
308 if( (*str)[0] !=
'"' ||
309 (*str)[strlen( *str ) - 1] !=
'"' )
311 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
316 (*str)[strlen( *str ) - 1] =
'\0';
328 for( i = 0; i < strlen( str ); i++ )
330 if( i == 0 && str[i] ==
'-' )
336 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
337 str[i - 1] ==
'0' && str[i] ==
'x' )
343 if( str[i] <
'0' || str[i] >
'9' )
353 *value = strtol( str, NULL, 16 );
355 *value = strtol( str, NULL, 10 );
360 if( strcmp( str,
"POLARSSL_ERR_MPI_BAD_INPUT_DATA" ) == 0 )
365 if( strcmp( str,
"-34" ) == 0 )
370 if( strcmp( str,
"-9871232" ) == 0 )
372 *value = ( -9871232 );
375 if( strcmp( str,
"POLARSSL_ERR_MPI_DIVISION_BY_ZERO" ) == 0 )
380 if( strcmp( str,
"-1" ) == 0 )
385 if( strcmp( str,
"POLARSSL_ERR_MPI_NEGATIVE_VALUE" ) == 0 )
390 if( strcmp( str,
"-13" ) == 0 )
395 if( strcmp( str,
"POLARSSL_ERR_MPI_INVALID_CHARACTER" ) == 0 )
400 if( strcmp( str,
"-3" ) == 0 )
405 if( strcmp( str,
"POLARSSL_ERR_MPI_BUFFER_TOO_SMALL" ) == 0 )
410 if( strcmp( str,
"-2" ) == 0 )
415 #ifdef POLARSSL_FS_IO
416 if( strcmp( str,
"POLARSSL_ERR_MPI_FILE_IO_ERROR" ) == 0 )
421 #endif // POLARSSL_FS_IO
422 if( strcmp( str,
"POLARSSL_ERR_MPI_NOT_ACCEPTABLE" ) == 0 )
429 printf(
"Expected integer for parameter and got: %s\n", str );
433 void test_suite_mpi_read_write_string(
int radix_X,
char *input_X,
int radix_A,
434 char *input_A,
int output_size,
int result_read,
439 size_t len = output_size;
444 if( result_read == 0 )
447 if( result_write == 0 )
456 void test_suite_mpi_read_binary(
char *input_X,
int radix_A,
char *input_A )
459 unsigned char str[1000];
460 unsigned char buf[1000];
466 input_len =
unhexify( buf, input_X );
470 TEST_ASSERT( strcmp( (
char *) str, input_A ) == 0 );
475 void test_suite_mpi_write_binary(
int radix_X,
char *input_X,
char *input_A,
476 int output_size,
int result )
479 unsigned char str[1000];
480 unsigned char buf[1000];
483 memset( buf, 0x00, 1000 );
484 memset( str, 0x00, 1000 );
491 if( buflen > (
size_t) output_size )
492 buflen = (size_t) output_size;
497 hexify( str, buf, buflen );
499 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
505 #ifdef POLARSSL_FS_IO
506 void test_suite_mpi_read_file(
int radix_X,
char *input_file,
char *input_A,
510 unsigned char str[1000];
511 unsigned char buf[1000];
515 memset( buf, 0x00, 1000 );
516 memset( str, 0x00, 1000 );
520 file = fopen( input_file,
"r" );
529 hexify( str, buf, buflen );
531 TEST_ASSERT( strcasecmp( (
char *) str, input_A ) == 0 );
538 #ifdef POLARSSL_FS_IO
539 void test_suite_mpi_write_file(
int radix_X,
char *input_X,
int output_radix,
543 FILE *file_out, *file_in;
549 file_out = fopen( output_file,
"w" );
554 file_in = fopen( output_file,
"r" );
565 void test_suite_mpi_get_bit(
int radix_X,
char *input_X,
int pos,
int val )
575 void test_suite_mpi_set_bit(
int radix_X,
char *input_X,
int pos,
int val,
int radix_Y,
589 void test_suite_mpi_lsb(
int radix_X,
char *input_X,
int nr_bits )
600 void test_suite_mpi_msb(
int radix_X,
char *input_X,
int nr_bits )
611 void test_suite_mpi_gcd(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
612 int radix_A,
char *input_A )
626 void test_suite_mpi_cmp_int(
int input_X,
int input_A,
int result_CMP )
637 void test_suite_mpi_cmp_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
650 void test_suite_mpi_cmp_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
663 void test_suite_mpi_copy(
int input_X,
int input_A )
680 void test_suite_mpi_copy_self(
int input_X )
692 void test_suite_mpi_swap(
int input_X,
int input_Y )
709 void test_suite_mpi_add_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
710 int radix_A,
char *input_A )
724 void test_suite_mpi_add_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
725 int radix_A,
char *input_A )
739 void test_suite_mpi_add_abs_add_first(
int radix_X,
char *input_X,
int radix_Y,
740 char *input_Y,
int radix_A,
char *input_A )
754 void test_suite_mpi_add_abs_add_second(
int radix_X,
char *input_X,
int radix_Y,
755 char *input_Y,
int radix_A,
char *input_A )
769 void test_suite_mpi_add_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
783 void test_suite_mpi_sub_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
784 int radix_A,
char *input_A )
798 void test_suite_mpi_sub_abs(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
799 int radix_A,
char *input_A,
int sub_result )
817 void test_suite_mpi_sub_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
831 void test_suite_mpi_mul_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
832 int radix_A,
char *input_A )
846 void test_suite_mpi_mul_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
847 char *input_A,
char *result_comparison )
855 if( strcmp( result_comparison,
"==" ) == 0 )
857 else if( strcmp( result_comparison,
"!=" ) == 0 )
865 void test_suite_mpi_div_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
866 int radix_A,
char *input_A,
int radix_B,
char *input_B,
869 mpi X, Y, Q, R, A, B;
890 void test_suite_mpi_div_int(
int radix_X,
char *input_X,
int input_Y,
int radix_A,
891 char *input_A,
int radix_B,
char *input_B,
int div_result )
913 void test_suite_mpi_mod_mpi(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
914 int radix_A,
char *input_A,
int div_result )
933 void test_suite_mpi_mod_int(
int radix_X,
char *input_X,
int input_Y,
int input_A,
952 void test_suite_mpi_exp_mod(
int radix_A,
char *input_A,
int radix_E,
char *input_E,
953 int radix_N,
char *input_N,
int radix_RR,
char *input_RR,
954 int radix_X,
char *input_X,
int div_result )
956 mpi A, E, N, RR, Z, X;
966 if( strlen( input_RR ) )
980 void test_suite_mpi_inv_mod(
int radix_X,
char *input_X,
int radix_Y,
char *input_Y,
981 int radix_A,
char *input_A,
int div_result )
1000 #ifdef POLARSSL_GENPRIME
1001 void test_suite_mpi_is_prime(
int radix_X,
char *input_X,
int div_result )
1015 void test_suite_mpi_shift_l(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1029 void test_suite_mpi_shift_r(
int radix_X,
char *input_X,
int shift_X,
int radix_A,
1043 #ifdef POLARSSL_SELF_TEST
1044 void test_suite_mpi_selftest()
1059 if( strcmp( str,
"POLARSSL_GENPRIME" ) == 0 )
1061 #if defined(POLARSSL_GENPRIME)
1067 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
1069 #if defined(POLARSSL_SELF_TEST)
1086 #if defined(TEST_SUITE_ACTIVE)
1087 if( strcmp( params[0],
"mpi_read_write_string" ) == 0 )
1091 char *param2 = params[2];
1093 char *param4 = params[4];
1100 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1104 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1106 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1108 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1109 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1110 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1112 test_suite_mpi_read_write_string( param1, param2, param3, param4, param5, param6, param7 );
1118 if( strcmp( params[0],
"mpi_read_binary" ) == 0 )
1121 char *param1 = params[1];
1123 char *param3 = params[3];
1127 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1132 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1135 test_suite_mpi_read_binary( param1, param2, param3 );
1141 if( strcmp( params[0],
"mpi_write_binary" ) == 0 )
1145 char *param2 = params[2];
1146 char *param3 = params[3];
1152 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1156 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1159 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1160 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1162 test_suite_mpi_write_binary( param1, param2, param3, param4, param5 );
1168 if( strcmp( params[0],
"mpi_read_file" ) == 0 )
1170 #ifdef POLARSSL_FS_IO
1173 char *param2 = params[2];
1174 char *param3 = params[3];
1179 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1183 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1186 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1188 test_suite_mpi_read_file( param1, param2, param3, param4 );
1195 if( strcmp( params[0],
"mpi_write_file" ) == 0 )
1197 #ifdef POLARSSL_FS_IO
1200 char *param2 = params[2];
1202 char *param4 = params[4];
1206 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1210 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1212 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1215 test_suite_mpi_write_file( param1, param2, param3, param4 );
1222 if( strcmp( params[0],
"mpi_get_bit" ) == 0 )
1226 char *param2 = params[2];
1232 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1236 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1238 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1239 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1241 test_suite_mpi_get_bit( param1, param2, param3, param4 );
1247 if( strcmp( params[0],
"mpi_set_bit" ) == 0 )
1251 char *param2 = params[2];
1255 char *param6 = params[6];
1259 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1263 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1265 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1266 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1267 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1270 test_suite_mpi_set_bit( param1, param2, param3, param4, param5, param6 );
1276 if( strcmp( params[0],
"mpi_lsb" ) == 0 )
1280 char *param2 = params[2];
1285 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1289 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1291 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1293 test_suite_mpi_lsb( param1, param2, param3 );
1299 if( strcmp( params[0],
"mpi_msb" ) == 0 )
1303 char *param2 = params[2];
1308 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1312 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1314 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1316 test_suite_mpi_msb( param1, param2, param3 );
1322 if( strcmp( params[0],
"mpi_gcd" ) == 0 )
1326 char *param2 = params[2];
1328 char *param4 = params[4];
1330 char *param6 = params[6];
1334 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1338 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1340 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1342 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1345 test_suite_mpi_gcd( param1, param2, param3, param4, param5, param6 );
1351 if( strcmp( params[0],
"mpi_cmp_int" ) == 0 )
1360 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1364 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1365 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1366 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1368 test_suite_mpi_cmp_int( param1, param2, param3 );
1374 if( strcmp( params[0],
"mpi_cmp_mpi" ) == 0 )
1378 char *param2 = params[2];
1380 char *param4 = params[4];
1385 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1389 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1391 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1393 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1395 test_suite_mpi_cmp_mpi( param1, param2, param3, param4, param5 );
1401 if( strcmp( params[0],
"mpi_cmp_abs" ) == 0 )
1405 char *param2 = params[2];
1407 char *param4 = params[4];
1412 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1416 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1418 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1420 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1422 test_suite_mpi_cmp_abs( param1, param2, param3, param4, param5 );
1428 if( strcmp( params[0],
"mpi_copy" ) == 0 )
1436 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1440 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1441 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1443 test_suite_mpi_copy( param1, param2 );
1449 if( strcmp( params[0],
"mpi_copy_self" ) == 0 )
1456 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1460 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1462 test_suite_mpi_copy_self( param1 );
1468 if( strcmp( params[0],
"mpi_swap" ) == 0 )
1476 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1480 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1481 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1483 test_suite_mpi_swap( param1, param2 );
1489 if( strcmp( params[0],
"mpi_add_mpi" ) == 0 )
1493 char *param2 = params[2];
1495 char *param4 = params[4];
1497 char *param6 = params[6];
1501 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1505 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1507 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1509 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1512 test_suite_mpi_add_mpi( param1, param2, param3, param4, param5, param6 );
1518 if( strcmp( params[0],
"mpi_add_abs" ) == 0 )
1522 char *param2 = params[2];
1524 char *param4 = params[4];
1526 char *param6 = params[6];
1530 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1534 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1536 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1538 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1541 test_suite_mpi_add_abs( param1, param2, param3, param4, param5, param6 );
1547 if( strcmp( params[0],
"mpi_add_abs_add_first" ) == 0 )
1551 char *param2 = params[2];
1553 char *param4 = params[4];
1555 char *param6 = params[6];
1559 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1563 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1565 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1567 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1570 test_suite_mpi_add_abs_add_first( param1, param2, param3, param4, param5, param6 );
1576 if( strcmp( params[0],
"mpi_add_abs_add_second" ) == 0 )
1580 char *param2 = params[2];
1582 char *param4 = params[4];
1584 char *param6 = params[6];
1588 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1592 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1594 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1596 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1599 test_suite_mpi_add_abs_add_second( param1, param2, param3, param4, param5, param6 );
1605 if( strcmp( params[0],
"mpi_add_int" ) == 0 )
1609 char *param2 = params[2];
1612 char *param5 = params[5];
1616 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1620 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1622 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1623 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1626 test_suite_mpi_add_int( param1, param2, param3, param4, param5 );
1632 if( strcmp( params[0],
"mpi_sub_mpi" ) == 0 )
1636 char *param2 = params[2];
1638 char *param4 = params[4];
1640 char *param6 = params[6];
1644 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1648 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1650 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1652 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1655 test_suite_mpi_sub_mpi( param1, param2, param3, param4, param5, param6 );
1661 if( strcmp( params[0],
"mpi_sub_abs" ) == 0 )
1665 char *param2 = params[2];
1667 char *param4 = params[4];
1669 char *param6 = params[6];
1674 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1678 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1680 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1682 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1684 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1686 test_suite_mpi_sub_abs( param1, param2, param3, param4, param5, param6, param7 );
1692 if( strcmp( params[0],
"mpi_sub_int" ) == 0 )
1696 char *param2 = params[2];
1699 char *param5 = params[5];
1703 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1707 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1709 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1710 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1713 test_suite_mpi_sub_int( param1, param2, param3, param4, param5 );
1719 if( strcmp( params[0],
"mpi_mul_mpi" ) == 0 )
1723 char *param2 = params[2];
1725 char *param4 = params[4];
1727 char *param6 = params[6];
1731 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1735 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1737 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1739 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1742 test_suite_mpi_mul_mpi( param1, param2, param3, param4, param5, param6 );
1748 if( strcmp( params[0],
"mpi_mul_int" ) == 0 )
1752 char *param2 = params[2];
1755 char *param5 = params[5];
1756 char *param6 = params[6];
1760 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1764 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1766 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1767 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1771 test_suite_mpi_mul_int( param1, param2, param3, param4, param5, param6 );
1777 if( strcmp( params[0],
"mpi_div_mpi" ) == 0 )
1781 char *param2 = params[2];
1783 char *param4 = params[4];
1785 char *param6 = params[6];
1787 char *param8 = params[8];
1792 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1796 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1798 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1800 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1802 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1804 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1806 test_suite_mpi_div_mpi( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1812 if( strcmp( params[0],
"mpi_div_int" ) == 0 )
1816 char *param2 = params[2];
1819 char *param5 = params[5];
1821 char *param7 = params[7];
1826 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1830 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1832 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1833 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1835 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1837 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1839 test_suite_mpi_div_int( param1, param2, param3, param4, param5, param6, param7, param8 );
1845 if( strcmp( params[0],
"mpi_mod_mpi" ) == 0 )
1849 char *param2 = params[2];
1851 char *param4 = params[4];
1853 char *param6 = params[6];
1858 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1862 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1864 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1866 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1868 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1870 test_suite_mpi_mod_mpi( param1, param2, param3, param4, param5, param6, param7 );
1876 if( strcmp( params[0],
"mpi_mod_int" ) == 0 )
1880 char *param2 = params[2];
1887 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1891 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1893 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1894 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1895 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1897 test_suite_mpi_mod_int( param1, param2, param3, param4, param5 );
1903 if( strcmp( params[0],
"mpi_exp_mod" ) == 0 )
1907 char *param2 = params[2];
1909 char *param4 = params[4];
1911 char *param6 = params[6];
1913 char *param8 = params[8];
1915 char *param10 = params[10];
1920 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
1924 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1926 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1928 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1930 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1932 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1934 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1936 test_suite_mpi_exp_mod( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
1942 if( strcmp( params[0],
"mpi_inv_mod" ) == 0 )
1946 char *param2 = params[2];
1948 char *param4 = params[4];
1950 char *param6 = params[6];
1955 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1959 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1961 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1963 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1965 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1967 test_suite_mpi_inv_mod( param1, param2, param3, param4, param5, param6, param7 );
1973 if( strcmp( params[0],
"mpi_is_prime" ) == 0 )
1975 #ifdef POLARSSL_GENPRIME
1978 char *param2 = params[2];
1983 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1987 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1989 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1991 test_suite_mpi_is_prime( param1, param2, param3 );
1998 if( strcmp( params[0],
"mpi_shift_l" ) == 0 )
2002 char *param2 = params[2];
2005 char *param5 = params[5];
2009 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2013 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2015 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2016 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2019 test_suite_mpi_shift_l( param1, param2, param3, param4, param5 );
2025 if( strcmp( params[0],
"mpi_shift_r" ) == 0 )
2029 char *param2 = params[2];
2032 char *param5 = params[5];
2036 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
2040 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
2042 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
2043 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
2046 test_suite_mpi_shift_r( param1, param2, param3, param4, param5 );
2052 if( strcmp( params[0],
"mpi_selftest" ) == 0 )
2054 #ifdef POLARSSL_SELF_TEST
2059 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
2064 test_suite_mpi_selftest( );
2073 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
2087 ret = fgets( buf, len, f );
2091 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
2092 buf[strlen(buf) - 1] =
'\0';
2093 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
2094 buf[strlen(buf) - 1] =
'\0';
2105 params[cnt++] = cur;
2107 while( *p !=
'\0' && p < buf + len )
2117 if( p + 1 < buf + len )
2120 params[cnt++] = cur;
2129 for( i = 0; i < cnt; i++ )
2136 if( *p ==
'\\' && *(p + 1) ==
'n' )
2141 else if( *p ==
'\\' && *(p + 1) ==
':' )
2146 else if( *p ==
'\\' && *(p + 1) ==
'?' )
2162 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
2163 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_mpi.data";
2168 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2169 unsigned char alloc_buf[1000000];
2170 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
2173 file = fopen( filename,
"r" );
2176 fprintf( stderr,
"Failed to open\n" );
2180 while( !feof( file ) )
2184 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2186 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
2187 fprintf( stdout,
" " );
2188 for( i = strlen( buf ) + 1; i < 67; i++ )
2189 fprintf( stdout,
"." );
2190 fprintf( stdout,
" " );
2195 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2199 if( strcmp( params[0],
"depends_on" ) == 0 )
2201 for( i = 1; i < cnt; i++ )
2205 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2216 if( skip == 1 || ret == 3 )
2219 fprintf( stdout,
"----\n" );
2222 else if( ret == 0 && test_errors == 0 )
2224 fprintf( stdout,
"PASS\n" );
2229 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
2236 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
2238 if( strlen(buf) != 0 )
2240 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
2246 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
2247 if( total_errors == 0 )
2248 fprintf( stdout,
"PASSED" );
2250 fprintf( stdout,
"FAILED" );
2252 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
2253 total_tests - total_errors, total_tests, total_skipped );
2255 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2256 #if defined(POLARSSL_MEMORY_DEBUG)
2257 memory_buffer_alloc_status();
2259 memory_buffer_alloc_free();
2262 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 subtraction: 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 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 subtraction: 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 subtraction: 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)