raw
ch2_truerandom          1 #include "smg_rsa.h"
ch2_truerandom 2
ch2_truerandom 3 #include <stdlib.h>
ch2_truerandom 4 #include <time.h>
ch2_truerandom 5
ch2_truerandom 6 void err(char *msg)
ch2_truerandom 7 {
ch2_truerandom 8 fprintf(stderr, "%s\n", msg);
ch2_truerandom 9 exit(1);
ch2_truerandom 10 }
ch2_truerandom 11
ch2_truerandom 12 void time_entropy_source(int nruns, int noctets) {
ch2_truerandom 13 unsigned char buffer[noctets];
ch2_truerandom 14 int read, i;
ch2_truerandom 15 struct timespec tstart, tend;
ch2_truerandom 16 long int diff;
ch2_truerandom 17
ch2_truerandom 18 clock_gettime(CLOCK_MONOTONIC, &tstart);
ch2_truerandom 19 for (i=0; i<nruns; i++) {
ch2_truerandom 20 read = get_random_octets(noctets,buffer);
ch2_truerandom 21 if (read != noctets)
ch2_truerandom 22 err("Failed reading from entropy source!");
ch2_truerandom 23 }
ch2_truerandom 24 clock_gettime(CLOCK_MONOTONIC, &tend);
ch2_truerandom 25
ch2_truerandom 26 diff = tend.tv_sec-tstart.tv_sec;
ch2_truerandom 27 double kbps = (nruns*noctets) / (diff*1000.0);
ch2_truerandom 28 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 29 }
ch2_truerandom 30
ch2_truerandom 31
ch2_truerandom 32 int main(int ac, char **av)
ch2_truerandom 33 {
ch2_truerandom 34 int nruns;
ch2_truerandom 35
ch2_truerandom 36 if (ac<2) {
ch2_truerandom 37 printf("Usage: %s number_of_runs\n", av[0]);
ch2_truerandom 38 return -1;
ch2_truerandom 39 }
ch2_truerandom 40 nruns = atoi(av[1]);
ch2_truerandom 41
ch2_truerandom 42 printf("Timing entropy source...\n");
ch2_truerandom 43 time_entropy_source(nruns,4096);
ch2_truerandom 44
ch2_truerandom 45 return 0;
ch2_truerandom 46 }