Mercurial > cpython-lru_cache-weakref
annotate Python/random.c @ 85420:ef889c3d5dc6
Issue #18571: Implementation of the PEP 446: file descriptors and file handles
are now created non-inheritable; add functions os.get/set_inheritable(),
os.get/set_handle_inheritable() and socket.socket.get/set_inheritable().
| author | Victor Stinner <victor.stinner@gmail.com> |
|---|---|
| date | Wed, 28 Aug 2013 00:53:59 +0200 |
| parents | fe949918616c |
| children | acc7439b1406 |
| 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 |
|
85420
ef889c3d5dc6
Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents:
85199
diff
changeset
|
104 fd = _Py_open("/dev/urandom", O_RDONLY); |
|
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
|
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 |
|
85420
ef889c3d5dc6
Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents:
85199
diff
changeset
|
137 fd = _Py_open("/dev/urandom", O_RDONLY); |
|
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
|
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 { |
|
85198
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
141 if (errno == ENOENT || errno == ENXIO || |
|
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
142 errno == ENODEV || errno == EACCES) |
|
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
143 PyErr_SetString(PyExc_NotImplementedError, |
|
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
144 "/dev/urandom (or equivalent) not found"); |
|
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
145 else |
|
193bcc12575d
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
78921
diff
changeset
|
146 PyErr_SetFromErrno(PyExc_OSError); |
|
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
|
147 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
|
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 |
|
f4b7ecf8a5f8
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 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
|
151 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
|
152 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
|
153 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
|
154 } 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 } 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
|
160 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
|
161 |
|
f4b7ecf8a5f8
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 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
|
163 { |
|
f4b7ecf8a5f8
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 /* 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 "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
|
170 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
|
171 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
|
172 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
|
173 } |
|
f4b7ecf8a5f8
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 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
|
175 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
|
176 } |
|
f4b7ecf8a5f8
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 #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
|
178 |
|
f4b7ecf8a5f8
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 /* 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
|
180 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
|
181 |
|
f4b7ecf8a5f8
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 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
|
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 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
|
185 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
|
186 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
|
187 { |
|
f4b7ecf8a5f8
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 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
|
189 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
|
190 |
|
f4b7ecf8a5f8
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 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
|
192 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
|
193 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
|
194 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
|
195 /* 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
|
196 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
|
197 } |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 /* 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
|
201 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
|
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 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
|
204 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
|
205 _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
|
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 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
|
208 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
|
209 "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
|
210 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
|
211 } |
|
f4b7ecf8a5f8
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 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
|
213 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
|
214 |
|
f4b7ecf8a5f8
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 #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
|
216 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
|
217 #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
|
218 # 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
|
219 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
|
220 # 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
|
221 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
|
222 # 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
|
223 #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
|
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 |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 _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
|
228 { |
|
f4b7ecf8a5f8
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 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
|
230 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
|
231 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
|
232 |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
233 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
|
234 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
|
235 _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
|
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 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
|
239 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
|
240 */ |
|
f4b7ecf8a5f8
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 |
|
f4b7ecf8a5f8
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 env = Py_GETENV("PYTHONHASHSEED"); |
| 75087 | 243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 || 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
|
249 || (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
|
250 { |
|
f4b7ecf8a5f8
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 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
|
252 "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
|
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 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
|
255 /* 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
|
256 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
|
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 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
|
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 } |
|
f4b7ecf8a5f8
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 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
|
263 #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
|
264 (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
|
265 #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
|
266 # 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
|
267 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
|
268 # 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
|
269 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
|
270 # 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
|
271 #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
|
272 } |
|
f4b7ecf8a5f8
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 } |
