Mercurial > cpython-lru_cache-weakref
annotate Python/random.c @ 83590:8a364deb0225
Closes #17892: Fix the name of _PyObject_CallMethodObjIdArgs
| author | Alexandre Vassalotti <alexandre@peadrop.com> |
|---|---|
| date | Thu, 02 May 2013 10:44:04 -0700 |
| parents | e52df05b496a |
| children | fe949918616c |
| 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 /* 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
|
16 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
|
17 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
|
18 |
|
f4b7ecf8a5f8
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 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
|
20 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
|
21 { |
|
f4b7ecf8a5f8
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 /* Acquire context */ |
|
81711
e52df05b496a
Drop support for Windows 2000; allow any XP API (but not Vista+).
Martin v. Löwis <martin@v.loewis.de>
parents:
78921
diff
changeset
|
23 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, |
|
e52df05b496a
Drop support for Windows 2000; allow any XP API (but not Vista+).
Martin v. Löwis <martin@v.loewis.de>
parents:
78921
diff
changeset
|
24 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
|
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
|
25 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
|
26 |
|
f4b7ecf8a5f8
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 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
|
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 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 } |
|
f4b7ecf8a5f8
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 /* 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
|
38 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
|
39 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
|
40 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
|
41 { |
|
f4b7ecf8a5f8
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 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
|
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 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
|
45 { |
|
f4b7ecf8a5f8
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 (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
|
47 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
|
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 |
|
f4b7ecf8a5f8
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 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
|
51 { |
|
f4b7ecf8a5f8
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 chunk = size > INT_MAX ? INT_MAX : size; |
|
81711
e52df05b496a
Drop support for Windows 2000; allow any XP API (but not Vista+).
Martin v. Löwis <martin@v.loewis.de>
parents:
78921
diff
changeset
|
53 if (!CryptGenRandom(hCryptProv, chunk, buffer)) |
|
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
|
54 { |
|
f4b7ecf8a5f8
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 /* 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 "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
|
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 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
|
64 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
|
65 } |
|
f4b7ecf8a5f8
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 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
|
67 } |
|
f4b7ecf8a5f8
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 #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
|
69 |
|
f4b7ecf8a5f8
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 #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
|
72 /* 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
|
73 #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
|
74 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
|
75 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
|
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 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
|
78 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
|
79 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
|
80 "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
|
81 } 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
|
82 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
|
83 "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
|
84 } |
|
f4b7ecf8a5f8
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 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
|
86 } |
|
f4b7ecf8a5f8
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 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
|
88 } |
|
f4b7ecf8a5f8
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 #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
|
90 |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 #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
|
93 |
|
f4b7ecf8a5f8
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 /* 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
|
95 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
|
96 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
|
97 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
|
98 { |
|
f4b7ecf8a5f8
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 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
|
100 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
|
101 |
|
f4b7ecf8a5f8
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 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
|
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 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
|
105 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
|
106 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
|
107 |
|
f4b7ecf8a5f8
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 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
|
109 { |
|
f4b7ecf8a5f8
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 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
|
111 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
|
112 } 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
|
113 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
|
114 { |
|
f4b7ecf8a5f8
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 /* 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
|
116 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
|
117 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
|
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 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
|
120 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
|
121 } |
|
f4b7ecf8a5f8
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 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
|
123 } |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 /* 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
|
126 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
|
127 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
|
128 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
|
129 { |
|
f4b7ecf8a5f8
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 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
|
131 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
|
132 |
|
f4b7ecf8a5f8
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 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
|
134 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
|
135 |
|
f4b7ecf8a5f8
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 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
|
137 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
|
138 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
|
139 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
|
140 { |
|
78901
6a782496f90a
Issue #15340: Fix importing the random module when /dev/urandom cannot be opened.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75152
diff
changeset
|
141 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
|
142 "/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
|
143 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
|
144 } |
|
f4b7ecf8a5f8
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 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
|
147 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
|
148 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
|
149 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
|
150 } 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 } 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
|
156 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
|
157 |
|
f4b7ecf8a5f8
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 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
|
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 /* 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 "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
|
166 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
|
167 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
|
168 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
|
169 } |
|
f4b7ecf8a5f8
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 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
|
171 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
|
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 #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
|
174 |
|
f4b7ecf8a5f8
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 /* 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
|
176 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
|
177 |
|
f4b7ecf8a5f8
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 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
|
179 |
|
f4b7ecf8a5f8
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 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
|
181 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
|
182 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
|
183 { |
|
f4b7ecf8a5f8
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 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
|
185 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
|
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 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
|
188 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
|
189 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
|
190 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
|
191 /* 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
|
192 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
|
193 } |
|
f4b7ecf8a5f8
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 } |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 /* 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
|
197 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
|
198 |
|
f4b7ecf8a5f8
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 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
|
200 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
|
201 _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
|
202 { |
|
f4b7ecf8a5f8
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 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
|
204 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
|
205 "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
|
206 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
|
207 } |
|
f4b7ecf8a5f8
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 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
|
209 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
|
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 #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
|
212 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
|
213 #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
|
214 # 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
|
215 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
|
216 # 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
|
217 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
|
218 # 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
|
219 #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
|
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 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
|
223 _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
|
224 { |
|
f4b7ecf8a5f8
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 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
|
226 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
|
227 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
|
228 |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
229 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
|
230 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
|
231 _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
|
232 |
|
f4b7ecf8a5f8
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 /* |
|
f4b7ecf8a5f8
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 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
|
235 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
|
236 */ |
|
f4b7ecf8a5f8
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 env = Py_GETENV("PYTHONHASHSEED"); |
| 75087 | 239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 || 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
|
245 || (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
|
246 { |
|
f4b7ecf8a5f8
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 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
|
248 "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
|
249 } |
|
f4b7ecf8a5f8
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 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
|
251 /* 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
|
252 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
|
253 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
254 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
|
255 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
|
256 } |
|
f4b7ecf8a5f8
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 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
258 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
|
259 #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
|
260 (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
|
261 #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
|
262 # 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
|
263 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
|
264 # 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
|
265 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
|
266 # 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
|
267 #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
|
268 } |
|
f4b7ecf8a5f8
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 } |
