- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / tlslite / tlslite / utils / win32prng.c
1
2 #include "Python.h"
3 #define _WIN32_WINNT 0x0400       /* Needed for CryptoAPI on some systems */
4 #include <windows.h>
5
6
7 static PyObject* getRandomBytes(PyObject *self, PyObject *args)
8 {
9         int howMany;
10         HCRYPTPROV hCryptProv;
11         unsigned char* bytes = NULL;
12         PyObject* returnVal = NULL;
13
14
15         /* Read Arguments */
16     if (!PyArg_ParseTuple(args, "i", &howMany))
17         return(NULL);
18
19         /* Get Context */
20         if(CryptAcquireContext(
21            &hCryptProv,
22            NULL,
23            NULL,
24            PROV_RSA_FULL,
25            CRYPT_VERIFYCONTEXT) == 0)
26                 return Py_BuildValue("s#", NULL, 0);
27
28
29         /* Allocate bytes */
30         bytes = malloc(howMany);
31
32
33         /* Get random data */
34         if(CryptGenRandom(
35            hCryptProv,
36            howMany,
37            bytes) == 0)
38                 returnVal = Py_BuildValue("s#", NULL, 0);
39         else
40                 returnVal = Py_BuildValue("s#", bytes, howMany);
41
42         free(bytes);
43         CryptReleaseContext(hCryptProv, 0);
44
45         return returnVal;
46 }
47
48
49
50 /* List of functions exported by this module */
51
52 static struct PyMethodDef win32prng_functions[] = {
53     {"getRandomBytes", (PyCFunction)getRandomBytes, METH_VARARGS},
54     {NULL,  NULL}        /* Sentinel */
55 };
56
57
58 /* Initialize this module. */
59
60 DL_EXPORT(void) initwin32prng(void)
61 {
62     Py_InitModule("win32prng", win32prng_functions);
63 }