1/* BEGIN_HEADER */ 2#include "mbedtls/bignum.h" 3#include "mbedtls/entropy.h" 4 5#if MBEDTLS_MPI_MAX_BITS > 792 6#define MPI_MAX_BITS_LARGER_THAN_792 7#endif 8 9/* Check the validity of the sign bit in an MPI object. Reject representations 10 * that are not supported by the rest of the library and indicate a bug when 11 * constructing the value. */ 12static int sign_is_valid( const mbedtls_mpi *X ) 13{ 14 if( X->s != 1 && X->s != -1 ) 15 return( 0 ); // invalid sign bit, e.g. 0 16 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 ) 17 return( 0 ); // negative zero 18 return( 1 ); 19} 20 21typedef struct mbedtls_test_mpi_random 22{ 23 data_t *data; 24 size_t pos; 25 size_t chunk_len; 26} mbedtls_test_mpi_random; 27 28/* 29 * This function is called by the Miller-Rabin primality test each time it 30 * chooses a random witness. The witnesses (or non-witnesses as provided by the 31 * test) are stored in the data member of the state structure. Each number is in 32 * the format that mbedtls_mpi_read_string understands and is chunk_len long. 33 */ 34int mbedtls_test_mpi_miller_rabin_determinizer( void* state, 35 unsigned char* buf, 36 size_t len ) 37{ 38 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state; 39 40 if( random == NULL || random->data->x == NULL || buf == NULL ) 41 return( -1 ); 42 43 if( random->pos + random->chunk_len > random->data->len 44 || random->chunk_len > len ) 45 { 46 return( -1 ); 47 } 48 49 memset( buf, 0, len ); 50 51 /* The witness is written to the end of the buffer, since the buffer is 52 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary. 53 * Writing the witness to the start of the buffer would result in the 54 * buffer being 'witness 000...000', which would be treated as 55 * witness * 2^n for some n. */ 56 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos], 57 random->chunk_len ); 58 59 random->pos += random->chunk_len; 60 61 return( 0 ); 62} 63 64/* Random generator that is told how many bytes to return. */ 65static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len ) 66{ 67 size_t *bytes_left = state; 68 size_t i; 69 for( i = 0; i < len; i++ ) 70 { 71 if( *bytes_left == 0 ) 72 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 73 buf[i] = *bytes_left & 0xff; 74 --( *bytes_left ); 75 } 76 return( 0 ); 77} 78 79/* END_HEADER */ 80 81/* BEGIN_DEPENDENCIES 82 * depends_on:MBEDTLS_BIGNUM_C 83 * END_DEPENDENCIES 84 */ 85 86/* BEGIN_CASE */ 87void mpi_valid_param( ) 88{ 89 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) ); 90} 91/* END_CASE */ 92 93/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ 94void mpi_invalid_param( ) 95{ 96 mbedtls_mpi X; 97 const char *s_in = "00101000101010"; 98 char s_out[16] = { 0 }; 99 unsigned char u_out[16] = { 0 }; 100 unsigned char u_in[16] = { 0 }; 101 size_t olen; 102 mbedtls_mpi_uint mpi_uint; 103 104 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) ); 105 106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 107 mbedtls_mpi_grow( NULL, 42 ) ); 108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 109 mbedtls_mpi_copy( NULL, &X ) ); 110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 111 mbedtls_mpi_copy( &X, NULL ) ); 112 113 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) ); 114 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) ); 115 116 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 117 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) ); 118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 119 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) ); 120 121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 122 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) ); 123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 124 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) ); 125 126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 127 mbedtls_mpi_lset( NULL, 42 ) ); 128 129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 130 mbedtls_mpi_get_bit( NULL, 42 ) ); 131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 132 mbedtls_mpi_set_bit( NULL, 42, 0 ) ); 133 134 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 135 mbedtls_mpi_read_string( NULL, 2, s_in ) ); 136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 137 mbedtls_mpi_read_string( &X, 2, NULL ) ); 138 139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 140 mbedtls_mpi_write_string( NULL, 2, 141 s_out, sizeof( s_out ), 142 &olen ) ); 143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 144 mbedtls_mpi_write_string( &X, 2, 145 NULL, sizeof( s_out ), 146 &olen ) ); 147 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 148 mbedtls_mpi_write_string( &X, 2, 149 s_out, sizeof( s_out ), 150 NULL ) ); 151 152#if defined(MBEDTLS_FS_IO) 153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 154 mbedtls_mpi_read_file( NULL, 2, stdin ) ); 155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 156 mbedtls_mpi_read_file( &X, 2, NULL ) ); 157 158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 159 mbedtls_mpi_write_file( "", NULL, 2, NULL ) ); 160#endif /* MBEDTLS_FS_IO */ 161 162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 163 mbedtls_mpi_read_binary( NULL, u_in, 164 sizeof( u_in ) ) ); 165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 166 mbedtls_mpi_read_binary( &X, NULL, 167 sizeof( u_in ) ) ); 168 169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 170 mbedtls_mpi_write_binary( NULL, u_out, 171 sizeof( u_out ) ) ); 172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 173 mbedtls_mpi_write_binary( &X, NULL, 174 sizeof( u_out ) ) ); 175 176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 177 mbedtls_mpi_shift_l( NULL, 42 ) ); 178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 179 mbedtls_mpi_shift_r( NULL, 42 ) ); 180 181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 182 mbedtls_mpi_cmp_abs( NULL, &X ) ); 183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 184 mbedtls_mpi_cmp_abs( &X, NULL ) ); 185 186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 187 mbedtls_mpi_cmp_mpi( NULL, &X ) ); 188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 189 mbedtls_mpi_cmp_mpi( &X, NULL ) ); 190 191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 192 mbedtls_mpi_cmp_int( NULL, 42 ) ); 193 194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 195 mbedtls_mpi_add_abs( NULL, &X, &X ) ); 196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 197 mbedtls_mpi_add_abs( &X, NULL, &X ) ); 198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 199 mbedtls_mpi_add_abs( &X, &X, NULL ) ); 200 201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 202 mbedtls_mpi_sub_abs( NULL, &X, &X ) ); 203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 204 mbedtls_mpi_sub_abs( &X, NULL, &X ) ); 205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 206 mbedtls_mpi_sub_abs( &X, &X, NULL ) ); 207 208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 209 mbedtls_mpi_add_mpi( NULL, &X, &X ) ); 210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 211 mbedtls_mpi_add_mpi( &X, NULL, &X ) ); 212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 213 mbedtls_mpi_add_mpi( &X, &X, NULL ) ); 214 215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 216 mbedtls_mpi_sub_mpi( NULL, &X, &X ) ); 217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 218 mbedtls_mpi_sub_mpi( &X, NULL, &X ) ); 219 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 220 mbedtls_mpi_sub_mpi( &X, &X, NULL ) ); 221 222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 223 mbedtls_mpi_add_int( NULL, &X, 42 ) ); 224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 225 mbedtls_mpi_add_int( &X, NULL, 42 ) ); 226 227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 228 mbedtls_mpi_sub_int( NULL, &X, 42 ) ); 229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 230 mbedtls_mpi_sub_int( &X, NULL, 42 ) ); 231 232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 233 mbedtls_mpi_mul_mpi( NULL, &X, &X ) ); 234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 235 mbedtls_mpi_mul_mpi( &X, NULL, &X ) ); 236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 237 mbedtls_mpi_mul_mpi( &X, &X, NULL ) ); 238 239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 240 mbedtls_mpi_mul_int( NULL, &X, 42 ) ); 241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 242 mbedtls_mpi_mul_int( &X, NULL, 42 ) ); 243 244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 245 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) ); 246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 247 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) ); 248 249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 250 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) ); 251 252 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) ); 253 254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 255 mbedtls_mpi_mod_mpi( NULL, &X, &X ) ); 256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 257 mbedtls_mpi_mod_mpi( &X, NULL, &X ) ); 258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 259 mbedtls_mpi_mod_mpi( &X, &X, NULL ) ); 260 261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 262 mbedtls_mpi_mod_int( NULL, &X, 42 ) ); 263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 264 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) ); 265 266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 267 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) ); 268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 269 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) ); 270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 271 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) ); 272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 273 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) ); 274 275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 276 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand, 277 NULL ) ); 278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 279 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) ); 280 281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 282 mbedtls_mpi_gcd( NULL, &X, &X ) ); 283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 284 mbedtls_mpi_gcd( &X, NULL, &X ) ); 285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 286 mbedtls_mpi_gcd( &X, &X, NULL ) ); 287 288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 289 mbedtls_mpi_inv_mod( NULL, &X, &X ) ); 290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 291 mbedtls_mpi_inv_mod( &X, NULL, &X ) ); 292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, 293 mbedtls_mpi_inv_mod( &X, &X, NULL ) ); 294 295exit: 296 return; 297} 298/* END_CASE */ 299 300/* BEGIN_CASE */ 301void mpi_null( ) 302{ 303 mbedtls_mpi X, Y, Z; 304 305 mbedtls_mpi_init( &X ); 306 mbedtls_mpi_init( &Y ); 307 mbedtls_mpi_init( &Z ); 308 309 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); 310 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); 311 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); 312 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); 313 314exit: 315 mbedtls_mpi_free( &X ); 316} 317/* END_CASE */ 318 319/* BEGIN_CASE */ 320void mpi_read_write_string( int radix_X, char * input_X, int radix_A, 321 char * input_A, int output_size, int result_read, 322 int result_write ) 323{ 324 mbedtls_mpi X; 325 char str[1000]; 326 size_t len; 327 328 mbedtls_mpi_init( &X ); 329 330 memset( str, '!', sizeof( str ) ); 331 332 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read ); 333 if( result_read == 0 ) 334 { 335 TEST_ASSERT( sign_is_valid( &X ) ); 336 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write ); 337 if( result_write == 0 ) 338 { 339 TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); 340 TEST_ASSERT( str[len] == '!' ); 341 } 342 } 343 344exit: 345 mbedtls_mpi_free( &X ); 346} 347/* END_CASE */ 348 349/* BEGIN_CASE */ 350void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A ) 351{ 352 mbedtls_mpi X; 353 unsigned char str[1000]; 354 size_t len; 355 356 mbedtls_mpi_init( &X ); 357 358 359 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 ); 360 TEST_ASSERT( sign_is_valid( &X ) ); 361 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 ); 362 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); 363 364exit: 365 mbedtls_mpi_free( &X ); 366} 367/* END_CASE */ 368 369/* BEGIN_CASE */ 370void mbedtls_mpi_write_binary( int radix_X, char * input_X, 371 data_t * input_A, int output_size, 372 int result ) 373{ 374 mbedtls_mpi X; 375 unsigned char buf[1000]; 376 size_t buflen; 377 378 memset( buf, 0x00, 1000 ); 379 380 mbedtls_mpi_init( &X ); 381 382 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 383 384 buflen = mbedtls_mpi_size( &X ); 385 if( buflen > (size_t) output_size ) 386 buflen = (size_t) output_size; 387 388 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result ); 389 if( result == 0) 390 { 391 392 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, 393 buflen, input_A->len ) == 0 ); 394 } 395 396exit: 397 mbedtls_mpi_free( &X ); 398} 399/* END_CASE */ 400 401/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ 402void mbedtls_mpi_read_file( int radix_X, char * input_file, 403 data_t * input_A, int result ) 404{ 405 mbedtls_mpi X; 406 unsigned char buf[1000]; 407 size_t buflen; 408 FILE *file; 409 int ret; 410 411 memset( buf, 0x00, 1000 ); 412 413 mbedtls_mpi_init( &X ); 414 415 file = fopen( input_file, "r" ); 416 TEST_ASSERT( file != NULL ); 417 ret = mbedtls_mpi_read_file( &X, radix_X, file ); 418 fclose(file); 419 TEST_ASSERT( ret == result ); 420 421 if( result == 0 ) 422 { 423 TEST_ASSERT( sign_is_valid( &X ) ); 424 buflen = mbedtls_mpi_size( &X ); 425 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 ); 426 427 428 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, 429 buflen, input_A->len ) == 0 ); 430 } 431 432exit: 433 mbedtls_mpi_free( &X ); 434} 435/* END_CASE */ 436 437/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ 438void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix, 439 char * output_file ) 440{ 441 mbedtls_mpi X, Y; 442 FILE *file_out, *file_in; 443 int ret; 444 445 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 446 447 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 448 449 file_out = fopen( output_file, "w" ); 450 TEST_ASSERT( file_out != NULL ); 451 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out ); 452 fclose(file_out); 453 TEST_ASSERT( ret == 0 ); 454 455 file_in = fopen( output_file, "r" ); 456 TEST_ASSERT( file_in != NULL ); 457 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in ); 458 fclose(file_in); 459 TEST_ASSERT( ret == 0 ); 460 461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 462 463exit: 464 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 465} 466/* END_CASE */ 467 468/* BEGIN_CASE */ 469void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val ) 470{ 471 mbedtls_mpi X; 472 mbedtls_mpi_init( &X ); 473 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 474 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val ); 475 476exit: 477 mbedtls_mpi_free( &X ); 478} 479/* END_CASE */ 480 481/* BEGIN_CASE */ 482void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val, 483 int radix_Y, char * output_Y, int result ) 484{ 485 mbedtls_mpi X, Y; 486 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 487 488 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 489 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 ); 490 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); 491 492 if( result == 0 ) 493 { 494 TEST_ASSERT( sign_is_valid( &X ) ); 495 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); 496 } 497 498exit: 499 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 500} 501/* END_CASE */ 502 503/* BEGIN_CASE */ 504void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits ) 505{ 506 mbedtls_mpi X; 507 mbedtls_mpi_init( &X ); 508 509 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 510 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits ); 511 512exit: 513 mbedtls_mpi_free( &X ); 514} 515/* END_CASE */ 516 517/* BEGIN_CASE */ 518void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits ) 519{ 520 mbedtls_mpi X; 521 mbedtls_mpi_init( &X ); 522 523 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 524 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); 525 526exit: 527 mbedtls_mpi_free( &X ); 528} 529/* END_CASE */ 530 531/* BEGIN_CASE */ 532void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y, 533 char * input_Y, int radix_A, char * input_A ) 534{ 535 mbedtls_mpi A, X, Y, Z; 536 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); 537 538 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 539 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 540 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 541 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 ); 542 TEST_ASSERT( sign_is_valid( &Z ) ); 543 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 544 545exit: 546 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); 547} 548/* END_CASE */ 549 550/* BEGIN_CASE */ 551void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP ) 552{ 553 mbedtls_mpi X; 554 mbedtls_mpi_init( &X ); 555 556 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0); 557 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP); 558 559exit: 560 mbedtls_mpi_free( &X ); 561} 562/* END_CASE */ 563 564/* BEGIN_CASE */ 565void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y, 566 char * input_Y, int input_A ) 567{ 568 mbedtls_mpi X, Y; 569 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 570 571 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 572 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 573 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A ); 574 575exit: 576 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 577} 578/* END_CASE */ 579 580/* BEGIN_CASE */ 581void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X, 582 int size_Y, char * input_Y, 583 int input_ret, int input_err ) 584{ 585 unsigned ret = -1; 586 unsigned input_uret = input_ret; 587 mbedtls_mpi X, Y; 588 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 589 590 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 ); 591 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 ); 592 593 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 ); 594 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 ); 595 596 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err ); 597 if( input_err == 0 ) 598 TEST_ASSERT( ret == input_uret ); 599 600exit: 601 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 602} 603/* END_CASE */ 604 605/* BEGIN_CASE */ 606void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y, 607 char * input_Y, int input_A ) 608{ 609 mbedtls_mpi X, Y; 610 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 611 612 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 613 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 614 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A ); 615 616exit: 617 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 618} 619/* END_CASE */ 620 621/* BEGIN_CASE */ 622void mbedtls_mpi_copy( char *src_hex, char *dst_hex ) 623{ 624 mbedtls_mpi src, dst, ref; 625 mbedtls_mpi_init( &src ); 626 mbedtls_mpi_init( &dst ); 627 mbedtls_mpi_init( &ref ); 628 629 TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 ); 630 TEST_ASSERT( mbedtls_test_read_mpi( &ref, 16, dst_hex ) == 0 ); 631 632 /* mbedtls_mpi_copy() */ 633 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 ); 634 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 ); 635 TEST_ASSERT( sign_is_valid( &dst ) ); 636 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); 637 638 /* mbedtls_mpi_safe_cond_assign(), assignment done */ 639 mbedtls_mpi_free( &dst ); 640 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 ); 641 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 ); 642 TEST_ASSERT( sign_is_valid( &dst ) ); 643 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); 644 645 /* mbedtls_mpi_safe_cond_assign(), assignment not done */ 646 mbedtls_mpi_free( &dst ); 647 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 ); 648 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 ); 649 TEST_ASSERT( sign_is_valid( &dst ) ); 650 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 ); 651 652exit: 653 mbedtls_mpi_free( &src ); 654 mbedtls_mpi_free( &dst ); 655 mbedtls_mpi_free( &ref ); 656} 657/* END_CASE */ 658 659/* BEGIN_CASE */ 660void mpi_copy_self( char *input_X ) 661{ 662 mbedtls_mpi X, A; 663 mbedtls_mpi_init( &A ); 664 mbedtls_mpi_init( &X ); 665 666 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 ); 667 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); 668 669 TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 ); 670 TEST_ASSERT( sign_is_valid( &X ) ); 671 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 672 673exit: 674 mbedtls_mpi_free( &A ); 675 mbedtls_mpi_free( &X ); 676} 677/* END_CASE */ 678 679/* BEGIN_CASE */ 680void mbedtls_mpi_swap( char *X_hex, char *Y_hex ) 681{ 682 mbedtls_mpi X, Y, X0, Y0; 683 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 684 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 ); 685 686 TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 ); 687 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, 16, Y_hex ) == 0 ); 688 689 /* mbedtls_mpi_swap() */ 690 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 ); 691 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 ); 692 mbedtls_mpi_swap( &X, &Y ); 693 TEST_ASSERT( sign_is_valid( &X ) ); 694 TEST_ASSERT( sign_is_valid( &Y ) ); 695 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); 696 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); 697 698 /* mbedtls_mpi_safe_cond_swap(), swap done */ 699 mbedtls_mpi_free( &X ); 700 mbedtls_mpi_free( &Y ); 701 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 ); 702 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 ); 703 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 ); 704 TEST_ASSERT( sign_is_valid( &X ) ); 705 TEST_ASSERT( sign_is_valid( &Y ) ); 706 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); 707 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); 708 709 /* mbedtls_mpi_safe_cond_swap(), swap not done */ 710 mbedtls_mpi_free( &X ); 711 mbedtls_mpi_free( &Y ); 712 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 ); 713 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 ); 714 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 ); 715 TEST_ASSERT( sign_is_valid( &X ) ); 716 TEST_ASSERT( sign_is_valid( &Y ) ); 717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); 718 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 ); 719 720exit: 721 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 722 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 ); 723} 724/* END_CASE */ 725 726/* BEGIN_CASE */ 727void mpi_swap_self( char *X_hex ) 728{ 729 mbedtls_mpi X, X0; 730 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 ); 731 732 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 ); 733 TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 ); 734 735 mbedtls_mpi_swap( &X, &X ); 736 TEST_ASSERT( sign_is_valid( &X ) ); 737 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); 738 739exit: 740 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 ); 741} 742/* END_CASE */ 743 744/* BEGIN_CASE */ 745void mbedtls_mpi_shrink( int before, int used, int min, int after ) 746{ 747 mbedtls_mpi X; 748 mbedtls_mpi_init( &X ); 749 750 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 ); 751 if( used > 0 ) 752 { 753 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint ); 754 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 ); 755 } 756 TEST_EQUAL( X.n, (size_t) before ); 757 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 ); 758 TEST_EQUAL( X.n, (size_t) after ); 759 760exit: 761 mbedtls_mpi_free( &X ); 762} 763/* END_CASE */ 764 765/* BEGIN_CASE */ 766void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y, 767 char * input_Y, int radix_A, char * input_A ) 768{ 769 mbedtls_mpi X, Y, Z, A; 770 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 771 772 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 773 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 774 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 775 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 ); 776 TEST_ASSERT( sign_is_valid( &Z ) ); 777 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 778 779 /* result == first operand */ 780 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 ); 781 TEST_ASSERT( sign_is_valid( &X ) ); 782 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 783 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 784 785 /* result == second operand */ 786 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 ); 787 TEST_ASSERT( sign_is_valid( &Y ) ); 788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 789 790exit: 791 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 792} 793/* END_CASE */ 794 795/* BEGIN_CASE */ 796void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A, 797 char * input_A ) 798{ 799 mbedtls_mpi X, A; 800 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 801 802 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 803 804 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 805 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 ); 806 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 ); 807 TEST_ASSERT( sign_is_valid( &X ) ); 808 809 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 810 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 ); 811 TEST_ASSERT( sign_is_valid( &X ) ); 812 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 813 814 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 815 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); 816 TEST_ASSERT( sign_is_valid( &X ) ); 817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 818 819exit: 820 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 821} 822/* END_CASE */ 823 824 825/* BEGIN_CASE */ 826void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y, 827 char * input_Y, int radix_A, char * input_A ) 828{ 829 mbedtls_mpi X, Y, Z, A; 830 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 831 832 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 833 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 834 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 835 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 ); 836 TEST_ASSERT( sign_is_valid( &Z ) ); 837 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 838 839 /* result == first operand */ 840 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 ); 841 TEST_ASSERT( sign_is_valid( &X ) ); 842 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 843 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 844 845 /* result == second operand */ 846 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 ); 847 TEST_ASSERT( sign_is_valid( &Y ) ); 848 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 849 850exit: 851 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 852} 853/* END_CASE */ 854 855/* BEGIN_CASE */ 856void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y, 857 int radix_A, char * input_A ) 858{ 859 mbedtls_mpi X, Z, A; 860 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 861 862 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 863 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 864 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 ); 865 TEST_ASSERT( sign_is_valid( &Z ) ); 866 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 867 868exit: 869 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 870} 871/* END_CASE */ 872 873/* BEGIN_CASE */ 874void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y, 875 char * input_Y, int radix_A, char * input_A ) 876{ 877 mbedtls_mpi X, Y, Z, A; 878 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 879 880 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 881 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 882 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 883 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 ); 884 TEST_ASSERT( sign_is_valid( &Z ) ); 885 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 886 887 /* result == first operand */ 888 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 ); 889 TEST_ASSERT( sign_is_valid( &X ) ); 890 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 891 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 892 893 /* result == second operand */ 894 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 ); 895 TEST_ASSERT( sign_is_valid( &Y ) ); 896 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 897 898exit: 899 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 900} 901/* END_CASE */ 902 903/* BEGIN_CASE */ 904void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y, 905 char * input_Y, int radix_A, char * input_A, 906 int sub_result ) 907{ 908 mbedtls_mpi X, Y, Z, A; 909 int res; 910 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 911 912 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 913 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 914 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 915 916 res = mbedtls_mpi_sub_abs( &Z, &X, &Y ); 917 TEST_ASSERT( res == sub_result ); 918 TEST_ASSERT( sign_is_valid( &Z ) ); 919 if( res == 0 ) 920 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 921 922 /* result == first operand */ 923 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result ); 924 TEST_ASSERT( sign_is_valid( &X ) ); 925 if( sub_result == 0 ) 926 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 927 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 928 929 /* result == second operand */ 930 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result ); 931 TEST_ASSERT( sign_is_valid( &Y ) ); 932 if( sub_result == 0 ) 933 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); 934 935exit: 936 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 937} 938/* END_CASE */ 939 940/* BEGIN_CASE */ 941void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y, 942 int radix_A, char * input_A ) 943{ 944 mbedtls_mpi X, Z, A; 945 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 946 947 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 948 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 949 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 ); 950 TEST_ASSERT( sign_is_valid( &Z ) ); 951 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 952 953exit: 954 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 955} 956/* END_CASE */ 957 958/* BEGIN_CASE */ 959void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y, 960 char * input_Y, int radix_A, char * input_A ) 961{ 962 mbedtls_mpi X, Y, Z, A; 963 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 964 965 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 966 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 967 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 968 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 ); 969 TEST_ASSERT( sign_is_valid( &Z ) ); 970 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 971 972exit: 973 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 974} 975/* END_CASE */ 976 977/* BEGIN_CASE */ 978void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y, 979 int radix_A, char * input_A, 980 char * result_comparison ) 981{ 982 mbedtls_mpi X, Z, A; 983 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 984 985 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 986 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 987 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 ); 988 TEST_ASSERT( sign_is_valid( &Z ) ); 989 if( strcmp( result_comparison, "==" ) == 0 ) 990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 991 else if( strcmp( result_comparison, "!=" ) == 0 ) 992 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 ); 993 else 994 TEST_ASSERT( "unknown operator" == 0 ); 995 996exit: 997 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 998} 999/* END_CASE */ 1000 1001/* BEGIN_CASE */ 1002void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y, 1003 char * input_Y, int radix_A, char * input_A, 1004 int radix_B, char * input_B, int div_result ) 1005{ 1006 mbedtls_mpi X, Y, Q, R, A, B; 1007 int res; 1008 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); 1009 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B ); 1010 1011 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1012 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 1013 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1014 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 ); 1015 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y ); 1016 TEST_ASSERT( res == div_result ); 1017 if( res == 0 ) 1018 { 1019 TEST_ASSERT( sign_is_valid( &Q ) ); 1020 TEST_ASSERT( sign_is_valid( &R ) ); 1021 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); 1022 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); 1023 } 1024 1025exit: 1026 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); 1027 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B ); 1028} 1029/* END_CASE */ 1030 1031/* BEGIN_CASE */ 1032void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y, 1033 int radix_A, char * input_A, int radix_B, 1034 char * input_B, int div_result ) 1035{ 1036 mbedtls_mpi X, Q, R, A, B; 1037 int res; 1038 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A ); 1039 mbedtls_mpi_init( &B ); 1040 1041 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1042 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1043 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 ); 1044 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y ); 1045 TEST_ASSERT( res == div_result ); 1046 if( res == 0 ) 1047 { 1048 TEST_ASSERT( sign_is_valid( &Q ) ); 1049 TEST_ASSERT( sign_is_valid( &R ) ); 1050 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); 1051 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); 1052 } 1053 1054exit: 1055 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A ); 1056 mbedtls_mpi_free( &B ); 1057} 1058/* END_CASE */ 1059 1060/* BEGIN_CASE */ 1061void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y, 1062 char * input_Y, int radix_A, char * input_A, 1063 int div_result ) 1064{ 1065 mbedtls_mpi X, Y, A; 1066 int res; 1067 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); 1068 1069 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1070 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 1071 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1072 res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); 1073 TEST_ASSERT( res == div_result ); 1074 if( res == 0 ) 1075 { 1076 TEST_ASSERT( sign_is_valid( &X ) ); 1077 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 1078 } 1079 1080exit: 1081 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); 1082} 1083/* END_CASE */ 1084 1085/* BEGIN_CASE */ 1086void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y, 1087 int input_A, int div_result ) 1088{ 1089 mbedtls_mpi X; 1090 int res; 1091 mbedtls_mpi_uint r; 1092 mbedtls_mpi_init( &X ); 1093 1094 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1095 res = mbedtls_mpi_mod_int( &r, &X, input_Y ); 1096 TEST_ASSERT( res == div_result ); 1097 if( res == 0 ) 1098 { 1099 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); 1100 } 1101 1102exit: 1103 mbedtls_mpi_free( &X ); 1104} 1105/* END_CASE */ 1106 1107/* BEGIN_CASE */ 1108void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E, 1109 char * input_E, int radix_N, char * input_N, 1110 int radix_X, char * input_X, int exp_result ) 1111{ 1112 mbedtls_mpi A, E, N, RR, Z, X; 1113 int res; 1114 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); 1115 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X ); 1116 1117 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1118 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 1119 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 1120 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1121 1122 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL ); 1123 TEST_ASSERT( res == exp_result ); 1124 if( res == 0 ) 1125 { 1126 TEST_ASSERT( sign_is_valid( &Z ) ); 1127 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); 1128 } 1129 1130 /* Now test again with the speed-up parameter supplied as an output. */ 1131 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); 1132 TEST_ASSERT( res == exp_result ); 1133 if( res == 0 ) 1134 { 1135 TEST_ASSERT( sign_is_valid( &Z ) ); 1136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); 1137 } 1138 1139 /* Now test again with the speed-up parameter supplied in calculated form. */ 1140 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); 1141 TEST_ASSERT( res == exp_result ); 1142 if( res == 0 ) 1143 { 1144 TEST_ASSERT( sign_is_valid( &Z ) ); 1145 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); 1146 } 1147 1148exit: 1149 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); 1150 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X ); 1151} 1152/* END_CASE */ 1153 1154/* BEGIN_CASE */ 1155void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes, 1156 int radix_RR, char * input_RR, int exp_result ) 1157{ 1158 mbedtls_mpi A, E, N, RR, Z; 1159 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); 1160 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); 1161 1162 /* Set A to 2^(A_bytes - 1) + 1 */ 1163 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 ); 1164 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 ); 1165 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 ); 1166 1167 /* Set E to 2^(E_bytes - 1) + 1 */ 1168 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 ); 1169 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 ); 1170 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 ); 1171 1172 /* Set N to 2^(N_bytes - 1) + 1 */ 1173 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 ); 1174 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 ); 1175 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 ); 1176 1177 if( strlen( input_RR ) ) 1178 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 ); 1179 1180 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result ); 1181 1182exit: 1183 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); 1184 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); 1185} 1186/* END_CASE */ 1187 1188/* BEGIN_CASE */ 1189void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y, 1190 char * input_Y, int radix_A, char * input_A, 1191 int div_result ) 1192{ 1193 mbedtls_mpi X, Y, Z, A; 1194 int res; 1195 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); 1196 1197 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1198 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 ); 1199 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1200 res = mbedtls_mpi_inv_mod( &Z, &X, &Y ); 1201 TEST_ASSERT( res == div_result ); 1202 if( res == 0 ) 1203 { 1204 TEST_ASSERT( sign_is_valid( &Z ) ); 1205 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); 1206 } 1207 1208exit: 1209 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); 1210} 1211/* END_CASE */ 1212 1213/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ 1214void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result ) 1215{ 1216 mbedtls_mpi X; 1217 int res; 1218 mbedtls_mpi_init( &X ); 1219 1220 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1221 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL ); 1222 TEST_ASSERT( res == div_result ); 1223 1224exit: 1225 mbedtls_mpi_free( &X ); 1226} 1227/* END_CASE */ 1228 1229/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ 1230void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses, 1231 int chunk_len, int rounds ) 1232{ 1233 mbedtls_mpi X; 1234 int res; 1235 mbedtls_test_mpi_random rand; 1236 1237 mbedtls_mpi_init( &X ); 1238 rand.data = witnesses; 1239 rand.pos = 0; 1240 rand.chunk_len = chunk_len; 1241 1242 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 ); 1243 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1, 1244 mbedtls_test_mpi_miller_rabin_determinizer, 1245 &rand ); 1246 TEST_ASSERT( res == 0 ); 1247 1248 rand.data = witnesses; 1249 rand.pos = 0; 1250 rand.chunk_len = chunk_len; 1251 1252 res = mbedtls_mpi_is_prime_ext( &X, rounds, 1253 mbedtls_test_mpi_miller_rabin_determinizer, 1254 &rand ); 1255 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); 1256 1257exit: 1258 mbedtls_mpi_free( &X ); 1259} 1260/* END_CASE */ 1261 1262/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ 1263void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret ) 1264{ 1265 mbedtls_mpi X; 1266 int my_ret; 1267 1268 mbedtls_mpi_init( &X ); 1269 1270 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL ); 1271 TEST_ASSERT( my_ret == ref_ret ); 1272 1273 if( ref_ret == 0 ) 1274 { 1275 size_t actual_bits = mbedtls_mpi_bitlen( &X ); 1276 1277 TEST_ASSERT( actual_bits >= (size_t) bits ); 1278 TEST_ASSERT( actual_bits <= (size_t) bits + 1 ); 1279 TEST_ASSERT( sign_is_valid( &X ) ); 1280 1281 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL ) 1282 == 0 ); 1283 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) 1284 { 1285 /* X = ( X - 1 ) / 2 */ 1286 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 ); 1287 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL ) 1288 == 0 ); 1289 } 1290 } 1291 1292exit: 1293 mbedtls_mpi_free( &X ); 1294} 1295/* END_CASE */ 1296 1297/* BEGIN_CASE */ 1298void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X, 1299 int radix_A, char * input_A ) 1300{ 1301 mbedtls_mpi X, A; 1302 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 1303 1304 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1305 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1306 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 ); 1307 TEST_ASSERT( sign_is_valid( &X ) ); 1308 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 1309 1310exit: 1311 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 1312} 1313/* END_CASE */ 1314 1315/* BEGIN_CASE */ 1316void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X, 1317 int radix_A, char * input_A ) 1318{ 1319 mbedtls_mpi X, A; 1320 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); 1321 1322 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 ); 1323 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 ); 1324 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 ); 1325 TEST_ASSERT( sign_is_valid( &X ) ); 1326 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); 1327 1328exit: 1329 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); 1330} 1331/* END_CASE */ 1332 1333/* BEGIN_CASE */ 1334void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret ) 1335{ 1336 mbedtls_mpi X; 1337 int ret; 1338 size_t bytes_left = rng_bytes; 1339 mbedtls_mpi_init( &X ); 1340 1341 ret = mbedtls_mpi_fill_random( &X, wanted_bytes, 1342 f_rng_bytes_left, &bytes_left ); 1343 TEST_ASSERT( ret == expected_ret ); 1344 1345 if( expected_ret == 0 ) 1346 { 1347 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG 1348 * as a big-endian representation of the number. We know when 1349 * our RNG function returns null bytes, so we know how many 1350 * leading zero bytes the number has. */ 1351 size_t leading_zeros = 0; 1352 if( wanted_bytes > 0 && rng_bytes % 256 == 0 ) 1353 leading_zeros = 1; 1354 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros == 1355 (size_t) wanted_bytes ); 1356 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes ); 1357 TEST_ASSERT( sign_is_valid( &X ) ); 1358 } 1359 1360exit: 1361 mbedtls_mpi_free( &X ); 1362} 1363/* END_CASE */ 1364 1365/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 1366void mpi_selftest( ) 1367{ 1368 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 ); 1369} 1370/* END_CASE */ 1371