2009-02-04 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / rand.c
index de373f2d6627981e412aaa0984762436b2a15c20..1060e6519f0a23861db4e5f9d151ddb21835358e 100644 (file)
@@ -6,8 +6,8 @@
  *      Patrik Torstensson (p@rxc.se)
  *     Sebastien Pouliot (sebastien@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
- * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
 #include <mono/metadata/object.h>
 #include <mono/metadata/rand.h>
@@ -94,7 +99,8 @@ get_entropy_from_server (const char *path, guchar *buf, int len)
 
 #if defined (PLATFORM_WIN32)
 
-#include <WinCrypt.h>
+#include <windows.h>
+#include <wincrypt.h>
 
 #ifndef PROV_INTEL_SEC
 #define PROV_INTEL_SEC         22
@@ -158,13 +164,14 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpo
        if (!CryptGenRandom (provider, len, buf)) {
                CryptReleaseContext (provider, 0);
                /* we may have lost our context with CryptoAPI, but all hope isn't lost yet! */
-               provider = ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL);
+               provider = (HCRYPTPROV) ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL);
                if (!CryptGenRandom (provider, len, buf)) {
                        CryptReleaseContext (provider, 0);
                        provider = 0;
                        /* exception will be thrown in managed code */
                }
-       } 
+       }
+       return (gpointer) provider;
 }
 
 void
@@ -227,16 +234,20 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpo
                        return NULL; 
                get_entropy_from_server (socket_path, mono_array_addr (arry, guchar, 0), mono_array_length (arry));
                return (gpointer) -1;
-       }
-       else {
+       } else {
                /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
                gint count = 0;
                gint err;
 
                do {
                        err = read (file, buf + count, len - count);
+                       if (err < 0) {
+                               if (errno == EINTR)
+                                       continue;
+                               break;
+                       }
                        count += err;
-               } while (err >= 0 && count < len);
+               } while (count < len);
 
                if (err < 0) {
                        g_warning("Entropy error! Error in read (%s).", strerror (errno));