gnutls-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU gnutls branch, master, updated. gnutls_2_9_10-293-gb422230


From: Nikos Mavrogiannopoulos
Subject: [SCM] GNU gnutls branch, master, updated. gnutls_2_9_10-293-gb422230
Date: Sun, 04 Jul 2010 08:48:23 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU gnutls".

http://git.savannah.gnu.org/cgit/gnutls.git/commit/?id=b422230d1b846155b64a9e8cdcf8ed2563f442cc

The branch, master has been updated
       via  b422230d1b846155b64a9e8cdcf8ed2563f442cc (commit)
       via  4ef54a780d581dabafcca1b42e3d413ead491184 (commit)
      from  89c342d9bb577ff901b658322cc177ff1dd4d11c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b422230d1b846155b64a9e8cdcf8ed2563f442cc
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Sun Jul 4 10:48:11 2010 +0200

    Use double to count bytes.

commit 4ef54a780d581dabafcca1b42e3d413ead491184
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Sun Jul 4 09:45:34 2010 +0200

    Added a windows version of the RNG.

-----------------------------------------------------------------------

Summary of changes:
 lib/nettle/rnd.c |  144 ++++++++++++++++++++++++++++++++++++++++++++++--------
 src/benchmark.c  |    6 +-
 2 files changed, 126 insertions(+), 24 deletions(-)

diff --git a/lib/nettle/rnd.c b/lib/nettle/rnd.c
index 84ef1a8..c4a0057 100644
--- a/lib/nettle/rnd.c
+++ b/lib/nettle/rnd.c
@@ -29,18 +29,9 @@
 #include <gnutls_errors.h>
 #include <gnutls_num.h>
 #include <nettle/yarrow.h>
-#include <time.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <fcntl.h>
-#include <locks.h>
 
 #define SOURCES 2
 
-static void* rnd_mutex;
-
 #define RND_LOCK if (gnutls_mutex_lock(&rnd_mutex)!=0) abort()
 #define RND_UNLOCK if (gnutls_mutex_unlock(&rnd_mutex)!=0) abort()
 
@@ -52,10 +43,99 @@ enum {
 static struct yarrow256_ctx yctx;
 static struct yarrow_source ysources[SOURCES];
 static time_t device_last_read = 0;
-static int device_fd;
+static time_t trivia_time_count = 0;
+
+static void* rnd_mutex;
+
+#define DEVICE_READ_SIZE 16
+#define DEVICE_READ_SIZE_MAX 32
+#define DEVICE_READ_INTERVAL 1200
+
+#ifdef _WIN32
+
+# include <windows.h>
+
+static HCRYPTPROV device_fd = NULL;
+
+static int do_trivia_source(int init)
+{
+    struct {
+               FILETIME now;
+               unsigned count;
+    } event;
+
+    unsigned entropy = 0;
+
+    GetSystemTimeAsFileTime(&event.now);
+    event.count = 0;
+
+    if (init) {
+               trivia_time_count = 0;
+    } else {
+               event.count = trivia_time_count++;
+               entropy = 1;
+    }
+
+    return yarrow256_update(&yctx, RANDOM_SOURCE_TRIVIA, entropy,
+                           sizeof(event), (const uint8_t *) &event);
+}
 
+static int do_device_source(int init)
+{
+    time_t now = time(NULL);
+       int read_size = DEVICE_READ_SIZE;
+
+    if (init) {
+               int old;
+
+               if(!CryptAcquireContext(&device_fd, NULL, NULL, PROV_RSA_FULL, 
CRYPT_SILENT|CRYPT_VERIFYCONTEXT)) {
+                       _gnutls_debug_log("error in CryptAcquireContext!\n");
+                       return GNUTLS_E_INTERNAL_ERROR;
+               }
+               device_last_read = now;
+               read_size = DEVICE_READ_SIZE_MAX; /* initially read more data */
+    }
+
+    if ((device_fd != NULL)
+               && (init || ((now - device_last_read) > DEVICE_READ_INTERVAL))) 
{
+
+               /* More than a minute since we last read the device */
+               uint8_t buf[DEVICE_READ_SIZE_MAX];
+
+               if(!CryptGenRandom(device_fd, (DWORD)read_size, buf)) {
+                       _gnutls_debug_log("Error in CryptGenRandom: %s\n", 
GetLastError());
+                       return GNUTLS_E_INTERNAL_ERROR;
+               }
+
+               device_last_read = now;
+               return yarrow256_update(&yctx, RANDOM_SOURCE_DEVICE, 
read_size*8/2 /* we trust the system RNG */,
+                                       read_size, buf);
+    }
+    return 0;
+}
+
+static void wrap_nettle_rnd_deinit(void* ctx)
+{
+       RND_LOCK;
+       CryptReleaseContext(device_fd, 0);
+       RND_UNLOCK;
+
+       gnutls_mutex_deinit(&rnd_mutex);
+       rnd_mutex = NULL;
+}
+
+#else /* POSIX */
+
+# include <time.h>
+# include <errno.h>
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <sys/time.h>
+# include <fcntl.h>
+# include <locks.h>
+
+static int device_fd;
 static time_t trivia_previous_time = 0;
-static time_t trivia_time_count = 0;
 
 static int do_trivia_source(int init)
 {
@@ -105,9 +185,6 @@ static int do_trivia_source(int init)
                            sizeof(event), (const uint8_t *) &event);
 }
 
