Browse Source

Merge pull request #383 from libtom/pr/fortuna-fix

new fortuna: fix getting current time
karel-m 7 years ago
parent
commit
49001f01b3
5 changed files with 47 additions and 18 deletions
  1. 9 0
      src/headers/tomcrypt_custom.h
  2. 2 3
      src/headers/tomcrypt_prng.h
  3. 5 2
      src/misc/crypt/crypt.c
  4. 23 9
      src/prngs/fortuna.c
  5. 8 4
      tests/test.c

+ 9 - 0
src/headers/tomcrypt_custom.h

@@ -364,6 +364,15 @@
 /* time-based rate limit of the reseeding */
 /* time-based rate limit of the reseeding */
 #define LTC_FORTUNA_RESEED_RATELIMIT_TIMED
 #define LTC_FORTUNA_RESEED_RATELIMIT_TIMED
 
 
+/* with non-glibc or glibc 2.17+ prefer clock_gettime over gettimeofday */
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 17)
+  #define LTC_CLOCK_GETTIME
+#endif
+#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
+  #define LTC_CLOCK_GETTIME
+#endif
+
 #else
 #else
 
 
 #ifndef LTC_FORTUNA_WD
 #ifndef LTC_FORTUNA_WD

+ 2 - 3
src/headers/tomcrypt_prng.h

@@ -40,9 +40,8 @@ struct fortuna_prng {
                   IV[16];     /* IV for CTR mode */
                   IV[16];     /* IV for CTR mode */
 
 
     unsigned long pool_idx,   /* current pool we will add to */
     unsigned long pool_idx,   /* current pool we will add to */
-                  pool0_len,  /* length of 0'th pool */
-                  wd;
-
+                  pool0_len;  /* length of 0'th pool */
+    ulong64       wd;
     ulong64       reset_cnt;  /* number of times we have reseeded */
     ulong64       reset_cnt;  /* number of times we have reseeded */
 };
 };
 #endif
 #endif

+ 5 - 2
src/misc/crypt/crypt.c

@@ -515,10 +515,13 @@ const char *crypt_build_settings =
     " LTC_MECC_ACCEL "
     " LTC_MECC_ACCEL "
 #endif
 #endif
 #if defined(LTC_MECC_FP)
 #if defined(LTC_MECC_FP)
-   " LTC_MECC_FP "
+    " LTC_MECC_FP "
 #endif
 #endif
 #if defined(LTC_ECC_SHAMIR)
 #if defined(LTC_ECC_SHAMIR)
-   " LTC_ECC_SHAMIR "
+    " LTC_ECC_SHAMIR "
+#endif
+#if defined(LTC_CLOCK_GETTIME)
+    " LTC_CLOCK_GETTIME "
 #endif
 #endif
     "\n"
     "\n"
     ;
     ;

+ 23 - 9
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;
 }
 }
@@ -93,7 +107,7 @@ static int _fortuna_reseed(prng_state *prng)
    int           err, x;
    int           err, x;
 
 
 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
-   unsigned long now = _fortuna_current_time();
+   ulong64 now = _fortuna_current_time();
    if (now == prng->fortuna.wd)
    if (now == prng->fortuna.wd)
       return CRYPT_OK;
       return CRYPT_OK;
 #else
 #else

+ 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
 }
 }