-
+ 89B1C405D92E249341BDC0BDDC5A98D8AE8DD0349E04A165063D938F01DF557A057BE544C51B0EFB6D72D505F1424A1AA3A6D7957E83F229711677240123E2FD
smg_comms/rsa/tests/tests.c
(0 . 0)(1 . 761)
9198 #include "smg_rsa.h"
9199 #include "mpi.h"
9200
9201 #include <stdlib.h>
9202 #include <unistd.h>
9203 #include <time.h>
9204 #include <stdio.h>
9205
9206 extern void adainit(void);
9207 extern void adafinal(void);
9208
9209 void err(char *msg)
9210 {
9211 fprintf(stderr, "%s\n", msg);
9212 exit(1);
9213 }
9214
9215 void time_entropy_source(int nruns, int noctets) {
9216 unsigned char buffer[noctets];
9217 int read, i;
9218 struct timespec tstart, tend;
9219 long int diff;
9220
9221 clock_gettime(CLOCK_MONOTONIC, &tstart);
9222 for (i=0; i<nruns; i++) {
9223 read = get_random_octets(noctets,buffer);
9224 if (read != noctets)
9225 err("Failed reading from entropy source!");
9226 }
9227 clock_gettime(CLOCK_MONOTONIC, &tend);
9228
9229 diff = tend.tv_sec-tstart.tv_sec;
9230 double kbps = (nruns*noctets) / (diff*1000.0);
9231 printf("ENTROPY source timing: %d kB in %ld seconds, at an average speed of %f kB/s over %d runs of %d octets each\n", nruns*noctets, diff, kbps, nruns, noctets);
9232 }
9233
9234 void test_entropy_output(unsigned int noctets, char * filename) {
9235 FILE * out;
9236 int source;
9237 unsigned int nread, total_read, to_read;
9238 const int buffer_length = 1000;
9239 unsigned char buffer[buffer_length];
9240
9241 source = open_entropy_source(ENTROPY_SOURCE);
9242 if (source <= 0)
9243 err("unable to access entropy source!");
9244
9245 out = fopen(filename, "wb");
9246 if ( !out )
9247 err("unable to open output file for test_entropy_output!");
9248
9249 printf("TEST_ENTROPY_SOURCE: reading %u octets from %s ", noctets, ENTROPY_SOURCE);
9250 total_read = 0;
9251 while (total_read < noctets) {
9252 to_read = noctets - total_read;
9253 if (to_read > buffer_length)
9254 to_read = buffer_length;
9255
9256 nread = get_random_octets_from(to_read, buffer, source);
9257 if (nread > 0) {
9258 total_read = total_read + nread;
9259 fwrite(buffer, 1, nread, out);
9260 fflush(out);
9261 printf(".");
9262 fflush(stdout);
9263 }
9264 }
9265 printf("done.\n");
9266
9267 fclose(out);
9268 close(source);
9269 }
9270
9271 void test_is_composite(int nruns, char *hex_number, int expected) {
9272 int i;
9273 int output;
9274 int count_ok = 0;
9275 int source = open_entropy_source(ENTROPY_SOURCE);
9276 MPI p = mpi_alloc(0);
9277
9278 mpi_fromstr(p, hex_number);
9279 printf("TEST is_composite on MPI(hex) ");
9280 mpi_print(stdout, p, 1);
9281 for (i=0; i < nruns; i++) {
9282 printf(".");
9283 fflush(stdout);
9284 output = is_composite(p, M_R_ITERATIONS, source);
9285 if (output == expected)
9286 count_ok = count_ok + 1;
9287 }
9288 printf("done, with %d out of %d correct runs for expected=%d: %s\n", count_ok, nruns, expected, count_ok==nruns? "PASS":"FAIL");
9289 mpi_free(p);
9290 close(source);
9291 }
9292
9293 void time_mr(int nruns) {
9294 struct timespec tstart, tend;
9295 long int diff;
9296 int i;
9297 MPI prime;
9298 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
9299 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
9300
9301 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
9302 if (entropy_source <= 0)
9303 err("can't open entropy source!");
9304
9305 /* first generate a prime of half key length, to make sure M-R will run max number of iterations */
9306 printf("Generating a prime number of %d octets length for M-R timing test\n", noctets);
9307 prime = mpi_alloc(nlimbs);
9308 gen_random_prime(noctets, prime);
9309
9310 printf("Running timing test for Miller-Rabin with %d repetitions and %d witnesses on prime number ", nruns, M_R_ITERATIONS);
9311 mpi_print(stdout, prime, 1);
9312 printf("\n");
9313 /* now do the actual runs and time it all */
9314 clock_gettime(CLOCK_MONOTONIC, &tstart);
9315 for (i=0; i<nruns; i++) {
9316 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
9317 printf("FAIL");
9318 else printf(".");
9319 fflush(stdout);
9320 }
9321 clock_gettime(CLOCK_MONOTONIC, &tend);
9322
9323 diff = tend.tv_sec-tstart.tv_sec;
9324 printf("\nTimings on prime number %d octets long, %d runs of MR with %d iterations (witnesses checked) each\n", \
9325 noctets, nruns, M_R_ITERATIONS);
9326 printf("Total time: %ld seconds\nTime per MR run: %f seconds\nTime per MR iteration: %f seconds\n",\
9327 diff, diff / (1.0*nruns), diff / (1.0*nruns * M_R_ITERATIONS));
9328
9329 mpi_free(prime);
9330 close(entropy_source);
9331 }
9332
9333 void test_rpng(int nruns) {
9334 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
9335 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
9336 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
9337 if (entropy_source <= 0)
9338 err("can't open entropy source!");
9339
9340 MPI prime = mpi_alloc(nlimbs);
9341 int i;
9342
9343 printf("TEST: random prime number generator with %d runs\n", nruns);
9344 for (i = 0;i < nruns; i++) {
9345 gen_random_prime(noctets, prime);
9346 printf("Run %d: ", i+1);
9347 mpi_print(stdout, prime, 1);
9348 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
9349 printf(" **FAIL**\n");
9350 else
9351 printf(" **PASS**\n");
9352 }
9353
9354 mpi_free(prime);
9355 close(entropy_source);
9356 }
9357
9358 void time_rpng(int nruns) {
9359 struct timespec tstart, tend;
9360 long int diff;
9361
9362 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
9363 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
9364
9365 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
9366 if (entropy_source <= 0)
9367 err("can't open entropy source!");
9368
9369 MPI prime = mpi_alloc(nlimbs);
9370 int i;
9371
9372 printf("TIMING: random prime number generator with %d runs\n", nruns);
9373 clock_gettime(CLOCK_MONOTONIC, &tstart);
9374 for (i = 0;i < nruns; i++) {
9375 gen_random_prime(noctets, prime);
9376 }
9377 clock_gettime(CLOCK_MONOTONIC, &tend);
9378
9379 diff = tend.tv_sec-tstart.tv_sec;
9380
9381 printf("TOTAL: %ld seconds\n", diff);
9382 printf("Average: %f seconds to generate one random prime of %d octets length\n", diff / (1.0*nruns), noctets);
9383 mpi_free(prime);
9384 close(entropy_source);
9385 }
9386
9387 /* Test encryption+decryption on noctets of random data, using sk
9388 * Output is written to file.
9389 */
9390 void test_rsa_keys( RSA_secret_key *sk, unsigned int noctets, FILE *file ) {
9391 RSA_public_key pk;
9392 MPI test = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
9393 MPI out1 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
9394 MPI out2 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
9395
9396 pk.n = mpi_copy(sk->n);
9397 pk.e = mpi_copy(sk->e);
9398 unsigned char *p;
9399 p = xmalloc(noctets);
9400
9401 fprintf(file, "TEST encrypt/decrypt on %d octets of random data\n", noctets);
9402 fflush(file);
9403 if (get_random_octets( noctets, p) == noctets) {
9404 mpi_set_buffer( test, p, noctets, 0 );
9405
9406 fprintf(file, "TEST data:\n");
9407 mpi_print(file, test, 1);
9408 fprintf(file, "\n");
9409 fflush(file);
9410
9411 public_rsa( out1, test, &pk );
9412 secret_rsa( out2, out1, sk );
9413
9414 fprintf(file, "ENCRYPTED with PUBLIC key data:\n");
9415 mpi_print(file, out1, 1);
9416 fprintf(file, "\n");
9417 fflush(file);
9418
9419 fprintf(file, "DECRYPTED with SECRET key:\n");
9420 mpi_print(file, out2, 1);
9421 fprintf(file, "\n");
9422 fflush(file);
9423
9424 if( mpi_cmp( test, out2 ) )
9425 fprintf(file, "FAILED: RSA operation: public(secret) failed\n");
9426 else
9427 fprintf(file, "PASSED: RSA operation: public(secret) passed\n");
9428 fflush(file);
9429
9430 secret_rsa( out1, test, sk );
9431 public_rsa( out2, out1, &pk );
9432 if( mpi_cmp( test, out2 ) )
9433 fprintf(file, "FAILED: RSA operation: secret(public) failed\n");
9434 else
9435 fprintf(file, "PASSED: RSA operation: secret(public) passed\n");
9436 }
9437 else
9438 fprintf(file, "FAILED: not enough bits returned from entropy source\n");
9439
9440 fflush(file);
9441 xfree(p);
9442 mpi_free( pk.n);
9443 mpi_free( pk.e);
9444
9445 mpi_free( test );
9446 mpi_free( out1 );
9447 mpi_free( out2 );
9448 }
9449
9450 void test_rsa( int nruns, FILE *fkeys, FILE *fout) {
9451 RSA_secret_key sk;
9452 int noctets = KEY_LENGTH_OCTETS;
9453 int noctets_pq = noctets / 2;
9454 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
9455 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
9456 int i;
9457
9458 sk.n = mpi_alloc(nlimbs);
9459 sk.e = mpi_alloc(nlimbs);
9460 sk.d = mpi_alloc(nlimbs);
9461 sk.p = mpi_alloc(nlimbs_pq);
9462 sk.q = mpi_alloc(nlimbs_pq);
9463 sk.u = mpi_alloc(nlimbs_pq);
9464
9465 printf("TEST RSA key generation and use with %d runs\n", nruns);
9466 fflush(stdout);
9467
9468 for (i = 0;i < nruns; i++) {
9469 gen_keypair(&sk);
9470 printf(".");
9471 fflush(stdout);
9472
9473 mpi_print(fkeys, sk.n, 1);
9474 fwrite("\n", sizeof(char), 1, fkeys);
9475
9476 mpi_print(fkeys, sk.e, 1);
9477 fwrite("\n", sizeof(char), 1, fkeys);
9478
9479 mpi_print(fkeys, sk.d, 1);
9480 fwrite("\n", sizeof(char), 1, fkeys);
9481
9482 mpi_print(fkeys, sk.p, 1);
9483 fwrite("\n", sizeof(char), 1, fkeys);
9484
9485 mpi_print(fkeys, sk.q, 1);
9486 fwrite("\n", sizeof(char), 1, fkeys);
9487
9488 mpi_print(fkeys, sk.u, 1);
9489 fwrite("\n", sizeof(char), 1, fkeys);
9490
9491 test_rsa_keys(&sk, noctets_pq, fout);
9492 printf("*");
9493 fflush(stdout);
9494 }
9495
9496 mpi_free(sk.n);
9497 mpi_free(sk.e);
9498 mpi_free(sk.d);
9499 mpi_free(sk.p);
9500 mpi_free(sk.q);
9501 mpi_free(sk.u);
9502
9503 }
9504
9505 void test_rsa_exp() {
9506 MPI msg = mpi_alloc(0);
9507 MPI expected = mpi_alloc(0);
9508 MPI result;
9509
9510 RSA_public_key pk;
9511 pk.n = mpi_alloc(0);
9512 pk.e = mpi_alloc(0);
9513
9514 printf("TEST verify of rsa exponentiation on input data: \n");
9515
9516 mpi_fromstr(msg, "0x\
9517 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
9518 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
9519 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
9520 8613747F340C18176E6D5F0C10246A2FCE3A668EACB6165C2052497CA2EE483F4FD8D0\
9521 6A9911BD97E9B6720521D872BD08FF8DA11A1B8DB147F252E4E69AE6201D3B374B171D\
9522 F445EF2BF509D468FD57CEB5840349B14C6E2AAA194D9531D238B85B8F0DD352D1E596\
9523 71539B429849E5D965E438BF9EFFC338DF9AADF304C4130D5A05E006ED855F37A06242\
9524 28097EF92F6E78CAE0CB97");
9525
9526 mpi_fromstr(expected, "0x\
9527 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
9528 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
9529 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
9530 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
9531 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003051300\
9532 D0609608648016503040203050004406255509399A3AF322C486C770C5F7F6E05E18FC\
9533 3E2219A03CA56C7501426A597187468B2F71B4A198C807171B73D0E7DBC3EEF6EA6AFF\
9534 693DE58E18FF84395BE");
9535 result = mpi_alloc( mpi_get_nlimbs(expected) );
9536
9537 mpi_fromstr(pk.n, "0x\
9538 CDD49A674BAF76D3B73E25BC6DF66EF3ABEDDCA461D3CCB6416793E3437C7806562694\
9539 73C2212D5FD5EED17AA067FEC001D8E76EC901EDEDF960304F891BD3CAD7F9A335D1A2\
9540 EC37EABEFF3FBE6D3C726DC68E599EBFE5456EF19813398CD7D548D746A30AA47D4293\
9541 968BFBAFCBF65A90DFFC87816FEE2A01E1DC699F4DDABB84965514C0D909D54FDA7062\
9542 A2037B50B771C153D5429BA4BA335EAB840F9551E9CD9DF8BB4A6DC3ED1318FF3969F7\
9543 B99D9FB90CAB968813F8AD4F9A069C9639A74D70A659C69C29692567CE863B88E191CC\
9544 9535B91B417D0AF14BE09C78B53AF9C5F494BCF2C60349FFA93C81E817AC682F0055A6\
9545 07BB56D6A281C1A04CEFE1");
9546
9547 mpi_fromstr( pk.e, "0x10001");
9548
9549 mpi_print( stdout, msg, 1);
9550 printf("\n");
9551
9552 public_rsa( result, msg, &pk);
9553 if ( mpi_cmp( result, expected) != 0 )
9554 printf( "FAILED\n");
9555 else
9556 printf( "PASSED\n");
9557
9558 printf("Expected:\n");
9559 mpi_print( stdout, expected, 1);
9560 printf("\n");
9561
9562 printf("Obtained:\n");
9563 mpi_print( stdout, result, 1);
9564 printf("\n");
9565
9566 mpi_free( pk.n );
9567 mpi_free( pk.e );
9568 mpi_free( msg );
9569 mpi_free( expected );
9570 mpi_free( result );
9571 }
9572
9573 void time_rsa_gen( int nruns ) {
9574 struct timespec tstart, tend;
9575 long int diff;
9576 int i;
9577
9578 RSA_secret_key sk;
9579 int noctets = KEY_LENGTH_OCTETS;
9580 int noctets_pq = noctets / 2;
9581 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
9582 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
9583 sk.n = mpi_alloc(nlimbs);
9584 sk.e = mpi_alloc(nlimbs);
9585 sk.d = mpi_alloc(nlimbs);
9586 sk.p = mpi_alloc(nlimbs_pq);
9587 sk.q = mpi_alloc(nlimbs_pq);
9588 sk.u = mpi_alloc(nlimbs_pq);
9589
9590 clock_gettime(CLOCK_MONOTONIC, &tstart);
9591 for (i = 0;i < nruns; i++) {
9592 gen_keypair(&sk);
9593 }
9594 clock_gettime(CLOCK_MONOTONIC, &tend);
9595
9596 diff = tend.tv_sec-tstart.tv_sec;
9597
9598 printf("TOTAL: %ld seconds for generating %d key pairs\n", diff, nruns);
9599 printf("Average (%d runs): %f seconds per TMSR RSA key pair.\n",
9600 nruns, diff / (1.0*nruns));
9601 mpi_free(sk.n);
9602 mpi_free(sk.e);
9603 mpi_free(sk.d);
9604 mpi_free(sk.p);
9605 mpi_free(sk.q);
9606 mpi_free(sk.u);
9607 }
9608
9609 void test_oaep_encr_decr( int nruns ) {
9610 /* a set of RSA keys previously generated with eucrypt */
9611 RSA_public_key pk;
9612 pk.n = mpi_alloc(0);
9613 pk.e = mpi_alloc(0);
9614
9615 RSA_secret_key sk;
9616 sk.n = mpi_alloc(0);
9617 sk.e = mpi_alloc(0);
9618 sk.d = mpi_alloc(0);
9619 sk.p = mpi_alloc(0);
9620 sk.q = mpi_alloc(0);
9621 sk.u = mpi_alloc(0);
9622
9623 mpi_fromstr(sk.n, "0x\
9624 CD2C025323BEA46FFF2FA8D7A9D39817EA713421F4AE03FA8120641193892A70BFECF5\
9625 83101635A432110D3DDE6339E3CC7ECC0AD91C026FCACE832DD3888A6FCA7BCE56C390\
9626 5A5AC8C7BC921DA675E4B62489B254EB34659D547D71165BC998983A81937BD251AEE1\
9627 2D985EC387D5376F5DCC5EF7EC530FBD6FD2AA7285EE1AF3335EA73163F0954F30402E\
9628 D7B374EE84A97B1849B0674B0DA0A2050BD79B71ABB1559F3A9CFDB8557DED7BC90CF2\
9629 09E8A847E9C226140845B7D03842162E7DA5DD16326CB1F71A248D841FE9076A09911F\
9630 2F4F5E3EA44EA8DE40332BF00406990BCCF61C322A03C456EF3A98B341E0BDBC1088CE\
9631 683E78510E76B72C2BCC1EE9AEDD80FFF18ABFC5923B2F36B581C25114AB2DF9F6C2B1\
9632 9481703FD19E313DCD7ACE15FA11B27D25BCE5388C180A7E21167FB87750599E1ED7C7\
9633 50F4A844E1DC2270C62D19671CF8F4C25B81E366B09FC850AE642136D204A9160AEECE\
9634 575B57378AA439E9DD46DC990288CD54BAA35EEE1C02456CD39458A6F1CBF012DCEDF4\
9635 27CCF3F3F53645658FC49C9C9D7F2856DB571D92B967AB5845514E0054DDB49099F5DD\
9636 04A6F6F5C5CE642276834B932881AEB648D1F25E9223971F56E249EF40CF7D80F22621\
9637 CDD0260E9E7D23746960ADB52CF2987584FB1DE95A69A39E5CB12B76E0F5C1A0529C0C\
9638 065D2E35720810F7C7983180B9A9EA0E00C11B79DC3D");
9639
9640 mpi_fromstr(sk.e, "0x\
9641 DD4856B4EE3D099A8604AE392D8EFEC094CDF01546A28BE87CB484F999E8E75CDFCD01\
9642 D04D455A6A9254C60BD28C0B03611FC3E751CC27EF768C0B401C4FD2B27C092834A6F2\
9643 49A145C4EDC47A3B3D363EC352462C945334D160AF9AA72202862912493AC6190AA3A6\
9644 149D4D8B9996BA7927D3D0D2AD00D30FD630CF464E6CAF9CF49355B9A70E05DB7AE915\
9645 F9F602772F8D11E5FCDFC7709210F248052615967090CC1F43D410C83724AA5912B2F0\
9646 52E6B39449A89A97C79C92DC8CB8DEEFCF248C1E1D2FC5BFE85165ECA31839CAA9CEB3\
9647 3A92EBDC0EB3BAC0F810938BB173C7DA21DCBB2220D44CBA0FD40A2C868FC93AC5243E\
9648 C137C27B0A76D65634EBB3");
9649
9650 mpi_fromstr(sk.d, "0x\
9651 7C8A6FA1199D99DCA45E9BDF567CA49D02B237340D7E999150BC4883AE29DEC5158521\
9652 B338F35DC883792356BDDBB3C8B3030A6DD4C6522599A3254E751F9BA1CB1061C5633C\
9653 81BBFACF6FCD64502614102DFED3F3FA284066C342D5E00953B415915331E30812E5FB\
9654 CD6680ADCCDEE40B8376A3A225F2E160EA59C7566804526D73BB660A648A3EF9802313\
9655 B2F841E8458B2AAACE7AACF31083E8F3F630298138393BC88BBD7D4AA4334949651D25\
9656 365B10DBF4A4A08E20A6CC74BFDD37C1C38E2ADC2A283DF06590DF06B46F67F6ACA67F\
9657 AC464C795261659A2F9558802D0BBAA05FD1E1AF2CDC70654723DF7EFAEA148B8CDBEB\
9658 C89EA2320AB9BBB1BC4311475DF3D91446F02EF192368DFEBAC598CCFD4407DEC58FDC\
9659 1A94CCDD6E5FBA9C52164ACEA8AEE633E557BCCEACB7A1AF656C379482D784A120A725\
9660 32F9B2B35173D505F21D5AD4CB9511BC836DC923730B70291B70290A216CA3B21CFF79\
9661 E895C35F4F7AF80E1BD9ED2773BD26919A76E4298D169160593E0335BE2A2A2D2E8516\
9662 948F657E1B1260E18808A9D463C108535FB60B3B28F711C81E5DE24F40214134A53CE5\
9663 9A952C8970A1D771EBEFFA2F4359DCF157995B3F1950DE3C6EC41B7FF837148F55F323\
9664 372AF3F20CE8B8038E750C23D8F5041FA951327859B0E47483F0A47103EF808C72C251\
9665 006FA526245291C8C84C12D2EF63FB2301EA3EEDA42B");
9666
9667 mpi_fromstr(sk.p, "0x\
9668 E236732452039C14EC1D3B8095BDDCFB7625CE27B1EA5394CF4ED09D3CEECAA4FC0BF6\
9669 2F7CE975E0C8929CE84B0259D773EA038396479BF15DA065BA70E549B248D77B4B23ED\
9670 A267308510DBEE2FD44E35D880EE7CFB81E0646AA8630165BD8988C3A8776D9E704C20\
9671 AA25CA0A3C32F27F592D5FD363B04DD57D8C61FFDCDFCCC59E2913DE0EE47769180340\
9672 E1EA5A803AA2301A010FF553A380F002601F0853FCACDB82D76FE2FACBCD6E5F294439\
9673 0799EA5AE9D7880D4E1D4AE146DC1D4E8495B9DD30E57E883923C5FC26682B7142D35C\
9674 D8A0FC561FE725A6CF419B15341F40FE0C31132CBD81DD8E50697BD1EBFFA16B522E16\
9675 F5B49A03B707218C7DA60B");
9676
9677 mpi_fromstr(sk.q, "0x\
9678 E830482A3C4F5C3A7E59C10FF8BA760DB1C6D55880B796FFDA4A82E0B60E974E81D04B\
9679 2A4AD417823EBFB4E8EFB13782943562B19B6C4A680E3BA0C8E37B5023470F4F1AC1F8\
9680 A0B10672EF75CD58BCD45E6B14503B8A6A70AFE79F6201AF56E7364A1C742BE1453FD2\
9681 24FDC9D66522EAF4466A084BCB9E46D455A2946E94CBF028770F38D0B741C2CC59308F\
9682 71D8C2B4B9C928E0AE8D68DEB48A3E9EFD84A10301EBD55F8221CA32FC567B306B2A8E\
9683 116350AFB995859FDF4378C5CFD06901494E8CFA5D8FAC564D6531FA8A2E4761F5EFBA\
9684 F78750B6F4662BE9EA4C2FAD67AF73EEB36B41FC15CB678810C19A51DF23555695C4C1\
9685 546F3FACA39CAA7BB8DBD7");
9686
9687 mpi_fromstr(sk.u, "0x\
9688 846232322775C1CD7D5569DC59E2F3E61A885AE2E9C4A4F8CB3ACBE8C3A5441E5FE348\
9689 A2A8AC9C2998FBF282222BF508AA1ECF66A76AEDD2D9C97028BFD3F6CA0542E38A5312\
9690 603C70B95650CE73F80FDD729988FBDB5595A5BF8A007EA34E54994A697906CE56354C\
9691 E00DF10EB711DEC274A62494E3D350D88736CF67A477FB600AC9F1D6580727585092BF\
9692 5EBC092CC4D6CF75769051033A1197103BE269942F372168A53771746FBA18ED6972D5\
9693 0B935A9B1D6B5B3DD50CD89A27FE93C10924E9103FACF7B4C5724A046C3D3B50CC1C78\
9694 5F5C8E00DBE1D6561F120F5294C170914BC10F978ED4356EED67A9F3A60D70AFE540FC\
9695 5373CBAE3D0A7FD1C87273");
9696
9697 /* copy the public key components */
9698 pk.n = mpi_copy( sk.n );
9699 pk.e = mpi_copy( sk.e );
9700
9701 /* some plain text message */
9702 MPI msg = mpi_alloc(0);
9703 mpi_fromstr(msg, "0x\
9704 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
9705 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
9706 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
9707 28097EF92F6E78CAE0CB97");
9708
9709 /* actual testing */
9710 printf("TEST verify oaep_encr_decr on message: \n");
9711 mpi_print( stdout, msg, 1);
9712 printf("\n");
9713
9714 int nlimbs_n = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS);
9715 MPI encr = mpi_alloc( nlimbs_n );
9716 MPI decr = mpi_alloc( nlimbs_n );
9717 int success;
9718
9719 adainit();
9720 rsa_oaep_encrypt( encr, msg, &pk );
9721 rsa_oaep_decrypt( decr, encr, &sk, &success );
9722
9723 if (success <= 0 ||
9724 mpi_cmp(encr, msg) == 0 ||
9725 mpi_cmp(msg, decr) != 0)
9726 printf("FAILED: success flag is %d\n", success);
9727 else
9728 printf("PASSED\n");
9729
9730 /* attempt to decrypt corrupted block */
9731 mpi_clear( decr );
9732 rsa_oaep_decrypt( decr, pk.n, &sk, &success);
9733 if (success > 0)
9734 printf("FAILED: attempt to decrypt non-/corrupted oaep block\n");
9735 else
9736 printf("PASSED: attempt to decrypt non-/corrupted oaep block\n");
9737 adafinal();
9738
9739 /* clean up */
9740 mpi_free( sk.n );
9741 mpi_free( sk.e );
9742 mpi_free( sk.d );
9743 mpi_free( sk.p );
9744 mpi_free( sk.q );
9745 mpi_free( sk.u );
9746
9747 mpi_free( pk.n );
9748 mpi_free( pk.e );
9749
9750 mpi_free( msg );
9751 mpi_free( encr );
9752 mpi_free( decr );
9753 }
9754
9755 void test_mpi_buffer() {
9756 unsigned int noctets = 10;
9757 int nlimbs = mpi_nlimb_hint_from_nbytes( noctets );
9758 MPI m = mpi_alloc( nlimbs );
9759 unsigned char *setbuffer = xmalloc( noctets );
9760 unsigned char *getbuffer;
9761 unsigned int i, sign, mpilen, nerrors;
9762
9763 for (i=0; i< noctets; i++)
9764 setbuffer[i] = i;
9765
9766 mpi_set_buffer( m, setbuffer, noctets, 0);
9767
9768 getbuffer = mpi_get_buffer( m, &mpilen, &sign );
9769
9770 if (mpilen == noctets -1 ) {
9771 nerrors = 0;
9772 for (i=0;i<noctets-1;i++)
9773 if (setbuffer[i+1] != getbuffer[i])
9774 nerrors = nerrors + 1;
9775 if (nerrors == 0) {
9776 printf("WARNING: 0-led octet discarded by mpi_set_buffer!\n");
9777 printf("Value ret by mpi_get_buffer != value given to set_buffer!\n");
9778 }
9779 else
9780 printf("FAIL: got different lengths and %d different values!\n", nerrors);
9781 }
9782 else if (mpilen != noctets)
9783 printf("FAIL: mpilen is %d; noctets is %d\n", mpilen, noctets);
9784 else
9785 {
9786 nerrors = 0;
9787 for (i=0;i<noctets-1;i++) {
9788 if (setbuffer[i]!=getbuffer[i])
9789 nerrors= nerrors+1;
9790 }
9791 if (nerrors>0)
9792 printf("FAIL: got %d different values!\n", nerrors);
9793 else printf("PASSED: mpi_get/set_buffer\n");
9794 }
9795
9796 mpi_free(m);
9797 xfree(setbuffer);
9798 xfree(getbuffer);
9799 }
9800
9801 void test_dirty_float_rng( int nruns ) {
9802 int i, status;
9803 float dirty;
9804
9805 printf("Running test for smg rng dirty float with %d runs\n", nruns);
9806 for (i=0; i<nruns; i++) {
9807 status = rng_dirty_float( &dirty );
9808 printf("Run %d: %f status %s\n", i+1, dirty, status>0 ? "OK" : "FAIL");
9809 }
9810 }
9811
9812 void test_ieee_float_rng( int nruns ) {
9813 int i, status;
9814 float ieee;
9815
9816 printf("Running test for smg rng ieee 745/1985 float with %d runs\n", nruns);
9817 for (i=0; i<nruns; i++) {
9818 status = rng_float_754_1985( &ieee );
9819 printf("Run %d: %f status %s\n", i+1, ieee, status>0 ? "OK" : "FAIL");
9820 }
9821 }
9822
9823 void test_uint32_rng( int nruns ) {
9824 int i, status;
9825 uint32_t n;
9826
9827 printf("Running test for smg rng unsigned int32 with %d runs\n", nruns);
9828 for (i=0; i<nruns; i++) {
9829 status = rng_uint32( &n );
9830 printf("Run %d: %"PRIu32" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
9831 }
9832 }
9833
9834 void test_uint64_rng( int nruns ) {
9835 int i, status;
9836 uint64_t n;
9837
9838 printf("Running test for smg rng unsigned int64 with %d runs\n", nruns);
9839 for (i=0; i<nruns; i++) {
9840 status = rng_uint64( &n );
9841 printf("Run %d: %"PRIu64" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
9842 }
9843 }
9844
9845
9846 int main(int ac, char **av)
9847 {
9848 int nruns;
9849 int id;
9850 FILE *fk;
9851 FILE *fout;
9852
9853 if (ac<2) {
9854 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
9855 return -1;
9856 }
9857 nruns = atoi(av[1]);
9858
9859 if (ac < 3)
9860 id = -1;
9861 else
9862 id = atoi(av[2]);
9863
9864 switch ( id ) {
9865 case 0:
9866 printf("Timing entropy source...\n");
9867 time_entropy_source(nruns, 4096);
9868 break;
9869 case 1:
9870 test_entropy_output(nruns, "entropy_source_output.txt");
9871 break;
9872 case 2:
9873 /* tests on miller-rabin */
9874 /* a few primes (decimal): 65537, 116447, 411949103, 20943302231 */
9875 test_is_composite(nruns, "0x10001", 0);
9876 test_is_composite(nruns, "0x1C6DF", 0);
9877 test_is_composite(nruns, "0x188DD82F", 0);
9878 test_is_composite(nruns, "0x4E0516E57", 0);
9879 /* a few mersenne primes (decimal): 2^13 - 1 = 8191, 2^17 - 1 = 131071, 2^31 - 1 = 2147483647 */
9880 test_is_composite(nruns, "0x1FFF", 0);
9881 test_is_composite(nruns, "0x1FFFF", 0);
9882 test_is_composite(nruns, "0x7FFFFFFF", 0);
9883 /* a few carmichael numbers, in decimal: 561, 60977817398996785 */
9884 test_is_composite(nruns, "0x231", 1);
9885 test_is_composite(nruns, "0xD8A300793EEF31", 1);
9886 /* an even number */
9887 test_is_composite(nruns, "0x15A9E672864B1E", 1);
9888 /* a phuctor-found non-prime public exponent: 170141183460469231731687303715884105731 */
9889 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
9890 break;
9891 case 3:
9892 time_mr(nruns);
9893 break;
9894 case 4:
9895 test_rpng(nruns);
9896 break;
9897 case 5:
9898 time_rpng(nruns);
9899 break;
9900 case 6:
9901 fk = fopen("keys.asc", "a");
9902 if ( fk == NULL )
9903 err("Failed to open file keys.asc!");
9904 fout = fopen("check_keys.asc", "a");
9905 if ( fout == NULL ) {
9906 fclose(fk);
9907 err("Failed to open file keys_check.asc!");
9908 }
9909 test_rsa(nruns, fk, fout);
9910 fclose(fk);
9911 fclose(fout);
9912 break;
9913 case 7:
9914 test_rsa_exp();
9915 break;
9916 case 8:
9917 time_rsa_gen(nruns);
9918 break;
9919 case 9:
9920 test_oaep_encr_decr(nruns);
9921 break;
9922 case 10:
9923 test_mpi_buffer();
9924 break;
9925 case 11:
9926 test_dirty_float_rng(nruns);
9927 break;
9928 case 12:
9929 test_ieee_float_rng(nruns);
9930 break;
9931 case 13:
9932 test_uint32_rng(nruns);
9933 break;
9934 case 14:
9935 test_uint64_rng(nruns);
9936 break;
9937 default:
9938 printf("Current test ids:\n");
9939 printf("0 for timing entropy source\n");
9940 printf("1 for entropy output test\n");
9941 printf("2 for is_composite (Miller-Rabin) test\n");
9942 printf("3 for timing Miller-Rabin\n");
9943 printf("4 for random prime number generator test\n");
9944 printf("5 for timing random prime number generator\n");
9945 printf("6 for testing rsa key pair generation and use; \
9946 writes to keys.asc and check_keys.asc\n");
9947 printf("7 for testing rsa exponentiation (fixed data)\n");
9948 printf("8 for timing rsa key pair generator\n");
9949 printf("9 for oaep encrypt/decrypt\n");
9950 printf("10 for testing mpi_set/get_buffer\n");
9951 printf("11 for testing smg_rng dirty float\n");
9952 printf("12 for testing smg_rng ieee 745/1985 float\n");
9953 printf("13 for testing smg_rng uint32 \n");
9954 printf("14 for testing smg_rng uint64 \n");
9955 }
9956
9957 return 0;
9958 }