-#define DEVICE_READ_SIZE 16
-#define DEVICE_READ_SIZE_MAX 32
-#define DEVICE_READ_INTERVAL 1200
 static int do_device_source(int init)
 {
     time_t now = time(NULL);
@@ -119,7 +196,7 @@ static int do_device_source(int init)
                device_fd = open("/dev/urandom", O_RDONLY);
                if (device_fd < 0) {
                        _gnutls_debug_log("Cannot open urandom!\n");
-                       abort();
+                       return GNUTLS_E_FILE_ERROR;
                }
 
                old = fcntl(device_fd, F_GETFD);
@@ -152,14 +229,14 @@ static int do_device_source(int init)
                                        ("Failed to read /dev/urandom: end of 
file\n");
                                }
 
-                               return 0;
+                               return GNUTLS_E_INTERNAL_ERROR;
                        }
 
                        done += res;
                }
 
                device_last_read = now;
-               return yarrow256_update(&yctx, RANDOM_SOURCE_DEVICE, 
read_size*8/3 /* be more conservative */,
+               return yarrow256_update(&yctx, RANDOM_SOURCE_DEVICE, 
read_size*8/2 /* we trust the RNG */,
                                        read_size, buf);
     }
     return 0;
@@ -175,6 +252,9 @@ static void wrap_nettle_rnd_deinit(void* ctx)
        rnd_mutex = NULL;
 }
 
+#endif
+
+
 static int wrap_nettle_rnd_init(void **ctx)
 {
 int ret;
@@ -187,8 +267,18 @@ int ret;
          }
 
        yarrow256_init(&yctx, SOURCES, ysources);
-       do_device_source(1);
-       do_trivia_source(1);
+
+       ret = do_device_source(1);
+       if (ret < 0) {
+               gnutls_assert();
+               return ret;
+       }
+
+       ret = do_trivia_source(1);
+       if (ret < 0) {
+               gnutls_assert();
+               return ret;
+       }
 
        yarrow256_slow_reseed(&yctx);
 
@@ -200,9 +290,21 @@ int ret;
 static int
 wrap_nettle_rnd(void *_ctx, int level, void *data, size_t datasize)
 {
+int ret;
+
        RND_LOCK;
-       do_trivia_source( 0);
-       do_device_source( 0);
+       
+       ret = do_trivia_source( 0);
+       if (ret < 0) {
+               gnutls_assert();
+               return ret;
+       }
+
+       ret = do_device_source( 0);
+       if (ret < 0) {
+               gnutls_assert();
+               return ret;
+       }
 
        yarrow256_random(&yctx, datasize, data);
        RND_UNLOCK;
diff --git a/src/benchmark.c b/src/benchmark.c
index 6c8fd6e..73aba25 100644
--- a/src/benchmark.c
+++ b/src/benchmark.c
@@ -46,7 +46,7 @@ tls_log_func (int level, const char *str)
   fprintf (stderr, "|<%d>| %s", level, str);
 }
 
-static void value2human(int bytes, double time, double* data, double* 
speed,char* metric)
+static void value2human(double bytes, double time, double* data, double* 
speed,char* metric)
 {
         if (bytes > 1000 && bytes < 1000*1000) {
                 *data = ((double)bytes)/1000;
@@ -80,7 +80,7 @@ cipher_bench (int algo, int size)
   gnutls_datum_t key, iv;
   struct timespec start, stop;
   double secs;
-  long data_size = 0;
+  double data_size = 0;
   double dspeed, ddata;
   int blocksize = gnutls_cipher_get_block_size (algo);
   int keysize = gnutls_cipher_get_key_size (algo);
@@ -149,7 +149,7 @@ mac_bench (int algo, int size)
   void *_key;
   struct timespec start, stop;
   double secs;
-  long data_size = 0;
+  double data_size = 0;
   double ddata, dspeed;
   int blocksize = gnutls_hmac_get_len (algo);
   char metric[16];


hooks/post-receive
-- 
GNU gnutls



reply via email to

[Prev in Thread] Current Thread [Next in Thread]