1/* BEGIN_HEADER */ 2#include "mbedtls/bignum.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_BIGNUM_C 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE */ 11void mpi_null( ) 12{ 13 mbedtls_mpi X, Y, Z; 14 15 mbedtls_mpi_init( &X ); 16 mbedtls_mpi_init( &Y ); 17 mbedtls_mpi_init( &Z ); 18 19 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); 20 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); 21 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); 22 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); 23 24exit: 25 mbedtls_mpi_free( &X ); 26} 27/* END_CASE */ 28 29/* BEGIN_CASE */ 30void mpi_read_write_string( int radix_X, char *input_X, int radix_A, 31 char *input_A, int output_size, int result_read, 32 int result_write ) 33{ 34 mbedtls_mpi X; 35 char str[1000]; 36 size_t len; 37 38 mbedtls_mpi_init( &X ); 39 40 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read ); 41 if( result_read == 0 ) 42 { 43 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write ); 44 if( result_write == 0 ) 45 { 46 TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); 47 } 48 } 49 50exit: 51 mbedtls_mpi_free( &X ); 52} 53/* END_CASE */ 54 55/* BEGIN_CASE */ 56void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A ) 57{ 58 mbedtls_mpi X; 59 unsigned char str[1000]; 60 unsigned char buf[1000]; 61 size_t len; 62 size_t input_len; 63 64 mbedtls_mpi_init( &X ); 65 66 input_len = unhexify( buf, input_X ); 67 68 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 ); 69 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 ); 70 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); 71 72exit: 73 mbedtls_mpi_free( &X ); 74} 75/* END_CASE */ 76 77/* BEGIN_CASE */ 78void mbedtls_mpi_write_binary( int radix_X, char *input_X, char *input_A, 79 int output_size, int result ) 80{ 81 mbedtls_mpi X; 82 unsigned char str[1000]; 83 unsigned char buf[1000]; 84 size_t buflen; 85 86 memset( buf, 0x00, 1000 ); 87 memset( str, 0x00, 1000 ); 88 89 mbedtls_mpi_init( &X ); 90 91 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 92 93 buflen = mbedtls_mpi_size( &X ); 94 if( buflen > (size_t) output_size ) 95 buflen = (size_t) output_size; 96 97 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result ); 98 if( result == 0) 99 { 100 hexify( str, buf, buflen ); 101 102 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 ); 103 } 104 105exit: 106 mbedtls_mpi_free( &X ); 107} 108/* END_CASE */ 109 110/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ 111void mbedtls_mpi_read_file( int radix_X, char *input_file, char *input_A, 112 int result ) 113{ 114 mbedtls_mpi X; 115 unsigned char str[1000]; 116 unsigned char buf[1000]; 117 size_t buflen; 118 FILE *file; 119 int ret; 120 121 memset( buf, 0x00, 1000 ); 122 memset( str, 0x00, 1000 ); 123 124 mbedtls_mpi_init( &X ); 125 126 file = fopen( input_file, "r" ); 127 TEST_ASSERT( file != NULL ); 128 ret = mbedtls_mpi_read_file( &X, radix_X, file ); 129 fclose(file); 130 TEST_ASSERT( ret == result ); 131 132 if( result == 0 ) 133 { 134 buflen = mbedtls_mpi_size( &X ); 135 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 ); 136 137 hexify( str, buf, buflen ); 138 139 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 ); 140 } 141 142exit: 143 mbedtls_mpi_free( &X ); 144} 145/* END_CASE */ 146 147/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ 148void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix, 149 char *output_file ) 150{ 151 mbedtls_mpi X, Y; 152 FILE *file_out, *file_in; 153 int ret; 154 155 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 156 157 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 158 159 file_out = fopen( output_file, "w" ); 160 TEST_ASSERT( file_out != NULL ); 161 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out ); 162 fclose(file_out); 163 TEST_ASSERT( ret == 0 ); 164 165 file_in = fopen( output_file, "r" ); 166 TEST_ASSERT( file_in != NULL ); 167 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in ); 168 fclose(file_in); 169 TEST_ASSERT( ret == 0 ); 170 171 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 172 173exit: 174 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 175} 176/* END_CASE */ 177 178/* BEGIN_CASE */ 179void mbedtls_mpi_get_bit( int radix_X, char *input_X, int pos, int val ) 180{ 181 mbedtls_mpi X; 182 mbedtls_mpi_init( &X ); 183 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 184 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val ); 185 186exit: 187 mbedtls_mpi_free( &X ); 188} 189/* END_CASE */ 190 191/* BEGIN_CASE */ 192void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val, 193 int radix_Y, char *output_Y, int result ) 194{ 195 mbedtls_mpi X, Y; 196 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 197 198 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 199 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 ); 200 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); 201 202 if( result == 0 ) 203 { 204 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 205 } 206 207exit: 208 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 209} 210/* END_CASE */ 211 212/* BEGIN_CASE */ 213void mbedtls_mpi_lsb( int radix_X, char *input_X, int nr_bits ) 214{ 215 mbedtls_mpi X; 216 mbedtls_mpi_init( &X ); 217 218 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 219 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits ); 220 221exit: 222 mbedtls_mpi_free( &X ); 223} 224/* END_CASE */ 225 226/* BEGIN_CASE */ 227void mbedtls_mpi_bitlen( int radix_X, char *input_X, int nr_bits ) 228{ 229 mbedtls_mpi X; 230 mbedtls_mpi_init( &X ); 231 232 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 233 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); 234 235exit: 236 mbedtls_mpi_free( &X ); 237} 238/* END_CASE */ 239 240/* BEGIN_CASE */ 241void mbedtls_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y, 242 int radix_A, char *input_A ) 243{ 244 mbedtls_mpi A, X, Y, Z; 245 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); 246 247 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 248 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 249 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 250 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 ); 251 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 252 253exit: 254 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); 255} 256/* END_CASE */ 257 258/* BEGIN_CASE */ 259void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP ) 260{ 261 mbedtls_mpi X; 262 mbedtls_mpi_init( &X ); 263 264 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0); 265 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP); 266 267exit: 268 mbedtls_mpi_free( &X ); 269} 270/* END_CASE */ 271 272/* BEGIN_CASE */ 273void mbedtls_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 274 int input_A ) 275{ 276 mbedtls_mpi X, Y; 277 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 278 279 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 280 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 281 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A ); 282 283exit: 284 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 285} 286/* END_CASE */ 287 288/* BEGIN_CASE */ 289void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y, 290 int input_A ) 291{ 292 mbedtls_mpi X, Y; 293 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 294 295 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 296 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 297 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A ); 298 299exit: 300 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 301} 302/* END_CASE */ 303 304/* BEGIN_CASE */ 305void mbedtls_mpi_copy( int input_X, int input_A ) 306{ 307 mbedtls_mpi X, Y, A; 308 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 309 310 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); 311 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 ); 312 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 ); 313 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 ); 314 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 315 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 ); 316 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 317 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 ); 318 319exit: 320 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 321} 322/* END_CASE */ 323 324/* BEGIN_CASE */ 325void mpi_copy_self( int input_X ) 326{ 327 mbedtls_mpi X; 328 mbedtls_mpi_init( &X ); 329 330 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); 331 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); 332 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 ); 333 334exit: 335 mbedtls_mpi_free( &X ); 336} 337/* END_CASE */ 338 339/* BEGIN_CASE */ 340void mbedtls_mpi_shrink( int before, int used, int min, int after ) 341{ 342 mbedtls_mpi X; 343 mbedtls_mpi_init( &X ); 344 345 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 ); 346 TEST_ASSERT( used <= before ); 347 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) ); 348 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 ); 349 TEST_ASSERT( X.n == (size_t) after ); 350 351exit: 352 mbedtls_mpi_free( &X ); 353} 354/* END_CASE */ 355 356/* BEGIN_CASE */ 357void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str, 358 int y_sign, char *y_str ) 359{ 360 mbedtls_mpi X, Y, XX; 361 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX ); 362 363 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 ); 364 X.s = x_sign; 365 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 ); 366 Y.s = y_sign; 367 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 ); 368 369 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 ); 370 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 ); 371 372 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 ); 373 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 374 375exit: 376 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX ); 377} 378/* END_CASE */ 379 380/* BEGIN_CASE */ 381void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str, 382 int y_sign, char *y_str ) 383{ 384 mbedtls_mpi X, Y, XX, YY; 385 386 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 387 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY ); 388 389 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 ); 390 X.s = x_sign; 391 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 ); 392 Y.s = y_sign; 393 394 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 ); 395 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 ); 396 397 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 ); 398 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 ); 399 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 ); 400 401 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 ); 402 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 ); 403 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 ); 404 405exit: 406 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 407 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY ); 408} 409/* END_CASE */ 410 411/* BEGIN_CASE */ 412void mbedtls_mpi_swap( int input_X, int input_Y ) 413{ 414 mbedtls_mpi X, Y, A; 415 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 416 417 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); 418 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 ); 419 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 ); 420 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 ); 421 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 422 mbedtls_mpi_swap( &X, &Y ); 423 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 ); 424 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 425 426exit: 427 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 428} 429/* END_CASE */ 430 431/* BEGIN_CASE */ 432void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 433 int radix_A, char *input_A ) 434{ 435 mbedtls_mpi X, Y, Z, A; 436 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 437 438 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 439 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 440 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 441 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 ); 442 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 443 444exit: 445 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 446} 447/* END_CASE */ 448 449/* BEGIN_CASE */ 450void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A ) 451{ 452 mbedtls_mpi X, A; 453 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 454 455 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 456 457 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 458 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 ); 459 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 ); 460 461 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 462 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 ); 463 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 464 465 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 466 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); 467 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 468 469exit: 470 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 471} 472/* END_CASE */ 473 474 475/* BEGIN_CASE */ 476void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y, 477 int radix_A, char *input_A ) 478{ 479 mbedtls_mpi X, Y, Z, A; 480 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 481 482 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 483 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 484 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 485 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 ); 486 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 487 488exit: 489 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 490} 491/* END_CASE */ 492 493/* BEGIN_CASE */ 494void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y, 495 char *input_Y, int radix_A, char *input_A ) 496{ 497 mbedtls_mpi X, Y, A; 498 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 499 500 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 501 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 502 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 503 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 ); 504 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 505 506exit: 507 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 508} 509/* END_CASE */ 510 511/* BEGIN_CASE */ 512void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y, 513 char *input_Y, int radix_A, char *input_A ) 514{ 515 mbedtls_mpi X, Y, A; 516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 517 518 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 519 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 520 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 521 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 ); 522 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 523 524exit: 525 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 526} 527/* END_CASE */ 528 529/* BEGIN_CASE */ 530void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A, 531 char *input_A ) 532{ 533 mbedtls_mpi X, Z, A; 534 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 535 536 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 537 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 538 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 ); 539 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 540 541exit: 542 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 543} 544/* END_CASE */ 545 546/* BEGIN_CASE */ 547void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 548 int radix_A, char *input_A ) 549{ 550 mbedtls_mpi X, Y, Z, A; 551 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 552 553 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 554 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 555 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 556 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 ); 557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 558 559exit: 560 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 561} 562/* END_CASE */ 563 564/* BEGIN_CASE */ 565void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y, 566 int radix_A, char *input_A, int sub_result ) 567{ 568 mbedtls_mpi X, Y, Z, A; 569 int res; 570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 571 572 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 573 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 574 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 575 576 res = mbedtls_mpi_sub_abs( &Z, &X, &Y ); 577 TEST_ASSERT( res == sub_result ); 578 if( res == 0 ) 579 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 580 581exit: 582 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 583} 584/* END_CASE */ 585 586/* BEGIN_CASE */ 587void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A, 588 char *input_A ) 589{ 590 mbedtls_mpi X, Z, A; 591 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 592 593 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 594 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 595 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 ); 596 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 597 598exit: 599 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 600} 601/* END_CASE */ 602 603/* BEGIN_CASE */ 604void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 605 int radix_A, char *input_A ) 606{ 607 mbedtls_mpi X, Y, Z, A; 608 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 609 610 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 611 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 612 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 613 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 ); 614 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 615 616exit: 617 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 618} 619/* END_CASE */ 620 621/* BEGIN_CASE */ 622void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A, 623 char *input_A, char *result_comparison ) 624{ 625 mbedtls_mpi X, Z, A; 626 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 627 628 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 629 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 630 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 ); 631 if( strcmp( result_comparison, "==" ) == 0 ) 632 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 633 else if( strcmp( result_comparison, "!=" ) == 0 ) 634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 ); 635 else 636 TEST_ASSERT( "unknown operator" == 0 ); 637 638exit: 639 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 640} 641/* END_CASE */ 642 643/* BEGIN_CASE */ 644void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 645 int radix_A, char *input_A, int radix_B, char *input_B, 646 int div_result ) 647{ 648 mbedtls_mpi X, Y, Q, R, A, B; 649 int res; 650 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); 651 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B ); 652 653 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 654 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 655 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 656 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 ); 657 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y ); 658 TEST_ASSERT( res == div_result ); 659 if( res == 0 ) 660 { 661 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); 662 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); 663 } 664 665exit: 666 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); 667 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B ); 668} 669/* END_CASE */ 670 671/* BEGIN_CASE */ 672void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A, 673 char *input_A, int radix_B, char *input_B, int div_result ) 674{ 675 mbedtls_mpi X, Q, R, A, B; 676 int res; 677 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A ); 678 mbedtls_mpi_init( &B ); 679 680 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 681 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 682 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 ); 683 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y ); 684 TEST_ASSERT( res == div_result ); 685 if( res == 0 ) 686 { 687 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); 688 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); 689 } 690 691exit: 692 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A ); 693 mbedtls_mpi_free( &B ); 694} 695/* END_CASE */ 696 697/* BEGIN_CASE */ 698void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y, 699 int radix_A, char *input_A, int div_result ) 700{ 701 mbedtls_mpi X, Y, A; 702 int res; 703 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 704 705 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 706 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 707 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 708 res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); 709 TEST_ASSERT( res == div_result ); 710 if( res == 0 ) 711 { 712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 713 } 714 715exit: 716 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 717} 718/* END_CASE */ 719 720/* BEGIN_CASE */ 721void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A, 722 int div_result ) 723{ 724 mbedtls_mpi X; 725 int res; 726 mbedtls_mpi_uint r; 727 mbedtls_mpi_init( &X ); 728 729 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 730 res = mbedtls_mpi_mod_int( &r, &X, input_Y ); 731 TEST_ASSERT( res == div_result ); 732 if( res == 0 ) 733 { 734 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); 735 } 736 737exit: 738 mbedtls_mpi_free( &X ); 739} 740/* END_CASE */ 741 742/* BEGIN_CASE */ 743void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E, 744 int radix_N, char *input_N, int radix_RR, char *input_RR, 745 int radix_X, char *input_X, int div_result ) 746{ 747 mbedtls_mpi A, E, N, RR, Z, X; 748 int res; 749 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); 750 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X ); 751 752 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 753 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); 754 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); 755 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 756 757 if( strlen( input_RR ) ) 758 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 ); 759 760 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); 761 TEST_ASSERT( res == div_result ); 762 if( res == 0 ) 763 { 764 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); 765 } 766 767exit: 768 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); 769 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X ); 770} 771/* END_CASE */ 772 773/* BEGIN_CASE */ 774void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y, 775 int radix_A, char *input_A, int div_result ) 776{ 777 mbedtls_mpi X, Y, Z, A; 778 int res; 779 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 780 781 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 782 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); 783 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 784 res = mbedtls_mpi_inv_mod( &Z, &X, &Y ); 785 TEST_ASSERT( res == div_result ); 786 if( res == 0 ) 787 { 788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 789 } 790 791exit: 792 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 793} 794/* END_CASE */ 795 796/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ 797void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result ) 798{ 799 mbedtls_mpi X; 800 int res; 801 mbedtls_mpi_init( &X ); 802 803 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 804 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ); 805 TEST_ASSERT( res == div_result ); 806 807exit: 808 mbedtls_mpi_free( &X ); 809} 810/* END_CASE */ 811 812/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ 813void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret ) 814{ 815 mbedtls_mpi X; 816 int my_ret; 817 818 mbedtls_mpi_init( &X ); 819 820 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL ); 821 TEST_ASSERT( my_ret == ref_ret ); 822 823 if( ref_ret == 0 ) 824 { 825 size_t actual_bits = mbedtls_mpi_bitlen( &X ); 826 827 TEST_ASSERT( actual_bits >= (size_t) bits ); 828 TEST_ASSERT( actual_bits <= (size_t) bits + 1 ); 829 830 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 ); 831 if( safe ) 832 { 833 mbedtls_mpi_shift_r( &X, 1 ); /* X = ( X - 1 ) / 2 */ 834 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 ); 835 } 836 } 837 838exit: 839 mbedtls_mpi_free( &X ); 840} 841/* END_CASE */ 842 843/* BEGIN_CASE */ 844void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A, 845 char *input_A) 846{ 847 mbedtls_mpi X, A; 848 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 849 850 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 851 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 852 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 ); 853 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 854 855exit: 856 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 857} 858/* END_CASE */ 859 860/* BEGIN_CASE */ 861void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A, 862 char *input_A ) 863{ 864 mbedtls_mpi X, A; 865 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 866 867 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); 868 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); 869 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 ); 870 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 871 872exit: 873 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 874} 875/* END_CASE */ 876 877/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 878void mpi_selftest() 879{ 880 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 ); 881} 882/* END_CASE */ 883