[ovs-dev] [PATCH] windows pseudorandom number generator

Alin Serdean aserdean at cloudbasesolutions.com
Mon Dec 16 17:13:59 UTC 2013


Sorry about that.

I can put in the ernno for the error but the problem is CryptGenRandom does not set the error number.

So it is either just print the value of GetLastError() or I use FormatMessage function to get the text from that value. Like the following:

 lib/entropy.c |   28 ++++++++++++++++++++++++++++
 1 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/lib/entropy.c b/lib/entropy.c
index 02f56e0..0343d86 100644
--- a/lib/entropy.c
+++ b/lib/entropy.c
@@ -27,12 +27,16 @@
 VLOG_DEFINE_THIS_MODULE(entropy);

 static const char urandom[] = "/dev/urandom";
+#ifdef _WIN32
+#include <Wincrypt.h>
+#endif

 /* Initializes 'buffer' with 'n' bytes of high-quality random numbers.  Returns
  * 0 if successful, otherwise a positive errno value or EOF on error. */
 int
 get_entropy(void *buffer, size_t n)
 {
+#ifndef _WIN32
     size_t bytes_read;
     int error;
     int fd;
@@ -49,6 +53,30 @@ get_entropy(void *buffer, size_t n)
     if (error) {
         VLOG_ERR("%s: read error (%s)", urandom, ovs_retval_to_string(error));
     }
+#else
+    int error = 0;
+    HCRYPTPROV   crypt_prov = 0;
+    CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
+
+    if (!CryptGenRandom(crypt_prov, n, buffer)) {
+        error = GetLastError();
+        LPVOID msg_buf;
+        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                       FORMAT_MESSAGE_FROM_SYSTEM |
+                       FORMAT_MESSAGE_IGNORE_INSERTS,
+                       NULL,
+                       error,
+                       0,
+                       (LPTSTR)&msg_buf,
+                       0,
+                       NULL
+                       );
+        VLOG_ERR("CryptGenRandom: read error (%s)", msg_buf);
+        LocalFree(msg_buf);
+    }
+
+    CryptReleaseContext(crypt_prov, 0);
+#endif
     return error;
 }

Would you like me to set up a helper function like ovs_retval_to_string (i.e. ovs_getlasterror_to_string) or just leave it the way it is for the moment?

Kind Regards,
Alin.
________________________________________
From: Ben Pfaff [blp at nicira.com]
Sent: Saturday, December 14, 2013 7:22 PM
To: Alin Serdean
Cc: Gurucharan Shetty; dev at openvswitch.org
Subject: Re: [ovs-dev] [PATCH] windows pseudorandom number generator

On Sat, Dec 14, 2013 at 01:20:31AM +0000, Alin Serdean wrote:
> This patch is to add for secure pseudorandom number generator on windows.
>
> Signed-off-by: Alin Serdean <aserdean at cloudbasesolutions.com>

I'm pretty sure that GetLastError() doesn't return an errno value, but
this code tries to pass it to ovs_retval_to_string() and return it to a
caller expecting an errno.  Can you do better than that?  Thanks.

> +       if (!CryptGenRandom(crypt_prov, n, buffer)) {
> +               error = GetLastError();
> +               VLOG_ERR("CryptGenRandom: read error (%s)", ovs_retval_to_string(error));
> +       }



More information about the dev mailing list