[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] On Win32, use CryptGenRandom as our entropy source



Update of /home/minion/cvsroot/src/minion/src
In directory moria.mit.edu:/tmp/cvs-serv25527/src

Modified Files:
	_minionlib.h crypt.c main.c 
Log Message:
On Win32, use CryptGenRandom as our entropy source

Index: _minionlib.h
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/_minionlib.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- _minionlib.h	10 Jul 2003 20:01:31 -0000	1.17
+++ _minionlib.h	14 Jul 2003 15:38:50 -0000	1.18
@@ -68,7 +68,8 @@
 FUNC_DOC(mm_strxor);
 FUNC_DOC(mm_openssl_seed);
 #ifdef MS_WINDOWS
-FUNC_DOC(mm_openssl_seed_win32);
+FUNC_DOC(mm_win32_openssl_seed);
+FUNC_DOC(mm_win32_get_random_bytes);
 #endif
 FUNC_DOC(mm_openssl_rand);
 FUNC_DOC(mm_add_oaep_padding);

Index: crypt.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/crypt.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- crypt.c	10 Jul 2003 20:01:31 -0000	1.26
+++ crypt.c	14 Jul 2003 15:38:50 -0000	1.27
@@ -2,6 +2,16 @@
 /* $Id$ */
 #include <Python.h>
 
+#ifdef MS_WINDOWS
+#define WIN32_WINNT 0x0400
+#define _WIN32_WINNT 0x0400
+#include <windows.h>
+#include <wincrypt.h>
+#ifndef ALG_CLASS_ANY
+#error no good
+#endif
+#endif
+
 #include <time.h>
 
 #ifndef TRUNCATED_OPENSSL_INCLUDES
@@ -23,6 +33,7 @@
 #include <pem.h>
 #include <evp.h>
 #endif
+
 #include "_minionlib.h"
 #include <assert.h>
 
@@ -279,16 +290,16 @@
 }
 
 #ifdef MS_WINDOWS
-const char mm_openssl_seed_win32__doc__[]=
+const char mm_win32_openssl_seed__doc__[]=
   "openssl_seed_win32()\n\n"
   "DOCDOC\n";
 
 PyObject *
-mm_openssl_seed_win32(PyObject *self, PyObject *args, PyObject *kwdict)
+mm_win32_openssl_seed(PyObject *self, PyObject *args, PyObject *kwdict)
 {
         static char *kwlist[] = { NULL };
 
-        if (!PyArg_ParseTupleAndKeywords(args, kwdict, ":openssl_seed_win32",
+        if (!PyArg_ParseTupleAndKeywords(args, kwdict, ":win32_openssl_seed",
                                          kwlist))
                 return NULL;
 
@@ -298,6 +309,85 @@
 
         Py_INCREF(Py_None);
         return Py_None;
+}
+
+/* DOCDOC */
+static int provider_set = 0;
+static HCRYPTPROV provider;
+
+static HCRYPTPROV getProvider()
+{
+        if (provider_set)
+                return provider;
+
+        if (!CryptAcquireContext(&provider,
+                                 NULL,
+                                 NULL,
+                                 PROV_RSA_FULL,
+                                 0)) {
+                if (GetLastError() != NTE_BAD_KEYSET) {
+                        /* XXXX005 error */
+                        return 0;
+                }
+                if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
+                                        CRYPT_NEWKEYSET)) {
+                        /* XXXX005 error */
+                        return 0;
+                }
+        }
+
+        provider_set =  1;
+
+        return provider;
+}
+
+const char mm_win32_get_random_bytes__doc__[]=
+  "win32_get_random_bytes(n)\n\n"
+  "DOCDOC";
+
+PyObject *
+mm_win32_get_random_bytes(PyObject *self, PyObject *args, PyObject *kwdict)
+{
+
+        static char *kwlist[] = { "n", NULL };
+        PyObject *result;
+        int n,r;
+        HCRYPTPROV p;
+
+        if (!PyArg_ParseTupleAndKeywords(args, kwdict, 
+                                         "i:win32_get_random_bytes",
+                                         kwlist, &n))
+                return NULL;
+
+        if (n<0) {
+                TYPE_ERR("n must be >= 0");
+                return NULL;
+        }
+
+        if (!(p = getProvider())) {
+                TYPE_ERR("XXXX005 internal error 2 ");
+                return NULL;
+        }
+
+
+        if (!(result = PyString_FromStringAndSize(NULL, n))) {
+                PyErr_NoMemory(); return NULL;
+        }
+
+                
+
+        Py_BEGIN_ALLOW_THREADS
+        r = CryptGenRandom(getProvider(), n, PyString_AS_STRING(result));
+        Py_END_ALLOW_THREADS
+        
+        if (!r) {
+               /*XXXX005 error */
+               Py_DECREF(result);
+               TYPE_ERR("XXXX005 internal error ");
+               return NULL;
+        }
+
+        return result;
 }
 #endif
 

Index: main.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/main.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- main.c	13 Jul 2003 03:45:36 -0000	1.18
+++ main.c	14 Jul 2003 15:38:50 -0000	1.19
@@ -31,10 +31,11 @@
         ENTRY(aes_ctr128_crypt),
         ENTRY(strxor),
         ENTRY(openssl_seed),
+        ENTRY(openssl_rand),
 #ifdef MS_WINDOWS
-        ENTRY(openssl_seed_win32),
+        ENTRY(win32_openssl_seed),
+        ENTRY(win32_get_random_bytes),
 #endif
-        ENTRY(openssl_rand),
         ENTRY(add_oaep_padding),
         ENTRY(check_oaep_padding),
         ENTRY(rsa_generate),