blob: 154f6f9684c1ca5af7c861cfebbadbd88e8c66ea [file] [log] [blame]
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001#include "Python.h"
2#ifdef MS_WINDOWS
Victor Stinner59f7fb22015-03-18 14:39:33 +01003# include <windows.h>
Georg Brandl2daf6ae2012-02-20 19:54:16 +01004#else
Victor Stinner59f7fb22015-03-18 14:39:33 +01005# include <fcntl.h>
6# ifdef HAVE_SYS_STAT_H
7# include <sys/stat.h>
8# endif
Victor Stinnerdddf4842016-06-07 11:21:42 +02009# ifdef HAVE_LINUX_RANDOM_H
10# include <linux/random.h>
11# endif
Ned Deily7ae41122016-11-12 16:35:48 -050012# if defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY)
Victor Stinnerbae2d622015-10-01 09:47:30 +020013# include <sys/random.h>
Ned Deily7ae41122016-11-12 16:35:48 -050014# endif
15# if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
Victor Stinner59f7fb22015-03-18 14:39:33 +010016# include <sys/syscall.h>
Victor Stinner59f7fb22015-03-18 14:39:33 +010017# endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010018#endif
19
Benjamin Peterson69e97272012-02-21 11:08:50 -050020#ifdef Py_DEBUG
21int _Py_HashSecret_Initialized = 0;
22#else
23static int _Py_HashSecret_Initialized = 0;
24#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010025
26#ifdef MS_WINDOWS
Georg Brandl2daf6ae2012-02-20 19:54:16 +010027static HCRYPTPROV hCryptProv = 0;
28
29static int
30win32_urandom_init(int raise)
31{
Georg Brandl2daf6ae2012-02-20 19:54:16 +010032 /* Acquire context */
Martin v. Löwis3f50bf62013-01-25 14:06:18 +010033 if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
34 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010035 goto error;
36
37 return 0;
38
39error:
40 if (raise)
41 PyErr_SetFromWindowsErr(0);
42 else
43 Py_FatalError("Failed to initialize Windows random API (CryptoGen)");
44 return -1;
45}
46
47/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
Victor Stinner4d6a3d62014-12-21 01:16:38 +010048 API. Return 0 on success, or raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010049static int
50win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
51{
52 Py_ssize_t chunk;
53
54 if (hCryptProv == 0)
55 {
56 if (win32_urandom_init(raise) == -1)
57 return -1;
58 }
59
60 while (size > 0)
61 {
62 chunk = size > INT_MAX ? INT_MAX : size;
Victor Stinner0c083462013-11-15 23:26:25 +010063 if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010064 {
65 /* CryptGenRandom() failed */
66 if (raise)
67 PyErr_SetFromWindowsErr(0);
68 else
69 Py_FatalError("Failed to initialized the randomized hash "
70 "secret using CryptoGen)");
71 return -1;
72 }
73 buffer += chunk;
74 size -= chunk;
75 }
76 return 0;
77}
Georg Brandl2daf6ae2012-02-20 19:54:16 +010078
Martin Panter39b10252016-06-10 08:07:11 +000079/* Issue #25003: Don't use getentropy() on Solaris (available since
80 * Solaris 11.3), it is blocking whereas os.urandom() should not block. */
Victor Stinnerbae2d622015-10-01 09:47:30 +020081#elif defined(HAVE_GETENTROPY) && !defined(sun)
82#define PY_GETENTROPY 1
83
Victor Stinner4d6a3d62014-12-21 01:16:38 +010084/* Fill buffer with size pseudo-random bytes generated by getentropy().
85 Return 0 on success, or raise an exception and return -1 on error.
Georg Brandl2daf6ae2012-02-20 19:54:16 +010086
Victor Stinner4d6a3d62014-12-21 01:16:38 +010087 If fatal is nonzero, call Py_FatalError() instead of raising an exception
88 on error. */
89static int
90py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
91{
92 while (size > 0) {
93 Py_ssize_t len = Py_MIN(size, 256);
Victor Stinner9aa13312015-03-30 11:18:30 +020094 int res;
95
96 if (!fatal) {
97 Py_BEGIN_ALLOW_THREADS
98 res = getentropy(buffer, len);
99 Py_END_ALLOW_THREADS
100
101 if (res < 0) {
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100102 PyErr_SetFromErrno(PyExc_OSError);
103 return -1;
104 }
105 }
Victor Stinner9aa13312015-03-30 11:18:30 +0200106 else {
107 res = getentropy(buffer, len);
108 if (res < 0)
109 Py_FatalError("getentropy() failed");
110 }
111
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100112 buffer += len;
113 size -= len;
114 }
115 return 0;
116}
117
Victor Stinnerbae2d622015-10-01 09:47:30 +0200118#else
Victor Stinner59f7fb22015-03-18 14:39:33 +0100119
Victor Stinnerbae2d622015-10-01 09:47:30 +0200120#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
121#define PY_GETRANDOM 1
122
Victor Stinneraf597322016-09-20 22:26:18 +0200123/* Call getrandom()
124 - Return 1 on success
Victor Stinner6d8bc462016-09-20 22:46:02 +0200125 - Return 0 if getrandom() syscall is not available (failed with ENOSYS or
126 EPERM) or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom
Victor Stinneraf597322016-09-20 22:26:18 +0200127 not initialized yet) and raise=0.
128 - Raise an exception (if raise is non-zero) and return -1 on error:
129 getrandom() failed with EINTR and the Python signal handler raised an
130 exception, or getrandom() failed with a different error. */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100131static int
132py_getrandom(void *buffer, Py_ssize_t size, int raise)
133{
Victor Stinneraf597322016-09-20 22:26:18 +0200134 /* Is getrandom() supported by the running kernel? Set to 0 if getrandom()
Victor Stinner6d8bc462016-09-20 22:46:02 +0200135 failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris
Victor Stinneraf597322016-09-20 22:26:18 +0200136 11.3 or newer */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100137 static int getrandom_works = 1;
Victor Stinnerdddf4842016-06-07 11:21:42 +0200138
139 /* getrandom() on Linux will block if called before the kernel has
140 * initialized the urandom entropy pool. This will cause Python
141 * to hang on startup if called very early in the boot process -
142 * see https://bugs.python.org/issue26839. To avoid this, use the
143 * GRND_NONBLOCK flag. */
144 const int flags = GRND_NONBLOCK;
Victor Stinnerec721f32016-06-16 23:53:47 +0200145 long n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100146
Victor Stinneraf597322016-09-20 22:26:18 +0200147 if (!getrandom_works) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100148 return 0;
Victor Stinneraf597322016-09-20 22:26:18 +0200149 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100150
151 while (0 < size) {
Victor Stinner9d242712016-04-12 22:28:49 +0200152#ifdef sun
153 /* Issue #26735: On Solaris, getrandom() is limited to returning up
154 to 1024 bytes */
155 n = Py_MIN(size, 1024);
156#else
Victor Stinnerec721f32016-06-16 23:53:47 +0200157 n = Py_MIN(size, LONG_MAX);
Victor Stinner9d242712016-04-12 22:28:49 +0200158#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200159
Victor Stinner9d242712016-04-12 22:28:49 +0200160 errno = 0;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200161#ifdef HAVE_GETRANDOM
162 if (raise) {
163 Py_BEGIN_ALLOW_THREADS
Victor Stinner9d242712016-04-12 22:28:49 +0200164 n = getrandom(buffer, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200165 Py_END_ALLOW_THREADS
166 }
167 else {
Victor Stinner9d242712016-04-12 22:28:49 +0200168 n = getrandom(buffer, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200169 }
170#else
171 /* On Linux, use the syscall() function because the GNU libc doesn't
172 * expose the Linux getrandom() syscall yet. See:
Victor Stinner59f7fb22015-03-18 14:39:33 +0100173 * https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
Victor Stinner79b74ae2015-03-30 11:16:40 +0200174 if (raise) {
175 Py_BEGIN_ALLOW_THREADS
Victor Stinner9d242712016-04-12 22:28:49 +0200176 n = syscall(SYS_getrandom, buffer, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200177 Py_END_ALLOW_THREADS
178 }
179 else {
Victor Stinner9d242712016-04-12 22:28:49 +0200180 n = syscall(SYS_getrandom, buffer, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200181 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200182#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200183
Victor Stinner59f7fb22015-03-18 14:39:33 +0100184 if (n < 0) {
Victor Stinneraf597322016-09-20 22:26:18 +0200185 /* ENOSYS: getrandom() syscall not supported by the kernel (but
Victor Stinner6d8bc462016-09-20 22:46:02 +0200186 * maybe supported by the host which built Python). EPERM:
187 * getrandom() syscall blocked by SECCOMP or something else. */
188 if (errno == ENOSYS || errno == EPERM) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100189 getrandom_works = 0;
190 return 0;
191 }
Victor Stinnerdddf4842016-06-07 11:21:42 +0200192 if (errno == EAGAIN) {
Victor Stinneraf597322016-09-20 22:26:18 +0200193 /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system
194 urandom is not initialiazed yet. In this case, fall back on
195 reading from /dev/urandom.
196
197 Note: In this case the data read will not be random so
198 should not be used for cryptographic purposes. Retaining
199 the existing semantics for practical purposes. */
Victor Stinnerdddf4842016-06-07 11:21:42 +0200200 getrandom_works = 0;
201 return 0;
202 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100203
204 if (errno == EINTR) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100205 if (PyErr_CheckSignals()) {
Victor Stinneraf597322016-09-20 22:26:18 +0200206 if (!raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100207 Py_FatalError("getrandom() interrupted by a signal");
Victor Stinneraf597322016-09-20 22:26:18 +0200208 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100209 return -1;
210 }
Victor Stinneraf597322016-09-20 22:26:18 +0200211
Victor Stinner59f7fb22015-03-18 14:39:33 +0100212 /* retry getrandom() */
213 continue;
214 }
215
Victor Stinneraf597322016-09-20 22:26:18 +0200216 if (raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100217 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinneraf597322016-09-20 22:26:18 +0200218 }
219 else {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100220 Py_FatalError("getrandom() failed");
Victor Stinneraf597322016-09-20 22:26:18 +0200221 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100222 return -1;
223 }
224
225 buffer += n;
226 size -= n;
227 }
228 return 1;
229}
230#endif
231
Antoine Pitroue472aea2014-04-26 14:33:03 +0200232static struct {
233 int fd;
234 dev_t st_dev;
235 ino_t st_ino;
236} urandom_cache = { -1 };
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100237
Victor Stinner59f7fb22015-03-18 14:39:33 +0100238
Victor Stinneraf597322016-09-20 22:26:18 +0200239/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
240 /dev/urandom if getrandom() is not available.
241
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100242 Call Py_FatalError() on error. */
243static void
Christian Heimes985ecdc2013-11-20 11:46:18 +0100244dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100245{
246 int fd;
247 Py_ssize_t n;
248
249 assert (0 < size);
250
Victor Stinnerbae2d622015-10-01 09:47:30 +0200251#ifdef PY_GETRANDOM
Victor Stinneraf597322016-09-20 22:26:18 +0200252 if (py_getrandom(buffer, size, 0) == 1) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100253 return;
Victor Stinneraf597322016-09-20 22:26:18 +0200254 }
Victor Stinner6d8bc462016-09-20 22:46:02 +0200255 /* getrandom() failed with ENOSYS or EPERM,
Victor Stinneraf597322016-09-20 22:26:18 +0200256 fall back on reading /dev/urandom */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100257#endif
258
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100259 fd = _Py_open_noraise("/dev/urandom", O_RDONLY);
Victor Stinneraf597322016-09-20 22:26:18 +0200260 if (fd < 0) {
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100261 Py_FatalError("Failed to open /dev/urandom");
Victor Stinneraf597322016-09-20 22:26:18 +0200262 }
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100263
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100264 while (0 < size)
265 {
266 do {
267 n = read(fd, buffer, (size_t)size);
268 } while (n < 0 && errno == EINTR);
Victor Stinneraf597322016-09-20 22:26:18 +0200269
270 if (n <= 0) {
271 /* read() failed or returned 0 bytes */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100272 Py_FatalError("Failed to read bytes from /dev/urandom");
273 break;
274 }
275 buffer += n;
Victor Stinnerc72828b2016-06-14 16:35:49 +0200276 size -= n;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100277 }
278 close(fd);
279}
280
Victor Stinneraf597322016-09-20 22:26:18 +0200281/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
282 /dev/urandom if getrandom() is not available.
283
284 Return 0 on success. Raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100285static int
286dev_urandom_python(char *buffer, Py_ssize_t size)
287{
288 int fd;
289 Py_ssize_t n;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800290 struct _Py_stat_struct st;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200291#ifdef PY_GETRANDOM
Victor Stinner59f7fb22015-03-18 14:39:33 +0100292 int res;
293#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100294
295 if (size <= 0)
296 return 0;
297
Victor Stinnerbae2d622015-10-01 09:47:30 +0200298#ifdef PY_GETRANDOM
Victor Stinner59f7fb22015-03-18 14:39:33 +0100299 res = py_getrandom(buffer, size, 1);
Victor Stinneraf597322016-09-20 22:26:18 +0200300 if (res < 0) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100301 return -1;
Victor Stinneraf597322016-09-20 22:26:18 +0200302 }
303 if (res == 1) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100304 return 0;
Victor Stinneraf597322016-09-20 22:26:18 +0200305 }
Victor Stinner6d8bc462016-09-20 22:46:02 +0200306 /* getrandom() failed with ENOSYS or EPERM,
Victor Stinneraf597322016-09-20 22:26:18 +0200307 fall back on reading /dev/urandom */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100308#endif
309
Antoine Pitroue472aea2014-04-26 14:33:03 +0200310 if (urandom_cache.fd >= 0) {
311 /* Does the fd point to the same thing as before? (issue #21207) */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200312 if (_Py_fstat_noraise(urandom_cache.fd, &st)
Antoine Pitroue472aea2014-04-26 14:33:03 +0200313 || st.st_dev != urandom_cache.st_dev
314 || st.st_ino != urandom_cache.st_ino) {
315 /* Something changed: forget the cached fd (but don't close it,
316 since it probably points to something important for some
317 third-party code). */
318 urandom_cache.fd = -1;
319 }
320 }
321 if (urandom_cache.fd >= 0)
322 fd = urandom_cache.fd;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200323 else {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200324 fd = _Py_open("/dev/urandom", O_RDONLY);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100325 if (fd < 0) {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200326 if (errno == ENOENT || errno == ENXIO ||
327 errno == ENODEV || errno == EACCES)
328 PyErr_SetString(PyExc_NotImplementedError,
329 "/dev/urandom (or equivalent) not found");
Victor Stinnera555cfc2015-03-18 00:22:14 +0100330 /* otherwise, keep the OSError exception raised by _Py_open() */
Antoine Pitrou4879a962013-08-31 00:26:02 +0200331 return -1;
332 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200333 if (urandom_cache.fd >= 0) {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200334 /* urandom_fd was initialized by another thread while we were
335 not holding the GIL, keep it. */
336 close(fd);
Antoine Pitroue472aea2014-04-26 14:33:03 +0200337 fd = urandom_cache.fd;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200338 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200339 else {
Steve Dowerf2f373f2015-02-21 08:44:05 -0800340 if (_Py_fstat(fd, &st)) {
Antoine Pitroue472aea2014-04-26 14:33:03 +0200341 close(fd);
342 return -1;
343 }
344 else {
345 urandom_cache.fd = fd;
346 urandom_cache.st_dev = st.st_dev;
347 urandom_cache.st_ino = st.st_ino;
348 }
349 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100350 }
351
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100352 do {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100353 n = _Py_read(fd, buffer, (size_t)size);
Victor Stinneraf597322016-09-20 22:26:18 +0200354 if (n == -1) {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100355 return -1;
Victor Stinneraf597322016-09-20 22:26:18 +0200356 }
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100357 if (n == 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100358 PyErr_Format(PyExc_RuntimeError,
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100359 "Failed to read %zi bytes from /dev/urandom",
360 size);
361 return -1;
362 }
363
364 buffer += n;
365 size -= n;
366 } while (0 < size);
367
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100368 return 0;
369}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200370
371static void
372dev_urandom_close(void)
373{
Antoine Pitroue472aea2014-04-26 14:33:03 +0200374 if (urandom_cache.fd >= 0) {
375 close(urandom_cache.fd);
376 urandom_cache.fd = -1;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200377 }
378}
379
Victor Stinnerbae2d622015-10-01 09:47:30 +0200380#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100381
382/* Fill buffer with pseudo-random bytes generated by a linear congruent
383 generator (LCG):
384
385 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
386
387 Use bits 23..16 of x(n) to generate a byte. */
388static void
389lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
390{
391 size_t index;
392 unsigned int x;
393
394 x = x0;
395 for (index=0; index < size; index++) {
396 x *= 214013;
397 x += 2531011;
398 /* modulo 2 ^ (8 * sizeof(int)) */
399 buffer[index] = (x >> 16) & 0xff;
400 }
401}
402
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200403/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka56a6d852014-12-01 18:28:43 +0200404 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200405 except long living private keys for asymmetric encryption.
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100406
407 Return 0 on success, raise an exception and return -1 on error. */
408int
409_PyOS_URandom(void *buffer, Py_ssize_t size)
410{
411 if (size < 0) {
412 PyErr_Format(PyExc_ValueError,
413 "negative argument not allowed");
414 return -1;
415 }
416 if (size == 0)
417 return 0;
418
419#ifdef MS_WINDOWS
420 return win32_urandom((unsigned char *)buffer, size, 1);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200421#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100422 return py_getentropy(buffer, size, 0);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100423#else
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100424 return dev_urandom_python((char*)buffer, size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100425#endif
426}
427
428void
429_PyRandom_Init(void)
430{
431 char *env;
Christian Heimes985ecdc2013-11-20 11:46:18 +0100432 unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500433 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Christian Heimes985ecdc2013-11-20 11:46:18 +0100434 assert(secret_size == sizeof(_Py_HashSecret.uc));
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100435
Benjamin Peterson69e97272012-02-21 11:08:50 -0500436 if (_Py_HashSecret_Initialized)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100437 return;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500438 _Py_HashSecret_Initialized = 1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100439
440 /*
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100441 Hash randomization is enabled. Generate a per-process secret,
442 using PYTHONHASHSEED if provided.
443 */
444
445 env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl12897d72012-02-20 23:49:29 +0100446 if (env && *env != '\0' && strcmp(env, "random") != 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100447 char *endptr = env;
448 unsigned long seed;
449 seed = strtoul(env, &endptr, 10);
450 if (*endptr != '\0'
451 || seed > 4294967295UL
452 || (errno == ERANGE && seed == ULONG_MAX))
453 {
454 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
455 "in range [0; 4294967295]");
456 }
457 if (seed == 0) {
458 /* disable the randomized hash */
459 memset(secret, 0, secret_size);
460 }
461 else {
Christian Heimes985ecdc2013-11-20 11:46:18 +0100462 lcg_urandom(seed, secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100463 }
464 }
465 else {
466#ifdef MS_WINDOWS
Christian Heimes985ecdc2013-11-20 11:46:18 +0100467 (void)win32_urandom(secret, secret_size, 0);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200468#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100469 (void)py_getentropy(secret, secret_size, 1);
Christian Heimesaf01f662013-12-21 16:19:10 +0100470#else
Christian Heimes985ecdc2013-11-20 11:46:18 +0100471 dev_urandom_noraise(secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100472#endif
473 }
474}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200475
476void
477_PyRandom_Fini(void)
478{
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200479#ifdef MS_WINDOWS
480 if (hCryptProv) {
Tim Goldenb8ac3e12014-05-06 13:29:45 +0100481 CryptReleaseContext(hCryptProv, 0);
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200482 hCryptProv = 0;
483 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200484#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100485 /* nothing to clean */
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200486#else
Antoine Pitrou4879a962013-08-31 00:26:02 +0200487 dev_urandom_close();
488#endif
489}