raw
ch2_truerandom          1 #include "smg_rsa.h"
eucrypt_ch3_mille... 2 #include "mpi.h"
ch2_truerandom 3
ch2_truerandom 4 #include <stdlib.h>
eucrypt_ch3_mille... 5 #include <unistd.h>
ch2_truerandom 6 #include <time.h>
ch2_truerandom 7
ch2_truerandom 8 void err(char *msg)
ch2_truerandom 9 {
ch2_truerandom 10 fprintf(stderr, "%s\n", msg);
ch2_truerandom 11 exit(1);
ch2_truerandom 12 }
ch2_truerandom 13
ch2_truerandom 14 void time_entropy_source(int nruns, int noctets) {
ch2_truerandom 15 unsigned char buffer[noctets];
ch2_truerandom 16 int read, i;
ch2_truerandom 17 struct timespec tstart, tend;
ch2_truerandom 18 long int diff;
ch2_truerandom 19
ch2_truerandom 20 clock_gettime(CLOCK_MONOTONIC, &tstart);
ch2_truerandom 21 for (i=0; i<nruns; i++) {
ch2_truerandom 22 read = get_random_octets(noctets,buffer);
ch2_truerandom 23 if (read != noctets)
ch2_truerandom 24 err("Failed reading from entropy source!");
ch2_truerandom 25 }
ch2_truerandom 26 clock_gettime(CLOCK_MONOTONIC, &tend);
ch2_truerandom 27
ch2_truerandom 28 diff = tend.tv_sec-tstart.tv_sec;
ch2_truerandom 29 double kbps = (nruns*noctets) / (diff*1000.0);
ch2_truerandom 30 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);
ch2_truerandom 31 }
ch2_truerandom 32
eucrypt_ch3_mille... 33 void test_is_composite(int nruns, char *hex_number, int expected) {
eucrypt_ch3_mille... 34 int i;
eucrypt_ch3_mille... 35 int output;
eucrypt_ch3_mille... 36 int count_ok = 0;
eucrypt_ch3_mille... 37 int source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch3_mille... 38 MPI p = mpi_alloc(0);
eucrypt_ch3_mille... 39
eucrypt_ch3_mille... 40 mpi_fromstr(p, hex_number);
eucrypt_ch3_mille... 41 printf("TEST is_composite on MPI(hex) ");
eucrypt_ch3_mille... 42 mpi_print(stdout, p, 1);
eucrypt_ch3_mille... 43 for (i=0; i < nruns; i++) {
eucrypt_ch3_mille... 44 printf(".");
eucrypt_ch3_mille... 45 fflush(stdout);
eucrypt_ch3_mille... 46 output = is_composite(p, M_R_ITERATIONS, source);
eucrypt_ch3_mille... 47 if (output == expected)
eucrypt_ch3_mille... 48 count_ok = count_ok + 1;
eucrypt_ch3_mille... 49 }
eucrypt_ch3_mille... 50 printf("done, with %d out of %d correct runs for expected=%d: %s\n", count_ok, nruns, expected, count_ok==nruns? "PASS":"FAIL");
eucrypt_ch3_mille... 51 mpi_free(p);
eucrypt_ch3_mille... 52 close(source);
eucrypt_ch3_mille... 53 }
ch2_truerandom 54
ch2_truerandom 55 int main(int ac, char **av)
ch2_truerandom 56 {
ch2_truerandom 57 int nruns;
eucrypt_ch3_mille... 58 int id;
ch2_truerandom 59
ch2_truerandom 60 if (ac<2) {
eucrypt_ch3_mille... 61 printf("Usage: %s number_of_runs [testID]\n", av[0]);
ch2_truerandom 62 return -1;
ch2_truerandom 63 }
ch2_truerandom 64 nruns = atoi(av[1]);
ch2_truerandom 65
eucrypt_ch3_mille... 66 if (ac < 3)
eucrypt_ch3_mille... 67 id = 0;
eucrypt_ch3_mille... 68 else
eucrypt_ch3_mille... 69 id = atoi(av[2]);
eucrypt_ch3_mille... 70
eucrypt_ch3_mille... 71 if (id == 0 || id == 1) {
eucrypt_ch3_mille... 72 printf("Timing entropy source...\n");
eucrypt_ch3_mille... 73 time_entropy_source(nruns,4096);
eucrypt_ch3_mille... 74 }
eucrypt_ch3_mille... 75
eucrypt_ch3_mille... 76 if (id == 0 || id == 2) {
eucrypt_ch3_mille... 77
eucrypt_ch3_mille... 78 /* a few primes (decimal): 65537, 116447, 411949103, 20943302231 */
eucrypt_ch3_mille... 79 test_is_composite(nruns, "0x10001", 0);
eucrypt_ch3_mille... 80 test_is_composite(nruns, "0x1C6DF", 0);
eucrypt_ch3_mille... 81 test_is_composite(nruns, "0x188DD82F", 0);
eucrypt_ch3_mille... 82 test_is_composite(nruns, "0x4E0516E57", 0);
eucrypt_ch3_mille... 83 /* a few mersenne primes (decimal): 2^13 - 1 = 8191, 2^17 - 1 = 131071, 2^31 - 1 = 2147483647 */
eucrypt_ch3_mille... 84 test_is_composite(nruns, "0x1FFF", 0);
eucrypt_ch3_mille... 85 test_is_composite(nruns, "0x1FFFF", 0);
eucrypt_ch3_mille... 86 test_is_composite(nruns, "0x7FFFFFFF", 0);
eucrypt_ch3_mille... 87 /* a few carmichael numbers, in decimal: 561, 60977817398996785 */
eucrypt_ch3_mille... 88 test_is_composite(nruns, "0x231", 1);
eucrypt_ch3_mille... 89 test_is_composite(nruns, "0xD8A300793EEF31", 1);
eucrypt_ch3_mille... 90 /* an even number */
eucrypt_ch3_mille... 91 test_is_composite(nruns, "0x15A9E672864B1E", 1);
eucrypt_ch3_mille... 92 /* a phuctor-found non-prime public exponent: 170141183460469231731687303715884105731 */
eucrypt_ch3_mille... 93 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
eucrypt_ch3_mille... 94 }
eucrypt_ch3_mille... 95
eucrypt_ch3_mille... 96 if (id > 2)
eucrypt_ch3_mille... 97 printf("Current test ids: 0 for all, 1 for entropy source test only, 2 for is_composite test only.");
ch2_truerandom 98
ch2_truerandom 99 return 0;
ch2_truerandom 100 }