Mercurial > cpython-lru_cache-weakref
annotate Python/random.c @ 86823:17d730d37b2f
Issue #19410: Put back in special-casing of '' for
importlib.machinery.FileFinder.
While originally moved to stop special-casing '' as PathFinder farther
up the typical call chain now uses the cwd in the instance of '', it
was deemed an unnecessary risk to breaking subclasses of FileFinder to
take the special-casing out.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Fri, 01 Nov 2013 10:37:57 -0400 |
| parents | b0295cc736d8 |
| children | baab11a466ab |
| 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) |
|
85468
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
93 static int urandom_fd = -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
|
94 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
95 /* 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
|
96 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
|
97 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
|
98 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
|
99 { |
|
f4b7ecf8a5f8
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 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
|
101 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
|
102 |
|
f4b7ecf8a5f8
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 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
|
104 |
|
85420
ef889c3d5dc6
Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents:
85199
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 |
|
f4b7ecf8a5f8
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 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
|
110 { |
|
f4b7ecf8a5f8
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 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
|
112 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
|
113 } 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
|
114 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
|
115 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 /* 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
|
117 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
|
118 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
|
119 } |
|
f4b7ecf8a5f8
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 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
|
121 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
|
122 } |
|
f4b7ecf8a5f8
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 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
|
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 |
|
f4b7ecf8a5f8
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 /* 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
|
127 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
|
128 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
|
129 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
|
130 { |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 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
|
132 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
|
133 |
|
f4b7ecf8a5f8
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 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
|
135 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
|
136 |
|
85468
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
137 if (urandom_fd >= 0) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
138 fd = urandom_fd; |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
139 else { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
140 Py_BEGIN_ALLOW_THREADS |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
141 fd = _Py_open("/dev/urandom", O_RDONLY); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
142 Py_END_ALLOW_THREADS |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
143 if (fd < 0) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
144 { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
145 if (errno == ENOENT || errno == ENXIO || |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
146 errno == ENODEV || errno == EACCES) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
147 PyErr_SetString(PyExc_NotImplementedError, |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
148 "/dev/urandom (or equivalent) not found"); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
149 else |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
150 PyErr_SetFromErrno(PyExc_OSError); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
151 return -1; |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
152 } |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
153 if (urandom_fd >= 0) { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
154 /* urandom_fd was initialized by another thread while we were |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
155 not holding the GIL, keep it. */ |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
156 close(fd); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
157 fd = urandom_fd; |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
158 } |
|
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
|
159 else |
|
85468
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
160 urandom_fd = fd; |
|
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
|
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 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 Py_BEGIN_ALLOW_THREADS |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
164 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
|
165 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
|
166 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
|
167 } 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 } 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
|
173 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
|
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 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
|
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 /* 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
|
178 if (n < 0) |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 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
|
180 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
|
181 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
|
182 "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
|
183 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
|
184 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
|
185 } |
|
f4b7ecf8a5f8
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 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
|
187 } |
|
85468
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
188 |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
189 static void |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
190 dev_urandom_close(void) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
191 { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
192 if (urandom_fd >= 0) { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
193 close(urandom_fd); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
194 urandom_fd = -1; |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
195 } |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
196 } |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
197 |
|
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
|
198 #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
|
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 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
|
201 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
|
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 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
|
204 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 { |
|
f4b7ecf8a5f8
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 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
|
210 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
|
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 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
|
213 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
|
214 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
|
215 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
|
216 /* 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
|
217 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
|
218 } |
|
f4b7ecf8a5f8
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 } |
|
f4b7ecf8a5f8
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 |
|
86107
176bb5a98463
Closes #15213: update comment for _PyOS_URandom
Georg Brandl <georg@python.org>
parents:
85198
diff
changeset
|
221 /* Fill buffer with size pseudo-random bytes from the operating system random |
|
176bb5a98463
Closes #15213: update comment for _PyOS_URandom
Georg Brandl <georg@python.org>
parents:
85198
diff
changeset
|
222 number generator (RNG). It is suitable for for most cryptographic purposes |
|
176bb5a98463
Closes #15213: update comment for _PyOS_URandom
Georg Brandl <georg@python.org>
parents:
85198
diff
changeset
|
223 except long living private keys for asymmetric encryption. |
|
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
|
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 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
|
226 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
|
227 _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
|
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 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
|
230 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
|
231 "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
|
232 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
|
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 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
|
235 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
|
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 #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
|
238 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
|
239 #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
|
240 # 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
|
241 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
|
242 # 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
|
243 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
|
244 # 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
|
245 #endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
246 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
247 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
248 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
|
249 _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
|
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 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
|
252 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
|
253 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
|
254 |
|
75127
160a56eac332
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
75087
diff
changeset
|
255 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
|
256 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
|
257 _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
|
258 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
259 /* |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
260 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
|
261 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
|
262 */ |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
263 |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
264 env = Py_GETENV("PYTHONHASHSEED"); |
| 75087 | 265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 || 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
|
271 || (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
|
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 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
|
274 "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
|
275 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 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
|
277 /* 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
|
278 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
|
279 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 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
|
281 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
|
282 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 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
|
285 #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
|
286 (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
|
287 #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
|
288 # 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
|
289 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
|
290 # 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
|
291 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
|
292 # 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
|
293 #endif |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 } |
|
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 } |
|
85468
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
296 |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
297 void |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
298 _PyRandom_Fini(void) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
299 { |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
300 #if !defined(MS_WINDOWS) && !defined(__VMS) |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
301 dev_urandom_close(); |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
302 #endif |
|
acc7439b1406
Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85420
diff
changeset
|
303 } |
