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