1/* BEGIN_HEADER */ 2#include "mbedtls/rsa.h" 3#include "rsa_alt_helpers.h" 4#include "mbedtls/md5.h" 5#include "mbedtls/sha1.h" 6#include "mbedtls/sha256.h" 7#include "mbedtls/sha512.h" 8#include "mbedtls/entropy.h" 9#include "mbedtls/ctr_drbg.h" 10 11/* END_HEADER */ 12 13/* BEGIN_DEPENDENCIES 14 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME 15 * END_DEPENDENCIES 16 */ 17 18/* BEGIN_CASE */ 19void rsa_invalid_param( ) 20{ 21 mbedtls_rsa_context ctx; 22 const int invalid_padding = 42; 23 const int invalid_hash_id = 0xff; 24 25 mbedtls_rsa_init( &ctx ); 26 27 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx, 28 invalid_padding, 29 MBEDTLS_MD_NONE ), 30 MBEDTLS_ERR_RSA_INVALID_PADDING ); 31 32 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx, 33 MBEDTLS_RSA_PKCS_V21, 34 invalid_hash_id ), 35 MBEDTLS_ERR_RSA_INVALID_PADDING ); 36 37#if !defined(MBEDTLS_PKCS1_V15) 38 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx, 39 MBEDTLS_RSA_PKCS_V15, 40 MBEDTLS_MD_NONE ), 41 MBEDTLS_ERR_RSA_INVALID_PADDING ); 42#endif 43 44#if !defined(MBEDTLS_PKCS1_V21) 45 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx, 46 MBEDTLS_RSA_PKCS_V21, 47 MBEDTLS_MD_NONE ), 48 MBEDTLS_ERR_RSA_INVALID_PADDING ); 49#endif 50 51exit: 52 mbedtls_rsa_free( &ctx ); 53} 54/* END_CASE */ 55 56/* BEGIN_CASE */ 57void rsa_init_free( int reinit ) 58{ 59 mbedtls_rsa_context ctx; 60 61 /* Double free is not explicitly documented to work, but we rely on it 62 * even inside the library so that you can call mbedtls_rsa_free() 63 * unconditionally on an error path without checking whether it has 64 * already been called in the success path. */ 65 66 mbedtls_rsa_init( &ctx ); 67 mbedtls_rsa_free( &ctx ); 68 69 if( reinit ) 70 mbedtls_rsa_init( &ctx ); 71 mbedtls_rsa_free( &ctx ); 72 73 /* This test case always succeeds, functionally speaking. A plausible 74 * bug might trigger an invalid pointer dereference or a memory leak. */ 75 goto exit; 76} 77/* END_CASE */ 78 79/* BEGIN_CASE */ 80void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode, 81 int digest, int mod, int radix_P, char * input_P, 82 int radix_Q, char * input_Q, int radix_N, 83 char * input_N, int radix_E, char * input_E, 84 data_t * result_str, int result ) 85{ 86 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; 87 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( digest ); 88 unsigned char output[256]; 89 mbedtls_rsa_context ctx; 90 mbedtls_mpi N, P, Q, E; 91 mbedtls_test_rnd_pseudo_info rnd_info; 92 93 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); 94 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); 95 mbedtls_rsa_init( &ctx ); 96 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode, 97 MBEDTLS_MD_NONE ) == 0 ); 98 99 memset( hash_result, 0x00, sizeof( hash_result ) ); 100 memset( output, 0x00, sizeof( output ) ); 101 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 102 103 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 104 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 105 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 106 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 107 108 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); 109 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 110 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 111 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); 112 113 if( md_info != NULL ) 114 TEST_ASSERT( mbedtls_md( md_info, message_str->x, message_str->len, hash_result ) == 0 ); 115 116 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( 117 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info, 118 digest, mbedtls_md_get_size( md_info ), hash_result, 119 output ) == result ); 120 if( result == 0 ) 121 { 122 123 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 124 ctx.len, result_str->len ) == 0 ); 125 } 126 127exit: 128 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); 129 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); 130 mbedtls_rsa_free( &ctx ); 131} 132/* END_CASE */ 133 134/* BEGIN_CASE */ 135void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode, 136 int digest, int mod, int radix_N, 137 char * input_N, int radix_E, char * input_E, 138 data_t * result_str, int result ) 139{ 140 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; 141 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( digest ); 142 mbedtls_rsa_context ctx; 143 mbedtls_mpi N, E; 144 145 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 146 mbedtls_rsa_init( &ctx ); 147 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 148 MBEDTLS_MD_NONE ) == 0 ); 149 memset( hash_result, 0x00, sizeof( hash_result ) ); 150 151 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 152 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 153 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 154 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 155 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); 156 157 if( md_info != NULL ) 158 TEST_ASSERT( mbedtls_md( md_info, message_str->x, message_str->len, hash_result ) == 0 ); 159 160 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, mbedtls_md_get_size( md_info ), hash_result, result_str->x ) == result ); 161 162exit: 163 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 164 mbedtls_rsa_free( &ctx ); 165} 166/* END_CASE */ 167 168 169/* BEGIN_CASE */ 170void rsa_pkcs1_sign_raw( data_t * hash_result, 171 int padding_mode, int mod, int radix_P, 172 char * input_P, int radix_Q, char * input_Q, 173 int radix_N, char * input_N, int radix_E, 174 char * input_E, data_t * result_str ) 175{ 176 unsigned char output[256]; 177 mbedtls_rsa_context ctx; 178 mbedtls_mpi N, P, Q, E; 179 mbedtls_test_rnd_pseudo_info rnd_info; 180 181 mbedtls_rsa_init( &ctx ); 182 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); 183 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); 184 185 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 186 MBEDTLS_MD_NONE ) == 0 ); 187 188 memset( output, 0x00, sizeof( output ) ); 189 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 190 191 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 192 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 193 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 194 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 195 196 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); 197 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 198 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 199 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); 200 201 202 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand, 203 &rnd_info, MBEDTLS_MD_NONE, 204 hash_result->len, 205 hash_result->x, output ) == 0 ); 206 207 208 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 209 ctx.len, result_str->len ) == 0 ); 210 211exit: 212 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); 213 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); 214 215 mbedtls_rsa_free( &ctx ); 216} 217/* END_CASE */ 218 219/* BEGIN_CASE */ 220void rsa_pkcs1_verify_raw( data_t * hash_result, 221 int padding_mode, int mod, int radix_N, 222 char * input_N, int radix_E, char * input_E, 223 data_t * result_str, int correct ) 224{ 225 unsigned char output[256]; 226 mbedtls_rsa_context ctx; 227 228 mbedtls_mpi N, E; 229 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 230 231 mbedtls_rsa_init( &ctx ); 232 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 233 MBEDTLS_MD_NONE ) == 0 ); 234 memset( output, 0x00, sizeof( output ) ); 235 236 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 237 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 238 239 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 240 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 241 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); 242 243 244 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct ); 245 246exit: 247 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 248 mbedtls_rsa_free( &ctx ); 249} 250/* END_CASE */ 251 252/* BEGIN_CASE */ 253void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode, 254 int mod, int radix_N, char * input_N, 255 int radix_E, char * input_E, 256 data_t * result_str, int result ) 257{ 258 unsigned char output[256]; 259 mbedtls_rsa_context ctx; 260 mbedtls_test_rnd_pseudo_info rnd_info; 261 262 mbedtls_mpi N, E; 263 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 264 265 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 266 267 mbedtls_rsa_init( &ctx ); 268 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 269 MBEDTLS_MD_NONE ) == 0 ); 270 memset( output, 0x00, sizeof( output ) ); 271 272 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 273 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 274 275 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 276 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 277 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); 278 279 280 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, 281 &mbedtls_test_rnd_pseudo_rand, 282 &rnd_info, message_str->len, 283 message_str->x, 284 output ) == result ); 285 if( result == 0 ) 286 { 287 288 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 289 ctx.len, result_str->len ) == 0 ); 290 } 291 292exit: 293 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 294 mbedtls_rsa_free( &ctx ); 295} 296/* END_CASE */ 297 298/* BEGIN_CASE */ 299void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode, 300 int mod, int radix_N, char * input_N, 301 int radix_E, char * input_E, 302 data_t * result_str, int result ) 303{ 304 unsigned char output[256]; 305 mbedtls_rsa_context ctx; 306 307 mbedtls_mpi N, E; 308 309 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 310 mbedtls_rsa_init( &ctx ); 311 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 312 MBEDTLS_MD_NONE ) == 0 ); 313 memset( output, 0x00, sizeof( output ) ); 314 315 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 316 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 317 318 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 319 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 320 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); 321 322 323 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand, 324 NULL, message_str->len, 325 message_str->x, 326 output ) == result ); 327 if( result == 0 ) 328 { 329 330 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 331 ctx.len, result_str->len ) == 0 ); 332 } 333 334exit: 335 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 336 mbedtls_rsa_free( &ctx ); 337} 338/* END_CASE */ 339 340/* BEGIN_CASE */ 341void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode, 342 int mod, int radix_P, char * input_P, 343 int radix_Q, char * input_Q, int radix_N, 344 char * input_N, int radix_E, char * input_E, 345 int max_output, data_t * result_str, 346 int result ) 347{ 348 unsigned char output[32]; 349 mbedtls_rsa_context ctx; 350 size_t output_len; 351 mbedtls_test_rnd_pseudo_info rnd_info; 352 mbedtls_mpi N, P, Q, E; 353 354 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); 355 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); 356 357 mbedtls_rsa_init( &ctx ); 358 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode, 359 MBEDTLS_MD_NONE ) == 0 ); 360 361 memset( output, 0x00, sizeof( output ) ); 362 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 363 364 365 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 366 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 367 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 368 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 369 370 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); 371 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 372 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 373 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); 374 375 output_len = 0; 376 377 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand, 378 &rnd_info, 379 &output_len, message_str->x, output, 380 max_output ) == result ); 381 if( result == 0 ) 382 { 383 384 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 385 output_len, 386 result_str->len ) == 0 ); 387 } 388 389exit: 390 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); 391 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); 392 mbedtls_rsa_free( &ctx ); 393} 394/* END_CASE */ 395 396/* BEGIN_CASE */ 397void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N, 398 char * input_N, int radix_E, char * input_E, 399 data_t * result_str, int result ) 400{ 401 unsigned char output[256]; 402 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ 403 404 mbedtls_mpi N, E; 405 406 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 407 mbedtls_rsa_init( &ctx ); 408 mbedtls_rsa_init( &ctx2 ); 409 memset( output, 0x00, sizeof( output ) ); 410 411 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 412 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 413 414 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 415 416 /* Check test data consistency */ 417 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) ); 418 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 419 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); 420 421 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result ); 422 if( result == 0 ) 423 { 424 425 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 426 ctx.len, result_str->len ) == 0 ); 427 } 428 429 /* And now with the copy */ 430 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); 431 /* clear the original to be sure */ 432 mbedtls_rsa_free( &ctx ); 433 434 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 ); 435 436 memset( output, 0x00, sizeof( output ) ); 437 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result ); 438 if( result == 0 ) 439 { 440 441 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 442 ctx.len, result_str->len ) == 0 ); 443 } 444 445exit: 446 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 447 mbedtls_rsa_free( &ctx ); 448 mbedtls_rsa_free( &ctx2 ); 449} 450/* END_CASE */ 451 452/* BEGIN_CASE */ 453void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P, 454 char * input_P, int radix_Q, char * input_Q, 455 int radix_N, char * input_N, int radix_E, 456 char * input_E, data_t * result_str, 457 int result ) 458{ 459 unsigned char output[256]; 460 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ 461 mbedtls_mpi N, P, Q, E; 462 mbedtls_test_rnd_pseudo_info rnd_info; 463 int i; 464 465 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); 466 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); 467 mbedtls_rsa_init( &ctx ); 468 mbedtls_rsa_init( &ctx2 ); 469 470 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); 471 472 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 473 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 474 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 475 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 476 477 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); 478 479 /* Check test data consistency */ 480 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) ); 481 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); 482 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 483 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); 484 485 /* repeat three times to test updating of blinding values */ 486 for( i = 0; i < 3; i++ ) 487 { 488 memset( output, 0x00, sizeof( output ) ); 489 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand, 490 &rnd_info, message_str->x, 491 output ) == result ); 492 if( result == 0 ) 493 { 494 495 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 496 ctx.len, 497 result_str->len ) == 0 ); 498 } 499 } 500 501 /* And now one more time with the copy */ 502 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); 503 /* clear the original to be sure */ 504 mbedtls_rsa_free( &ctx ); 505 506 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 ); 507 508 memset( output, 0x00, sizeof( output ) ); 509 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand, 510 &rnd_info, message_str->x, 511 output ) == result ); 512 if( result == 0 ) 513 { 514 515 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 516 ctx2.len, 517 result_str->len ) == 0 ); 518 } 519 520exit: 521 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); 522 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); 523 524 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 ); 525} 526/* END_CASE */ 527 528/* BEGIN_CASE */ 529void rsa_check_privkey_null( ) 530{ 531 mbedtls_rsa_context ctx; 532 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) ); 533 534 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); 535} 536/* END_CASE */ 537 538/* BEGIN_CASE */ 539void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E, 540 char * input_E, int result ) 541{ 542 mbedtls_rsa_context ctx; 543 mbedtls_mpi N, E; 544 545 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); 546 mbedtls_rsa_init( &ctx ); 547 548 if( strlen( input_N ) ) 549 { 550 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 551 } 552 if( strlen( input_E ) ) 553 { 554 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 555 } 556 557 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); 558 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result ); 559 560exit: 561 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); 562 mbedtls_rsa_free( &ctx ); 563} 564/* END_CASE */ 565 566/* BEGIN_CASE */ 567void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P, 568 int radix_Q, char * input_Q, int radix_N, 569 char * input_N, int radix_E, char * input_E, 570 int radix_D, char * input_D, int radix_DP, 571 char * input_DP, int radix_DQ, 572 char * input_DQ, int radix_QP, 573 char * input_QP, int result ) 574{ 575 mbedtls_rsa_context ctx; 576 577 mbedtls_rsa_init( &ctx ); 578 579 ctx.len = mod / 8; 580 if( strlen( input_P ) ) 581 { 582 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, radix_P, input_P ) == 0 ); 583 } 584 if( strlen( input_Q ) ) 585 { 586 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, radix_Q, input_Q ) == 0 ); 587 } 588 if( strlen( input_N ) ) 589 { 590 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, radix_N, input_N ) == 0 ); 591 } 592 if( strlen( input_E ) ) 593 { 594 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, radix_E, input_E ) == 0 ); 595 } 596 if( strlen( input_D ) ) 597 { 598 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, radix_D, input_D ) == 0 ); 599 } 600#if !defined(MBEDTLS_RSA_NO_CRT) 601 if( strlen( input_DP ) ) 602 { 603 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, radix_DP, input_DP ) == 0 ); 604 } 605 if( strlen( input_DQ ) ) 606 { 607 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, radix_DQ, input_DQ ) == 0 ); 608 } 609 if( strlen( input_QP ) ) 610 { 611 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, radix_QP, input_QP ) == 0 ); 612 } 613#else 614 ((void) radix_DP); ((void) input_DP); 615 ((void) radix_DQ); ((void) input_DQ); 616 ((void) radix_QP); ((void) input_QP); 617#endif 618 619 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result ); 620 621exit: 622 mbedtls_rsa_free( &ctx ); 623} 624/* END_CASE */ 625 626/* BEGIN_CASE */ 627void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub, 628 int radix_Epub, char * input_Epub, int radix_P, 629 char * input_P, int radix_Q, char * input_Q, 630 int radix_N, char * input_N, int radix_E, 631 char * input_E, int radix_D, char * input_D, 632 int radix_DP, char * input_DP, int radix_DQ, 633 char * input_DQ, int radix_QP, char * input_QP, 634 int result ) 635{ 636 mbedtls_rsa_context pub, prv; 637 638 mbedtls_rsa_init( &pub ); 639 mbedtls_rsa_init( &prv ); 640 641 pub.len = mod / 8; 642 prv.len = mod / 8; 643 644 if( strlen( input_Npub ) ) 645 { 646 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, radix_Npub, input_Npub ) == 0 ); 647 } 648 if( strlen( input_Epub ) ) 649 { 650 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, radix_Epub, input_Epub ) == 0 ); 651 } 652 653 if( strlen( input_P ) ) 654 { 655 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, radix_P, input_P ) == 0 ); 656 } 657 if( strlen( input_Q ) ) 658 { 659 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, radix_Q, input_Q ) == 0 ); 660 } 661 if( strlen( input_N ) ) 662 { 663 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, radix_N, input_N ) == 0 ); 664 } 665 if( strlen( input_E ) ) 666 { 667 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, radix_E, input_E ) == 0 ); 668 } 669 if( strlen( input_D ) ) 670 { 671 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, radix_D, input_D ) == 0 ); 672 } 673#if !defined(MBEDTLS_RSA_NO_CRT) 674 if( strlen( input_DP ) ) 675 { 676 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, radix_DP, input_DP ) == 0 ); 677 } 678 if( strlen( input_DQ ) ) 679 { 680 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, radix_DQ, input_DQ ) == 0 ); 681 } 682 if( strlen( input_QP ) ) 683 { 684 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, radix_QP, input_QP ) == 0 ); 685 } 686#else 687 ((void) radix_DP); ((void) input_DP); 688 ((void) radix_DQ); ((void) input_DQ); 689 ((void) radix_QP); ((void) input_QP); 690#endif 691 692 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result ); 693 694exit: 695 mbedtls_rsa_free( &pub ); 696 mbedtls_rsa_free( &prv ); 697} 698/* END_CASE */ 699 700/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ 701void mbedtls_rsa_gen_key( int nrbits, int exponent, int result) 702{ 703 mbedtls_rsa_context ctx; 704 mbedtls_entropy_context entropy; 705 mbedtls_ctr_drbg_context ctr_drbg; 706 const char *pers = "test_suite_rsa"; 707 708 mbedtls_ctr_drbg_init( &ctr_drbg ); 709 mbedtls_entropy_init( &entropy ); 710 mbedtls_rsa_init ( &ctx ); 711 712 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, 713 &entropy, (const unsigned char *) pers, 714 strlen( pers ) ) == 0 ); 715 716 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result ); 717 if( result == 0 ) 718 { 719 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); 720 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 ); 721 } 722 723exit: 724 mbedtls_rsa_free( &ctx ); 725 mbedtls_ctr_drbg_free( &ctr_drbg ); 726 mbedtls_entropy_free( &entropy ); 727} 728/* END_CASE */ 729 730/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ 731void mbedtls_rsa_deduce_primes( int radix_N, char *input_N, 732 int radix_D, char *input_D, 733 int radix_E, char *input_E, 734 int radix_P, char *output_P, 735 int radix_Q, char *output_Q, 736 int corrupt, int result ) 737{ 738 mbedtls_mpi N, P, Pp, Q, Qp, D, E; 739 740 mbedtls_mpi_init( &N ); 741 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); 742 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp ); 743 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); 744 745 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 746 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 ); 747 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 748 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, radix_P, output_P ) == 0 ); 749 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, radix_Q, output_Q ) == 0 ); 750 751 if( corrupt ) 752 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 ); 753 754 /* Try to deduce P, Q from N, D, E only. */ 755 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result ); 756 757 if( !corrupt ) 758 { 759 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */ 760 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) || 761 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) ); 762 } 763 764exit: 765 mbedtls_mpi_free( &N ); 766 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); 767 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp ); 768 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); 769} 770/* END_CASE */ 771 772/* BEGIN_CASE */ 773void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P, 774 int radix_Q, char *input_Q, 775 int radix_E, char *input_E, 776 int radix_D, char *output_D, 777 int corrupt, int result ) 778{ 779 mbedtls_mpi P, Q, D, Dp, E, R, Rp; 780 781 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); 782 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp ); 783 mbedtls_mpi_init( &E ); 784 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp ); 785 786 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 787 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 788 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 789 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, radix_D, output_D ) == 0 ); 790 791 if( corrupt ) 792 { 793 /* Make E even */ 794 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 ); 795 } 796 797 /* Try to deduce D from N, P, Q, E. */ 798 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q, 799 &E, &D ) == result ); 800 801 if( !corrupt ) 802 { 803 /* 804 * Check that D and Dp agree modulo LCM(P-1, Q-1). 805 */ 806 807 /* Replace P,Q by P-1, Q-1 */ 808 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 ); 809 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 ); 810 811 /* Check D == Dp modulo P-1 */ 812 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 ); 813 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 ); 814 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); 815 816 /* Check D == Dp modulo Q-1 */ 817 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 ); 818 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 ); 819 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); 820 } 821 822exit: 823 824 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); 825 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp ); 826 mbedtls_mpi_free( &E ); 827 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp ); 828} 829/* END_CASE */ 830 831/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ 832void mbedtls_rsa_import( int radix_N, char *input_N, 833 int radix_P, char *input_P, 834 int radix_Q, char *input_Q, 835 int radix_D, char *input_D, 836 int radix_E, char *input_E, 837 int successive, 838 int is_priv, 839 int res_check, 840 int res_complete ) 841{ 842 mbedtls_mpi N, P, Q, D, E; 843 mbedtls_rsa_context ctx; 844 845 /* Buffers used for encryption-decryption test */ 846 unsigned char *buf_orig = NULL; 847 unsigned char *buf_enc = NULL; 848 unsigned char *buf_dec = NULL; 849 850 mbedtls_entropy_context entropy; 851 mbedtls_ctr_drbg_context ctr_drbg; 852 const char *pers = "test_suite_rsa"; 853 854 const int have_N = ( strlen( input_N ) > 0 ); 855 const int have_P = ( strlen( input_P ) > 0 ); 856 const int have_Q = ( strlen( input_Q ) > 0 ); 857 const int have_D = ( strlen( input_D ) > 0 ); 858 const int have_E = ( strlen( input_E ) > 0 ); 859 860 mbedtls_ctr_drbg_init( &ctr_drbg ); 861 mbedtls_entropy_init( &entropy ); 862 mbedtls_rsa_init( &ctx ); 863 864 mbedtls_mpi_init( &N ); 865 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); 866 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); 867 868 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, 869 (const unsigned char *) pers, strlen( pers ) ) == 0 ); 870 871 if( have_N ) 872 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 873 874 if( have_P ) 875 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 876 877 if( have_Q ) 878 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 879 880 if( have_D ) 881 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 ); 882 883 if( have_E ) 884 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 885 886 if( !successive ) 887 { 888 TEST_ASSERT( mbedtls_rsa_import( &ctx, 889 have_N ? &N : NULL, 890 have_P ? &P : NULL, 891 have_Q ? &Q : NULL, 892 have_D ? &D : NULL, 893 have_E ? &E : NULL ) == 0 ); 894 } 895 else 896 { 897 /* Import N, P, Q, D, E separately. 898 * This should make no functional difference. */ 899 900 TEST_ASSERT( mbedtls_rsa_import( &ctx, 901 have_N ? &N : NULL, 902 NULL, NULL, NULL, NULL ) == 0 ); 903 904 TEST_ASSERT( mbedtls_rsa_import( &ctx, 905 NULL, 906 have_P ? &P : NULL, 907 NULL, NULL, NULL ) == 0 ); 908 909 TEST_ASSERT( mbedtls_rsa_import( &ctx, 910 NULL, NULL, 911 have_Q ? &Q : NULL, 912 NULL, NULL ) == 0 ); 913 914 TEST_ASSERT( mbedtls_rsa_import( &ctx, 915 NULL, NULL, NULL, 916 have_D ? &D : NULL, 917 NULL ) == 0 ); 918 919 TEST_ASSERT( mbedtls_rsa_import( &ctx, 920 NULL, NULL, NULL, NULL, 921 have_E ? &E : NULL ) == 0 ); 922 } 923 924 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); 925 926 /* On expected success, perform some public and private 927 * key operations to check if the key is working properly. */ 928 if( res_complete == 0 ) 929 { 930 if( is_priv ) 931 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); 932 else 933 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); 934 935 if( res_check != 0 ) 936 goto exit; 937 938 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 939 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 940 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 941 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) 942 goto exit; 943 944 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, 945 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); 946 947 /* Make sure the number we're generating is smaller than the modulus */ 948 buf_orig[0] = 0x00; 949 950 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); 951 952 if( is_priv ) 953 { 954 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, 955 &ctr_drbg, buf_enc, 956 buf_dec ) == 0 ); 957 958 TEST_ASSERT( memcmp( buf_orig, buf_dec, 959 mbedtls_rsa_get_len( &ctx ) ) == 0 ); 960 } 961 } 962 963exit: 964 965 mbedtls_free( buf_orig ); 966 mbedtls_free( buf_enc ); 967 mbedtls_free( buf_dec ); 968 969 mbedtls_rsa_free( &ctx ); 970 971 mbedtls_ctr_drbg_free( &ctr_drbg ); 972 mbedtls_entropy_free( &entropy ); 973 974 mbedtls_mpi_free( &N ); 975 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); 976 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); 977} 978/* END_CASE */ 979 980/* BEGIN_CASE */ 981void mbedtls_rsa_export( int radix_N, char *input_N, 982 int radix_P, char *input_P, 983 int radix_Q, char *input_Q, 984 int radix_D, char *input_D, 985 int radix_E, char *input_E, 986 int is_priv, 987 int successive ) 988{ 989 /* Original MPI's with which we set up the RSA context */ 990 mbedtls_mpi N, P, Q, D, E; 991 992 /* Exported MPI's */ 993 mbedtls_mpi Ne, Pe, Qe, De, Ee; 994 995 const int have_N = ( strlen( input_N ) > 0 ); 996 const int have_P = ( strlen( input_P ) > 0 ); 997 const int have_Q = ( strlen( input_Q ) > 0 ); 998 const int have_D = ( strlen( input_D ) > 0 ); 999 const int have_E = ( strlen( input_E ) > 0 ); 1000 1001 mbedtls_rsa_context ctx; 1002 1003 mbedtls_rsa_init( &ctx ); 1004 1005 mbedtls_mpi_init( &N ); 1006 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); 1007 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); 1008 1009 mbedtls_mpi_init( &Ne ); 1010 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe ); 1011 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee ); 1012 1013 /* Setup RSA context */ 1014 1015 if( have_N ) 1016 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 1017 1018 if( have_P ) 1019 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 1020 1021 if( have_Q ) 1022 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 1023 1024 if( have_D ) 1025 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 ); 1026 1027 if( have_E ) 1028 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 1029 1030 TEST_ASSERT( mbedtls_rsa_import( &ctx, 1031 strlen( input_N ) ? &N : NULL, 1032 strlen( input_P ) ? &P : NULL, 1033 strlen( input_Q ) ? &Q : NULL, 1034 strlen( input_D ) ? &D : NULL, 1035 strlen( input_E ) ? &E : NULL ) == 0 ); 1036 1037 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 1038 1039 /* 1040 * Export parameters and compare to original ones. 1041 */ 1042 1043 /* N and E must always be present. */ 1044 if( !successive ) 1045 { 1046 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 ); 1047 } 1048 else 1049 { 1050 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 ); 1051 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 ); 1052 } 1053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 ); 1054 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 ); 1055 1056 /* If we were providing enough information to setup a complete private context, 1057 * we expect to be able to export all core parameters. */ 1058 1059 if( is_priv ) 1060 { 1061 if( !successive ) 1062 { 1063 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe, 1064 &De, NULL ) == 0 ); 1065 } 1066 else 1067 { 1068 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL, 1069 NULL, NULL ) == 0 ); 1070 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe, 1071 NULL, NULL ) == 0 ); 1072 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, 1073 &De, NULL ) == 0 ); 1074 } 1075 1076 if( have_P ) 1077 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 ); 1078 1079 if( have_Q ) 1080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 ); 1081 1082 if( have_D ) 1083 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 ); 1084 1085 /* While at it, perform a sanity check */ 1086 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee, 1087 NULL, NULL ) == 0 ); 1088 } 1089 1090exit: 1091 1092 mbedtls_rsa_free( &ctx ); 1093 1094 mbedtls_mpi_free( &N ); 1095 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); 1096 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); 1097 1098 mbedtls_mpi_free( &Ne ); 1099 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe ); 1100 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee ); 1101} 1102/* END_CASE */ 1103 1104/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 1105void mbedtls_rsa_validate_params( int radix_N, char *input_N, 1106 int radix_P, char *input_P, 1107 int radix_Q, char *input_Q, 1108 int radix_D, char *input_D, 1109 int radix_E, char *input_E, 1110 int prng, int result ) 1111{ 1112 /* Original MPI's with which we set up the RSA context */ 1113 mbedtls_mpi N, P, Q, D, E; 1114 1115 const int have_N = ( strlen( input_N ) > 0 ); 1116 const int have_P = ( strlen( input_P ) > 0 ); 1117 const int have_Q = ( strlen( input_Q ) > 0 ); 1118 const int have_D = ( strlen( input_D ) > 0 ); 1119 const int have_E = ( strlen( input_E ) > 0 ); 1120 1121 mbedtls_entropy_context entropy; 1122 mbedtls_ctr_drbg_context ctr_drbg; 1123 const char *pers = "test_suite_rsa"; 1124 1125 mbedtls_mpi_init( &N ); 1126 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); 1127 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); 1128 1129 mbedtls_ctr_drbg_init( &ctr_drbg ); 1130 mbedtls_entropy_init( &entropy ); 1131 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, 1132 &entropy, (const unsigned char *) pers, 1133 strlen( pers ) ) == 0 ); 1134 1135 if( have_N ) 1136 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 ); 1137 1138 if( have_P ) 1139 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 ); 1140 1141 if( have_Q ) 1142 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 ); 1143 1144 if( have_D ) 1145 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 ); 1146 1147 if( have_E ) 1148 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 ); 1149 1150 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL, 1151 have_P ? &P : NULL, 1152 have_Q ? &Q : NULL, 1153 have_D ? &D : NULL, 1154 have_E ? &E : NULL, 1155 prng ? mbedtls_ctr_drbg_random : NULL, 1156 prng ? &ctr_drbg : NULL ) == result ); 1157exit: 1158 1159 mbedtls_ctr_drbg_free( &ctr_drbg ); 1160 mbedtls_entropy_free( &entropy ); 1161 1162 mbedtls_mpi_free( &N ); 1163 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); 1164 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); 1165} 1166/* END_CASE */ 1167 1168/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ 1169void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P, 1170 data_t *input_Q, data_t *input_D, 1171 data_t *input_E, int is_priv, 1172 int successive ) 1173{ 1174 /* Exported buffers */ 1175 unsigned char bufNe[256]; 1176 unsigned char bufPe[128]; 1177 unsigned char bufQe[128]; 1178 unsigned char bufDe[256]; 1179 unsigned char bufEe[1]; 1180 1181 mbedtls_rsa_context ctx; 1182 1183 mbedtls_rsa_init( &ctx ); 1184 1185 /* Setup RSA context */ 1186 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1187 input_N->len ? input_N->x : NULL, input_N->len, 1188 input_P->len ? input_P->x : NULL, input_P->len, 1189 input_Q->len ? input_Q->x : NULL, input_Q->len, 1190 input_D->len ? input_D->x : NULL, input_D->len, 1191 input_E->len ? input_E->x : NULL, input_E->len ) == 0 ); 1192 1193 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); 1194 1195 /* 1196 * Export parameters and compare to original ones. 1197 */ 1198 1199 /* N and E must always be present. */ 1200 if( !successive ) 1201 { 1202 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len, 1203 NULL, 0, NULL, 0, NULL, 0, 1204 bufEe, input_E->len ) == 0 ); 1205 } 1206 else 1207 { 1208 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len, 1209 NULL, 0, NULL, 0, NULL, 0, 1210 NULL, 0 ) == 0 ); 1211 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, 1212 NULL, 0, NULL, 0, NULL, 0, 1213 bufEe, input_E->len ) == 0 ); 1214 } 1215 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 ); 1216 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 ); 1217 1218 /* If we were providing enough information to setup a complete private context, 1219 * we expect to be able to export all core parameters. */ 1220 1221 if( is_priv ) 1222 { 1223 if( !successive ) 1224 { 1225 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, 1226 bufPe, input_P->len ? input_P->len : sizeof( bufPe ), 1227 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ), 1228 bufDe, input_D->len ? input_D->len : sizeof( bufDe ), 1229 NULL, 0 ) == 0 ); 1230 } 1231 else 1232 { 1233 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, 1234 bufPe, input_P->len ? input_P->len : sizeof( bufPe ), 1235 NULL, 0, NULL, 0, 1236 NULL, 0 ) == 0 ); 1237 1238 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, 1239 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ), 1240 NULL, 0, NULL, 0 ) == 0 ); 1241 1242 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0, 1243 bufDe, input_D->len ? input_D->len : sizeof( bufDe ), 1244 NULL, 0 ) == 0 ); 1245 } 1246 1247 if( input_P->len ) 1248 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 ); 1249 1250 if( input_Q->len ) 1251 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 ); 1252 1253 if( input_D->len ) 1254 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 ); 1255 1256 } 1257 1258exit: 1259 mbedtls_rsa_free( &ctx ); 1260} 1261/* END_CASE */ 1262 1263/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ 1264void mbedtls_rsa_import_raw( data_t *input_N, 1265 data_t *input_P, data_t *input_Q, 1266 data_t *input_D, data_t *input_E, 1267 int successive, 1268 int is_priv, 1269 int res_check, 1270 int res_complete ) 1271{ 1272 /* Buffers used for encryption-decryption test */ 1273 unsigned char *buf_orig = NULL; 1274 unsigned char *buf_enc = NULL; 1275 unsigned char *buf_dec = NULL; 1276 1277 mbedtls_rsa_context ctx; 1278 mbedtls_entropy_context entropy; 1279 mbedtls_ctr_drbg_context ctr_drbg; 1280 1281 const char *pers = "test_suite_rsa"; 1282 1283 mbedtls_ctr_drbg_init( &ctr_drbg ); 1284 mbedtls_entropy_init( &entropy ); 1285 mbedtls_rsa_init( &ctx ); 1286 1287 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, 1288 &entropy, (const unsigned char *) pers, 1289 strlen( pers ) ) == 0 ); 1290 1291 if( !successive ) 1292 { 1293 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1294 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len, 1295 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len, 1296 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len, 1297 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len, 1298 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 ); 1299 } 1300 else 1301 { 1302 /* Import N, P, Q, D, E separately. 1303 * This should make no functional difference. */ 1304 1305 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1306 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len, 1307 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 ); 1308 1309 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1310 NULL, 0, 1311 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len, 1312 NULL, 0, NULL, 0, NULL, 0 ) == 0 ); 1313 1314 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1315 NULL, 0, NULL, 0, 1316 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len, 1317 NULL, 0, NULL, 0 ) == 0 ); 1318 1319 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1320 NULL, 0, NULL, 0, NULL, 0, 1321 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len, 1322 NULL, 0 ) == 0 ); 1323 1324 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, 1325 NULL, 0, NULL, 0, NULL, 0, NULL, 0, 1326 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 ); 1327 } 1328 1329 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); 1330 1331 /* On expected success, perform some public and private 1332 * key operations to check if the key is working properly. */ 1333 if( res_complete == 0 ) 1334 { 1335 if( is_priv ) 1336 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); 1337 else 1338 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); 1339 1340 if( res_check != 0 ) 1341 goto exit; 1342 1343 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 1344 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 1345 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); 1346 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) 1347 goto exit; 1348 1349 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, 1350 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); 1351 1352 /* Make sure the number we're generating is smaller than the modulus */ 1353 buf_orig[0] = 0x00; 1354 1355 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); 1356 1357 if( is_priv ) 1358 { 1359 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, 1360 &ctr_drbg, buf_enc, 1361 buf_dec ) == 0 ); 1362 1363 TEST_ASSERT( memcmp( buf_orig, buf_dec, 1364 mbedtls_rsa_get_len( &ctx ) ) == 0 ); 1365 } 1366 } 1367 1368exit: 1369 1370 mbedtls_free( buf_orig ); 1371 mbedtls_free( buf_enc ); 1372 mbedtls_free( buf_dec ); 1373 1374 mbedtls_rsa_free( &ctx ); 1375 1376 mbedtls_ctr_drbg_free( &ctr_drbg ); 1377 mbedtls_entropy_free( &entropy ); 1378 1379} 1380/* END_CASE */ 1381 1382/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 1383void rsa_selftest( ) 1384{ 1385 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 ); 1386} 1387/* END_CASE */ 1388