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