Browse Source

fix getting current time on MS Windows + improve gettimeofday/clock_gettime usage on UNIX

Karel Miko 7 years ago
parent
commit
29af2d95cc
2 changed files with 30 additions and 12 deletions
  1. 22 8
      src/prngs/fortuna.c
  2. 8 4
      tests/test.c

+ 22 - 8
src/prngs/fortuna.c

@@ -8,6 +8,16 @@
  */
  */
 #include "tomcrypt.h"
 #include "tomcrypt.h"
 
 
+#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
+#if defined(_WIN32)
+  #include <windows.h>
+#elif defined(LTC_CLOCK_GETTIME)
+  #include <time.h> /* struct timespec + clock_gettime */
+#else
+  #include <sys/time.h> /* struct timeval + gettimeofday */
+#endif
+#endif
+
 /**
 /**
   @file fortuna.c
   @file fortuna.c
   Fortuna PRNG, Tom St Denis
   Fortuna PRNG, Tom St Denis
@@ -66,19 +76,23 @@ static void _fortuna_update_iv(prng_state *prng)
 static ulong64 _fortuna_current_time(void)
 static ulong64 _fortuna_current_time(void)
 {
 {
    ulong64 cur_time;
    ulong64 cur_time;
-#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
-   struct timespec ts;
-   clock_gettime(CLOCK_MONOTONIC, &ts);
-   cur_time = (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
-#elif defined(_WIN32)
+#if defined(_WIN32)
    FILETIME CurrentTime;
    FILETIME CurrentTime;
    ULARGE_INTEGER ul;
    ULARGE_INTEGER ul;
    GetSystemTimeAsFileTime(&CurrentTime);
    GetSystemTimeAsFileTime(&CurrentTime);
    ul.LowPart  = CurrentTime.dwLowDateTime;
    ul.LowPart  = CurrentTime.dwLowDateTime;
    ul.HighPart = CurrentTime.dwHighDateTime;
    ul.HighPart = CurrentTime.dwHighDateTime;
-   cur_time = ul.QuadPart;
-   cur_time -= CONST64(116444736000000000); /* subtract epoch in microseconds */
-   cur_time /= 1000; /* nanoseconds -> microseconds */
+   cur_time = ul.QuadPart; /* now we have 100ns intervals since 1 January 1601 */
+   cur_time -= CONST64(116444736000000000); /* subtract 100ns intervals between 1601-1970 */
+   cur_time /= 10; /* 100ns intervals > microseconds */
+#elif defined(LTC_CLOCK_GETTIME)
+  struct timespec ts;
+  clock_gettime(CLOCK_MONOTONIC, &ts);
+  cur_time = (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
+#else
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  cur_time = (ulong64)(tv.tv_sec) * 1000000 + (ulong64)(tv.tv_usec); /* get microseconds */
 #endif
 #endif
    return cur_time / 100;
    return cur_time / 100;
 }
 }

+ 8 - 4
tests/test.c

@@ -67,14 +67,18 @@ static ulong64 epoch_usec(void)
   GetSystemTimeAsFileTime(&CurrentTime);
   GetSystemTimeAsFileTime(&CurrentTime);
   ul.LowPart  = CurrentTime.dwLowDateTime;
   ul.LowPart  = CurrentTime.dwLowDateTime;
   ul.HighPart = CurrentTime.dwHighDateTime;
   ul.HighPart = CurrentTime.dwHighDateTime;
-  cur_time = ul.QuadPart;
-  cur_time -= CONST64(116444736000000000); /* subtract epoch in microseconds */
-  cur_time /= 10; /* nanoseconds > microseconds */
+  cur_time = ul.QuadPart; /* now we have 100ns intervals since 1 January 1601 */
+  cur_time -= CONST64(116444736000000000); /* subtract 100ns intervals between 1601-1970 */
+  cur_time /= 10; /* 100ns intervals > microseconds */
   return cur_time;
   return cur_time;
-#else
+#elif defined(LTC_CLOCK_GETTIME)
   struct timespec ts;
   struct timespec ts;
   clock_gettime(CLOCK_MONOTONIC, &ts);
   clock_gettime(CLOCK_MONOTONIC, &ts);
   return (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
   return (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
+#else
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return (ulong64)(tv.tv_sec) * 1000000 + (ulong64)(tv.tv_usec); /* get microseconds */
 #endif
 #endif
 }
 }