Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(165)

Side by Side Diff: Modules/posixmodule.c

Issue 13704: Random number generator in Python core
Patch Set: Created 8 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 1
2 /* POSIX module implementation */ 2 /* POSIX module implementation */
3 3
4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the 4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few 5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source 6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent 7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature 8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler 9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default 10 independent macro PYOS_OS2 should be defined. On OS/2 the default
(...skipping 9311 matching lines...) Expand 10 before | Expand all | Expand 10 after
9322 } else 9322 } else
9323 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); 9323 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
9324 } 9324 }
9325 #endif 9325 #endif
9326 9326
9327 #ifdef MS_WINDOWS 9327 #ifdef MS_WINDOWS
9328 9328
9329 PyDoc_STRVAR(win32_urandom__doc__, 9329 PyDoc_STRVAR(win32_urandom__doc__,
9330 "urandom(n) -> str\n\n\ 9330 "urandom(n) -> str\n\n\
9331 Return n random bytes suitable for cryptographic use."); 9331 Return n random bytes suitable for cryptographic use.");
9332
9333 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9334 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9335 DWORD dwFlags );
9336 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9337 BYTE *pbBuffer );
9338
9339 static CRYPTGENRANDOM pCryptGenRandom = NULL;
9340 /* This handle is never explicitly released. Instead, the operating
9341 system will release it when the process terminates. */
9342 static HCRYPTPROV hCryptProv = 0;
9343 9332
9344 static PyObject* 9333 static PyObject*
9345 win32_urandom(PyObject *self, PyObject *args) 9334 win32_urandom(PyObject *self, PyObject *args)
9346 { 9335 {
9347 int howMany; 9336 int howMany;
9348 PyObject* result; 9337 PyObject* result;
9349 9338
9350 /* Read arguments */ 9339 /* Read arguments */
9351 if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) 9340 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9352 return NULL; 9341 return NULL;
9353 if (howMany < 0)
9354 return PyErr_Format(PyExc_ValueError,
9355 "negative argument not allowed");
9356
9357 if (hCryptProv == 0) {
9358 HINSTANCE hAdvAPI32 = NULL;
9359 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
9360
9361 /* Obtain handle to the DLL containing CryptoAPI
9362 This should not fail */
9363 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9364 if(hAdvAPI32 == NULL)
9365 return win32_error("GetModuleHandle", NULL);
9366
9367 /* Obtain pointers to the CryptoAPI functions
9368 This will fail on some early versions of Win95 */
9369 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9370 hAdvAPI32,
9371 "CryptAcquireContextA");
9372 if (pCryptAcquireContext == NULL)
9373 return PyErr_Format(PyExc_NotImplementedError,
9374 "CryptAcquireContextA not found");
9375
9376 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9377 hAdvAPI32, "CryptGenRandom");
9378 if (pCryptGenRandom == NULL)
9379 return PyErr_Format(PyExc_NotImplementedError,
9380 "CryptGenRandom not found");
9381
9382 /* Acquire context */
9383 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9384 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9385 return win32_error("CryptAcquireContext", NULL);
9386 }
9387 9342
9388 /* Allocate bytes */ 9343 /* Allocate bytes */
9389 result = PyBytes_FromStringAndSize(NULL, howMany); 9344 result = PyBytes_FromStringAndSize(NULL, howMany);
9390 if (result != NULL) { 9345 if (result != NULL) {
9391 /* Get random data */ 9346 /* Get random data */
9392 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ 9347 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9393 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) 9348 if (PyOS_URandom(PyBytes_AS_STRING(result), howMany) == -1) {
9394 PyBytes_AS_STRING(result))) {
9395 Py_DECREF(result); 9349 Py_DECREF(result);
9396 return win32_error("CryptGenRandom", NULL); 9350 return NULL;
9397 } 9351 }
9398 } 9352 }
9399 return result; 9353 return result;
9400 } 9354 }
9401 #endif 9355 #endif
9402 9356
9403 PyDoc_STRVAR(device_encoding__doc__, 9357 PyDoc_STRVAR(device_encoding__doc__,
9404 "device_encoding(fd) -> str\n\n\ 9358 "device_encoding(fd) -> str\n\n\
9405 Return a string describing the encoding of the device\n\ 9359 Return a string describing the encoding of the device\n\
9406 if the output is a terminal; else return None."); 9360 if the output is a terminal; else return None.");
(...skipping 2204 matching lines...) Expand 10 before | Expand all | Expand 10 after
11611 11565
11612 11566
11613 #endif /* __APPLE__ */ 11567 #endif /* __APPLE__ */
11614 return m; 11568 return m;
11615 11569
11616 } 11570 }
11617 11571
11618 #ifdef __cplusplus 11572 #ifdef __cplusplus
11619 } 11573 }
11620 #endif 11574 #endif
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+