Mercurial > cpython-lru_cache-weakref
annotate Python/random.c @ 81258:5bb289e4fb35 3.3
(Merge 3.2) Issue #16455: On FreeBSD and Solaris, if the locale is C, the
ASCII/surrogateescape codec is now used, instead of the locale encoding, to
decode the command line arguments. This change fixes inconsistencies with
os.fsencode() and os.fsdecode() because these operating systems announces an
ASCII locale encoding, whereas the ISO-8859-1 encoding is used in practice.
| author | Victor Stinner <victor.stinner@gmail.com> |
|---|---|
| date | Thu, 03 Jan 2013 01:21:07 +0100 |
| parents | 800507fb0a7e |
| children | 193bcc12575d e52df05b496a |
| rev | line source |
|---|---|
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 #include "Python.h" |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 #ifdef MS_WINDOWS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 #include <windows.h> |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 #else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 #include <fcntl.h> |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 #endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
8 #ifdef Py_DEBUG |
|
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
9 int _Py_HashSecret_Initialized = 0; |
|
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
10 #else |
|
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
11 static int _Py_HashSecret_Initialized = 0; |
|
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
12 #endif |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
13 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 #ifdef MS_WINDOWS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 DWORD dwFlags ); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 BYTE *pbBuffer ); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 static CRYPTGENRANDOM pCryptGenRandom = NULL; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 /* This handle is never explicitly released. Instead, the operating |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 system will release it when the process terminates. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 static HCRYPTPROV hCryptProv = 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 static int |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 win32_urandom_init(int raise) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 HINSTANCE hAdvAPI32 = NULL; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 /* Obtain handle to the DLL containing CryptoAPI. This should not fail. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 if(hAdvAPI32 == NULL) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
35 goto error; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 /* Obtain pointers to the CryptoAPI functions. This will fail on some early |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 versions of Win95. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 hAdvAPI32, "CryptAcquireContextA"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 if (pCryptAcquireContext == NULL) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 goto error; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 "CryptGenRandom"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
46 if (pCryptGenRandom == NULL) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 goto error; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
48 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 /* Acquire context */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 goto error; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
53 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 error: |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 if (raise) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 PyErr_SetFromWindowsErr(0); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 Py_FatalError("Failed to initialize Windows random API (CryptoGen)"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
63 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
65 API. Return 0 on success, or -1 on error. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
66 static int |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
67 win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
68 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 Py_ssize_t chunk; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
70 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
71 if (hCryptProv == 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
73 if (win32_urandom_init(raise) == -1) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
74 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 while (size > 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 chunk = size > INT_MAX ? INT_MAX : size; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
80 if (!pCryptGenRandom(hCryptProv, chunk, buffer)) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
82 /* CryptGenRandom() failed */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 if (raise) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
84 PyErr_SetFromWindowsErr(0); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 Py_FatalError("Failed to initialized the randomized hash " |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 "secret using CryptoGen)"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 buffer += chunk; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 size -= chunk; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
92 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
93 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
94 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
95 #endif /* MS_WINDOWS */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
96 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
97 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 #ifdef __VMS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 /* Use openssl random routine */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
100 #include <openssl/rand.h> |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 static int |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 if (RAND_pseudo_bytes(buffer, size) < 0) { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 if (raise) { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 PyErr_Format(PyExc_ValueError, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 "RAND_pseudo_bytes"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
108 } else { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 Py_FatalError("Failed to initialize the randomized hash " |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 "secret using RAND_pseudo_bytes"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 #endif /* __VMS */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 #if !defined(MS_WINDOWS) && !defined(__VMS) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 /* Read size bytes from /dev/urandom into buffer. |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 Call Py_FatalError() on error. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 static void |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 dev_urandom_noraise(char *buffer, Py_ssize_t size) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 int fd; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 Py_ssize_t n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
129 assert (0 < size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
130 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 fd = open("/dev/urandom", O_RDONLY); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 if (fd < 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 Py_FatalError("Failed to open /dev/urandom"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 while (0 < size) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
136 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
137 do { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
138 n = read(fd, buffer, (size_t)size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 } while (n < 0 && errno == EINTR); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 if (n <= 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
142 /* stop on error or if read(size) returned 0 */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
143 Py_FatalError("Failed to read bytes from /dev/urandom"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
144 break; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
146 buffer += n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 size -= (Py_ssize_t)n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
149 close(fd); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
150 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
151 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
152 /* Read size bytes from /dev/urandom into buffer. |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
153 Return 0 on success, raise an exception and return -1 on error. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
154 static int |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 dev_urandom_python(char *buffer, Py_ssize_t size) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
157 int fd; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
158 Py_ssize_t n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
159 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
160 if (size <= 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
161 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
162 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 Py_BEGIN_ALLOW_THREADS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
164 fd = open("/dev/urandom", O_RDONLY); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
165 Py_END_ALLOW_THREADS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
166 if (fd < 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
167 { |
|
78901
6a782496f90a
Issue #15340: Fix importing the random module when /dev/urandom cannot be opened.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75152
diff
changeset
|
168 PyErr_SetString(PyExc_NotImplementedError, |
|
6a782496f90a
Issue #15340: Fix importing the random module when /dev/urandom cannot be opened.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75152
diff
changeset
|
169 "/dev/urandom (or equivalent) not found"); |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
170 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
172 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
173 Py_BEGIN_ALLOW_THREADS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
174 do { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
175 do { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
176 n = read(fd, buffer, (size_t)size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
177 } while (n < 0 && errno == EINTR); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
178 if (n <= 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 break; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
180 buffer += n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
181 size -= (Py_ssize_t)n; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
182 } while (0 < size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 Py_END_ALLOW_THREADS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
184 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 if (n <= 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 /* stop on error or if read(size) returned 0 */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
188 if (n < 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
189 PyErr_SetFromErrno(PyExc_OSError); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
190 else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 PyErr_Format(PyExc_RuntimeError, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 "Failed to read %zi bytes from /dev/urandom", |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
194 close(fd); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
196 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
197 close(fd); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
199 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
200 #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
201 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
202 /* Fill buffer with pseudo-random bytes generated by a linear congruent |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
203 generator (LCG): |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
207 Use bits 23..16 of x(n) to generate a byte. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
208 static void |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 size_t index; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 unsigned int x; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 x = x0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 for (index=0; index < size; index++) { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 x *= 214013; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
217 x += 2531011; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 /* modulo 2 ^ (8 * sizeof(int)) */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
219 buffer[index] = (x >> 16) & 0xff; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
221 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
222 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
223 /* Fill buffer with size pseudo-random bytes, not suitable for cryptographic |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
224 use, from the operating random number generator (RNG). |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
225 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
226 Return 0 on success, raise an exception and return -1 on error. */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
227 int |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
228 _PyOS_URandom(void *buffer, Py_ssize_t size) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
230 if (size < 0) { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
231 PyErr_Format(PyExc_ValueError, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
232 "negative argument not allowed"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
233 return -1; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
234 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
235 if (size == 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
236 return 0; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
237 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
238 #ifdef MS_WINDOWS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
239 return win32_urandom((unsigned char *)buffer, size, 1); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
240 #else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
241 # ifdef __VMS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
242 return vms_urandom((unsigned char *)buffer, size, 1); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
243 # else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
244 return dev_urandom_python((char*)buffer, size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
245 # endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
246 #endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
247 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
248 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
249 void |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 _PyRandom_Init(void) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
251 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
252 char *env; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
253 void *secret = &_Py_HashSecret; |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
254 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
255 |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
256 if (_Py_HashSecret_Initialized) |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
257 return; |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
258 _Py_HashSecret_Initialized = 1; |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
259 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
260 /* |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
261 Hash randomization is enabled. Generate a per-process secret, |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
262 using PYTHONHASHSEED if provided. |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
263 */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
264 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 env = Py_GETENV("PYTHONHASHSEED"); |
| 75087 | 266 if (env && *env != '\0' && strcmp(env, "random") != 0) { |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 char *endptr = env; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 unsigned long seed; |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
269 seed = strtoul(env, &endptr, 10); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
270 if (*endptr != '\0' |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
271 || seed > 4294967295UL |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
272 || (errno == ERANGE && seed == ULONG_MAX)) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
273 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer " |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 "in range [0; 4294967295]"); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 if (seed == 0) { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 /* disable the randomized hash */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
279 memset(secret, 0, secret_size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
281 else { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
282 lcg_urandom(seed, (unsigned char*)secret, secret_size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
285 else { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 #ifdef MS_WINDOWS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 (void)win32_urandom((unsigned char *)secret, secret_size, 0); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 #else /* #ifdef MS_WINDOWS */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 # ifdef __VMS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 vms_urandom((unsigned char *)secret, secret_size, 0); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
291 # else |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 dev_urandom_noraise((char*)secret, secret_size); |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 # endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 #endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 } |
