diff -r 656fa057aa28 -r 3106cc0a2024 Include/Python.h --- a/Include/Python.h Fri Dec 30 21:26:08 2011 +0100 +++ b/Include/Python.h Tue Jan 03 21:48:43 2012 +0100 @@ -128,5 +128,6 @@ #include "dtoa.h" #include "fileutils.h" #include "pyfpe.h" +#include "pyhash.h" #endif /* !Py_PYTHON_H */ diff -r 656fa057aa28 -r 3106cc0a2024 Include/pydebug.h --- a/Include/pydebug.h Fri Dec 30 21:26:08 2011 +0100 +++ b/Include/pydebug.h Tue Jan 03 21:48:43 2012 +0100 @@ -19,6 +19,7 @@ PyAPI_DATA(int) Py_DontWriteBytecodeFlag; PyAPI_DATA(int) Py_NoUserSiteDirectory; PyAPI_DATA(int) Py_UnbufferedStdioFlag; +PyAPI_DATA(int) Py_NoRandomHashFlag; /* this is a wrapper around getenv() that pays attention to Py_IgnoreEnvironmentFlag. It should be used for getting variables like diff -r 656fa057aa28 -r 3106cc0a2024 Include/pyhash.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Include/pyhash.h Tue Jan 03 21:48:43 2012 +0100 @@ -0,0 +1,7 @@ +#ifndef Py_PYHASH_H +#define Py_PYHASH_H + +PyAPI_DATA(Py_hash_t) Py_RndHashSeed; +PyAPI_FUNC(Py_hash_t) Py_GetRndHashSeed(void); + +#endif diff -r 656fa057aa28 -r 3106cc0a2024 Include/pyrandom.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Include/pyrandom.h Tue Jan 03 21:48:43 2012 +0100 @@ -0,0 +1,18 @@ +#ifndef Py_PYRANDOM_H +#define Py_PYRANDOM_H + +int PyOS_URandom(unsigned char *buf, Py_ssize_t len); + +#define _Py_MT_N 624 + +typedef struct { + unsigned long state[_Py_MT_N]; + int index; +} _Py_MT_RandomState; + +unsigned long _Py_MT_GenRand_Int32(_Py_MT_RandomState *state); // genrand_int32() +double _Py_MT_GenRand_Res53(_Py_MT_RandomState *state); // random_random() +void _Py_MT_GenRand_Init(_Py_MT_RandomState *state, unsigned long seed); // init_genrand() +void _Py_MT_GenRand_InitArray(_Py_MT_RandomState *state, unsigned long init_key[], unsigned long key_length); // init_by_array + +#endif diff -r 656fa057aa28 -r 3106cc0a2024 Include/pythonrun.h --- a/Include/pythonrun.h Fri Dec 30 21:26:08 2011 +0100 +++ b/Include/pythonrun.h Tue Jan 03 21:48:43 2012 +0100 @@ -246,7 +246,6 @@ PyAPI_FUNC(PyOS_sighandler_t) PyOS_getsig(int); PyAPI_FUNC(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t); - #ifdef __cplusplus } #endif diff -r 656fa057aa28 -r 3106cc0a2024 Lib/test/test_sys.py --- a/Lib/test/test_sys.py Fri Dec 30 21:26:08 2011 +0100 +++ b/Lib/test/test_sys.py Tue Jan 03 21:48:43 2012 +0100 @@ -513,7 +513,7 @@ attrs = ("debug", "inspect", "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site", "ignore_environment", "verbose", - "bytes_warning", "quiet") + "bytes_warning", "quiet", "no_random_hash") for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) self.assertEqual(type(getattr(sys.flags, attr)), int, attr) diff -r 656fa057aa28 -r 3106cc0a2024 Makefile.pre.in --- a/Makefile.pre.in Fri Dec 30 21:26:08 2011 +0100 +++ b/Makefile.pre.in Tue Jan 03 21:48:43 2012 +0100 @@ -308,6 +308,7 @@ Python/getplatform.o \ Python/getversion.o \ Python/graminit.o \ + Python/hash.o \ Python/import.o \ Python/importdl.o \ Python/marshal.o \ @@ -322,6 +323,7 @@ Python/pystate.o \ Python/pythonrun.o \ Python/pytime.o \ + Python/random.o \ Python/structmember.o \ Python/symtable.o \ Python/sysmodule.o \ @@ -736,11 +738,13 @@ $(srcdir)/Include/pydebug.h \ $(srcdir)/Include/pyerrors.h \ $(srcdir)/Include/pyfpe.h \ + $(srcdir)/Include/pyhash.h \ $(srcdir)/Include/pymath.h \ $(srcdir)/Include/pygetopt.h \ $(srcdir)/Include/pymacro.h \ $(srcdir)/Include/pymem.h \ $(srcdir)/Include/pyport.h \ + $(srcdir)/Include/pyrandom.h \ $(srcdir)/Include/pystate.h \ $(srcdir)/Include/pystrcmp.h \ $(srcdir)/Include/pystrtod.h \ diff -r 656fa057aa28 -r 3106cc0a2024 Modules/_randommodule.c --- a/Modules/_randommodule.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Modules/_randommodule.c Tue Jan 03 21:48:43 2012 +0100 @@ -1,85 +1,13 @@ /* Random objects */ - -/* ------------------------------------------------------------------ - The code in this module was based on a download from: - http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html - - It was modified in 2002 by Raymond Hettinger as follows: - - * the principal computational lines untouched. - - * renamed genrand_res53() to random_random() and wrapped - in python calling/return code. - - * genrand_int32() and the helper functions, init_genrand() - and init_by_array(), were declared static, wrapped in - Python calling/return code. also, their global data - references were replaced with structure references. - - * unused functions from the original were deleted. - new, original C python code was added to implement the - Random() interface. - - The following are the verbatim comments from the original code: - - A C-program for MT19937, with initialization improved 2002/1/26. - Coded by Takuji Nishimura and Makoto Matsumoto. - - Before using, initialize the state by using init_genrand(seed) - or init_by_array(init_key, key_length). - - Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. The names of its contributors may not be used to endorse or promote - products derived from this software without specific prior written - permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - - Any feedback is very welcome. - http://www.math.keio.ac.jp/matumoto/emt.html - email: matumoto@math.keio.ac.jp -*/ - -/* ---------------------------------------------------------------*/ - #include "Python.h" +#include "pyrandom.h" #include /* for seeding to current time */ -/* Period parameters -- These are all magic. Don't change. */ -#define N 624 -#define M 397 -#define MATRIX_A 0x9908b0dfUL /* constant vector a */ -#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ -#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ +#define N _Py_MT_N typedef struct { PyObject_HEAD - unsigned long state[N]; - int index; + _Py_MT_RandomState rs; } RandomObject; static PyTypeObject Random_Type; @@ -87,123 +15,19 @@ #define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type) -/* Random methods */ - - -/* generates a random number on [0,0xffffffff]-interval */ -static unsigned long -genrand_int32(RandomObject *self) -{ - unsigned long y; - static unsigned long mag01[2]={0x0UL, MATRIX_A}; - /* mag01[x] = x * MATRIX_A for x=0,1 */ - unsigned long *mt; - - mt = self->state; - if (self->index >= N) { /* generate N words at one time */ - int kk; - - for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; - } - for (;kk> 1) ^ mag01[y & 0x1UL]; - } - y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); - mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; - - self->index = 0; - } - - y = mt[self->index++]; - y ^= (y >> 11); - y ^= (y << 7) & 0x9d2c5680UL; - y ^= (y << 15) & 0xefc60000UL; - y ^= (y >> 18); - return y; -} - -/* random_random is the function named genrand_res53 in the original code; - * generates a random number on [0,1) with 53-bit resolution; note that - * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as - * multiply-by-reciprocal in the (likely vain) hope that the compiler will - * optimize the division away at compile-time. 67108864 is 2**26. In - * effect, a contains 27 random bits shifted left 26, and b fills in the - * lower 26 bits of the 53-bit numerator. - * The orginal code credited Isaku Wada for this algorithm, 2002/01/09. - */ -static PyObject * -random_random(RandomObject *self) -{ - unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6; - return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); -} - -/* initializes mt[N] with a seed */ -static void -init_genrand(RandomObject *self, unsigned long s) -{ - int mti; - unsigned long *mt; - - mt = self->state; - mt[0]= s & 0xffffffffUL; - for (mti=1; mti> 30)) + mti); - /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ - /* In the previous versions, MSBs of the seed affect */ - /* only MSBs of the array mt[]. */ - /* 2002/01/09 modified by Makoto Matsumoto */ - mt[mti] &= 0xffffffffUL; - /* for >32 bit machines */ - } - self->index = mti; - return; -} - -/* initialize by an array with array-length */ -/* init_key is the array for initializing keys */ -/* key_length is its length */ -static PyObject * -init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length) -{ - unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */ - unsigned long *mt; - - mt = self->state; - init_genrand(self, 19650218UL); - i=1; j=0; - k = (N>key_length ? N : key_length); - for (; k; k--) { - mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) - + init_key[j] + j; /* non linear */ - mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ - i++; j++; - if (i>=N) { mt[0] = mt[N-1]; i=1; } - if (j>=key_length) j=0; - } - for (k=N-1; k; k--) { - mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - - i; /* non linear */ - mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ - i++; - if (i>=N) { mt[0] = mt[N-1]; i=1; } - } - - mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ - Py_INCREF(Py_None); - return Py_None; -} - /* * The rest is Python-specific code, neither part of, nor derived from, the * Twister download. */ static PyObject * +random_random(RandomObject *self) +{ + return PyFloat_FromDouble(_Py_MT_GenRand_Res53(&self->rs)); +} + + +static PyObject * random_seed(RandomObject *self, PyObject *args) { PyObject *result = NULL; /* guilty until proved innocent */ @@ -224,7 +48,7 @@ time_t now; time(&now); - init_genrand(self, (unsigned long)now); + _Py_MT_GenRand_Init(&self->rs, (unsigned long)now); Py_INCREF(Py_None); return Py_None; } @@ -299,7 +123,9 @@ if (keyused == 0) key[keyused++] = 0UL; - result = init_by_array(self, key, keyused); + _Py_MT_GenRand_InitArray(&self->rs, key, keyused); + Py_INCREF(Py_None); + result = Py_None; Done: Py_XDECREF(masklower); Py_XDECREF(thirtytwo); @@ -319,12 +145,12 @@ if (state == NULL) return NULL; for (i=0; istate[i]); + element = PyLong_FromUnsignedLong(self->rs.state[i]); if (element == NULL) goto Fail; PyTuple_SET_ITEM(state, i, element); } - element = PyLong_FromLong((long)(self->index)); + element = PyLong_FromLong((long)(self->rs.index)); if (element == NULL) goto Fail; PyTuple_SET_ITEM(state, i, element); @@ -357,13 +183,13 @@ element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); if (element == (unsigned long)-1 && PyErr_Occurred()) return NULL; - self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */ + self->rs.state[i] = element & 0xffffffffUL; /* Make sure we get sane state */ } index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); if (index == -1 && PyErr_Occurred()) return NULL; - self->index = (int)index; + self->rs.index = (int)index; Py_INCREF(Py_None); return Py_None; @@ -395,7 +221,7 @@ /* Fill-out whole words, byte-by-byte to avoid endianness issues */ for (i=0 ; irs); if (k < 32) r >>= (32 - k); bytearray[i+0] = (unsigned char)r; diff -r 656fa057aa28 -r 3106cc0a2024 Modules/main.c --- a/Modules/main.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Modules/main.c Tue Jan 03 21:48:43 2012 +0100 @@ -73,6 +73,7 @@ -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ -OO : remove doc-strings in addition to the -O optimizations\n\ -q : don't print version and copyright messages on interactive startup\n\ +-r : don't use randomized hash\n\ -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\ -S : don't imply 'import site' on initialization\n\ "; @@ -381,6 +382,10 @@ Py_DontWriteBytecodeFlag++; break; + case 'r': + Py_NoRandomHashFlag++; + break; + case 's': Py_NoUserSiteDirectory++; break; diff -r 656fa057aa28 -r 3106cc0a2024 Modules/posixmodule.c --- a/Modules/posixmodule.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Modules/posixmodule.c Tue Jan 03 21:48:43 2012 +0100 @@ -9330,17 +9330,6 @@ "urandom(n) -> str\n\n\ Return n random bytes suitable for cryptographic use."); -typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ - LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ - DWORD dwFlags ); -typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ - BYTE *pbBuffer ); - -static CRYPTGENRANDOM pCryptGenRandom = NULL; -/* This handle is never explicitly released. Instead, the operating - system will release it when the process terminates. */ -static HCRYPTPROV hCryptProv = 0; - static PyObject* win32_urandom(PyObject *self, PyObject *args) { @@ -9350,50 +9339,15 @@ /* Read arguments */ if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) return NULL; - if (howMany < 0) - return PyErr_Format(PyExc_ValueError, - "negative argument not allowed"); - - if (hCryptProv == 0) { - HINSTANCE hAdvAPI32 = NULL; - CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; - - /* Obtain handle to the DLL containing CryptoAPI - This should not fail */ - hAdvAPI32 = GetModuleHandle("advapi32.dll"); - if(hAdvAPI32 == NULL) - return win32_error("GetModuleHandle", NULL); - - /* Obtain pointers to the CryptoAPI functions - This will fail on some early versions of Win95 */ - pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( - hAdvAPI32, - "CryptAcquireContextA"); - if (pCryptAcquireContext == NULL) - return PyErr_Format(PyExc_NotImplementedError, - "CryptAcquireContextA not found"); - - pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( - hAdvAPI32, "CryptGenRandom"); - if (pCryptGenRandom == NULL) - return PyErr_Format(PyExc_NotImplementedError, - "CryptGenRandom not found"); - - /* Acquire context */ - if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, - PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - return win32_error("CryptAcquireContext", NULL); - } /* Allocate bytes */ result = PyBytes_FromStringAndSize(NULL, howMany); if (result != NULL) { /* Get random data */ memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ - if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) - PyBytes_AS_STRING(result))) { + if (PyOS_URandom(PyBytes_AS_STRING(result), howMany) == -1) { Py_DECREF(result); - return win32_error("CryptGenRandom", NULL); + return NULL; } } return result; diff -r 656fa057aa28 -r 3106cc0a2024 Objects/object.c --- a/Objects/object.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Objects/object.c Tue Jan 03 21:48:43 2012 +0100 @@ -759,7 +759,7 @@ Py_uhash_t x; Py_ssize_t i; - x = (Py_uhash_t) *p << 7; + x = Py_RndHashSeed + ((Py_uhash_t) *p << 7); for (i = 0; i < len; i++) x = (1000003U * x) ^ (Py_uhash_t) *p++; x ^= (Py_uhash_t) len; diff -r 656fa057aa28 -r 3106cc0a2024 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Objects/unicodeobject.c Tue Jan 03 21:48:43 2012 +0100 @@ -11107,7 +11107,7 @@ /* The hash function as a macro, gets expanded three times below. */ #define HASH(P) \ - x = (Py_uhash_t)*P << 7; \ + x = Py_RndHashSeed + ((Py_uhash_t)*P << 7); \ while (--len >= 0) \ x = (1000003*x) ^ (Py_uhash_t)*P++; diff -r 656fa057aa28 -r 3106cc0a2024 Python/hash.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Python/hash.c Tue Jan 03 21:48:43 2012 +0100 @@ -0,0 +1,39 @@ +#include "Python.h" +#include "pyrandom.h" + +Py_hash_t Py_RndHashSeed = -1; + +void +_Py_InitRndHashSeed(void) +{ + static int initialized = 0; + int result; + if (initialized) { + return; + } + initialized = 1; + + result = PyOS_URandom((unsigned char*)&Py_RndHashSeed, sizeof(Py_hash_t)); + /* fall back to Mersenne twister */ + if (result == -1) { + _Py_MT_RandomState state; + time_t seed; + int i; + + time(&seed); + _Py_MT_GenRand_Init(&state, seed); + + Py_RndHashSeed = _Py_MT_GenRand_Int32(&state); + if (Py_RndHashSeed == -1) { + Py_RndHashSeed = -2; + } + + } +} + +/* Randomized hash seed */ +Py_hash_t +Py_GetRndHashSeed(void) +{ + return Py_RndHashSeed; +} diff -r 656fa057aa28 -r 3106cc0a2024 Python/pythonrun.c --- a/Python/pythonrun.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Python/pythonrun.c Tue Jan 03 21:48:43 2012 +0100 @@ -73,6 +73,7 @@ extern void PyLong_Fini(void); extern int _PyFaulthandler_Init(void); extern void _PyFaulthandler_Fini(void); +extern void _Py_InitRndHashSeed(void); #ifdef WITH_THREAD extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); @@ -92,9 +93,11 @@ int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ +int Py_NoRandomHashFlag = 0; /* Don't use randomized hashes */ PyThreadState *_Py_Finalizing = NULL; + /* PyModule_GetWarningsModule is no longer necessary as of 2.6 since _warnings is builtin. This API should not be used. */ PyObject * @@ -218,6 +221,12 @@ Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); + if ((p = Py_GETENV("PYTHONNORANDOMHASH")) && *p != '\0') + Py_NoRandomHashFlag = add_flag(Py_NoRandomHashFlag, p); + + if (!Py_NoRandomHashFlag) { + _Py_InitRndHashSeed(); + } interp = PyInterpreterState_New(); if (interp == NULL) diff -r 656fa057aa28 -r 3106cc0a2024 Python/random.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Python/random.c Tue Jan 03 21:48:43 2012 +0100 @@ -0,0 +1,344 @@ +#include "Python.h" +#include "pyrandom.h" +#include + +#ifndef DEV_URANDOM +#define DEV_URANDOM "/dev/urandom" +#endif + +/* raise a fatal error when the interpreter is in the early initialization + * phase + * XXX: replace with something better than Py_RndHashSeed == -1 + * */ + +#define error_string(exc, msg) do { \ + if (Py_RndHashSeed == -1) { \ + Py_FatalError(msg); \ + } else { \ + PyErr_SetString(exc, msg); \ + } \ + } while(0) + +#define error_errno(exc, filename, msg) do { \ + if (Py_RndHashSeed == -1) { \ + Py_FatalError(msg); \ + } else { \ + PyErr_SetFromErrnoWithFilename(exc, filename); \ + } \ + } while(0) + + +#ifdef MS_WINDOWS + +#define win32_error(function, filename) do { \ + if (Py_RndHashSeed == -1) { \ + Py_FatalError(function); \ + } else { \ + errno = GetLastError(); \ + PyErr_SetFromWindowsErr(errno) \ + } \ + } while(0) + +typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ + LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ + DWORD dwFlags ); +typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ + BYTE *pbBuffer ); + +static CRYPTGENRANDOM pCryptGenRandom = NULL; +/* This handle is never explicitly released. Instead, the operating + system will release it when the process terminates. */ +static HCRYPTPROV hCryptProv = 0; + +/* + * Read random data with CryptGenRandom() + * + * In case of error, an exception is set + * + * @param buf: input buffer + * @param len: how many bytes to read into buf + * @return: 0 on success, -1 on error + */ +int +PyOS_URandom(unsigned char *buf, Py_ssize_t len) +{ + if (len < 0) { + error_string(PyExc_ValueError, + "negative argument not allowed"); + return -1; + } + + if (hCryptProv == 0) { + HINSTANCE hAdvAPI32 = NULL; + CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; + + /* Obtain handle to the DLL containing CryptoAPI + This should not fail */ + hAdvAPI32 = GetModuleHandle("advapi32.dll"); + if (hAdvAPI32 == NULL) { + win32_error("GetModuleHandle", NULL); + return -1 + } + + /* Obtain pointers to the CryptoAPI functions + This will fail on some early versions of Win95 */ + pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( + hAdvAPI32, + "CryptAcquireContextA"); + if (pCryptAcquireContext == NULL) { + error_string(PyExc_NotImplementedError, + "CryptAcquireContextA not found"); + return -1; + } + + pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( + hAdvAPI32, "CryptGenRandom"); + if (pCryptGenRandom == NULL) { + error_string(PyExc_NotImplementedError, + "CryptGenRandom not found"); + return -1; + } + + /* Acquire context */ + if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, + PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { + win32_error("CryptAcquireContext", NULL); + return -1; + } + } + + /* Get random data */ + memset(buf, 0, len); /* zero seed */ + if (!pCryptGenRandom(hCryptProv, len, buf)) { + win32_error("CryptGenRandom", NULL); + return -1; + } + } + return 0; +} +#else + +#include +#include +#include + +/* + * Read random data from /dev/urandom + * + * In case of error, an exception is set + * + * @param buf: input buffer + * @param len: how many bytes to read into buf + * @return: 0 on success, -1 on error + */ +int +PyOS_URandom(unsigned char *buf, Py_ssize_t len) { + int fd; + ssize_t pos, result = 0; + + if (len < 0) { + error_string(PyExc_ValueError, + "negative argument not allowed"); + return -1; + } + + if ((fd = open(DEV_URANDOM, O_RDONLY)) == -1) { + error_errno(PyExc_OSError, DEV_URANDOM, "Can't open /dev/urandom"); + return -1; + } + + while (pos < len) { + if ((result = read(fd, buf+pos, len-pos)) == -1) { + close(fd); + error_errno(PyExc_OSError, DEV_URANDOM, "Error reading from /dev/urandom"); + return -1; + } + pos += result; + } + close(fd); + return 0; +} + +#endif + + +/* ------------------------------------------------------------------ + The code in this module was based on a download from: + http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html + + It was modified in 2002 by Raymond Hettinger as follows: + + * the principal computational lines untouched. + + * renamed genrand_res53() to random_random() and wrapped + in python calling/return code. + + * genrand_int32() and the helper functions, init_genrand() + and init_by_array(), were declared static, wrapped in + Python calling/return code. also, their global data + references were replaced with structure references. + + * unused functions from the original were deleted. + new, original C python code was added to implement the + Random() interface. + + The following are the verbatim comments from the original code: + + A C-program for MT19937, with initialization improved 2002/1/26. + Coded by Takuji Nishimura and Makoto Matsumoto. + + Before using, initialize the state by using init_genrand(seed) + or init_by_array(init_key, key_length). + + Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + Any feedback is very welcome. + http://www.math.keio.ac.jp/matumoto/emt.html + email: matumoto@math.keio.ac.jp +*/ + +/* ---------------------------------------------------------------*/ + +/* Period parameters -- These are all magic. Don't change. */ +#define N _Py_MT_N +#define M 397 +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ + +/* Random methods */ + +/* generates a random number on [0,0xffffffff]-interval */ +unsigned long +_Py_MT_GenRand_Int32(_Py_MT_RandomState *state) +{ + unsigned long y; + static unsigned long mag01[2]={0x0UL, MATRIX_A}; + /* mag01[x] = x * MATRIX_A for x=0,1 */ + unsigned long *mt; + + mt = state->state; + if (state->index >= N) { /* generate N words at one time */ + int kk; + + for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; + } + for (;kk> 1) ^ mag01[y & 0x1UL]; + } + y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); + mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; + + state->index = 0; + } + + y = mt[state->index++]; + y ^= (y >> 11); + y ^= (y << 7) & 0x9d2c5680UL; + y ^= (y << 15) & 0xefc60000UL; + y ^= (y >> 18); + return y; +} + +/* random_random is the function named genrand_res53 in the original code; + * generates a random number on [0,1) with 53-bit resolution; note that + * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as + * multiply-by-reciprocal in the (likely vain) hope that the compiler will + * optimize the division away at compile-time. 67108864 is 2**26. In + * effect, a contains 27 random bits shifted left 26, and b fills in the + * lower 26 bits of the 53-bit numerator. + * The orginal code credited Isaku Wada for this algorithm, 2002/01/09. + */ +double +_Py_MT_GenRand_Res53(_Py_MT_RandomState *state) +{ + unsigned long a=_Py_MT_GenRand_Int32(state)>>5, b=_Py_MT_GenRand_Int32(state)>>6; + return (a*67108864.0+b)*(1.0/9007199254740992.0); +} + +/* initializes mt[N] with a seed */ +void +_Py_MT_GenRand_Init(_Py_MT_RandomState *state, unsigned long s) +{ + int mti; + unsigned long *mt; + + mt = state->state; + mt[0]= s & 0xffffffffUL; + for (mti=1; mti> 30)) + mti); + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ + /* In the previous versions, MSBs of the seed affect */ + /* only MSBs of the array mt[]. */ + /* 2002/01/09 modified by Makoto Matsumoto */ + mt[mti] &= 0xffffffffUL; + /* for >32 bit machines */ + } + state->index = mti; + return; +} + +/* initialize by an array with array-length */ +/* init_key is the array for initializing keys */ +/* key_length is its length */ +void +_Py_MT_GenRand_InitArray(_Py_MT_RandomState *state, unsigned long init_key[], unsigned long key_length) +{ + unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */ + unsigned long *mt; + + mt = state->state; + _Py_MT_GenRand_Init(state, 19650218UL); + i=1; j=0; + k = (N>key_length ? N : key_length); + for (; k; k--) { + mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + + init_key[j] + j; /* non linear */ + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; j++; + if (i>=N) { mt[0] = mt[N-1]; i=1; } + if (j>=key_length) j=0; + } + for (k=N-1; k; k--) { + mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) + - i; /* non linear */ + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; + if (i>=N) { mt[0] = mt[N-1]; i=1; } + } + + mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ +} diff -r 656fa057aa28 -r 3106cc0a2024 Python/sysmodule.c --- a/Python/sysmodule.c Fri Dec 30 21:26:08 2011 +0100 +++ b/Python/sysmodule.c Tue Jan 03 21:48:43 2012 +0100 @@ -1332,6 +1332,7 @@ /* {"skip_first", "-x"}, */ {"bytes_warning", "-b"}, {"quiet", "-q"}, + {"no_random_hash", "-r"}, {0} }; @@ -1340,9 +1341,9 @@ flags__doc__, /* doc */ flags_fields, /* fields */ #ifdef RISCOS + 13 +#else 12 -#else - 11 #endif }; @@ -1375,6 +1376,7 @@ /* SetFlag(skipfirstline); */ SetFlag(Py_BytesWarningFlag); SetFlag(Py_QuietFlag); + SetFlag(Py_NoRandomHashFlag); #undef SetFlag if (PyErr_Occurred()) {