blob: ea506eeeb236e2b9aceb5604efa58d84eeb448d1 [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>
Martin Panterd2f87472016-07-29 04:00:44 +00004/* All sample MSDN wincrypt programs include the header below. It is at least
5 * required with Min GW. */
6# include <wincrypt.h>
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007#else
Victor Stinner59f7fb22015-03-18 14:39:33 +01008# include <fcntl.h>
9# ifdef HAVE_SYS_STAT_H
10# include <sys/stat.h>
11# endif
Victor Stinnerdddf4842016-06-07 11:21:42 +020012# ifdef HAVE_LINUX_RANDOM_H
13# include <linux/random.h>
14# endif
Victor Stinnerbae2d622015-10-01 09:47:30 +020015# ifdef HAVE_GETRANDOM
16# include <sys/random.h>
17# elif defined(HAVE_GETRANDOM_SYSCALL)
Victor Stinner59f7fb22015-03-18 14:39:33 +010018# include <sys/syscall.h>
Victor Stinner59f7fb22015-03-18 14:39:33 +010019# endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010020#endif
21
Benjamin Peterson69e97272012-02-21 11:08:50 -050022#ifdef Py_DEBUG
23int _Py_HashSecret_Initialized = 0;
24#else
25static int _Py_HashSecret_Initialized = 0;
26#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010027
28#ifdef MS_WINDOWS
Georg Brandl2daf6ae2012-02-20 19:54:16 +010029static HCRYPTPROV hCryptProv = 0;
30
31static int
32win32_urandom_init(int raise)
33{
Georg Brandl2daf6ae2012-02-20 19:54:16 +010034 /* Acquire context */
Martin v. Löwis3f50bf62013-01-25 14:06:18 +010035 if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
36 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010037 goto error;
38
39 return 0;
40
41error:
Victor Stinner4bad3b62016-08-16 15:23:58 +020042 if (raise) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010043 PyErr_SetFromWindowsErr(0);
Victor Stinner4bad3b62016-08-16 15:23:58 +020044 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010045 return -1;
46}
47
48/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
Victor Stinner4d6a3d62014-12-21 01:16:38 +010049 API. Return 0 on success, or raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010050static int
51win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
52{
53 Py_ssize_t chunk;
54
55 if (hCryptProv == 0)
56 {
Victor Stinner4bad3b62016-08-16 15:23:58 +020057 if (win32_urandom_init(raise) == -1) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010058 return -1;
Victor Stinner4bad3b62016-08-16 15:23:58 +020059 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010060 }
61
62 while (size > 0)
63 {
64 chunk = size > INT_MAX ? INT_MAX : size;
Victor Stinner0c083462013-11-15 23:26:25 +010065 if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010066 {
67 /* CryptGenRandom() failed */
Victor Stinner4bad3b62016-08-16 15:23:58 +020068 if (raise) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010069 PyErr_SetFromWindowsErr(0);
Victor Stinner4bad3b62016-08-16 15:23:58 +020070 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010071 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
Victor Stinnere66987e2016-09-06 16:33:52 -070080 * 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 Stinner4bad3b62016-08-16 15:23:58 +020087 If raise is zero, don't raise an exception on error. */
Victor Stinner4d6a3d62014-12-21 01:16:38 +010088static int
Victor Stinner4bad3b62016-08-16 15:23:58 +020089py_getentropy(char *buffer, Py_ssize_t size, int raise)
Victor Stinner4d6a3d62014-12-21 01:16:38 +010090{
91 while (size > 0) {
92 Py_ssize_t len = Py_MIN(size, 256);
Victor Stinner9aa13312015-03-30 11:18:30 +020093 int res;
94
Victor Stinner4bad3b62016-08-16 15:23:58 +020095 if (raise) {
Victor Stinner9aa13312015-03-30 11:18:30 +020096 Py_BEGIN_ALLOW_THREADS
97 res = getentropy(buffer, len);
98 Py_END_ALLOW_THREADS
Victor Stinner4d6a3d62014-12-21 01:16:38 +010099 }
Victor Stinner9aa13312015-03-30 11:18:30 +0200100 else {
101 res = getentropy(buffer, len);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200102 }
103
104 if (res < 0) {
105 if (raise) {
106 PyErr_SetFromErrno(PyExc_OSError);
107 }
108 return -1;
Victor Stinner9aa13312015-03-30 11:18:30 +0200109 }
110
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100111 buffer += len;
112 size -= len;
113 }
114 return 0;
115}
116
Victor Stinnerbae2d622015-10-01 09:47:30 +0200117#else
Victor Stinner59f7fb22015-03-18 14:39:33 +0100118
Victor Stinnerbae2d622015-10-01 09:47:30 +0200119#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
120#define PY_GETRANDOM 1
121
Victor Stinner6974cf22016-08-16 18:46:38 +0200122/* Call getrandom()
123 - Return 1 on success
Victor Stinnere66987e2016-09-06 16:33:52 -0700124 - Return 0 if getrandom() syscall is not available (fails with ENOSYS)
125 or if getrandom(GRND_NONBLOCK) fails with EAGAIN (blocking=0 and system
126 urandom not initialized yet) and raise=0.
Victor Stinner6974cf22016-08-16 18:46:38 +0200127 - Raise an exception (if raise is non-zero) and return -1 on error:
128 getrandom() failed with EINTR and the Python signal handler raised an
129 exception, or getrandom() failed with a different error. */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100130static int
Victor Stinnere66987e2016-09-06 16:33:52 -0700131py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
Victor Stinner59f7fb22015-03-18 14:39:33 +0100132{
Victor Stinnere66987e2016-09-06 16:33:52 -0700133 /* Is getrandom() supported by the running kernel? Set to 0 if getrandom()
134 fails with ENOSYS. Need Linux kernel 3.17 or newer, or Solaris 11.3
135 or newer */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100136 static int getrandom_works = 1;
Victor Stinnere66987e2016-09-06 16:33:52 -0700137 int flags;
Victor Stinnercfb19612016-06-08 10:16:50 +0200138 char *dest;
Victor Stinnerec721f32016-06-16 23:53:47 +0200139 long n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100140
Victor Stinner6974cf22016-08-16 18:46:38 +0200141 if (!getrandom_works) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100142 return 0;
Victor Stinner6974cf22016-08-16 18:46:38 +0200143 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100144
Victor Stinnere66987e2016-09-06 16:33:52 -0700145 flags = blocking ? 0 : GRND_NONBLOCK;
Victor Stinnercfb19612016-06-08 10:16:50 +0200146 dest = buffer;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100147 while (0 < size) {
Victor Stinner9d242712016-04-12 22:28:49 +0200148#ifdef sun
149 /* Issue #26735: On Solaris, getrandom() is limited to returning up
150 to 1024 bytes */
151 n = Py_MIN(size, 1024);
152#else
Victor Stinnerec721f32016-06-16 23:53:47 +0200153 n = Py_MIN(size, LONG_MAX);
Victor Stinner9d242712016-04-12 22:28:49 +0200154#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200155
Victor Stinner9d242712016-04-12 22:28:49 +0200156 errno = 0;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200157#ifdef HAVE_GETRANDOM
158 if (raise) {
159 Py_BEGIN_ALLOW_THREADS
Victor Stinnercfb19612016-06-08 10:16:50 +0200160 n = getrandom(dest, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200161 Py_END_ALLOW_THREADS
162 }
163 else {
Victor Stinnercfb19612016-06-08 10:16:50 +0200164 n = getrandom(dest, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200165 }
166#else
167 /* On Linux, use the syscall() function because the GNU libc doesn't
Victor Stinner6974cf22016-08-16 18:46:38 +0200168 expose the Linux getrandom() syscall yet. See:
169 https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
Victor Stinner79b74ae2015-03-30 11:16:40 +0200170 if (raise) {
171 Py_BEGIN_ALLOW_THREADS
Victor Stinnercfb19612016-06-08 10:16:50 +0200172 n = syscall(SYS_getrandom, dest, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200173 Py_END_ALLOW_THREADS
174 }
175 else {
Victor Stinnercfb19612016-06-08 10:16:50 +0200176 n = syscall(SYS_getrandom, dest, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200177 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200178#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200179
Victor Stinner59f7fb22015-03-18 14:39:33 +0100180 if (n < 0) {
181 if (errno == ENOSYS) {
182 getrandom_works = 0;
183 return 0;
184 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200185
Victor Stinnere66987e2016-09-06 16:33:52 -0700186 /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system urandom
187 is not initialiazed yet. For _PyRandom_Init(), we ignore their
188 error and fall back on reading /dev/urandom which never blocks,
189 even if the system urandom is not initialized yet. */
190 if (errno == EAGAIN && !raise && !blocking) {
Victor Stinnerdddf4842016-06-07 11:21:42 +0200191 return 0;
192 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100193
194 if (errno == EINTR) {
Victor Stinnercecdd962016-08-16 15:19:09 +0200195 if (raise) {
196 if (PyErr_CheckSignals()) {
197 return -1;
198 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100199 }
Victor Stinnercecdd962016-08-16 15:19:09 +0200200
201 /* retry getrandom() if it was interrupted by a signal */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100202 continue;
203 }
204
Victor Stinner4bad3b62016-08-16 15:23:58 +0200205 if (raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100206 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200207 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100208 return -1;
209 }
210
Victor Stinnercfb19612016-06-08 10:16:50 +0200211 dest += n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100212 size -= n;
213 }
214 return 1;
215}
216#endif
217
Antoine Pitroue472aea2014-04-26 14:33:03 +0200218static struct {
219 int fd;
220 dev_t st_dev;
221 ino_t st_ino;
222} urandom_cache = { -1 };
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100223
Victor Stinner59f7fb22015-03-18 14:39:33 +0100224
Victor Stinnere66987e2016-09-06 16:33:52 -0700225/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
Victor Stinner6974cf22016-08-16 18:46:38 +0200226 /dev/urandom if getrandom() is not available.
227
228 Return 0 on success. Raise an exception (if raise is non-zero) and return -1
229 on error. */
Victor Stinner4bad3b62016-08-16 15:23:58 +0200230static int
Victor Stinnere66987e2016-09-06 16:33:52 -0700231dev_urandom(char *buffer, Py_ssize_t size, int blocking, int raise)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100232{
233 int fd;
234 Py_ssize_t n;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200235#ifdef PY_GETRANDOM
Victor Stinner59f7fb22015-03-18 14:39:33 +0100236 int res;
237#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100238
Victor Stinner6974cf22016-08-16 18:46:38 +0200239 assert(size > 0);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100240
Victor Stinnerbae2d622015-10-01 09:47:30 +0200241#ifdef PY_GETRANDOM
Victor Stinnere66987e2016-09-06 16:33:52 -0700242 res = py_getrandom(buffer, size, blocking, raise);
Victor Stinner6974cf22016-08-16 18:46:38 +0200243 if (res < 0) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100244 return -1;
Victor Stinner6974cf22016-08-16 18:46:38 +0200245 }
246 if (res == 1) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100247 return 0;
Victor Stinner6974cf22016-08-16 18:46:38 +0200248 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100249 /* getrandom() is not supported by the running kernel, fall back
Victor Stinner6974cf22016-08-16 18:46:38 +0200250 on reading /dev/urandom */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100251#endif
252
Victor Stinner6974cf22016-08-16 18:46:38 +0200253
254 if (raise) {
255 struct _Py_stat_struct st;
256
Antoine Pitroue472aea2014-04-26 14:33:03 +0200257 if (urandom_cache.fd >= 0) {
Victor Stinner6974cf22016-08-16 18:46:38 +0200258 /* Does the fd point to the same thing as before? (issue #21207) */
259 if (_Py_fstat_noraise(urandom_cache.fd, &st)
260 || st.st_dev != urandom_cache.st_dev
261 || st.st_ino != urandom_cache.st_ino) {
262 /* Something changed: forget the cached fd (but don't close it,
263 since it probably points to something important for some
264 third-party code). */
265 urandom_cache.fd = -1;
266 }
Antoine Pitrou4879a962013-08-31 00:26:02 +0200267 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200268 if (urandom_cache.fd >= 0)
269 fd = urandom_cache.fd;
Antoine Pitroue472aea2014-04-26 14:33:03 +0200270 else {
Victor Stinner6974cf22016-08-16 18:46:38 +0200271 fd = _Py_open("/dev/urandom", O_RDONLY);
272 if (fd < 0) {
273 if (errno == ENOENT || errno == ENXIO ||
274 errno == ENODEV || errno == EACCES)
275 PyErr_SetString(PyExc_NotImplementedError,
276 "/dev/urandom (or equivalent) not found");
277 /* otherwise, keep the OSError exception raised by _Py_open() */
Antoine Pitroue472aea2014-04-26 14:33:03 +0200278 return -1;
279 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200280 if (urandom_cache.fd >= 0) {
281 /* urandom_fd was initialized by another thread while we were
282 not holding the GIL, keep it. */
283 close(fd);
284 fd = urandom_cache.fd;
285 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200286 else {
Victor Stinner6974cf22016-08-16 18:46:38 +0200287 if (_Py_fstat(fd, &st)) {
288 close(fd);
289 return -1;
290 }
291 else {
292 urandom_cache.fd = fd;
293 urandom_cache.st_dev = st.st_dev;
294 urandom_cache.st_ino = st.st_ino;
295 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200296 }
297 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100298
Victor Stinner6974cf22016-08-16 18:46:38 +0200299 do {
300 n = _Py_read(fd, buffer, (size_t)size);
301 if (n == -1)
302 return -1;
303 if (n == 0) {
304 PyErr_Format(PyExc_RuntimeError,
305 "Failed to read %zi bytes from /dev/urandom",
306 size);
307 return -1;
308 }
309
310 buffer += n;
311 size -= n;
312 } while (0 < size);
313 }
314 else {
315 fd = _Py_open_noraise("/dev/urandom", O_RDONLY);
316 if (fd < 0) {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100317 return -1;
318 }
319
Victor Stinner6974cf22016-08-16 18:46:38 +0200320 while (0 < size)
321 {
322 do {
323 n = read(fd, buffer, (size_t)size);
324 } while (n < 0 && errno == EINTR);
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100325
Victor Stinner6974cf22016-08-16 18:46:38 +0200326 if (n <= 0) {
327 /* stop on error or if read(size) returned 0 */
Victor Stinner3ee933f2016-08-16 18:27:44 +0200328 close(fd);
Victor Stinner6974cf22016-08-16 18:46:38 +0200329 return -1;
330 }
331
332 buffer += n;
333 size -= n;
334 }
335 close(fd);
336 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100337 return 0;
338}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200339
340static void
341dev_urandom_close(void)
342{
Antoine Pitroue472aea2014-04-26 14:33:03 +0200343 if (urandom_cache.fd >= 0) {
344 close(urandom_cache.fd);
345 urandom_cache.fd = -1;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200346 }
347}
348
Victor Stinnerbae2d622015-10-01 09:47:30 +0200349#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100350
351/* Fill buffer with pseudo-random bytes generated by a linear congruent
352 generator (LCG):
353
354 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
355
356 Use bits 23..16 of x(n) to generate a byte. */
357static void
358lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
359{
360 size_t index;
361 unsigned int x;
362
363 x = x0;
364 for (index=0; index < size; index++) {
365 x *= 214013;
366 x += 2531011;
367 /* modulo 2 ^ (8 * sizeof(int)) */
368 buffer[index] = (x >> 16) & 0xff;
369 }
370}
371
Victor Stinner4bad3b62016-08-16 15:23:58 +0200372/* If raise is zero:
Victor Stinner6974cf22016-08-16 18:46:38 +0200373 - Don't raise exceptions on error
374 - Don't call PyErr_CheckSignals() on EINTR (retry directly the interrupted
375 syscall)
376 - Don't release the GIL to call syscalls. */
Victor Stinner4bad3b62016-08-16 15:23:58 +0200377static int
Victor Stinnere66987e2016-09-06 16:33:52 -0700378pyurandom(void *buffer, Py_ssize_t size, int blocking, int raise)
Victor Stinner4bad3b62016-08-16 15:23:58 +0200379{
380 if (size < 0) {
381 if (raise) {
382 PyErr_Format(PyExc_ValueError,
383 "negative argument not allowed");
384 }
385 return -1;
386 }
387
388 if (size == 0) {
389 return 0;
390 }
391
392#ifdef MS_WINDOWS
393 return win32_urandom((unsigned char *)buffer, size, raise);
394#elif defined(PY_GETENTROPY)
395 return py_getentropy(buffer, size, raise);
396#else
Victor Stinnere66987e2016-09-06 16:33:52 -0700397 return dev_urandom(buffer, size, blocking, raise);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200398#endif
399}
400
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200401/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka56a6d852014-12-01 18:28:43 +0200402 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200403 except long living private keys for asymmetric encryption.
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100404
Victor Stinnere66987e2016-09-06 16:33:52 -0700405 On Linux 3.17 and newer, the getrandom() syscall is used in blocking mode:
406 block until the system urandom entropy pool is initialized (128 bits are
407 collected by the kernel).
408
409 Return 0 on success. Raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100410int
411_PyOS_URandom(void *buffer, Py_ssize_t size)
412{
Victor Stinnere66987e2016-09-06 16:33:52 -0700413 return pyurandom(buffer, size, 1, 1);
414}
415
416/* Fill buffer with size pseudo-random bytes from the operating system random
417 number generator (RNG). It is not suitable for cryptographic purpose.
418
419 On Linux 3.17 and newer (when getrandom() syscall is used), if the system
420 urandom is not initialized yet, the function returns "weak" entropy read
421 from /dev/urandom.
422
423 Return 0 on success. Raise an exception and return -1 on error. */
424int
425_PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
426{
427 return pyurandom(buffer, size, 0, 1);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100428}
429
430void
431_PyRandom_Init(void)
432{
433 char *env;
Christian Heimes985ecdc2013-11-20 11:46:18 +0100434 unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500435 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200436 Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100437
Benjamin Peterson69e97272012-02-21 11:08:50 -0500438 if (_Py_HashSecret_Initialized)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100439 return;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500440 _Py_HashSecret_Initialized = 1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100441
442 /*
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100443 Hash randomization is enabled. Generate a per-process secret,
444 using PYTHONHASHSEED if provided.
445 */
446
447 env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl12897d72012-02-20 23:49:29 +0100448 if (env && *env != '\0' && strcmp(env, "random") != 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100449 char *endptr = env;
450 unsigned long seed;
451 seed = strtoul(env, &endptr, 10);
452 if (*endptr != '\0'
453 || seed > 4294967295UL
454 || (errno == ERANGE && seed == ULONG_MAX))
455 {
456 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
457 "in range [0; 4294967295]");
458 }
459 if (seed == 0) {
460 /* disable the randomized hash */
461 memset(secret, 0, secret_size);
462 }
463 else {
Christian Heimes985ecdc2013-11-20 11:46:18 +0100464 lcg_urandom(seed, secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100465 }
466 }
467 else {
Victor Stinner4bad3b62016-08-16 15:23:58 +0200468 int res;
469
470 /* _PyRandom_Init() is called very early in the Python initialization
Victor Stinnere66987e2016-09-06 16:33:52 -0700471 and so exceptions cannot be used (use raise=0).
472
473 _PyRandom_Init() must not block Python initialization: call
474 pyurandom() is non-blocking mode (blocking=0): see the PEP 524. */
475 res = pyurandom(secret, secret_size, 0, 0);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200476 if (res < 0) {
477 Py_FatalError("failed to get random numbers to initialize Python");
478 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100479 }
480}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200481
482void
483_PyRandom_Fini(void)
484{
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200485#ifdef MS_WINDOWS
486 if (hCryptProv) {
Tim Goldenb8ac3e12014-05-06 13:29:45 +0100487 CryptReleaseContext(hCryptProv, 0);
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200488 hCryptProv = 0;
489 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200490#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100491 /* nothing to clean */
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200492#else
Antoine Pitrou4879a962013-08-31 00:26:02 +0200493 dev_urandom_close();
494#endif
495}