--- pgp/src/noise.c 2018/04/24 16:42:18 1.1.1.2 +++ pgp/src/noise.c 2018/04/24 16:43:38 1.1.1.3 @@ -24,11 +24,11 @@ #ifdef UNIX #include -#include /* For gettimeofday() */ -#include /* for times() */ -#include /* For qsort() */ +#include /* For gettimeofday() */ +#include /* for times() */ +#include /* For qsort() */ -#endif /* UNIX */ +#endif /* UNIX */ #include #include "usuals.h" @@ -44,7 +44,7 @@ typedef long clock_t; /* Use IBM PC hardware timer (1.19 MHz) */ -#include /* for inp() and outp() */ +#include /* for inp() and outp() */ /* timer0 on 8253-5 on IBM PC or AT tics every .84 usec. */ #define timer0 0x40 /* 8253 timer 0 port */ @@ -82,41 +82,39 @@ typedef long clock_t; * (Which MS-DOS, silly thing that it is, almost certainly uses * internally.) */ -static unsigned -pctimer0(void) +static unsigned pctimer0(void) { - unsigned count; + unsigned count; - inp(timer0); - outp(timercntl, 0xC2); /* Latch status and count for timer 0 */ - count = (inp(timer0) & 0x80) << 8; - outp(timercntl, 0x00); /* Latch count of timer 0 */ - count |= (inp(timer0) & 0xFF) >> 1; - count |= (inp(timer0) & 0xFF) << 7; + inp(timer0); + outp(timercntl, 0xC2); /* Latch status and count for timer 0 */ + count = (inp(timer0) & 0x80) << 8; + outp(timercntl, 0x00); /* Latch count of timer 0 */ + count |= (inp(timer0) & 0xFF) >> 1; + count |= (inp(timer0) & 0xFF) << 7; - return count; + return count; } -#endif /* MSDOS || __MSDOS__ */ +#endif /* MSDOS || __MSDOS__ */ #ifdef UNIX #define NOISEDEBUG #ifdef NOISEDEBUG -#include "pgp.h" /* for verbose and pgpout */ +#include "pgp.h" /* for verbose and pgpout */ #include #endif /* Function needed for qsort() */ -static int -noiseCompare(void const *p1, void const *p2) +static int noiseCompare(void const *p1, void const *p2) { - return *(int const *)p1 - *(int const *)p2; + return *(int const *) p1 - *(int const *) p2; } -#define DELTAS 15 /* Number of deltas to try */ +#define DELTAS 15 /* Number of deltas to try */ /* * Find the resolution of the gettimeofday() clock by sampling @@ -164,49 +162,49 @@ noiseCompare(void const *p1, void const * latencies and compute the entropy directly. A model considering * the previous interval only should be adequate. */ -static unsigned -noiseTickSize(void) +static unsigned noiseTickSize(void) { - int i; - int j; - unsigned deltas[DELTAS]; - unsigned t; - struct timeval tv_base, tv_old, tv_new; - - i = j = 0; - gettimeofday(&tv_base, 0); - tv_old = tv_base; - do { - gettimeofday(&tv_new, 0); - if (tv_new.tv_usec > tv_old.tv_usec+2) { - deltas[i++] = tv_new.tv_usec - tv_base.tv_usec + - 1000000 * (tv_new.tv_sec - tv_base.tv_sec); - tv_base = tv_new; - j = 0; - } - tv_old = tv_new; - - /* - * If we are forever getting <= 2 us, then just assume - * it's 2 us. - */ - if (j++ > 10000) - return 2; - } while (i < DELTAS); + int i; + int j; + unsigned deltas[DELTAS]; + unsigned t; + struct timeval tv_base, tv_old, tv_new; + + i = j = 0; + gettimeofday(&tv_base, 0); + tv_old = tv_base; + do { + gettimeofday(&tv_new, 0); + if (tv_new.tv_usec > tv_old.tv_usec + 2) { + deltas[i++] = tv_new.tv_usec - tv_base.tv_usec + + 1000000 * (tv_new.tv_sec - tv_base.tv_sec); + tv_base = tv_new; + j = 0; + } + tv_old = tv_new; + + /* + * If we are forever getting <= 2 us, then just assume + * it's 2 us. + */ + if (j++ > 10000) + return 2; + } while (i < DELTAS); - qsort(deltas, DELTAS, sizeof(deltas[0]), noiseCompare); + qsort(deltas, DELTAS, sizeof(deltas[0]), noiseCompare); - t = deltas[DELTAS/2]; /* Median */ + t = deltas[DELTAS / 2]; /* Median */ #ifdef NOISEDEBUG - if (verbose) - fprintf(pgpout, "t = %u, clock frequency is %u Hz\n", t, (2000000+t)/(2*t)); + if (verbose) + fprintf(pgpout, "t = %u, clock frequency is %u Hz\n", + t, (2000000 + t) / (2 * t)); #endif - return t; + return t; } -#endif /* UNIX */ +#endif /* UNIX */ /* @@ -220,76 +218,75 @@ noiseTickSize(void) word32 noise(void) { - static word32 lastcounter; - word32 delta; - time_t tnow; - clock_t cnow; + static word32 lastcounter; + word32 delta; + time_t tnow; + clock_t cnow; - cnow = clock(); - randPoolAddBytes((byte *)&cnow, sizeof(cnow)); + cnow = clock(); + randPoolAddBytes((byte *) & cnow, sizeof(cnow)); - tnow = time((time_t *)0); - randPoolAddBytes((byte *)&tnow, sizeof(tnow)); + tnow = time((time_t *) 0); + randPoolAddBytes((byte *) & tnow, sizeof(tnow)); #if defined(MSDOS) || defined(__MSDOS__) - { - unsigned t; + { + unsigned t; - t = pctimer0(); - randPoolAddBytes((byte *)&t, sizeof(t)); - delta = t - (unsigned)lastcounter; - lastcounter = t; - } + t = pctimer0(); + randPoolAddBytes((byte *) & t, sizeof(t)); + delta = t - (unsigned) lastcounter; + lastcounter = t; + } #endif #ifdef VMS - { - word32 t; - /* VMS Hardware Clock */ - extern unsigned long vms_clock_bits[2]; - /* Clock update int. */ - extern const long vms_ticks_per_update; - - /* Capture fast system timer: */ - SYS$GETTIM(vms_clock_bits); - randPoolAddBytes((byte *)&vms_clock_bits, sizeof(vms_clock_bits)); - t = vms_clock_bits[0] / vms_ticks_per_update; - delta = t - lastcounter; - lastcounter = t; - } -#endif /* VMS */ + { + word32 t; + /* VMS Hardware Clock */ + extern unsigned long vms_clock_bits[2]; + /* Clock update int. */ + extern const long vms_ticks_per_update; + + /* Capture fast system timer: */ + SYS$GETTIM(vms_clock_bits); + randPoolAddBytes((byte *) & vms_clock_bits, sizeof(vms_clock_bits)); + t = vms_clock_bits[0] / vms_ticks_per_update; + delta = t - lastcounter; + lastcounter = t; + } +#endif /* VMS */ #ifdef UNIX - /* Get noise from gettimeofday() */ - { - struct timeval tv; - word32 t; - static unsigned ticksize = 0; - - if (!ticksize) - ticksize = noiseTickSize(); - - gettimeofday(&tv, NULL); - randPoolAddBytes((byte *)&tv, sizeof(tv)); - - /* This may wrap, but it's unsigned, so that's okay */ - t = tv.tv_sec * 1000000 + tv.tv_usec; - delta = t-lastcounter; - lastcounter = t; + /* Get noise from gettimeofday() */ + { + struct timeval tv; + word32 t; + static unsigned ticksize = 0; + + if (!ticksize) + ticksize = noiseTickSize(); + + gettimeofday(&tv, NULL); + randPoolAddBytes((byte *) & tv, sizeof(tv)); + + /* This may wrap, but it's unsigned, so that's okay */ + t = tv.tv_sec * 1000000 + tv.tv_usec; + delta = t - lastcounter; + lastcounter = t; + + delta /= ticksize; + } + /* Get noise from times() */ + { + clock_t t; + struct tms tms; + + t = times(&tms); + randPoolAddBytes((byte *) & tms, sizeof(tms)); + randPoolAddBytes((byte *) & t, sizeof(t)); + } +#endif /* UNIX */ - delta /= ticksize; - } - /* Get noise from times() */ - { - clock_t t; - struct tms tms; - - t = times(&tms); - randPoolAddBytes((byte *)&tms, sizeof(tms)); - randPoolAddBytes((byte *)&t, sizeof(t)); - } -#endif /* UNIX */ - - return delta; + return delta; } -