summaryrefslogtreecommitdiff
path: root/lib/gc-gnulib.c
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2008-04-11 09:16:11 +0200
committerSimon Josefsson <simon@josefsson.org>2008-04-11 09:16:11 +0200
commit2fd7e9189c239ef737105bc085ace4dc80435533 (patch)
tree6db3746b782ffc598f668945b2bba3b2233d1939 /lib/gc-gnulib.c
parent4305538f3f4009f399fe0571541368ae3c10141b (diff)
downloadgnulib-2fd7e9189c239ef737105bc085ace4dc80435533.tar.gz
Make gc_random work under Windows.
Based on patch from Adam Strzelecki <ono@java.pl> in <http://lists.gnu.org/archive/html/help-gsasl/2008-02/msg00000.html>.
Diffstat (limited to 'lib/gc-gnulib.c')
-rw-r--r--lib/gc-gnulib.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c
index d535d032ec..eb6c403c6f 100644
--- a/lib/gc-gnulib.c
+++ b/lib/gc-gnulib.c
@@ -1,5 +1,5 @@
/* gc-gnulib.c --- Common gnulib internal crypto interface functions
- * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
+ * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Simon Josefsson
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -73,15 +73,40 @@
#undef open
#undef close
+#ifdef GNULIB_GC_RANDOM
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# include <wincrypt.h>
+HCRYPTPROV g_hProv = 0;
+# endif
+#endif
+
Gc_rc
gc_init (void)
{
+#ifdef GNULIB_GC_RANDOM
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+ if(g_hProv)
+ CryptReleaseContext(g_hProv, 0);
+ CryptAcquireContext(&g_hProv, NULL, NULL, PROV_RSA_FULL, 0);
+# endif
+#endif
+
return GC_OK;
}
void
gc_done (void)
{
+#ifdef GNULIB_GC_RANDOM
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+ if(g_hProv)
+ {
+ CryptReleaseContext(g_hProv, 0);
+ g_hProv = 0;
+ }
+# endif
+#endif
+
return;
}
@@ -92,6 +117,11 @@ gc_done (void)
static Gc_rc
randomize (int level, char *data, size_t datalen)
{
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+ if(!g_hProv)
+ return GC_RANDOM_ERROR;
+ CryptGenRandom(g_hProv, (DWORD)datalen, data);
+#else
int fd;
const char *device;
size_t len = 0;
@@ -140,6 +170,7 @@ randomize (int level, char *data, size_t datalen)
rc = close (fd);
if (rc < 0)
return GC_RANDOM_ERROR;
+#endif
return GC_OK;
}