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