#include "smg_rsa.h" #include "mpi.h" #include #include #include #include void err(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); } void time_entropy_source(int nruns, int noctets) { unsigned char buffer[noctets]; int read, i; struct timespec tstart, tend; long int diff; clock_gettime(CLOCK_MONOTONIC, &tstart); for (i=0; i buffer_length) to_read = buffer_length; nread = get_random_octets_from(to_read, buffer, source); if (nread > 0) { total_read = total_read + nread; fwrite(buffer, 1, nread, out); fflush(out); printf("."); fflush(stdout); } } printf("done.\n"); fclose(out); close(source); } void test_is_composite(int nruns, char *hex_number, int expected) { int i; int output; int count_ok = 0; int source = open_entropy_source(ENTROPY_SOURCE); MPI p = mpi_alloc(0); mpi_fromstr(p, hex_number); printf("TEST is_composite on MPI(hex) "); mpi_print(stdout, p, 1); for (i=0; i < nruns; i++) { printf("."); fflush(stdout); output = is_composite(p, M_R_ITERATIONS, source); if (output == expected) count_ok = count_ok + 1; } printf("done, with %d out of %d correct runs for expected=%d: %s\n", count_ok, nruns, expected, count_ok==nruns? "PASS":"FAIL"); mpi_free(p); close(source); } void time_mr(int nruns) { struct timespec tstart, tend; long int diff; int i; MPI prime; unsigned int noctets = KEY_LENGTH_OCTETS / 2; unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets); int entropy_source = open_entropy_source(ENTROPY_SOURCE); if (entropy_source <= 0) err("can't open entropy source!"); /* first generate a prime of half key length, to make sure M-R will run max number of iterations */ printf("Generating a prime number of %d octets length for M-R timing test\n", noctets); prime = mpi_alloc(nlimbs); gen_random_prime(noctets, prime); printf("Running timing test for Miller-Rabin with %d repetitions and %d witnesses on prime number ", nruns, M_R_ITERATIONS); mpi_print(stdout, prime, 1); printf("\n"); /* now do the actual runs and time it all */ clock_gettime(CLOCK_MONOTONIC, &tstart); for (i=0; i